Skia
2DGraphicsLibrary
 All Classes Namespaces 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 "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 class SkStream;
24 class SkStreamAsset;
25 class SkWStream;
26 struct SkAdvancedTypefaceMetrics;
27 struct SkScalerContextEffects;
28 struct SkScalerContextRec;
29 
30 typedef uint32_t SkFontID;
32 typedef uint32_t SkFontTableTag;
33 
43 class SK_API SkTypeface : public SkWeakRefCnt {
44 public:
47  enum Style {
48  kNormal = 0,
49  kBold = 0x01,
50  kItalic = 0x02,
51 
52  // helpers
53  kBoldItalic = 0x03
54  };
55 
58  return fStyle;
59  }
60 
64  Style style() const {
65  return static_cast<Style>(
66  (fStyle.weight() >= SkFontStyle::kSemiBold_Weight ? kBold : kNormal) |
67  (fStyle.slant() != SkFontStyle::kUpright_Slant ? kItalic : kNormal));
68  }
69 
71  bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
72 
74  bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
75 
79  bool isFixedPitch() const { return fIsFixedPitch; }
80 
92  int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
93  int coordinateCount) const;
94 
98  SkFontID uniqueID() const { return fUniqueID; }
99 
104  static SkFontID UniqueID(const SkTypeface* face);
105 
109  static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
110 
112  static sk_sp<SkTypeface> MakeDefault(Style style = SkTypeface::kNormal);
113 
123  static sk_sp<SkTypeface> MakeFromName(const char familyName[], SkFontStyle fontStyle);
124 
133  static sk_sp<SkTypeface> MakeFromTypeface(SkTypeface* family, Style);
134 
138  static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
139 
144  static sk_sp<SkTypeface> MakeFromStream(SkStreamAsset* stream, int index = 0);
145 
149  static sk_sp<SkTypeface> MakeFromFontData(std::unique_ptr<SkFontData>);
150 
154  void serialize(SkWStream*) const;
155 
161  static sk_sp<SkTypeface> MakeDeserialize(SkStream*);
162 
163  enum Encoding {
164  kUTF8_Encoding,
165  kUTF16_Encoding,
166  kUTF32_Encoding
167  };
168 
185  int charsToGlyphs(const void* chars, Encoding encoding, SkGlyphID glyphs[],
186  int glyphCount) const;
187 
191  int countGlyphs() const;
192 
193  // Table getters -- may fail if the underlying font format is not organized
194  // as 4-byte tables.
195 
197  int countTables() const;
198 
204  int getTableTags(SkFontTableTag tags[]) const;
205 
208  size_t getTableSize(SkFontTableTag) const;
209 
230  size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
231  void* data) const;
232 
237  int getUnitsPerEm() const;
238 
259  bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
260  int32_t adjustments[]) const;
261 
263  SkString fString;
264  SkString fLanguage;
265  };
266  class LocalizedStrings : ::SkNoncopyable {
267  public:
268  virtual ~LocalizedStrings() { }
269  virtual bool next(LocalizedString* localizedString) = 0;
270  void unref() { delete this; }
271  };
277  LocalizedStrings* createFamilyNameIterator() const;
278 
284  void getFamilyName(SkString* name) const;
285 
293  SkStreamAsset* openStream(int* ttcIndex) const;
294 
298  std::unique_ptr<SkFontData> makeFontData() const;
299 
305  std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
306  const SkDescriptor*,
307  bool allowFailure = false) const;
308 
314  SkRect getBounds() const;
315 
316  // PRIVATE / EXPERIMENTAL -- do not call
317  void filterRec(SkScalerContextRec* rec) const {
318  this->onFilterRec(rec);
319  }
320  // PRIVATE / EXPERIMENTAL -- do not call
321  void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
322  this->onGetFontDescriptor(desc, isLocal);
323  }
324  // PRIVATE / EXPERIMENTAL -- do not call
325  void* internal_private_getCTFontRef() const {
326  return this->onGetCTFontRef();
327  }
328 
329 protected:
332  SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
333  virtual ~SkTypeface();
334 
336  void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
338  void setFontStyle(SkFontStyle style) { fStyle = style; }
339 
340  friend class SkScalerContext;
341  static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
342 
343  virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
344  const SkDescriptor*) const = 0;
345  virtual void onFilterRec(SkScalerContextRec*) const = 0;
346 
347  // Subclasses *must* override this method to work with the PDF backend.
348  virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const;
349 
350  virtual SkStreamAsset* onOpenStream(int* ttcIndex) const = 0;
351  // TODO: make pure virtual.
352  virtual std::unique_ptr<SkFontData> onMakeFontData() const;
353 
354  virtual int onGetVariationDesignPosition(
356  int coordinateCount) const = 0;
357 
358  virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
359 
360  virtual int onCharsToGlyphs(const void* chars, Encoding, SkGlyphID glyphs[],
361  int glyphCount) const = 0;
362  virtual int onCountGlyphs() const = 0;
363 
364  virtual int onGetUPEM() const = 0;
365  virtual bool onGetKerningPairAdjustments(const SkGlyphID glyphs[], int count,
366  int32_t adjustments[]) const;
367 
371  virtual void onGetFamilyName(SkString* familyName) const = 0;
372 
374  virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0;
375 
376  virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
377  virtual size_t onGetTableData(SkFontTableTag, size_t offset,
378  size_t length, void* data) const = 0;
379 
380  virtual bool onComputeBounds(SkRect*) const;
381 
382  virtual void* onGetCTFontRef() const { return nullptr; }
383 
384 private:
385  friend class SkRandomTypeface;
386  friend class SkPDFFont;
387  friend class GrPathRendering;
388  friend class GrGLPathRendering;
389 
391  std::unique_ptr<SkAdvancedTypefaceMetrics> getAdvancedMetrics() const;
392 
393 private:
394  SkFontID fUniqueID;
395  SkFontStyle fStyle;
396  mutable SkRect fBounds;
397  mutable SkOnce fBoundsOnce;
398  bool fIsFixedPitch;
399 
400  friend class SkPaint;
401  friend class SkGlyphCache; // GetDefaultTypeface
402 
403  typedef SkWeakRefCnt INHERITED;
404 };
405 #endif
bool isFixedPitch() const
Returns true if the typeface claims to be fixed-pitch.
Definition: SkTypeface.h:79
Style style() const
Returns the typeface's intrinsic style attributes.
Definition: SkTypeface.h:64
SkFontStyle fontStyle() const
Returns the typeface's intrinsic style attributes.
Definition: SkTypeface.h:57
Style
Style specifies the intrinsic style attributes of a given typeface.
Definition: SkTypeface.h:47
Definition: SkFontArguments.h:17
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:299
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:164
void setIsFixedPitch(bool isFixedPitch)
Sets the fixedPitch bit.
Definition: SkTypeface.h:336
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:45
Definition: SkTypeface.h:262
The SkTypeface class specifies the typeface and intrinsic style of a font.
Definition: SkTypeface.h:43
void setFontStyle(SkFontStyle style)
Sets the font style.
Definition: SkTypeface.h:338
Definition: SkTypeface.h:266
Definition: SkFontStyle.h:13
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:40
Definition: SkStream.h:182
Definition: SkRect.h:404
bool isBold() const
Returns true if style() has the kBold bit set.
Definition: SkTypeface.h:71
SkFontID uniqueID() const
Return a 32bit value for this typeface, unique for the underlying font data.
Definition: SkTypeface.h:98
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:74