• 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 "SkRefCnt.h"
21 
22 class SkStream;
23 class SkWStream;
24 
25 /** \class SkTypeface
26 
27     The SkTypeface class specifies the typeface and intrinsic style of a font.
28     This is used in the paint, along with optionally algorithmic settings like
29     textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
30     how text appears when drawn (and measured).
31 
32     Typeface objects are immutable, and so they can be shared between threads.
33 */
34 class SkTypeface : public SkRefCnt {
35 public:
36     /** Style specifies the intrinsic style attributes of a given typeface
37     */
38     enum Style {
39         kNormal = 0,
40         kBold   = 0x01,
41         kItalic = 0x02,
42 
43         // helpers
44         kBoldItalic = 0x03
45     };
46 
47     /** Returns the typeface's intrinsic style attributes
48     */
style()49     Style style() const { return fStyle; }
50 
51     /** Returns true if getStyle() has the kBold bit set.
52     */
isBold()53     bool isBold() const { return (fStyle & kBold) != 0; }
54 
55     /** Returns true if getStyle() has the kItalic bit set.
56     */
isItalic()57     bool isItalic() const { return (fStyle & kItalic) != 0; }
58 
59     /** Return a 32bit value for this typeface, unique for the underlying font
60         data. Will never return 0.
61      */
uniqueID()62     uint32_t uniqueID() const { return fUniqueID; }
63 
64     /** Return the uniqueID for the specified typeface. If the face is null,
65         resolve it to the default font and return its uniqueID. Will never
66         return 0.
67     */
68     static uint32_t UniqueID(const SkTypeface* face);
69 
70     /** Returns true if the two typefaces reference the same underlying font,
71         handling either being null (treating null as the default font)
72      */
73     static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
74 
75     /** Return a new reference to the typeface that most closely matches the
76         requested familyName and style. Pass null as the familyName to return
77         the default font for the requested style. Will never return null
78 
79         @param familyName  May be NULL. The name of the font family.
80         @param style       The style (normal, bold, italic) of the typeface.
81         @return reference to the closest-matching typeface. Call must call
82                 unref() when they are done.
83     */
84     static SkTypeface* CreateFromName(const char familyName[], Style style);
85 
86     /** Return a new reference to the typeface that most closely matches the
87         requested typeface and specified Style. Use this call if you want to
88         pick a new style from the same family of the existing typeface.
89         If family is NULL, this selects from the default font's family.
90 
91         @param family  May be NULL. The name of the existing type face.
92         @param s       The style (normal, bold, italic) of the type face.
93         @return reference to the closest-matching typeface. Call must call
94                 unref() when they are done.
95     */
96     static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
97 
98     /** Return a new typeface given a file. If the file does not exist, or is
99         not a valid font file, returns null.
100     */
101     static SkTypeface* CreateFromFile(const char path[]);
102 
103     /** Return a new typeface given a stream. If the stream is
104         not a valid font file, returns null. Ownership of the stream is
105         transferred, so the caller must not reference it again.
106     */
107     static SkTypeface* CreateFromStream(SkStream* stream);
108 
109     /** Write a unique signature to a stream, sufficient to reconstruct a
110         typeface referencing the same font when Deserialize is called.
111      */
112     void serialize(SkWStream*) const;
113 
114     /** Given the data previously written by serialize(), return a new instance
115         to a typeface referring to the same font. If that font is not available,
116         return null. If an instance is returned, the caller is responsible for
117         calling unref() when they are done with it.
118      */
119     static SkTypeface* Deserialize(SkStream*);
120 
121 protected:
122     /** uniqueID must be unique (please!) and non-zero
123     */
SkTypeface(Style style,uint32_t uniqueID)124     SkTypeface(Style style, uint32_t uniqueID)
125         : fUniqueID(uniqueID), fStyle(style) {}
126 
127 private:
128     uint32_t    fUniqueID;
129     Style       fStyle;
130 
131     typedef SkRefCnt INHERITED;
132 };
133 
134 #endif
135