blob: 0cde81e741bcc47c4b1548a9ad928b8896be8ba1 [file] [log] [blame]
yangsu@google.com1a2b4c12011-06-24 17:20:50 +00001#import <UIKit/UIKit.h>
2
3#include "SkStream_NSData.h"
4#include "SkTypeface.h"
5#include "SkFontHost.h"
6#include "SkThread.h"
7#include "SkTemplates.h"
8
9enum FontDesign {
10 kUnknown_Design,
11 kSans_FontDesign,
12 kSerif_FontDesign,
13
14 kIllegal_FontDesign, // never use with a real font
15};
16
17// returns kIllegal_FontDesign if not found
18static FontDesign find_design_from_name(const char name[]) {
19 static const struct {
20 const char* fName;
21 FontDesign fDesign;
22 } gRec[] = {
23 { "sans-serif", kSans_FontDesign },
24 { "serif", kSerif_FontDesign },
25 };
26
27 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
28 if (!strcasecmp(name, gRec[i].fName)) {
29 return gRec[i].fDesign;
30 }
31 }
32 return kIllegal_FontDesign;
33}
34
35struct FontRes {
36 const char* fName;
37 SkTypeface::Style fStyle;
38 FontDesign fDesign;
39};
40
41static const FontRes gFontRes[] = {
42 { "DroidSans", SkTypeface::kNormal, kSans_FontDesign },
43 { "DroidSans", SkTypeface::kBold, kSans_FontDesign },
44 { "DroidSerif-Regular", SkTypeface::kNormal, kSerif_FontDesign },
45 { "DroidSerif-Bold", SkTypeface::kBold, kSerif_FontDesign },
46// { "PescaderoPro", SkTypeface::kNormal, kSerif_FontDesign },
47// { "PescaderoPro-Bold", SkTypeface::kBold, kSerif_FontDesign },
48};
49#define FONTRES_COUNT SK_ARRAY_COUNT(gFontRes)
50
51#define DEFAULT_INDEX_REGULAR 1
52#define DEFAULT_INDEX_BOLD 2
53
54///////////////////////////////////////////////////////////////////////////////
55
56class SkTypeface_Stream : public SkTypeface {
57public:
58 SkTypeface_Stream(SkStream* stream, Style style);
59 virtual ~SkTypeface_Stream();
60
61 SkStream* refStream() {
62 fStream->ref();
63 return fStream;
64 }
65
66private:
67 SkStream* fStream;
68};
69
70static int32_t gUniqueFontID;
71
72SkTypeface_Stream::SkTypeface_Stream(SkStream* stream, Style style)
73: SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1) {
74 fStream = stream;
75 fStream->ref();
76}
77
78SkTypeface_Stream::~SkTypeface_Stream() {
79 fStream->unref();
80}
81
82static SkTypeface_Stream* create_from_fontres(const FontRes& res) {
83 SkStream* stream = SkStream_NSData::CreateFromResource(res.fName, "ttf");
84 SkAutoUnref aur(stream);
85
86 return SkNEW_ARGS(SkTypeface_Stream, (stream, res.fStyle));
87}
88
89///////////////////////////////////////////////////////////////////////////////
90
91static int compute_style_distance(SkTypeface::Style a, SkTypeface::Style b) {
92 int dist = 0;
93 int diff = a ^ b;
94 if (diff & SkTypeface::kBold) {
95 dist += 2;
96 }
97 if (diff & SkTypeface::kItalic) {
98 dist += 1;
99 }
100 return dist;
101}
102
103static SkTypeface_Stream* gFonts[FONTRES_COUNT];
104
105static void assure_init_fonts() {
106 static bool gOnce;
107 if (!gOnce) {
108 for (size_t i = 0; i < FONTRES_COUNT; i++) {
109 gFonts[i] = create_from_fontres(gFontRes[i]);
110 gOnce = true;
111 }
112 }
113}
114
115static SkTypeface_Stream* get_default_font(SkTypeface::Style style) {
116 assure_init_fonts();
117
118 if (style & SkTypeface::kBold) {
119 return gFonts[DEFAULT_INDEX_BOLD];
120 } else {
121 return gFonts[DEFAULT_INDEX_REGULAR];
122 }
123}
124
125static SkTypeface_Stream* find_by_id(SkFontID fontID) {
126 assure_init_fonts();
127
128 for (size_t i = 0; i < FONTRES_COUNT; i++) {
129 if (gFonts[i]->uniqueID() == fontID) {
130 return gFonts[i];
131 }
132 }
133 return NULL;
134}
135
136///////////////////////////////////////////////////////////////////////////////
137
138template <typename T> T* ref_and_return(T* obj) {
139 obj->ref();
140 return obj;
141}
142
143SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
144 const char familyName[],
145 const void* data, size_t bytelength,
146 SkTypeface::Style style) {
147 assure_init_fonts();
148
149 if (familyName) {
150 FontDesign design = find_design_from_name(familyName);
151 if (kIllegal_FontDesign != design) {
152 familyName = "$#@*&%*#$@ never match any name";
153 }
154
155 int bestDistance = 999;
156 int bestIndex = -1;
157 for (size_t i = 0; i < FONTRES_COUNT; i++) {
158 if (design == gFontRes[i].fDesign || !strcmp(gFontRes[i].fName, familyName)) {
159 int dist = compute_style_distance(style, gFontRes[i].fStyle);
160 if (dist < bestDistance) {
161 bestDistance = dist;
162 bestIndex = i;
163 }
164 }
165 }
166 if (bestIndex >= 0) {
167 return ref_and_return(gFonts[bestIndex]);
168 }
169 }
170
171 return ref_and_return(get_default_font(style));
172}
173
174SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
175 SkASSERT(!"SkFontHost::CreateTypeface unimplemented");
176 return NULL;
177}
178
179SkTypeface* SkFontHost::CreateTypefaceFromFile(char const*) {
180// SkASSERT(!"SkFontHost::CreateTypefaceFromFile unimplemented");
181 return NULL;
182}
183
184///////////////////////////////////////////////////////////////////////////////
185
186bool SkFontHost::ValidFontID(uint32_t uniqueID) {
187 return true;
188}
189
190SkStream* SkFontHost::OpenStream(uint32_t uniqueID) {
191 SkTypeface_Stream* tf = find_by_id(uniqueID);
192 SkASSERT(tf);
193 return tf->refStream();
194}
195
196size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
197 int32_t* index) {
198 SkDebugf("SkFontHost::GetFileName unimplemented\n");
199 return 0;
200}
201
202///////////////////////////////////////////////////////////////////////////////
203
204void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
205 SkASSERT(!"SkFontHost::Serialize unimplemented");
206}
207
208SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
209 int style = stream->readU8();
210 int len = stream->readPackedUInt();
211 const char* name = NULL;
212 if (len > 0) {
213 SkString str;
214 str.resize(len);
215 stream->read(str.writable_str(), len);
216
217 if (str.startsWith("DroidSans")) {
218 name = "sans-serif";
219 } else if (str.startsWith("DroidSerif")) {
220 name = "serif";
221 }
222 SkDebugf("---- deserialize typeface <%s> %d %s\n", str.c_str(), style, name);
223 }
224// name = NULL; style = 0;
225 return SkFontHost::CreateTypeface(NULL, name, NULL, NULL,
226 (SkTypeface::Style)style);
227}
228
229SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
230 return 0;
231}
232
233#define FONT_CACHE_MEMORY_BUDGET 1 * 1024 * 1024
234
235size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) {
236 if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET)
237 return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET;
238 else
239 return 0; // nothing to do
240}
241
242///////////////////////////////////////////////////////////////////////////////
243int SkFontHost::ComputeGammaFlag(const SkPaint& paint) {
244 return 0;
245}
246
247void SkFontHost::GetGammaTables(const uint8_t* tables[2]) {
248 tables[0] = NULL; // black gamma (e.g. exp=1.4)
249 tables[1] = NULL; // white gamma (e.g. exp= 1/1.4)
250}
251
252// static
253SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
254 uint32_t fontID,
255 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
256 SkASSERT(!"SkFontHost::GetAdvancedTypefaceMetrics unimplemented");
257 return NULL;
258}
259
260void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
261}
262
263SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
264 SkASSERT(!"SkFontHost::CreateScalarContext unimplemented");
265 return NULL;
266}