2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "Sk1DPathEffect.h"
11 #include "Sk2DPathEffect.h"
12 #include "SkCornerPathEffect.h"
13 #include "SkDashPathEffect.h"
14 #include "SkDiscretePathEffect.h"
18 static void compose_pe(SkPaint* paint) {
19 SkPathEffect* pe = paint->getPathEffect();
20 sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
21 sk_sp<SkPathEffect> compose;
23 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
27 paint->setPathEffect(compose);
30 static void hair_pe(SkPaint* paint) {
31 paint->setStrokeWidth(0);
34 static void hair2_pe(SkPaint* paint) {
35 paint->setStrokeWidth(0);
39 static void stroke_pe(SkPaint* paint) {
40 paint->setStrokeWidth(12);
44 static void dash_pe(SkPaint* paint) {
45 SkScalar inter[] = { 20, 10, 10, 10 };
46 paint->setStrokeWidth(12);
47 paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0));
51 constexpr int gXY[] = {
52 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
55 static void scale(SkPath* path, SkScalar scale) {
57 m.setScale(scale, scale);
61 static void one_d_pe(SkPaint* paint) {
63 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
64 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
65 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
67 path.offset(SkIntToScalar(-6), 0);
70 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
71 SkPath1DPathEffect::kRotate_Style));
75 typedef void (*PE_Proc)(SkPaint*);
76 constexpr PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
78 static void fill_pe(SkPaint* paint) {
79 paint->setStyle(SkPaint::kFill_Style);
80 paint->setPathEffect(nullptr);
83 static void discrete_pe(SkPaint* paint) {
84 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
87 static sk_sp<SkPathEffect> MakeTileEffect() {
89 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
92 path.addCircle(0, 0, SkIntToScalar(5));
94 return SkPath2DPathEffect::Make(m, path);
97 static void tile_pe(SkPaint* paint) {
98 paint->setPathEffect(MakeTileEffect());
101 constexpr PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
103 class PathEffectGM : public GM {
109 SkString onShortName() override {
110 return SkString("patheffect");
113 SkISize onISize() override { return SkISize::Make(800, 600); }
115 void onDraw(SkCanvas* canvas) override {
117 paint.setAntiAlias(true);
118 paint.setStyle(SkPaint::kStroke_Style);
122 path.lineTo(70, 120);
123 path.lineTo(120, 30);
124 path.lineTo(170, 80);
125 path.lineTo(240, 50);
129 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
131 canvas->drawPath(path, paint);
132 canvas->translate(0, 75);
137 SkRect r = { 0, 0, 250, 120 };
138 path.addOval(r, SkPath::kCW_Direction);
140 path.addRect(r, SkPath::kCCW_Direction);
142 canvas->translate(320, 20);
143 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
145 canvas->drawPath(path, paint);
146 canvas->translate(0, 160);
149 SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60);
150 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
152 p.setAntiAlias(true);
153 p.setStyle(SkPaint::kFill_Style);
155 canvas->drawIRect(rect, p);
156 canvas->translate(75, 0);
161 typedef GM INHERITED;
164 //////////////////////////////////////////////////////////////////////////////
166 static GM* PathEffectFactory(void*) { return new PathEffectGM; }
167 static GMRegistry regPathEffect(PathEffectFactory);