2013-07-31 22:54:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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/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/SkPathEffect.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkDashPathEffect.h"
|
2013-07-31 22:54:31 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2013-07-31 22:54:31 +00:00
|
|
|
static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
|
2020-08-02 22:56:34 +00:00
|
|
|
return SkPath::Rect(SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w));
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
|
2020-08-02 22:56:34 +00:00
|
|
|
return SkPath::Rect(SkRect::MakeXYWH(cx - l / 2, cy, l, 0));
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
|
2020-08-02 22:56:34 +00:00
|
|
|
return SkPath::Circle(cx, cy, d/2, SkPathDirection::kCW);
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
|
2020-08-02 22:56:34 +00:00
|
|
|
return SkPath::Line({cx - l / 2, cy}, {cx + l / 2, cy});
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
namespace {
|
2016-06-10 19:09:59 +00:00
|
|
|
struct Style {
|
|
|
|
Style(SkPaint::Style paintStyle, sk_sp<SkPathEffect> pe = sk_sp<SkPathEffect>())
|
|
|
|
: fPaintStyle(paintStyle)
|
|
|
|
, fPathEffect(std::move(pe)) {}
|
|
|
|
SkPaint::Style fPaintStyle;
|
|
|
|
sk_sp<SkPathEffect> fPathEffect;
|
2013-07-31 22:54:31 +00:00
|
|
|
};
|
2016-06-10 19:09:59 +00:00
|
|
|
|
|
|
|
sk_sp<SkPathEffect> make_dash() {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kIntervals[] = { 4.f, 3.f };
|
2016-06-10 19:09:59 +00:00
|
|
|
return SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Style styles[] {
|
|
|
|
{SkPaint::kStroke_Style},
|
|
|
|
{SkPaint::kStrokeAndFill_Style},
|
|
|
|
{SkPaint::kFill_Style},
|
|
|
|
{SkPaint::kStroke_Style, make_dash()},
|
|
|
|
};
|
|
|
|
|
2013-07-31 22:54:31 +00:00
|
|
|
SkScalar pathSizes[] = {
|
|
|
|
40,
|
|
|
|
10,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
SkScalar strokeWidths[] = {
|
|
|
|
10,
|
|
|
|
0
|
|
|
|
};
|
2017-10-17 21:33:44 +00:00
|
|
|
SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = {
|
2013-07-31 22:54:31 +00:00
|
|
|
generate_square,
|
|
|
|
generate_rect_line,
|
|
|
|
generate_circle,
|
|
|
|
generate_line
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkScalar slideWidth = 90, slideHeight = 90;
|
|
|
|
const SkScalar slideBoundary = 5;
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
} // namespace
|
2013-07-31 22:54:31 +00:00
|
|
|
|
2016-06-10 19:09:59 +00:00
|
|
|
DEF_SIMPLE_GM(inverse_paths, canvas, 800, 1200) {
|
|
|
|
SkScalar cx = slideWidth / 2 + slideBoundary;
|
|
|
|
SkScalar cy = slideHeight / 2 + slideBoundary;
|
|
|
|
SkScalar dx = slideWidth + 2 * slideBoundary;
|
|
|
|
SkScalar dy = slideHeight + 2 * slideBoundary;
|
|
|
|
|
|
|
|
SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary,
|
|
|
|
slideBoundary + slideWidth,
|
|
|
|
slideBoundary + slideHeight);
|
|
|
|
SkPaint clipPaint;
|
|
|
|
clipPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
clipPaint.setStrokeWidth(SkIntToScalar(2));
|
|
|
|
|
|
|
|
SkPaint outlinePaint;
|
|
|
|
outlinePaint.setColor(0x40000000);
|
|
|
|
outlinePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
outlinePaint.setStrokeWidth(SkIntToScalar(0));
|
|
|
|
|
|
|
|
for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles);
|
|
|
|
styleIndex++) {
|
|
|
|
for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes);
|
|
|
|
sizeIndex++) {
|
|
|
|
SkScalar size = pathSizes[sizeIndex];
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
for (size_t widthIndex = 0;
|
|
|
|
widthIndex < SK_ARRAY_COUNT(strokeWidths);
|
|
|
|
widthIndex++) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xff007000);
|
|
|
|
paint.setStrokeWidth(strokeWidths[widthIndex]);
|
|
|
|
paint.setStyle(styles[styleIndex].fPaintStyle);
|
|
|
|
paint.setPathEffect(styles[styleIndex].fPathEffect);
|
|
|
|
|
|
|
|
for (size_t pathIndex = 0;
|
|
|
|
pathIndex < SK_ARRAY_COUNT(paths);
|
|
|
|
pathIndex++) {
|
|
|
|
canvas->drawRect(clipRect, clipPaint);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipRect);
|
|
|
|
|
|
|
|
SkPath path = paths[pathIndex](cx, cy, size);
|
2019-11-26 17:17:17 +00:00
|
|
|
path.setFillType(SkPathFillType::kInverseWinding);
|
2016-06-10 19:09:59 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
2019-11-26 17:17:17 +00:00
|
|
|
path.setFillType(SkPathFillType::kWinding);
|
2016-06-10 19:09:59 +00:00
|
|
|
canvas->drawPath(path, outlinePaint);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-10 19:09:59 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dy);
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|
2016-06-10 19:09:59 +00:00
|
|
|
}
|
2013-07-31 22:54:31 +00:00
|
|
|
}
|