blob: 74fc63da9949b2f949d12f455b08355a7ef4ef1e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkFontHost.h"
djsollen@google.com97145162012-05-31 19:55:08 +000011#include "SkFontDescriptor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkDescriptor.h"
13#include "SkMMapStream.h"
14#include "SkOSFile.h"
15#include "SkPaint.h"
16#include "SkString.h"
17#include "SkStream.h"
18#include "SkThread.h"
19#include "SkTSearch.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#ifndef SK_FONT_FILE_PREFIX
22 #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/msttcorefonts/"
23#endif
24
djsollen@google.com4dc686d2012-02-15 21:03:45 +000025bool find_name_and_attributes(SkStream* stream, SkString* name,
26 SkTypeface::Style* style, bool* isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
28static void GetFullPathForSysFonts(SkString* full, const char name[])
29{
30 full->append(SK_FONT_FILE_PREFIX);
31 full->append(name);
32}
33
34///////////////////////////////////////////////////////////////////////////////
35
36struct FamilyRec;
37
38/* This guy holds a mapping of a name -> family, used for looking up fonts.
39 Since it is stored in a stretchy array that doesn't preserve object
40 semantics, we don't use constructor/destructors, but just have explicit
41 helpers to manage our internal bookkeeping.
42 */
43struct NameFamilyPair {
44 const char* fName; // we own this
45 FamilyRec* fFamily; // we don't own this, we just reference it
46
47 void construct(const char name[], FamilyRec* family)
48 {
49 fName = strdup(name);
50 fFamily = family; // we don't own this, so just record the referene
51 }
52 void destruct()
53 {
54 free((char*)fName);
55 // we don't own family, so just ignore our reference
56 }
57};
58
59// we use atomic_inc to grow this for each typeface we create
60static int32_t gUniqueFontID;
61
62// this is the mutex that protects these globals
digit@google.com1771cbf2012-01-26 21:26:40 +000063SK_DECLARE_STATIC_MUTEX(gFamilyMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064static FamilyRec* gFamilyHead;
65static SkTDArray<NameFamilyPair> gNameList;
66
67struct FamilyRec {
68 FamilyRec* fNext;
69 SkTypeface* fFaces[4];
70
71 FamilyRec()
72 {
73 fNext = gFamilyHead;
74 memset(fFaces, 0, sizeof(fFaces));
75 gFamilyHead = this;
76 }
77};
78
79static SkTypeface* find_best_face(const FamilyRec* family,
reed@android.com1bfd0ca2009-02-20 14:22:36 +000080 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkTypeface* const* faces = family->fFaces;
82
83 if (faces[style] != NULL) { // exact match
84 return faces[style];
85 }
86 // look for a matching bold
87 style = (SkTypeface::Style)(style ^ SkTypeface::kItalic);
88 if (faces[style] != NULL) {
89 return faces[style];
90 }
91 // look for the plain
92 if (faces[SkTypeface::kNormal] != NULL) {
93 return faces[SkTypeface::kNormal];
94 }
95 // look for anything
96 for (int i = 0; i < 4; i++) {
97 if (faces[i] != NULL) {
98 return faces[i];
99 }
100 }
101 // should never get here, since the faces list should not be empty
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000102 SkDEBUGFAIL("faces list is empty");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 return NULL;
104}
105
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000106static FamilyRec* find_family(const SkTypeface* member) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 FamilyRec* curr = gFamilyHead;
108 while (curr != NULL) {
109 for (int i = 0; i < 4; i++) {
110 if (curr->fFaces[i] == member) {
111 return curr;
112 }
113 }
114 curr = curr->fNext;
115 }
116 return NULL;
117}
118
reed@android.comf2afb672009-09-28 16:12:48 +0000119static SkTypeface* find_from_uniqueID(uint32_t uniqueID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 FamilyRec* curr = gFamilyHead;
121 while (curr != NULL) {
122 for (int i = 0; i < 4; i++) {
123 SkTypeface* face = curr->fFaces[i];
124 if (face != NULL && face->uniqueID() == uniqueID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000125 return face;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 }
127 }
128 curr = curr->fNext;
129 }
epoger@google.com17b78942011-08-26 14:40:38 +0000130 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131}
132
133/* Remove reference to this face from its family. If the resulting family
134 is empty (has no faces), return that family, otherwise return NULL
135 */
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000136static FamilyRec* remove_from_family(const SkTypeface* face) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 FamilyRec* family = find_family(face);
138 SkASSERT(family->fFaces[face->style()] == face);
139 family->fFaces[face->style()] = NULL;
140
141 for (int i = 0; i < 4; i++) {
142 if (family->fFaces[i] != NULL) { // family is non-empty
143 return NULL;
144 }
145 }
146 return family; // return the empty family
147}
148
149// maybe we should make FamilyRec be doubly-linked
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000150static void detach_and_delete_family(FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 FamilyRec* curr = gFamilyHead;
152 FamilyRec* prev = NULL;
153
154 while (curr != NULL) {
155 FamilyRec* next = curr->fNext;
156 if (curr == family) {
157 if (prev == NULL) {
158 gFamilyHead = next;
159 } else {
160 prev->fNext = next;
161 }
162 SkDELETE(family);
163 return;
164 }
165 prev = curr;
166 curr = next;
167 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000168 SkDEBUGFAIL("Yikes, couldn't find family in our list to remove/delete");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169}
170
djsollen@google.com97145162012-05-31 19:55:08 +0000171static const char* find_family_name(const SkTypeface* familyMember) {
172 const FamilyRec* familyRec = find_family(familyMember);
173 for (int i = 0; i < gNameList.count(); i++) {
174 if (gNameList[i].fFamily == familyRec) {
175 return gNameList[i].fName;
176 }
177 }
178 return NULL;
179}
180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181static FamilyRec* find_familyrec(const char name[]) {
182 const NameFamilyPair* list = gNameList.begin();
183 int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name,
184 sizeof(list[0]));
185 return index >= 0 ? list[index].fFamily : NULL;
186}
187
188static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) {
189 FamilyRec* rec = find_familyrec(name);
190 return rec ? find_best_face(rec, style) : NULL;
191}
192
193static SkTypeface* find_typeface(const SkTypeface* familyMember,
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000194 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 const FamilyRec* family = find_family(familyMember);
196 return family ? find_best_face(family, style) : NULL;
197}
198
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000199static void add_name(const char name[], FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 SkAutoAsciiToLC tolc(name);
201 name = tolc.lc();
202
203 NameFamilyPair* list = gNameList.begin();
204 int count = gNameList.count();
205
206 int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0]));
207
208 if (index < 0) {
209 list = gNameList.insert(~index);
210 list->construct(name, family);
211 }
212}
213
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000214static void remove_from_names(FamilyRec* emptyFamily) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215#ifdef SK_DEBUG
216 for (int i = 0; i < 4; i++) {
217 SkASSERT(emptyFamily->fFaces[i] == NULL);
218 }
219#endif
220
221 SkTDArray<NameFamilyPair>& list = gNameList;
222
223 // must go backwards when removing
224 for (int i = list.count() - 1; i >= 0; --i) {
225 NameFamilyPair* pair = &list[i];
226 if (pair->fFamily == emptyFamily) {
227 pair->destruct();
228 list.remove(i);
229 }
230 }
231}
232
233///////////////////////////////////////////////////////////////////////////////
234
235class FamilyTypeface : public SkTypeface {
236public:
reed@google.com36812762011-02-23 14:49:33 +0000237 FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth)
238 : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 fIsSysFont = sysFont;
240
241 SkAutoMutexAcquire ac(gFamilyMutex);
242
243 if (NULL == family) {
244 family = SkNEW(FamilyRec);
245 }
246 family->fFaces[style] = this;
247 fFamilyRec = family; // just record it so we can return it if asked
248 }
249
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000250 virtual ~FamilyTypeface() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 SkAutoMutexAcquire ac(gFamilyMutex);
252
253 // remove us from our family. If the family is now empty, we return
254 // that and then remove that family from the name list
255 FamilyRec* family = remove_from_family(this);
256 if (NULL != family) {
257 remove_from_names(family);
258 detach_and_delete_family(family);
259 }
260 }
261
262 bool isSysFont() const { return fIsSysFont; }
263 FamilyRec* getFamily() const { return fFamilyRec; }
reed@android.com22dbaaf2009-05-18 00:43:58 +0000264 // openStream returns a SkStream that has been ref-ed
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 virtual SkStream* openStream() = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 virtual const char* getUniqueString() const = 0;
267
268private:
269 FamilyRec* fFamilyRec; // we don't own this, just point to it
270 bool fIsSysFont;
271
272 typedef SkTypeface INHERITED;
273};
274
275///////////////////////////////////////////////////////////////////////////////
276
reed@android.comf244f1b2010-04-16 12:40:08 +0000277/* This subclass is just a place holder for when we have no fonts available.
278 It exists so that our globals (e.g. gFamilyHead) that expect *something*
279 will not be null.
280 */
281class EmptyTypeface : public FamilyTypeface {
282public:
reed@google.com36812762011-02-23 14:49:33 +0000283 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
reed@android.comf244f1b2010-04-16 12:40:08 +0000284
285 // overrides
286 virtual SkStream* openStream() { return NULL; }
reed@android.comf244f1b2010-04-16 12:40:08 +0000287 virtual const char* getUniqueString() const { return NULL; }
288
289private:
290 typedef FamilyTypeface INHERITED;
291};
292
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293class StreamTypeface : public FamilyTypeface {
294public:
295 StreamTypeface(Style style, bool sysFont, FamilyRec* family,
reed@google.com36812762011-02-23 14:49:33 +0000296 SkStream* stream, bool isFixedWidth)
297 : INHERITED(style, sysFont, family, isFixedWidth) {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000298 stream->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 fStream = stream;
300 }
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000301 virtual ~StreamTypeface() {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000302 fStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 }
304
305 // overrides
reed@android.com22dbaaf2009-05-18 00:43:58 +0000306 virtual SkStream* openStream()
307 {
308 // openStream returns a refed stream.
309 fStream->ref();
310 return fStream;
311 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 virtual const char* getUniqueString() const { return NULL; }
313
314private:
315 SkStream* fStream;
316
317 typedef FamilyTypeface INHERITED;
318};
319
320class FileTypeface : public FamilyTypeface {
321public:
322 FileTypeface(Style style, bool sysFont, FamilyRec* family,
reed@google.com36812762011-02-23 14:49:33 +0000323 const char path[], bool isFixedWidth)
324 : INHERITED(style, sysFont, family, isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 fPath.set(path);
326 }
327
328 // overrides
329 virtual SkStream* openStream()
330 {
331 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str()));
332
333 // check for failure
334 if (stream->getLength() <= 0) {
335 SkDELETE(stream);
336 // maybe MMAP isn't supported. try FILE
337 stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str()));
338 if (stream->getLength() <= 0) {
339 SkDELETE(stream);
340 stream = NULL;
341 }
342 }
343 return stream;
344 }
reed@android.com51709c72010-04-16 12:51:29 +0000345
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 virtual const char* getUniqueString() const {
347 const char* str = strrchr(fPath.c_str(), '/');
348 if (str) {
349 str += 1; // skip the '/'
350 }
351 return str;
352 }
353
354private:
355 SkString fPath;
356
357 typedef FamilyTypeface INHERITED;
358};
359
360///////////////////////////////////////////////////////////////////////////////
361///////////////////////////////////////////////////////////////////////////////
362
363static bool get_name_and_style(const char path[], SkString* name,
reed@google.com36812762011-02-23 14:49:33 +0000364 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 SkMMAPStream stream(path);
366 if (stream.getLength() > 0) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000367 return find_name_and_attributes(&stream, name, style, isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 }
369 else {
370 SkFILEStream stream(path);
371 if (stream.getLength() > 0) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000372 return find_name_and_attributes(&stream, name, style, isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 }
374 }
375
376 SkDebugf("---- failed to open <%s> as a font\n", path);
377 return false;
378}
379
380// these globals are assigned (once) by load_system_fonts()
381static SkTypeface* gFallBackTypeface;
382static FamilyRec* gDefaultFamily;
383static SkTypeface* gDefaultNormal;
384
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000385static void load_system_fonts() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 // check if we've already be called
387 if (NULL != gDefaultNormal) {
reed@google.com6fb8f772011-05-17 15:47:04 +0000388// printf("---- default font %p\n", gDefaultNormal);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 return;
390 }
reed@android.comf2afb672009-09-28 16:12:48 +0000391
reed@android.comf244f1b2010-04-16 12:40:08 +0000392 SkOSFile::Iter iter(SK_FONT_FILE_PREFIX, ".ttf");
393 SkString name;
394 int count = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395
396 while (iter.next(&name, false)) {
397 SkString filename;
398 GetFullPathForSysFonts(&filename, name.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399
reed@google.com36812762011-02-23 14:49:33 +0000400 bool isFixedWidth;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 SkString realname;
reed@google.com6963af22011-01-04 12:52:02 +0000402 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403
reed@google.com36812762011-02-23 14:49:33 +0000404 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
406 continue;
407 }
reed@android.comf2afb672009-09-28 16:12:48 +0000408
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409// SkDebugf("font: <%s> %d <%s>\n", realname.c_str(), style, filename.c_str());
reed@android.comf2afb672009-09-28 16:12:48 +0000410
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 FamilyRec* family = find_familyrec(realname.c_str());
reed@android.com887e4f32010-04-15 14:04:52 +0000412 if (family && family->fFaces[style]) {
413// SkDebugf("---- skipping duplicate typeface %s style %d\n",
414// realname.c_str(), style);
415 continue;
416 }
417
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 // this constructor puts us into the global gFamilyHead llist
419 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
420 (style,
421 true, // system-font (cannot delete)
422 family, // what family to join
reed@google.com36812762011-02-23 14:49:33 +0000423 filename.c_str(),
424 isFixedWidth) // filename
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 );
426
427 if (NULL == family) {
428 add_name(realname.c_str(), tf->getFamily());
429 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000430 count += 1;
431 }
432
433 if (0 == count) {
434 SkNEW(EmptyTypeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 }
reed@android.comf2afb672009-09-28 16:12:48 +0000436
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 // do this after all fonts are loaded. This is our default font, and it
438 // acts as a sentinel so we only execute load_system_fonts() once
439 static const char* gDefaultNames[] = {
440 "Arial", "Verdana", "Times New Roman", NULL
441 };
442 const char** names = gDefaultNames;
443 while (*names) {
444 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
445 if (tf) {
446 gDefaultNormal = tf;
447 break;
448 }
449 }
450 // check if we found *something*
451 if (NULL == gDefaultNormal) {
452 if (NULL == gFamilyHead) {
453 sk_throw();
454 }
455 for (int i = 0; i < 4; i++) {
456 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
457 break;
458 }
459 }
460 }
461 if (NULL == gDefaultNormal) {
462 sk_throw();
463 }
464 gFallBackTypeface = gDefaultNormal;
465 gDefaultFamily = find_family(gDefaultNormal);
466
467// SkDebugf("---- default %p head %p family %p\n", gDefaultNormal, gFamilyHead, gDefaultFamily);
468}
469
470///////////////////////////////////////////////////////////////////////////////
471
472void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
djsollen@google.com97145162012-05-31 19:55:08 +0000473
474 SkFontDescriptor descriptor;
475 descriptor.setFontFamilyName(find_family_name(face));
476 descriptor.setFontStyle(face->style());
477 descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString());
478
479 descriptor.serialize(stream);
480
481 const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont();
482 if (isCustomFont) {
483 // store the entire font in the fontData
484 SkStream* fontStream = ((FamilyTypeface*)face)->openStream();
485 const uint32_t length = fontStream->getLength();
486
487 stream->writePackedUInt(length);
488 stream->writeStream(fontStream, length);
489
490 fontStream->unref();
djsollen@google.come6488ad2012-03-29 16:09:48 +0000491 } else {
djsollen@google.com97145162012-05-31 19:55:08 +0000492 stream->writePackedUInt(0);
djsollen@google.come6488ad2012-03-29 16:09:48 +0000493 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494}
495
496SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
djsollen@google.com97145162012-05-31 19:55:08 +0000497 {
498 SkAutoMutexAcquire ac(gFamilyMutex);
499 load_system_fonts();
djsollen@google.come6488ad2012-03-29 16:09:48 +0000500 }
djsollen@google.com97145162012-05-31 19:55:08 +0000501
502 SkFontDescriptor descriptor(stream);
503 const char* familyName = descriptor.getFontFamilyName();
504 const char* typefaceName = descriptor.getFontFileName();
505 const SkTypeface::Style style = descriptor.getFontStyle();
506
507 const uint32_t customFontDataLength = stream->readPackedUInt();
508 if (customFontDataLength > 0) {
509
510 // generate a new stream to store the custom typeface
511 SkMemoryStream* fontStream = new SkMemoryStream(customFontDataLength - 1);
512 stream->read((void*)fontStream->getMemoryBase(), customFontDataLength - 1);
513
514 SkTypeface* face = CreateTypefaceFromStream(fontStream);
515
516 fontStream->unref();
517 return face;
518 }
519
520 return SkFontHost::CreateTypeface(NULL, familyName, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521}
522
523///////////////////////////////////////////////////////////////////////////////
524
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000525SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
526 const char familyName[],
527 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 load_system_fonts();
529
530 SkAutoMutexAcquire ac(gFamilyMutex);
531
532 // clip to legal style bits
533 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
534
535 SkTypeface* tf = NULL;
536
537 if (NULL != familyFace) {
538 tf = find_typeface(familyFace, style);
539 } else if (NULL != familyName) {
540 // SkDebugf("======= familyName <%s>\n", familyName);
541 tf = find_typeface(familyName, style);
542 }
543
544 if (NULL == tf) {
545 tf = find_best_face(gDefaultFamily, style);
546 }
reed@android.com887e4f32010-04-15 14:04:52 +0000547
548 SkSafeRef(tf);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549 return tf;
550}
551
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000552SkStream* SkFontHost::OpenStream(uint32_t fontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000553 FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554 SkStream* stream = tf ? tf->openStream() : NULL;
555
reed@android.com1c0c5a02010-04-12 20:16:49 +0000556 if (stream && stream->getLength() == 0) {
557 stream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 stream = NULL;
559 }
560 return stream;
561}
562
reed@android.comac981542009-07-31 16:17:01 +0000563size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
564 int32_t* index) {
565 SkDebugf("SkFontHost::GetFileName unimplemented\n");
566 return 0;
567}
568
reed@google.com7d26c592011-06-13 13:01:10 +0000569SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000570 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571}
572
573///////////////////////////////////////////////////////////////////////////////
574
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000575SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576 if (NULL == stream || stream->getLength() <= 0) {
577 SkDELETE(stream);
578 return NULL;
579 }
reed@google.com36812762011-02-23 14:49:33 +0000580
581 bool isFixedWidth;
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000582 SkTypeface::Style style;
583 if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
584 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth));
585 } else {
586 return NULL;
587 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588}
589
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000590SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
591 SkTypeface* face = NULL;
592 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
593
594 if (stream->isValid()) {
reed@android.comf2afb672009-09-28 16:12:48 +0000595 face = CreateTypefaceFromStream(stream);
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000596 }
597 stream->unref();
reed@android.comf2afb672009-09-28 16:12:48 +0000598 return face;
reed@android.com0becfc5b2009-01-13 13:26:44 +0000599}
600