• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "Sample.h"
9 #include "SkCanvas.h"
10 #include "SkFontMetrics.h"
11 #include "SkGraphics.h"
12 #include "SkPath.h"
13 #include "SkRandom.h"
14 #include "SkString.h"
15 #include "SkTextUtils.h"
16 #include "SkTime.h"
17 
18 class PolyToPolyView : public Sample {
19 public:
PolyToPolyView()20     PolyToPolyView() {
21         // tests
22         {
23             SkPoint src[] = { { 0, 0 },
24                               { SK_Scalar1, 0 },
25                               { 0, SK_Scalar1 } };
26             SkPoint dst[] = { { 0, 0 },
27                               { 2*SK_Scalar1, 0 },
28                               { 0, 2*SK_Scalar1 } };
29             SkMatrix m1, m2;
30 
31             (void) m1.setPolyToPoly(src, dst, 3);
32 
33             m2.reset();
34             m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX);
35             m2.set(SkMatrix::kMSkewX,  dst[2].fX - dst[0].fX);
36             m2.set(SkMatrix::kMTransX, dst[0].fX);
37             m2.set(SkMatrix::kMSkewY,  dst[1].fY - dst[0].fY);
38             m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY);
39             m2.set(SkMatrix::kMTransY, dst[0].fY);
40 
41             m1.reset();
42 
43             const SkScalar src1[] = {
44                 0, 0, 0, 427, 316, 427, 316, 0
45             };
46             const SkScalar dst1[] = {
47                 158, 177.5f, 158, 249.5f,
48                 158, 604.5f, 158, -177.5f
49             };
50 
51             (void) m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4);
52 
53             {
54                 const SkPoint src[] = {
55                     { SkIntToScalar(1), SkIntToScalar(0) },
56                     { SkIntToScalar(4), SkIntToScalar(7) },
57                     { SkIntToScalar(10), SkIntToScalar(2) }
58                 };
59                 const SkPoint dst[] = {
60                     { SkIntToScalar(4), SkIntToScalar(2) },
61                     { SkIntToScalar(45), SkIntToScalar(26) },
62                     { SkIntToScalar(32), SkIntToScalar(17) }
63                 };
64 
65                 SkMatrix m0;
66                 m0.setPolyToPoly(src, dst, 3);
67             }
68         }
69     }
70 
71 protected:
onQuery(Sample::Event * evt)72     virtual bool onQuery(Sample::Event* evt)  {
73         if (Sample::TitleQ(*evt)) {
74             Sample::TitleR(evt, "PolyToPolyView");
75             return true;
76         }
77         return this->INHERITED::onQuery(evt);
78     }
79 
doDraw(SkCanvas * canvas,SkPaint * paint,const SkFont & font,const int isrc[],const int idst[],int count)80     static void doDraw(SkCanvas* canvas, SkPaint* paint, const SkFont& font, const int isrc[],
81                        const int idst[], int count) {
82         SkMatrix matrix;
83         SkPoint src[4], dst[4];
84 
85         for (int i = 0; i < count; i++) {
86             src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
87             dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
88         }
89 
90         canvas->save();
91         matrix.setPolyToPoly(src, dst, count);
92         canvas->concat(matrix);
93 
94         paint->setColor(SK_ColorGRAY);
95         paint->setStyle(SkPaint::kStroke_Style);
96         const SkScalar D = SkIntToScalar(64);
97         canvas->drawRect(SkRect::MakeWH(D, D), *paint);
98         canvas->drawLine(0, 0, D, D, *paint);
99         canvas->drawLine(0, D, D, 0, *paint);
100 
101         SkFontMetrics fm;
102         font.getMetrics(&fm);
103         paint->setColor(SK_ColorRED);
104         paint->setStyle(SkPaint::kFill_Style);
105         SkScalar x = D/2;
106         float y = D/2 - (fm.fAscent + fm.fDescent)/2;
107         SkString str;
108         str.appendS32(count);
109         SkTextUtils::DrawString(canvas, str.c_str(), x, y, font, *paint, SkTextUtils::kCenter_Align);
110 
111         canvas->restore();
112     }
113 
onDrawContent(SkCanvas * canvas)114     virtual void onDrawContent(SkCanvas* canvas) {
115         SkPaint paint;
116         paint.setAntiAlias(true);
117         paint.setStrokeWidth(SkIntToScalar(4));
118 
119         SkFont font;
120         font.setSize(40);
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, font, 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, font, 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, font, 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, font, src4, dst4, 4);
152         canvas->restore();
153     }
154 
155 private:
156     typedef Sample INHERITED;
157 };
158 
159 //////////////////////////////////////////////////////////////////////////////
160 
161 DEF_SAMPLE( return new PolyToPolyView(); )
162