Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 | |
| 9 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkCornerPathEffect.h" | |
| 12 #include "SkDashPathEffect.h" | |
| 10 #include "SkPathUtils.h" | 13 #include "SkPathUtils.h" |
| 14 #include "SkRandom.h" | |
| 11 #include "SkView.h" | 15 #include "SkView.h" |
| 12 //#include "SkPathOps.h" // loads fine here, won't in PathUtils src files | |
| 13 #include "SkRandom.h" | |
| 14 //#include "SkTime.h" | |
| 15 | 16 |
| 16 class samplePathUtils : public SampleView { | 17 typedef void (*BitsToPath)(SkPath*, const char*, int, int, int); |
| 18 | |
| 19 static const BitsToPath gBitsToPath_fns[] = { | |
| 20 SkPathUtils::BitsToPath_Path, | |
| 21 SkPathUtils::BitsToPath_Region, | |
| 22 }; | |
| 23 | |
| 24 // hardcoded bitmap patterns | |
| 25 static const uint8_t gBits[][16] = { | |
| 26 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, | |
| 27 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, | |
| 28 | |
| 29 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, | |
| 30 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, | |
| 31 | |
| 32 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, | |
| 33 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } | |
| 34 }; | |
| 35 | |
| 36 | |
| 37 class SamplePathUtils : public SampleView { | |
| 17 public: | 38 public: |
| 18 samplePathUtils() { | 39 static const int fNumBits = 3; |
| 19 bmp_paint.setAntiAlias(true); // Black paint for bitmap | 40 static const int fH = 8, fW = 12; |
| 20 bmp_paint.setStyle(SkPaint::kFill_Style); | 41 static const size_t fRowBytes = 2; |
| 21 bmp_paint.setColor(SK_ColorBLACK); | 42 static const int fNumChars = fH * fRowBytes; |
| 43 | |
| 44 SkPaint fBmpPaint; | |
| 45 SkScalar fPhase; | |
| 46 | |
| 47 SamplePathUtils() { | |
| 48 fBmpPaint.setAntiAlias(true); // Black paint for bitmap | |
| 49 fBmpPaint.setStyle(SkPaint::kFill_Style); | |
| 50 | |
| 51 fPhase = 0.0f; // to animate the dashed path | |
| 22 } | 52 } |
| 23 | 53 |
| 24 protected: | 54 protected: |
| 25 static const int numModes = 3; | |
| 26 static const int h=8, w=12, stride=2; // stride is in bytes | |
| 27 static const int numChars = h * stride; // number of chars in entire array | |
| 28 | |
| 29 SkPaint bmp_paint; | |
| 30 | |
| 31 // overrides from SkEventSink | 55 // overrides from SkEventSink |
| 32 virtual bool onQuery(SkEvent* evt) { | 56 virtual bool onQuery(SkEvent* evt) { |
| 33 if (SampleCode::TitleQ(*evt)) { | 57 if (SampleCode::TitleQ(*evt)) { |
| 34 SampleCode::TitleR(evt, "PathUtils"); | 58 SampleCode::TitleR(evt, "PathUtils"); |
| 35 return true; | 59 return true; |
| 36 } | 60 } |
| 37 return this->INHERITED::onQuery(evt); | 61 return this->INHERITED::onQuery(evt); |
| 38 } | 62 } |
| 39 | 63 |
| 40 ///////////////////////////////////////////////////////////// | 64 ///////////////////////////////////////////////////////////// |
| 41 | 65 |
| 42 virtual void onDrawContent(SkCanvas* canvas) { | 66 virtual void onDrawContent(SkCanvas* canvas) { |
| 43 // bitmap definitions | 67 SkScalar intervals[8] = {.5, .3, .5, .3, .5, .3, .5, .3}; |
| 44 const uint8_t bits[numModes][numChars] = { | 68 SkDashPathEffect dash(intervals, 2, fPhase); |
| 45 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, | 69 SkCornerPathEffect corner(.25f); |
| 46 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, | 70 SkComposePathEffect compose(&dash, &corner); |
| 47 | 71 |
| 48 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, | 72 SkPaint OutlinePaint; |
|
scroggo
2013/07/09 15:34:22
Local variables should be lowercase (e.g. outlineP
dierk
2013/07/09 15:42:02
Done.
| |
| 49 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, | 73 OutlinePaint.setAntiAlias(true); // dashed paint for bitmap |
| 74 OutlinePaint.setStyle(SkPaint::kStroke_Style); | |
| 75 OutlinePaint.setPathEffect(&compose); | |
| 50 | 76 |
| 51 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, | 77 canvas->scale(10.0f, 10.0f); // scales up |
| 52 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } | |
| 53 }; | |
| 54 | 78 |
| 55 static const SkScalar kScale = 10.0f; | 79 for (int i = 0; i < fNumBits; ++i) { |
| 80 canvas->save(); | |
| 81 for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) { | |
| 82 SkPath path; | |
| 83 gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes); | |
| 56 | 84 |
| 57 for (int i = 0; i < numModes; ++i) { | 85 //draw skPath and outline |
| 58 SkPath path; // generate and simplify each path | 86 canvas->drawPath(path, fBmpPaint); |
| 59 SkPathUtils::BitsToPath_Path(&path, (char*) &bits[i], h, w, stride); | 87 canvas->translate(1.5f * fW, 0); // translates past previous bit map |
| 88 canvas->drawPath(path, OutlinePaint); | |
| 89 canvas->translate(1.5f * fW, 0); // translates past previous bit map | |
| 90 } | |
| 91 canvas->restore(); | |
| 92 canvas->translate(0, 1.5f * fH); //translate to next row | |
| 93 } | |
| 60 | 94 |
| 61 canvas->save(); // DRAWING | 95 // for animated pathEffect |
| 62 canvas->scale(kScale, kScale); // scales up each bitmap | 96 fPhase += .01; |
| 63 canvas->translate(0, 1.5f * h * i); | 97 this->inval(NULL); |
| 64 canvas->drawPath(path, bmp_paint); // draw bitmap | |
| 65 canvas->restore(); | |
| 66 | |
| 67 // use the SkRegion method | |
| 68 SkPath pathR; | |
| 69 SkPathUtils::BitsToPath_Region(&pathR, (char*) &bits[i], h, w, strid e); | |
| 70 | |
| 71 canvas->save(); | |
| 72 | |
| 73 canvas->scale(kScale, kScale); // scales up each bitmap | |
| 74 canvas->translate(1.5f * w, 1.5f * h * i); // translates past previo us bitmap | |
| 75 canvas->drawPath(pathR, bmp_paint); // draw bitmap | |
| 76 canvas->restore(); | |
| 77 } | |
| 78 } | 98 } |
| 79 | 99 |
| 80 private: | 100 private: |
| 81 typedef SkView INHERITED; | 101 typedef SkView INHERITED; |
| 82 }; | 102 }; |
| 83 | 103 |
| 84 ////////////////////////////////////////////////////////////////////////////// | 104 ////////////////////////////////////////////////////////////////////////////// |
| 85 | 105 |
| 86 static SkView* MyFactory() { return new samplePathUtils; } | 106 static SkView* MyFactory() { return new SamplePathUtils; } |
| 87 static SkViewRegister reg(MyFactory) | 107 static SkViewRegister reg(MyFactory) |
| 88 ; | 108 ; |
| OLD | NEW |