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