Skia
2D Graphics Library
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"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/SkWeakRefCnt.h"
19 #include "include/private/base/SkOnce.h"
20 
21 #include <cstddef>
22 #include <cstdint>
23 #include <memory>
24 
25 class SkData;
26 class SkDescriptor;
27 class SkFontMgr;
28 class SkFontDescriptor;
29 class SkScalerContext;
30 class SkStream;
31 class SkStreamAsset;
32 class SkWStream;
33 enum class SkTextEncoding;
34 struct SkAdvancedTypefaceMetrics;
35 struct SkScalerContextEffects;
36 struct SkScalerContextRec;
37 
38 using SkTypefaceID = uint32_t;
39 
41 typedef uint32_t SkFontTableTag;
42 
52 class SK_API SkTypeface : public SkWeakRefCnt {
53 public:
56  return fStyle;
57  }
58 
60  bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
61 
63  bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
64 
68  bool isFixedPitch() const { return fIsFixedPitch; }
69 
82  int coordinateCount) const;
83 
96  int parameterCount) const;
97 
101  SkTypefaceID uniqueID() const { return fUniqueID; }
102 
106  static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
107 
108 #if !defined(SK_DISABLE_LEGACY_DEFAULT_TYPEFACE)
111 #endif
114 
125 #if !defined(SK_DISABLE_LEGACY_FONTMGR_REFDEFAULT)
126  static sk_sp<SkTypeface> MakeFromName(const char familyName[], SkFontStyle fontStyle);
127 #endif
128 
133 #if !defined(SK_DISABLE_LEGACY_FONTMGR_REFDEFAULT)
134  static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
135 #endif
136 
142 #if !defined(SK_DISABLE_LEGACY_FONTMGR_REFDEFAULT)
143  static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index = 0);
144 #endif
145 
150 #if !defined(SK_DISABLE_LEGACY_FONTMGR_REFDEFAULT)
152 #endif
153 
161 
167  enum class SerializeBehavior {
168  kDoIncludeData,
169  kDontIncludeData,
170  kIncludeDataIfLocal,
171  };
172 
176  void serialize(SkWStream*, SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
177 
182  sk_sp<SkData> serialize(SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
183 
191 #if !defined(SK_DISABLE_LEGACY_FONTMGR_REFDEFAULT)
193 #endif
195 
203  void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const;
204 
205  int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
206  SkGlyphID glyphs[], int maxGlyphCount) const;
207 
215 
219  int countGlyphs() const;
220 
221  // Table getters -- may fail if the underlying font format is not organized
222  // as 4-byte tables.
223 
225  int countTables() const;
226 
232  int getTableTags(SkFontTableTag tags[]) const;
233 
237 
257  size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
258  void* data) const;
259 
269 
274  int getUnitsPerEm() const;
275 
296  bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
297  int32_t adjustments[]) const;
298 
302  };
304  public:
305  LocalizedStrings() = default;
306  virtual ~LocalizedStrings() { }
307  virtual bool next(LocalizedString* localizedString) = 0;
308  void unref() { delete this; }
309 
310  private:
311  LocalizedStrings(const LocalizedStrings&) = delete;
312  LocalizedStrings& operator=(const LocalizedStrings&) = delete;
313  };
320 
326  void getFamilyName(SkString* name) const;
327 
333  bool getPostScriptName(SkString* name) const;
334 
342  std::unique_ptr<SkStreamAsset> openStream(int* ttcIndex) const;
343 
351  std::unique_ptr<SkStreamAsset> openExistingStream(int* ttcIndex) const;
352 
357  std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
358  const SkDescriptor*) const;
359 
365  SkRect getBounds() const;
366 
367  // PRIVATE / EXPERIMENTAL -- do not call
368  void filterRec(SkScalerContextRec* rec) const {
369  this->onFilterRec(rec);
370  }
371  // PRIVATE / EXPERIMENTAL -- do not call
372  void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
373  this->onGetFontDescriptor(desc, isLocal);
374  }
375  // PRIVATE / EXPERIMENTAL -- do not call
377  return this->onGetCTFontRef();
378  }
379 
380  /* Skia reserves all tags that begin with a lower case letter and 0 */
382  static void Register(
383  FactoryId id,
384  sk_sp<SkTypeface> (*make)(std::unique_ptr<SkStreamAsset>, const SkFontArguments&));
385 
386 protected:
387  explicit SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
388  ~SkTypeface() override;
389 
390  virtual sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const = 0;
391 
393  void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
395  void setFontStyle(SkFontStyle style) { fStyle = style; }
396 
397  // Must return a valid scaler context. It can not return nullptr.
398  virtual std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
399  const SkDescriptor*) const = 0;
400  virtual void onFilterRec(SkScalerContextRec*) const = 0;
401  friend class SkScalerContext; // onFilterRec
402 
403  // Subclasses *must* override this method to work with the PDF backend.
404  virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const = 0;
405  // For type1 postscript fonts only, set the glyph names for each glyph.
406  // destination array is non-null, and points to an array of size this->countGlyphs().
407  // Backends that do not suport type1 fonts should not override.
408  virtual void getPostScriptGlyphNames(SkString*) const = 0;
409 
410  // The mapping from glyph to Unicode; array indices are glyph ids.
411  // For each glyph, give the default Unicode value, if it exists.
412  // dstArray is non-null, and points to an array of size this->countGlyphs().
413  virtual void getGlyphToUnicodeMap(SkUnichar* dstArray) const = 0;
414 
415  virtual std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const = 0;
416 
417  virtual std::unique_ptr<SkStreamAsset> onOpenExistingStream(int* ttcIndex) const;
418 
419  virtual bool onGlyphMaskNeedsCurrentColor() const = 0;
420 
423  int coordinateCount) const = 0;
424 
426  SkFontParameters::Variation::Axis parameters[], int parameterCount) const = 0;
427 
428  virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
429 
430  virtual void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const = 0;
431  virtual int onCountGlyphs() const = 0;
432 
433  virtual int onGetUPEM() const = 0;
434  virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
435  int32_t adjustments[]) const;
436 
440  virtual void onGetFamilyName(SkString* familyName) const = 0;
441  virtual bool onGetPostScriptName(SkString*) const = 0;
442 
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;
450 
451  virtual bool onComputeBounds(SkRect*) const;
452 
453  virtual void* onGetCTFontRef() const { return nullptr; }
454 
455 private:
460  bool glyphMaskNeedsCurrentColor() const;
461  friend class SkStrikeServerImpl; // glyphMaskNeedsCurrentColor
462  friend class SkTypefaceProxyPrototype; // glyphMaskNeedsCurrentColor
463 
465  std::unique_ptr<SkAdvancedTypefaceMetrics> getAdvancedMetrics() const;
466  friend class SkRandomTypeface; // getAdvancedMetrics
467  friend class SkPDFFont; // getAdvancedMetrics
468 
470  enum Style {
471  kNormal = 0,
472  kBold = 0x01,
473  kItalic = 0x02,
474 
475  // helpers
476  kBoldItalic = 0x03
477  };
478  static SkFontStyle FromOldStyle(Style oldStyle);
479  static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
480 
481  friend class SkFontPriv; // GetDefaultTypeface
482  friend class SkPaintPriv; // GetDefaultTypeface
483  friend class SkFont; // getGlyphToUnicodeMap
484 
485 private:
486  SkTypefaceID fUniqueID;
487  SkFontStyle fStyle;
488  mutable SkRect fBounds;
489  mutable SkOnce fBoundsOnce;
490  bool fIsFixedPitch;
491 
492  using INHERITED = SkWeakRefCnt;
493 };
494 #endif
@ kNormal
glyph outlines modified to improve constrast
SkTextEncoding
Definition: SkFontTypes.h:11
uint32_t SkFontTableTag
Machine endian.
Definition: SkTypeface.h:41
uint32_t SkTypefaceID
Definition: SkTypeface.h:38
int32_t SkUnichar
32 bit integer to hold a unicode value
Definition: SkTypes.h:167
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:171
uint32_t SkFourByteTag
Definition: SkTypes.h:158
SkData holds an immutable data buffer.
Definition: SkData.h:25
Definition: SkFontMgr.h:36
Definition: SkFontStyle.h:16
@ kSemiBold_Weight
Definition: SkFontStyle.h:25
@ kUpright_Slant
Definition: SkFontStyle.h:45
SkFont controls options applied when drawing and measuring text.
Definition: SkFont.h:36
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:186
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:29
Light weight class for managing strings.
Definition: SkString.h:118
Definition: SkTypeface.h:303
virtual bool next(LocalizedString *localizedString)=0
virtual ~LocalizedStrings()
Definition: SkTypeface.h:306
void unref()
Definition: SkTypeface.h:308
The SkTypeface class specifies the typeface and intrinsic style of a font.
Definition: SkTypeface.h:52
sk_sp< SkTypeface > makeClone(const SkFontArguments &) const
Return a new typeface based on this typeface but parameterized as specified in the SkFontArguments.
int countGlyphs() const
Return the number of glyphs in the typeface.
virtual std::unique_ptr< SkStreamAsset > onOpenExistingStream(int *ttcIndex) const
void * internal_private_getCTFontRef() const
Definition: SkTypeface.h:376
static sk_sp< SkTypeface > MakeDeserialize(SkStream *)
Given the data previously written by serialize(), return a new instance of a typeface referring to th...
static sk_sp< SkTypeface > MakeFromName(const char familyName[], SkFontStyle fontStyle)
Creates a new reference to the typeface that most closely matches the requested familyName and fontSt...
SkRect getBounds() const
Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all of the glyphs,...
virtual void getPostScriptGlyphNames(SkString *) const =0
~SkTypeface() override
virtual LocalizedStrings * onCreateFamilyNameIterator() const =0
Returns an iterator over the family names in the font.
SkTypefaceID uniqueID() const
Return a 32bit value for this typeface, unique for the underlying font data.
Definition: SkTypeface.h:101
LocalizedStrings * createFamilyNameIterator() const
Returns an iterator which will attempt to enumerate all of the family names specified by the font.
static sk_sp< SkTypeface > MakeDeserialize(SkStream *, sk_sp< SkFontMgr > lastResortMgr)
virtual bool onComputeBounds(SkRect *) const
void setFontStyle(SkFontStyle style)
Sets the font style.
Definition: SkTypeface.h:395
static void Register(FactoryId id, sk_sp< SkTypeface >(*make)(std::unique_ptr< SkStreamAsset >, const SkFontArguments &))
std::unique_ptr< SkStreamAsset > openStream(int *ttcIndex) const
Return a stream for the contents of the font data, or NULL on failure.
void getFontDescriptor(SkFontDescriptor *desc, bool *isLocal) const
Definition: SkTypeface.h:372
int getUnitsPerEm() const
Return the units-per-em value for this typeface, or zero if there is an error.
static sk_sp< SkTypeface > MakeFromFile(const char path[], int index=0)
Return a new typeface given a file.
void setIsFixedPitch(bool isFixedPitch)
Sets the fixedPitch bit.
Definition: SkTypeface.h:393
void getFamilyName(SkString *name) const
Return the family name for this typeface.
SkFontStyle fontStyle() const
Returns the typeface's intrinsic style attributes.
Definition: SkTypeface.h:55
virtual void getGlyphToUnicodeMap(SkUnichar *dstArray) const =0
virtual int onCountGlyphs() const =0
void serialize(SkWStream *, SerializeBehavior=SerializeBehavior::kIncludeDataIfLocal) const
Write a unique signature to a stream, sufficient to reconstruct a typeface referencing the same font ...
virtual int onGetTableTags(SkFontTableTag tags[]) const =0
int getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], int parameterCount) const
Copy into 'parameters' (allocated by the caller) the design variation parameters.
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
virtual std::unique_ptr< SkAdvancedTypefaceMetrics > onGetAdvancedMetrics() const =0
bool isFixedPitch() const
Returns true if the typeface claims to be fixed-pitch.
Definition: SkTypeface.h:68
bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count, int32_t adjustments[]) const
Given a run of glyphs, return the associated horizontal adjustments.
int getTableTags(SkFontTableTag tags[]) const
Copy into tags[] (allocated by the caller) the list of table tags in the font, and return the number.
void filterRec(SkScalerContextRec *rec) const
Definition: SkTypeface.h:368
virtual void onGetFontDescriptor(SkFontDescriptor *, bool *isLocal) const =0
size_t getTableSize(SkFontTableTag) const
Given a table tag, return the size of its contents, or 0 if not present.
virtual std::unique_ptr< SkScalerContext > onCreateScalerContext(const SkScalerContextEffects &, const SkDescriptor *) const =0
bool isBold() const
Returns true if style() has the kBold bit set.
Definition: SkTypeface.h:60
static sk_sp< SkTypeface > MakeDefault()
Returns the default normal typeface, which is never nullptr.
int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
Copy into 'coordinates' (allocated by the caller) the design variation coordinates.
static sk_sp< SkTypeface > MakeFromData(sk_sp< SkData >, int index=0)
Return a new typeface given a SkData and TTC index (pass 0 for none).
bool getPostScriptName(SkString *name) const
Return the PostScript name for this typeface.
virtual size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void *data) const =0
virtual sk_sp< SkTypeface > onMakeClone(const SkFontArguments &) const =0
virtual bool onGetPostScriptName(SkString *) const =0
SerializeBehavior
A typeface can serialize just a descriptor (names, etc.), or it can also include the actual font data...
Definition: SkTypeface.h:167
std::unique_ptr< SkScalerContext > createScalerContext(const SkScalerContextEffects &, const SkDescriptor *) const
Return a scalercontext for the given descriptor.
sk_sp< SkData > copyTableData(SkFontTableTag tag) const
Return an immutable copy of the requested font table, or nullptr if that table was not found.
static sk_sp< SkTypeface > MakeEmpty()
Returns a non-null typeface which contains no glyphs.
virtual void onGetFamilyName(SkString *familyName) const =0
Returns the family name of the typeface as known by its font manager.
virtual void onFilterRec(SkScalerContextRec *) const =0
virtual bool onGlyphMaskNeedsCurrentColor() const =0
int countTables() const
Return the number of tables in the font.
static sk_sp< SkTypeface > MakeFromStream(std::unique_ptr< SkStreamAsset > stream, int index=0)
Return a new typeface given a stream and TTC index(pass 0 for none).
virtual int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], int parameterCount) const =0
virtual void * onGetCTFontRef() const
Definition: SkTypeface.h:453
SkFourByteTag FactoryId
Definition: SkTypeface.h:381
size_t getTableData(SkFontTableTag tag, size_t offset, size_t length, void *data) const
Copy the contents of a table into data (allocated by the caller).
bool isItalic() const
Returns true if style() has the kItalic bit set.
Definition: SkTypeface.h:63
std::unique_ptr< SkStreamAsset > openExistingStream(int *ttcIndex) const
Return a stream for the contents of the font data.
static bool Equal(const SkTypeface *facea, const SkTypeface *faceb)
Returns true if the two typefaces reference the same underlying font, handling either being null (tre...
SkGlyphID unicharToGlyph(SkUnichar unichar) const
Return the glyphID that corresponds to the specified unicode code-point (in UTF32 encoding).
virtual void onCharsToGlyphs(const SkUnichar *chars, int count, SkGlyphID glyphs[]) const =0
virtual int onGetUPEM() const =0
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
Given an array of UTF32 character codes, return their corresponding glyph IDs.
SkTypeface(const SkFontStyle &style, bool isFixedPitch=false)
virtual std::unique_ptr< SkStreamAsset > onOpenStream(int *ttcIndex) const =0
sk_sp< SkData > serialize(SerializeBehavior=SerializeBehavior::kIncludeDataIfLocal) const
Same as serialize(SkWStream*, ...) but returns the serialized data in SkData, instead of writing it t...
virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count, int32_t adjustments[]) const
virtual int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const =0
virtual sk_sp< SkData > onCopyTableData(SkFontTableTag) const
Definition: SkStream.h:218
Definition: SkFontArguments.h:18
Represents a set of actual arguments for a font.
Definition: SkFontArguments.h:16
Definition: SkFontParameters.h:17
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: SkTypeface.h:299
SkString fString
Definition: SkTypeface.h:300
SkString fLanguage
Definition: SkTypeface.h:301