SamplePath.cpp revision ae933ce0ea5fd9d21cb6ef2cee7e729d32690aac
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
9#include "SampleCode.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#include "SkPath.h"
16#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
19#include "SkXfermode.h"
20#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkParsePath.h"
23#include "SkTime.h"
24#include "SkTypeface.h"
25
26#include "SkGeometry.h"
27
28// http://code.google.com/p/skia/issues/detail?id=32
29static void test_cubic() {
30    SkPoint src[4] = {
31        { 556.25000f, 523.03003f },
32        { 556.23999f, 522.96002f },
33        { 556.21997f, 522.89001f },
34        { 556.21997f, 522.82001f }
35    };
36    SkPoint dst[11];
37    dst[10].set(42, -42);   // one past the end, that we don't clobber these
38    SkScalar tval[] = { 0.33333334f, 0.99999994f };
39
40    SkChopCubicAt(src, dst, tval, 2);
41
42#if 0
43    for (int i = 0; i < 11; i++) {
44        SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
45    }
46#endif
47}
48
49static void test_cubic2() {
50    const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
51    SkPath path;
52    SkParsePath::FromSVGString(str, &path);
53
54    {
55#ifdef SK_BUILD_FOR_WIN
56        // windows doesn't have strtof
57        float x = (float)strtod("9.94099e+07", NULL);
58#else
59        float x = strtof("9.94099e+07", NULL);
60#endif
61        int ix = (int)x;
62        int fx = (int)(x * 65536);
63        int ffx = SkScalarToFixed(x);
64        printf("%g %x %x %x\n", x, ix, fx, ffx);
65
66        SkRect r = path.getBounds();
67        SkIRect ir;
68        r.round(&ir);
69        printf("[%g %g %g %g] [%x %x %x %x]\n",
70               SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
71               SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
72               ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
73    }
74
75    SkBitmap bitmap;
76    bitmap.setConfig(SkBitmap::kARGB_8888_Config, 300, 200);
77    bitmap.allocPixels();
78
79    SkCanvas canvas(bitmap);
80    SkPaint paint;
81    paint.setAntiAlias(true);
82    canvas.drawPath(path, paint);
83}
84
85class PathView : public SampleView {
86public:
87    int fDStroke, fStroke, fMinStroke, fMaxStroke;
88    SkPath fPath[6];
89    bool fShowHairline;
90    bool fOnce;
91
92    PathView() {
93        fOnce = false;
94    }
95
96    void init() {
97        if (fOnce) {
98            return;
99        }
100        fOnce = true;
101
102        test_cubic();
103        test_cubic2();
104
105        fShowHairline = false;
106
107        fDStroke = 1;
108        fStroke = 10;
109        fMinStroke = 10;
110        fMaxStroke = 180;
111
112        const int V = 85;
113
114        fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
115        fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
116        fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
117
118        fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
119        fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
120        fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
121
122        fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
123        fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
124        fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
125
126        fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
127        fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
128        fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
129
130        fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
131        fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
132        fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
133
134        fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
135        fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
136        fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50));
137
138        this->setBGColor(0xFFDDDDDD);
139    }
140
141    void nextStroke() {
142        fStroke += fDStroke;
143        if (fStroke > fMaxStroke || fStroke < fMinStroke)
144            fDStroke = -fDStroke;
145    }
146
147protected:
148    // overrides from SkEventSink
149    virtual bool onQuery(SkEvent* evt) {
150        if (SampleCode::TitleQ(*evt)) {
151            SampleCode::TitleR(evt, "Paths");
152            return true;
153        }
154        return this->INHERITED::onQuery(evt);
155    }
156
157    void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
158        SkPaint paint;
159
160        paint.setAntiAlias(true);
161        paint.setStyle(SkPaint::kStroke_Style);
162        paint.setStrokeJoin(j);
163        paint.setStrokeWidth(SkIntToScalar(fStroke));
164
165        if (fShowHairline) {
166            SkPath  fill;
167
168            paint.getFillPath(path, &fill);
169            paint.setStrokeWidth(0);
170            canvas->drawPath(fill, paint);
171        } else {
172            canvas->drawPath(path, paint);
173        }
174
175        paint.setColor(SK_ColorRED);
176        paint.setStrokeWidth(0);
177        canvas->drawPath(path, paint);
178    }
179
180    virtual void onDrawContent(SkCanvas* canvas) {
181        this->init();
182        canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
183
184        static const SkPaint::Join gJoins[] = {
185            SkPaint::kBevel_Join,
186            SkPaint::kMiter_Join,
187            SkPaint::kRound_Join
188        };
189
190        for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
191            canvas->save();
192            for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
193                this->drawPath(canvas, fPath[j], gJoins[i]);
194                canvas->translate(SkIntToScalar(200), 0);
195            }
196            canvas->restore();
197
198            canvas->translate(0, SkIntToScalar(200));
199        }
200
201        this->nextStroke();
202        this->inval(NULL);
203    }
204
205    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
206        fShowHairline = !fShowHairline;
207        this->inval(NULL);
208        return this->INHERITED::onFindClickHandler(x, y);
209    }
210
211private:
212    typedef SampleView INHERITED;
213};
214
215//////////////////////////////////////////////////////////////////////////////
216
217static SkView* MyFactory() { return new PathView; }
218static SkViewRegister reg(MyFactory);
219
220