Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
poly2poly.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
24#include "tools/Resources.h"
26
27#include <stdint.h>
28
29class Poly2PolyGM : public skiagm::GM {
30public:
32
33protected:
34 SkString getName() const override { return SkString("poly2poly"); }
35
36 SkISize getISize() override { return SkISize::Make(835, 840); }
37
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);
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
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,
68 canvas->restore();
69 }
70
71 void onOnceBeforeDraw() override {
72 fEmFace = ToolUtils::CreateTypefaceFromResource("fonts/Em.ttf");
73 if (!fEmFace) {
75 }
76 }
77
78 void onDraw(SkCanvas* canvas) override {
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
117private:
118 using INHERITED = skiagm::GM;
119 sk_sp<SkTypeface> fEmFace;
120};
121
122//////////////////////////////////////////////////////////////////////////////
123
124DEF_GM( return new Poly2PolyGM; )
int count
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
@ kGlyphID
uses two byte words to represent glyph indices
#define SkIntToScalar(x)
Definition SkScalar.h:57
SkISize getISize() override
Definition poly2poly.cpp:36
SkString getName() const override
Definition poly2poly.cpp:34
static void doDraw(SkCanvas *canvas, const SkFont &font, SkPaint *paint, const int isrc[], const int idst[], int count)
Definition poly2poly.cpp:38
void onOnceBeforeDraw() override
Definition poly2poly.cpp:71
void onDraw(SkCanvas *canvas) override
Definition poly2poly.cpp:78
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
int save()
Definition SkCanvas.cpp:451
void concat(const SkMatrix &matrix)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
static void Draw(SkCanvas *, const void *text, size_t size, SkTextEncoding, SkScalar x, SkScalar y, const SkFont &, const SkPaint &, Align=kLeft_Align)
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)
SkScalar fAscent
distance to reserve above baseline, typically negative
SkScalar fDescent
distance to reserve below baseline, typically positive
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609