1 /*
2  * Copyright 2011 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 #include "include/core/SkCanvas.h"
8 #include "include/core/SkColorFilter.h"
9 #include "include/core/SkColorPriv.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkRegion.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkStrokeRec.h"
14 #include "include/core/SkTypeface.h"
15 #include "include/effects/SkGradientShader.h"
16 #include "include/utils/SkTextUtils.h"
17 #include "samplecode/Sample.h"
18 #include "src/core/SkReadBuffer.h"
19 #include "src/core/SkWriteBuffer.h"
20 #include "src/utils/SkUTF.h"
21 
22 #include "include/effects/SkGradientShader.h"
23 
24 #include "include/effects/Sk2DPathEffect.h"
25 
26 class Dot2DPathEffect : public Sk2DPathEffect {
27 public:
Dot2DPathEffect(SkScalar radius,const SkMatrix & matrix,SkTDArray<SkPoint> * pts)28     Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
29                     SkTDArray<SkPoint>* pts)
30     : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
31 
32     SK_FLATTENABLE_HOOKS(Dot2DPathEffect)
33 protected:
begin(const SkIRect & uvBounds,SkPath * dst) const34     void begin(const SkIRect& uvBounds, SkPath* dst) const override {
35         if (fPts) {
36             fPts->reset();
37         }
38         this->INHERITED::begin(uvBounds, dst);
39     }
40 
next(const SkPoint & loc,int u,int v,SkPath * dst) const41     virtual void next(const SkPoint& loc, int u, int v,
42                       SkPath* dst) const override {
43         if (fPts) {
44             *fPts->append() = loc;
45         }
46         dst->addCircle(loc.fX, loc.fY, fRadius);
47     }
48 
flatten(SkWriteBuffer & buffer) const49     void flatten(SkWriteBuffer& buffer) const override {
50         buffer.writeMatrix(this->getMatrix());
51         buffer.writeScalar(fRadius);
52     }
53 
54 private:
55 
56     SkScalar fRadius;
57     SkTDArray<SkPoint>* fPts;
58 
59     typedef Sk2DPathEffect INHERITED;
60 };
61 
62 // Register this path effect as deserializable before main().
63 namespace {
64     static struct Initializer {
Initializer__anon946cf3340111::Initializer65         Initializer() {
66             SK_REGISTER_FLATTENABLE(Dot2DPathEffect);
67         }
68     } initializer;
69 }
70 
71 
CreateProc(SkReadBuffer & buffer)72 sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
73     SkMatrix matrix;
74     buffer.readMatrix(&matrix);
75     return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr);
76 }
77 
78 class InverseFillPE : public SkPathEffect {
79 public:
InverseFillPE()80     InverseFillPE() {}
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec *,const SkRect *) const81     virtual bool onFilterPath(SkPath* dst, const SkPath& src,
82                               SkStrokeRec*, const SkRect*) const override {
83         *dst = src;
84         dst->setFillType(SkPathFillType::kInverseWinding);
85         return true;
86     }
87 
88 private:
89     SK_FLATTENABLE_HOOKS(InverseFillPE)
90 
91     typedef SkPathEffect INHERITED;
92 };
93 
CreateProc(SkReadBuffer & buffer)94 sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
95     return sk_make_sp<InverseFillPE>();
96 }
97 
makepe(float interp,SkTDArray<SkPoint> * pts)98 static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
99     SkMatrix    lattice;
100     SkScalar    rad = 3 + SkIntToScalar(4) * (1 - interp);
101     lattice.setScale(rad*2, rad*2, 0, 0);
102     lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
103     return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
104 }
105 
106 class TextEffectsView : public Sample {
107     sk_sp<SkTypeface> fFace;
108     SkScalar fInterp;
109     SkScalar fDx;
110 
111 public:
TextEffectsView()112     TextEffectsView() {
113         fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
114         fInterp = 0;
115         fDx = SK_Scalar1/64;
116     }
117 
118 protected:
name()119     SkString name() override { return SkString("Text Effects"); }
120 
drawBG(SkCanvas * canvas)121     void drawBG(SkCanvas* canvas) {
122         canvas->drawColor(SK_ColorWHITE);
123     }
124 
drawdots(SkCanvas * canvas,SkString s,SkScalar x,SkScalar y,const SkFont & font)125     void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkFont& font) {
126         SkTDArray<SkPoint> pts;
127         auto pe = makepe(fInterp, &pts);
128 
129         SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
130         SkPath path, dstPath;
131         SkTextUtils::GetPath(s.c_str(), s.size(), SkTextEncoding::kUTF8, x, y, font, &path);
132         pe->filterPath(&dstPath, path, &rec, nullptr);
133 
134         SkPaint paint;
135         paint.setAntiAlias(true);
136         paint.setStrokeWidth(10);
137         paint.setColor(SK_ColorRED);
138         canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint);
139     }
140 
onDrawContent(SkCanvas * canvas)141     void onDrawContent(SkCanvas* canvas) override {
142         this->drawBG(canvas);
143 
144         SkScalar x = SkIntToScalar(20);
145         SkScalar y = SkIntToScalar(300);
146 
147         SkFont font(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()), 240);
148 
149         SkString str("9");
150 
151         canvas->drawString(str, x, y, font, SkPaint());
152         drawdots(canvas, str, x, y, font);
153 
154         if (false) {
155             fInterp += fDx;
156             if (fInterp > 1) {
157                 fInterp = 1;
158                 fDx = -fDx;
159             } else if (fInterp < 0) {
160                 fInterp = 0;
161                 fDx = -fDx;
162             }
163         }
164     }
165 
166 private:
167     typedef Sample INHERITED;
168 };
169 
170 //////////////////////////////////////////////////////////////////////////////
171 
172 DEF_SAMPLE( return new TextEffectsView(); )
173