1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkTypeface_DEFINED
11#define SkTypeface_DEFINED
12
13#include "SkAdvancedTypefaceMetrics.h"
14#include "SkRefCnt.h"
15
16class SkStream;
17class SkAdvancedTypefaceMetrics;
18class SkWStream;
19
20typedef uint32_t SkFontID;
21
22/** \class SkTypeface
23
24    The SkTypeface class specifies the typeface and intrinsic style of a font.
25    This is used in the paint, along with optionally algorithmic settings like
26    textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
27    how text appears when drawn (and measured).
28
29    Typeface objects are immutable, and so they can be shared between threads.
30*/
31class SK_API SkTypeface : public SkRefCnt {
32public:
33    /** Style specifies the intrinsic style attributes of a given typeface
34    */
35    enum Style {
36        kNormal = 0,
37        kBold   = 0x01,
38        kItalic = 0x02,
39
40        // helpers
41        kBoldItalic = 0x03
42    };
43
44    /** Returns the typeface's intrinsic style attributes
45    */
46    Style style() const { return fStyle; }
47
48    /** Returns true if getStyle() has the kBold bit set.
49    */
50    bool isBold() const { return (fStyle & kBold) != 0; }
51
52    /** Returns true if getStyle() has the kItalic bit set.
53    */
54    bool isItalic() const { return (fStyle & kItalic) != 0; }
55
56    /** Returns true if the typeface is fixed-width
57     */
58    bool isFixedWidth() const { return fIsFixedWidth; }
59
60    /** Return a 32bit value for this typeface, unique for the underlying font
61        data. Will never return 0.
62     */
63    SkFontID uniqueID() const { return fUniqueID; }
64
65    /** Return the uniqueID for the specified typeface. If the face is null,
66        resolve it to the default font and return its uniqueID. Will never
67        return 0.
68    */
69    static SkFontID UniqueID(const SkTypeface* face);
70
71    /** Returns true if the two typefaces reference the same underlying font,
72        handling either being null (treating null as the default font)
73     */
74    static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
75
76    /** Return a new reference to the typeface that most closely matches the
77        requested familyName and style. Pass null as the familyName to return
78        the default font for the requested style. Will never return null
79
80        @param familyName  May be NULL. The name of the font family.
81        @param style       The style (normal, bold, italic) of the typeface.
82        @return reference to the closest-matching typeface. Call must call
83                unref() when they are done.
84    */
85    static SkTypeface* CreateFromName(const char familyName[], Style style);
86
87    /** Return a new reference to the typeface that covers a set of Unicode
88        code points with the specified Style. Use this call if you want to
89        pick any font that covers a given string of text.
90
91        @param data        UTF-16 characters
92        @param bytelength  length of data, in bytes
93        @return reference to the closest-matching typeface. Call must call
94                unref() when they are done.
95    */
96    static SkTypeface* CreateForChars(const void* data, size_t bytelength,
97                                      Style s);
98
99    /** Return a new reference to the typeface that most closely matches the
100        requested typeface and specified Style. Use this call if you want to
101        pick a new style from the same family of the existing typeface.
102        If family is NULL, this selects from the default font's family.
103
104        @param family  May be NULL. The name of the existing type face.
105        @param s       The style (normal, bold, italic) of the type face.
106        @return reference to the closest-matching typeface. Call must call
107                unref() when they are done.
108    */
109    static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
110
111    /** Return a new typeface given a file. If the file does not exist, or is
112        not a valid font file, returns null.
113    */
114    static SkTypeface* CreateFromFile(const char path[]);
115
116    /** Return a new typeface given a stream. If the stream is
117        not a valid font file, returns null. Ownership of the stream is
118        transferred, so the caller must not reference it again.
119    */
120    static SkTypeface* CreateFromStream(SkStream* stream);
121
122    /** Write a unique signature to a stream, sufficient to reconstruct a
123        typeface referencing the same font when Deserialize is called.
124     */
125    void serialize(SkWStream*) const;
126
127    /** Given the data previously written by serialize(), return a new instance
128        to a typeface referring to the same font. If that font is not available,
129        return null. If an instance is returned, the caller is responsible for
130        calling unref() when they are done with it.
131     */
132    static SkTypeface* Deserialize(SkStream*);
133
134    /** Retrieve detailed typeface metrics.  Used by the PDF backend.
135        @param perGlyphInfo Indicate what glyph specific information (advances,
136                            names, etc.) should be populated.
137        @param glyphIDs  For per-glyph info, specify subset of the font by
138                         giving glyph ids.  Each integer represents a glyph
139                         id.  Passing NULL means all glyphs in the font.
140        @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
141                             glyphIDs is NULL.
142        @return The returned object has already been referenced.
143     */
144    SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics(
145            SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
146            const uint32_t* glyphIDs = NULL,
147            uint32_t glyphIDsCount = 0) const;
148
149protected:
150    /** uniqueID must be unique (please!) and non-zero
151    */
152    SkTypeface(Style style, SkFontID uniqueID, bool isFixedWidth = false);
153    virtual ~SkTypeface();
154
155private:
156    SkFontID    fUniqueID;
157    Style       fStyle;
158    bool        fIsFixedWidth;
159
160    typedef SkRefCnt INHERITED;
161};
162
163#endif
164