• 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 #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;
31 /** Machine endian. */
32 typedef uint32_t SkFontTableTag;
33 
34 /** \class SkTypeface
35 
36     The SkTypeface class specifies the typeface and intrinsic style of a font.
37     This is used in the paint, along with optionally algorithmic settings like
38     textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
39     how text appears when drawn (and measured).
40 
41     Typeface objects are immutable, and so they can be shared between threads.
42 */
43 class SK_API SkTypeface : public SkWeakRefCnt {
44 public:
45     /** Style specifies the intrinsic style attributes of a given typeface
46     */
47     enum Style {
48         kNormal = 0,
49         kBold   = 0x01,
50         kItalic = 0x02,
51 
52         // helpers
53         kBoldItalic = 0x03
54     };
55 
56     /** Returns the typeface's intrinsic style attributes. */
fontStyle()57     SkFontStyle fontStyle() const {
58         return fStyle;
59     }
60 
61     /** Returns the typeface's intrinsic style attributes.
62      *  @deprecated use fontStyle() instead.
63      */
style()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 
70     /** Returns true if style() has the kBold bit set. */
isBold()71     bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
72 
73     /** Returns true if style() has the kItalic bit set. */
isItalic()74     bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
75 
76     /** Returns true if the typeface claims to be fixed-pitch.
77      *  This is a style bit, advance widths may vary even if this returns true.
78      */
isFixedPitch()79     bool isFixedPitch() const { return fIsFixedPitch; }
80 
81     /** Copy into 'coordinates' (allocated by the caller) the design variation coordinates.
82      *
83      *  @param coordinates the buffer into which to write the design variation coordinates.
84      *  @param coordinateCount the number of entries available through 'coordinates'.
85      *
86      *  @return The number of axes, or -1 if there is an error.
87      *  If 'coordinates != nullptr' and 'coordinateCount >= numAxes' then 'coordinates' will be
88      *  filled with the variation coordinates describing the position of this typeface in design
89      *  variation space. It is possible the number of axes can be retrieved but actual position
90      *  cannot.
91      */
92     int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
93                                    int coordinateCount) 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 typeface, which is never nullptr. */
112     static sk_sp<SkTypeface> MakeDefault(Style style = SkTypeface::kNormal);
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 the typeface that most closely matches the requested typeface and style.
126         Use this to pick a new style from the same family of the existing typeface.
127         If family is nullptr, this selects from the default font's family.
128 
129         @param family  May be NULL. The name of the existing type face.
130         @param s       The style (normal, bold, italic) of the type face.
131         @return the closest-matching typeface.
132     */
133     static sk_sp<SkTypeface> MakeFromTypeface(SkTypeface* family, Style);
134 
135     /** Return a new typeface given a file. If the file does not exist, or is
136         not a valid font file, returns nullptr.
137     */
138     static sk_sp<SkTypeface> MakeFromFile(const char path[], int index = 0);
139 
140     /** Return a new typeface given a stream. If the stream is
141         not a valid font file, returns nullptr. Ownership of the stream is
142         transferred, so the caller must not reference it again.
143     */
144     static sk_sp<SkTypeface> MakeFromStream(SkStreamAsset* stream, int index = 0);
145 
146     /** Return a new typeface given font data and configuration. If the data
147         is not valid font data, returns nullptr.
148     */
149     static sk_sp<SkTypeface> MakeFromFontData(std::unique_ptr<SkFontData>);
150 
151     /** Write a unique signature to a stream, sufficient to reconstruct a
152         typeface referencing the same font when Deserialize is called.
153      */
154     void serialize(SkWStream*) const;
155 
156     /** Given the data previously written by serialize(), return a new instance
157         of a typeface referring to the same font. If that font is not available,
158         return nullptr.
159         Does not affect ownership of SkStream.
160      */
161     static sk_sp<SkTypeface> MakeDeserialize(SkStream*);
162 
163     enum Encoding {
164         kUTF8_Encoding,
165         kUTF16_Encoding,
166         kUTF32_Encoding
167     };
168 
169     /**
170      *  Given an array of character codes, of the specified encoding,
171      *  optionally return their corresponding glyph IDs (if glyphs is not NULL).
172      *
173      *  @param chars pointer to the array of character codes
174      *  @param encoding how the characters are encoded
175      *  @param glyphs (optional) returns the corresponding glyph IDs for each
176      *          character code, up to glyphCount values. If a character code is
177      *          not found in the typeface, the corresponding glyph ID will be 0.
178      *  @param glyphCount number of code points in 'chars' to process. If glyphs
179      *          is not NULL, then it must point sufficient memory to write
180      *          glyphCount values into it.
181      *  @return the number of number of continuous non-zero glyph IDs computed
182      *          from the beginning of chars. This value is valid, even if the
183      *          glyphs parameter is NULL.
184      */
185     int charsToGlyphs(const void* chars, Encoding encoding, SkGlyphID glyphs[],
186                       int glyphCount) const;
187 
188     /**
189      *  Return the number of glyphs in the typeface.
190      */
191     int countGlyphs() const;
192 
193     // Table getters -- may fail if the underlying font format is not organized
194     // as 4-byte tables.
195 
196     /** Return the number of tables in the font. */
197     int countTables() const;
198 
199     /** Copy into tags[] (allocated by the caller) the list of table tags in
200      *  the font, and return the number. This will be the same as CountTables()
201      *  or 0 if an error occured. If tags == NULL, this only returns the count
202      *  (the same as calling countTables()).
203      */
204     int getTableTags(SkFontTableTag tags[]) const;
205 
206     /** Given a table tag, return the size of its contents, or 0 if not present
207      */
208     size_t getTableSize(SkFontTableTag) const;
209 
210     /** Copy the contents of a table into data (allocated by the caller). Note
211      *  that the contents of the table will be in their native endian order
212      *  (which for most truetype tables is big endian). If the table tag is
213      *  not found, or there is an error copying the data, then 0 is returned.
214      *  If this happens, it is possible that some or all of the memory pointed
215      *  to by data may have been written to, even though an error has occured.
216      *
217      *  @param fontID the font to copy the table from
218      *  @param tag  The table tag whose contents are to be copied
219      *  @param offset The offset in bytes into the table's contents where the
220      *  copy should start from.
221      *  @param length The number of bytes, starting at offset, of table data
222      *  to copy.
223      *  @param data storage address where the table contents are copied to
224      *  @return the number of bytes actually copied into data. If offset+length
225      *  exceeds the table's size, then only the bytes up to the table's
226      *  size are actually copied, and this is the value returned. If
227      *  offset > the table's size, or tag is not a valid table,
228      *  then 0 is returned.
229      */
230     size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
231                         void* data) const;
232 
233     /**
234      *  Return the units-per-em value for this typeface, or zero if there is an
235      *  error.
236      */
237     int getUnitsPerEm() const;
238 
239     /**
240      *  Given a run of glyphs, return the associated horizontal adjustments.
241      *  Adjustments are in "design units", which are integers relative to the
242      *  typeface's units per em (see getUnitsPerEm).
243      *
244      *  Some typefaces are known to never support kerning. Calling this method
245      *  with all zeros (e.g. getKerningPairAdustments(NULL, 0, NULL)) returns
246      *  a boolean indicating if the typeface might support kerning. If it
247      *  returns false, then it will always return false (no kerning) for all
248      *  possible glyph runs. If it returns true, then it *may* return true for
249      *  somne glyph runs.
250      *
251      *  If count is non-zero, then the glyphs parameter must point to at least
252      *  [count] valid glyph IDs, and the adjustments parameter must be
253      *  sized to at least [count - 1] entries. If the method returns true, then
254      *  [count-1] entries in the adjustments array will be set. If the method
255      *  returns false, then no kerning should be applied, and the adjustments
256      *  array will be in an undefined state (possibly some values may have been
257      *  written, but none of them should be interpreted as valid values).
258      */
259     bool getKerningPairAdjustments(const SkGlyphID glyphs[], int count,
260                                    int32_t adjustments[]) const;
261 
262     struct LocalizedString {
263         SkString fString;
264         SkString fLanguage;
265     };
266     class LocalizedStrings : ::SkNoncopyable {
267     public:
~LocalizedStrings()268         virtual ~LocalizedStrings() { }
269         virtual bool next(LocalizedString* localizedString) = 0;
unref()270         void unref() { delete this; }
271     };
272     /**
273      *  Returns an iterator which will attempt to enumerate all of the
274      *  family names specified by the font.
275      *  It is the caller's responsibility to unref() the returned pointer.
276      */
277     LocalizedStrings* createFamilyNameIterator() const;
278 
279     /**
280      *  Return the family name for this typeface. It will always be returned
281      *  encoded as UTF8, but the language of the name is whatever the host
282      *  platform chooses.
283      */
284     void getFamilyName(SkString* name) const;
285 
286     /**
287      *  Return a stream for the contents of the font data, or NULL on failure.
288      *  If ttcIndex is not null, it is set to the TrueTypeCollection index
289      *  of this typeface within the stream, or 0 if the stream is not a
290      *  collection.
291      *  The caller is responsible for deleting the stream.
292      */
293     SkStreamAsset* openStream(int* ttcIndex) const;
294 
295     /**
296      *  Return the font data, or nullptr on failure.
297      */
298     std::unique_ptr<SkFontData> makeFontData() const;
299 
300     /**
301      *  Return a scalercontext for the given descriptor. If this fails, then
302      *  if allowFailure is true, this returns NULL, else it returns a
303      *  dummy scalercontext that will not crash, but will draw nothing.
304      */
305     std::unique_ptr<SkScalerContext> createScalerContext(const SkScalerContextEffects&,
306                                                          const SkDescriptor*,
307                                                          bool allowFailure = false) const;
308 
309     /**
310      *  Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all
311      *  of the glyphs, but each one positioned at (0,). This may be conservatively large, and
312      *  will not take into account any hinting or other size-specific adjustments.
313      */
314     SkRect getBounds() const;
315 
316     // PRIVATE / EXPERIMENTAL -- do not call
filterRec(SkScalerContextRec * rec)317     void filterRec(SkScalerContextRec* rec) const {
318         this->onFilterRec(rec);
319     }
320     // PRIVATE / EXPERIMENTAL -- do not call
getFontDescriptor(SkFontDescriptor * desc,bool * isLocal)321     void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
322         this->onGetFontDescriptor(desc, isLocal);
323     }
324     // PRIVATE / EXPERIMENTAL -- do not call
internal_private_getCTFontRef()325     void* internal_private_getCTFontRef() const {
326         return this->onGetCTFontRef();
327     }
328 
329 protected:
330     /** uniqueID must be unique and non-zero
331     */
332     SkTypeface(const SkFontStyle& style, bool isFixedPitch = false);
333     virtual ~SkTypeface();
334 
335     /** Sets the fixedPitch bit. If used, must be called in the constructor. */
setIsFixedPitch(bool isFixedPitch)336     void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
337     /** Sets the font style. If used, must be called in the constructor. */
setFontStyle(SkFontStyle style)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(
355         SkFontArguments::VariationPosition::Coordinate coordinates[],
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 
368     /** Returns the family name of the typeface as known by its font manager.
369      *  This name may or may not be produced by the family name iterator.
370      */
371     virtual void onGetFamilyName(SkString* familyName) const = 0;
372 
373     /** Returns an iterator over the family names in the font. */
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 
onGetCTFontRef()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 
390     /** Retrieve detailed typeface metrics.  Used by the PDF backend.  */
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
406