blob: 0b6f88efa8824baded96b7bf3515bd3395b3275a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/ports/SkFontHost_android.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18#include "SkFontHost.h"
19#include "SkDescriptor.h"
20#include "SkMMapStream.h"
21#include "SkOSFile.h"
22#include "SkPaint.h"
23#include "SkString.h"
24#include "SkStream.h"
25#include "SkThread.h"
26#include "SkTSearch.h"
27#include <stdio.h>
28
29#define FONT_CACHE_MEMORY_BUDGET (1 * 1024 * 1024)
30
31#ifndef SK_FONT_FILE_PREFIX
32 #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/msttcorefonts/"
33#endif
34
35SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name);
36
37static void GetFullPathForSysFonts(SkString* full, const char name[])
38{
39 full->append(SK_FONT_FILE_PREFIX);
40 full->append(name);
41}
42
43///////////////////////////////////////////////////////////////////////////////
44
45struct FamilyRec;
46
47/* This guy holds a mapping of a name -> family, used for looking up fonts.
48 Since it is stored in a stretchy array that doesn't preserve object
49 semantics, we don't use constructor/destructors, but just have explicit
50 helpers to manage our internal bookkeeping.
51 */
52struct NameFamilyPair {
53 const char* fName; // we own this
54 FamilyRec* fFamily; // we don't own this, we just reference it
55
56 void construct(const char name[], FamilyRec* family)
57 {
58 fName = strdup(name);
59 fFamily = family; // we don't own this, so just record the referene
60 }
61 void destruct()
62 {
63 free((char*)fName);
64 // we don't own family, so just ignore our reference
65 }
66};
67
68// we use atomic_inc to grow this for each typeface we create
69static int32_t gUniqueFontID;
70
71// this is the mutex that protects these globals
72static SkMutex gFamilyMutex;
73static FamilyRec* gFamilyHead;
74static SkTDArray<NameFamilyPair> gNameList;
75
76struct FamilyRec {
77 FamilyRec* fNext;
78 SkTypeface* fFaces[4];
79
80 FamilyRec()
81 {
82 fNext = gFamilyHead;
83 memset(fFaces, 0, sizeof(fFaces));
84 gFamilyHead = this;
85 }
86};
87
88static SkTypeface* find_best_face(const FamilyRec* family,
reed@android.com1bfd0ca2009-02-20 14:22:36 +000089 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 SkTypeface* const* faces = family->fFaces;
91
92 if (faces[style] != NULL) { // exact match
93 return faces[style];
94 }
95 // look for a matching bold
96 style = (SkTypeface::Style)(style ^ SkTypeface::kItalic);
97 if (faces[style] != NULL) {
98 return faces[style];
99 }
100 // look for the plain
101 if (faces[SkTypeface::kNormal] != NULL) {
102 return faces[SkTypeface::kNormal];
103 }
104 // look for anything
105 for (int i = 0; i < 4; i++) {
106 if (faces[i] != NULL) {
107 return faces[i];
108 }
109 }
110 // should never get here, since the faces list should not be empty
111 SkASSERT(!"faces list is empty");
112 return NULL;
113}
114
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000115static FamilyRec* find_family(const SkTypeface* member) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 FamilyRec* curr = gFamilyHead;
117 while (curr != NULL) {
118 for (int i = 0; i < 4; i++) {
119 if (curr->fFaces[i] == member) {
120 return curr;
121 }
122 }
123 curr = curr->fNext;
124 }
125 return NULL;
126}
127
reed@android.comf2afb672009-09-28 16:12:48 +0000128static SkTypeface* find_from_uniqueID(uint32_t uniqueID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 FamilyRec* curr = gFamilyHead;
130 while (curr != NULL) {
131 for (int i = 0; i < 4; i++) {
132 SkTypeface* face = curr->fFaces[i];
133 if (face != NULL && face->uniqueID() == uniqueID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000134 return face;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 }
136 }
137 curr = curr->fNext;
138 }
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000139 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140}
141
reed@android.comf2afb672009-09-28 16:12:48 +0000142static bool valid_uniqueID(uint32_t uniqueID) {
143 return find_from_uniqueID(uniqueID) != NULL;
144}
145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146/* Remove reference to this face from its family. If the resulting family
147 is empty (has no faces), return that family, otherwise return NULL
148 */
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000149static FamilyRec* remove_from_family(const SkTypeface* face) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 FamilyRec* family = find_family(face);
151 SkASSERT(family->fFaces[face->style()] == face);
152 family->fFaces[face->style()] = NULL;
153
154 for (int i = 0; i < 4; i++) {
155 if (family->fFaces[i] != NULL) { // family is non-empty
156 return NULL;
157 }
158 }
159 return family; // return the empty family
160}
161
162// maybe we should make FamilyRec be doubly-linked
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000163static void detach_and_delete_family(FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 FamilyRec* curr = gFamilyHead;
165 FamilyRec* prev = NULL;
166
167 while (curr != NULL) {
168 FamilyRec* next = curr->fNext;
169 if (curr == family) {
170 if (prev == NULL) {
171 gFamilyHead = next;
172 } else {
173 prev->fNext = next;
174 }
175 SkDELETE(family);
176 return;
177 }
178 prev = curr;
179 curr = next;
180 }
181 SkASSERT(!"Yikes, couldn't find family in our list to remove/delete");
182}
183
184static FamilyRec* find_familyrec(const char name[]) {
185 const NameFamilyPair* list = gNameList.begin();
186 int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name,
187 sizeof(list[0]));
188 return index >= 0 ? list[index].fFamily : NULL;
189}
190
191static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) {
192 FamilyRec* rec = find_familyrec(name);
193 return rec ? find_best_face(rec, style) : NULL;
194}
195
196static SkTypeface* find_typeface(const SkTypeface* familyMember,
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000197 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 const FamilyRec* family = find_family(familyMember);
199 return family ? find_best_face(family, style) : NULL;
200}
201
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000202static void add_name(const char name[], FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 SkAutoAsciiToLC tolc(name);
204 name = tolc.lc();
205
206 NameFamilyPair* list = gNameList.begin();
207 int count = gNameList.count();
208
209 int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0]));
210
211 if (index < 0) {
212 list = gNameList.insert(~index);
213 list->construct(name, family);
214 }
215}
216
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000217static void remove_from_names(FamilyRec* emptyFamily) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218#ifdef SK_DEBUG
219 for (int i = 0; i < 4; i++) {
220 SkASSERT(emptyFamily->fFaces[i] == NULL);
221 }
222#endif
223
224 SkTDArray<NameFamilyPair>& list = gNameList;
225
226 // must go backwards when removing
227 for (int i = list.count() - 1; i >= 0; --i) {
228 NameFamilyPair* pair = &list[i];
229 if (pair->fFamily == emptyFamily) {
230 pair->destruct();
231 list.remove(i);
232 }
233 }
234}
235
236///////////////////////////////////////////////////////////////////////////////
237
238class FamilyTypeface : public SkTypeface {
239public:
240 FamilyTypeface(Style style, bool sysFont, FamilyRec* family)
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000241 : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 fIsSysFont = sysFont;
243
244 SkAutoMutexAcquire ac(gFamilyMutex);
245
246 if (NULL == family) {
247 family = SkNEW(FamilyRec);
248 }
249 family->fFaces[style] = this;
250 fFamilyRec = family; // just record it so we can return it if asked
251 }
252
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000253 virtual ~FamilyTypeface() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 SkAutoMutexAcquire ac(gFamilyMutex);
255
256 // remove us from our family. If the family is now empty, we return
257 // that and then remove that family from the name list
258 FamilyRec* family = remove_from_family(this);
259 if (NULL != family) {
260 remove_from_names(family);
261 detach_and_delete_family(family);
262 }
263 }
264
265 bool isSysFont() const { return fIsSysFont; }
266 FamilyRec* getFamily() const { return fFamilyRec; }
reed@android.com22dbaaf2009-05-18 00:43:58 +0000267 // openStream returns a SkStream that has been ref-ed
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 virtual SkStream* openStream() = 0;
269 virtual void closeStream(SkStream*) = 0;
270 virtual const char* getUniqueString() const = 0;
271
272private:
273 FamilyRec* fFamilyRec; // we don't own this, just point to it
274 bool fIsSysFont;
275
276 typedef SkTypeface INHERITED;
277};
278
279///////////////////////////////////////////////////////////////////////////////
280
reed@android.comf244f1b2010-04-16 12:40:08 +0000281/* This subclass is just a place holder for when we have no fonts available.
282 It exists so that our globals (e.g. gFamilyHead) that expect *something*
283 will not be null.
284 */
285class EmptyTypeface : public FamilyTypeface {
286public:
287 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL) {}
288
289 // overrides
290 virtual SkStream* openStream() { return NULL; }
291 virtual void closeStream(SkStream*) {}
292 virtual const char* getUniqueString() const { return NULL; }
293
294private:
295 typedef FamilyTypeface INHERITED;
296};
297
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298class StreamTypeface : public FamilyTypeface {
299public:
300 StreamTypeface(Style style, bool sysFont, FamilyRec* family,
301 SkStream* stream)
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000302 : INHERITED(style, sysFont, family) {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000303 stream->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 fStream = stream;
305 }
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000306 virtual ~StreamTypeface() {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000307 fStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 }
309
310 // overrides
reed@android.com22dbaaf2009-05-18 00:43:58 +0000311 virtual SkStream* openStream()
312 {
313 // openStream returns a refed stream.
314 fStream->ref();
315 return fStream;
316 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 virtual void closeStream(SkStream*) {}
318 virtual const char* getUniqueString() const { return NULL; }
319
320private:
321 SkStream* fStream;
322
323 typedef FamilyTypeface INHERITED;
324};
325
326class FileTypeface : public FamilyTypeface {
327public:
328 FileTypeface(Style style, bool sysFont, FamilyRec* family,
329 const char path[])
330 : INHERITED(style, sysFont, family) {
331 fPath.set(path);
332 }
333
334 // overrides
335 virtual SkStream* openStream()
336 {
337 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str()));
338
339 // check for failure
340 if (stream->getLength() <= 0) {
341 SkDELETE(stream);
342 // maybe MMAP isn't supported. try FILE
343 stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str()));
344 if (stream->getLength() <= 0) {
345 SkDELETE(stream);
346 stream = NULL;
347 }
348 }
349 return stream;
350 }
351 virtual void closeStream(SkStream* stream)
352 {
353 SkDELETE(stream);
354 }
355 virtual const char* getUniqueString() const {
356 const char* str = strrchr(fPath.c_str(), '/');
357 if (str) {
358 str += 1; // skip the '/'
359 }
360 return str;
361 }
362
363private:
364 SkString fPath;
365
366 typedef FamilyTypeface INHERITED;
367};
368
369///////////////////////////////////////////////////////////////////////////////
370///////////////////////////////////////////////////////////////////////////////
371
372static bool get_name_and_style(const char path[], SkString* name,
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000373 SkTypeface::Style* style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 SkMMAPStream stream(path);
375 if (stream.getLength() > 0) {
376 *style = find_name_and_style(&stream, name);
377 return true;
378 }
379 else {
380 SkFILEStream stream(path);
381 if (stream.getLength() > 0) {
382 *style = find_name_and_style(&stream, name);
383 return true;
384 }
385 }
386
387 SkDebugf("---- failed to open <%s> as a font\n", path);
388 return false;
389}
390
391// these globals are assigned (once) by load_system_fonts()
392static SkTypeface* gFallBackTypeface;
393static FamilyRec* gDefaultFamily;
394static SkTypeface* gDefaultNormal;
395
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000396static void load_system_fonts() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 // check if we've already be called
398 if (NULL != gDefaultNormal) {
reed@android.comf2afb672009-09-28 16:12:48 +0000399 printf("---- default font %p\n", gDefaultNormal);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 return;
401 }
reed@android.comf2afb672009-09-28 16:12:48 +0000402
reed@android.comf244f1b2010-04-16 12:40:08 +0000403 SkOSFile::Iter iter(SK_FONT_FILE_PREFIX, ".ttf");
404 SkString name;
405 int count = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406
407 while (iter.next(&name, false)) {
408 SkString filename;
409 GetFullPathForSysFonts(&filename, name.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410
411 SkString realname;
412 SkTypeface::Style style;
413
414 if (!get_name_and_style(filename.c_str(), &realname, &style)) {
415 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
416 continue;
417 }
reed@android.comf2afb672009-09-28 16:12:48 +0000418
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419// SkDebugf("font: <%s> %d <%s>\n", realname.c_str(), style, filename.c_str());
reed@android.comf2afb672009-09-28 16:12:48 +0000420
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421 FamilyRec* family = find_familyrec(realname.c_str());
reed@android.com887e4f32010-04-15 14:04:52 +0000422 if (family && family->fFaces[style]) {
423// SkDebugf("---- skipping duplicate typeface %s style %d\n",
424// realname.c_str(), style);
425 continue;
426 }
427
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 // this constructor puts us into the global gFamilyHead llist
429 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
430 (style,
431 true, // system-font (cannot delete)
432 family, // what family to join
433 filename.c_str()) // filename
434 );
435
436 if (NULL == family) {
437 add_name(realname.c_str(), tf->getFamily());
438 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000439 count += 1;
440 }
441
442 if (0 == count) {
443 SkNEW(EmptyTypeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 }
reed@android.comf2afb672009-09-28 16:12:48 +0000445
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 // do this after all fonts are loaded. This is our default font, and it
447 // acts as a sentinel so we only execute load_system_fonts() once
448 static const char* gDefaultNames[] = {
449 "Arial", "Verdana", "Times New Roman", NULL
450 };
451 const char** names = gDefaultNames;
452 while (*names) {
453 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
454 if (tf) {
455 gDefaultNormal = tf;
456 break;
457 }
458 }
459 // check if we found *something*
460 if (NULL == gDefaultNormal) {
461 if (NULL == gFamilyHead) {
462 sk_throw();
463 }
464 for (int i = 0; i < 4; i++) {
465 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
466 break;
467 }
468 }
469 }
470 if (NULL == gDefaultNormal) {
471 sk_throw();
472 }
473 gFallBackTypeface = gDefaultNormal;
474 gDefaultFamily = find_family(gDefaultNormal);
475
476// SkDebugf("---- default %p head %p family %p\n", gDefaultNormal, gFamilyHead, gDefaultFamily);
477}
478
479///////////////////////////////////////////////////////////////////////////////
480
481void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
482#if 0
483 const char* name = ((FamilyTypeface*)face)->getUniqueString();
484
485 stream->write8((uint8_t)face->getStyle());
486
487 if (NULL == name || 0 == *name) {
488 stream->writePackedUInt(0);
489 // SkDebugf("--- fonthost serialize null\n");
490 } else {
491 uint32_t len = strlen(name);
492 stream->writePackedUInt(len);
493 stream->write(name, len);
494 // SkDebugf("--- fonthost serialize <%s> %d\n", name, face->getStyle());
495 }
496#endif
497 sk_throw();
498}
499
500SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
501#if 0
502 load_system_fonts();
503
504 int style = stream->readU8();
505
506 int len = stream->readPackedUInt();
507 if (len > 0) {
508 SkString str;
509 str.resize(len);
510 stream->read(str.writable_str(), len);
511
512 const FontInitRec* rec = gSystemFonts;
513 for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) {
514 if (strcmp(rec[i].fFileName, str.c_str()) == 0) {
515 // backup until we hit the fNames
516 for (int j = i; j >= 0; --j) {
517 if (rec[j].fNames != NULL) {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000518 return SkFontHost::CreateTypeface(NULL, rec[j].fNames[0],
519 (SkTypeface::Style)style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 }
521 }
522 }
523 }
524 }
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000525 return SkFontHost::CreateTypeface(NULL, NULL, (SkTypeface::Style)style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526#endif
527 sk_throw();
reed@android.com6f252972009-01-14 16:46:16 +0000528 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529}
530
531///////////////////////////////////////////////////////////////////////////////
532
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000533SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
534 const char familyName[],
535 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 load_system_fonts();
537
538 SkAutoMutexAcquire ac(gFamilyMutex);
539
540 // clip to legal style bits
541 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
542
543 SkTypeface* tf = NULL;
544
545 if (NULL != familyFace) {
546 tf = find_typeface(familyFace, style);
547 } else if (NULL != familyName) {
548 // SkDebugf("======= familyName <%s>\n", familyName);
549 tf = find_typeface(familyName, style);
550 }
551
552 if (NULL == tf) {
553 tf = find_best_face(gDefaultFamily, style);
554 }
reed@android.com887e4f32010-04-15 14:04:52 +0000555
556 SkSafeRef(tf);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 return tf;
558}
559
reed@android.comf2afb672009-09-28 16:12:48 +0000560bool SkFontHost::ValidFontID(uint32_t fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000561 SkAutoMutexAcquire ac(gFamilyMutex);
562
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000563 return valid_uniqueID(fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564}
565
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000566SkStream* SkFontHost::OpenStream(uint32_t fontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000567 FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 SkStream* stream = tf ? tf->openStream() : NULL;
569
reed@android.com1c0c5a02010-04-12 20:16:49 +0000570 if (stream && stream->getLength() == 0) {
571 stream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572 stream = NULL;
573 }
574 return stream;
575}
576
reed@android.comac981542009-07-31 16:17:01 +0000577size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
578 int32_t* index) {
579 SkDebugf("SkFontHost::GetFileName unimplemented\n");
580 return 0;
581}
582
reed@android.comf2afb672009-09-28 16:12:48 +0000583uint32_t SkFontHost::NextLogicalFont(uint32_t fontID) {
584 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585}
586
587///////////////////////////////////////////////////////////////////////////////
588
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000589SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000590 if (NULL == stream || stream->getLength() <= 0) {
591 SkDELETE(stream);
592 return NULL;
593 }
594
595 SkString name;
596 SkTypeface::Style style = find_name_and_style(stream, &name);
597
598 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream));
599}
600
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000601SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
602 SkTypeface* face = NULL;
603 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
604
605 if (stream->isValid()) {
reed@android.comf2afb672009-09-28 16:12:48 +0000606 face = CreateTypefaceFromStream(stream);
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000607 }
608 stream->unref();
reed@android.comf2afb672009-09-28 16:12:48 +0000609 return face;
reed@android.com0becfc5b2009-01-13 13:26:44 +0000610}
611
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612///////////////////////////////////////////////////////////////////////////////
613
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000614size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615 if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET)
616 return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET;
617 else
618 return 0; // nothing to do
619}
620