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