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); |
| 64 | printf("%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); |
| 69 | printf("[%g %g %g %g] [%x %x %x %x]\n", |
reed@google.com | 84ef10f | 2012-02-16 13:18:48 +0000 | [diff] [blame] | 70 | SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop), |
| 71 | SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom), |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 72 | ir.fLeft, ir.fTop, ir.fRight, ir.fBottom); |
| 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; |
| 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.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 85 | class PathView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | public: |
| 87 | int fDStroke, fStroke, fMinStroke, fMaxStroke; |
| 88 | SkPath fPath[6]; |
| 89 | bool fShowHairline; |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 90 | bool fOnce; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
| 92 | PathView() { |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 93 | fOnce = false; |
| 94 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 95 | |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 96 | void init() { |
| 97 | if (fOnce) { |
| 98 | return; |
| 99 | } |
| 100 | fOnce = true; |
| 101 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 102 | test_cubic(); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 103 | test_cubic2(); |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | fShowHairline = false; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | fDStroke = 1; |
| 108 | fStroke = 10; |
| 109 | fMinStroke = 10; |
| 110 | fMaxStroke = 180; |
| 111 | |
| 112 | const int V = 85; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 117 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 129 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 137 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 138 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 140 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 141 | void nextStroke() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | fStroke += fDStroke; |
| 143 | if (fStroke > fMaxStroke || fStroke < fMinStroke) |
| 144 | fDStroke = -fDStroke; |
| 145 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | protected: |
| 148 | // overrides from SkEventSink |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 149 | virtual bool onQuery(SkEvent* evt) { |
| 150 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | SampleCode::TitleR(evt, "Paths"); |
| 152 | return true; |
| 153 | } |
| 154 | return this->INHERITED::onQuery(evt); |
| 155 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 157 | void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | paint.setAntiAlias(true); |
| 161 | paint.setStyle(SkPaint::kStroke_Style); |
| 162 | paint.setStrokeJoin(j); |
| 163 | paint.setStrokeWidth(SkIntToScalar(fStroke)); |
| 164 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 165 | if (fShowHairline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | SkPath fill; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 167 | |
| 168 | paint.getFillPath(path, &fill); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | paint.setStrokeWidth(0); |
| 170 | canvas->drawPath(fill, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 171 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | canvas->drawPath(path, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 173 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | paint.setColor(SK_ColorRED); |
| 176 | paint.setStrokeWidth(0); |
| 177 | canvas->drawPath(path, paint); |
| 178 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 180 | virtual void onDrawContent(SkCanvas* canvas) { |
| 181 | this->init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 190 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | canvas->save(); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 192 | for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | this->drawPath(canvas, fPath[j], gJoins[i]); |
| 194 | canvas->translate(SkIntToScalar(200), 0); |
| 195 | } |
| 196 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 197 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | canvas->translate(0, SkIntToScalar(200)); |
| 199 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 200 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | this->nextStroke(); |
| 202 | this->inval(NULL); |
| 203 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 204 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 205 | 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] | 206 | fShowHairline = !fShowHairline; |
| 207 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 208 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 210 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 212 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | ////////////////////////////////////////////////////////////////////////////// |
| 216 | |
| 217 | static SkView* MyFactory() { return new PathView; } |
| 218 | static SkViewRegister reg(MyFactory); |