Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkTypeface.h
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 "SkFontStyle.h"
15 #include "SkRect.h"
16 #include "SkString.h"
17 
18 class SkDescriptor;
19 class SkFontData;
20 class SkFontDescriptor;
21 class SkScalerContext;
22 struct SkScalerContextRec;
23 struct SkScalerContextEffects;
24 class SkStream;
25 class SkStreamAsset;
26 class SkAdvancedTypefaceMetrics;
27 class SkWStream;
28 
29 typedef uint32_t SkFontID;
31 typedef uint32_t SkFontTableTag;
32 
42 class SK_API SkTypeface : public SkWeakRefCnt {
43 public:
46  enum Style {
47  kNormal = 0,
48  kBold = 0x01,
49  kItalic = 0x02,
50 
51  // helpers
52  kBoldItalic = 0x03
53  };
54 
57  return fStyle;
58  }
59 
63  Style style() const {
64  return static_cast<Style>(
65  (fStyle.weight() >= SkFontStyle::kSemiBold_Weight ? kBold : kNormal) |
66  (fStyle.slant() != SkFontStyle::kUpright_Slant ? kItalic : kNormal));
67  }
68 
70  bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
71 
73  bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
74 
78  bool isFixedPitch() const { return fIsFixedPitch; }
79 
83  SkFontID uniqueID() const { return fUniqueID; }
84 
89  static SkFontID UniqueID(const SkTypeface* face);
90 
94  static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
95 
97  static sk_sp<SkTypeface> MakeDefault(Style style = SkTypeface::kNormal);
98 #ifdef SK_SUPPORT_LEGACY_TYPEFACE_PTR
99  static SkTypeface* RefDefault(Style style = SkTypeface::kNormal) {
100  return MakeDefault(style).release();
101  }
102 #endif
103 
113  static sk_sp<SkTypeface> MakeFromName(const char familyName[], SkFontStyle fontStyle);
114 
115 #ifdef SK_SUPPORT_LEGACY_TYPEFACE_PTR
116  static SkTypeface* CreateFromName(const char familyName[], Style style) {
117  return MakeFromName(familyName, SkFontStyle::FromOldStyle(style)).release();
118  }
119 #endif
120 
129  static sk_sp<SkTypeface> MakeFromTypeface(SkTypeface* family, Style);
130 
134  static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
135 #ifdef SK_SUPPORT_LEGACY_TYPEFACE_PTR
136  static SkTypeface* CreateFromFile(const char path[], int index = 0) {
137  return MakeFromFile(path, index).release();
138  }
139 #endif
140 
145  static sk_sp<SkTypeface> MakeFromStream(SkStreamAsset* stream, int index = 0);
146 #ifdef SK_SUPPORT_LEGACY_TYPEFACE_PTR
147  static SkTypeface* CreateFromStream(SkStreamAsset* stream, int index = 0) {
148  return MakeFromStream(stream, index).release();
149  }
150 #endif
151 
156  static sk_sp<SkTypeface> MakeFromFontData(SkFontData*);
157 
161  void serialize(SkWStream*) const;
162 
168  static sk_sp<SkTypeface> MakeDeserialize(SkStream*);
169 
170  enum Encoding {
171  kUTF8_Encoding,
172  kUTF16_Encoding,
173  kUTF32_Encoding
174  };
175 
192  int charsToGlyphs(const void* chars, Encoding encoding, SkGlyphID glyphs[],
193  int glyphCount) const;
194 
198  int countGlyphs() const;
199 
200  // Table getters -- may fail if the underlying font format is not organized
201  // as 4-byte tables.
202 
204  int countTables() const;
205 
211  int getTableTags(SkFontTableTag tags[]) const;
212 
215  size_t getTableSize(SkFontTableTag) const;
216 
237  size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
238  void* data) const;
239 
244  int getUnitsPerEm() const;
245 
266  bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
267  int32_t adjustments[]) const;
268 
270  SkString fString;
271  SkString fLanguage;
272  };
273  class LocalizedStrings : ::SkNoncopyable {
274  public:
275  virtual ~LocalizedStrings() { }
276  virtual bool next(LocalizedString* localizedString) = 0;
277  void unref() { delete this; }
278  };
284  LocalizedStrings* createFamilyNameIterator() const;
285 
291  void getFamilyName(SkString* name) const;
292 
300  SkStreamAsset* openStream(int* ttcIndex) const;
301 
306  SkFontData* createFontData() const;
307 
313  SkScalerContext* createScalerContext(const SkScalerContextEffects&, const SkDescriptor*,
314  bool allowFailure = false) const;
315 
321  SkRect getBounds() const;
322 
323  // PRIVATE / EXPERIMENTAL -- do not call
324  void filterRec(SkScalerContextRec* rec) const {
325  this->onFilterRec(rec);
326  }
327  // PRIVATE / EXPERIMENTAL -- do not call
328  void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
329  this->onGetFontDescriptor(desc, isLocal);
330  }
331 
332 protected:
333  // The type of advance data wanted.
334  enum PerGlyphInfo {
335  kNo_PerGlyphInfo = 0x0, // Don't populate any per glyph info.
336  kGlyphNames_PerGlyphInfo = 0x1, // Populate glyph names (Type 1 only).
337  kToUnicode_PerGlyphInfo = 0x2 // Populate ToUnicode table, ignored
338  // for Type 1 fonts
339  };
340 
343  SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
344  virtual ~SkTypeface();
345 
347  void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
349  void setFontStyle(SkFontStyle style) { fStyle = style; }
350 
351  friend class SkScalerContext;
352  static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
353 
354  virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
355  const SkDescriptor*) const = 0;
356  virtual void onFilterRec(SkScalerContextRec*) const = 0;
357  virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
358  PerGlyphInfo,
359  const uint32_t* glyphIDs,
360  uint32_t glyphIDsCount) const = 0;
361 
362  virtual SkStreamAsset* onOpenStream(int* ttcIndex) const = 0;
363  // TODO: make pure virtual.
364  virtual SkFontData* onCreateFontData() const;
365 
366  virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
367 
368  virtual int onCharsToGlyphs(const void* chars, Encoding, SkGlyphID glyphs[],
369  int glyphCount) const = 0;
370  virtual int onCountGlyphs() const = 0;
371 
372  virtual int onGetUPEM() const = 0;
373  virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
374  int32_t adjustments[]) const;
375 
379  virtual void onGetFamilyName(SkString* familyName) const = 0;
380 
382  virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0;
383 
384  virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
385  virtual size_t onGetTableData(SkFontTableTag, size_t offset,
386  size_t length, void* data) const = 0;
387 
388  virtual bool onComputeBounds(SkRect*) const;
389 
390 private:
391  friend class SkGTypeface;
392  friend class SkRandomTypeface;
393  friend class SkPDFFont;
394  friend class GrPathRendering;
395  friend class GrGLPathRendering;
396 
407  SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics(
408  PerGlyphInfo,
409  const uint32_t* glyphIDs = NULL,
410  uint32_t glyphIDsCount = 0) const;
411 
412 private:
413  SkFontID fUniqueID;
414  SkFontStyle fStyle;
415  mutable SkRect fBounds;
416  mutable SkOnce fBoundsOnce;
417  bool fIsFixedPitch;
418 
419  friend class SkPaint;
420  friend class SkGlyphCache; // GetDefaultTypeface
421 
422  typedef SkWeakRefCnt INHERITED;
423 };
424 
425 namespace skstd {
426 template <> struct is_bitmask_enum<SkTypeface::PerGlyphInfo> : std::true_type {};
427 }
428 
429 #endif
bool isFixedPitch() const
Returns true if the typeface claims to be fixed-pitch.
Definition: SkTypeface.h:78
Style style() const
Returns the typeface's intrinsic style attributes.
Definition: SkTypeface.h:63
SkFontStyle fontStyle() const
Returns the typeface's intrinsic style attributes.
Definition: SkTypeface.h:56
Style
Style specifies the intrinsic style attributes of a given typeface.
Definition: SkTypeface.h:46
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:359
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:164
void setIsFixedPitch(bool isFixedPitch)
Sets the fixedPitch bit.
Definition: SkTypeface.h:347
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:46
Definition: SkTypeface.h:269
The SkTypeface class specifies the typeface and intrinsic style of a font.
Definition: SkTypeface.h:42
void setFontStyle(SkFontStyle style)
Sets the font style.
Definition: SkTypeface.h:349
Definition: SkTypeface.h:273
Definition: SkFontStyle.h:13
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:38
Definition: SkStream.h:182
Definition: SkRect.h:390
bool isBold() const
Returns true if style() has the kBold bit set.
Definition: SkTypeface.h:70
SkFontID uniqueID() const
Return a 32bit value for this typeface, unique for the underlying font data.
Definition: SkTypeface.h:83
Light weight class for managing strings.
Definition: SkString.h:121
bool isItalic() const
Returns true if style() has the kItalic bit set.
Definition: SkTypeface.h:73