• 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 SkFontHost_DEFINED
18 #define SkFontHost_DEFINED
19 
20 #include "SkScalerContext.h"
21 #include "SkTypeface.h"
22 
23 class SkDescriptor;
24 class SkStream;
25 class SkWStream;
26 
27 typedef uint32_t SkFontTableTag;
28 
29 /** \class SkFontHost
30 
31     This class is ported to each environment. It is responsible for bridging
32     the gap between the (sort of) abstract class SkTypeface and the
33     platform-specific implementation that provides access to font files.
34 
35     One basic task is for each create (subclass of) SkTypeface, the FontHost is
36     resonsible for assigning a uniqueID. The ID should be unique for the
37     underlying font file/data, not unique per typeface instance. Thus it is
38     possible/common to request a typeface for the same font more than once
39     (e.g. asking for the same font by name several times). The FontHost may
40     return seperate typeface instances in that case, or it may choose to use a
41     cache and return the same instance (but calling typeface->ref(), since the
42     caller is always responsible for calling unref() on each instance that is
43     returned). Either way, the fontID for those instance(s) will be the same.
44     In addition, the fontID should never be set to 0. That value is used as a
45     sentinel to indicate no-font-id.
46 
47     The major aspects are:
48     1) Given either a name/style, return a subclass of SkTypeface that
49         references the closest matching font available on the host system.
50     2) Given the data for a font (either in a stream or a file name), return
51         a typeface that allows access to that data.
52     3) Each typeface instance carries a 32bit ID for its corresponding font.
53         SkFontHost turns that ID into a stream to access the font's data.
54     4) Given a font ID, return a subclass of SkScalerContext, which connects a
55         font scaler (e.g. freetype or other) to the font's data.
56     5) Utilites to manage the font cache (budgeting) and gamma correction
57 */
58 class SK_API SkFontHost {
59 public:
60     /** Return a new, closest matching typeface given either an existing family
61         (specified by a typeface in that family) or by a familyName and a
62         requested style, or by a set of Unicode codepoitns to cover in a given
63         style.
64         1) If familyFace is null, use familyName.
65         2) If familyName is null, use data (UTF-16 to cover).
66         3) If all are null, return the default font that best matches style
67      */
68     static SkTypeface* CreateTypeface(const SkTypeface* familyFace,
69                                       const char familyName[],
70                                       const void* data, size_t bytelength,
71                                       SkTypeface::Style style);
72 
73     /** Return a new typeface given the data buffer. If the data does not
74         represent a valid font, returns null.
75 
76         If a typeface instance is returned, the caller is responsible for
77         calling unref() on the typeface when they are finished with it.
78 
79         The returned typeface may or may not have called ref() on the stream
80         parameter. If the typeface has not called ref(), then it may have made
81         a copy of the releveant data. In either case, the caller is still
82         responsible for its refcnt ownership of the stream.
83      */
84     static SkTypeface* CreateTypefaceFromStream(SkStream*);
85 
86     /** Return a new typeface from the specified file path. If the file does not
87         represent a valid font, this returns null. If a typeface is returned,
88         the caller is responsible for calling unref() when it is no longer used.
89      */
90     static SkTypeface* CreateTypefaceFromFile(const char path[]);
91 
92     ///////////////////////////////////////////////////////////////////////////
93 
94     /** Returns true if the specified unique ID matches an existing font.
95         Returning false is similar to calling OpenStream with an invalid ID,
96         which will return NULL in that case.
97     */
98     static bool ValidFontID(SkFontID uniqueID);
99 
100     /** Return a new stream to read the font data, or null if the uniqueID does
101         not match an existing typeface. .The caller must call stream->unref()
102         when it is finished reading the data.
103     */
104     static SkStream* OpenStream(SkFontID uniqueID);
105 
106     /** Some fonts are stored in files. If that is true for the fontID, then
107         this returns the byte length of the full file path. If path is not null,
108         then the full path is copied into path (allocated by the caller), up to
109         length bytes. If index is not null, then it is set to the truetype
110         collection index for this font, or 0 if the font is not in a collection.
111 
112         Note: GetFileName does not assume that path is a null-terminated string,
113         so when it succeeds, it only copies the bytes of the file name and
114         nothing else (i.e. it copies exactly the number of bytes returned by the
115         function. If the caller wants to treat path[] as a C string, it must be
116         sure that it is allocated at least 1 byte larger than the returned size,
117         and it must copy in the terminating 0.
118 
119         If the fontID does not correspond to a file, then the function returns
120         0, and the path and index parameters are ignored.
121 
122         @param fontID   The font whose file name is being queried
123         @param path     Either NULL, or storage for receiving up to length bytes
124                         of the font's file name. Allocated by the caller.
125         @param length   The maximum space allocated in path (by the caller).
126                         Ignored if path is NULL.
127         @param index    Either NULL, or receives the TTC index for this font.
128                         If the font is not a TTC, then will be set to 0.
129         @return The byte length of th font's file name, or 0 if the font is not
130                 baked by a file.
131      */
132     static size_t GetFileName(SkFontID fontID, char path[], size_t length,
133                               int32_t* index);
134 
135     ///////////////////////////////////////////////////////////////////////////
136 
137     /** Write a unique identifier to the stream, so that the same typeface can
138         be retrieved with Deserialize().
139     */
140     static void Serialize(const SkTypeface*, SkWStream*);
141 
142     /** Given a stream created by Serialize(), return a new typeface (like
143         CreateTypeface) or return NULL if no match is found.
144      */
145     static SkTypeface* Deserialize(SkStream*);
146 
147     ///////////////////////////////////////////////////////////////////////////
148 
149     /** Return a subclass of SkScalarContext
150     */
151     static SkScalerContext* CreateScalerContext(const SkDescriptor* desc);
152 
153     /**
154      *  Given a "current" fontID, return the next logical fontID to use
155      *  when searching fonts for a given unicode value. Typically the caller
156      *  will query a given font, and if a unicode value is not supported, they
157      *  will call this, and if 0 is not returned, will search that font, and so
158      *  on. This process must be finite, and when the fonthost sees a
159      *  font with no logical successor, it must return 0.
160      *
161      *  The original fontID is also provided. This is the initial font that was
162      *  stored in the typeface of the caller. It is provided as an aid to choose
163      *  the best next logical font. e.g. If the original font was bold or serif,
164      *  but the 2nd in the logical chain was plain, then a subsequent call to
165      *  get the 3rd can still inspect the original, and try to match its
166      *  stylistic attributes.
167      */
168     static SkFontID NextLogicalFont(SkFontID currFontID, SkFontID origFontID);
169 
170     ///////////////////////////////////////////////////////////////////////////
171 
172     /** Given a filled-out rec, the fonthost may decide to modify it to reflect
173         what the host is actually capable of fulfilling. For example, if the
174         rec is requesting a level of hinting that, for this host, maps some
175         other level (e.g. kFull -> kNormal), it should update the rec to reflect
176         what will actually be done. This is an optimization so that the font
177         cache does not contain different recs (i.e. keys) that in reality map to
178         the same output.
179 
180         A lazy (but valid) fonthost can do nothing in its FilterRec routine.
181      */
182     static void FilterRec(SkScalerContext::Rec* rec);
183 
184     ///////////////////////////////////////////////////////////////////////////
185 
186     /** Retrieve detailed typeface metrics.  Used by the PDF backend.
187         @param perGlyphInfo Indicate what glyph specific information (advances,
188                             names, etc.) should be populated.
189         @return The returned object has already been referenced.  NULL is
190                 returned if the font is not found.
191      */
192     static SkAdvancedTypefaceMetrics* GetAdvancedTypefaceMetrics(
193             SkFontID fontID,
194             SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo);
195 
196     /** Return the number of tables in the font
197      */
198     static int CountTables(SkFontID);
199 
200     /** Copy into tags[] (allocated by the caller) the list of table tags in
201         the font, and return the number. This will be the same as CountTables()
202         or 0 if an error occured.
203      */
204     static int GetTableTags(SkFontID, SkFontTableTag[]);
205 
206     /** Given a table tag, return the size of its contents, or 0 if not present
207      */
208     static size_t GetTableSize(SkFontID, SkFontTableTag);
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     static size_t GetTableData(SkFontID fontID, SkFontTableTag tag,
231                                size_t offset, size_t length, void* data);
232 
233     ///////////////////////////////////////////////////////////////////////////
234 
235     /** Return the number of bytes (approx) that should be purged from the font
236         cache. The input parameter is the cache's estimate of how much as been
237         allocated by the cache so far.
238         To purge (basically) everything, return the input parameter.
239         To purge nothing, return 0
240     */
241     static size_t ShouldPurgeFontCache(size_t sizeAllocatedSoFar);
242 
243     /** Return SkScalerContext gamma flag, or 0, based on the paint that will be
244         used to draw something with antialiasing.
245     */
246     static int ComputeGammaFlag(const SkPaint& paint);
247 
248     /** Return NULL or a pointer to 256 bytes for the black (table[0]) and
249         white (table[1]) gamma tables.
250     */
251     static void GetGammaTables(const uint8_t* tables[2]);
252 
253     ///////////////////////////////////////////////////////////////////////////
254 
255     /** LCDs either have their color elements arranged horizontally or
256         vertically. When rendering subpixel glyphs we need to know which way
257         round they are.
258 
259         Note, if you change this after startup, you'll need to flush the glyph
260         cache because it'll have the wrong type of masks cached.
261     */
262     enum LCDOrientation {
263         kHorizontal_LCDOrientation = 0,    //!< this is the default
264         kVertical_LCDOrientation   = 1,
265     };
266 
267     static void SetSubpixelOrientation(LCDOrientation orientation);
268     static LCDOrientation GetSubpixelOrientation();
269 
270     /** LCD color elements can vary in order. For subpixel text we need to know
271         the order which the LCDs uses so that the color fringes are in the
272         correct place.
273 
274         Note, if you change this after startup, you'll need to flush the glyph
275         cache because it'll have the wrong type of masks cached.
276 
277         kNONE_LCDOrder means that the subpixel elements are not spatially
278         separated in any usable fashion.
279      */
280     enum LCDOrder {
281         kRGB_LCDOrder = 0,    //!< this is the default
282         kBGR_LCDOrder = 1,
283         kNONE_LCDOrder = 2,
284     };
285 
286     static void SetSubpixelOrder(LCDOrder order);
287     static LCDOrder GetSubpixelOrder();
288 
289 #ifdef ANDROID
290     ///////////////////////////////////////////////////////////////////////////
291 
292     /**
293      * Return the number of font units per em.
294      *
295      * @param fontID the font to query.
296      * @return the number of font units per em or 0 on error.
297      */
298     static uint32_t GetUnitsPerEm(SkFontID fontID);
299 #endif
300 };
301 
302 #endif
303 
304