blob: 7e2750a354e401304761760906f376f837b1efd3 [file] [log] [blame]
reed@android.comf2b98d62010-12-20 18:26:13 +00001
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
19#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkColorPriv.h"
21#include "SkColorFilter.h"
reed@android.comd0d0e652009-10-13 13:31:27 +000022#include "SkParsePath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkTime.h"
24#include "SkTypeface.h"
25
reed@android.coma9640282009-08-28 20:06:54 +000026#include "SkGeometry.h"
27
28// http://code.google.com/p/skia/issues/detail?id=32
29static void test_cubic() {
30 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000031 { 556.25000f, 523.03003f },
32 { 556.23999f, 522.96002f },
33 { 556.21997f, 522.89001f },
34 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000035 };
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
reed@android.comd0d0e652009-10-13 13:31:27 +000049static 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 {
reed@android.comf2b98d62010-12-20 18:26:13 +000055#ifdef SK_BUILD_FOR_WIN
56 // windows doesn't have strtof
57 float x = (float)strtod("9.94099e+07", NULL);
58#else
reed@android.comd0d0e652009-10-13 13:31:27 +000059 float x = strtof("9.94099e+07", NULL);
reed@android.comf2b98d62010-12-20 18:26:13 +000060#endif
reed@android.comd0d0e652009-10-13 13:31:27 +000061 int ix = (int)x;
62 int fx = (int)(x * 65536);
63 int ffx = SkScalarToFixed(x);
64 printf("%g %x %x %x\n", x, ix, fx, ffx);
65
66 SkRect r = path.getBounds();
67 SkIRect ir;
68 r.round(&ir);
69 printf("[%g %g %g %g] [%x %x %x %x]\n",
70 r.fLeft, r.fTop, r.fRight, r.fBottom,
71 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
72 }
73
74 SkBitmap bitmap;
75 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 300, 200);
76 bitmap.allocPixels();
77
78 SkCanvas canvas(bitmap);
79 SkPaint paint;
80 paint.setAntiAlias(true);
81 canvas.drawPath(path, paint);
82}
83
reed@google.com0faac1e2011-05-11 05:58:58 +000084class PathView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085public:
86 int fDStroke, fStroke, fMinStroke, fMaxStroke;
87 SkPath fPath[6];
88 bool fShowHairline;
89
reed@android.coma9640282009-08-28 20:06:54 +000090 PathView() {
91 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +000092 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 fShowHairline = false;
95
96 fDStroke = 1;
97 fStroke = 10;
98 fMinStroke = 10;
99 fMaxStroke = 180;
100
101 const int V = 85;
102
103 fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
104 fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
105 fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
106
107 fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
108 fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
109 fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
110
111 fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
112 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
113 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
114
115 fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
116 fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
117 fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
118
119 fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
120 fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
121 fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
122
123 fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
124 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
125 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50));
reed@google.com0faac1e2011-05-11 05:58:58 +0000126
127 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 }
129
reed@google.com0faac1e2011-05-11 05:58:58 +0000130 void nextStroke() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 fStroke += fDStroke;
132 if (fStroke > fMaxStroke || fStroke < fMinStroke)
133 fDStroke = -fDStroke;
134 }
135
136protected:
137 // overrides from SkEventSink
reed@google.com0faac1e2011-05-11 05:58:58 +0000138 virtual bool onQuery(SkEvent* evt) {
139 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SampleCode::TitleR(evt, "Paths");
141 return true;
142 }
143 return this->INHERITED::onQuery(evt);
144 }
145
reed@google.com0faac1e2011-05-11 05:58:58 +0000146 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 SkPaint paint;
148
149 paint.setAntiAlias(true);
150 paint.setStyle(SkPaint::kStroke_Style);
151 paint.setStrokeJoin(j);
152 paint.setStrokeWidth(SkIntToScalar(fStroke));
153
reed@google.com0faac1e2011-05-11 05:58:58 +0000154 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 SkPath fill;
156
157 paint.getFillPath(path, &fill);
158 paint.setStrokeWidth(0);
159 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000160 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000162 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163
164 paint.setColor(SK_ColorRED);
165 paint.setStrokeWidth(0);
166 canvas->drawPath(path, paint);
167 }
168
reed@google.com0faac1e2011-05-11 05:58:58 +0000169 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
171
172 static const SkPaint::Join gJoins[] = {
173 SkPaint::kBevel_Join,
174 SkPaint::kMiter_Join,
175 SkPaint::kRound_Join
176 };
177
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000178 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000180 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 this->drawPath(canvas, fPath[j], gJoins[i]);
182 canvas->translate(SkIntToScalar(200), 0);
183 }
184 canvas->restore();
185
186 canvas->translate(0, SkIntToScalar(200));
187 }
188
189 this->nextStroke();
190 this->inval(NULL);
191 }
192
reed@google.com0faac1e2011-05-11 05:58:58 +0000193 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 fShowHairline = !fShowHairline;
195 this->inval(NULL);
196 return this->INHERITED::onFindClickHandler(x, y);
197 }
198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000200 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201};
202
203//////////////////////////////////////////////////////////////////////////////
204
205static SkView* MyFactory() { return new PathView; }
206static SkViewRegister reg(MyFactory);
207