2012-11-20 19:00:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
|
|
|
|
|
|
|
#include <float.h>
|
2012-11-20 19:00:28 +00:00
|
|
|
|
|
|
|
#define STROKE_WIDTH SkIntToScalar(20)
|
|
|
|
|
2012-11-20 21:32:30 +00:00
|
|
|
static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
|
|
|
|
SkPaint::Join join, int doFill) {
|
2012-11-20 19:00:28 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-11-20 21:32:30 +00:00
|
|
|
paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
|
2012-11-20 19:00:28 +00:00
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
paint.setColor(SK_ColorGRAY);
|
2012-11-20 19:00:28 +00:00
|
|
|
paint.setStrokeWidth(STROKE_WIDTH);
|
|
|
|
paint.setStrokeJoin(join);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
|
2012-11-20 21:32:30 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-11-20 19:00:28 +00:00
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
|
|
|
paint.setStrokeWidth(3);
|
|
|
|
paint.setStrokeJoin(SkPaint::kMiter_Join);
|
|
|
|
int n = path.countPoints();
|
|
|
|
SkAutoTArray<SkPoint> points(n);
|
|
|
|
path.getPoints(points.get(), n);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test calling SkStroker for rectangles. Cases to cover:
|
|
|
|
*
|
|
|
|
* geometry: normal, small (smaller than stroke-width), empty, inverted
|
|
|
|
* joint-type for the corners
|
|
|
|
*/
|
|
|
|
class StrokeRectGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
StrokeRectGM() {}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-11-20 19:00:28 +00:00
|
|
|
return SkString("strokerect");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2016-07-06 13:30:36 +00:00
|
|
|
return SkISize::Make(1400, 740);
|
2012-11-20 19:00:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-11-20 19:00:28 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(STROKE_WIDTH);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkPaint::Join gJoins[] = {
|
2012-11-20 19:00:28 +00:00
|
|
|
SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar W = 80;
|
|
|
|
constexpr SkScalar H = 80;
|
|
|
|
constexpr SkRect gRects[] = {
|
2012-11-20 19:00:28 +00:00
|
|
|
{ 0, 0, W, H },
|
|
|
|
{ W, 0, 0, H },
|
|
|
|
{ 0, H, W, 0 },
|
|
|
|
{ 0, 0, STROKE_WIDTH, H },
|
|
|
|
{ 0, 0, W, STROKE_WIDTH },
|
|
|
|
{ 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
|
|
|
|
{ 0, 0, W, 0 },
|
|
|
|
{ 0, 0, 0, H },
|
|
|
|
{ 0, 0, 0, 0 },
|
2015-12-22 15:50:16 +00:00
|
|
|
{ 0, 0, W, FLT_EPSILON },
|
|
|
|
{ 0, 0, FLT_EPSILON, H },
|
|
|
|
{ 0, 0, FLT_EPSILON, FLT_EPSILON },
|
2012-11-20 19:00:28 +00:00
|
|
|
};
|
|
|
|
|
2012-11-20 21:32:30 +00:00
|
|
|
for (int doFill = 0; doFill <= 1; ++doFill) {
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) {
|
|
|
|
SkPaint::Join join = gJoins[i];
|
|
|
|
paint.setStrokeJoin(join);
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) {
|
|
|
|
const SkRect& r = gRects[j];
|
2012-11-20 19:00:28 +00:00
|
|
|
|
2012-11-20 21:32:30 +00:00
|
|
|
SkPath path, fillPath;
|
|
|
|
path.addRect(r);
|
|
|
|
paint.getFillPath(path, &fillPath);
|
|
|
|
draw_path(canvas, fillPath, r, join, doFill);
|
2012-11-20 19:00:28 +00:00
|
|
|
|
2012-11-20 21:32:30 +00:00
|
|
|
canvas->translate(W + 2 * STROKE_WIDTH, 0);
|
|
|
|
}
|
|
|
|
acr.restore();
|
|
|
|
canvas->translate(0, H + 2 * STROKE_WIDTH);
|
2012-11-20 19:00:28 +00:00
|
|
|
}
|
2012-11-20 21:32:30 +00:00
|
|
|
paint.setStyle(SkPaint::kStrokeAndFill_Style);
|
2012-11-20 19:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2012-11-20 19:00:28 +00:00
|
|
|
};
|
2016-06-10 20:21:07 +00:00
|
|
|
DEF_GM(return new StrokeRectGM;)
|
2012-11-20 19:00:28 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-10 20:21:07 +00:00
|
|
|
/*
|
|
|
|
* Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is
|
|
|
|
* non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408
|
|
|
|
*/
|
|
|
|
DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(6);
|
|
|
|
|
|
|
|
canvas->scale(10, 1);
|
|
|
|
SkRect r = SkRect::MakeXYWH(5, 20, 10, 10);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|