blob: ac93738d626ee25ec4746566233c749f95d4aabc [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"
reed@google.com0fc17c32013-03-21 13:33:49 +000011#include "SkFontHost_FreeType_common.h"
djsollen@google.com97145162012-05-31 19:55:08 +000012#include "SkFontDescriptor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkDescriptor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#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
reed@google.com0fc17c32013-03-21 13:33:49 +0000232class FamilyTypeface : public SkTypeface_FreeType {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233public:
reed@google.com36812762011-02-23 14:49:33 +0000234 FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth)
reed@google.com032fbb82013-03-21 13:38:18 +0000235 : INHERITED(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@google.com292b1d42013-03-22 17:21:59 +0000261
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 virtual const char* getUniqueString() const = 0;
chudy@google.comada44802012-07-30 12:59:12 +0000263
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264private:
265 FamilyRec* fFamilyRec; // we don't own this, just point to it
266 bool fIsSysFont;
chudy@google.comada44802012-07-30 12:59:12 +0000267
reed@google.com032fbb82013-03-21 13:38:18 +0000268 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269};
270
271///////////////////////////////////////////////////////////////////////////////
272
reed@android.comf244f1b2010-04-16 12:40:08 +0000273/* This subclass is just a place holder for when we have no fonts available.
274 It exists so that our globals (e.g. gFamilyHead) that expect *something*
275 will not be null.
276 */
277class EmptyTypeface : public FamilyTypeface {
278public:
reed@google.com36812762011-02-23 14:49:33 +0000279 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
chudy@google.comada44802012-07-30 12:59:12 +0000280
reed@google.come1575aa2013-03-18 21:08:46 +0000281 virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +0000282
reed@google.com292b1d42013-03-22 17:21:59 +0000283protected:
284 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
285
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@google.come1575aa2013-03-18 21:08:46 +0000302 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +0000303
reed@google.com292b1d42013-03-22 17:21:59 +0000304protected:
305 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
306 *ttcIndex = 0;
307 fStream->ref();
308 return fStream;
309 }
310
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@google.come1575aa2013-03-18 21:08:46 +0000325 virtual const char* getUniqueString() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 const char* str = strrchr(fPath.c_str(), '/');
327 if (str) {
328 str += 1; // skip the '/'
329 }
330 return str;
331 }
chudy@google.comada44802012-07-30 12:59:12 +0000332
reed@google.com292b1d42013-03-22 17:21:59 +0000333protected:
334 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
335 *ttcIndex = 0;
336 return SkStream::NewFromFile(fPath.c_str());
337 }
338
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339private:
340 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000341
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 typedef FamilyTypeface INHERITED;
343};
344
345///////////////////////////////////////////////////////////////////////////////
346///////////////////////////////////////////////////////////////////////////////
347
348static bool get_name_and_style(const char path[], SkString* name,
chudy@google.comada44802012-07-30 12:59:12 +0000349 SkTypeface::Style* style, bool* isFixedWidth) {
reed@google.come1575aa2013-03-18 21:08:46 +0000350 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
351 if (stream.get()) {
352 return find_name_and_attributes(stream, name, style, isFixedWidth);
353 } else {
354 SkDebugf("---- failed to open <%s> as a font\n", path);
355 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357}
358
359// these globals are assigned (once) by load_system_fonts()
360static SkTypeface* gFallBackTypeface;
361static FamilyRec* gDefaultFamily;
362static SkTypeface* gDefaultNormal;
363
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000364static void load_directory_fonts(const SkString& directory, unsigned int* count) {
365 SkOSFile::Iter iter(directory.c_str(), ".ttf");
reed@android.comf244f1b2010-04-16 12:40:08 +0000366 SkString name;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367
368 while (iter.next(&name, false)) {
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000369 SkString filename(directory);
370 filename.append(name);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371
reed@google.com36812762011-02-23 14:49:33 +0000372 bool isFixedWidth;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 SkString realname;
reed@google.com6963af22011-01-04 12:52:02 +0000374 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
chudy@google.comada44802012-07-30 12:59:12 +0000375
reed@google.com36812762011-02-23 14:49:33 +0000376 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
378 continue;
379 }
reed@android.comf2afb672009-09-28 16:12:48 +0000380
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 FamilyRec* family = find_familyrec(realname.c_str());
reed@android.com887e4f32010-04-15 14:04:52 +0000382 if (family && family->fFaces[style]) {
reed@android.com887e4f32010-04-15 14:04:52 +0000383 continue;
384 }
385
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 // this constructor puts us into the global gFamilyHead llist
387 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
388 (style,
389 true, // system-font (cannot delete)
390 family, // what family to join
reed@google.com36812762011-02-23 14:49:33 +0000391 filename.c_str(),
392 isFixedWidth) // filename
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 );
394
395 if (NULL == family) {
396 add_name(realname.c_str(), tf->getFamily());
397 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000398 *count += 1;
reed@android.comf244f1b2010-04-16 12:40:08 +0000399 }
400
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000401 SkOSFile::Iter dirIter(directory.c_str());
402 while (dirIter.next(&name, true)) {
403 if (name.startsWith(".")) {
404 continue;
405 }
406 SkString dirname(directory);
407 dirname.append(name);
408 dirname.append(SK_FONT_FILE_DIR_SEPERATOR);
409 load_directory_fonts(dirname, count);
410 }
411}
412
413static void load_system_fonts() {
414 // check if we've already be called
415 if (NULL != gDefaultNormal) {
416 return;
417 }
418
419 SkString baseDirectory(SK_FONT_FILE_PREFIX);
420 unsigned int count = 0;
421 load_directory_fonts(baseDirectory, &count);
422
reed@android.comf244f1b2010-04-16 12:40:08 +0000423 if (0 == count) {
424 SkNEW(EmptyTypeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 }
reed@android.comf2afb672009-09-28 16:12:48 +0000426
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 // do this after all fonts are loaded. This is our default font, and it
428 // acts as a sentinel so we only execute load_system_fonts() once
429 static const char* gDefaultNames[] = {
430 "Arial", "Verdana", "Times New Roman", NULL
431 };
432 const char** names = gDefaultNames;
433 while (*names) {
434 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
435 if (tf) {
436 gDefaultNormal = tf;
437 break;
438 }
439 }
440 // check if we found *something*
441 if (NULL == gDefaultNormal) {
442 if (NULL == gFamilyHead) {
443 sk_throw();
444 }
445 for (int i = 0; i < 4; i++) {
446 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
447 break;
448 }
449 }
450 }
451 if (NULL == gDefaultNormal) {
452 sk_throw();
453 }
chudy@google.comada44802012-07-30 12:59:12 +0000454 gFallBackTypeface = gDefaultNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 gDefaultFamily = find_family(gDefaultNormal);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456}
457
458///////////////////////////////////////////////////////////////////////////////
459
460void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
djsollen@google.com97145162012-05-31 19:55:08 +0000461
462 SkFontDescriptor descriptor;
bungeman@google.comb6896e72012-06-01 15:53:06 +0000463 descriptor.setFamilyName(find_family_name(face));
464 descriptor.setStyle(face->style());
djsollen@google.com97145162012-05-31 19:55:08 +0000465 descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString());
466
467 descriptor.serialize(stream);
468
469 const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont();
470 if (isCustomFont) {
471 // store the entire font in the fontData
reed@google.com292b1d42013-03-22 17:21:59 +0000472 SkStream* fontStream = face->openStream(NULL);
djsollen@google.com97145162012-05-31 19:55:08 +0000473 const uint32_t length = fontStream->getLength();
474
475 stream->writePackedUInt(length);
476 stream->writeStream(fontStream, length);
477
478 fontStream->unref();
djsollen@google.come6488ad2012-03-29 16:09:48 +0000479 } else {
djsollen@google.com97145162012-05-31 19:55:08 +0000480 stream->writePackedUInt(0);
djsollen@google.come6488ad2012-03-29 16:09:48 +0000481 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482}
483
484SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
chudy@google.comada44802012-07-30 12:59:12 +0000485 load_system_fonts();
djsollen@google.com97145162012-05-31 19:55:08 +0000486
487 SkFontDescriptor descriptor(stream);
bungeman@google.comb6896e72012-06-01 15:53:06 +0000488 const char* familyName = descriptor.getFamilyName();
bungeman@google.comb6896e72012-06-01 15:53:06 +0000489 const SkTypeface::Style style = descriptor.getStyle();
djsollen@google.com97145162012-05-31 19:55:08 +0000490
491 const uint32_t customFontDataLength = stream->readPackedUInt();
492 if (customFontDataLength > 0) {
493
494 // generate a new stream to store the custom typeface
495 SkMemoryStream* fontStream = new SkMemoryStream(customFontDataLength - 1);
496 stream->read((void*)fontStream->getMemoryBase(), customFontDataLength - 1);
497
498 SkTypeface* face = CreateTypefaceFromStream(fontStream);
499
500 fontStream->unref();
501 return face;
502 }
503
504 return SkFontHost::CreateTypeface(NULL, familyName, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000505}
506
507///////////////////////////////////////////////////////////////////////////////
508
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000509SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
510 const char familyName[],
511 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 load_system_fonts();
chudy@google.comada44802012-07-30 12:59:12 +0000513
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514 SkAutoMutexAcquire ac(gFamilyMutex);
chudy@google.comada44802012-07-30 12:59:12 +0000515
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516 // clip to legal style bits
517 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
chudy@google.comada44802012-07-30 12:59:12 +0000518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519 SkTypeface* tf = NULL;
chudy@google.comada44802012-07-30 12:59:12 +0000520
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 if (NULL != familyFace) {
522 tf = find_typeface(familyFace, style);
523 } else if (NULL != familyName) {
524 // SkDebugf("======= familyName <%s>\n", familyName);
525 tf = find_typeface(familyName, style);
526 }
chudy@google.comada44802012-07-30 12:59:12 +0000527
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 if (NULL == tf) {
529 tf = find_best_face(gDefaultFamily, style);
530 }
chudy@google.comada44802012-07-30 12:59:12 +0000531
532 SkSafeRef(tf);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 return tf;
534}
535
reed@google.com0da48612013-03-19 16:06:52 +0000536SkTypeface* SkFontHost::NextLogicalTypeface(SkFontID currFontID, SkFontID origFontID) {
537 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538}
539
540///////////////////////////////////////////////////////////////////////////////
541
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000542SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 if (NULL == stream || stream->getLength() <= 0) {
544 SkDELETE(stream);
545 return NULL;
546 }
reed@google.com36812762011-02-23 14:49:33 +0000547
548 bool isFixedWidth;
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000549 SkTypeface::Style style;
550 if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
551 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth));
552 } else {
553 return NULL;
554 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555}
556
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000557SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
mike@reedtribe.orgf3811622013-03-19 02:18:33 +0000558 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
559 return stream.get() ? CreateTypefaceFromStream(stream) : NULL;
reed@android.com0becfc5b2009-01-13 13:26:44 +0000560}