1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "SampleCode.h"
9 #include "SkBlurMask.h"
10 #include "SkBlurDrawLooper.h"
11 #include "SkCanvas.h"
12 #include "SkPath.h"
13 #include "SkPathMeasure.h"
14
15 #define REPEAT_COUNT 1
16
textStrokePath(SkCanvas * canvas)17 static void textStrokePath(SkCanvas* canvas) {
18 SkPaint paint;
19 SkPath path;
20 SkRect rect;
21
22 canvas->save();
23 canvas->scale(SkIntToScalar(250),SkIntToScalar(250));
24
25 rect.set(0.0f, 0.21f,
26 0.78f, 0.99f);
27
28 path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350));
29
30 paint.setAntiAlias(true);
31 paint.setStyle(SkPaint::kStroke_Style);
32 paint.setColor(0xFFFF0000);
33 paint.setTextSize(0.085f);
34 paint.setStrokeWidth(.005f);
35
36 canvas->drawPath(path, paint);
37
38 paint.setLooper(SkBlurDrawLooper::Create(SK_ColorBLACK,
39 SkBlurMask::ConvertRadiusToSigma(0.002f),
40 0.0f,
41 0.0f))->unref();
42
43 const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH";
44 size_t len = strlen(text);
45
46 canvas->drawTextOnPathHV(text, len, path, 0,
47 -0.025f, paint);
48 canvas->restore();
49 }
50
textPathMatrix(SkCanvas * canvas)51 static void textPathMatrix(SkCanvas* canvas) {
52 SkPaint paint;
53 SkPath path;
54 SkMatrix matrix;
55
56 path.moveTo(SkIntToScalar(050), SkIntToScalar(200));
57 path.quadTo(SkIntToScalar(250), SkIntToScalar(000),
58 SkIntToScalar(450), SkIntToScalar(200));
59
60 paint.setAntiAlias(true);
61
62 paint.setStyle(SkPaint::kStroke_Style);
63 canvas->drawPath(path, paint);
64 paint.setStyle(SkPaint::kFill_Style);
65 paint.setTextSize(SkIntToScalar(48));
66 paint.setTextAlign(SkPaint::kRight_Align);
67
68 const char* text = "Reflection";
69 size_t len = strlen(text);
70
71 SkPathMeasure meas(path, false);
72 SkScalar pathLen = meas.getLength();
73
74 canvas->drawTextOnPath(text, len, path, nullptr, paint);
75
76 paint.setColor(SK_ColorRED);
77 matrix.setScale(-SK_Scalar1, SK_Scalar1);
78 matrix.postTranslate(pathLen, 0);
79 canvas->drawTextOnPath(text, len, path, &matrix, paint);
80
81 paint.setColor(SK_ColorBLUE);
82 matrix.setScale(SK_Scalar1, -SK_Scalar1);
83 canvas->drawTextOnPath(text, len, path, &matrix, paint);
84
85 paint.setColor(SK_ColorGREEN);
86 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
87 matrix.postTranslate(pathLen, 0);
88 canvas->drawTextOnPath(text, len, path, &matrix, paint);
89 }
90
91 class TextOnPathView : public SampleView {
92 public:
93 SkPath fPath;
94 SkScalar fHOffset;
95
96 protected:
onOnceBeforeDraw()97 void onOnceBeforeDraw() override {
98 SkRect r;
99 r.set(SkIntToScalar(100), SkIntToScalar(100),
100 SkIntToScalar(300), SkIntToScalar(300));
101 fPath.addOval(r);
102 fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50));
103
104 fHOffset = SkIntToScalar(50);
105 }
106
107 // overrides from SkEventSink
onQuery(SkEvent * evt)108 bool onQuery(SkEvent* evt) override {
109 if (SampleCode::TitleQ(*evt)) {
110 SampleCode::TitleR(evt, "Text On Path");
111 return true;
112 }
113 return this->INHERITED::onQuery(evt);
114 }
115
onDrawContent(SkCanvas * canvas)116 void onDrawContent(SkCanvas* canvas) override {
117 SkPaint paint;
118 paint.setAntiAlias(true);
119 paint.setTextSize(SkIntToScalar(48));
120
121 const char* text = "Hamburgefons";
122 size_t len = strlen(text);
123
124 for (int j = 0; j < REPEAT_COUNT; j++) {
125 SkScalar x = fHOffset;
126
127 paint.setColor(SK_ColorBLACK);
128 canvas->drawTextOnPathHV(text, len, fPath,
129 x, paint.getTextSize()/2, paint);
130
131 paint.setColor(SK_ColorRED);
132 canvas->drawTextOnPathHV(text, len, fPath,
133 x + SkIntToScalar(50), 0, paint);
134
135 paint.setColor(SK_ColorBLUE);
136 canvas->drawTextOnPathHV(text, len, fPath,
137 x + SkIntToScalar(100), -paint.getTextSize()/2, paint);
138 }
139
140 paint.setColor(SK_ColorGREEN);
141 paint.setStyle(SkPaint::kStroke_Style);
142 canvas->drawPath(fPath, paint);
143
144 canvas->translate(SkIntToScalar(275), 0);
145 textStrokePath(canvas);
146
147 canvas->translate(SkIntToScalar(-275), SkIntToScalar(250));
148 textPathMatrix(canvas);
149
150 if (REPEAT_COUNT > 1)
151 this->inval(nullptr);
152 }
153
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)154 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
155 fHints += 1;
156 this->inval(nullptr);
157 return this->INHERITED::onFindClickHandler(x, y, modi);
158 }
159
onClick(Click * click)160 bool onClick(Click* click) override {
161 return this->INHERITED::onClick(click);
162 }
163
164 private:
165 int fHints;
166 typedef SampleView INHERITED;
167 };
168
169 //////////////////////////////////////////////////////////////////////////////
170
MyFactory()171 static SkView* MyFactory() {
172 return new TextOnPathView;
173 }
174
175 static SkViewRegister reg(MyFactory);
176