blob: fa6ad43464e4905700ea0e784490ca7753f67d2c [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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
reed@android.comd0d0e652009-10-13 13:31:27 +000054 {
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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000065
reed@android.comd0d0e652009-10-13 13:31:27 +000066 SkRect r = path.getBounds();
67 SkIRect ir;
68 r.round(&ir);
69 printf("[%g %g %g %g] [%x %x %x %x]\n",
reed@google.com84ef10f2012-02-16 13:18:48 +000070 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
71 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
reed@android.comd0d0e652009-10-13 13:31:27 +000072 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
73 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.comd0d0e652009-10-13 13:31:27 +000075 SkBitmap bitmap;
76 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 300, 200);
77 bitmap.allocPixels();
78
79 SkCanvas canvas(bitmap);
80 SkPaint paint;
81 paint.setAntiAlias(true);
82 canvas.drawPath(path, paint);
83}
84
reed@google.com0faac1e2011-05-11 05:58:58 +000085class PathView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086public:
87 int fDStroke, fStroke, fMinStroke, fMaxStroke;
88 SkPath fPath[6];
89 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000090 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
92 PathView() {
reed@google.comc9fa63c2012-03-12 21:14:09 +000093 fOnce = false;
94 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
reed@google.comc9fa63c2012-03-12 21:14:09 +000096 void init() {
97 if (fOnce) {
98 return;
99 }
100 fOnce = true;
101
reed@android.coma9640282009-08-28 20:06:54 +0000102 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +0000103 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 fDStroke = 1;
108 fStroke = 10;
109 fMinStroke = 10;
110 fMaxStroke = 180;
111
112 const int V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
115 fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
116 fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
119 fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
120 fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
123 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
124 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
127 fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
128 fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
131 fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
132 fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
135 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
136 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@google.com0faac1e2011-05-11 05:58:58 +0000138 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@google.com0faac1e2011-05-11 05:58:58 +0000141 void nextStroke() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 fStroke += fDStroke;
143 if (fStroke > fMaxStroke || fStroke < fMinStroke)
144 fDStroke = -fDStroke;
145 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147protected:
148 // overrides from SkEventSink
reed@google.com0faac1e2011-05-11 05:58:58 +0000149 virtual bool onQuery(SkEvent* evt) {
150 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 SampleCode::TitleR(evt, "Paths");
152 return true;
153 }
154 return this->INHERITED::onQuery(evt);
155 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156
reed@google.com0faac1e2011-05-11 05:58:58 +0000157 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 paint.setAntiAlias(true);
161 paint.setStyle(SkPaint::kStroke_Style);
162 paint.setStrokeJoin(j);
163 paint.setStrokeWidth(SkIntToScalar(fStroke));
164
reed@google.com0faac1e2011-05-11 05:58:58 +0000165 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000167
168 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 paint.setStrokeWidth(0);
170 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000171 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000173 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 paint.setColor(SK_ColorRED);
176 paint.setStrokeWidth(0);
177 canvas->drawPath(path, paint);
178 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@google.comc9fa63c2012-03-12 21:14:09 +0000180 virtual void onDrawContent(SkCanvas* canvas) {
181 this->init();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
183
184 static const SkPaint::Join gJoins[] = {
185 SkPaint::kBevel_Join,
186 SkPaint::kMiter_Join,
187 SkPaint::kRound_Join
188 };
189
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000190 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000192 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 this->drawPath(canvas, fPath[j], gJoins[i]);
194 canvas->translate(SkIntToScalar(200), 0);
195 }
196 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 canvas->translate(0, SkIntToScalar(200));
199 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 this->nextStroke();
202 this->inval(NULL);
203 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
reed@google.com4d5c26d2013-01-08 16:17:50 +0000205 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 fShowHairline = !fShowHairline;
207 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000208 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000210
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000212 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213};
214
215//////////////////////////////////////////////////////////////////////////////
216
217static SkView* MyFactory() { return new PathView; }
218static SkViewRegister reg(MyFactory);