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