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 
15 #include "include/core/SkRect.h"
16 #include "include/core/SkString.h"
17 #include "include/private/SkOnce.h"
18 #include "include/private/SkWeakRefCnt.h"
19 
20 class SkData;
21 class SkDescriptor;
22 class SkFontData;
23 class SkFontDescriptor;
24 class SkScalerContext;
25 class SkStream;
26 class SkStreamAsset;
27 class SkWStream;
28 struct SkAdvancedTypefaceMetrics;
29 struct SkScalerContextEffects;
30 struct SkScalerContextRec;
31 
32 typedef uint32_t SkFontID;
34 typedef uint32_t SkFontTableTag;
35 
45 class SK_API SkTypeface : public SkWeakRefCnt {
46 public:
49  return fStyle;
50  }
51 
53  bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
54 
56  bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
57 
61  bool isFixedPitch() const { return fIsFixedPitch; }
62 
75  int coordinateCount) const;
76 
89  int parameterCount) const;
90 
94  SkFontID uniqueID() const { return fUniqueID; }
95 
100  static SkFontID UniqueID(const SkTypeface* face);
101 
105  static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
106 
109 
119  static sk_sp<SkTypeface> MakeFromName(const char familyName[], SkFontStyle fontStyle);
120 
124  static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
125 
130  static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index = 0);
131 
136 
144 
150  enum class SerializeBehavior {
151  kDoIncludeData,
152  kDontIncludeData,
153  kIncludeDataIfLocal,
154  };
155 
159  void serialize(SkWStream*, SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
160 
165  sk_sp<SkData> serialize(SerializeBehavior = SerializeBehavior::kIncludeDataIfLocal) const;
166 
173 
181  void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const;
182 
190 
194  int countGlyphs() const;
195 
196  // Table getters -- may fail if the underlying font format is not organized
197  // as 4-byte tables.
198 
200  int countTables() const;
201 
207  int getTableTags(SkFontTableTag tags[]) const;
208 
212 
232  size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
233  void* data) const;
234 
244 
249  int getUnitsPerEm() const;
250 
271  bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
272  int32_t adjustments[]) const;
273 
277  };
279  public:
280  LocalizedStrings() = default;
281  virtual ~LocalizedStrings() { }
282  virtual bool next(LocalizedString* localizedString) = 0;
283  void unref() { delete this; }
284 
285  private:
286  LocalizedStrings(const LocalizedStrings&) = delete;
287  LocalizedStrings& operator=(const LocalizedStrings&) = delete;
288  };
295 
301  void getFamilyName(SkString* name) const;
302 
308  bool getPostScriptName(SkString* name) const;
309 
317  std::unique_ptr<SkStreamAsset> openStream(int* ttcIndex) const;
318 
323  std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
324  const SkDescriptor*) const;
325 
331  SkRect getBounds() const;
332 
333  // PRIVATE / EXPERIMENTAL -- do not call
334  void filterRec(SkScalerContextRec* rec) const {
335  this->onFilterRec(rec);
336  }
337  // PRIVATE / EXPERIMENTAL -- do not call
338  void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
339  this->onGetFontDescriptor(desc, isLocal);
340  }
341  // PRIVATE / EXPERIMENTAL -- do not call
343  return this->onGetCTFontRef();
344  }
345 
346 protected:
347  explicit SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
348  ~SkTypeface() override;
349 
350  virtual sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const = 0;
351 
353  void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
355  void setFontStyle(SkFontStyle style) { fStyle = style; }
356 
357  // Must return a valid scaler context. It can not return nullptr.
358  virtual std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
359  const SkDescriptor*) const = 0;
360  virtual void onFilterRec(SkScalerContextRec*) const = 0;
361  friend class SkScalerContext; // onFilterRec
362 
363  // Subclasses *must* override this method to work with the PDF backend.
364  virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const = 0;
365  // For type1 postscript fonts only, set the glyph names for each glyph.
366  // destination array is non-null, and points to an array of size this->countGlyphs().
367  // Backends that do not suport type1 fonts should not override.
368  virtual void getPostScriptGlyphNames(SkString*) const = 0;
369 
370  // The mapping from glyph to Unicode; array indices are glyph ids.
371  // For each glyph, give the default Unicode value, if it exists.
372  // dstArray is non-null, and points to an array of size this->countGlyphs().
373  virtual void getGlyphToUnicodeMap(SkUnichar* dstArray) const = 0;
374 
375  virtual std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const = 0;
376 
379  int coordinateCount) const = 0;
380 
382  SkFontParameters::Variation::Axis parameters[], int parameterCount) const = 0;
383 
384  virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
385 
386  virtual void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const = 0;
387  virtual int onCountGlyphs() const = 0;
388 
389  virtual int onGetUPEM() const = 0;
390  virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
391  int32_t adjustments[]) const;
392 
396  virtual void onGetFamilyName(SkString* familyName) const = 0;
397  virtual bool onGetPostScriptName(SkString*) const = 0;
398 
401 
402  virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
403  virtual size_t onGetTableData(SkFontTableTag, size_t offset,
404  size_t length, void* data) const = 0;
406 
407  virtual bool onComputeBounds(SkRect*) const;
408 
409  virtual void* onGetCTFontRef() const { return nullptr; }
410 
411 private:
413  std::unique_ptr<SkAdvancedTypefaceMetrics> getAdvancedMetrics() const;
414  friend class SkRandomTypeface; // getAdvancedMetrics
415  friend class SkPDFFont; // getAdvancedMetrics
416 
418  enum Style {
419  kNormal = 0,
420  kBold = 0x01,
421  kItalic = 0x02,
422 
423  // helpers
424  kBoldItalic = 0x03
425  };
426  static SkFontStyle FromOldStyle(Style oldStyle);
427  static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
428 
429  friend class SkFontPriv; // GetDefaultTypeface
430  friend class SkPaintPriv; // GetDefaultTypeface
431  friend class SkFont; // getGlyphToUnicodeMap
432 
433 private:
434  SkFontID fUniqueID;
435  SkFontStyle fStyle;
436  mutable SkRect fBounds;
437  mutable SkOnce fBoundsOnce;
438  bool fIsFixedPitch;
439 
440  using INHERITED = SkWeakRefCnt;
441 };
442 #endif
SkTypeface::isItalic
bool isItalic() const
Returns true if style() has the kItalic bit set.
Definition: SkTypeface.h:56
SkFontHinting::kNormal
@ kNormal
glyph outlines modified to improve constrast
SkGlyphID
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:556
SkFontStyle
Definition: SkFontStyle.h:14
SkTypeface::createScalerContext
std::unique_ptr< SkScalerContext > createScalerContext(const SkScalerContextEffects &, const SkDescriptor *) const
Return a scalercontext for the given descriptor.
SkTypeface::onCreateScalerContext
virtual std::unique_ptr< SkScalerContext > onCreateScalerContext(const SkScalerContextEffects &, const SkDescriptor *) const =0
SkFontArguments.h
SkTypeface::onGetFontDescriptor
virtual void onGetFontDescriptor(SkFontDescriptor *, bool *isLocal) const =0
SkTypeface::getFamilyName
void getFamilyName(SkString *name) const
Return the family name for this typeface.
SkTypeface::LocalizedString
Definition: SkTypeface.h:274
SkFontParameters.h
SkTypeface::onMakeClone
virtual sk_sp< SkTypeface > onMakeClone(const SkFontArguments &) const =0
SkTypeface::getUnitsPerEm
int getUnitsPerEm() const
Return the units-per-em value for this typeface, or zero if there is an error.
SkTypeface::getTableTags
int getTableTags(SkFontTableTag tags[]) const
Copy into tags[] (allocated by the caller) the list of table tags in the font, and return the number.
SkTypeface::countGlyphs
int countGlyphs() const
Return the number of glyphs in the typeface.
SkTypeface::LocalizedStrings::~LocalizedStrings
virtual ~LocalizedStrings()
Definition: SkTypeface.h:281
SkFontStyle::kSemiBold_Weight
@ kSemiBold_Weight
Definition: SkFontStyle.h:23
SkRect.h
SkTypeface::SkTypeface
SkTypeface(const SkFontStyle &style, bool isFixedPitch=false)
SkTypeface::onOpenStream
virtual std::unique_ptr< SkStreamAsset > onOpenStream(int *ttcIndex) const =0
SkTypeface::uniqueID
SkFontID uniqueID() const
Return a 32bit value for this typeface, unique for the underlying font data.
Definition: SkTypeface.h:94
SkTypeface::onGetTableTags
virtual int onGetTableTags(SkFontTableTag tags[]) const =0
SkTypeface::LocalizedStrings::LocalizedStrings
LocalizedStrings()=default
SkTypeface::onGetVariationDesignParameters
virtual int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], int parameterCount) const =0
SkTypeface::onCreateFamilyNameIterator
virtual LocalizedStrings * onCreateFamilyNameIterator() const =0
Returns an iterator over the family names in the font.
SkTypeface::getPostScriptName
bool getPostScriptName(SkString *name) const
Return the PostScript name for this typeface.
SkTypeface::onFilterRec
virtual void onFilterRec(SkScalerContextRec *) const =0
SkData
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkStream
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:41
SkTypeface::filterRec
void filterRec(SkScalerContextRec *rec) const
Definition: SkTypeface.h:334
SkFontArguments::VariationPosition::Coordinate
Definition: SkFontArguments.h:17
SkTypeface::fontStyle
SkFontStyle fontStyle() const
Returns the typeface's intrinsic style attributes.
Definition: SkTypeface.h:48
SkTypeface::serialize
void serialize(SkWStream *, SerializeBehavior=SerializeBehavior::kIncludeDataIfLocal) const
Write a unique signature to a stream, sufficient to reconstruct a typeface referencing the same font ...
SkTypeface::onGetPostScriptName
virtual bool onGetPostScriptName(SkString *) const =0
SkTypeface
Definition: SkTypeface.h:45
sk_sp< SkTypeface >
SkTypeface::LocalizedString::fLanguage
SkString fLanguage
Definition: SkTypeface.h:276
SkTypeface::MakeDefault
static sk_sp< SkTypeface > MakeDefault()
Returns the default normal typeface, which is never nullptr.
SkTypeface::SerializeBehavior
SerializeBehavior
A typeface can serialize just a descriptor (names, etc.), or it can also include the actual font data...
Definition: SkTypeface.h:150
SkTypeface::getFontDescriptor
void getFontDescriptor(SkFontDescriptor *desc, bool *isLocal) const
Definition: SkTypeface.h:338
SkFontTypes.h
SK_API
#define SK_API
Definition: SkTypes.h:181
SkTypeface::onGetFamilyName
virtual void onGetFamilyName(SkString *familyName) const =0
Returns the family name of the typeface as known by its font manager.
SkFontTableTag
uint32_t SkFontTableTag
Machine endian.
Definition: SkTypeface.h:34
SkTypeface::LocalizedStrings
Definition: SkTypeface.h:278
SkTypeface::getTableData
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).
SkTypeface::getKerningPairAdjustments
bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count, int32_t adjustments[]) const
Given a run of glyphs, return the associated horizontal adjustments.
SkTypeface::makeClone
sk_sp< SkTypeface > makeClone(const SkFontArguments &) const
Return a new typeface based on this typeface but parameterized as specified in the SkFontArguments.
SkTypeface::onGetVariationDesignPosition
virtual int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const =0
SkTypeface::UniqueID
static SkFontID UniqueID(const SkTypeface *face)
Return the uniqueID for the specified typeface.
SkTypeface::serialize
sk_sp< SkData > serialize(SerializeBehavior=SerializeBehavior::kIncludeDataIfLocal) const
Same as serialize(SkWStream*, ...) but returns the serialized data in SkData, instead of writing it t...
SkTypeface::getBounds
SkRect getBounds() const
Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all of the glyphs,...
SkTypeface::internal_private_getCTFontRef
void * internal_private_getCTFontRef() const
Definition: SkTypeface.h:342
SkTypeface::MakeFromData
static sk_sp< SkTypeface > MakeFromData(sk_sp< SkData >, int index=0)
Return a new typeface given a SkData.
SkTypeface::LocalizedString::fString
SkString fString
Definition: SkTypeface.h:275
SkTypeface::unicharToGlyph
SkGlyphID unicharToGlyph(SkUnichar unichar) const
Return the glyphID that corresponds to the specified unicode code-point (in UTF32 encoding).
SkUnichar
int32_t SkUnichar
32 bit integer to hold a unicode value
Definition: SkTypes.h:552
SkTypeface::onGetTableData
virtual size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void *data) const =0
SkStreamAsset
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:195
SkTypeface::getVariationDesignPosition
int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
Copy into 'coordinates' (allocated by the caller) the design variation coordinates.
SkTypeface::MakeFromName
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...
SkTypeface::MakeDeserialize
static sk_sp< SkTypeface > MakeDeserialize(SkStream *)
Given the data previously written by serialize(), return a new instance of a typeface referring to th...
SkFontID
uint32_t SkFontID
Definition: SkTypeface.h:30
SkTypeface::onGetCTFontRef
virtual void * onGetCTFontRef() const
Definition: SkTypeface.h:409
SkTypeface::createFamilyNameIterator
LocalizedStrings * createFamilyNameIterator() const
Returns an iterator which will attempt to enumerate all of the family names specified by the font.
SkTypeface::setFontStyle
void setFontStyle(SkFontStyle style)
Sets the font style.
Definition: SkTypeface.h:355
SkTypeface::onCountGlyphs
virtual int onCountGlyphs() const =0
SkString.h
SkTypeface::onComputeBounds
virtual bool onComputeBounds(SkRect *) const
SkWStream
Definition: SkStream.h:227
SkTypeface::~SkTypeface
~SkTypeface() override
SkString
Definition: SkString.h:116
SkFont
Definition: SkFont.h:23
SkTypeface::countTables
int countTables() const
Return the number of tables in the font.
SkTypeface::openStream
std::unique_ptr< SkStreamAsset > openStream(int *ttcIndex) const
Return a stream for the contents of the font data, or NULL on failure.
SkFontStyle.h
SkTypeface::onCharsToGlyphs
virtual void onCharsToGlyphs(const SkUnichar *chars, int count, SkGlyphID glyphs[]) const =0
SkTypeface::unicharsToGlyphs
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
Given an array of UTF32 character codes, return their corresponding glyph IDs.
SkTypeface::MakeFromFile
static sk_sp< SkTypeface > MakeFromFile(const char path[], int index=0)
Return a new typeface given a file.
SkRect
Definition: SkRect.h:572
SkTypeface::LocalizedStrings::unref
void unref()
Definition: SkTypeface.h:283
SkTypeface::getGlyphToUnicodeMap
virtual void getGlyphToUnicodeMap(SkUnichar *dstArray) const =0
SkTypeface::onGetKerningPairAdjustments
virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count, int32_t adjustments[]) const
SkTypeface::getPostScriptGlyphNames
virtual void getPostScriptGlyphNames(SkString *) const =0
SkTypeface::copyTableData
sk_sp< SkData > copyTableData(SkFontTableTag tag) const
Return an immutable copy of the requested font table, or nullptr if that table was not found.
SkTypeface::isBold
bool isBold() const
Returns true if style() has the kBold bit set.
Definition: SkTypeface.h:53
SkTypeface::setIsFixedPitch
void setIsFixedPitch(bool isFixedPitch)
Sets the fixedPitch bit.
Definition: SkTypeface.h:353
SkFontParameters::Variation::Axis
Definition: SkFontParameters.h:17
SkTypeface::onGetAdvancedMetrics
virtual std::unique_ptr< SkAdvancedTypefaceMetrics > onGetAdvancedMetrics() const =0
SkTypeface::Equal
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...
SkTypeface::LocalizedStrings::next
virtual bool next(LocalizedString *localizedString)=0
SkTypeface::MakeFromStream
static sk_sp< SkTypeface > MakeFromStream(std::unique_ptr< SkStreamAsset > stream, int index=0)
Return a new typeface given a stream.
SkTypeface::onCopyTableData
virtual sk_sp< SkData > onCopyTableData(SkFontTableTag) const
SkTypeface::getVariationDesignParameters
int getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], int parameterCount) const
Copy into 'parameters' (allocated by the caller) the design variation parameters.
SkFontArguments
Represents a set of actual arguments for a font.
Definition: SkFontArguments.h:15
SkFontStyle::kUpright_Slant
@ kUpright_Slant
Definition: SkFontStyle.h:43
SkTypeface::isFixedPitch
bool isFixedPitch() const
Returns true if the typeface claims to be fixed-pitch.
Definition: SkTypeface.h:61
SkTypeface::getTableSize
size_t getTableSize(SkFontTableTag) const
Given a table tag, return the size of its contents, or 0 if not present.
SkTypeface::onGetUPEM
virtual int onGetUPEM() const =0