blob: 8baa359fa3d3301b89ef5b6fa5c7388849feab58 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed339cdbf2015-02-05 22:02:37 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
18#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
reed@android.comd0d0e652009-10-13 13:31:27 +000021#include "SkParsePath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkTime.h"
23#include "SkTypeface.h"
24
reed@android.coma9640282009-08-28 20:06:54 +000025#include "SkGeometry.h"
26
bungeman60e0fee2015-08-26 05:15:46 -070027#include <stdlib.h>
28
reed@android.coma9640282009-08-28 20:06:54 +000029// http://code.google.com/p/skia/issues/detail?id=32
30static void test_cubic() {
31 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000032 { 556.25000f, 523.03003f },
33 { 556.23999f, 522.96002f },
34 { 556.21997f, 522.89001f },
35 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000036 };
37 SkPoint dst[11];
38 dst[10].set(42, -42); // one past the end, that we don't clobber these
39 SkScalar tval[] = { 0.33333334f, 0.99999994f };
40
41 SkChopCubicAt(src, dst, tval, 2);
42
43#if 0
44 for (int i = 0; i < 11; i++) {
45 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
46 }
47#endif
48}
49
reed@android.comd0d0e652009-10-13 13:31:27 +000050static void test_cubic2() {
51 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
52 SkPath path;
53 SkParsePath::FromSVGString(str, &path);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.comd0d0e652009-10-13 13:31:27 +000055 {
reed@android.comd0d0e652009-10-13 13:31:27 +000056 SkRect r = path.getBounds();
57 SkIRect ir;
58 r.round(&ir);
bungeman@google.comfab44db2013-10-11 18:50:45 +000059 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
60 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
61 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
62 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000063 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.comd0d0e652009-10-13 13:31:27 +000065 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000066 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000067
68 SkCanvas canvas(bitmap);
69 SkPaint paint;
70 paint.setAntiAlias(true);
71 canvas.drawPath(path, paint);
72}
73
reed@google.com0faac1e2011-05-11 05:58:58 +000074class PathView : public SampleView {
reed339cdbf2015-02-05 22:02:37 -080075 SkScalar fPrevSecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076public:
reed339cdbf2015-02-05 22:02:37 -080077 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkPath fPath[6];
79 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000080 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
82 PathView() {
reed339cdbf2015-02-05 22:02:37 -080083 fPrevSecs = 0;
reed@google.comc9fa63c2012-03-12 21:14:09 +000084 fOnce = false;
85 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@google.comc9fa63c2012-03-12 21:14:09 +000087 void init() {
88 if (fOnce) {
89 return;
90 }
91 fOnce = true;
92
reed@android.coma9640282009-08-28 20:06:54 +000093 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +000094 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 fDStroke = 1;
99 fStroke = 10;
100 fMinStroke = 10;
101 fMaxStroke = 180;
102
reed339cdbf2015-02-05 22:02:37 -0800103 const SkScalar V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed339cdbf2015-02-05 22:02:37 -0800105 fPath[0].moveTo(40, 70);
106 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
107 fPath[0].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed339cdbf2015-02-05 22:02:37 -0800109 fPath[1].moveTo(40, 70);
110 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
111 fPath[1].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed339cdbf2015-02-05 22:02:37 -0800113 fPath[2].moveTo(V, V);
114 fPath[2].lineTo(50, V);
115 fPath[2].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed339cdbf2015-02-05 22:02:37 -0800117 fPath[3].moveTo(50, 50);
118 fPath[3].lineTo(50, V);
119 fPath[3].lineTo(V, V);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed339cdbf2015-02-05 22:02:37 -0800121 fPath[4].moveTo(50, 50);
122 fPath[4].lineTo(50, V);
123 fPath[4].lineTo(52, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed339cdbf2015-02-05 22:02:37 -0800125 fPath[5].moveTo(52, 50);
126 fPath[5].lineTo(50, V);
127 fPath[5].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.com0faac1e2011-05-11 05:58:58 +0000129 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132protected:
133 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700134 bool onQuery(SkEvent* evt) override {
reed@google.com0faac1e2011-05-11 05:58:58 +0000135 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SampleCode::TitleR(evt, "Paths");
137 return true;
138 }
139 return this->INHERITED::onQuery(evt);
140 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@google.com0faac1e2011-05-11 05:58:58 +0000142 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 paint.setAntiAlias(true);
146 paint.setStyle(SkPaint::kStroke_Style);
147 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800148 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
reed@google.com0faac1e2011-05-11 05:58:58 +0000150 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
153 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 paint.setStrokeWidth(0);
155 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000156 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 paint.setColor(SK_ColorRED);
161 paint.setStrokeWidth(0);
162 canvas->drawPath(path, paint);
163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
mtkleinf0599002015-07-13 06:18:39 -0700165 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000166 this->init();
reed339cdbf2015-02-05 22:02:37 -0800167 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168
169 static const SkPaint::Join gJoins[] = {
170 SkPaint::kBevel_Join,
171 SkPaint::kMiter_Join,
172 SkPaint::kRound_Join
173 };
174
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000175 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000177 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800179 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 }
181 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed339cdbf2015-02-05 22:02:37 -0800183 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 }
reed339cdbf2015-02-05 22:02:37 -0800185 }
mtkleinf0599002015-07-13 06:18:39 -0700186
mtklein36352bf2015-03-25 18:17:31 -0700187 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800188 SkScalar currSecs = timer.scaled(100);
189 SkScalar delta = currSecs - fPrevSecs;
190 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed339cdbf2015-02-05 22:02:37 -0800192 fStroke += fDStroke * delta;
193 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
194 fDStroke = -fDStroke;
195 }
196 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
mtklein36352bf2015-03-25 18:17:31 -0700199 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 fShowHairline = !fShowHairline;
halcanary96fcdcc2015-08-27 07:41:13 -0700201 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000202 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000206 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207};
reeda7a8b102014-12-16 08:07:43 -0800208DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209
210//////////////////////////////////////////////////////////////////////////////
211
reed8b575242014-12-17 01:47:32 -0800212#include "SkArcToPathEffect.h"
reeda7a8b102014-12-16 08:07:43 -0800213#include "SkCornerPathEffect.h"
214#include "SkRandom.h"
215
216class ArcToView : public SampleView {
reed8b575242014-12-17 01:47:32 -0800217 bool fDoFrame, fDoArcTo, fDoCorner, fDoConic;
218 SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800219public:
220 enum {
221 N = 4
222 };
223 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800224
reed8b575242014-12-17 01:47:32 -0800225 ArcToView()
226 : fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false)
227 {
reeda7a8b102014-12-16 08:07:43 -0800228 SkRandom rand;
229 for (int i = 0; i < N; ++i) {
230 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
231 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
232 }
mtkleinf0599002015-07-13 06:18:39 -0700233
reed8b575242014-12-17 01:47:32 -0800234 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800235
236 fPtsPaint.setAntiAlias(true);
237 fPtsPaint.setStrokeWidth(15);
238 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
239
reed8b575242014-12-17 01:47:32 -0800240 fArcToPaint.setAntiAlias(true);
241 fArcToPaint.setStyle(SkPaint::kStroke_Style);
242 fArcToPaint.setStrokeWidth(9);
243 fArcToPaint.setColor(0x800000FF);
reeda4393342016-03-18 11:22:57 -0700244 fArcToPaint.setPathEffect(SkArcToPathEffect::Make(rad));
reeda7a8b102014-12-16 08:07:43 -0800245
246 fCornerPaint.setAntiAlias(true);
247 fCornerPaint.setStyle(SkPaint::kStroke_Style);
248 fCornerPaint.setStrokeWidth(13);
249 fCornerPaint.setColor(SK_ColorGREEN);
reeda4393342016-03-18 11:22:57 -0700250 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
reeda7a8b102014-12-16 08:07:43 -0800251
252 fSkeletonPaint.setAntiAlias(true);
253 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
254 fSkeletonPaint.setColor(SK_ColorRED);
255 }
256
reed8b575242014-12-17 01:47:32 -0800257 void toggle(bool& value) {
258 value = !value;
halcanary96fcdcc2015-08-27 07:41:13 -0700259 this->inval(nullptr);
reed8b575242014-12-17 01:47:32 -0800260 }
261
reeda7a8b102014-12-16 08:07:43 -0800262protected:
263 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700264 bool onQuery(SkEvent* evt) override {
reeda7a8b102014-12-16 08:07:43 -0800265 if (SampleCode::TitleQ(*evt)) {
266 SampleCode::TitleR(evt, "ArcTo");
267 return true;
268 }
reed8b575242014-12-17 01:47:32 -0800269 SkUnichar uni;
270 if (SampleCode::CharQ(*evt, &uni)) {
271 switch (uni) {
272 case '1': this->toggle(fDoFrame); return true;
273 case '2': this->toggle(fDoArcTo); return true;
274 case '3': this->toggle(fDoCorner); return true;
275 case '4': this->toggle(fDoConic); return true;
276 default: break;
277 }
278 }
reeda7a8b102014-12-16 08:07:43 -0800279 return this->INHERITED::onQuery(evt);
280 }
mtkleinf0599002015-07-13 06:18:39 -0700281
reed8b575242014-12-17 01:47:32 -0800282 void makePath(SkPath* path) {
283 path->moveTo(fPts[0]);
284 for (int i = 1; i < N; ++i) {
285 path->lineTo(fPts[i]);
286 }
287 if (!fDoFrame) {
288 path->close();
289 }
290 }
reeda7a8b102014-12-16 08:07:43 -0800291
mtklein36352bf2015-03-25 18:17:31 -0700292 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800293 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
294
295 SkPath path;
reed8b575242014-12-17 01:47:32 -0800296 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800297
reed8b575242014-12-17 01:47:32 -0800298 if (fDoCorner) {
299 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800300 }
reed8b575242014-12-17 01:47:32 -0800301 if (fDoArcTo) {
302 canvas->drawPath(path, fArcToPaint);
reeda7a8b102014-12-16 08:07:43 -0800303 }
reeda7a8b102014-12-16 08:07:43 -0800304
reed8b575242014-12-17 01:47:32 -0800305 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800306 }
307
mtklein36352bf2015-03-25 18:17:31 -0700308 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800309 int32_t index;
310 if (click->fMeta.findS32("index", &index)) {
311 SkASSERT((unsigned)index < N);
312 fPts[index] = click->fCurr;
halcanary96fcdcc2015-08-27 07:41:13 -0700313 this->inval(nullptr);
reeda7a8b102014-12-16 08:07:43 -0800314 return true;
315 }
316 return false;
317 }
318
mtklein36352bf2015-03-25 18:17:31 -0700319 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800320 const SkScalar tol = 4;
321 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
322 for (int i = 0; i < N; ++i) {
323 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
324 Click* click = new Click(this);
325 click->fMeta.setS32("index", i);
326 return click;
327 }
328 }
329 return this->INHERITED::onFindClickHandler(x, y, modi);
330 }
331
332private:
333 typedef SampleView INHERITED;
334};
335DEF_SAMPLE( return new ArcToView; )