blob: e35eeb6717575ffe2e91c1ca349cc2e1b5c449f5 [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);
bungeman@google.comfab44db2013-10-11 18:50:45 +000064 SkDebugf("%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);
bungeman@google.comfab44db2013-10-11 18:50:45 +000069 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
70 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
71 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
72 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000073 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.comd0d0e652009-10-13 13:31:27 +000075 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000076 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000077
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;
reed@google.comc9fa63c2012-03-12 21:14:09 +000089 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
91 PathView() {
reed@google.comc9fa63c2012-03-12 21:14:09 +000092 fOnce = false;
93 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@google.comc9fa63c2012-03-12 21:14:09 +000095 void init() {
96 if (fOnce) {
97 return;
98 }
99 fOnce = true;
100
reed@android.coma9640282009-08-28 20:06:54 +0000101 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +0000102 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 fDStroke = 1;
107 fStroke = 10;
108 fMinStroke = 10;
109 fMaxStroke = 180;
110
111 const int V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
114 fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
115 fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
118 fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
119 fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
122 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
123 fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
126 fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
127 fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
130 fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
131 fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
134 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
135 fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@google.com0faac1e2011-05-11 05:58:58 +0000137 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@google.com0faac1e2011-05-11 05:58:58 +0000140 void nextStroke() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 fStroke += fDStroke;
142 if (fStroke > fMaxStroke || fStroke < fMinStroke)
143 fDStroke = -fDStroke;
144 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146protected:
147 // overrides from SkEventSink
reed@google.com0faac1e2011-05-11 05:58:58 +0000148 virtual bool onQuery(SkEvent* evt) {
149 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 SampleCode::TitleR(evt, "Paths");
151 return true;
152 }
153 return this->INHERITED::onQuery(evt);
154 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@google.com0faac1e2011-05-11 05:58:58 +0000156 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 paint.setAntiAlias(true);
160 paint.setStyle(SkPaint::kStroke_Style);
161 paint.setStrokeJoin(j);
162 paint.setStrokeWidth(SkIntToScalar(fStroke));
163
reed@google.com0faac1e2011-05-11 05:58:58 +0000164 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
167 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 paint.setStrokeWidth(0);
169 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000170 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000172 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 paint.setColor(SK_ColorRED);
175 paint.setStrokeWidth(0);
176 canvas->drawPath(path, paint);
177 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@google.comc9fa63c2012-03-12 21:14:09 +0000179 virtual void onDrawContent(SkCanvas* canvas) {
180 this->init();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
182
183 static const SkPaint::Join gJoins[] = {
184 SkPaint::kBevel_Join,
185 SkPaint::kMiter_Join,
186 SkPaint::kRound_Join
187 };
188
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000189 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000191 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 this->drawPath(canvas, fPath[j], gJoins[i]);
193 canvas->translate(SkIntToScalar(200), 0);
194 }
195 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 canvas->translate(0, SkIntToScalar(200));
198 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 this->nextStroke();
201 this->inval(NULL);
202 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed@google.com4d5c26d2013-01-08 16:17:50 +0000204 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 fShowHairline = !fShowHairline;
206 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000207 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000211 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212};
213
214//////////////////////////////////////////////////////////////////////////////
215
216static SkView* MyFactory() { return new PathView; }
217static SkViewRegister reg(MyFactory);