SamplePath.cpp revision 84ef10f8f91a48936db6e6742b4203fdc6a91b7a
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
91	PathView() {
92        test_cubic();
93        test_cubic2();
94
95        fShowHairline = false;
96
97        fDStroke = 1;
98        fStroke = 10;
99        fMinStroke = 10;
100        fMaxStroke = 180;
101
102        const int V = 85;
103
104        fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
105        fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
106        fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
107
108        fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
109        fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
110        fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
111
112        fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
113        fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
114        fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
115
116        fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
117        fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
118        fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
119
120        fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
121        fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
122        fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
123
124        fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
125        fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
126        fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50));
127
128        this->setBGColor(0xFFDDDDDD);
129    }
130
131    void nextStroke() {
132        fStroke += fDStroke;
133        if (fStroke > fMaxStroke || fStroke < fMinStroke)
134            fDStroke = -fDStroke;
135    }
136
137protected:
138    // overrides from SkEventSink
139    virtual bool onQuery(SkEvent* evt) {
140        if (SampleCode::TitleQ(*evt)) {
141            SampleCode::TitleR(evt, "Paths");
142            return true;
143        }
144        return this->INHERITED::onQuery(evt);
145    }
146
147    void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
148        SkPaint paint;
149
150        paint.setAntiAlias(true);
151        paint.setStyle(SkPaint::kStroke_Style);
152        paint.setStrokeJoin(j);
153        paint.setStrokeWidth(SkIntToScalar(fStroke));
154
155        if (fShowHairline) {
156            SkPath  fill;
157
158            paint.getFillPath(path, &fill);
159            paint.setStrokeWidth(0);
160            canvas->drawPath(fill, paint);
161        } else {
162            canvas->drawPath(path, paint);
163        }
164
165        paint.setColor(SK_ColorRED);
166        paint.setStrokeWidth(0);
167        canvas->drawPath(path, paint);
168    }
169
170    virtual void onDrawContent(SkCanvas* canvas) {
171        canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
172
173        static const SkPaint::Join gJoins[] = {
174            SkPaint::kBevel_Join,
175            SkPaint::kMiter_Join,
176            SkPaint::kRound_Join
177        };
178
179        for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
180            canvas->save();
181            for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
182                this->drawPath(canvas, fPath[j], gJoins[i]);
183                canvas->translate(SkIntToScalar(200), 0);
184            }
185            canvas->restore();
186
187            canvas->translate(0, SkIntToScalar(200));
188        }
189
190        this->nextStroke();
191        this->inval(NULL);
192    }
193
194    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
195        fShowHairline = !fShowHairline;
196        this->inval(NULL);
197        return this->INHERITED::onFindClickHandler(x, y);
198    }
199
200private:
201    typedef SampleView INHERITED;
202};
203
204//////////////////////////////////////////////////////////////////////////////
205
206static SkView* MyFactory() { return new PathView; }
207static SkViewRegister reg(MyFactory);
208
209