reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkRegion.h" |
| 17 | #include "SkShader.h" |
| 18 | #include "SkUtils.h" |
| 19 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 22 | #include "SkParsePath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkTime.h" |
| 24 | #include "SkTypeface.h" |
| 25 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 26 | #include "SkGeometry.h" |
| 27 | |
| 28 | // http://code.google.com/p/skia/issues/detail?id=32 |
| 29 | static void test_cubic() { |
| 30 | SkPoint src[4] = { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 31 | { 556.25000f, 523.03003f }, |
| 32 | { 556.23999f, 522.96002f }, |
| 33 | { 556.21997f, 522.89001f }, |
| 34 | { 556.21997f, 522.82001f } |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 35 | }; |
| 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.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 49 | static 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 54 | { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 55 | #ifdef SK_BUILD_FOR_WIN |
| 56 | // windows doesn't have strtof |
| 57 | float x = (float)strtod("9.94099e+07", NULL); |
| 58 | #else |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 59 | float x = strtof("9.94099e+07", NULL); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 60 | #endif |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 61 | int ix = (int)x; |
| 62 | int fx = (int)(x * 65536); |
| 63 | int ffx = SkScalarToFixed(x); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 64 | SkDebugf("%g %x %x %x\n", x, ix, fx, ffx); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 66 | SkRect r = path.getBounds(); |
| 67 | SkIRect ir; |
| 68 | r.round(&ir); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 69 | 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.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 73 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 75 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 76 | bitmap.allocN32Pixels(300, 200); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 77 | |
| 78 | SkCanvas canvas(bitmap); |
| 79 | SkPaint paint; |
| 80 | paint.setAntiAlias(true); |
| 81 | canvas.drawPath(path, paint); |
| 82 | } |
| 83 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 84 | class PathView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | public: |
| 86 | int fDStroke, fStroke, fMinStroke, fMaxStroke; |
| 87 | SkPath fPath[6]; |
| 88 | bool fShowHairline; |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 89 | bool fOnce; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 90 | |
| 91 | PathView() { |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 92 | fOnce = false; |
| 93 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 95 | void init() { |
| 96 | if (fOnce) { |
| 97 | return; |
| 98 | } |
| 99 | fOnce = true; |
| 100 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 101 | test_cubic(); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 102 | test_cubic2(); |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | fShowHairline = false; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | fDStroke = 1; |
| 107 | fStroke = 10; |
| 108 | fMinStroke = 10; |
| 109 | fMaxStroke = 180; |
| 110 | |
| 111 | const int V = 85; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 137 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 140 | void nextStroke() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | fStroke += fDStroke; |
| 142 | if (fStroke > fMaxStroke || fStroke < fMinStroke) |
| 143 | fDStroke = -fDStroke; |
| 144 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | protected: |
| 147 | // overrides from SkEventSink |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 148 | virtual bool onQuery(SkEvent* evt) { |
| 149 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | SampleCode::TitleR(evt, "Paths"); |
| 151 | return true; |
| 152 | } |
| 153 | return this->INHERITED::onQuery(evt); |
| 154 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 155 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 156 | void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | paint.setAntiAlias(true); |
| 160 | paint.setStyle(SkPaint::kStroke_Style); |
| 161 | paint.setStrokeJoin(j); |
| 162 | paint.setStrokeWidth(SkIntToScalar(fStroke)); |
| 163 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 164 | if (fShowHairline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | SkPath fill; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 166 | |
| 167 | paint.getFillPath(path, &fill); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | paint.setStrokeWidth(0); |
| 169 | canvas->drawPath(fill, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 170 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | canvas->drawPath(path, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 172 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 173 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | paint.setColor(SK_ColorRED); |
| 175 | paint.setStrokeWidth(0); |
| 176 | canvas->drawPath(path, paint); |
| 177 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 178 | |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 179 | virtual void onDrawContent(SkCanvas* canvas) { |
| 180 | this->init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 189 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | canvas->save(); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 191 | for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | this->drawPath(canvas, fPath[j], gJoins[i]); |
| 193 | canvas->translate(SkIntToScalar(200), 0); |
| 194 | } |
| 195 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | canvas->translate(0, SkIntToScalar(200)); |
| 198 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | this->nextStroke(); |
| 201 | this->inval(NULL); |
| 202 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 203 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 204 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | fShowHairline = !fShowHairline; |
| 206 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 207 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 209 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 211 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | ////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| 216 | static SkView* MyFactory() { return new PathView; } |
| 217 | static SkViewRegister reg(MyFactory); |