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