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);
142 #if 0
143 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
144
145 SkPaint paint;
146 paint.setAntiAlias(true);
147
148 if (true) {
149 canvas->drawColor(SK_ColorBLACK);
150
151 SkFont font(nullptr, 24);
152 paint.setColor(SK_ColorWHITE);
153 canvas->translate(10, 30);
154
155 static const SkBlurStyle gStyle[] = {
156 kNormal_SkBlurStyle,
157 kInner_SkBlurStyle,
158 kOuter_SkBlurStyle,
159 kSolid_SkBlurStyle,
160 };
161 for (int x = 0; x < 5; x++) {
162 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
163 for (int y = 0; y < 10; y++) {
164 if (x) {
165 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
166 }
167 canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
168 sigma *= 0.75f;
169 }
170
171 }
172 return;
173 }
174
175 paint.setColor(SK_ColorBLUE);
176
177 #if 1
178 SkPath p;
179 float r = rand.nextUScalar1() + 0.5f;
180 SkScalar x = 0, y = 0;
181 p.moveTo(x, y);
182 #if 0
183 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
184 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
185 #else
186 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
187 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
188 #endif
189 p.close();
190 fPath = p;
191 fPath.offset(100, 0);
192 #endif
193
194 fPath.setFillType(SkPathFillType::kWinding);
195 drawSet(canvas, &paint);
196
197 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
198 fPath.setFillType(SkPathFillType::kEvenOdd);
199 drawSet(canvas, &paint);
200 #endif
201 }
202
203 private:
204 using INHERITED = Sample;
205 };
206
207 //////////////////////////////////////////////////////////////////////////////
208
209 DEF_SAMPLE( return new StrokePathView(); )
210