blob: ede40b7a31fe8e2673824e80f8ba6544e99135be [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
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
23class SkDescriptor;
24class SkStream;
25class SkWStream;
26
27/** \class SkFontHost
28
29 This class is ported to each environment. It is responsible for bridging the gap
30 between SkTypeface and the resulting platform-specific instance of SkScalerContext.
31*/
32class SkFontHost {
33public:
34 /** Return the closest matching typeface given either an existing family
35 (specified by a typeface in that family) or by a familyName, and a
36 requested style.
37 1) If familyFace is null, use famillyName.
38 2) If famillyName is null, use familyFace.
39 3) If both are null, return the default font that best matches style
40
41 NOTE: this does not return a new typeface, nor does it affect the
42 owner count of an existing one, so the caller is free to ignore the
43 return result, or just compare it against null.
44 */
45 static SkTypeface* FindTypeface(const SkTypeface* familyFace,
46 const char famillyName[],
47 SkTypeface::Style style);
48
49 /** Return the typeface associated with the uniqueID, or null if that ID
50 does not match any faces.
51
52 NOTE: this does not return a new typeface, nor does it affect the
53 owner count of an existing one, so the caller is free to ignore the
54 return result, or just compare it against null.
55 */
56 static SkTypeface* ResolveTypeface(uint32_t uniqueID);
57
58 /** Return a new stream to read the font data, or null if the uniqueID does
59 not match an existing typeface. The caller must call CloseStream() when
60 it is finished reading the stream.
61 */
62 static SkStream* OpenStream(uint32_t uniqueID);
63
64 /** Call this when finished reading from the stream returned by OpenStream.
65 The caller should NOT try to delete the stream.
66 */
67 static void CloseStream(uint32_t uniqueID, SkStream*);
68
69 /** Return a new typeface given the data buffer (owned by the caller).
70 If the data does not represent a valid font, return null. The caller is
71 responsible for unref-ing the returned typeface (if it is not null).
72 */
73 static SkTypeface* CreateTypeface(SkStream*);
74
75 ///////////////////////////////////////////////////////////////////////////
76
77 /** Write a unique identifier to the stream, so that the same typeface can
78 be retrieved with Deserialize().
79 */
80 static void Serialize(const SkTypeface*, SkWStream*);
81
82 /** Given a stream created by Serialize(), return the corresponding typeface
83 or null if no match is found.
84
85 NOTE: this does not return a new typeface, nor does it affect the
86 owner count of an existing one, so the caller is free to ignore the
87 return result, or just compare it against null.
88 */
89 static SkTypeface* Deserialize(SkStream*);
90
91 ///////////////////////////////////////////////////////////////////////////
92
93 /** Return a subclass of SkScalarContext
94 */
95 static SkScalerContext* CreateScalerContext(const SkDescriptor* desc);
96
97 /** Return a scalercontext using the "fallback" font. If there is no designated
98 fallback, return null.
99 */
100 static SkScalerContext* CreateFallbackScalerContext(const SkScalerContext::Rec&);
101
102 /** Return the number of bytes (approx) that should be purged from the font
103 cache. The input parameter is the cache's estimate of how much as been
104 allocated by the cache so far.
105 To purge (basically) everything, return the input parameter.
106 To purge nothing, return 0
107 */
108 static size_t ShouldPurgeFontCache(size_t sizeAllocatedSoFar);
109
110 /** Return SkScalerContext gamma flag, or 0, based on the paint that will be
111 used to draw something with antialiasing.
112 */
113 static int ComputeGammaFlag(const SkPaint& paint);
114
115 /** Return NULL or a pointer to 256 bytes for the black (table[0]) and
116 white (table[1]) gamma tables.
117 */
118 static void GetGammaTables(const uint8_t* tables[2]);
119};
120
121#endif
122