blob: fec3b5e22648173ef922cd3ddc67ab957544778d [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"
reed339cdbf2015-02-05 22:02:37 -080010#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkGradientShader.h"
14#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#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
bungeman60e0fee2015-08-26 05:15:46 -070028#include <stdlib.h>
29
reed@android.coma9640282009-08-28 20:06:54 +000030// http://code.google.com/p/skia/issues/detail?id=32
31static void test_cubic() {
32 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000033 { 556.25000f, 523.03003f },
34 { 556.23999f, 522.96002f },
35 { 556.21997f, 522.89001f },
36 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000037 };
38 SkPoint dst[11];
39 dst[10].set(42, -42); // one past the end, that we don't clobber these
40 SkScalar tval[] = { 0.33333334f, 0.99999994f };
41
42 SkChopCubicAt(src, dst, tval, 2);
43
44#if 0
45 for (int i = 0; i < 11; i++) {
46 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
47 }
48#endif
49}
50
reed@android.comd0d0e652009-10-13 13:31:27 +000051static void test_cubic2() {
52 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
53 SkPath path;
54 SkParsePath::FromSVGString(str, &path);
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@android.comd0d0e652009-10-13 13:31:27 +000056 {
reed@android.comf2b98d62010-12-20 18:26:13 +000057#ifdef SK_BUILD_FOR_WIN
58 // windows doesn't have strtof
halcanary96fcdcc2015-08-27 07:41:13 -070059 float x = (float)strtod("9.94099e+07", nullptr);
reed@android.comf2b98d62010-12-20 18:26:13 +000060#else
halcanary96fcdcc2015-08-27 07:41:13 -070061 float x = strtof("9.94099e+07", nullptr);
reed@android.comf2b98d62010-12-20 18:26:13 +000062#endif
reed@android.comd0d0e652009-10-13 13:31:27 +000063 int ix = (int)x;
64 int fx = (int)(x * 65536);
65 int ffx = SkScalarToFixed(x);
bungeman@google.comfab44db2013-10-11 18:50:45 +000066 SkDebugf("%g %x %x %x\n", x, ix, fx, ffx);
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.comd0d0e652009-10-13 13:31:27 +000068 SkRect r = path.getBounds();
69 SkIRect ir;
70 r.round(&ir);
bungeman@google.comfab44db2013-10-11 18:50:45 +000071 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
72 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
73 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
74 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000075 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@android.comd0d0e652009-10-13 13:31:27 +000077 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000078 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000079
80 SkCanvas canvas(bitmap);
81 SkPaint paint;
82 paint.setAntiAlias(true);
83 canvas.drawPath(path, paint);
84}
85
reed@google.com0faac1e2011-05-11 05:58:58 +000086class PathView : public SampleView {
reed339cdbf2015-02-05 22:02:37 -080087 SkScalar fPrevSecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000088public:
reed339cdbf2015-02-05 22:02:37 -080089 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 SkPath fPath[6];
91 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000092 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
94 PathView() {
reed339cdbf2015-02-05 22:02:37 -080095 fPrevSecs = 0;
reed@google.comc9fa63c2012-03-12 21:14:09 +000096 fOnce = false;
97 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@google.comc9fa63c2012-03-12 21:14:09 +000099 void init() {
100 if (fOnce) {
101 return;
102 }
103 fOnce = true;
104
reed@android.coma9640282009-08-28 20:06:54 +0000105 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +0000106 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 fDStroke = 1;
111 fStroke = 10;
112 fMinStroke = 10;
113 fMaxStroke = 180;
114
reed339cdbf2015-02-05 22:02:37 -0800115 const SkScalar V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed339cdbf2015-02-05 22:02:37 -0800117 fPath[0].moveTo(40, 70);
118 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
119 fPath[0].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed339cdbf2015-02-05 22:02:37 -0800121 fPath[1].moveTo(40, 70);
122 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
123 fPath[1].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed339cdbf2015-02-05 22:02:37 -0800125 fPath[2].moveTo(V, V);
126 fPath[2].lineTo(50, V);
127 fPath[2].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed339cdbf2015-02-05 22:02:37 -0800129 fPath[3].moveTo(50, 50);
130 fPath[3].lineTo(50, V);
131 fPath[3].lineTo(V, V);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed339cdbf2015-02-05 22:02:37 -0800133 fPath[4].moveTo(50, 50);
134 fPath[4].lineTo(50, V);
135 fPath[4].lineTo(52, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed339cdbf2015-02-05 22:02:37 -0800137 fPath[5].moveTo(52, 50);
138 fPath[5].lineTo(50, V);
139 fPath[5].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@google.com0faac1e2011-05-11 05:58:58 +0000141 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144protected:
145 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700146 bool onQuery(SkEvent* evt) override {
reed@google.com0faac1e2011-05-11 05:58:58 +0000147 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 SampleCode::TitleR(evt, "Paths");
149 return true;
150 }
151 return this->INHERITED::onQuery(evt);
152 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@google.com0faac1e2011-05-11 05:58:58 +0000154 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 paint.setAntiAlias(true);
158 paint.setStyle(SkPaint::kStroke_Style);
159 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800160 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
reed@google.com0faac1e2011-05-11 05:58:58 +0000162 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
165 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 paint.setStrokeWidth(0);
167 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000168 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 paint.setColor(SK_ColorRED);
173 paint.setStrokeWidth(0);
174 canvas->drawPath(path, paint);
175 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176
mtkleinf0599002015-07-13 06:18:39 -0700177 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000178 this->init();
reed339cdbf2015-02-05 22:02:37 -0800179 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180
181 static const SkPaint::Join gJoins[] = {
182 SkPaint::kBevel_Join,
183 SkPaint::kMiter_Join,
184 SkPaint::kRound_Join
185 };
186
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000187 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000189 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800191 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 }
193 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
reed339cdbf2015-02-05 22:02:37 -0800195 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 }
reed339cdbf2015-02-05 22:02:37 -0800197 }
mtkleinf0599002015-07-13 06:18:39 -0700198
mtklein36352bf2015-03-25 18:17:31 -0700199 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800200 SkScalar currSecs = timer.scaled(100);
201 SkScalar delta = currSecs - fPrevSecs;
202 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed339cdbf2015-02-05 22:02:37 -0800204 fStroke += fDStroke * delta;
205 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
206 fDStroke = -fDStroke;
207 }
208 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000210
mtklein36352bf2015-03-25 18:17:31 -0700211 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 fShowHairline = !fShowHairline;
halcanary96fcdcc2015-08-27 07:41:13 -0700213 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000214 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000216
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000218 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219};
reeda7a8b102014-12-16 08:07:43 -0800220DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221
222//////////////////////////////////////////////////////////////////////////////
223
reed8b575242014-12-17 01:47:32 -0800224#include "SkArcToPathEffect.h"
reeda7a8b102014-12-16 08:07:43 -0800225#include "SkCornerPathEffect.h"
226#include "SkRandom.h"
227
228class ArcToView : public SampleView {
reed8b575242014-12-17 01:47:32 -0800229 bool fDoFrame, fDoArcTo, fDoCorner, fDoConic;
230 SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800231public:
232 enum {
233 N = 4
234 };
235 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800236
reed8b575242014-12-17 01:47:32 -0800237 ArcToView()
238 : fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false)
239 {
reeda7a8b102014-12-16 08:07:43 -0800240 SkRandom rand;
241 for (int i = 0; i < N; ++i) {
242 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
243 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
244 }
mtkleinf0599002015-07-13 06:18:39 -0700245
reed8b575242014-12-17 01:47:32 -0800246 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800247
248 fPtsPaint.setAntiAlias(true);
249 fPtsPaint.setStrokeWidth(15);
250 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
251
reed8b575242014-12-17 01:47:32 -0800252 fArcToPaint.setAntiAlias(true);
253 fArcToPaint.setStyle(SkPaint::kStroke_Style);
254 fArcToPaint.setStrokeWidth(9);
255 fArcToPaint.setColor(0x800000FF);
reeda4393342016-03-18 11:22:57 -0700256 fArcToPaint.setPathEffect(SkArcToPathEffect::Make(rad));
reeda7a8b102014-12-16 08:07:43 -0800257
258 fCornerPaint.setAntiAlias(true);
259 fCornerPaint.setStyle(SkPaint::kStroke_Style);
260 fCornerPaint.setStrokeWidth(13);
261 fCornerPaint.setColor(SK_ColorGREEN);
reeda4393342016-03-18 11:22:57 -0700262 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
reeda7a8b102014-12-16 08:07:43 -0800263
264 fSkeletonPaint.setAntiAlias(true);
265 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
266 fSkeletonPaint.setColor(SK_ColorRED);
267 }
268
reed8b575242014-12-17 01:47:32 -0800269 void toggle(bool& value) {
270 value = !value;
halcanary96fcdcc2015-08-27 07:41:13 -0700271 this->inval(nullptr);
reed8b575242014-12-17 01:47:32 -0800272 }
273
reeda7a8b102014-12-16 08:07:43 -0800274protected:
275 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700276 bool onQuery(SkEvent* evt) override {
reeda7a8b102014-12-16 08:07:43 -0800277 if (SampleCode::TitleQ(*evt)) {
278 SampleCode::TitleR(evt, "ArcTo");
279 return true;
280 }
reed8b575242014-12-17 01:47:32 -0800281 SkUnichar uni;
282 if (SampleCode::CharQ(*evt, &uni)) {
283 switch (uni) {
284 case '1': this->toggle(fDoFrame); return true;
285 case '2': this->toggle(fDoArcTo); return true;
286 case '3': this->toggle(fDoCorner); return true;
287 case '4': this->toggle(fDoConic); return true;
288 default: break;
289 }
290 }
reeda7a8b102014-12-16 08:07:43 -0800291 return this->INHERITED::onQuery(evt);
292 }
mtkleinf0599002015-07-13 06:18:39 -0700293
reed8b575242014-12-17 01:47:32 -0800294 void makePath(SkPath* path) {
295 path->moveTo(fPts[0]);
296 for (int i = 1; i < N; ++i) {
297 path->lineTo(fPts[i]);
298 }
299 if (!fDoFrame) {
300 path->close();
301 }
302 }
reeda7a8b102014-12-16 08:07:43 -0800303
mtklein36352bf2015-03-25 18:17:31 -0700304 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800305 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
306
307 SkPath path;
reed8b575242014-12-17 01:47:32 -0800308 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800309
reed8b575242014-12-17 01:47:32 -0800310 if (fDoCorner) {
311 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800312 }
reed8b575242014-12-17 01:47:32 -0800313 if (fDoArcTo) {
314 canvas->drawPath(path, fArcToPaint);
reeda7a8b102014-12-16 08:07:43 -0800315 }
reeda7a8b102014-12-16 08:07:43 -0800316
reed8b575242014-12-17 01:47:32 -0800317 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800318 }
319
mtklein36352bf2015-03-25 18:17:31 -0700320 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800321 int32_t index;
322 if (click->fMeta.findS32("index", &index)) {
323 SkASSERT((unsigned)index < N);
324 fPts[index] = click->fCurr;
halcanary96fcdcc2015-08-27 07:41:13 -0700325 this->inval(nullptr);
reeda7a8b102014-12-16 08:07:43 -0800326 return true;
327 }
328 return false;
329 }
330
mtklein36352bf2015-03-25 18:17:31 -0700331 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800332 const SkScalar tol = 4;
333 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
334 for (int i = 0; i < N; ++i) {
335 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
336 Click* click = new Click(this);
337 click->fMeta.setS32("index", i);
338 return click;
339 }
340 }
341 return this->INHERITED::onFindClickHandler(x, y, modi);
342 }
343
344private:
345 typedef SampleView INHERITED;
346};
347DEF_SAMPLE( return new ArcToView; )
348