• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "gm.h"
9 #include "SkAnimTimer.h"
10 #include "SkCanvas.h"
11 #include "SkDashPathEffect.h"
12 #include "SkPath.h"
13 #include "SkParsePath.h"
14 #include "SkTArray.h"
15 #include "SkTrimPathEffect.h"
16 
17 #include <utility>
18 
19 /*
20  *  Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
21  */
flower(SkCanvas * canvas,const SkPath & path,SkScalar intervals[2],SkPaint::Join join)22 static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
23                    SkPaint::Join join) {
24     SkPaint paint;
25     paint.setAntiAlias(true);
26     paint.setStyle(SkPaint::kStroke_Style);
27     paint.setStrokeJoin(join);
28     paint.setStrokeWidth(42);
29     canvas->drawPath(path, paint);
30 
31     paint.setColor(SK_ColorRED);
32     paint.setStrokeWidth(21);
33     paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
34     canvas->drawPath(path, paint);
35 
36     paint.setColor(SK_ColorGREEN);
37     paint.setPathEffect(nullptr);
38     paint.setStrokeWidth(0);
39     canvas->drawPath(path, paint);
40 }
41 
42 DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
43         SkPath path;
44         const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
45         "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
46         "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
47         "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
48         "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
49         "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
50         "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
51         "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
52         "C 283,231 344,195 338,98";
53 
54         SkParsePath::FromSVGString(d, &path);
55             canvas->translate(-35.f, -55.f);
56         for (int x = 0; x < 2; ++x) {
57             for (int y = 0; y < 2; ++y) {
58                 canvas->save();
59                 canvas->translate(x * 430.f, y * 355.f);
60                 SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
61                 flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
62                 canvas->restore();
63             }
64         }
65 }
66 
67 class TrimGM : public skiagm::GM {
68 public:
TrimGM()69     TrimGM() {
70         SkAssertResult(SkParsePath::FromSVGString(
71             "M   0,100 C  10, 50 190, 50 200,100"
72             "M 200,100 C 210,150 390,150 400,100"
73             "M 400,100 C 390, 50 210, 50 200,100"
74             "M 200,100 C 190,150  10,150   0,100",
75             &fPaths.push_back()));
76 
77         SkAssertResult(SkParsePath::FromSVGString(
78             "M   0, 75 L 200, 75"
79             "M 200, 91 L 200, 91"
80             "M 200,108 L 200,108"
81             "M 200,125 L 400,125",
82             &fPaths.push_back()));
83 
84         SkAssertResult(SkParsePath::FromSVGString(
85             "M   0,100 L  50, 50"
86             "M  50, 50 L 150,150"
87             "M 150,150 L 250, 50"
88             "M 250, 50 L 350,150"
89             "M 350,150 L 400,100",
90             &fPaths.push_back()));
91 
92     }
93 
94 protected:
onShortName()95     SkString onShortName() override { return SkString("trimpatheffect"); }
96 
onISize()97     SkISize onISize() override {
98         return SkISize::Make(1400, 1000);
99     }
100 
onDraw(SkCanvas * canvas)101     void onDraw(SkCanvas* canvas) override {
102         static constexpr SkSize kCellSize = { 440, 150 };
103         static constexpr SkScalar kOffsets[][2] = {
104             { -0.33f, -0.66f },
105             {  0    ,  1    },
106             {  0    ,  0.25f},
107             {  0.25f,  0.75f},
108             {  0.75f,  1    },
109             {  1    ,  0.75f},
110         };
111 
112         SkPaint hairlinePaint;
113         hairlinePaint.setAntiAlias(true);
114         hairlinePaint.setStyle(SkPaint::kStroke_Style);
115         hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
116         hairlinePaint.setStrokeWidth(2);
117         SkPaint normalPaint = hairlinePaint;
118         normalPaint.setStrokeWidth(10);
119         normalPaint.setColor(0x8000ff00);
120         SkPaint invertedPaint = normalPaint;
121         invertedPaint.setColor(0x80ff0000);
122 
123         for (const auto& offset : kOffsets) {
124             auto start = offset[0] + fOffset,
125                  stop  = offset[1] + fOffset;
126 
127             auto normalMode   = SkTrimPathEffect::Mode::kNormal,
128                  invertedMode = SkTrimPathEffect::Mode::kInverted;
129             if (fOffset) {
130                 start -= SkScalarFloorToScalar(start);
131                 stop  -= SkScalarFloorToScalar(stop);
132                 if (start > stop) {
133                     using std::swap;
134                     swap(start, stop);
135                     swap(normalMode, invertedMode);
136                 }
137             }
138 
139             normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
140             invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
141 
142             {
143                 SkAutoCanvasRestore acr(canvas, true);
144                 for (const auto& path : fPaths) {
145                     canvas->drawPath(path, normalPaint);
146                     canvas->drawPath(path, invertedPaint);
147                     canvas->drawPath(path, hairlinePaint);
148                     canvas->translate(kCellSize.width(), 0);
149                 }
150             }
151 
152             canvas->translate(0, kCellSize.height());
153         }
154     }
155 
onAnimate(const SkAnimTimer & t)156     bool onAnimate(const SkAnimTimer& t) override {
157         fOffset = t.msec() / 2000.0f;
158         fOffset -= floorf(fOffset);
159         return true;
160     }
161 
162 private:
163     SkTArray<SkPath> fPaths;
164     SkScalar         fOffset = 0;
165 
166     typedef skiagm::GM INHERITED;
167 };
168 DEF_GM(return new TrimGM;)
169 
170