Line data Source code
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 :
11 : #include "../private/SkBitmaskEnum.h"
12 : #include "../private/SkOnce.h"
13 : #include "../private/SkWeakRefCnt.h"
14 : #include "SkFontArguments.h"
15 : #include "SkFontStyle.h"
16 : #include "SkRect.h"
17 : #include "SkString.h"
18 :
19 : class SkDescriptor;
20 : class SkFontData;
21 : class SkFontDescriptor;
22 : class SkScalerContext;
23 : struct SkScalerContextRec;
24 : struct SkScalerContextEffects;
25 : class SkStream;
26 : class SkStreamAsset;
27 : class SkAdvancedTypefaceMetrics;
28 : class SkWStream;
29 :
30 : typedef uint32_t SkFontID;
31 : /** Machine endian. */
32 : typedef uint32_t SkFontTableTag;
33 :
34 : /** \class SkTypeface
35 :
36 : The SkTypeface class specifies the typeface and intrinsic style of a font.
37 : This is used in the paint, along with optionally algorithmic settings like
38 : textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
39 : how text appears when drawn (and measured).
40 :
41 : Typeface objects are immutable, and so they can be shared between threads.
42 : */
43 : class SK_API SkTypeface : public SkWeakRefCnt {
44 : public:
45 : /** Style specifies the intrinsic style attributes of a given typeface
46 : */
47 : enum Style {
48 : kNormal = 0,
49 : kBold = 0x01,
50 : kItalic = 0x02,
51 :
52 : // helpers
53 : kBoldItalic = 0x03
54 : };
55 :
56 : /** Returns the typeface's intrinsic style attributes. */
57 0 : SkFontStyle fontStyle() const {
58 0 : return fStyle;
59 : }
60 :
61 : /** Returns the typeface's intrinsic style attributes.
62 : * @deprecated use fontStyle() instead.
63 : */
64 0 : Style style() const {
65 : return static_cast<Style>(
66 0 : (fStyle.weight() >= SkFontStyle::kSemiBold_Weight ? kBold : kNormal) |
67 0 : (fStyle.slant() != SkFontStyle::kUpright_Slant ? kItalic : kNormal));
68 : }
69 :
70 : /** Returns true if style() has the kBold bit set. */
71 : bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
72 :
73 : /** Returns true if style() has the kItalic bit set. */
74 : bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
75 :
76 : /** Returns true if the typeface claims to be fixed-pitch.
77 : * This is a style bit, advance widths may vary even if this returns true.
78 : */
79 : bool isFixedPitch() const { return fIsFixedPitch; }
80 :
81 : /** Copy into 'coordinates' (allocated by the caller) the design variation coordinates.
82 : *
83 : * @param coordinates the buffer into which to write the design variation coordinates.
84 : * @param coordinateCount the number of entries available through 'coordinates'.
85 : *
86 : * @return The number of axes, or -1 if there is an error.
87 : * If 'coordinates != nullptr' and 'coordinateCount >= numAxes' then 'coordinates' will be
88 : * filled with the variation coordinates describing the position of this typeface in design
89 : * variation space. It is possible the number of axes can be retrieved but actual position
90 : * cannot.
91 : */
92 : int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
93 : int coordinateCount) const;
94 :
95 : /** Return a 32bit value for this typeface, unique for the underlying font
96 : data. Will never return 0.
97 : */
98 42 : SkFontID uniqueID() const { return fUniqueID; }
99 :
100 : /** Return the uniqueID for the specified typeface. If the face is null,
101 : resolve it to the default font and return its uniqueID. Will never
102 : return 0.
103 : */
104 : static SkFontID UniqueID(const SkTypeface* face);
105 :
106 : /** Returns true if the two typefaces reference the same underlying font,
107 : handling either being null (treating null as the default font)
108 : */
109 : static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
110 :
111 : /** Returns the default typeface, which is never nullptr. */
112 : static sk_sp<SkTypeface> MakeDefault(Style style = SkTypeface::kNormal);
113 :
114 : /** Creates a new reference to the typeface that most closely matches the
115 : requested familyName and fontStyle. This method allows extended font
116 : face specifiers as in the SkFontStyle type. Will never return null.
117 :
118 : @param familyName May be NULL. The name of the font family.
119 : @param fontStyle The style of the typeface.
120 : @return reference to the closest-matching typeface. Call must call
121 : unref() when they are done.
122 : */
123 : static sk_sp<SkTypeface> MakeFromName(const char familyName[], SkFontStyle fontStyle);
124 :
125 : /** Return the typeface that most closely matches the requested typeface and style.
126 : Use this to pick a new style from the same family of the existing typeface.
127 : If family is nullptr, this selects from the default font's family.
128 :
129 : @param family May be NULL. The name of the existing type face.
130 : @param s The style (normal, bold, italic) of the type face.
131 : @return the closest-matching typeface.
132 : */
133 : static sk_sp<SkTypeface> MakeFromTypeface(SkTypeface* family, Style);
134 :
135 : /** Return a new typeface given a file. If the file does not exist, or is
136 : not a valid font file, returns nullptr.
137 : */
138 : static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
139 :
140 : /** Return a new typeface given a stream. If the stream is
141 : not a valid font file, returns nullptr. Ownership of the stream is
142 : transferred, so the caller must not reference it again.
143 : */
144 : static sk_sp<SkTypeface> MakeFromStream(SkStreamAsset* stream, int index = 0);
145 :
146 : /** Return a new typeface given font data and configuration. If the data
147 : is not valid font data, returns nullptr.
148 : */
149 : static sk_sp<SkTypeface> MakeFromFontData(std::unique_ptr<SkFontData>);
150 :
151 : /** Write a unique signature to a stream, sufficient to reconstruct a
152 : typeface referencing the same font when Deserialize is called.
153 : */
154 : void serialize(SkWStream*) const;
155 :
156 : /** Given the data previously written by serialize(), return a new instance
157 : of a typeface referring to the same font. If that font is not available,
158 : return nullptr.
159 : Does not affect ownership of SkStream.
160 : */
161 : static sk_sp<SkTypeface> MakeDeserialize(SkStream*);
162 :
163 : enum Encoding {
164 : kUTF8_Encoding,
165 : kUTF16_Encoding,
166 : kUTF32_Encoding
167 : };
168 :
169 : /**
170 : * Given an array of character codes, of the specified encoding,
171 : * optionally return their corresponding glyph IDs (if glyphs is not NULL).
172 : *
173 : * @param chars pointer to the array of character codes
174 : * @param encoding how the characters are encoded
175 : * @param glyphs (optional) returns the corresponding glyph IDs for each
176 : * character code, up to glyphCount values. If a character code is
177 : * not found in the typeface, the corresponding glyph ID will be 0.
178 : * @param glyphCount number of code points in 'chars' to process. If glyphs
179 : * is not NULL, then it must point sufficient memory to write
180 : * glyphCount values into it.
181 : * @return the number of number of continuous non-zero glyph IDs computed
182 : * from the beginning of chars. This value is valid, even if the
183 : * glyphs parameter is NULL.
184 : */
185 : int charsToGlyphs(const void* chars, Encoding encoding, SkGlyphID glyphs[],
186 : int glyphCount) const;
187 :
188 : /**
189 : * Return the number of glyphs in the typeface.
190 : */
191 : int countGlyphs() const;
192 :
193 : // Table getters -- may fail if the underlying font format is not organized
194 : // as 4-byte tables.
195 :
196 : /** Return the number of tables in the font. */
197 : int countTables() const;
198 :
199 : /** Copy into tags[] (allocated by the caller) the list of table tags in
200 : * the font, and return the number. This will be the same as CountTables()
201 : * or 0 if an error occured. If tags == NULL, this only returns the count
202 : * (the same as calling countTables()).
203 : */
204 : int getTableTags(SkFontTableTag tags[]) const;
205 :
206 : /** Given a table tag, return the size of its contents, or 0 if not present
207 : */
208 : size_t getTableSize(SkFontTableTag) const;
209 :
210 : /** Copy the contents of a table into data (allocated by the caller). Note
211 : * that the contents of the table will be in their native endian order
212 : * (which for most truetype tables is big endian). If the table tag is
213 : * not found, or there is an error copying the data, then 0 is returned.
214 : * If this happens, it is possible that some or all of the memory pointed
215 : * to by data may have been written to, even though an error has occured.
216 : *
217 : * @param fontID the font to copy the table from
218 : * @param tag The table tag whose contents are to be copied
219 : * @param offset The offset in bytes into the table's contents where the
220 : * copy should start from.
221 : * @param length The number of bytes, starting at offset, of table data
222 : * to copy.
223 : * @param data storage address where the table contents are copied to
224 : * @return the number of bytes actually copied into data. If offset+length
225 : * exceeds the table's size, then only the bytes up to the table's
226 : * size are actually copied, and this is the value returned. If
227 : * offset > the table's size, or tag is not a valid table,
228 : * then 0 is returned.
229 : */
230 : size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
231 : void* data) const;
232 :
233 : /**
234 : * Return the units-per-em value for this typeface, or zero if there is an
235 : * error.
236 : */
237 : int getUnitsPerEm() const;
238 :
239 : /**
240 : * Given a run of glyphs, return the associated horizontal adjustments.
241 : * Adjustments are in "design units", which are integers relative to the
242 : * typeface's units per em (see getUnitsPerEm).
243 : *
244 : * Some typefaces are known to never support kerning. Calling this method
245 : * with all zeros (e.g. getKerningPairAdustments(NULL, 0, NULL)) returns
246 : * a boolean indicating if the typeface might support kerning. If it
247 : * returns false, then it will always return false (no kerning) for all
248 : * possible glyph runs. If it returns true, then it *may* return true for
249 : * somne glyph runs.
250 : *
251 : * If count is non-zero, then the glyphs parameter must point to at least
252 : * [count] valid glyph IDs, and the adjustments parameter must be
253 : * sized to at least [count - 1] entries. If the method returns true, then
254 : * [count-1] entries in the adjustments array will be set. If the method
255 : * returns false, then no kerning should be applied, and the adjustments
256 : * array will be in an undefined state (possibly some values may have been
257 : * written, but none of them should be interpreted as valid values).
258 : */
259 : bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
260 : int32_t adjustments[]) const;
261 :
262 : struct LocalizedString {
263 : SkString fString;
264 : SkString fLanguage;
265 : };
266 0 : class LocalizedStrings : ::SkNoncopyable {
267 : public:
268 0 : virtual ~LocalizedStrings() { }
269 : virtual bool next(LocalizedString* localizedString) = 0;
270 : void unref() { delete this; }
271 : };
272 : /**
273 : * Returns an iterator which will attempt to enumerate all of the
274 : * family names specified by the font.
275 : * It is the caller's responsibility to unref() the returned pointer.
276 : */
277 : LocalizedStrings* createFamilyNameIterator() const;
278 :
279 : /**
280 : * Return the family name for this typeface. It will always be returned
281 : * encoded as UTF8, but the language of the name is whatever the host
282 : * platform chooses.
283 : */
284 : void getFamilyName(SkString* name) const;
285 :
286 : /**
287 : * Return a stream for the contents of the font data, or NULL on failure.
288 : * If ttcIndex is not null, it is set to the TrueTypeCollection index
289 : * of this typeface within the stream, or 0 if the stream is not a
290 : * collection.
291 : * The caller is responsible for deleting the stream.
292 : */
293 : SkStreamAsset* openStream(int* ttcIndex) const;
294 :
295 : /**
296 : * Return the font data, or nullptr on failure.
297 : */
298 : std::unique_ptr<SkFontData> makeFontData() const;
299 :
300 : /**
301 : * Return a scalercontext for the given descriptor. If this fails, then
302 : * if allowFailure is true, this returns NULL, else it returns a
303 : * dummy scalercontext that will not crash, but will draw nothing.
304 : */
305 : std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
306 : const SkDescriptor*,
307 : bool allowFailure = false) const;
308 :
309 : /**
310 : * Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all
311 : * of the glyphs, but each one positioned at (0,). This may be conservatively large, and
312 : * will not take into account any hinting or other size-specific adjustments.
313 : */
314 : SkRect getBounds() const;
315 :
316 : /***
317 : * Returns whether this typeface has color glyphs and therefore cannot be
318 : * rendered as a path. e.g. Emojis.
319 : */
320 21 : virtual bool hasColorGlyphs() const { return false; }
321 :
322 : // PRIVATE / EXPERIMENTAL -- do not call
323 : void filterRec(SkScalerContextRec* rec) const {
324 : this->onFilterRec(rec);
325 : }
326 : // PRIVATE / EXPERIMENTAL -- do not call
327 : void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
328 : this->onGetFontDescriptor(desc, isLocal);
329 : }
330 :
331 : protected:
332 : // The type of advance data wanted.
333 : enum PerGlyphInfo {
334 : kNo_PerGlyphInfo = 0x0, // Don't populate any per glyph info.
335 : kGlyphNames_PerGlyphInfo = 0x1, // Populate glyph names (Type 1 only).
336 : kToUnicode_PerGlyphInfo = 0x2 // Populate ToUnicode table, ignored
337 : // for Type 1 fonts
338 : };
339 :
340 : /** uniqueID must be unique and non-zero
341 : */
342 : SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
343 : virtual ~SkTypeface();
344 :
345 : /** Sets the fixedPitch bit. If used, must be called in the constructor. */
346 : void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
347 : /** Sets the font style. If used, must be called in the constructor. */
348 : void setFontStyle(SkFontStyle style) { fStyle = style; }
349 :
350 : friend class SkScalerContext;
351 : static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
352 :
353 : virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
354 : const SkDescriptor*) const = 0;
355 : virtual void onFilterRec(SkScalerContextRec*) const = 0;
356 : virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
357 : PerGlyphInfo,
358 : const uint32_t* glyphIDs,
359 : uint32_t glyphIDsCount) const = 0;
360 :
361 : virtual SkStreamAsset* onOpenStream(int* ttcIndex) const = 0;
362 : // TODO: make pure virtual.
363 : virtual std::unique_ptr<SkFontData> onMakeFontData() const;
364 :
365 : virtual int onGetVariationDesignPosition(
366 : SkFontArguments::VariationPosition::Coordinate coordinates[],
367 : int coordinateCount) const = 0;
368 :
369 : virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
370 :
371 : virtual int onCharsToGlyphs(const void* chars, Encoding, SkGlyphID glyphs[],
372 : int glyphCount) const = 0;
373 : virtual int onCountGlyphs() const = 0;
374 :
375 : virtual int onGetUPEM() const = 0;
376 : virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
377 : int32_t adjustments[]) const;
378 :
379 : /** Returns the family name of the typeface as known by its font manager.
380 : * This name may or may not be produced by the family name iterator.
381 : */
382 : virtual void onGetFamilyName(SkString* familyName) const = 0;
383 :
384 : /** Returns an iterator over the family names in the font. */
385 : virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0;
386 :
387 : virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
388 : virtual size_t onGetTableData(SkFontTableTag, size_t offset,
389 : size_t length, void* data) const = 0;
390 :
391 : virtual bool onComputeBounds(SkRect*) const;
392 :
393 : private:
394 : friend class SkGTypeface;
395 : friend class SkRandomTypeface;
396 : friend class SkPDFFont;
397 : friend class GrPathRendering;
398 : friend class GrGLPathRendering;
399 :
400 : /** Retrieve detailed typeface metrics. Used by the PDF backend.
401 : @param perGlyphInfo Indicate what glyph specific information (advances,
402 : names, etc.) should be populated.
403 : @param glyphIDs For per-glyph info, specify subset of the font by
404 : giving glyph ids. Each integer represents a glyph
405 : id. Passing NULL means all glyphs in the font.
406 : @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
407 : glyphIDs is NULL.
408 : @return The returned object has already been referenced.
409 : */
410 : SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics(
411 : PerGlyphInfo,
412 : const uint32_t* glyphIDs = NULL,
413 : uint32_t glyphIDsCount = 0) const;
414 :
415 : private:
416 : SkFontID fUniqueID;
417 : SkFontStyle fStyle;
418 : mutable SkRect fBounds;
419 : mutable SkOnce fBoundsOnce;
420 : bool fIsFixedPitch;
421 :
422 : friend class SkPaint;
423 : friend class SkGlyphCache; // GetDefaultTypeface
424 :
425 : typedef SkWeakRefCnt INHERITED;
426 : };
427 :
428 : namespace skstd {
429 : template <> struct is_bitmask_enum<SkTypeface::PerGlyphInfo> : std::true_type {};
430 : }
431 :
432 : #endif
|