blob: 8825919a987349e3388b07e5946ef5a6f0d678ed [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,
bungeman@google.comfe747652013-03-25 19:36:11 +000029 SkTypeface::Style* style, bool* isFixedPitch);
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:
bungeman@google.comfe747652013-03-25 19:36:11 +0000234 FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedPitch)
235 : INHERITED(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedPitch) {
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@google.com5526ede2013-03-25 13:03:37 +0000264protected:
265 virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE;
266
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267private:
268 FamilyRec* fFamilyRec; // we don't own this, just point to it
269 bool fIsSysFont;
chudy@google.comada44802012-07-30 12:59:12 +0000270
reed@google.com032fbb82013-03-21 13:38:18 +0000271 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272};
273
274///////////////////////////////////////////////////////////////////////////////
275
reed@android.comf244f1b2010-04-16 12:40:08 +0000276/* This subclass is just a place holder for when we have no fonts available.
277 It exists so that our globals (e.g. gFamilyHead) that expect *something*
278 will not be null.
279 */
280class EmptyTypeface : public FamilyTypeface {
281public:
reed@google.com36812762011-02-23 14:49:33 +0000282 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
chudy@google.comada44802012-07-30 12:59:12 +0000283
reed@google.come1575aa2013-03-18 21:08:46 +0000284 virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +0000285
reed@google.com292b1d42013-03-22 17:21:59 +0000286protected:
287 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
288
reed@android.comf244f1b2010-04-16 12:40:08 +0000289private:
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,
bungeman@google.comfe747652013-03-25 19:36:11 +0000296 SkStream* stream, bool isFixedPitch)
297 : INHERITED(style, sysFont, family, isFixedPitch) {
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 }
chudy@google.comada44802012-07-30 12:59:12 +0000304
reed@google.come1575aa2013-03-18 21:08:46 +0000305 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +0000306
reed@google.com292b1d42013-03-22 17:21:59 +0000307protected:
308 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
309 *ttcIndex = 0;
310 fStream->ref();
311 return fStream;
312 }
313
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314private:
315 SkStream* fStream;
chudy@google.comada44802012-07-30 12:59:12 +0000316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 typedef FamilyTypeface INHERITED;
318};
319
320class FileTypeface : public FamilyTypeface {
321public:
322 FileTypeface(Style style, bool sysFont, FamilyRec* family,
bungeman@google.comfe747652013-03-25 19:36:11 +0000323 const char path[], bool isFixedPitch)
324 : INHERITED(style, sysFont, family, isFixedPitch) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 fPath.set(path);
326 }
chudy@google.comada44802012-07-30 12:59:12 +0000327
reed@google.come1575aa2013-03-18 21:08:46 +0000328 virtual const char* getUniqueString() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 const char* str = strrchr(fPath.c_str(), '/');
330 if (str) {
331 str += 1; // skip the '/'
332 }
333 return str;
334 }
chudy@google.comada44802012-07-30 12:59:12 +0000335
reed@google.com292b1d42013-03-22 17:21:59 +0000336protected:
337 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
338 *ttcIndex = 0;
339 return SkStream::NewFromFile(fPath.c_str());
340 }
341
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342private:
343 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000344
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 typedef FamilyTypeface INHERITED;
346};
347
348///////////////////////////////////////////////////////////////////////////////
349///////////////////////////////////////////////////////////////////////////////
350
351static bool get_name_and_style(const char path[], SkString* name,
bungeman@google.comfe747652013-03-25 19:36:11 +0000352 SkTypeface::Style* style, bool* isFixedPitch) {
reed@google.come1575aa2013-03-18 21:08:46 +0000353 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
354 if (stream.get()) {
bungeman@google.comfe747652013-03-25 19:36:11 +0000355 return find_name_and_attributes(stream, name, style, isFixedPitch);
reed@google.come1575aa2013-03-18 21:08:46 +0000356 } else {
357 SkDebugf("---- failed to open <%s> as a font\n", path);
358 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360}
361
362// these globals are assigned (once) by load_system_fonts()
363static SkTypeface* gFallBackTypeface;
364static FamilyRec* gDefaultFamily;
365static SkTypeface* gDefaultNormal;
366
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000367static void load_directory_fonts(const SkString& directory, unsigned int* count) {
368 SkOSFile::Iter iter(directory.c_str(), ".ttf");
reed@android.comf244f1b2010-04-16 12:40:08 +0000369 SkString name;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370
371 while (iter.next(&name, false)) {
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000372 SkString filename(directory);
373 filename.append(name);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374
bungeman@google.comfe747652013-03-25 19:36:11 +0000375 bool isFixedPitch;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 SkString realname;
reed@google.com6963af22011-01-04 12:52:02 +0000377 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
chudy@google.comada44802012-07-30 12:59:12 +0000378
bungeman@google.comfe747652013-03-25 19:36:11 +0000379 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
381 continue;
382 }
reed@android.comf2afb672009-09-28 16:12:48 +0000383
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384 FamilyRec* family = find_familyrec(realname.c_str());
reed@android.com887e4f32010-04-15 14:04:52 +0000385 if (family && family->fFaces[style]) {
reed@android.com887e4f32010-04-15 14:04:52 +0000386 continue;
387 }
388
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 // this constructor puts us into the global gFamilyHead llist
390 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
391 (style,
392 true, // system-font (cannot delete)
393 family, // what family to join
reed@google.com36812762011-02-23 14:49:33 +0000394 filename.c_str(),
bungeman@google.comfe747652013-03-25 19:36:11 +0000395 isFixedPitch) // filename
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 );
397
398 if (NULL == family) {
399 add_name(realname.c_str(), tf->getFamily());
400 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000401 *count += 1;
reed@android.comf244f1b2010-04-16 12:40:08 +0000402 }
403
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000404 SkOSFile::Iter dirIter(directory.c_str());
405 while (dirIter.next(&name, true)) {
406 if (name.startsWith(".")) {
407 continue;
408 }
409 SkString dirname(directory);
410 dirname.append(name);
411 dirname.append(SK_FONT_FILE_DIR_SEPERATOR);
412 load_directory_fonts(dirname, count);
413 }
414}
415
416static void load_system_fonts() {
417 // check if we've already be called
418 if (NULL != gDefaultNormal) {
419 return;
420 }
421
422 SkString baseDirectory(SK_FONT_FILE_PREFIX);
423 unsigned int count = 0;
424 load_directory_fonts(baseDirectory, &count);
425
reed@android.comf244f1b2010-04-16 12:40:08 +0000426 if (0 == count) {
427 SkNEW(EmptyTypeface);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 }
reed@android.comf2afb672009-09-28 16:12:48 +0000429
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430 // do this after all fonts are loaded. This is our default font, and it
431 // acts as a sentinel so we only execute load_system_fonts() once
432 static const char* gDefaultNames[] = {
433 "Arial", "Verdana", "Times New Roman", NULL
434 };
435 const char** names = gDefaultNames;
436 while (*names) {
437 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
438 if (tf) {
439 gDefaultNormal = tf;
440 break;
441 }
442 }
443 // check if we found *something*
444 if (NULL == gDefaultNormal) {
445 if (NULL == gFamilyHead) {
446 sk_throw();
447 }
448 for (int i = 0; i < 4; i++) {
449 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
450 break;
451 }
452 }
453 }
454 if (NULL == gDefaultNormal) {
455 sk_throw();
456 }
chudy@google.comada44802012-07-30 12:59:12 +0000457 gFallBackTypeface = gDefaultNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458 gDefaultFamily = find_family(gDefaultNormal);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459}
460
461///////////////////////////////////////////////////////////////////////////////
462
reed@google.com5526ede2013-03-25 13:03:37 +0000463void FamilyTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
464 bool* isLocalStream) const {
465 desc->setFamilyName(find_family_name(this));
466 desc->setFontFileName(this->getUniqueString());
467 *isLocalStream = !this->isSysFont();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468}
469
470///////////////////////////////////////////////////////////////////////////////
471
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000472SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
473 const char familyName[],
474 SkTypeface::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475 load_system_fonts();
chudy@google.comada44802012-07-30 12:59:12 +0000476
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477 SkAutoMutexAcquire ac(gFamilyMutex);
chudy@google.comada44802012-07-30 12:59:12 +0000478
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479 // clip to legal style bits
480 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
chudy@google.comada44802012-07-30 12:59:12 +0000481
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482 SkTypeface* tf = NULL;
chudy@google.comada44802012-07-30 12:59:12 +0000483
reed@android.com8a1c16f2008-12-17 15:59:43 +0000484 if (NULL != familyFace) {
485 tf = find_typeface(familyFace, style);
486 } else if (NULL != familyName) {
487 // SkDebugf("======= familyName <%s>\n", familyName);
488 tf = find_typeface(familyName, style);
489 }
chudy@google.comada44802012-07-30 12:59:12 +0000490
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491 if (NULL == tf) {
492 tf = find_best_face(gDefaultFamily, style);
493 }
chudy@google.comada44802012-07-30 12:59:12 +0000494
495 SkSafeRef(tf);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 return tf;
497}
498
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499///////////////////////////////////////////////////////////////////////////////
500
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000501SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502 if (NULL == stream || stream->getLength() <= 0) {
503 SkDELETE(stream);
504 return NULL;
505 }
reed@google.com36812762011-02-23 14:49:33 +0000506
bungeman@google.comfe747652013-03-25 19:36:11 +0000507 bool isFixedPitch;
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000508 SkTypeface::Style style;
bungeman@google.comfe747652013-03-25 19:36:11 +0000509 if (find_name_and_attributes(stream, NULL, &style, &isFixedPitch)) {
510 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedPitch));
djsollen@google.com4dc686d2012-02-15 21:03:45 +0000511 } else {
512 return NULL;
513 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514}
515
reed@android.com1bfd0ca2009-02-20 14:22:36 +0000516SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
mike@reedtribe.orgf3811622013-03-19 02:18:33 +0000517 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
518 return stream.get() ? CreateTypefaceFromStream(stream) : NULL;
reed@android.com0becfc5b2009-01-13 13:26:44 +0000519}
reed@google.com070da5e2013-03-27 20:01:49 +0000520
521///////////////////////////////////////////////////////////////////////////////
522
523#include "SkFontMgr.h"
524
525SkFontMgr* SkFontMgr::Factory() {
526 // todo
527 return NULL;
528}