blob: 805dc2202c3bce17274d83732de0f93cc7bbcf93 [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"
11#include "SkDescriptor.h"
12#include "SkMMapStream.h"
13#include "SkOSFile.h"
14#include "SkPaint.h"
15#include "SkString.h"
16#include "SkStream.h"
17#include "SkThread.h"
18#include "SkTSearch.h"
19#include <stdio.h>
20
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
reed@android.comf2afb672009-09-28 16:12:48 +0000133static bool valid_uniqueID(uint32_t uniqueID) {
134 return find_from_uniqueID(uniqueID) != NULL;
135}
136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137/* Remove reference to this face from its family. If the resulting family
138 is empty (has no faces), return that family, otherwise return NULL
139 */
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000140static FamilyRec* remove_from_family(const SkTypeface* face) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 FamilyRec* family = find_family(face);
142 SkASSERT(family->fFaces[face->style()] == face);
143 family->fFaces[face->style()] = NULL;
144
145 for (int i = 0; i < 4; i++) {
146 if (family->fFaces[i] != NULL) { // family is non-empty
147 return NULL;
148 }
149 }
150 return family; // return the empty family
151}
152
153// maybe we should make FamilyRec be doubly-linked
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000154static void detach_and_delete_family(FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 FamilyRec* curr = gFamilyHead;
156 FamilyRec* prev = NULL;
157
158 while (curr != NULL) {
159 FamilyRec* next = curr->fNext;
160 if (curr == family) {
161 if (prev == NULL) {
162 gFamilyHead = next;
163 } else {
164 prev->fNext = next;
165 }
166 SkDELETE(family);
167 return;
168 }
169 prev = curr;
170 curr = next;
171 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000172 SkDEBUGFAIL("Yikes, couldn't find family in our list to remove/delete");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173}
174
175static FamilyRec* find_familyrec(const char name[]) {
176 const NameFamilyPair* list = gNameList.begin();
177 int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name,
178 sizeof(list[0]));
179 return index >= 0 ? list[index].fFamily : NULL;
180}
181
182static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) {
183 FamilyRec* rec = find_familyrec(name);
184 return rec ? find_best_face(rec, style) : NULL;
185}
186
187static SkTypeface* find_typeface(const SkTypeface* familyMember,
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000188 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 const FamilyRec* family = find_family(familyMember);
190 return family ? find_best_face(family, style) : NULL;
191}
192
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000193static void add_name(const char name[], FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 SkAutoAsciiToLC tolc(name);
195 name = tolc.lc();
196
197 NameFamilyPair* list = gNameList.begin();
198 int count = gNameList.count();
199
200 int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0]));
201
202 if (index < 0) {
203 list = gNameList.insert(~index);
204 list->construct(name, family);
205 }
206}
207
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000208static void remove_from_names(FamilyRec* emptyFamily) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209#ifdef SK_DEBUG
210 for (int i = 0; i < 4; i++) {
211 SkASSERT(emptyFamily->fFaces[i] == NULL);
212 }
213#endif
214
215 SkTDArray<NameFamilyPair>& list = gNameList;
216
217 // must go backwards when removing
218 for (int i = list.count() - 1; i >= 0; --i) {
219 NameFamilyPair* pair = &list[i];
220 if (pair->fFamily == emptyFamily) {
221 pair->destruct();
222 list.remove(i);
223 }
224 }
225}
226
227///////////////////////////////////////////////////////////////////////////////
228
229class FamilyTypeface : public SkTypeface {
230public:
reed@google.com36812762011-02-23 14:49:33 +0000231 FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth)
232 : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 fIsSysFont = sysFont;
234
235 SkAutoMutexAcquire ac(gFamilyMutex);
236
237 if (NULL == family) {
238 family = SkNEW(FamilyRec);
239 }
240 family->fFaces[style] = this;
241 fFamilyRec = family; // just record it so we can return it if asked
242 }
243
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000244 virtual ~FamilyTypeface() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 SkAutoMutexAcquire ac(gFamilyMutex);
246
247 // remove us from our family. If the family is now empty, we return
248 // that and then remove that family from the name list
249 FamilyRec* family = remove_from_family(this);
250 if (NULL != family) {
251 remove_from_names(family);
252 detach_and_delete_family(family);
253 }
254 }
255
256 bool isSysFont() const { return fIsSysFont; }
257 FamilyRec* getFamily() const { return fFamilyRec; }
reed@android.com22dbaaf2009-05-18 00:43:58 +0000258 // openStream returns a SkStream that has been ref-ed
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 virtual SkStream* openStream() = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 virtual const char* getUniqueString() const = 0;
261
262private:
263 FamilyRec* fFamilyRec; // we don't own this, just point to it
264 bool fIsSysFont;
265
266 typedef SkTypeface INHERITED;
267};
268
269///////////////////////////////////////////////////////////////////////////////
270
reed@android.comf244f1b2010-04-16 12:40:08 +0000271/* This subclass is just a place holder for when we have no fonts available.
272 It exists so that our globals (e.g. gFamilyHead) that expect *something*
273 will not be null.
274 */
275class EmptyTypeface : public FamilyTypeface {
276public:
reed@google.com36812762011-02-23 14:49:33 +0000277 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
reed@android.comf244f1b2010-04-16 12:40:08 +0000278
279 // overrides
280 virtual SkStream* openStream() { return NULL; }
reed@android.comf244f1b2010-04-16 12:40:08 +0000281 virtual const char* getUniqueString() const { return NULL; }
282
283private:
284 typedef FamilyTypeface INHERITED;
285};
286
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287class StreamTypeface : public FamilyTypeface {
288public:
289 StreamTypeface(Style style, bool sysFont, FamilyRec* family,
reed@google.com36812762011-02-23 14:49:33 +0000290 SkStream* stream, bool isFixedWidth)
291 : INHERITED(style, sysFont, family, isFixedWidth) {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000292 stream->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 fStream = stream;
294 }
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000295 virtual ~StreamTypeface() {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000296 fStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 }
298
299 // overrides
reed@android.com22dbaaf2009-05-18 00:43:58 +0000300 virtual SkStream* openStream()
301 {
302 // openStream returns a refed stream.
303 fStream->ref();
304 return fStream;
305 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 virtual const char* getUniqueString() const { return NULL; }
307
308private:
309 SkStream* fStream;
310
311 typedef FamilyTypeface INHERITED;
312};
313
314class FileTypeface : public FamilyTypeface {
315public:
316 FileTypeface(Style style, bool sysFont, FamilyRec* family,
reed@google.com36812762011-02-23 14:49:33 +0000317 const char path[], bool isFixedWidth)
318 : INHERITED(style, sysFont, family, isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 fPath.set(path);
320 }
321
322 // overrides
323 virtual SkStream* openStream()
324 {
325 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str()));
326
327 // check for failure
328 if (stream->getLength() <= 0) {
329 SkDELETE(stream);
330 // maybe MMAP isn't supported. try FILE
331 stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str()));
332 if (stream->getLength() <= 0) {
333 SkDELETE(stream);
334 stream = NULL;
335 }
336 }
337 return stream;
338 }
reed@android.com51709c72010-04-16 12:51:29 +0000339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 virtual const char* getUniqueString() const {
341 const char* str = strrchr(fPath.c_str(), '/');
342 if (str) {
343 str += 1; // skip the '/'
344 }
345 return str;
346 }
347
348private:
349 SkString fPath;
350
351 typedef FamilyTypeface INHERITED;
352};
353
354///////////////////////////////////////////////////////////////////////////////
355///////////////////////////////////////////////////////////////////////////////
356
357static bool get_name_and_style(const char path[], SkString* name,
reed@google.com36812762011-02-23 14:49:33 +0000358 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 SkMMAPStream stream(path);
360 if (stream.getLength() > 0) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000361 return find_name_and_attributes(&stream, name, style, isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 }
363 else {
364 SkFILEStream stream(path);
365 if (stream.getLength() > 0) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000366 return find_name_and_attributes(&stream, name, style, isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 }
368 }
369
370 SkDebugf("---- failed to open <%s> as a font\n", path);
371 return false;
372}
373
374// these globals are assigned (once) by load_system_fonts()
375static SkTypeface* gFallBackTypeface;
376static FamilyRec* gDefaultFamily;
377static SkTypeface* gDefaultNormal;
378
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000379static void load_system_fonts() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 // check if we've already be called
381 if (NULL != gDefaultNormal) {
reed@google.com6fb8f772011-05-17 15:47:04 +0000382// printf("---- default font %p\n", gDefaultNormal);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 return;
384 }
reed@android.comf2afb672009-09-28 16:12:48 +0000385
reed@android.comf244f1b2010-04-16 12:40:08 +0000386 SkOSFile::Iter iter(SK_FONT_FILE_PREFIX, ".ttf");
387 SkString name;
388 int count = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389
390 while (iter.next(&name, false)) {
391 SkString filename;
392 GetFullPathForSysFonts(&filename, name.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393
reed@google.com36812762011-02-23 14:49:33 +0000394 bool isFixedWidth;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395 SkString realname;
reed@google.com6963af22011-01-04 12:52:02 +0000396 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397
reed@google.com36812762011-02-23 14:49:33 +0000398 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
400 continue;
401 }
reed@android.comf2afb672009-09-28 16:12:48 +0000402
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403// SkDebugf("font: <%s> %d <%s>\n", realname.c_str(), style, filename.c_str());
reed@android.comf2afb672009-09-28 16:12:48 +0000404
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405 FamilyRec* family = find_familyrec(realname.c_str());
reed@android.com887e4f32010-04-15 14:04:52 +0000406 if (family && family->fFaces[style]) {
407// SkDebugf("---- skipping duplicate typeface %s style %d\n",
408// realname.c_str(), style);
409 continue;
410 }
411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 // this constructor puts us into the global gFamilyHead llist
413 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
414 (style,
415 true, // system-font (cannot delete)
416 family, // what family to join
reed@google.com36812762011-02-23 14:49:33 +0000417 filename.c_str(),
418 isFixedWidth) // filename
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 );
420
421 if (NULL == family) {
422 add_name(realname.c_str(), tf->getFamily());
423 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000424 count += 1;
425 }
426
427 if (0 == count) {
428 SkNEW(EmptyTypeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 }
reed@android.comf2afb672009-09-28 16:12:48 +0000430
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431 // do this after all fonts are loaded. This is our default font, and it
432 // acts as a sentinel so we only execute load_system_fonts() once
433 static const char* gDefaultNames[] = {
434 "Arial", "Verdana", "Times New Roman", NULL
435 };
436 const char** names = gDefaultNames;
437 while (*names) {
438 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
439 if (tf) {
440 gDefaultNormal = tf;
441 break;
442 }
443 }
444 // check if we found *something*
445 if (NULL == gDefaultNormal) {
446 if (NULL == gFamilyHead) {
447 sk_throw();
448 }
449 for (int i = 0; i < 4; i++) {
450 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
451 break;
452 }
453 }
454 }
455 if (NULL == gDefaultNormal) {
456 sk_throw();
457 }
458 gFallBackTypeface = gDefaultNormal;
459 gDefaultFamily = find_family(gDefaultNormal);
460
461// SkDebugf("---- default %p head %p family %p\n", gDefaultNormal, gFamilyHead, gDefaultFamily);
462}
463
464///////////////////////////////////////////////////////////////////////////////
465
466void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
djsollen@google.come6488ad2012-03-29 16:09:48 +0000467#if 0
468 const char* name = ((FamilyTypeface*)face)->getUniqueString();
469
470 stream->write8((uint8_t)face->getStyle());
471
472 if (NULL == name || 0 == *name) {
473 stream->writePackedUInt(0);
474 // SkDebugf("--- fonthost serialize null\n");
475 } else {
476 uint32_t len = strlen(name);
477 stream->writePackedUInt(len);
478 stream->write(name, len);
479 // SkDebugf("--- fonthost serialize <%s> %d\n", name, face->getStyle());
480 }
481#endif
482 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483}
484
485SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
djsollen@google.come6488ad2012-03-29 16:09:48 +0000486#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000487 load_system_fonts();
djsollen@google.come6488ad2012-03-29 16:09:48 +0000488
489 int style = stream->readU8();
490
491 int len = stream->readPackedUInt();
492 if (len > 0) {
493 SkString str;
494 str.resize(len);
495 stream->read(str.writable_str(), len);
496
497 const FontInitRec* rec = gSystemFonts;
498 for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) {
499 if (strcmp(rec[i].fFileName, str.c_str()) == 0) {
500 // backup until we hit the fNames
501 for (int j = i; j >= 0; --j) {
502 if (rec[j].fNames != NULL) {
reed@google.com0389d932012-05-07 21:06:55 +0000503 return SkFontHost::CreateTypeface(NULL, rec[j].fNames[0],
djsollen@google.come6488ad2012-03-29 16:09:48 +0000504 (SkTypeface::Style)style);
505 }
506 }
507 }
508 }
509 }
reed@google.com0389d932012-05-07 21:06:55 +0000510 return SkFontHost::CreateTypeface(NULL, NULL, (SkTypeface::Style)style);
djsollen@google.come6488ad2012-03-29 16:09:48 +0000511#endif
512 sk_throw();
513 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514}
515
516///////////////////////////////////////////////////////////////////////////////
517
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000518SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
519 const char familyName[],
520 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 load_system_fonts();
522
523 SkAutoMutexAcquire ac(gFamilyMutex);
524
525 // clip to legal style bits
526 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
527
528 SkTypeface* tf = NULL;
529
530 if (NULL != familyFace) {
531 tf = find_typeface(familyFace, style);
532 } else if (NULL != familyName) {
533 // SkDebugf("======= familyName <%s>\n", familyName);
534 tf = find_typeface(familyName, style);
535 }
536
537 if (NULL == tf) {
538 tf = find_best_face(gDefaultFamily, style);
539 }
reed@android.com887e4f32010-04-15 14:04:52 +0000540
541 SkSafeRef(tf);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 return tf;
543}
544
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000545SkStream* SkFontHost::OpenStream(uint32_t fontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000546 FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000547 SkStream* stream = tf ? tf->openStream() : NULL;
548
reed@android.com1c0c5a02010-04-12 20:16:49 +0000549 if (stream && stream->getLength() == 0) {
550 stream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551 stream = NULL;
552 }
553 return stream;
554}
555
reed@android.comac981542009-07-31 16:17:01 +0000556size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
557 int32_t* index) {
558 SkDebugf("SkFontHost::GetFileName unimplemented\n");
559 return 0;
560}
561
reed@google.com7d26c592011-06-13 13:01:10 +0000562SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000563 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564}
565
566///////////////////////////////////////////////////////////////////////////////
567
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000568SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 if (NULL == stream || stream->getLength() <= 0) {
570 SkDELETE(stream);
571 return NULL;
572 }
reed@google.com36812762011-02-23 14:49:33 +0000573
574 bool isFixedWidth;
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000575 SkTypeface::Style style;
576 if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
577 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth));
578 } else {
579 return NULL;
580 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581}
582
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000583SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
584 SkTypeface* face = NULL;
585 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
586
587 if (stream->isValid()) {
reed@android.comf2afb672009-09-28 16:12:48 +0000588 face = CreateTypefaceFromStream(stream);
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000589 }
590 stream->unref();
reed@android.comf2afb672009-09-28 16:12:48 +0000591 return face;
reed@android.com0becfc5b2009-01-13 13:26:44 +0000592}
593