blob: cd21fc2c218d0f1f1ac7deb1f90d0d7c658ec98f [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
bungeman@google.com2cf84ec2012-09-26 19:16:54 +000022 #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/"
23#endif
24#ifndef SK_FONT_FILE_DIR_SEPERATOR
25 #define SK_FONT_FILE_DIR_SEPERATOR "/"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#endif
27
djsollen@google.com4dc686d2012-02-15 21:03:45 +000028bool find_name_and_attributes(SkStream* stream, SkString* name,
29 SkTypeface::Style* style, bool* isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031///////////////////////////////////////////////////////////////////////////////
32
33struct FamilyRec;
34
35/* This guy holds a mapping of a name -> family, used for looking up fonts.
36 Since it is stored in a stretchy array that doesn't preserve object
37 semantics, we don't use constructor/destructors, but just have explicit
38 helpers to manage our internal bookkeeping.
39 */
40struct NameFamilyPair {
41 const char* fName; // we own this
42 FamilyRec* fFamily; // we don't own this, we just reference it
chudy@google.comada44802012-07-30 12:59:12 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 void construct(const char name[], FamilyRec* family)
45 {
46 fName = strdup(name);
47 fFamily = family; // we don't own this, so just record the referene
48 }
49 void destruct()
50 {
51 free((char*)fName);
52 // we don't own family, so just ignore our reference
53 }
54};
55
56// we use atomic_inc to grow this for each typeface we create
57static int32_t gUniqueFontID;
58
59// this is the mutex that protects these globals
digit@google.com1771cbf2012-01-26 21:26:40 +000060SK_DECLARE_STATIC_MUTEX(gFamilyMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000061static FamilyRec* gFamilyHead;
62static SkTDArray<NameFamilyPair> gNameList;
63
64struct FamilyRec {
65 FamilyRec* fNext;
66 SkTypeface* fFaces[4];
chudy@google.comada44802012-07-30 12:59:12 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 FamilyRec()
69 {
70 fNext = gFamilyHead;
71 memset(fFaces, 0, sizeof(fFaces));
72 gFamilyHead = this;
73 }
74};
75
76static SkTypeface* find_best_face(const FamilyRec* family,
reed@android.com1bfd0ca2009-02-20 14:22:36 +000077 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkTypeface* const* faces = family->fFaces;
chudy@google.comada44802012-07-30 12:59:12 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 if (faces[style] != NULL) { // exact match
81 return faces[style];
82 }
83 // look for a matching bold
84 style = (SkTypeface::Style)(style ^ SkTypeface::kItalic);
85 if (faces[style] != NULL) {
86 return faces[style];
87 }
88 // look for the plain
89 if (faces[SkTypeface::kNormal] != NULL) {
90 return faces[SkTypeface::kNormal];
91 }
92 // look for anything
93 for (int i = 0; i < 4; i++) {
94 if (faces[i] != NULL) {
95 return faces[i];
96 }
97 }
98 // should never get here, since the faces list should not be empty
tomhudson@google.com0c00f212011-12-28 14:59:50 +000099 SkDEBUGFAIL("faces list is empty");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 return NULL;
101}
102
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000103static FamilyRec* find_family(const SkTypeface* member) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 FamilyRec* curr = gFamilyHead;
105 while (curr != NULL) {
106 for (int i = 0; i < 4; i++) {
107 if (curr->fFaces[i] == member) {
108 return curr;
109 }
110 }
111 curr = curr->fNext;
112 }
113 return NULL;
114}
115
reed@android.comf2afb672009-09-28 16:12:48 +0000116static SkTypeface* find_from_uniqueID(uint32_t uniqueID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 FamilyRec* curr = gFamilyHead;
118 while (curr != NULL) {
119 for (int i = 0; i < 4; i++) {
120 SkTypeface* face = curr->fFaces[i];
121 if (face != NULL && face->uniqueID() == uniqueID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000122 return face;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 }
124 }
125 curr = curr->fNext;
126 }
epoger@google.com17b78942011-08-26 14:40:38 +0000127 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128}
129
130/* Remove reference to this face from its family. If the resulting family
131 is empty (has no faces), return that family, otherwise return NULL
132 */
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000133static FamilyRec* remove_from_family(const SkTypeface* face) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 FamilyRec* family = find_family(face);
135 SkASSERT(family->fFaces[face->style()] == face);
136 family->fFaces[face->style()] = NULL;
chudy@google.comada44802012-07-30 12:59:12 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 for (int i = 0; i < 4; i++) {
139 if (family->fFaces[i] != NULL) { // family is non-empty
140 return NULL;
141 }
142 }
143 return family; // return the empty family
144}
145
146// maybe we should make FamilyRec be doubly-linked
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000147static void detach_and_delete_family(FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 FamilyRec* curr = gFamilyHead;
149 FamilyRec* prev = NULL;
chudy@google.comada44802012-07-30 12:59:12 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 while (curr != NULL) {
152 FamilyRec* next = curr->fNext;
153 if (curr == family) {
154 if (prev == NULL) {
155 gFamilyHead = next;
156 } else {
157 prev->fNext = next;
158 }
159 SkDELETE(family);
160 return;
161 }
162 prev = curr;
163 curr = next;
164 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000165 SkDEBUGFAIL("Yikes, couldn't find family in our list to remove/delete");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166}
167
djsollen@google.com97145162012-05-31 19:55:08 +0000168static const char* find_family_name(const SkTypeface* familyMember) {
169 const FamilyRec* familyRec = find_family(familyMember);
170 for (int i = 0; i < gNameList.count(); i++) {
171 if (gNameList[i].fFamily == familyRec) {
172 return gNameList[i].fName;
173 }
174 }
175 return NULL;
176}
177
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178static FamilyRec* find_familyrec(const char name[]) {
chudy@google.comada44802012-07-30 12:59:12 +0000179 const NameFamilyPair* list = gNameList.begin();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name,
181 sizeof(list[0]));
182 return index >= 0 ? list[index].fFamily : NULL;
183}
184
185static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) {
186 FamilyRec* rec = find_familyrec(name);
187 return rec ? find_best_face(rec, style) : NULL;
188}
189
190static SkTypeface* find_typeface(const SkTypeface* familyMember,
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000191 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 const FamilyRec* family = find_family(familyMember);
193 return family ? find_best_face(family, style) : NULL;
194}
195
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000196static void add_name(const char name[], FamilyRec* family) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 SkAutoAsciiToLC tolc(name);
198 name = tolc.lc();
chudy@google.comada44802012-07-30 12:59:12 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 NameFamilyPair* list = gNameList.begin();
201 int count = gNameList.count();
chudy@google.comada44802012-07-30 12:59:12 +0000202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0]));
chudy@google.comada44802012-07-30 12:59:12 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 if (index < 0) {
206 list = gNameList.insert(~index);
207 list->construct(name, family);
208 }
209}
210
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000211static void remove_from_names(FamilyRec* emptyFamily) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212#ifdef SK_DEBUG
213 for (int i = 0; i < 4; i++) {
214 SkASSERT(emptyFamily->fFaces[i] == NULL);
215 }
216#endif
chudy@google.comada44802012-07-30 12:59:12 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 SkTDArray<NameFamilyPair>& list = gNameList;
chudy@google.comada44802012-07-30 12:59:12 +0000219
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 // must go backwards when removing
221 for (int i = list.count() - 1; i >= 0; --i) {
222 NameFamilyPair* pair = &list[i];
223 if (pair->fFamily == emptyFamily) {
224 pair->destruct();
225 list.remove(i);
226 }
227 }
228}
229
230///////////////////////////////////////////////////////////////////////////////
231
232class FamilyTypeface : public SkTypeface {
233public:
reed@google.com36812762011-02-23 14:49:33 +0000234 FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth)
235 : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 fIsSysFont = sysFont;
chudy@google.comada44802012-07-30 12:59:12 +0000237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 SkAutoMutexAcquire ac(gFamilyMutex);
chudy@google.comada44802012-07-30 12:59:12 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 if (NULL == family) {
241 family = SkNEW(FamilyRec);
242 }
243 family->fFaces[style] = this;
244 fFamilyRec = family; // just record it so we can return it if asked
245 }
chudy@google.comada44802012-07-30 12:59:12 +0000246
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000247 virtual ~FamilyTypeface() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 SkAutoMutexAcquire ac(gFamilyMutex);
chudy@google.comada44802012-07-30 12:59:12 +0000249
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 // remove us from our family. If the family is now empty, we return
251 // that and then remove that family from the name list
252 FamilyRec* family = remove_from_family(this);
253 if (NULL != family) {
254 remove_from_names(family);
255 detach_and_delete_family(family);
256 }
257 }
chudy@google.comada44802012-07-30 12:59:12 +0000258
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 bool isSysFont() const { return fIsSysFont; }
260 FamilyRec* getFamily() const { return fFamilyRec; }
reed@android.com22dbaaf2009-05-18 00:43:58 +0000261 // openStream returns a SkStream that has been ref-ed
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 virtual SkStream* openStream() = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 virtual const char* getUniqueString() const = 0;
chudy@google.comada44802012-07-30 12:59:12 +0000264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265private:
266 FamilyRec* fFamilyRec; // we don't own this, just point to it
267 bool fIsSysFont;
chudy@google.comada44802012-07-30 12:59:12 +0000268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 typedef SkTypeface INHERITED;
270};
271
272///////////////////////////////////////////////////////////////////////////////
273
reed@android.comf244f1b2010-04-16 12:40:08 +0000274/* This subclass is just a place holder for when we have no fonts available.
275 It exists so that our globals (e.g. gFamilyHead) that expect *something*
276 will not be null.
277 */
278class EmptyTypeface : public FamilyTypeface {
279public:
reed@google.com36812762011-02-23 14:49:33 +0000280 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
chudy@google.comada44802012-07-30 12:59:12 +0000281
reed@android.comf244f1b2010-04-16 12:40:08 +0000282 // overrides
283 virtual SkStream* openStream() { return NULL; }
reed@android.comf244f1b2010-04-16 12:40:08 +0000284 virtual const char* getUniqueString() const { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +0000285
reed@android.comf244f1b2010-04-16 12:40:08 +0000286private:
287 typedef FamilyTypeface INHERITED;
288};
289
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290class StreamTypeface : public FamilyTypeface {
291public:
292 StreamTypeface(Style style, bool sysFont, FamilyRec* family,
reed@google.com36812762011-02-23 14:49:33 +0000293 SkStream* stream, bool isFixedWidth)
294 : INHERITED(style, sysFont, family, isFixedWidth) {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000295 stream->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 fStream = stream;
297 }
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000298 virtual ~StreamTypeface() {
reed@android.com1c0c5a02010-04-12 20:16:49 +0000299 fStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 }
chudy@google.comada44802012-07-30 12:59:12 +0000301
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 // overrides
chudy@google.comada44802012-07-30 12:59:12 +0000303 virtual SkStream* openStream()
304 {
reed@android.com22dbaaf2009-05-18 00:43:58 +0000305 // openStream returns a refed stream.
306 fStream->ref();
307 return fStream;
308 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 virtual const char* getUniqueString() const { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +0000310
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311private:
312 SkStream* fStream;
chudy@google.comada44802012-07-30 12:59:12 +0000313
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 typedef FamilyTypeface INHERITED;
315};
316
317class FileTypeface : public FamilyTypeface {
318public:
319 FileTypeface(Style style, bool sysFont, FamilyRec* family,
reed@google.com36812762011-02-23 14:49:33 +0000320 const char path[], bool isFixedWidth)
321 : INHERITED(style, sysFont, family, isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 fPath.set(path);
323 }
chudy@google.comada44802012-07-30 12:59:12 +0000324
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 // overrides
326 virtual SkStream* openStream()
327 {
328 SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str()));
chudy@google.comada44802012-07-30 12:59:12 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 // check for failure
331 if (stream->getLength() <= 0) {
332 SkDELETE(stream);
333 // maybe MMAP isn't supported. try FILE
334 stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str()));
335 if (stream->getLength() <= 0) {
336 SkDELETE(stream);
337 stream = NULL;
338 }
339 }
340 return stream;
341 }
reed@android.com51709c72010-04-16 12:51:29 +0000342
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 virtual const char* getUniqueString() const {
344 const char* str = strrchr(fPath.c_str(), '/');
345 if (str) {
346 str += 1; // skip the '/'
347 }
348 return str;
349 }
chudy@google.comada44802012-07-30 12:59:12 +0000350
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351private:
352 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000353
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354 typedef FamilyTypeface INHERITED;
355};
356
357///////////////////////////////////////////////////////////////////////////////
358///////////////////////////////////////////////////////////////////////////////
359
360static bool get_name_and_style(const char path[], SkString* name,
chudy@google.comada44802012-07-30 12:59:12 +0000361 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 SkMMAPStream stream(path);
363 if (stream.getLength() > 0) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000364 return find_name_and_attributes(&stream, name, style, isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 }
366 else {
367 SkFILEStream stream(path);
368 if (stream.getLength() > 0) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000369 return find_name_and_attributes(&stream, name, style, isFixedWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370 }
371 }
chudy@google.comada44802012-07-30 12:59:12 +0000372
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 SkDebugf("---- failed to open <%s> as a font\n", path);
374 return false;
375}
376
377// these globals are assigned (once) by load_system_fonts()
378static SkTypeface* gFallBackTypeface;
379static FamilyRec* gDefaultFamily;
380static SkTypeface* gDefaultNormal;
381
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000382static void load_directory_fonts(const SkString& directory, unsigned int* count) {
383 SkOSFile::Iter iter(directory.c_str(), ".ttf");
reed@android.comf244f1b2010-04-16 12:40:08 +0000384 SkString name;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385
386 while (iter.next(&name, false)) {
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000387 SkString filename(directory);
388 filename.append(name);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389
reed@google.com36812762011-02-23 14:49:33 +0000390 bool isFixedWidth;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 SkString realname;
reed@google.com6963af22011-01-04 12:52:02 +0000392 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
chudy@google.comada44802012-07-30 12:59:12 +0000393
reed@google.com36812762011-02-23 14:49:33 +0000394 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
396 continue;
397 }
reed@android.comf2afb672009-09-28 16:12:48 +0000398
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 FamilyRec* family = find_familyrec(realname.c_str());
reed@android.com887e4f32010-04-15 14:04:52 +0000400 if (family && family->fFaces[style]) {
reed@android.com887e4f32010-04-15 14:04:52 +0000401 continue;
402 }
403
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 // this constructor puts us into the global gFamilyHead llist
405 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
406 (style,
407 true, // system-font (cannot delete)
408 family, // what family to join
reed@google.com36812762011-02-23 14:49:33 +0000409 filename.c_str(),
410 isFixedWidth) // filename
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 );
412
413 if (NULL == family) {
414 add_name(realname.c_str(), tf->getFamily());
415 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000416 *count += 1;
reed@android.comf244f1b2010-04-16 12:40:08 +0000417 }
418
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000419 SkOSFile::Iter dirIter(directory.c_str());
420 while (dirIter.next(&name, true)) {
421 if (name.startsWith(".")) {
422 continue;
423 }
424 SkString dirname(directory);
425 dirname.append(name);
426 dirname.append(SK_FONT_FILE_DIR_SEPERATOR);
427 load_directory_fonts(dirname, count);
428 }
429}
430
431static void load_system_fonts() {
432 // check if we've already be called
433 if (NULL != gDefaultNormal) {
434 return;
435 }
436
437 SkString baseDirectory(SK_FONT_FILE_PREFIX);
438 unsigned int count = 0;
439 load_directory_fonts(baseDirectory, &count);
440
reed@android.comf244f1b2010-04-16 12:40:08 +0000441 if (0 == count) {
442 SkNEW(EmptyTypeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 }
reed@android.comf2afb672009-09-28 16:12:48 +0000444
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 // do this after all fonts are loaded. This is our default font, and it
446 // acts as a sentinel so we only execute load_system_fonts() once
447 static const char* gDefaultNames[] = {
448 "Arial", "Verdana", "Times New Roman", NULL
449 };
450 const char** names = gDefaultNames;
451 while (*names) {
452 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
453 if (tf) {
454 gDefaultNormal = tf;
455 break;
456 }
457 }
458 // check if we found *something*
459 if (NULL == gDefaultNormal) {
460 if (NULL == gFamilyHead) {
461 sk_throw();
462 }
463 for (int i = 0; i < 4; i++) {
464 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
465 break;
466 }
467 }
468 }
469 if (NULL == gDefaultNormal) {
470 sk_throw();
471 }
chudy@google.comada44802012-07-30 12:59:12 +0000472 gFallBackTypeface = gDefaultNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 gDefaultFamily = find_family(gDefaultNormal);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474}
475
476///////////////////////////////////////////////////////////////////////////////
477
478void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
djsollen@google.com97145162012-05-31 19:55:08 +0000479
480 SkFontDescriptor descriptor;
bungeman@google.comb6896e72012-06-01 15:53:06 +0000481 descriptor.setFamilyName(find_family_name(face));
482 descriptor.setStyle(face->style());
djsollen@google.com97145162012-05-31 19:55:08 +0000483 descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString());
484
485 descriptor.serialize(stream);
486
487 const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont();
488 if (isCustomFont) {
489 // store the entire font in the fontData
490 SkStream* fontStream = ((FamilyTypeface*)face)->openStream();
491 const uint32_t length = fontStream->getLength();
492
493 stream->writePackedUInt(length);
494 stream->writeStream(fontStream, length);
495
496 fontStream->unref();
djsollen@google.come6488ad2012-03-29 16:09:48 +0000497 } else {
djsollen@google.com97145162012-05-31 19:55:08 +0000498 stream->writePackedUInt(0);
djsollen@google.come6488ad2012-03-29 16:09:48 +0000499 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500}
501
502SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
chudy@google.comada44802012-07-30 12:59:12 +0000503 load_system_fonts();
djsollen@google.com97145162012-05-31 19:55:08 +0000504
505 SkFontDescriptor descriptor(stream);
bungeman@google.comb6896e72012-06-01 15:53:06 +0000506 const char* familyName = descriptor.getFamilyName();
djsollen@google.com97145162012-05-31 19:55:08 +0000507 const char* typefaceName = descriptor.getFontFileName();
bungeman@google.comb6896e72012-06-01 15:53:06 +0000508 const SkTypeface::Style style = descriptor.getStyle();
djsollen@google.com97145162012-05-31 19:55:08 +0000509
510 const uint32_t customFontDataLength = stream->readPackedUInt();
511 if (customFontDataLength > 0) {
512
513 // generate a new stream to store the custom typeface
514 SkMemoryStream* fontStream = new SkMemoryStream(customFontDataLength - 1);
515 stream->read((void*)fontStream->getMemoryBase(), customFontDataLength - 1);
516
517 SkTypeface* face = CreateTypefaceFromStream(fontStream);
518
519 fontStream->unref();
520 return face;
521 }
522
523 return SkFontHost::CreateTypeface(NULL, familyName, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524}
525
526///////////////////////////////////////////////////////////////////////////////
527
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000528SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
529 const char familyName[],
530 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000531 load_system_fonts();
chudy@google.comada44802012-07-30 12:59:12 +0000532
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 SkAutoMutexAcquire ac(gFamilyMutex);
chudy@google.comada44802012-07-30 12:59:12 +0000534
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535 // clip to legal style bits
536 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
chudy@google.comada44802012-07-30 12:59:12 +0000537
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538 SkTypeface* tf = NULL;
chudy@google.comada44802012-07-30 12:59:12 +0000539
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540 if (NULL != familyFace) {
541 tf = find_typeface(familyFace, style);
542 } else if (NULL != familyName) {
543 // SkDebugf("======= familyName <%s>\n", familyName);
544 tf = find_typeface(familyName, style);
545 }
chudy@google.comada44802012-07-30 12:59:12 +0000546
reed@android.com8a1c16f2008-12-17 15:59:43 +0000547 if (NULL == tf) {
548 tf = find_best_face(gDefaultFamily, style);
549 }
chudy@google.comada44802012-07-30 12:59:12 +0000550
551 SkSafeRef(tf);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 return tf;
553}
554
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000555SkStream* SkFontHost::OpenStream(uint32_t fontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000556 FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 SkStream* stream = tf ? tf->openStream() : NULL;
chudy@google.comada44802012-07-30 12:59:12 +0000558
reed@android.com1c0c5a02010-04-12 20:16:49 +0000559 if (stream && stream->getLength() == 0) {
560 stream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000561 stream = NULL;
562 }
563 return stream;
564}
565
reed@android.comac981542009-07-31 16:17:01 +0000566size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
567 int32_t* index) {
568 SkDebugf("SkFontHost::GetFileName unimplemented\n");
569 return 0;
570}
571
reed@google.com7d26c592011-06-13 13:01:10 +0000572SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
reed@android.comf2afb672009-09-28 16:12:48 +0000573 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574}
575
576///////////////////////////////////////////////////////////////////////////////
577
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000578SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579 if (NULL == stream || stream->getLength() <= 0) {
580 SkDELETE(stream);
581 return NULL;
582 }
reed@google.com36812762011-02-23 14:49:33 +0000583
584 bool isFixedWidth;
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000585 SkTypeface::Style style;
586 if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
587 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth));
588 } else {
589 return NULL;
590 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591}
592
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000593SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
594 SkTypeface* face = NULL;
595 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
596
597 if (stream->isValid()) {
reed@android.comf2afb672009-09-28 16:12:48 +0000598 face = CreateTypefaceFromStream(stream);
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000599 }
600 stream->unref();
reed@android.comf2afb672009-09-28 16:12:48 +0000601 return face;
reed@android.com0becfc5b2009-01-13 13:26:44 +0000602}
603