Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions | Variables
glyph_pos.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRect.h"
#include "include/core/SkScalar.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"

Go to the source code of this file.

Functions

static void drawTestCase (SkCanvas *canvas, SkScalar textScale, SkScalar strokeWidth, SkPaint::Style strokeStyle)
 
static void draw_gm (SkCanvas *canvas, SkScalar strokeWidth, SkPaint::Style strokeStyle)
 
 DEF_SIMPLE_GM (glyph_pos_h_b, c, 800, 600)
 
 DEF_SIMPLE_GM (glyph_pos_n_b, c, 800, 600)
 
 DEF_SIMPLE_GM (glyph_pos_h_s, c, 800, 600)
 
 DEF_SIMPLE_GM (glyph_pos_n_s, c, 800, 600)
 
 DEF_SIMPLE_GM (glyph_pos_h_f, c, 800, 600)
 
 DEF_SIMPLE_GM (glyph_pos_n_f, c, 800, 600)
 

Variables

constexpr SkScalar kTextHeight = 14.0f
 
constexpr char kText [] = "Proportional Hamburgefons #% fi"
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/6]

DEF_SIMPLE_GM ( glyph_pos_h_b  ,
,
800  ,
600   
)

Definition at line 150 of file glyph_pos.cpp.

150 {
152}
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition SkPaint.h:195
static void draw_gm(SkCanvas *canvas, SkScalar strokeWidth, SkPaint::Style strokeStyle)
Definition glyph_pos.cpp:35

◆ DEF_SIMPLE_GM() [2/6]

DEF_SIMPLE_GM ( glyph_pos_h_f  ,
,
800  ,
600   
)

Definition at line 162 of file glyph_pos.cpp.

162 {
164}
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193

◆ DEF_SIMPLE_GM() [3/6]

DEF_SIMPLE_GM ( glyph_pos_h_s  ,
,
800  ,
600   
)

Definition at line 156 of file glyph_pos.cpp.

156 {
158}
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194

◆ DEF_SIMPLE_GM() [4/6]

DEF_SIMPLE_GM ( glyph_pos_n_b  ,
,
800  ,
600   
)

Definition at line 153 of file glyph_pos.cpp.

153 {
155}

◆ DEF_SIMPLE_GM() [5/6]

DEF_SIMPLE_GM ( glyph_pos_n_f  ,
,
800  ,
600   
)

Definition at line 165 of file glyph_pos.cpp.

165 {
167}

◆ DEF_SIMPLE_GM() [6/6]

DEF_SIMPLE_GM ( glyph_pos_n_s  ,
,
800  ,
600   
)

Definition at line 159 of file glyph_pos.cpp.

159 {
161}

◆ draw_gm()

static void draw_gm ( SkCanvas canvas,
SkScalar  strokeWidth,
SkPaint::Style  strokeStyle 
)
static

Definition at line 35 of file glyph_pos.cpp.

37 {
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}
static const int strokeWidth
Definition BlurTest.cpp:60
#define SkScalarInvert(x)
Definition SkScalar.h:73
void restore()
Definition SkCanvas.cpp:465
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void concat(const SkMatrix &matrix)
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
static void drawTestCase(SkCanvas *canvas, SkScalar textScale, SkScalar strokeWidth, SkPaint::Style strokeStyle)
Definition glyph_pos.cpp:88

◆ drawTestCase()

static void drawTestCase ( SkCanvas canvas,
SkScalar  textScale,
SkScalar  strokeWidth,
SkPaint::Style  strokeStyle 
)
static

Definition at line 88 of file glyph_pos.cpp.

91 {
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}
uint16_t glyphs[5]
int count
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
uint16_t SkGlyphID
Definition SkTypes.h:179
const SkScalar widths[]
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
SkMatrix getLocalToDeviceAs3x3() const
Definition SkCanvas.h:2222
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
@ 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
const Paint & paint
float SkScalar
Definition extension.cpp:12
constexpr SkScalar kTextHeight
Definition glyph_pos.cpp:27
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

Variable Documentation

◆ kText

constexpr char kText[] = "Proportional Hamburgefons #% fi"
constexpr

Definition at line 28 of file glyph_pos.cpp.

◆ kTextHeight

constexpr SkScalar kTextHeight = 14.0f
constexpr

Definition at line 27 of file glyph_pos.cpp.