Flutter Engine
The Flutter Engine
glyph_pos.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
15#include "include/core/SkRect.h"
19#include "tools/ToolUtils.h"
21
22/* This test tries to define the effect of using hairline strokes on text.
23 * Provides non-hairline images for reference and consistency checks.
24 * glyph_pos_(h/n)_(s/f/b)
25 * -> test hairline/non-hairline stroke/fill/stroke+fill.
26 */
27constexpr SkScalar kTextHeight = 14.0f;
28constexpr char kText[] = "Proportional Hamburgefons #% fi";
29
30static void drawTestCase(SkCanvas* canvas,
31 SkScalar textScale,
33 SkPaint::Style strokeStyle);
34
35static void draw_gm(SkCanvas* canvas,
37 SkPaint::Style strokeStyle) {
38 // There's a black pixel at 40, 40 for reference.
39 canvas->drawPoint(40, 40, SkPaint());
40
41 // Two reference images.
42 canvas->translate(50.0f, 50.0f);
43 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
44
45 canvas->translate(0.0f, 50.0f);
46 drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
47
48 // Uniform scaling test.
49 canvas->translate(0.0f, 100.0f);
50 canvas->save();
51 canvas->scale(3.0f, 3.0f);
52 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
53 canvas->restore();
54
55 // Non-uniform scaling test.
56 canvas->translate(0.0f, 100.0f);
57 canvas->save();
58 canvas->scale(3.0f, 6.0f);
59 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
60 canvas->restore();
61
62 // Skew test.
63 canvas->translate(0.0f, 80.0f);
64 canvas->save();
65 canvas->scale(3.0f, 3.0f);
66 SkMatrix skew;
67 skew.setIdentity();
68 skew.setSkewX(8.0f / 25.0f);
69 skew.setSkewY(2.0f / 25.0f);
70 canvas->concat(skew);
71 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
72 canvas->restore();
73
74 // Perspective test.
75 canvas->translate(0.0f, 80.0f);
76 canvas->save();
77 SkMatrix perspective;
78 perspective.setIdentity();
79 perspective.setPerspX(-SkScalarInvert(340));
80 perspective.setSkewX(8.0f / 25.0f);
81 perspective.setSkewY(2.0f / 25.0f);
82
83 canvas->concat(perspective);
84 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
85 canvas->restore();
86}
87
88static void drawTestCase(SkCanvas* canvas,
89 SkScalar textScale,
91 SkPaint::Style strokeStyle) {
93 paint.setColor(SK_ColorBLACK);
94 paint.setAntiAlias(true);
95 paint.setStrokeWidth(strokeWidth);
96 paint.setStyle(strokeStyle);
97
99
100 // This demonstrates that we can not measure the text if
101 // there's a device transform. The canvas total matrix will
102 // end up being a device transform.
103 bool drawRef = !(canvas->getLocalToDeviceAs3x3().getType() &
105
107 if (drawRef) {
108 SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
109 &bounds, &paint);
110
111 paint.setStrokeWidth(0.0f);
113
114 // Green box is the measured text bounds.
115 paint.setColor(SK_ColorGREEN);
116 canvas->drawRect(bounds, paint);
117
118 // Red line is the measured advance from the 0,0 of the text position.
119 paint.setColor(SK_ColorRED);
120 canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
121 }
122
123 // Black text is the testcase, eg. the text.
124 paint.setColor(SK_ColorBLACK);
125 paint.setStrokeWidth(strokeWidth);
126 paint.setStyle(strokeStyle);
127 canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
128 0.0f, 0.0f, font, paint);
129
130 if (drawRef) {
131 const size_t len = sizeof(kText) - 1;
133 const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len);
134 SkScalar widths[len]; // len is conservative. we really only need 'count'
135 font.getWidthsBounds(glyphs, count, widths, nullptr, &paint);
136
137 paint.setStrokeWidth(0.0f);
139
140 // Magenta lines are the positions for the characters.
141 paint.setColor(SK_ColorMAGENTA);
142 SkScalar w = 0;
143 for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
144 canvas->drawLine(w, 0.0f, w, 5.0f, paint);
145 w += widths[i];
146 }
147 }
148}
149
150DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
152}
153DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
155}
156DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
158}
159DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
161}
162DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
164}
165DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
167}
static const int strokeWidth
Definition: BlurTest.cpp:60
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
int count
Definition: FontMgrTest.cpp:50
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkScalarInvert(x)
Definition: SkScalar.h:73
uint16_t SkGlyphID
Definition: SkTypes.h:179
const SkScalar widths[]
Definition: StrokerTest.cpp:39
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2695
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
SkMatrix getLocalToDeviceAs3x3() const
Definition: SkCanvas.h:2222
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
Definition: SkFont.h:35
SkMatrix & setSkewX(SkScalar v)
Definition: SkMatrix.h:518
SkMatrix & setPerspX(SkScalar v)
Definition: SkMatrix.h:537
SkMatrix & setIdentity()
Definition: SkMatrix.h:626
SkMatrix & setSkewY(SkScalar v)
Definition: SkMatrix.h:512
@ kTranslate_Mask
translation SkMatrix
Definition: SkMatrix.h:193
@ kIdentity_Mask
identity SkMatrix; all bits clear
Definition: SkMatrix.h:192
TypeMask getType() const
Definition: SkMatrix.h:207
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition: SkPaint.h:195
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
constexpr SkScalar kTextHeight
Definition: glyph_pos.cpp:27
static void drawTestCase(SkCanvas *canvas, SkScalar textScale, SkScalar strokeWidth, SkPaint::Style strokeStyle)
Definition: glyph_pos.cpp:88
static void draw_gm(SkCanvas *canvas, SkScalar strokeWidth, SkPaint::Style strokeStyle)
Definition: glyph_pos.cpp:35
DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600)
Definition: glyph_pos.cpp:150
constexpr char kText[]
Definition: glyph_pos.cpp:28
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< SkTypeface > DefaultPortableTypeface()
font
Font Metadata and Metrics.
SkScalar w