blob: cd45ed95e046e24078676e8ae0e02ea4f01ff500 [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@google.com0faac1e2011-05-11 05:58:58 +000077class PathView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078public:
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));
reed@google.com0faac1e2011-05-11 05:58:58 +0000119
120 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 }
122
reed@google.com0faac1e2011-05-11 05:58:58 +0000123 void nextStroke() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 fStroke += fDStroke;
125 if (fStroke > fMaxStroke || fStroke < fMinStroke)
126 fDStroke = -fDStroke;
127 }
128
129protected:
130 // overrides from SkEventSink
reed@google.com0faac1e2011-05-11 05:58:58 +0000131 virtual bool onQuery(SkEvent* evt) {
132 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SampleCode::TitleR(evt, "Paths");
134 return true;
135 }
136 return this->INHERITED::onQuery(evt);
137 }
138
reed@google.com0faac1e2011-05-11 05:58:58 +0000139 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SkPaint paint;
141
142 paint.setAntiAlias(true);
143 paint.setStyle(SkPaint::kStroke_Style);
144 paint.setStrokeJoin(j);
145 paint.setStrokeWidth(SkIntToScalar(fStroke));
146
reed@google.com0faac1e2011-05-11 05:58:58 +0000147 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 SkPath fill;
149
150 paint.getFillPath(path, &fill);
151 paint.setStrokeWidth(0);
152 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000153 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000155 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
157 paint.setColor(SK_ColorRED);
158 paint.setStrokeWidth(0);
159 canvas->drawPath(path, paint);
160 }
161
reed@google.com0faac1e2011-05-11 05:58:58 +0000162 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
164
165 static const SkPaint::Join gJoins[] = {
166 SkPaint::kBevel_Join,
167 SkPaint::kMiter_Join,
168 SkPaint::kRound_Join
169 };
170
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000171 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000173 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 this->drawPath(canvas, fPath[j], gJoins[i]);
175 canvas->translate(SkIntToScalar(200), 0);
176 }
177 canvas->restore();
178
179 canvas->translate(0, SkIntToScalar(200));
180 }
181
182 this->nextStroke();
183 this->inval(NULL);
184 }
185
reed@google.com0faac1e2011-05-11 05:58:58 +0000186 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 fShowHairline = !fShowHairline;
188 this->inval(NULL);
189 return this->INHERITED::onFindClickHandler(x, y);
190 }
191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000193 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194};
195
196//////////////////////////////////////////////////////////////////////////////
197
198static SkView* MyFactory() { return new PathView; }
199static SkViewRegister reg(MyFactory);
200