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