Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkFont.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
9
14#include "include/core/SkPath.h"
16#include "include/core/SkRect.h"
27#include "src/base/SkUTF.h"
28#include "src/core/SkFontPriv.h"
29#include "src/core/SkGlyph.h"
32#include "src/core/SkStrike.h"
34
35#include <algorithm>
36#include <cstddef>
37#include <cstdint>
38#include <utility>
39
40using namespace skia_private;
41
42#define kDefault_Size SkPaintDefaults_TextSize
43#define kDefault_Flags SkFont::kBaselineSnap_PrivFlag
44#define kDefault_Edging SkFont::Edging::kAntiAlias
45#define kDefault_Hinting SkPaintDefaults_Hinting
46
47static inline SkScalar valid_size(SkScalar size) {
48 return std::max<SkScalar>(0, size);
49}
50
52 : fTypeface(std::move(face))
53 , fSize(valid_size(size))
54 , fScaleX(scaleX)
55 , fSkewX(skewX)
57 , fEdging(static_cast<unsigned>(kDefault_Edging))
58 , fHinting(static_cast<unsigned>(kDefault_Hinting)) {
59 if (!fTypeface) {
60 fTypeface = SkTypeface::MakeEmpty();
61 }
62}
63
64SkFont::SkFont(sk_sp<SkTypeface> face, SkScalar size) : SkFont(std::move(face), size, 1, 0) {}
65
67
69
70bool SkFont::operator==(const SkFont& b) const {
71 return fTypeface.get() == b.fTypeface.get() &&
72 fSize == b.fSize &&
73 fScaleX == b.fScaleX &&
74 fSkewX == b.fSkewX &&
75 fFlags == b.fFlags &&
76 fEdging == b.fEdging &&
77 fHinting == b.fHinting;
78}
79
80void SkFont::dump() const {
81 SkDebugf("typeface %p\n", fTypeface.get());
82 SkDebugf("size %g\n", fSize);
83 SkDebugf("skewx %g\n", fSkewX);
84 SkDebugf("scalex %g\n", fScaleX);
85 SkDebugf("flags 0x%X\n", fFlags);
86 SkDebugf("edging %u\n", (unsigned)fEdging);
87 SkDebugf("hinting %u\n", (unsigned)fHinting);
88}
89
91 fTypeface = std::move(tf);
92 if (!fTypeface) {
93 fTypeface = SkTypeface::MakeEmpty();
94 }
95}
96
97///////////////////////////////////////////////////////////////////////////////////////////////////
98
99static inline uint32_t set_clear_mask(uint32_t bits, bool cond, uint32_t mask) {
100 return cond ? bits | mask : bits & ~mask;
101}
102
103void SkFont::setForceAutoHinting(bool predicate) {
104 fFlags = set_clear_mask(fFlags, predicate, kForceAutoHinting_PrivFlag);
105}
106void SkFont::setEmbeddedBitmaps(bool predicate) {
107 fFlags = set_clear_mask(fFlags, predicate, kEmbeddedBitmaps_PrivFlag);
108}
109void SkFont::setSubpixel(bool predicate) {
110 fFlags = set_clear_mask(fFlags, predicate, kSubpixel_PrivFlag);
111}
112void SkFont::setLinearMetrics(bool predicate) {
113 fFlags = set_clear_mask(fFlags, predicate, kLinearMetrics_PrivFlag);
114}
115void SkFont::setEmbolden(bool predicate) {
116 fFlags = set_clear_mask(fFlags, predicate, kEmbolden_PrivFlag);
117}
118void SkFont::setBaselineSnap(bool predicate) {
119 fFlags = set_clear_mask(fFlags, predicate, kBaselineSnap_PrivFlag);
120}
122 fEdging = SkToU8(e);
123}
124
126 fHinting = SkToU8(h);
127}
128
130 fSize = valid_size(size);
131}
133 fScaleX = scale;
134}
136 fSkewX = skew;
137}
138
140 SkFont font = *this;
141 font.setSize(newSize);
142 return font;
143}
144
145///////////////////////////////////////////////////////////////////////////////////////////////////
146
147SkScalar SkFont::setupForAsPaths(SkPaint* paint) {
148 constexpr uint32_t flagsToIgnore = kEmbeddedBitmaps_PrivFlag |
149 kForceAutoHinting_PrivFlag;
150
151 fFlags = (fFlags & ~flagsToIgnore) | kSubpixel_PrivFlag;
153
154 if (this->getEdging() == Edging::kSubpixelAntiAlias) {
156 }
157
158 if (paint) {
159 paint->setStyle(SkPaint::kFill_Style);
160 paint->setPathEffect(nullptr);
161 }
162 SkScalar textSize = fSize;
165}
166
167bool SkFont::hasSomeAntiAliasing() const {
168 Edging edging = this->getEdging();
169 return edging == SkFont::Edging::kAntiAlias
171}
172
174 return this->getTypeface()->unicharToGlyph(uni);
175}
176
177void SkFont::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
179}
180
181int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
182 SkGlyphID glyphs[], int maxGlyphCount) const {
183 return this->getTypeface()->textToGlyphs(text, byteLength, encoding,
184 glyphs, maxGlyphCount);
185}
186
188 SkRect* bounds, const SkPaint* paint) const {
189
190 SkAutoToGlyphs atg(*this, text, length, encoding);
191 const int glyphCount = atg.count();
192 if (glyphCount == 0) {
193 if (bounds) {
194 bounds->setEmpty();
195 }
196 return 0;
197 }
198 const SkGlyphID* glyphIDs = atg.glyphs();
199
200 auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this, paint);
201 SkBulkGlyphMetrics metrics{strikeSpec};
202 SkSpan<const SkGlyph*> glyphs = metrics.glyphs(SkSpan(glyphIDs, glyphCount));
203
204 SkScalar width = 0;
205 if (bounds) {
206 *bounds = glyphs[0]->rect();
207 width = glyphs[0]->advanceX();
208 for (int i = 1; i < glyphCount; ++i) {
209 SkRect r = glyphs[i]->rect();
210 r.offset(width, 0);
211 bounds->join(r);
212 width += glyphs[i]->advanceX();
213 }
214 } else {
215 for (auto glyph : glyphs) {
216 width += glyph->advanceX();
217 }
218 }
219
220 if (strikeToSourceScale != 1) {
221 width *= strikeToSourceScale;
222 if (bounds) {
223 bounds->fLeft *= strikeToSourceScale;
224 bounds->fTop *= strikeToSourceScale;
225 bounds->fRight *= strikeToSourceScale;
226 bounds->fBottom *= strikeToSourceScale;
227 }
228 }
229
230 return width;
231}
232
233void SkFont::getWidthsBounds(const SkGlyphID glyphIDs[],
234 int count,
236 SkRect bounds[],
237 const SkPaint* paint) const {
238 auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this, paint);
239 SkBulkGlyphMetrics metrics{strikeSpec};
240 SkSpan<const SkGlyph*> glyphs = metrics.glyphs(SkSpan(glyphIDs, count));
241
242 if (bounds) {
243 SkMatrix scaleMat = SkMatrix::Scale(strikeToSourceScale, strikeToSourceScale);
244 SkRect* cursor = bounds;
245 for (auto glyph : glyphs) {
246 scaleMat.mapRectScaleTranslate(cursor++, glyph->rect());
247 }
248 }
249
250 if (widths) {
251 SkScalar* cursor = widths;
252 for (auto glyph : glyphs) {
253 *cursor++ = glyph->advanceX() * strikeToSourceScale;
254 }
255 }
256}
257
258void SkFont::getPos(const SkGlyphID glyphIDs[], int count, SkPoint pos[], SkPoint origin) const {
259 auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this);
260 SkBulkGlyphMetrics metrics{strikeSpec};
261 SkSpan<const SkGlyph*> glyphs = metrics.glyphs(SkSpan(glyphIDs, count));
262
263 SkPoint sum = origin;
264 for (auto glyph : glyphs) {
265 *pos++ = sum;
266 sum += glyph->advanceVector() * strikeToSourceScale;
267 }
268}
269
271 const SkGlyphID glyphIDs[], int count, SkScalar xpos[], SkScalar origin) const {
272
273 auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this);
274 SkBulkGlyphMetrics metrics{strikeSpec};
275 SkSpan<const SkGlyph*> glyphs = metrics.glyphs(SkSpan(glyphIDs, count));
276
277 SkScalar loc = origin;
278 SkScalar* cursor = xpos;
279 for (auto glyph : glyphs) {
280 *cursor++ = loc;
281 loc += glyph->advanceX() * strikeToSourceScale;
282 }
283}
284
285void SkFont::getPaths(const SkGlyphID glyphIDs[], int count,
286 void (*proc)(const SkPath*, const SkMatrix&, void*), void* ctx) const {
287 SkFont font(*this);
288 SkScalar scale = font.setupForAsPaths(nullptr);
289 const SkMatrix mx = SkMatrix::Scale(scale, scale);
290
292 SkBulkGlyphMetricsAndPaths paths{strikeSpec};
293 SkSpan<const SkGlyph*> glyphs = paths.glyphs(SkSpan(glyphIDs, count));
294
295 for (auto glyph : glyphs) {
296 proc(glyph->path(), mx, ctx);
297 }
298}
299
300bool SkFont::getPath(SkGlyphID glyphID, SkPath* path) const {
301 struct Pair {
302 SkPath* fPath;
303 bool fWasSet;
304 } pair = { path, false };
305
306 this->getPaths(&glyphID, 1, [](const SkPath* orig, const SkMatrix& mx, void* ctx) {
307 Pair* pair = static_cast<Pair*>(ctx);
308 if (orig) {
309 orig->transform(mx, pair->fPath);
310 pair->fWasSet = true;
311 }
312 }, &pair);
313 return pair.fWasSet;
314}
315
317
318 auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this, nullptr);
319
320 SkFontMetrics storage;
321 if (nullptr == metrics) {
322 metrics = &storage;
323 }
324
325 auto cache = strikeSpec.findOrCreateStrike();
326 *metrics = cache->getFontMetrics();
327
328 if (strikeToSourceScale != 1) {
329 SkFontPriv::ScaleFontMetrics(metrics, strikeToSourceScale);
330 }
331 return metrics->fDescent - metrics->fAscent + metrics->fLeading;
332}
333
334//////////////////////////////////////////////////////////////////////////////////////////////////
335
337 metrics->fTop *= scale;
338 metrics->fAscent *= scale;
339 metrics->fDescent *= scale;
340 metrics->fBottom *= scale;
341 metrics->fLeading *= scale;
342 metrics->fAvgCharWidth *= scale;
343 metrics->fMaxCharWidth *= scale;
344 metrics->fXMin *= scale;
345 metrics->fXMax *= scale;
346 metrics->fXHeight *= scale;
347 metrics->fCapHeight *= scale;
348 metrics->fUnderlineThickness *= scale;
349 metrics->fUnderlinePosition *= scale;
350 metrics->fStrikeoutThickness *= scale;
351 metrics->fStrikeoutPosition *= scale;
352}
353
355 SkMatrix m;
356 m.setScale(font.getSize() * font.getScaleX(), font.getSize());
357 m.postSkew(font.getSkewX(), 0);
358
359 SkTypeface* typeface = font.getTypeface();
360
361 SkRect bounds;
362 m.mapRect(&bounds, typeface->getBounds());
363 return bounds;
364}
365
367 const SkPoint& textLocation) {
368 if (!matrix.hasPerspective()) {
369 return font.getSize() * matrix.getMaxScale();
370 } else {
371 // approximate the scale since we can't get it directly from the matrix
372 SkScalar maxScaleSq = SkMatrixPriv::DifferentialAreaScale(matrix, textLocation);
373 if (SkIsFinite(maxScaleSq) && !SkScalarNearlyZero(maxScaleSq)) {
374 return font.getSize() * SkScalarSqrt(maxScaleSq);
375 } else {
376 return -font.getSize();
377 }
378 }
379}
380
381int SkFontPriv::CountTextElements(const void* text, size_t byteLength, SkTextEncoding encoding) {
382 switch (encoding) {
384 return SkUTF::CountUTF8(reinterpret_cast<const char*>(text), byteLength);
386 return SkUTF::CountUTF16(reinterpret_cast<const uint16_t*>(text), byteLength);
388 return byteLength >> 2;
390 return byteLength >> 1;
391 }
392 SkASSERT(false);
393 return 0;
394}
395
397 SkUnichar text[]) {
398 if (count <= 0) {
399 return;
400 }
401
402 auto typeface = font.getTypeface();
403 const unsigned numGlyphsInTypeface = typeface->countGlyphs();
404 AutoTArray<SkUnichar> unichars(static_cast<size_t>(numGlyphsInTypeface));
405 typeface->getGlyphToUnicodeMap(unichars.get());
406
407 for (int i = 0; i < count; ++i) {
408 unsigned id = glyphs[i];
409 text[i] = (id < numGlyphsInTypeface) ? unichars[id] : 0xFFFD;
410 }
411}
SkPath fPath
uint16_t glyphs[5]
int count
SkPoint pos
uint16_t fFlags
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static bool SkIsFinite(T x, Pack... values)
SkFontHinting
Definition SkFontTypes.h:18
@ kNone
glyph outlines unchanged
SkTextEncoding
Definition SkFontTypes.h:11
@ kUTF8
uses bytes to represent UTF-8 or ASCII
@ kUTF16
uses two byte words to represent most of Unicode
@ kUTF32
uses four byte words to represent all of Unicode
@ kGlyphID
uses two byte words to represent glyph indices
#define kDefault_Edging
Definition SkFont.cpp:44
static SkScalar valid_size(SkScalar size)
Definition SkFont.cpp:47
#define kDefault_Flags
Definition SkFont.cpp:43
static uint32_t set_clear_mask(uint32_t bits, bool cond, uint32_t mask)
Definition SkFont.cpp:99
#define kDefault_Size
Definition SkFont.cpp:42
#define kDefault_Hinting
Definition SkFont.cpp:45
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:101
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarSqrt(x)
Definition SkScalar.h:42
constexpr uint8_t SkToU8(S x)
Definition SkTo.h:22
int32_t SkUnichar
Definition SkTypes.h:175
uint16_t SkGlyphID
Definition SkTypes.h:179
const SkScalar widths[]
int count() const
Definition SkFontPriv.h:110
const uint16_t * glyphs() const
Definition SkFontPriv.h:111
static int CountTextElements(const void *text, size_t byteLength, SkTextEncoding)
Definition SkFont.cpp:381
static void ScaleFontMetrics(SkFontMetrics *, SkScalar)
Definition SkFont.cpp:336
static SkScalar ApproximateTransformedTextSize(const SkFont &font, const SkMatrix &matrix, const SkPoint &textLocation)
Definition SkFont.cpp:366
static constexpr int kCanonicalTextSizeForPaths
Definition SkFontPriv.h:36
static void GlyphsToUnichars(const SkFont &, const uint16_t glyphs[], int count, SkUnichar[])
Definition SkFont.cpp:396
static SkRect GetFontBounds(const SkFont &)
Definition SkFont.cpp:354
void setSubpixel(bool subpixel)
Definition SkFont.cpp:109
void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin=0) const
Definition SkFont.cpp:270
bool getPath(SkGlyphID glyphID, SkPath *path) const
Definition SkFont.cpp:300
void setScaleX(SkScalar scaleX)
Definition SkFont.cpp:132
bool operator==(const SkFont &font) const
Definition SkFont.cpp:70
void setLinearMetrics(bool linearMetrics)
Definition SkFont.cpp:112
Edging getEdging() const
Definition SkFont.h:180
SkTypeface * getTypeface() const
Definition SkFont.h:208
void setTypeface(sk_sp< SkTypeface > tf)
Definition SkFont.cpp:90
SkScalar measureText(const void *text, size_t byteLength, SkTextEncoding encoding, SkRect *bounds=nullptr) const
Definition SkFont.h:336
void setBaselineSnap(bool baselineSnap)
Definition SkFont.cpp:118
void getPos(const SkGlyphID glyphs[], int count, SkPoint pos[], SkPoint origin={0, 0}) const
Definition SkFont.cpp:258
void setEdging(Edging edging)
Definition SkFont.cpp:121
void setSize(SkScalar textSize)
Definition SkFont.cpp:129
void dump() const
Definition SkFont.cpp:80
void getPaths(const SkGlyphID glyphIDs[], int count, void(*glyphPathProc)(const SkPath *pathOrNull, const SkMatrix &mx, void *ctx), void *ctx) const
Definition SkFont.cpp:285
void setSkewX(SkScalar skewX)
Definition SkFont.cpp:135
void setForceAutoHinting(bool forceAutoHinting)
Definition SkFont.cpp:103
SkGlyphID unicharToGlyph(SkUnichar uni) const
Definition SkFont.cpp:173
SkScalar getMetrics(SkFontMetrics *metrics) const
Definition SkFont.cpp:316
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
Definition SkFont.cpp:181
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
Definition SkFont.cpp:177
SkFont makeWithSize(SkScalar size) const
Definition SkFont.cpp:139
void setHinting(SkFontHinting hintingLevel)
Definition SkFont.cpp:125
Edging
Definition SkFont.h:39
@ kAntiAlias
may have transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
void setEmbolden(bool embolden)
Definition SkFont.cpp:115
SkFont()
Definition SkFont.cpp:68
void setEmbeddedBitmaps(bool embeddedBitmaps)
Definition SkFont.cpp:106
void getWidthsBounds(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[], const SkPaint *paint) const
Definition SkFont.cpp:233
static SkScalar DifferentialAreaScale(const SkMatrix &m, const SkPoint &p)
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
void mapRectScaleTranslate(SkRect *dst, const SkRect &src) const
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
void transform(const SkMatrix &matrix, SkPath *dst, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition SkPath.cpp:1647
static SkStrikeSpec MakeWithNoDevice(const SkFont &font, const SkPaint *paint=nullptr)
static std::tuple< SkStrikeSpec, SkScalar > MakeCanonicalized(const SkFont &font, const SkPaint *paint=nullptr)
SkRect getBounds() const
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
static sk_sp< SkTypeface > MakeEmpty()
SkGlyphID unicharToGlyph(SkUnichar unichar) const
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
float SkScalar
Definition extension.cpp:12
static bool b
size_t length
std::u16string text
SK_SPI int CountUTF16(const uint16_t *utf16, size_t byteLength)
Definition SkUTF.cpp:70
SK_SPI int CountUTF8(const char *utf8, size_t byteLength)
Definition SkUTF.cpp:47
Definition ref_ptr.h:256
SkScalar h
int32_t width
const Scalar scale
SkScalar fTop
greatest extent above origin of any glyph bounding box, typically negative; deprecated with variable ...
SkScalar fLeading
distance to add between lines, typically positive or zero
SkScalar fAvgCharWidth
average character width, zero if unknown
SkScalar fStrikeoutPosition
distance from baseline to bottom of stroke, typically negative
SkScalar fStrikeoutThickness
strikeout thickness
SkScalar fMaxCharWidth
maximum character width, zero if unknown
SkScalar fBottom
greatest extent below origin of any glyph bounding box, typically positive; deprecated with variable ...
SkScalar fAscent
distance to reserve above baseline, typically negative
SkScalar fXHeight
height of lower-case 'x', zero if unknown, typically negative
SkScalar fUnderlineThickness
underline thickness
SkScalar fDescent
distance to reserve below baseline, typically positive
SkScalar fCapHeight
height of an upper-case letter, zero if unknown, typically negative
SkScalar fXMin
greatest extent to left of origin of any glyph bounding box, typically negative; deprecated with vari...
SkScalar fUnderlinePosition
distance from baseline to top of stroke, typically positive
SkScalar fXMax
greatest extent to right of origin of any glyph bounding box, typically positive; deprecated with var...
void offset(float dx, float dy)
Definition SkRect.h:1016