• 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 /** \class SkFontHost
28 
29     This class is ported to each environment. It is responsible for bridging
30     the gap between the (sort of) abstract class SkTypeface and the
31     platform-specific implementation that provides access to font files.
32 
33     One basic task is for each create (subclass of) SkTypeface, the FontHost is
34     resonsible for assigning a uniqueID. The ID should be unique for the
35     underlying font file/data, not unique per typeface instance. Thus it is
36     possible/common to request a typeface for the same font more than once
37     (e.g. asking for the same font by name several times). The FontHost may
38     return seperate typeface instances in that case, or it may choose to use a
39     cache and return the same instance (but calling typeface->ref(), since the
40     caller is always responsible for calling unref() on each instance that is
41     returned). Either way, the fontID for those instance(s) will be the same.
42     In addition, the fontID should never be set to 0. That value is used as a
43     sentinel to indicate no-font-id.
44 
45     The major aspects are:
46     1) Given either a name/style, return a subclass of SkTypeface that
47         references the closest matching font available on the host system.
48     2) Given the data for a font (either in a stream or a file name), return
49         a typeface that allows access to that data.
50     3) Each typeface instance carries a 32bit ID for its corresponding font.
51         SkFontHost turns that ID into a stream to access the font's data.
52     4) Given a font ID, return a subclass of SkScalerContext, which connects a
53         font scaler (e.g. freetype or other) to the font's data.
54     5) Utilites to manage the font cache (budgeting) and gamma correction
55 */
56 class SkFontHost {
57 public:
58     /** Return a new, closest matching typeface given either an existing family
59         (specified by a typeface in that family) or by a familyName, and a
60         requested style.
61         1) If familyFace is null, use famillyName.
62         2) If famillyName is null, use familyFace.
63         3) If both are null, return the default font that best matches style
64      */
65     static SkTypeface* CreateTypeface(const SkTypeface* familyFace,
66                                       const char famillyName[],
67                                       SkTypeface::Style style);
68 
69     /** Return a new typeface given the data buffer. If the data does not
70         represent a valid font, returns null.
71 
72         If a typeface instance is returned, the caller is responsible for
73         calling unref() on the typeface when they are finished with it.
74 
75         The returned typeface may or may not have called ref() on the stream
76         parameter. If the typeface has not called ref(), then it may have made
77         a copy of the releveant data. In either case, the caller is still
78         responsible for its refcnt ownership of the stream.
79      */
80     static SkTypeface* CreateTypefaceFromStream(SkStream*);
81 
82     /** Return a new typeface from the specified file path. If the file does not
83         represent a valid font, this returns null. If a typeface is returned,
84         the caller is responsible for calling unref() when it is no longer used.
85      */
86     static SkTypeface* CreateTypefaceFromFile(const char path[]);
87 
88     ///////////////////////////////////////////////////////////////////////////
89 
90     /** Returns true if the specified unique ID matches an existing font.
91         Returning false is similar to calling OpenStream with an invalid ID,
92         which will return NULL in that case.
93     */
94     static bool ValidFontID(uint32_t uniqueID);
95 
96     /** Return a new stream to read the font data, or null if the uniqueID does
97         not match an existing typeface. .The caller must call stream->unref()
98         when it is finished reading the data.
99     */
100     static SkStream* OpenStream(uint32_t uniqueID);
101 
102     ///////////////////////////////////////////////////////////////////////////
103 
104     /** Write a unique identifier to the stream, so that the same typeface can
105         be retrieved with Deserialize().
106     */
107     static void Serialize(const SkTypeface*, SkWStream*);
108 
109     /** Given a stream created by Serialize(), return a new typeface (like
110         CreateTypeface) or return NULL if no match is found.
111      */
112     static SkTypeface* Deserialize(SkStream*);
113 
114     ///////////////////////////////////////////////////////////////////////////
115 
116     /** Return a subclass of SkScalarContext
117     */
118     static SkScalerContext* CreateScalerContext(const SkDescriptor* desc);
119 
120     /** Given a "current" fontID, return the next logical fontID to use
121         when searching fonts for a given unicode value. Typically the caller
122         will query a given font, and if a unicode value is not supported, they
123         will call this, and if 0 is not returned, will search that font, and so
124         on. This process must be finite, and when the fonthost sees a
125         font with no logical successor, it must return 0.
126     */
127     static uint32_t NextLogicalFont(uint32_t fontID);
128 
129     ///////////////////////////////////////////////////////////////////////////
130 
131     /** Return the number of bytes (approx) that should be purged from the font
132         cache. The input parameter is the cache's estimate of how much as been
133         allocated by the cache so far.
134         To purge (basically) everything, return the input parameter.
135         To purge nothing, return 0
136     */
137     static size_t ShouldPurgeFontCache(size_t sizeAllocatedSoFar);
138 
139     /** Return SkScalerContext gamma flag, or 0, based on the paint that will be
140         used to draw something with antialiasing.
141     */
142     static int ComputeGammaFlag(const SkPaint& paint);
143 
144     /** Return NULL or a pointer to 256 bytes for the black (table[0]) and
145         white (table[1]) gamma tables.
146     */
147     static void GetGammaTables(const uint8_t* tables[2]);
148 };
149 
150 #endif
151 
152