Flutter Engine
The Flutter Engine
SkTypeface.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
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#ifndef SkTypeface_DEFINED
9#define SkTypeface_DEFINED
10
14#include "include/core/SkRect.h"
20
21#include <cstddef>
22#include <cstdint>
23#include <memory>
24
25class SkData;
26class SkDescriptor;
27class SkFontMgr;
29class SkScalerContext;
30class SkStream;
31class SkStreamAsset;
32class SkWStream;
33enum class SkTextEncoding;
37
38using SkTypefaceID = uint32_t;
39
40/** Machine endian. */
41typedef uint32_t SkFontTableTag;
42
43/** \class SkTypeface
44
45 The SkTypeface class specifies the typeface and intrinsic style of a font.
46 This is used in the paint, along with optionally algorithmic settings like
47 textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
48 how text appears when drawn (and measured).
49
50 Typeface objects are immutable, and so they can be shared between threads.
51*/
53public:
54 /** Returns the typeface's intrinsic style attributes. */
56 return fStyle;
57 }
58
59 /** Returns true if style() has the kBold bit set. */
60 bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
61
62 /** Returns true if style() has the kItalic bit set. */
63 bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
64
65 /** Returns true if the typeface claims to be fixed-pitch.
66 * This is a style bit, advance widths may vary even if this returns true.
67 */
68 bool isFixedPitch() const { return fIsFixedPitch; }
69
70 /** Copy into 'coordinates' (allocated by the caller) the design variation coordinates.
71 *
72 * @param coordinates the buffer into which to write the design variation coordinates.
73 * @param coordinateCount the number of entries available through 'coordinates'.
74 *
75 * @return The number of axes, or -1 if there is an error.
76 * If 'coordinates != nullptr' and 'coordinateCount >= numAxes' then 'coordinates' will be
77 * filled with the variation coordinates describing the position of this typeface in design
78 * variation space. It is possible the number of axes can be retrieved but actual position
79 * cannot.
80 */
81 int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
82 int coordinateCount) const;
83
84 /** Copy into 'parameters' (allocated by the caller) the design variation parameters.
85 *
86 * @param parameters the buffer into which to write the design variation parameters.
87 * @param coordinateCount the number of entries available through 'parameters'.
88 *
89 * @return The number of axes, or -1 if there is an error.
90 * If 'parameters != nullptr' and 'parameterCount >= numAxes' then 'parameters' will be
91 * filled with the variation parameters describing the position of this typeface in design
92 * variation space. It is possible the number of axes can be retrieved but actual parameters
93 * cannot.
94 */
95 int getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
96 int parameterCount) const;
97
98 /** Return a 32bit value for this typeface, unique for the underlying font
99 data. Will never return 0.
100 */
101 SkTypefaceID uniqueID() const { return fUniqueID; }
102
103 /** Returns true if the two typefaces reference the same underlying font,
104 handling either being null (treating null as not equal to any font).
105 */
106 static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
107
108 /** Returns a non-null typeface which contains no glyphs. */
109 static sk_sp<SkTypeface> MakeEmpty();
110
111 /** Return a new typeface based on this typeface but parameterized as specified in the
112 SkFontArguments. If the SkFontArguments does not supply an argument for a parameter
113 in the font then the value from this typeface will be used as the value for that
114 argument. If the cloned typeface would be exaclty the same as this typeface then
115 this typeface may be ref'ed and returned. May return nullptr on failure.
116 */
117 sk_sp<SkTypeface> makeClone(const SkFontArguments&) const;
118
119 /**
120 * A typeface can serialize just a descriptor (names, etc.), or it can also include the
121 * actual font data (which can be large). This enum controls how serialize() decides what
122 * to serialize.
123 */
124 enum class SerializeBehavior {
125 kDoIncludeData,
126 kDontIncludeData,
127 kIncludeDataIfLocal,
128 };
129
130 /** Write a unique signature to a stream, sufficient to reconstruct a
131 typeface referencing the same font when Deserialize is called.
132 */
133 void serialize(SkWStream*, SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
134
135 /**
136 * Same as serialize(SkWStream*, ...) but returns the serialized data in SkData, instead of
137 * writing it to a stream.
138 */
139 sk_sp<SkData> serialize(SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
140
141 /** Given the data previously written by serialize(), return a new instance
142 of a typeface referring to the same font. If that font is not available,
143 return nullptr.
144 Goes through all registered typeface factories and lastResortMgr (if non-null).
145 Does not affect ownership of SkStream.
146 */
147
148 static sk_sp<SkTypeface> MakeDeserialize(SkStream*, sk_sp<SkFontMgr> lastResortMgr);
149
150 /**
151 * Given an array of UTF32 character codes, return their corresponding glyph IDs.
152 *
153 * @param chars pointer to the array of UTF32 chars
154 * @param number of chars and glyphs
155 * @param glyphs returns the corresponding glyph IDs for each character.
156 */
157 void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const;
158
159 int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
160 SkGlyphID glyphs[], int maxGlyphCount) const;
161
162 /**
163 * Return the glyphID that corresponds to the specified unicode code-point
164 * (in UTF32 encoding). If the unichar is not supported, returns 0.
165 *
166 * This is a short-cut for calling unicharsToGlyphs().
167 */
168 SkGlyphID unicharToGlyph(SkUnichar unichar) const;
169
170 /**
171 * Return the number of glyphs in the typeface.
172 */
173 int countGlyphs() const;
174
175 // Table getters -- may fail if the underlying font format is not organized
176 // as 4-byte tables.
177
178 /** Return the number of tables in the font. */
179 int countTables() const;
180
181 /** Copy into tags[] (allocated by the caller) the list of table tags in
182 * the font, and return the number. This will be the same as CountTables()
183 * or 0 if an error occured. If tags == NULL, this only returns the count
184 * (the same as calling countTables()).
185 */
186 int getTableTags(SkFontTableTag tags[]) const;
187
188 /** Given a table tag, return the size of its contents, or 0 if not present
189 */
190 size_t getTableSize(SkFontTableTag) const;
191
192 /** Copy the contents of a table into data (allocated by the caller). Note
193 * that the contents of the table will be in their native endian order
194 * (which for most truetype tables is big endian). If the table tag is
195 * not found, or there is an error copying the data, then 0 is returned.
196 * If this happens, it is possible that some or all of the memory pointed
197 * to by data may have been written to, even though an error has occured.
198 *
199 * @param tag The table tag whose contents are to be copied
200 * @param offset The offset in bytes into the table's contents where the
201 * copy should start from.
202 * @param length The number of bytes, starting at offset, of table data
203 * to copy.
204 * @param data storage address where the table contents are copied to
205 * @return the number of bytes actually copied into data. If offset+length
206 * exceeds the table's size, then only the bytes up to the table's
207 * size are actually copied, and this is the value returned. If
208 * offset > the table's size, or tag is not a valid table,
209 * then 0 is returned.
210 */
211 size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
212 void* data) const;
213
214 /**
215 * Return an immutable copy of the requested font table, or nullptr if that table was
216 * not found. This can sometimes be faster than calling getTableData() twice: once to find
217 * the length, and then again to copy the data.
218 *
219 * @param tag The table tag whose contents are to be copied
220 * @return an immutable copy of the table's data, or nullptr.
221 */
222 sk_sp<SkData> copyTableData(SkFontTableTag tag) const;
223
224 /**
225 * Return the units-per-em value for this typeface, or zero if there is an
226 * error.
227 */
228 int getUnitsPerEm() const;
229
230 /**
231 * Given a run of glyphs, return the associated horizontal adjustments.
232 * Adjustments are in "design units", which are integers relative to the
233 * typeface's units per em (see getUnitsPerEm).
234 *
235 * Some typefaces are known to never support kerning. Calling this method
236 * with all zeros (e.g. getKerningPairAdustments(NULL, 0, NULL)) returns
237 * a boolean indicating if the typeface might support kerning. If it
238 * returns false, then it will always return false (no kerning) for all
239 * possible glyph runs. If it returns true, then it *may* return true for
240 * somne glyph runs.
241 *
242 * If count is non-zero, then the glyphs parameter must point to at least
243 * [count] valid glyph IDs, and the adjustments parameter must be
244 * sized to at least [count - 1] entries. If the method returns true, then
245 * [count-1] entries in the adjustments array will be set. If the method
246 * returns false, then no kerning should be applied, and the adjustments
247 * array will be in an undefined state (possibly some values may have been
248 * written, but none of them should be interpreted as valid values).
249 */
250 bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
251 int32_t adjustments[]) const;
252
256 };
258 public:
259 LocalizedStrings() = default;
260 virtual ~LocalizedStrings() { }
261 virtual bool next(LocalizedString* localizedString) = 0;
262 void unref() { delete this; }
263
264 private:
265 LocalizedStrings(const LocalizedStrings&) = delete;
266 LocalizedStrings& operator=(const LocalizedStrings&) = delete;
267 };
268 /**
269 * Returns an iterator which will attempt to enumerate all of the
270 * family names specified by the font.
271 * It is the caller's responsibility to unref() the returned pointer.
272 */
273 LocalizedStrings* createFamilyNameIterator() const;
274
275 /**
276 * Return the family name for this typeface. It will always be returned
277 * encoded as UTF8, but the language of the name is whatever the host
278 * platform chooses.
279 */
280 void getFamilyName(SkString* name) const;
281
282 /**
283 * Return the PostScript name for this typeface.
284 * Value may change based on variation parameters.
285 * Returns false if no PostScript name is available.
286 */
287 bool getPostScriptName(SkString* name) const;
288
289 /**
290 * Return a stream for the contents of the font data, or NULL on failure.
291 * If ttcIndex is not null, it is set to the TrueTypeCollection index
292 * of this typeface within the stream, or 0 if the stream is not a
293 * collection.
294 * The caller is responsible for deleting the stream.
295 */
296 std::unique_ptr<SkStreamAsset> openStream(int* ttcIndex) const;
297
298 /**
299 * Return a stream for the contents of the font data.
300 * Returns nullptr on failure or if the font data isn't already available in stream form.
301 * Use when the stream can be used opportunistically but the calling code would prefer
302 * to fall back to table access if creating the stream would be expensive.
303 * Otherwise acts the same as openStream.
304 */
305 std::unique_ptr<SkStreamAsset> openExistingStream(int* ttcIndex) const;
306
307 /**
308 * Return a scalercontext for the given descriptor. It may return a
309 * stub scalercontext that will not crash, but will draw nothing.
310 */
311 std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
312 const SkDescriptor*) const;
313
314 /**
315 * Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all
316 * of the glyphs, but each one positioned at (0,). This may be conservatively large, and
317 * will not take into account any hinting or other size-specific adjustments.
318 */
319 SkRect getBounds() const;
320
321 // PRIVATE / EXPERIMENTAL -- do not call
322 void filterRec(SkScalerContextRec* rec) const {
323 this->onFilterRec(rec);
324 }
325 // PRIVATE / EXPERIMENTAL -- do not call
326 void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
327 this->onGetFontDescriptor(desc, isLocal);
328 }
329 // PRIVATE / EXPERIMENTAL -- do not call
331 return this->onGetCTFontRef();
332 }
333
334 /* Skia reserves all tags that begin with a lower case letter and 0 */
336 static void Register(
337 FactoryId id,
338 sk_sp<SkTypeface> (*make)(std::unique_ptr<SkStreamAsset>, const SkFontArguments&));
339
340protected:
341 explicit SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
342 ~SkTypeface() override;
343
345
346 /** Sets the fixedPitch bit. If used, must be called in the constructor. */
347 void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
348 /** Sets the font style. If used, must be called in the constructor. */
349 void setFontStyle(SkFontStyle style) { fStyle = style; }
350
351 // Must return a valid scaler context. It can not return nullptr.
352 virtual std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
353 const SkDescriptor*) const = 0;
354 virtual void onFilterRec(SkScalerContextRec*) const = 0;
355 friend class SkScalerContext; // onFilterRec
356
357 // Subclasses *must* override this method to work with the PDF backend.
358 virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const = 0;
359 // For type1 postscript fonts only, set the glyph names for each glyph.
360 // destination array is non-null, and points to an array of size this->countGlyphs().
361 // Backends that do not suport type1 fonts should not override.
362 virtual void getPostScriptGlyphNames(SkString*) const = 0;
363
364 // The mapping from glyph to Unicode; array indices are glyph ids.
365 // For each glyph, give the default Unicode value, if it exists.
366 // dstArray is non-null, and points to an array of size this->countGlyphs().
367 virtual void getGlyphToUnicodeMap(SkUnichar* dstArray) const = 0;
368
369 virtual std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const = 0;
370
371 virtual std::unique_ptr<SkStreamAsset> onOpenExistingStream(int* ttcIndex) const;
372
373 virtual bool onGlyphMaskNeedsCurrentColor() const = 0;
374
377 int coordinateCount) const = 0;
378
380 SkFontParameters::Variation::Axis parameters[], int parameterCount) const = 0;
381
382 virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
383
384 virtual void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const = 0;
385 virtual int onCountGlyphs() const = 0;
386
387 virtual int onGetUPEM() const = 0;
388 virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
389 int32_t adjustments[]) const;
390
391 /** Returns the family name of the typeface as known by its font manager.
392 * This name may or may not be produced by the family name iterator.
393 */
394 virtual void onGetFamilyName(SkString* familyName) const = 0;
395 virtual bool onGetPostScriptName(SkString*) const = 0;
396
397 /** Returns an iterator over the family names in the font. */
399
400 virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
401 virtual size_t onGetTableData(SkFontTableTag, size_t offset,
402 size_t length, void* data) const = 0;
403 virtual sk_sp<SkData> onCopyTableData(SkFontTableTag) const;
404
405 virtual bool onComputeBounds(SkRect*) const;
406
407 virtual void* onGetCTFontRef() const { return nullptr; }
408
409private:
410 /** Returns true if the typeface's glyph masks may refer to the foreground
411 * paint foreground color. This is needed to determine caching requirements. Usually true for
412 * typefaces that contain a COLR table.
413 */
414 bool glyphMaskNeedsCurrentColor() const;
415 friend class SkStrikeServerImpl; // glyphMaskNeedsCurrentColor
416 friend class SkTypefaceProxyPrototype; // glyphMaskNeedsCurrentColor
417
418 /** Retrieve detailed typeface metrics. Used by the PDF backend. */
419 std::unique_ptr<SkAdvancedTypefaceMetrics> getAdvancedMetrics() const;
420 friend class SkRandomTypeface; // getAdvancedMetrics
421 friend class SkPDFFont; // getAdvancedMetrics
422
423 friend class SkFontPriv; // getGlyphToUnicodeMap
424
425private:
426 SkTypefaceID fUniqueID;
428 mutable SkRect fBounds;
429 mutable SkOnce fBoundsOnce;
430 bool fIsFixedPitch;
431
432 using INHERITED = SkWeakRefCnt;
433};
434#endif
SkStrokeRec::Style fStyle
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
int count
Definition: FontMgrTest.cpp:50
const SkRect fBounds
#define SK_API
Definition: SkAPI.h:35
SkTextEncoding
Definition: SkFontTypes.h:11
uint32_t SkFontTableTag
Definition: SkTypeface.h:41
uint32_t SkTypefaceID
Definition: SkTypeface.h:38
int32_t SkUnichar
Definition: SkTypes.h:175
uint16_t SkGlyphID
Definition: SkTypes.h:179
uint32_t SkFourByteTag
Definition: SkTypes.h:166
Definition: SkData.h:25
@ kSemiBold_Weight
Definition: SkFontStyle.h:25
Definition: SkOnce.h:22
virtual bool next(LocalizedString *localizedString)=0
virtual void * onGetCTFontRef() const
Definition: SkTypeface.h:407
virtual void getPostScriptGlyphNames(SkString *) const =0
SkTypefaceID uniqueID() const
Definition: SkTypeface.h:101
void * internal_private_getCTFontRef() const
Definition: SkTypeface.h:330
virtual std::unique_ptr< SkAdvancedTypefaceMetrics > onGetAdvancedMetrics() const =0
void setFontStyle(SkFontStyle style)
Definition: SkTypeface.h:349
void getFontDescriptor(SkFontDescriptor *desc, bool *isLocal) const
Definition: SkTypeface.h:326
void setIsFixedPitch(bool isFixedPitch)
Definition: SkTypeface.h:347
SkFontStyle fontStyle() const
Definition: SkTypeface.h:55
virtual std::unique_ptr< SkScalerContext > onCreateScalerContext(const SkScalerContextEffects &, const SkDescriptor *) const =0
virtual int onCountGlyphs() const =0
virtual int onGetTableTags(SkFontTableTag tags[]) const =0
bool isFixedPitch() const
Definition: SkTypeface.h:68
virtual std::unique_ptr< SkStreamAsset > onOpenStream(int *ttcIndex) const =0
void filterRec(SkScalerContextRec *rec) const
Definition: SkTypeface.h:322
virtual void onGetFontDescriptor(SkFontDescriptor *, bool *isLocal) const =0
bool isBold() const
Definition: SkTypeface.h:60
virtual sk_sp< SkTypeface > onMakeClone(const SkFontArguments &) const =0
virtual size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void *data) const =0
virtual bool onGetPostScriptName(SkString *) const =0
virtual void onGetFamilyName(SkString *familyName) const =0
virtual void onFilterRec(SkScalerContextRec *) const =0
virtual bool onGlyphMaskNeedsCurrentColor() const =0
virtual LocalizedStrings * onCreateFamilyNameIterator() const =0
virtual int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], int parameterCount) const =0
SkFourByteTag FactoryId
Definition: SkTypeface.h:335
bool isItalic() const
Definition: SkTypeface.h:63
virtual void onCharsToGlyphs(const SkUnichar *chars, int count, SkGlyphID glyphs[]) const =0
virtual int onGetUPEM() const =0
virtual int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const =0
size_t length
std::u16string text
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
Definition: encode_srgb.cpp:35
SeparatedVector2 offset
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63