• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef OHOS_SUPPORT
12 #include <vector>
13 #endif
14 
15 #include "include/core/SkFontArguments.h"
16 #include "include/core/SkFontParameters.h"
17 #include "include/core/SkFontStyle.h"
18 #include "include/core/SkFontTypes.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkString.h"
21 #include "include/private/SkOnce.h"
22 #include "include/private/SkWeakRefCnt.h"
23 
24 class SkData;
25 class SkDescriptor;
26 class SkFontData;
27 class SkFontDescriptor;
28 class SkScalerContext;
29 class SkStream;
30 class SkStreamAsset;
31 class SkWStream;
32 struct SkAdvancedTypefaceMetrics;
33 struct SkScalerContextEffects;
34 struct SkScalerContextRec;
35 
36 typedef uint32_t SkFontID;
37 /** Machine endian. */
38 typedef uint32_t SkFontTableTag;
39 
40 /** \class SkTypeface
41 
42     The SkTypeface class specifies the typeface and intrinsic style of a font.
43     This is used in the paint, along with optionally algorithmic settings like
44     textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
45     how text appears when drawn (and measured).
46 
47     Typeface objects are immutable, and so they can be shared between threads.
48 */
49 class SK_API SkTypeface : public SkWeakRefCnt {
50 public:
51     /** Returns the typeface's intrinsic style attributes. */
fontStyle()52     SkFontStyle fontStyle() const {
53         return fStyle;
54     }
55 
56     /** Returns true if style() has the kBold bit set. */
isBold()57     bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
58 
59     /** Returns true if style() has the kItalic bit set. */
isItalic()60     bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
61 
62     /** Returns true if the typeface claims to be fixed-pitch.
63      *  This is a style bit, advance widths may vary even if this returns true.
64      */
isFixedPitch()65     bool isFixedPitch() const { return fIsFixedPitch; }
66 
67     /** Copy into 'coordinates' (allocated by the caller) the design variation coordinates.
68      *
69      *  @param coordinates the buffer into which to write the design variation coordinates.
70      *  @param coordinateCount the number of entries available through 'coordinates'.
71      *
72      *  @return The number of axes, or -1 if there is an error.
73      *  If 'coordinates != nullptr' and 'coordinateCount >= numAxes' then 'coordinates' will be
74      *  filled with the variation coordinates describing the position of this typeface in design
75      *  variation space. It is possible the number of axes can be retrieved but actual position
76      *  cannot.
77      */
78     int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
79                                    int coordinateCount) const;
80 
81     /** Copy into 'parameters' (allocated by the caller) the design variation parameters.
82      *
83      *  @param parameters the buffer into which to write the design variation parameters.
84      *  @param coordinateCount the number of entries available through 'parameters'.
85      *
86      *  @return The number of axes, or -1 if there is an error.
87      *  If 'parameters != nullptr' and 'parameterCount >= numAxes' then 'parameters' will be
88      *  filled with the variation parameters describing the position of this typeface in design
89      *  variation space. It is possible the number of axes can be retrieved but actual parameters
90      *  cannot.
91      */
92     int getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
93                                      int parameterCount) const;
94 
95     /** Return a 32bit value for this typeface, unique for the underlying font
96         data. Will never return 0.
97      */
uniqueID()98     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 normal typeface, which is never nullptr. */
112     static sk_sp<SkTypeface> MakeDefault();
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 a new typeface given a file. If the file does not exist, or is
126         not a valid font file, returns nullptr.
127     */
128     static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
129 
130     /** Return a new typeface given a stream. If the stream is
131         not a valid font file, returns nullptr. Ownership of the stream is
132         transferred, so the caller must not reference it again.
133     */
134     static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index = 0);
135 
136     /** Return a new typeface given a SkData. If the data is null, or is not a valid font file,
137      *  returns nullptr.
138      */
139     static sk_sp<SkTypeface> MakeFromData(sk_sp<SkData>, int index = 0);
140 
141 #ifdef OHOS_SUPPORT
142     static std::vector<sk_sp<SkTypeface>> GetSystemFonts();
143 #endif
144 
145     /** Return a new typeface based on this typeface but parameterized as specified in the
146         SkFontArguments. If the SkFontArguments does not supply an argument for a parameter
147         in the font then the value from this typeface will be used as the value for that
148         argument. If the cloned typeface would be exaclty the same as this typeface then
149         this typeface may be ref'ed and returned. May return nullptr on failure.
150     */
151     sk_sp<SkTypeface> makeClone(const SkFontArguments&) const;
152 
153     /**
154      *  A typeface can serialize just a descriptor (names, etc.), or it can also include the
155      *  actual font data (which can be large). This enum controls how serialize() decides what
156      *  to serialize.
157      */
158     enum class SerializeBehavior {
159         kDoIncludeData,
160         kDontIncludeData,
161         kIncludeDataIfLocal,
162     };
163 
164     /** Write a unique signature to a stream, sufficient to reconstruct a
165         typeface referencing the same font when Deserialize is called.
166      */
167     void serialize(SkWStream*, SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
168 
169     /**
170      *  Same as serialize(SkWStream*, ...) but returns the serialized data in SkData, instead of
171      *  writing it to a stream.
172      */
173     sk_sp<SkData> serialize(SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
174 
175     /** Given the data previously written by serialize(), return a new instance
176         of a typeface referring to the same font. If that font is not available,
177         return nullptr.
178         Does not affect ownership of SkStream.
179      */
180     static sk_sp<SkTypeface> MakeDeserialize(SkStream*);
181 
182     /**
183      *  Given an array of UTF32 character codes, return their corresponding glyph IDs.
184      *
185      *  @param chars pointer to the array of UTF32 chars
186      *  @param number of chars and glyphs
187      *  @param glyphs returns the corresponding glyph IDs for each character.
188      */
189     void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const;
190 
191     int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
192                      SkGlyphID glyphs[], int maxGlyphCount) const;
193 
194     /**
195      *  Return the glyphID that corresponds to the specified unicode code-point
196      *  (in UTF32 encoding). If the unichar is not supported, returns 0.
197      *
198      *  This is a short-cut for calling unicharsToGlyphs().
199      */
200     SkGlyphID unicharToGlyph(SkUnichar unichar) const;
201 
202     /**
203      *  Return the number of glyphs in the typeface.
204      */
205     int countGlyphs() const;
206 
207     // Table getters -- may fail if the underlying font format is not organized
208     // as 4-byte tables.
209 
210     /** Return the number of tables in the font. */
211     int countTables() const;
212 
213     /** Copy into tags[] (allocated by the caller) the list of table tags in
214      *  the font, and return the number. This will be the same as CountTables()
215      *  or 0 if an error occured. If tags == NULL, this only returns the count
216      *  (the same as calling countTables()).
217      */
218     int getTableTags(SkFontTableTag tags[]) const;
219 
220     /** Given a table tag, return the size of its contents, or 0 if not present
221      */
222     size_t getTableSize(SkFontTableTag) const;
223 
224     /** Copy the contents of a table into data (allocated by the caller). Note
225      *  that the contents of the table will be in their native endian order
226      *  (which for most truetype tables is big endian). If the table tag is
227      *  not found, or there is an error copying the data, then 0 is returned.
228      *  If this happens, it is possible that some or all of the memory pointed
229      *  to by data may have been written to, even though an error has occured.
230      *
231      *  @param tag  The table tag whose contents are to be copied
232      *  @param offset The offset in bytes into the table's contents where the
233      *  copy should start from.
234      *  @param length The number of bytes, starting at offset, of table data
235      *  to copy.
236      *  @param data storage address where the table contents are copied to
237      *  @return the number of bytes actually copied into data. If offset+length
238      *  exceeds the table's size, then only the bytes up to the table's
239      *  size are actually copied, and this is the value returned. If
240      *  offset > the table's size, or tag is not a valid table,
241      *  then 0 is returned.
242      */
243     size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
244                         void* data) const;
245 
246     /**
247      *  Return an immutable copy of the requested font table, or nullptr if that table was
248      *  not found. This can sometimes be faster than calling getTableData() twice: once to find
249      *  the length, and then again to copy the data.
250      *
251      *  @param tag  The table tag whose contents are to be copied
252      *  @return an immutable copy of the table's data, or nullptr.
253      */
254     sk_sp<SkData> copyTableData(SkFontTableTag tag) const;
255 
256     /**
257      *  Return the units-per-em value for this typeface, or zero if there is an
258      *  error.
259      */
260     int getUnitsPerEm() const;
261 
262     /**
263      *  Given a run of glyphs, return the associated horizontal adjustments.
264      *  Adjustments are in "design units", which are integers relative to the
265      *  typeface's units per em (see getUnitsPerEm).
266      *
267      *  Some typefaces are known to never support kerning. Calling this method
268      *  with all zeros (e.g. getKerningPairAdustments(NULL, 0, NULL)) returns
269      *  a boolean indicating if the typeface might support kerning. If it
270      *  returns false, then it will always return false (no kerning) for all
271      *  possible glyph runs. If it returns true, then it *may* return true for
272      *  somne glyph runs.
273      *
274      *  If count is non-zero, then the glyphs parameter must point to at least
275      *  [count] valid glyph IDs, and the adjustments parameter must be
276      *  sized to at least [count - 1] entries. If the method returns true, then
277      *  [count-1] entries in the adjustments array will be set. If the method
278      *  returns false, then no kerning should be applied, and the adjustments
279      *  array will be in an undefined state (possibly some values may have been
280      *  written, but none of them should be interpreted as valid values).
281      */
282     bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
283                                    int32_t adjustments[]) const;
284 
285     struct LocalizedString {
286         SkString fString;
287         SkString fLanguage;
288     };
289     class LocalizedStrings {
290     public:
291         LocalizedStrings() = default;
~LocalizedStrings()292         virtual ~LocalizedStrings() { }
293         virtual bool next(LocalizedString* localizedString) = 0;
unref()294         void unref() { delete this; }
295 
296     private:
297         LocalizedStrings(const LocalizedStrings&) = delete;
298         LocalizedStrings& operator=(const LocalizedStrings&) = delete;
299     };
300     /**
301      *  Returns an iterator which will attempt to enumerate all of the
302      *  family names specified by the font.
303      *  It is the caller's responsibility to unref() the returned pointer.
304      */
305     LocalizedStrings* createFamilyNameIterator() const;
306 
307     /**
308      *  Return the family name for this typeface. It will always be returned
309      *  encoded as UTF8, but the language of the name is whatever the host
310      *  platform chooses.
311      */
312     void getFamilyName(SkString* name) const;
313 
314 #ifdef OHOS_SUPPORT
315     void getFontPath(SkString* path) const;
316 #endif
317 
318     /**
319      *  Return the PostScript name for this typeface.
320      *  Value may change based on variation parameters.
321      *  Returns false if no PostScript name is available.
322      */
323     bool getPostScriptName(SkString* name) const;
324 
325     /**
326      *  Return a stream for the contents of the font data, or NULL on failure.
327      *  If ttcIndex is not null, it is set to the TrueTypeCollection index
328      *  of this typeface within the stream, or 0 if the stream is not a
329      *  collection.
330      *  The caller is responsible for deleting the stream.
331      */
332     std::unique_ptr<SkStreamAsset> openStream(int* ttcIndex) const;
333 
334     /**
335      * Return a stream for the contents of the font data.
336      * Returns nullptr on failure or if the font data isn't already available in stream form.
337      * Use when the stream can be used opportunistically but the calling code would prefer
338      * to fall back to table access if creating the stream would be expensive.
339      * Otherwise acts the same as openStream.
340      */
341     std::unique_ptr<SkStreamAsset> openExistingStream(int* ttcIndex) const;
342 
343     /**
344      *  Return a scalercontext for the given descriptor. It may return a
345      *  stub scalercontext that will not crash, but will draw nothing.
346      */
347     std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
348                                                          const SkDescriptor*) const;
349 
350     /**
351      *  Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all
352      *  of the glyphs, but each one positioned at (0,). This may be conservatively large, and
353      *  will not take into account any hinting or other size-specific adjustments.
354      */
355     SkRect getBounds() const;
356 
357     bool isCustomTypeface() const;
358     void setIsCustomTypeface(bool isCustom);
359 #ifdef OHOS_SUPPORT
360     bool isThemeTypeface() const;
361     void setIsThemeTypeface(bool isTheme);
362 #endif
363     // PRIVATE / EXPERIMENTAL -- do not call
filterRec(SkScalerContextRec * rec)364     void filterRec(SkScalerContextRec* rec) const {
365         this->onFilterRec(rec);
366     }
367     // PRIVATE / EXPERIMENTAL -- do not call
getFontDescriptor(SkFontDescriptor * desc,bool * isLocal)368     void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
369         this->onGetFontDescriptor(desc, isLocal);
370     }
371     // PRIVATE / EXPERIMENTAL -- do not call
internal_private_getCTFontRef()372     void* internal_private_getCTFontRef() const {
373         return this->onGetCTFontRef();
374     }
375 
376 #ifdef OHOS_SUPPORT
377     uint32_t GetHash() const;
378     void SetHash(uint32_t hash);
379 #endif
380 
381 protected:
382     explicit SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
383     ~SkTypeface() override;
384 
385     virtual sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const = 0;
386 
387     /** Sets the fixedPitch bit. If used, must be called in the constructor. */
setIsFixedPitch(bool isFixedPitch)388     void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
389     /** Sets the font style. If used, must be called in the constructor. */
setFontStyle(SkFontStyle style)390     void setFontStyle(SkFontStyle style) { fStyle = style; }
391 
392     // Must return a valid scaler context. It can not return nullptr.
393     virtual std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
394                                                                    const SkDescriptor*) const = 0;
395     virtual void onFilterRec(SkScalerContextRec*) const = 0;
396     friend class SkScalerContext;  // onFilterRec
397 
398     //  Subclasses *must* override this method to work with the PDF backend.
399     virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const = 0;
400     // For type1 postscript fonts only, set the glyph names for each glyph.
401     // destination array is non-null, and points to an array of size this->countGlyphs().
402     // Backends that do not suport type1 fonts should not override.
403     virtual void getPostScriptGlyphNames(SkString*) const = 0;
404 
405     // The mapping from glyph to Unicode; array indices are glyph ids.
406     // For each glyph, give the default Unicode value, if it exists.
407     // dstArray is non-null, and points to an array of size this->countGlyphs().
408     virtual void getGlyphToUnicodeMap(SkUnichar* dstArray) const = 0;
409 
410     virtual std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const = 0;
411 
412     virtual std::unique_ptr<SkStreamAsset> onOpenExistingStream(int* ttcIndex) const;
413 
414     virtual bool onGlyphMaskNeedsCurrentColor() const = 0;
415 
416     virtual int onGetVariationDesignPosition(
417         SkFontArguments::VariationPosition::Coordinate coordinates[],
418         int coordinateCount) const = 0;
419 
420     virtual int onGetVariationDesignParameters(
421         SkFontParameters::Variation::Axis parameters[], int parameterCount) const = 0;
422 
423     virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
424 
425     virtual void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const = 0;
426     virtual int onCountGlyphs() const = 0;
427 
428     virtual int onGetUPEM() const = 0;
429     virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
430                                              int32_t adjustments[]) const;
431 
432     /** Returns the family name of the typeface as known by its font manager.
433      *  This name may or may not be produced by the family name iterator.
434      */
435     virtual void onGetFamilyName(SkString* familyName) const = 0;
436 
437 #ifdef OHOS_SUPPORT
onGetFontPath(SkString * path)438     virtual void onGetFontPath(SkString* path) const { path->reset(); }
439 #endif
440 
441     virtual bool onGetPostScriptName(SkString*) const = 0;
442 
443     /** Returns an iterator over the family names in the font. */
444     virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0;
445 
446     virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
447     virtual size_t onGetTableData(SkFontTableTag, size_t offset,
448                                   size_t length, void* data) const = 0;
449     virtual sk_sp<SkData> onCopyTableData(SkFontTableTag) const;
450 
451     virtual bool onComputeBounds(SkRect*) const;
452 
onGetCTFontRef()453     virtual void* onGetCTFontRef() const { return nullptr; }
454 
455 private:
456     /** Returns true if the typeface's glyph masks may refer to the foreground
457      *  paint foreground color. This is needed to determine caching requirements. Usually true for
458      *  typefaces that contain a COLR table.
459      */
460     bool glyphMaskNeedsCurrentColor() const;
461     friend class SkStrikeServerImpl; // glyphMaskNeedsCurrentColor
462 
463     /** Retrieve detailed typeface metrics.  Used by the PDF backend.  */
464     std::unique_ptr<SkAdvancedTypefaceMetrics> getAdvancedMetrics() const;
465     friend class SkRandomTypeface;   // getAdvancedMetrics
466     friend class SkPDFFont;          // getAdvancedMetrics
467 
468     /** Style specifies the intrinsic style attributes of a given typeface */
469     enum Style {
470         kNormal = 0,
471         kBold   = 0x01,
472         kItalic = 0x02,
473 
474         // helpers
475         kBoldItalic = 0x03
476     };
477     static SkFontStyle FromOldStyle(Style oldStyle);
478     static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
479 
480     friend class SkFontPriv;         // GetDefaultTypeface
481     friend class SkPaintPriv;        // GetDefaultTypeface
482     friend class SkFont;             // getGlyphToUnicodeMap
483 
484 #ifdef OHOS_SUPPORT
485     mutable uint32_t hash_{0};
486 #endif
487 
488 private:
489     SkFontID            fUniqueID;
490     SkFontStyle         fStyle;
491     mutable SkRect      fBounds;
492     mutable SkOnce      fBoundsOnce;
493     bool                fIsFixedPitch;
494     bool                fIsCustom = false;
495 #ifdef OHOS_SUPPORT
496     bool                fIsTheme = false;
497 #endif
498     using INHERITED = SkWeakRefCnt;
499 };
500 #endif
501