reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | #include "SampleCode.h" |
| 3 | #include "SkView.h" |
| 4 | #include "SkCanvas.h" |
| 5 | #include "SkGradientShader.h" |
| 6 | #include "SkGraphics.h" |
| 7 | #include "SkImageDecoder.h" |
| 8 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkRegion.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkUtils.h" |
| 12 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
| 14 | #include "SkColorFilter.h" |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 15 | #include "SkParsePath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkTime.h" |
| 17 | #include "SkTypeface.h" |
| 18 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 19 | #include "SkGeometry.h" |
| 20 | |
| 21 | // http://code.google.com/p/skia/issues/detail?id=32 |
| 22 | static void test_cubic() { |
| 23 | SkPoint src[4] = { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 24 | { 556.25000f, 523.03003f }, |
| 25 | { 556.23999f, 522.96002f }, |
| 26 | { 556.21997f, 522.89001f }, |
| 27 | { 556.21997f, 522.82001f } |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 28 | }; |
| 29 | SkPoint dst[11]; |
| 30 | dst[10].set(42, -42); // one past the end, that we don't clobber these |
| 31 | SkScalar tval[] = { 0.33333334f, 0.99999994f }; |
| 32 | |
| 33 | SkChopCubicAt(src, dst, tval, 2); |
| 34 | |
| 35 | #if 0 |
| 36 | for (int i = 0; i < 11; i++) { |
| 37 | SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY); |
| 38 | } |
| 39 | #endif |
| 40 | } |
| 41 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 42 | static void test_cubic2() { |
| 43 | const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z"; |
| 44 | SkPath path; |
| 45 | SkParsePath::FromSVGString(str, &path); |
| 46 | |
| 47 | { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 48 | #ifdef SK_BUILD_FOR_WIN |
| 49 | // windows doesn't have strtof |
| 50 | float x = (float)strtod("9.94099e+07", NULL); |
| 51 | #else |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 52 | float x = strtof("9.94099e+07", NULL); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 53 | #endif |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 54 | int ix = (int)x; |
| 55 | int fx = (int)(x * 65536); |
| 56 | int ffx = SkScalarToFixed(x); |
| 57 | printf("%g %x %x %x\n", x, ix, fx, ffx); |
| 58 | |
| 59 | SkRect r = path.getBounds(); |
| 60 | SkIRect ir; |
| 61 | r.round(&ir); |
| 62 | printf("[%g %g %g %g] [%x %x %x %x]\n", |
| 63 | r.fLeft, r.fTop, r.fRight, r.fBottom, |
| 64 | ir.fLeft, ir.fTop, ir.fRight, ir.fBottom); |
| 65 | } |
| 66 | |
| 67 | SkBitmap bitmap; |
| 68 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 300, 200); |
| 69 | bitmap.allocPixels(); |
| 70 | |
| 71 | SkCanvas canvas(bitmap); |
| 72 | SkPaint paint; |
| 73 | paint.setAntiAlias(true); |
| 74 | canvas.drawPath(path, paint); |
| 75 | } |
| 76 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 77 | class PathView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | public: |
| 79 | int fDStroke, fStroke, fMinStroke, fMaxStroke; |
| 80 | SkPath fPath[6]; |
| 81 | bool fShowHairline; |
| 82 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 83 | PathView() { |
| 84 | test_cubic(); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 85 | test_cubic2(); |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 86 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | fShowHairline = false; |
| 88 | |
| 89 | fDStroke = 1; |
| 90 | fStroke = 10; |
| 91 | fMinStroke = 10; |
| 92 | fMaxStroke = 180; |
| 93 | |
| 94 | const int V = 85; |
| 95 | |
| 96 | fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70)); |
| 97 | fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1); |
| 98 | fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70)); |
| 99 | |
| 100 | fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70)); |
| 101 | fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1); |
| 102 | fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70)); |
| 103 | |
| 104 | fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V)); |
| 105 | fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 106 | fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 107 | |
| 108 | fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 109 | fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 110 | fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V)); |
| 111 | |
| 112 | fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 113 | fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 114 | fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50)); |
| 115 | |
| 116 | fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50)); |
| 117 | fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 118 | fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50)); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 119 | |
| 120 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 123 | void nextStroke() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | fStroke += fDStroke; |
| 125 | if (fStroke > fMaxStroke || fStroke < fMinStroke) |
| 126 | fDStroke = -fDStroke; |
| 127 | } |
| 128 | |
| 129 | protected: |
| 130 | // overrides from SkEventSink |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 131 | virtual bool onQuery(SkEvent* evt) { |
| 132 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | SampleCode::TitleR(evt, "Paths"); |
| 134 | return true; |
| 135 | } |
| 136 | return this->INHERITED::onQuery(evt); |
| 137 | } |
| 138 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 139 | void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | SkPaint paint; |
| 141 | |
| 142 | paint.setAntiAlias(true); |
| 143 | paint.setStyle(SkPaint::kStroke_Style); |
| 144 | paint.setStrokeJoin(j); |
| 145 | paint.setStrokeWidth(SkIntToScalar(fStroke)); |
| 146 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 147 | if (fShowHairline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | SkPath fill; |
| 149 | |
| 150 | paint.getFillPath(path, &fill); |
| 151 | paint.setStrokeWidth(0); |
| 152 | canvas->drawPath(fill, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 153 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | canvas->drawPath(path, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 155 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | |
| 157 | paint.setColor(SK_ColorRED); |
| 158 | paint.setStrokeWidth(0); |
| 159 | canvas->drawPath(path, paint); |
| 160 | } |
| 161 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 162 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | canvas->translate(SkIntToScalar(50), SkIntToScalar(50)); |
| 164 | |
| 165 | static const SkPaint::Join gJoins[] = { |
| 166 | SkPaint::kBevel_Join, |
| 167 | SkPaint::kMiter_Join, |
| 168 | SkPaint::kRound_Join |
| 169 | }; |
| 170 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 171 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | canvas->save(); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 173 | for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | this->drawPath(canvas, fPath[j], gJoins[i]); |
| 175 | canvas->translate(SkIntToScalar(200), 0); |
| 176 | } |
| 177 | canvas->restore(); |
| 178 | |
| 179 | canvas->translate(0, SkIntToScalar(200)); |
| 180 | } |
| 181 | |
| 182 | this->nextStroke(); |
| 183 | this->inval(NULL); |
| 184 | } |
| 185 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 186 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | fShowHairline = !fShowHairline; |
| 188 | this->inval(NULL); |
| 189 | return this->INHERITED::onFindClickHandler(x, y); |
| 190 | } |
| 191 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 193 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | ////////////////////////////////////////////////////////////////////////////// |
| 197 | |
| 198 | static SkView* MyFactory() { return new PathView; } |
| 199 | static SkViewRegister reg(MyFactory); |
| 200 | |