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