1 /* 2 * Copyright 2011 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 #include "SampleCode.h" 8 #include "SkView.h" 9 #include "SkCanvas.h" 10 #include "SkGraphics.h" 11 #include "SkPath.h" 12 #include "SkRandom.h" 13 #include "SkString.h" 14 #include "SkTime.h" 15 16 class PolyToPolyView : public SampleView { 17 public: 18 PolyToPolyView() { 19 // tests 20 { 21 SkPoint src[] = { { 0, 0 }, 22 { SK_Scalar1, 0 }, 23 { 0, SK_Scalar1 } }; 24 SkPoint dst[] = { { 0, 0 }, 25 { 2*SK_Scalar1, 0 }, 26 { 0, 2*SK_Scalar1 } }; 27 SkMatrix m1, m2; 28 29 (void) m1.setPolyToPoly(src, dst, 3); 30 31 m2.reset(); 32 m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX); 33 m2.set(SkMatrix::kMSkewX, dst[2].fX - dst[0].fX); 34 m2.set(SkMatrix::kMTransX, dst[0].fX); 35 m2.set(SkMatrix::kMSkewY, dst[1].fY - dst[0].fY); 36 m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY); 37 m2.set(SkMatrix::kMTransY, dst[0].fY); 38 39 m1.reset(); 40 41 const SkScalar src1[] = { 42 0, 0, 0, 427, 316, 427, 316, 0 43 }; 44 const SkScalar dst1[] = { 45 158, 177.5f, 158, 249.5f, 46 158, 604.5f, 158, -177.5f 47 }; 48 49 (void) m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4); 50 51 { 52 const SkPoint src[] = { 53 { SkIntToScalar(1), SkIntToScalar(0) }, 54 { SkIntToScalar(4), SkIntToScalar(7) }, 55 { SkIntToScalar(10), SkIntToScalar(2) } 56 }; 57 const SkPoint dst[] = { 58 { SkIntToScalar(4), SkIntToScalar(2) }, 59 { SkIntToScalar(45), SkIntToScalar(26) }, 60 { SkIntToScalar(32), SkIntToScalar(17) } 61 }; 62 63 SkMatrix m0; 64 m0.setPolyToPoly(src, dst, 3); 65 } 66 } 67 } 68 69 protected: 70 // overrides from SkEventSink 71 virtual bool onQuery(SkEvent* evt) { 72 if (SampleCode::TitleQ(*evt)) { 73 SampleCode::TitleR(evt, "PolyToPolyView"); 74 return true; 75 } 76 return this->INHERITED::onQuery(evt); 77 } 78 79 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], 80 const int idst[], int count) { 81 SkMatrix matrix; 82 SkPoint src[4], dst[4]; 83 84 for (int i = 0; i < count; i++) { 85 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); 86 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); 87 } 88 89 canvas->save(); 90 matrix.setPolyToPoly(src, dst, count); 91 canvas->concat(matrix); 92 93 paint->setColor(SK_ColorGRAY); 94 paint->setStyle(SkPaint::kStroke_Style); 95 const SkScalar D = SkIntToScalar(64); 96 canvas->drawRect(SkRect::MakeWH(D, D), *paint); 97 canvas->drawLine(0, 0, D, D, *paint); 98 canvas->drawLine(0, D, D, 0, *paint); 99 100 SkPaint::FontMetrics fm; 101 paint->getFontMetrics(&fm); 102 paint->setColor(SK_ColorRED); 103 paint->setStyle(SkPaint::kFill_Style); 104 SkScalar x = D/2; 105 float y = D/2 - (fm.fAscent + fm.fDescent)/2; 106 SkString str; 107 str.appendS32(count); 108 canvas->drawString(str, 109 x, y, 110 *paint); 111 112 canvas->restore(); 113 } 114 115 virtual void onDrawContent(SkCanvas* canvas) { 116 SkPaint paint; 117 paint.setAntiAlias(true); 118 paint.setStrokeWidth(SkIntToScalar(4)); 119 paint.setTextSize(SkIntToScalar(40)); 120 paint.setTextAlign(SkPaint::kCenter_Align); 121 122 canvas->save(); 123 canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); 124 // translate (1 point) 125 const int src1[] = { 0, 0 }; 126 const int dst1[] = { 5, 5 }; 127 doDraw(canvas, &paint, src1, dst1, 1); 128 canvas->restore(); 129 130 canvas->save(); 131 canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); 132 // rotate/uniform-scale (2 points) 133 const int src2[] = { 32, 32, 64, 32 }; 134 const int dst2[] = { 32, 32, 64, 48 }; 135 doDraw(canvas, &paint, src2, dst2, 2); 136 canvas->restore(); 137 138 canvas->save(); 139 canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); 140 // rotate/skew (3 points) 141 const int src3[] = { 0, 0, 64, 0, 0, 64 }; 142 const int dst3[] = { 0, 0, 96, 0, 24, 64 }; 143 doDraw(canvas, &paint, src3, dst3, 3); 144 canvas->restore(); 145 146 canvas->save(); 147 canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); 148 // perspective (4 points) 149 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; 150 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; 151 doDraw(canvas, &paint, src4, dst4, 4); 152 canvas->restore(); 153 } 154 155 private: 156 typedef SampleView INHERITED; 157 }; 158 159 ////////////////////////////////////////////////////////////////////////////// 160 161 static SkView* MyFactory() { return new PolyToPolyView; } 162 static SkViewRegister reg(MyFactory); 163