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