commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPathUtils.h" |
| 11 | #include "SkView.h" |
| 12 | //#include "SkPathOps.h" // loads fine here, won't in PathUtils src files |
| 13 | #include "SkRandom.h" |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 14 | //#include "SkTime.h" |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 15 | |
| 16 | class samplePathUtils : public SampleView { |
| 17 | public: |
| 18 | samplePathUtils() { |
| 19 | bmp_paint.setAntiAlias(true); // Black paint for bitmap |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 20 | bmp_paint.setStyle(SkPaint::kFill_Style); |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 21 | bmp_paint.setColor(SK_ColorBLACK); |
| 22 | } |
| 23 | |
| 24 | protected: |
| 25 | static const int numModes = 3; |
robertphillips@google.com | 95faedb | 2013-07-01 23:15:57 +0000 | [diff] [blame] | 26 | static const int h=8, w=12, stride=2; // stride is in bytes |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 27 | static const int numChars = h * stride; // number of chars in entire array |
| 28 | |
| 29 | SkPaint bmp_paint; |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 31 | // overrides from SkEventSink |
| 32 | virtual bool onQuery(SkEvent* evt) { |
| 33 | if (SampleCode::TitleQ(*evt)) { |
| 34 | SampleCode::TitleR(evt, "PathUtils"); |
| 35 | return true; |
| 36 | } |
| 37 | return this->INHERITED::onQuery(evt); |
| 38 | } |
| 39 | |
| 40 | ///////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 42 | virtual void onDrawContent(SkCanvas* canvas) { |
| 43 | // bitmap definitions |
dierk@google.com | 74887b6 | 2013-07-01 19:30:14 +0000 | [diff] [blame] | 44 | const uint8_t bits[numModes][numChars] = { |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 45 | { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, |
| 46 | 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 48 | { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, |
| 49 | 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, |
| 50 | |
| 51 | { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, |
| 52 | 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } |
| 53 | }; |
| 54 | |
robertphillips@google.com | 95faedb | 2013-07-01 23:15:57 +0000 | [diff] [blame] | 55 | static const SkScalar kScale = 10.0f; |
| 56 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 57 | for (int i = 0; i < numModes; ++i) { |
| 58 | SkPath path; // generate and simplify each path |
| 59 | SkPathUtils::BitsToPath_Path(&path, (char*) &bits[i], h, w, stride); |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 61 | canvas->save(); // DRAWING |
robertphillips@google.com | 95faedb | 2013-07-01 23:15:57 +0000 | [diff] [blame] | 62 | canvas->scale(kScale, kScale); // scales up each bitmap |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 63 | canvas->translate(0, 1.5f * h * i); |
| 64 | canvas->drawPath(path, bmp_paint); // draw bitmap |
| 65 | canvas->restore(); |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 67 | // use the SkRegion method |
| 68 | SkPath pathR; |
| 69 | SkPathUtils::BitsToPath_Region(&pathR, (char*) &bits[i], h, w, stride); |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 70 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 71 | canvas->save(); |
robertphillips@google.com | 95faedb | 2013-07-01 23:15:57 +0000 | [diff] [blame] | 72 | |
| 73 | canvas->scale(kScale, kScale); // scales up each bitmap |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 74 | canvas->translate(1.5f * w, 1.5f * h * i); // translates past previous bitmap |
| 75 | canvas->drawPath(pathR, bmp_paint); // draw bitmap |
| 76 | canvas->restore(); |
| 77 | } |
| 78 | } |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 80 | private: |
| 81 | typedef SkView INHERITED; |
| 82 | }; |
| 83 | |
| 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | static SkView* MyFactory() { return new samplePathUtils; } |
| 87 | static SkViewRegister reg(MyFactory) |
| 88 | ; |