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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontMetrics.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/utils/SkTextUtils.h"
24 #include "tools/Resources.h"
25 
26 #include <stdint.h>
27 
28 class Poly2PolyGM : public skiagm::GM {
29 public:
Poly2PolyGM()30     Poly2PolyGM() {}
31 
32 protected:
33 
onShortName()34     SkString onShortName() override {
35         return SkString("poly2poly");
36     }
37 
onISize()38     SkISize onISize() override {
39         return SkISize::Make(835, 840);
40     }
41 
doDraw(SkCanvas * canvas,const SkFont & font,SkPaint * paint,const int isrc[],const int idst[],int count)42     static void doDraw(SkCanvas* canvas, const SkFont& font, SkPaint* paint, const int isrc[],
43                        const int idst[], int count) {
44         SkMatrix matrix;
45         SkPoint src[4], dst[4];
46 
47         for (int i = 0; i < count; i++) {
48             src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
49             dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
50         }
51 
52         canvas->save();
53         matrix.setPolyToPoly(src, dst, count);
54         canvas->concat(matrix);
55 
56         paint->setColor(SK_ColorGRAY);
57         paint->setStyle(SkPaint::kStroke_Style);
58         const SkScalar D = 64;
59         canvas->drawRect(SkRect::MakeWH(D, D), *paint);
60         canvas->drawLine(0, 0, D, D, *paint);
61         canvas->drawLine(0, D, D, 0, *paint);
62 
63         SkFontMetrics fm;
64         font.getMetrics(&fm);
65         paint->setColor(SK_ColorRED);
66         paint->setStyle(SkPaint::kFill_Style);
67         SkScalar x = D/2;
68         SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
69         uint16_t glyphID = 3; // X
70         SkTextUtils::Draw(canvas, &glyphID, sizeof(glyphID), SkTextEncoding::kGlyphID, x, y,
71                           font, *paint, SkTextUtils::kCenter_Align);
72         canvas->restore();
73     }
74 
onOnceBeforeDraw()75     void onOnceBeforeDraw() override {
76         fEmFace = MakeResourceAsTypeface("fonts/Em.ttf");
77     }
78 
onDraw(SkCanvas * canvas)79     void onDraw(SkCanvas* canvas) override {
80         SkPaint paint;
81         paint.setAntiAlias(true);
82         paint.setStrokeWidth(SkIntToScalar(4));
83         SkFont font(fEmFace, 40);
84 
85         canvas->save();
86         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
87         // translate (1 point)
88         const int src1[] = { 0, 0 };
89         const int dst1[] = { 5, 5 };
90         doDraw(canvas, font, &paint, src1, dst1, 1);
91         canvas->restore();
92 
93         canvas->save();
94         canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
95         // rotate/uniform-scale (2 points)
96         const int src2[] = { 32, 32, 64, 32 };
97         const int dst2[] = { 32, 32, 64, 48 };
98         doDraw(canvas, font, &paint, src2, dst2, 2);
99         canvas->restore();
100 
101         canvas->save();
102         canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
103         // rotate/skew (3 points)
104         const int src3[] = { 0, 0, 64, 0, 0, 64 };
105         const int dst3[] = { 0, 0, 96, 0, 24, 64 };
106         doDraw(canvas, font, &paint, src3, dst3, 3);
107         canvas->restore();
108 
109         canvas->save();
110         canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
111         // perspective (4 points)
112         const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
113         const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
114         doDraw(canvas, font, &paint, src4, dst4, 4);
115         canvas->restore();
116     }
117 
118 private:
119     using INHERITED = skiagm::GM;
120     sk_sp<SkTypeface> fEmFace;
121 };
122 
123 //////////////////////////////////////////////////////////////////////////////
124 
125 DEF_GM( return new Poly2PolyGM; )
126