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 "samplecode/Sample.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkMaskFilter.h"
13 #include "include/core/SkPath.h"
14 #include "include/utils/SkParsePath.h"
15 #include "include/utils/SkRandom.h"
16 #include "src/core/SkBlurMask.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 canvas->concat(SkMatrix::RectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit));
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 Sample {
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), SkPathDirection::kCW);
114 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPathDirection::kCW);
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
name()124 SkString name() override { return SkString("StrokePath"); }
125
126 SkRandom rand;
127
drawSet(SkCanvas * canvas,SkPaint * paint)128 void drawSet(SkCanvas* canvas, SkPaint* paint) {
129 SkAutoCanvasRestore acr(canvas, true);
130
131 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
132 paint->setStyle(gRec[i].fStyle);
133 paint->setStrokeJoin(gRec[i].fJoin);
134 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
135 canvas->drawPath(fPath, *paint);
136 canvas->translate(fWidth * 5 / 4, 0);
137 }
138 }
139
onDrawContent(SkCanvas * canvas)140 void onDrawContent(SkCanvas* canvas) override {
141 test_huge_stroke(canvas); return;
142 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
143
144 SkPaint paint;
145 paint.setAntiAlias(true);
146
147 if (true) {
148 canvas->drawColor(SK_ColorBLACK);
149
150 SkFont font(nullptr, 24);
151 paint.setColor(SK_ColorWHITE);
152 canvas->translate(10, 30);
153
154 static const SkBlurStyle gStyle[] = {
155 kNormal_SkBlurStyle,
156 kInner_SkBlurStyle,
157 kOuter_SkBlurStyle,
158 kSolid_SkBlurStyle,
159 };
160 for (int x = 0; x < 5; x++) {
161 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
162 for (int y = 0; y < 10; y++) {
163 if (x) {
164 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
165 }
166 canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
167 sigma *= 0.75f;
168 }
169
170 }
171 return;
172 }
173
174 paint.setColor(SK_ColorBLUE);
175
176 #if 1
177 SkPath p;
178 float r = rand.nextUScalar1() + 0.5f;
179 SkScalar x = 0, y = 0;
180 p.moveTo(x, y);
181 #if 0
182 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
183 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
184 #else
185 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
186 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
187 #endif
188 p.close();
189 fPath = p;
190 fPath.offset(100, 0);
191 #endif
192
193 fPath.setFillType(SkPathFillType::kWinding);
194 drawSet(canvas, &paint);
195
196 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
197 fPath.setFillType(SkPathFillType::kEvenOdd);
198 drawSet(canvas, &paint);
199 }
200
201 private:
202 using INHERITED = Sample;
203 };
204
205 //////////////////////////////////////////////////////////////////////////////
206
207 DEF_SAMPLE( return new StrokePathView(); )
208