SampleStrokePath.cpp revision 4d5c26de0a24f86c37c1da8b0e30d11a550ea67b
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkParsePath.h"
11#include "SkPath.h"
12#include "SkRandom.h"
13#include "SkView.h"
14
15#include "SkBlurMaskFilter.h"
16
17static 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#include "SkBlurMask.h"
45static void test_blur() {
46    uint8_t cell[9];
47    memset(cell, 0xFF, sizeof(cell));
48    SkMask src;
49    src.fImage = cell;
50    src.fFormat = SkMask::kA8_Format;
51    SkMask dst;
52
53    for (int y = 1; y <= 3; y++) {
54        for (int x = 1; x <= 3; x++) {
55            src.fBounds.set(0, 0, x, y);
56            src.fRowBytes = src.fBounds.width();
57
58            SkScalar radius = 1.f;
59
60            printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
61                   src.fBounds.fRight, src.fBounds.fBottom, radius);
62
63            SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
64            uint8_t* dstPtr = dst.fImage;
65
66            for (int y = 0; y < dst.fBounds.height(); y++) {
67                for (int x = 0; x < dst.fBounds.width(); x++) {
68                    printf(" %02X", dstPtr[x]);
69                }
70                printf("\n");
71                dstPtr += dst.fRowBytes;
72            }
73        }
74    }
75}
76#endif
77
78static void scale_to_width(SkPath* path, SkScalar dstWidth) {
79    const SkRect& bounds = path->getBounds();
80    SkScalar scale = dstWidth / bounds.width();
81    SkMatrix matrix;
82
83    matrix.setScale(scale, scale);
84    path->transform(matrix);
85}
86
87static const struct {
88    SkPaint::Style  fStyle;
89    SkPaint::Join   fJoin;
90    int             fStrokeWidth;
91} gRec[] = {
92    { SkPaint::kFill_Style,             SkPaint::kMiter_Join,   0 },
93    { SkPaint::kStroke_Style,           SkPaint::kMiter_Join,   0 },
94    { SkPaint::kStroke_Style,           SkPaint::kMiter_Join,   10 },
95    { SkPaint::kStrokeAndFill_Style,    SkPaint::kMiter_Join,   10 },
96};
97
98class StrokePathView : public SampleView {
99    SkScalar    fWidth;
100    SkPath      fPath;
101public:
102    StrokePathView() {
103//        test_blur();
104        fWidth = SkIntToScalar(120);
105
106#if 0
107        const char str[] =
108            "M 0, 3"
109            "C 10, -10, 30, -10, 0, 28"
110            "C -30, -10, -10, -10, 0, 3"
111            "Z";
112        SkParsePath::FromSVGString(str, &fPath);
113#else
114        fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
115        fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
116#endif
117
118        scale_to_width(&fPath, fWidth);
119        const SkRect& bounds = fPath.getBounds();
120        fPath.offset(-bounds.fLeft, -bounds.fTop);
121
122        this->setBGColor(0xFFDDDDDD);
123    }
124
125protected:
126    // overrides from SkEventSink
127    virtual bool onQuery(SkEvent* evt) {
128        if (SampleCode::TitleQ(*evt)) {
129            SampleCode::TitleR(evt, "StrokePath");
130            return true;
131        }
132        return this->INHERITED::onQuery(evt);
133    }
134
135    SkRandom rand;
136
137    void drawSet(SkCanvas* canvas, SkPaint* paint) {
138        SkAutoCanvasRestore acr(canvas, true);
139
140        for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
141            paint->setStyle(gRec[i].fStyle);
142            paint->setStrokeJoin(gRec[i].fJoin);
143            paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
144            canvas->drawPath(fPath, *paint);
145            canvas->translate(fWidth * 5 / 4, 0);
146        }
147    }
148
149    virtual void onDrawContent(SkCanvas* canvas) {
150        test_huge_stroke(canvas); return;
151        canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
152
153        SkPaint paint;
154        paint.setAntiAlias(true);
155
156        if (true) {
157            canvas->drawColor(SK_ColorBLACK);
158
159            paint.setTextSize(24);
160            paint.setColor(SK_ColorWHITE);
161            canvas->translate(10, 30);
162
163            static const SkBlurMaskFilter::BlurStyle gStyle[] = {
164                SkBlurMaskFilter::kNormal_BlurStyle,
165                SkBlurMaskFilter::kInner_BlurStyle,
166                SkBlurMaskFilter::kOuter_BlurStyle,
167                SkBlurMaskFilter::kSolid_BlurStyle,
168            };
169            for (int x = 0; x < 5; x++) {
170                SkMaskFilter* mf;
171                SkScalar radius = 4;
172                for (int y = 0; y < 10; y++) {
173                    if (x) {
174                        mf = SkBlurMaskFilter::Create(radius, gStyle[x - 1]);
175                        paint.setMaskFilter(mf)->unref();
176                    }
177                    canvas->drawText("Title Bar", 9, x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
178                    radius *= 0.75f;
179                }
180
181            }
182            return;
183        }
184
185        paint.setColor(SK_ColorBLUE);
186
187#if 1
188        SkPath p;
189        float r = rand.nextUScalar1() + 0.5f;
190        SkScalar x = 0, y = 0;
191        p.moveTo(x, y);
192#if 0
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#else
196        p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
197        p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
198#endif
199        p.close();
200        fPath = p;
201        fPath.offset(100, 0);
202#endif
203
204        fPath.setFillType(SkPath::kWinding_FillType);
205        drawSet(canvas, &paint);
206
207        canvas->translate(0, fPath.getBounds().height() * 5 / 4);
208        fPath.setFillType(SkPath::kEvenOdd_FillType);
209        drawSet(canvas, &paint);
210    }
211
212    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
213                                              unsigned modi) SK_OVERRIDE {
214        this->inval(NULL);
215        return this->INHERITED::onFindClickHandler(x, y, modi);
216    }
217private:
218    typedef SampleView INHERITED;
219};
220
221//////////////////////////////////////////////////////////////////////////////
222
223static SkView* MyFactory() { return new StrokePathView; }
224static SkViewRegister reg(MyFactory);
225
226