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 "SampleCode.h"
8 #include "SkBlurMask.h"
9 #include "SkBlurMaskFilter.h"
10 #include "SkCanvas.h"
11 #include "SkParsePath.h"
12 #include "SkPath.h"
13 #include "SkRandom.h"
14 #include "SkView.h"
15
16
test_huge_stroke(SkCanvas * canvas)17 static void test_huge_stroke(SkCanvas* canvas) {
18 SkRect srcR = { 0, 0, 72000, 54000 };
19 SkRect dstR = { 0, 0, 640, 480 };
20
21 SkPath path;
22 path.moveTo(17600, 8000);
23 path.lineTo(52800, 8000);
24 path.lineTo(52800, 41600);
25 path.lineTo(17600, 41600);
26 path.close();
27
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setStrokeWidth(8000);
31 paint.setStrokeMiter(10);
32 paint.setStrokeCap(SkPaint::kButt_Cap);
33 paint.setStrokeJoin(SkPaint::kRound_Join);
34 paint.setStyle(SkPaint::kStroke_Style);
35
36 SkMatrix matrix;
37 matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
38 canvas->concat(matrix);
39
40 canvas->drawPath(path, paint);
41 }
42
43 #if 0
44 static void test_blur() {
45 uint8_t cell[9];
46 memset(cell, 0xFF, sizeof(cell));
47 SkMask src;
48 src.fImage = cell;
49 src.fFormat = SkMask::kA8_Format;
50 SkMask dst;
51
52 for (int y = 1; y <= 3; y++) {
53 for (int x = 1; x <= 3; x++) {
54 src.fBounds.set(0, 0, x, y);
55 src.fRowBytes = src.fBounds.width();
56
57 SkScalar radius = 1.f;
58
59 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
60 src.fBounds.fRight, src.fBounds.fBottom, radius);
61
62 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
63 uint8_t* dstPtr = dst.fImage;
64
65 for (int y = 0; y < dst.fBounds.height(); y++) {
66 for (int x = 0; x < dst.fBounds.width(); x++) {
67 printf(" %02X", dstPtr[x]);
68 }
69 printf("\n");
70 dstPtr += dst.fRowBytes;
71 }
72 }
73 }
74 }
75 #endif
76
scale_to_width(SkPath * path,SkScalar dstWidth)77 static void scale_to_width(SkPath* path, SkScalar dstWidth) {
78 const SkRect& bounds = path->getBounds();
79 SkScalar scale = dstWidth / bounds.width();
80 SkMatrix matrix;
81
82 matrix.setScale(scale, scale);
83 path->transform(matrix);
84 }
85
86 static const struct {
87 SkPaint::Style fStyle;
88 SkPaint::Join fJoin;
89 int fStrokeWidth;
90 } gRec[] = {
91 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
92 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
93 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
94 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
95 };
96
97 class StrokePathView : public SampleView {
98 SkScalar fWidth;
99 SkPath fPath;
100 protected:
onOnceBeforeDraw()101 void onOnceBeforeDraw() override {
102 // test_blur();
103 fWidth = SkIntToScalar(120);
104
105 #if 0
106 const char str[] =
107 "M 0, 3"
108 "C 10, -10, 30, -10, 0, 28"
109 "C -30, -10, -10, -10, 0, 3"
110 "Z";
111 SkParsePath::FromSVGString(str, &fPath);
112 #else
113 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
114 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
115 #endif
116
117 scale_to_width(&fPath, fWidth);
118 const SkRect& bounds = fPath.getBounds();
119 fPath.offset(-bounds.fLeft, -bounds.fTop);
120
121 this->setBGColor(0xFFDDDDDD);
122 }
123
124 // overrides from SkEventSink
onQuery(SkEvent * evt)125 bool onQuery(SkEvent* evt) override {
126 if (SampleCode::TitleQ(*evt)) {
127 SampleCode::TitleR(evt, "StrokePath");
128 return true;
129 }
130 return this->INHERITED::onQuery(evt);
131 }
132
133 SkRandom rand;
134
drawSet(SkCanvas * canvas,SkPaint * paint)135 void drawSet(SkCanvas* canvas, SkPaint* paint) {
136 SkAutoCanvasRestore acr(canvas, true);
137
138 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
139 paint->setStyle(gRec[i].fStyle);
140 paint->setStrokeJoin(gRec[i].fJoin);
141 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
142 canvas->drawPath(fPath, *paint);
143 canvas->translate(fWidth * 5 / 4, 0);
144 }
145 }
146
onDrawContent(SkCanvas * canvas)147 void onDrawContent(SkCanvas* canvas) override {
148 test_huge_stroke(canvas); return;
149 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
150
151 SkPaint paint;
152 paint.setAntiAlias(true);
153
154 if (true) {
155 canvas->drawColor(SK_ColorBLACK);
156
157 paint.setTextSize(24);
158 paint.setColor(SK_ColorWHITE);
159 canvas->translate(10, 30);
160
161 static const SkBlurStyle gStyle[] = {
162 kNormal_SkBlurStyle,
163 kInner_SkBlurStyle,
164 kOuter_SkBlurStyle,
165 kSolid_SkBlurStyle,
166 };
167 for (int x = 0; x < 5; x++) {
168 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
169 for (int y = 0; y < 10; y++) {
170 if (x) {
171 paint.setMaskFilter(SkBlurMaskFilter::Make(gStyle[x - 1], sigma));
172 }
173 canvas->drawString("Title Bar", x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
174 sigma *= 0.75f;
175 }
176
177 }
178 return;
179 }
180
181 paint.setColor(SK_ColorBLUE);
182
183 #if 1
184 SkPath p;
185 float r = rand.nextUScalar1() + 0.5f;
186 SkScalar x = 0, y = 0;
187 p.moveTo(x, y);
188 #if 0
189 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
190 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
191 #else
192 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
193 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
194 #endif
195 p.close();
196 fPath = p;
197 fPath.offset(100, 0);
198 #endif
199
200 fPath.setFillType(SkPath::kWinding_FillType);
201 drawSet(canvas, &paint);
202
203 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
204 fPath.setFillType(SkPath::kEvenOdd_FillType);
205 drawSet(canvas, &paint);
206 }
207
208 private:
209 typedef SampleView INHERITED;
210 };
211
212 //////////////////////////////////////////////////////////////////////////////
213
MyFactory()214 static SkView* MyFactory() { return new StrokePathView; }
215 static SkViewRegister reg(MyFactory);
216