blob: c977ca0dc2a0656a7e25eb5aeebe492c4f72192d [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"
15#include "SkImageDecoder.h"
16#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkRegion.h"
18#include "SkShader.h"
19#include "SkUtils.h"
20#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkColorPriv.h"
22#include "SkColorFilter.h"
reed@android.comd0d0e652009-10-13 13:31:27 +000023#include "SkParsePath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkTime.h"
25#include "SkTypeface.h"
26
reed@android.coma9640282009-08-28 20:06:54 +000027#include "SkGeometry.h"
28
bungeman60e0fee2015-08-26 05:15:46 -070029#include <stdlib.h>
30
reed@android.coma9640282009-08-28 20:06:54 +000031// http://code.google.com/p/skia/issues/detail?id=32
32static void test_cubic() {
33 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000034 { 556.25000f, 523.03003f },
35 { 556.23999f, 522.96002f },
36 { 556.21997f, 522.89001f },
37 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000038 };
39 SkPoint dst[11];
40 dst[10].set(42, -42); // one past the end, that we don't clobber these
41 SkScalar tval[] = { 0.33333334f, 0.99999994f };
42
43 SkChopCubicAt(src, dst, tval, 2);
44
45#if 0
46 for (int i = 0; i < 11; i++) {
47 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
48 }
49#endif
50}
51
reed@android.comd0d0e652009-10-13 13:31:27 +000052static void test_cubic2() {
53 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
54 SkPath path;
55 SkParsePath::FromSVGString(str, &path);
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@android.comd0d0e652009-10-13 13:31:27 +000057 {
reed@android.comf2b98d62010-12-20 18:26:13 +000058#ifdef SK_BUILD_FOR_WIN
59 // windows doesn't have strtof
halcanary96fcdcc2015-08-27 07:41:13 -070060 float x = (float)strtod("9.94099e+07", nullptr);
reed@android.comf2b98d62010-12-20 18:26:13 +000061#else
halcanary96fcdcc2015-08-27 07:41:13 -070062 float x = strtof("9.94099e+07", nullptr);
reed@android.comf2b98d62010-12-20 18:26:13 +000063#endif
reed@android.comd0d0e652009-10-13 13:31:27 +000064 int ix = (int)x;
65 int fx = (int)(x * 65536);
66 int ffx = SkScalarToFixed(x);
bungeman@google.comfab44db2013-10-11 18:50:45 +000067 SkDebugf("%g %x %x %x\n", x, ix, fx, ffx);
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@android.comd0d0e652009-10-13 13:31:27 +000069 SkRect r = path.getBounds();
70 SkIRect ir;
71 r.round(&ir);
bungeman@google.comfab44db2013-10-11 18:50:45 +000072 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
73 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
74 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
75 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000076 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000077
reed@android.comd0d0e652009-10-13 13:31:27 +000078 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000079 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000080
81 SkCanvas canvas(bitmap);
82 SkPaint paint;
83 paint.setAntiAlias(true);
84 canvas.drawPath(path, paint);
85}
86
reed@google.com0faac1e2011-05-11 05:58:58 +000087class PathView : public SampleView {
reed339cdbf2015-02-05 22:02:37 -080088 SkScalar fPrevSecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000089public:
reed339cdbf2015-02-05 22:02:37 -080090 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkPath fPath[6];
92 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000093 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
95 PathView() {
reed339cdbf2015-02-05 22:02:37 -080096 fPrevSecs = 0;
reed@google.comc9fa63c2012-03-12 21:14:09 +000097 fOnce = false;
98 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@google.comc9fa63c2012-03-12 21:14:09 +0000100 void init() {
101 if (fOnce) {
102 return;
103 }
104 fOnce = true;
105
reed@android.coma9640282009-08-28 20:06:54 +0000106 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +0000107 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 fDStroke = 1;
112 fStroke = 10;
113 fMinStroke = 10;
114 fMaxStroke = 180;
115
reed339cdbf2015-02-05 22:02:37 -0800116 const SkScalar V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
reed339cdbf2015-02-05 22:02:37 -0800118 fPath[0].moveTo(40, 70);
119 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
120 fPath[0].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed339cdbf2015-02-05 22:02:37 -0800122 fPath[1].moveTo(40, 70);
123 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
124 fPath[1].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed339cdbf2015-02-05 22:02:37 -0800126 fPath[2].moveTo(V, V);
127 fPath[2].lineTo(50, V);
128 fPath[2].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed339cdbf2015-02-05 22:02:37 -0800130 fPath[3].moveTo(50, 50);
131 fPath[3].lineTo(50, V);
132 fPath[3].lineTo(V, V);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed339cdbf2015-02-05 22:02:37 -0800134 fPath[4].moveTo(50, 50);
135 fPath[4].lineTo(50, V);
136 fPath[4].lineTo(52, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed339cdbf2015-02-05 22:02:37 -0800138 fPath[5].moveTo(52, 50);
139 fPath[5].lineTo(50, V);
140 fPath[5].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@google.com0faac1e2011-05-11 05:58:58 +0000142 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145protected:
146 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700147 bool onQuery(SkEvent* evt) override {
reed@google.com0faac1e2011-05-11 05:58:58 +0000148 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 SampleCode::TitleR(evt, "Paths");
150 return true;
151 }
152 return this->INHERITED::onQuery(evt);
153 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@google.com0faac1e2011-05-11 05:58:58 +0000155 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 paint.setAntiAlias(true);
159 paint.setStyle(SkPaint::kStroke_Style);
160 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800161 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162
reed@google.com0faac1e2011-05-11 05:58:58 +0000163 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
166 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 paint.setStrokeWidth(0);
168 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000169 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 paint.setColor(SK_ColorRED);
174 paint.setStrokeWidth(0);
175 canvas->drawPath(path, paint);
176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
mtkleinf0599002015-07-13 06:18:39 -0700178 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000179 this->init();
reed339cdbf2015-02-05 22:02:37 -0800180 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181
182 static const SkPaint::Join gJoins[] = {
183 SkPaint::kBevel_Join,
184 SkPaint::kMiter_Join,
185 SkPaint::kRound_Join
186 };
187
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000188 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000190 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800192 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 }
194 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reed339cdbf2015-02-05 22:02:37 -0800196 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 }
reed339cdbf2015-02-05 22:02:37 -0800198 }
mtkleinf0599002015-07-13 06:18:39 -0700199
mtklein36352bf2015-03-25 18:17:31 -0700200 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800201 SkScalar currSecs = timer.scaled(100);
202 SkScalar delta = currSecs - fPrevSecs;
203 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
reed339cdbf2015-02-05 22:02:37 -0800205 fStroke += fDStroke * delta;
206 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
207 fDStroke = -fDStroke;
208 }
209 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000211
mtklein36352bf2015-03-25 18:17:31 -0700212 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 fShowHairline = !fShowHairline;
halcanary96fcdcc2015-08-27 07:41:13 -0700214 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000215 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000219 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220};
reeda7a8b102014-12-16 08:07:43 -0800221DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222
223//////////////////////////////////////////////////////////////////////////////
224
reed8b575242014-12-17 01:47:32 -0800225#include "SkArcToPathEffect.h"
reeda7a8b102014-12-16 08:07:43 -0800226#include "SkCornerPathEffect.h"
227#include "SkRandom.h"
228
229class ArcToView : public SampleView {
reed8b575242014-12-17 01:47:32 -0800230 bool fDoFrame, fDoArcTo, fDoCorner, fDoConic;
231 SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800232public:
233 enum {
234 N = 4
235 };
236 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800237
reed8b575242014-12-17 01:47:32 -0800238 ArcToView()
239 : fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false)
240 {
reeda7a8b102014-12-16 08:07:43 -0800241 SkRandom rand;
242 for (int i = 0; i < N; ++i) {
243 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
244 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
245 }
mtkleinf0599002015-07-13 06:18:39 -0700246
reed8b575242014-12-17 01:47:32 -0800247 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800248
249 fPtsPaint.setAntiAlias(true);
250 fPtsPaint.setStrokeWidth(15);
251 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
252
reed8b575242014-12-17 01:47:32 -0800253 fArcToPaint.setAntiAlias(true);
254 fArcToPaint.setStyle(SkPaint::kStroke_Style);
255 fArcToPaint.setStrokeWidth(9);
256 fArcToPaint.setColor(0x800000FF);
257 fArcToPaint.setPathEffect(SkArcToPathEffect::Create(rad))->unref();
reeda7a8b102014-12-16 08:07:43 -0800258
259 fCornerPaint.setAntiAlias(true);
260 fCornerPaint.setStyle(SkPaint::kStroke_Style);
261 fCornerPaint.setStrokeWidth(13);
262 fCornerPaint.setColor(SK_ColorGREEN);
reed8b575242014-12-17 01:47:32 -0800263 fCornerPaint.setPathEffect(SkCornerPathEffect::Create(rad*2))->unref();
reeda7a8b102014-12-16 08:07:43 -0800264
265 fSkeletonPaint.setAntiAlias(true);
266 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
267 fSkeletonPaint.setColor(SK_ColorRED);
268 }
269
reed8b575242014-12-17 01:47:32 -0800270 void toggle(bool& value) {
271 value = !value;
halcanary96fcdcc2015-08-27 07:41:13 -0700272 this->inval(nullptr);
reed8b575242014-12-17 01:47:32 -0800273 }
274
reeda7a8b102014-12-16 08:07:43 -0800275protected:
276 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700277 bool onQuery(SkEvent* evt) override {
reeda7a8b102014-12-16 08:07:43 -0800278 if (SampleCode::TitleQ(*evt)) {
279 SampleCode::TitleR(evt, "ArcTo");
280 return true;
281 }
reed8b575242014-12-17 01:47:32 -0800282 SkUnichar uni;
283 if (SampleCode::CharQ(*evt, &uni)) {
284 switch (uni) {
285 case '1': this->toggle(fDoFrame); return true;
286 case '2': this->toggle(fDoArcTo); return true;
287 case '3': this->toggle(fDoCorner); return true;
288 case '4': this->toggle(fDoConic); return true;
289 default: break;
290 }
291 }
reeda7a8b102014-12-16 08:07:43 -0800292 return this->INHERITED::onQuery(evt);
293 }
mtkleinf0599002015-07-13 06:18:39 -0700294
reed8b575242014-12-17 01:47:32 -0800295 void makePath(SkPath* path) {
296 path->moveTo(fPts[0]);
297 for (int i = 1; i < N; ++i) {
298 path->lineTo(fPts[i]);
299 }
300 if (!fDoFrame) {
301 path->close();
302 }
303 }
reeda7a8b102014-12-16 08:07:43 -0800304
mtklein36352bf2015-03-25 18:17:31 -0700305 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800306 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
307
308 SkPath path;
reed8b575242014-12-17 01:47:32 -0800309 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800310
reed8b575242014-12-17 01:47:32 -0800311 if (fDoCorner) {
312 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800313 }
reed8b575242014-12-17 01:47:32 -0800314 if (fDoArcTo) {
315 canvas->drawPath(path, fArcToPaint);
reeda7a8b102014-12-16 08:07:43 -0800316 }
reeda7a8b102014-12-16 08:07:43 -0800317
reed8b575242014-12-17 01:47:32 -0800318 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800319 }
320
mtklein36352bf2015-03-25 18:17:31 -0700321 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800322 int32_t index;
323 if (click->fMeta.findS32("index", &index)) {
324 SkASSERT((unsigned)index < N);
325 fPts[index] = click->fCurr;
halcanary96fcdcc2015-08-27 07:41:13 -0700326 this->inval(nullptr);
reeda7a8b102014-12-16 08:07:43 -0800327 return true;
328 }
329 return false;
330 }
331
mtklein36352bf2015-03-25 18:17:31 -0700332 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800333 const SkScalar tol = 4;
334 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
335 for (int i = 0; i < N; ++i) {
336 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
337 Click* click = new Click(this);
338 click->fMeta.setS32("index", i);
339 return click;
340 }
341 }
342 return this->INHERITED::onFindClickHandler(x, y, modi);
343 }
344
345private:
346 typedef SampleView INHERITED;
347};
348DEF_SAMPLE( return new ArcToView; )
349