reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 1 | #include "gm.h" |
| 2 | |
| 3 | namespace skiagm { |
| 4 | |
| 5 | class Poly2PolyGM : public GM { |
| 6 | public: |
| 7 | Poly2PolyGM() {} |
| 8 | |
| 9 | protected: |
| 10 | virtual SkString onShortName() { |
| 11 | return SkString("poly2poly"); |
| 12 | } |
| 13 | |
| 14 | virtual SkISize onISize() { |
| 15 | return make_isize(835, 840); |
| 16 | } |
| 17 | |
| 18 | void drawBG(SkCanvas* canvas) { |
| 19 | canvas->drawColor(SK_ColorWHITE); |
| 20 | } |
| 21 | |
| 22 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], |
| 23 | const int idst[], int count) { |
| 24 | SkMatrix matrix; |
| 25 | SkPoint src[4], dst[4]; |
| 26 | |
| 27 | for (int i = 0; i < count; i++) { |
| 28 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); |
| 29 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); |
| 30 | } |
| 31 | |
| 32 | canvas->save(); |
| 33 | matrix.setPolyToPoly(src, dst, count); |
| 34 | canvas->concat(matrix); |
| 35 | |
| 36 | paint->setColor(SK_ColorGRAY); |
| 37 | paint->setStyle(SkPaint::kStroke_Style); |
| 38 | const SkScalar D = SkIntToScalar(64); |
| 39 | canvas->drawRectCoords(0, 0, D, D, *paint); |
| 40 | canvas->drawLine(0, 0, D, D, *paint); |
| 41 | canvas->drawLine(0, D, D, 0, *paint); |
| 42 | |
| 43 | SkPaint::FontMetrics fm; |
| 44 | paint->getFontMetrics(&fm); |
| 45 | paint->setColor(SK_ColorRED); |
| 46 | paint->setStyle(SkPaint::kFill_Style); |
| 47 | SkScalar x = D/2; |
| 48 | float y = D/2 - (fm.fAscent + fm.fDescent)/2; |
| 49 | SkString str; |
| 50 | str.appendS32(count); |
| 51 | canvas->drawText(str.c_str(), str.size(), x, y, *paint); |
| 52 | |
| 53 | canvas->restore(); |
| 54 | } |
| 55 | |
| 56 | virtual void onDraw(SkCanvas* canvas) { |
| 57 | this->drawBG(canvas); |
| 58 | |
| 59 | SkPaint paint; |
| 60 | paint.setAntiAlias(true); |
| 61 | paint.setStrokeWidth(SkIntToScalar(4)); |
| 62 | paint.setTextSize(SkIntToScalar(40)); |
| 63 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 64 | |
| 65 | canvas->save(); |
| 66 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 67 | // translate (1 point) |
| 68 | const int src1[] = { 0, 0 }; |
| 69 | const int dst1[] = { 5, 5 }; |
| 70 | doDraw(canvas, &paint, src1, dst1, 1); |
| 71 | canvas->restore(); |
| 72 | |
| 73 | canvas->save(); |
| 74 | canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); |
| 75 | // rotate/uniform-scale (2 points) |
| 76 | const int src2[] = { 32, 32, 64, 32 }; |
| 77 | const int dst2[] = { 32, 32, 64, 48 }; |
| 78 | doDraw(canvas, &paint, src2, dst2, 2); |
| 79 | canvas->restore(); |
| 80 | |
| 81 | canvas->save(); |
| 82 | canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); |
| 83 | // rotate/skew (3 points) |
| 84 | const int src3[] = { 0, 0, 64, 0, 0, 64 }; |
| 85 | const int dst3[] = { 0, 0, 96, 0, 24, 64 }; |
| 86 | doDraw(canvas, &paint, src3, dst3, 3); |
| 87 | canvas->restore(); |
| 88 | |
| 89 | canvas->save(); |
| 90 | canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); |
| 91 | // perspective (4 points) |
| 92 | const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; |
| 93 | const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; |
| 94 | doDraw(canvas, &paint, src4, dst4, 4); |
| 95 | canvas->restore(); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | typedef GM INHERITED; |
| 100 | }; |
| 101 | |
| 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | static GM* MyFactory(void*) { return new Poly2PolyGM; } |
| 105 | static GMRegistry reg(MyFactory); |
| 106 | |
| 107 | } |
| 108 | |