blob: e63c513064cbad8bbf712f313555ff4642ef96f4 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkGraphics.h"
6#include "SkImageDecoder.h"
7#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkRegion.h"
9#include "SkShader.h"
10#include "SkUtils.h"
11#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkColorPriv.h"
13#include "SkColorFilter.h"
14#include "SkTime.h"
15#include "SkTypeface.h"
16
reed@android.coma9640282009-08-28 20:06:54 +000017#include "SkGeometry.h"
18
19// http://code.google.com/p/skia/issues/detail?id=32
20static void test_cubic() {
21 SkPoint src[4] = {
22 { 556.25000, 523.03003 },
23 { 556.23999, 522.96002 },
24 { 556.21997, 522.89001 },
25 { 556.21997, 522.82001 }
26 };
27 SkPoint dst[11];
28 dst[10].set(42, -42); // one past the end, that we don't clobber these
29 SkScalar tval[] = { 0.33333334f, 0.99999994f };
30
31 SkChopCubicAt(src, dst, tval, 2);
32
33#if 0
34 for (int i = 0; i < 11; i++) {
35 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
36 }
37#endif
38}
39
reed@android.com8a1c16f2008-12-17 15:59:43 +000040class PathView : public SkView {
41public:
42 int fDStroke, fStroke, fMinStroke, fMaxStroke;
43 SkPath fPath[6];
44 bool fShowHairline;
45
reed@android.coma9640282009-08-28 20:06:54 +000046 PathView() {
47 test_cubic();
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 fShowHairline = false;
50
51 fDStroke = 1;
52 fStroke = 10;
53 fMinStroke = 10;
54 fMaxStroke = 180;
55
56 const int V = 85;
57
58 fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
59 fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
60 fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
61
62 fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
63 fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
64 fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
65
66 fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
67 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
68 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
69
70 fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
71 fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
72 fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
73
74 fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
75 fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
76 fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
77
78 fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
79 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
80 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50));
81 }
82
83 virtual ~PathView()
84 {
85 }
86
87 void nextStroke()
88 {
89 fStroke += fDStroke;
90 if (fStroke > fMaxStroke || fStroke < fMinStroke)
91 fDStroke = -fDStroke;
92 }
93
94protected:
95 // overrides from SkEventSink
96 virtual bool onQuery(SkEvent* evt)
97 {
98 if (SampleCode::TitleQ(*evt))
99 {
100 SampleCode::TitleR(evt, "Paths");
101 return true;
102 }
103 return this->INHERITED::onQuery(evt);
104 }
105
106 void drawBG(SkCanvas* canvas)
107 {
108 canvas->drawColor(0xFFDDDDDD);
109// canvas->drawColor(SK_ColorWHITE);
110 }
111
112 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j)
113 {
114 SkPaint paint;
115
116 paint.setAntiAlias(true);
117 paint.setStyle(SkPaint::kStroke_Style);
118 paint.setStrokeJoin(j);
119 paint.setStrokeWidth(SkIntToScalar(fStroke));
120
121 if (fShowHairline)
122 {
123 SkPath fill;
124
125 paint.getFillPath(path, &fill);
126 paint.setStrokeWidth(0);
127 canvas->drawPath(fill, paint);
128 }
129 else
130 canvas->drawPath(path, paint);
131
132 paint.setColor(SK_ColorRED);
133 paint.setStrokeWidth(0);
134 canvas->drawPath(path, paint);
135 }
136
137 virtual void onDraw(SkCanvas* canvas)
138 {
139 this->drawBG(canvas);
140
141 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
142
143 static const SkPaint::Join gJoins[] = {
144 SkPaint::kBevel_Join,
145 SkPaint::kMiter_Join,
146 SkPaint::kRound_Join
147 };
148
149 for (int i = 0; i < SK_ARRAY_COUNT(gJoins); i++)
150 {
151 canvas->save();
152 for (int j = 0; j < SK_ARRAY_COUNT(fPath); j++)
153 {
154 this->drawPath(canvas, fPath[j], gJoins[i]);
155 canvas->translate(SkIntToScalar(200), 0);
156 }
157 canvas->restore();
158
159 canvas->translate(0, SkIntToScalar(200));
160 }
161
162 this->nextStroke();
163 this->inval(NULL);
164 }
165
166 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y)
167 {
168 fShowHairline = !fShowHairline;
169 this->inval(NULL);
170 return this->INHERITED::onFindClickHandler(x, y);
171 }
172
173 virtual bool onClick(Click* click)
174 {
175 return this->INHERITED::onClick(click);
176 }
177
178private:
179 typedef SkView INHERITED;
180};
181
182//////////////////////////////////////////////////////////////////////////////
183
184static SkView* MyFactory() { return new PathView; }
185static SkViewRegister reg(MyFactory);
186