3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SampleCode.h"
10 #include "SkAnimTimer.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkImageDecoder.h"
20 #include "SkXfermode.h"
21 #include "SkColorPriv.h"
22 #include "SkColorFilter.h"
23 #include "SkParsePath.h"
25 #include "SkTypeface.h"
27 #include "SkGeometry.h"
29 // http://code.google.com/p/skia/issues/detail?id=32
30 static void test_cubic() {
32 { 556.25000f, 523.03003f },
33 { 556.23999f, 522.96002f },
34 { 556.21997f, 522.89001f },
35 { 556.21997f, 522.82001f }
38 dst[10].set(42, -42); // one past the end, that we don't clobber these
39 SkScalar tval[] = { 0.33333334f, 0.99999994f };
41 SkChopCubicAt(src, dst, tval, 2);
44 for (int i = 0; i < 11; i++) {
45 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
50 static void test_cubic2() {
51 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
53 SkParsePath::FromSVGString(str, &path);
56 #ifdef SK_BUILD_FOR_WIN
57 // windows doesn't have strtof
58 float x = (float)strtod("9.94099e+07", NULL);
60 float x = strtof("9.94099e+07", NULL);
63 int fx = (int)(x * 65536);
64 int ffx = SkScalarToFixed(x);
65 SkDebugf("%g %x %x %x\n", x, ix, fx, ffx);
67 SkRect r = path.getBounds();
70 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
71 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
72 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
73 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
77 bitmap.allocN32Pixels(300, 200);
79 SkCanvas canvas(bitmap);
81 paint.setAntiAlias(true);
82 canvas.drawPath(path, paint);
85 class PathView : public SampleView {
88 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
107 fShowHairline = false;
114 const SkScalar V = 85;
116 fPath[0].moveTo(40, 70);
117 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
118 fPath[0].lineTo(110, 70);
120 fPath[1].moveTo(40, 70);
121 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
122 fPath[1].lineTo(110, 70);
124 fPath[2].moveTo(V, V);
125 fPath[2].lineTo(50, V);
126 fPath[2].lineTo(50, 50);
128 fPath[3].moveTo(50, 50);
129 fPath[3].lineTo(50, V);
130 fPath[3].lineTo(V, V);
132 fPath[4].moveTo(50, 50);
133 fPath[4].lineTo(50, V);
134 fPath[4].lineTo(52, 50);
136 fPath[5].moveTo(52, 50);
137 fPath[5].lineTo(50, V);
138 fPath[5].lineTo(50, 50);
140 this->setBGColor(0xFFDDDDDD);
144 // overrides from SkEventSink
145 virtual bool onQuery(SkEvent* evt) {
146 if (SampleCode::TitleQ(*evt)) {
147 SampleCode::TitleR(evt, "Paths");
150 return this->INHERITED::onQuery(evt);
153 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
156 paint.setAntiAlias(true);
157 paint.setStyle(SkPaint::kStroke_Style);
158 paint.setStrokeJoin(j);
159 paint.setStrokeWidth(fStroke);
164 paint.getFillPath(path, &fill);
165 paint.setStrokeWidth(0);
166 canvas->drawPath(fill, paint);
168 canvas->drawPath(path, paint);
171 paint.setColor(SK_ColorRED);
172 paint.setStrokeWidth(0);
173 canvas->drawPath(path, paint);
176 virtual void onDrawContent(SkCanvas* canvas) {
178 canvas->translate(50, 50);
180 static const SkPaint::Join gJoins[] = {
181 SkPaint::kBevel_Join,
182 SkPaint::kMiter_Join,
186 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
188 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
189 this->drawPath(canvas, fPath[j], gJoins[i]);
190 canvas->translate(200, 0);
194 canvas->translate(0, 200);
198 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
199 SkScalar currSecs = timer.scaled(100);
200 SkScalar delta = currSecs - fPrevSecs;
201 fPrevSecs = currSecs;
203 fStroke += fDStroke * delta;
204 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
205 fDStroke = -fDStroke;
210 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
211 fShowHairline = !fShowHairline;
213 return this->INHERITED::onFindClickHandler(x, y, modi);
217 typedef SampleView INHERITED;
219 DEF_SAMPLE( return new PathView; )
221 //////////////////////////////////////////////////////////////////////////////
223 #include "SkArcToPathEffect.h"
224 #include "SkCornerPathEffect.h"
225 #include "SkRandom.h"
227 class ArcToView : public SampleView {
228 bool fDoFrame, fDoArcTo, fDoCorner, fDoConic;
229 SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint;
237 : fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false)
240 for (int i = 0; i < N; ++i) {
241 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
242 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
245 const SkScalar rad = 50;
247 fPtsPaint.setAntiAlias(true);
248 fPtsPaint.setStrokeWidth(15);
249 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
251 fArcToPaint.setAntiAlias(true);
252 fArcToPaint.setStyle(SkPaint::kStroke_Style);
253 fArcToPaint.setStrokeWidth(9);
254 fArcToPaint.setColor(0x800000FF);
255 fArcToPaint.setPathEffect(SkArcToPathEffect::Create(rad))->unref();
257 fCornerPaint.setAntiAlias(true);
258 fCornerPaint.setStyle(SkPaint::kStroke_Style);
259 fCornerPaint.setStrokeWidth(13);
260 fCornerPaint.setColor(SK_ColorGREEN);
261 fCornerPaint.setPathEffect(SkCornerPathEffect::Create(rad*2))->unref();
263 fSkeletonPaint.setAntiAlias(true);
264 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
265 fSkeletonPaint.setColor(SK_ColorRED);
268 void toggle(bool& value) {
274 // overrides from SkEventSink
275 bool onQuery(SkEvent* evt) SK_OVERRIDE {
276 if (SampleCode::TitleQ(*evt)) {
277 SampleCode::TitleR(evt, "ArcTo");
281 if (SampleCode::CharQ(*evt, &uni)) {
283 case '1': this->toggle(fDoFrame); return true;
284 case '2': this->toggle(fDoArcTo); return true;
285 case '3': this->toggle(fDoCorner); return true;
286 case '4': this->toggle(fDoConic); return true;
290 return this->INHERITED::onQuery(evt);
293 void makePath(SkPath* path) {
294 path->moveTo(fPts[0]);
295 for (int i = 1; i < N; ++i) {
296 path->lineTo(fPts[i]);
303 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
304 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
307 this->makePath(&path);
310 canvas->drawPath(path, fCornerPaint);
313 canvas->drawPath(path, fArcToPaint);
316 canvas->drawPath(path, fSkeletonPaint);
319 bool onClick(Click* click) SK_OVERRIDE {
321 if (click->fMeta.findS32("index", &index)) {
322 SkASSERT((unsigned)index < N);
323 fPts[index] = click->fCurr;
330 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
331 const SkScalar tol = 4;
332 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
333 for (int i = 0; i < N; ++i) {
334 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
335 Click* click = new Click(this);
336 click->fMeta.setS32("index", i);
340 return this->INHERITED::onFindClickHandler(x, y, modi);
344 typedef SampleView INHERITED;
346 DEF_SAMPLE( return new ArcToView; )