blob: 9a38261e80262789788e545dd9d86cbb5d1d9825 [file] [log] [blame]
djsollen@google.combfae9d32013-05-21 16:53:50 +00001
2/*
3 * Copyright 2013 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.
7 */
8
9#include "SkFontConfigInterface.h"
10#include "SkTypeface_android.h"
11
12#include "SkFontConfigParser_android.h"
13#include "SkFontConfigTypeface.h"
14#include "SkFontMgr.h"
15#include "SkGlyphCache.h"
16#include "SkPaint.h"
djsollen@google.combfae9d32013-05-21 16:53:50 +000017#include "SkString.h"
18#include "SkStream.h"
19#include "SkThread.h"
20#include "SkTypefaceCache.h"
21#include "SkTArray.h"
22#include "SkTDict.h"
23#include "SkTSearch.h"
24
25#include <stdio.h>
26#include <string.h>
27
28#ifndef SK_DEBUG_FONTS
29 #define SK_DEBUG_FONTS 0
30#endif
31
32#if SK_DEBUG_FONTS
33 #define DEBUG_FONT(args) SkDebugf args
34#else
35 #define DEBUG_FONT(args)
36#endif
37
38///////////////////////////////////////////////////////////////////////////////
39
40// For test only.
41static const char* gTestMainConfigFile = NULL;
42static const char* gTestFallbackConfigFile = NULL;
43static const char* gTestFontFilePrefix = NULL;
44
45///////////////////////////////////////////////////////////////////////////////
46
djsollen@google.com40078cb2013-05-24 20:31:57 +000047typedef int32_t FontRecID;
48#define INVALID_FONT_REC_ID -1
49
50typedef int32_t FamilyRecID;
51#define INVALID_FAMILY_REC_ID -1
52
djsollen@google.combfae9d32013-05-21 16:53:50 +000053// used to record our notion of the pre-existing fonts
54struct FontRec {
55 SkRefPtr<SkTypeface> fTypeface;
56 SkString fFileName;
57 SkTypeface::Style fStyle;
djsollen@google.combfae9d32013-05-21 16:53:50 +000058 bool fIsValid;
djsollen@google.com40078cb2013-05-24 20:31:57 +000059 FamilyRecID fFamilyRecID;
djsollen@google.combfae9d32013-05-21 16:53:50 +000060};
61
djsollen@google.combfae9d32013-05-21 16:53:50 +000062struct FamilyRec {
63 FamilyRec() {
64 memset(fFontRecID, INVALID_FONT_REC_ID, sizeof(fFontRecID));
65 }
66
67 static const int FONT_STYLE_COUNT = 4;
68 FontRecID fFontRecID[FONT_STYLE_COUNT];
djsollen@google.comb27eba72013-09-06 12:59:50 +000069 bool fIsFallbackFont;
70 SkPaintOptionsAndroid fPaintOptions;
djsollen@google.combfae9d32013-05-21 16:53:50 +000071};
72
djsollen@google.combfae9d32013-05-21 16:53:50 +000073
djsollen@google.comb27eba72013-09-06 12:59:50 +000074typedef SkTDArray<FamilyRecID> FallbackFontList;
djsollen@google.combfae9d32013-05-21 16:53:50 +000075
76class SkFontConfigInterfaceAndroid : public SkFontConfigInterface {
77public:
78 SkFontConfigInterfaceAndroid(SkTDArray<FontFamily*>& fontFamilies);
79 virtual ~SkFontConfigInterfaceAndroid();
80
81 virtual bool matchFamilyName(const char familyName[],
82 SkTypeface::Style requested,
83 FontIdentity* outFontIdentifier,
84 SkString* outFamilyName,
85 SkTypeface::Style* outStyle) SK_OVERRIDE;
86 virtual SkStream* openStream(const FontIdentity&) SK_OVERRIDE;
87
88 // new APIs
89 virtual SkDataTable* getFamilyNames() SK_OVERRIDE;
90 virtual bool matchFamilySet(const char inFamilyName[],
91 SkString* outFamilyName,
92 SkTArray<FontIdentity>*) SK_OVERRIDE;
93
94 /**
95 * Get the family name of the font in the default fallback font list that
96 * contains the specified chararacter. if no font is found, returns false.
97 */
98 bool getFallbackFamilyNameForChar(SkUnichar uni, SkString* name);
99 /**
100 *
101 */
102 SkTypeface* getTypefaceForChar(SkUnichar uni, SkTypeface::Style style,
103 SkPaintOptionsAndroid::FontVariant fontVariant);
104 SkTypeface* nextLogicalTypeface(SkFontID currFontID, SkFontID origFontID,
105 const SkPaintOptionsAndroid& options);
106
107private:
djsollen@google.comb27eba72013-09-06 12:59:50 +0000108 void addFallbackFamily(FamilyRecID fontRecID);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000109 SkTypeface* getTypefaceForFontRec(FontRecID fontRecID);
djsollen@google.com9a70f342013-06-25 18:07:45 +0000110 FallbackFontList* getCurrentLocaleFallbackFontList();
djsollen@google.com40078cb2013-05-24 20:31:57 +0000111 FallbackFontList* findFallbackFontList(const SkLanguage& lang, bool isOriginal = true);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000112
113 SkTArray<FontRec> fFonts;
114 SkTArray<FamilyRec> fFontFamilies;
115 SkTDict<FamilyRecID> fFamilyNameDict;
116 FamilyRecID fDefaultFamilyRecID;
117
118 // (SkLanguage)<->(fallback chain index) translation
119 SkTDict<FallbackFontList*> fFallbackFontDict;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000120 SkTDict<FallbackFontList*> fFallbackFontAliasDict;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000121 FallbackFontList fDefaultFallbackList;
djsollen@google.com9a70f342013-06-25 18:07:45 +0000122
123 // fallback info for current locale
124 SkString fCachedLocale;
125 FallbackFontList* fLocaleFallbackFontList;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000126};
127
128///////////////////////////////////////////////////////////////////////////////
129
130static SkFontConfigInterfaceAndroid* getSingletonInterface() {
131 SK_DECLARE_STATIC_MUTEX(gMutex);
132 static SkFontConfigInterfaceAndroid* gFontConfigInterface;
133
134 SkAutoMutexAcquire ac(gMutex);
135 if (NULL == gFontConfigInterface) {
136 // load info from a configuration file that we can use to populate the
137 // system/fallback font structures
138 SkTDArray<FontFamily*> fontFamilies;
139 if (!gTestMainConfigFile) {
140 SkFontConfigParser::GetFontFamilies(fontFamilies);
141 } else {
142 SkFontConfigParser::GetTestFontFamilies(fontFamilies, gTestMainConfigFile,
143 gTestFallbackConfigFile);
144 }
145
146 gFontConfigInterface = new SkFontConfigInterfaceAndroid(fontFamilies);
147
148 // cleanup the data we received from the parser
149 fontFamilies.deleteAll();
150 }
151 return gFontConfigInterface;
152}
153
154SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
155 return getSingletonInterface();
156}
157
158///////////////////////////////////////////////////////////////////////////////
159
160static bool has_font(const SkTArray<FontRec>& array, const SkString& filename) {
161 for (int i = 0; i < array.count(); i++) {
162 if (array[i].fFileName == filename) {
163 return true;
164 }
165 }
166 return false;
167}
168
169#ifndef SK_FONT_FILE_PREFIX
170 #define SK_FONT_FILE_PREFIX "/fonts/"
171#endif
172
173static void get_path_for_sys_fonts(SkString* full, const char name[]) {
174 if (gTestFontFilePrefix) {
175 full->set(gTestFontFilePrefix);
176 } else {
177 full->set(getenv("ANDROID_ROOT"));
178 full->append(SK_FONT_FILE_PREFIX);
179 }
180 full->append(name);
181}
182
183static void insert_into_name_dict(SkTDict<FamilyRecID>& familyNameDict,
184 const char* name, FamilyRecID familyRecID) {
185 SkAutoAsciiToLC tolc(name);
djsollen@google.com92e3f082013-08-27 17:40:03 +0000186 if (familyNameDict.find(tolc.lc())) {
187 SkDebugf("---- system font attempting to use a the same name [%s] for"
188 "multiple families. skipping subsequent occurrences", tolc.lc());
189 } else {
190 familyNameDict.set(tolc.lc(), familyRecID);
191 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000192}
193
194// Defined in SkFontHost_FreeType.cpp
195bool find_name_and_attributes(SkStream* stream, SkString* name,
196 SkTypeface::Style* style, bool* isFixedWidth);
197
198///////////////////////////////////////////////////////////////////////////////
199
200SkFontConfigInterfaceAndroid::SkFontConfigInterfaceAndroid(SkTDArray<FontFamily*>& fontFamilies) :
201 fFonts(fontFamilies.count()),
202 fFontFamilies(fontFamilies.count() / FamilyRec::FONT_STYLE_COUNT),
203 fFamilyNameDict(1024),
204 fDefaultFamilyRecID(INVALID_FAMILY_REC_ID),
djsollen@google.com40078cb2013-05-24 20:31:57 +0000205 fFallbackFontDict(128),
djsollen@google.com9a70f342013-06-25 18:07:45 +0000206 fFallbackFontAliasDict(128),
207 fLocaleFallbackFontList(NULL) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000208
209 for (int i = 0; i < fontFamilies.count(); ++i) {
210 FontFamily* family = fontFamilies[i];
211
212 // defer initializing the familyRec until we can be sure that at least
213 // one of it's children contains a valid font file
214 FamilyRec* familyRec = NULL;
215 FamilyRecID familyRecID = INVALID_FAMILY_REC_ID;
216
217 for (int j = 0; j < family->fFontFiles.count(); ++j) {
218 SkString filename;
219 get_path_for_sys_fonts(&filename, family->fFontFiles[j]->fFileName);
220
221 if (has_font(fFonts, filename)) {
222 SkDebugf("---- system font and fallback font files specify a duplicate "
223 "font %s, skipping the second occurrence", filename.c_str());
224 continue;
225 }
226
227 FontRec& fontRec = fFonts.push_back();
228 fontRec.fFileName = filename;
229 fontRec.fStyle = SkTypeface::kNormal;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000230 fontRec.fIsValid = false;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000231 fontRec.fFamilyRecID = familyRecID;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000232
233 const FontRecID fontRecID = fFonts.count() - 1;
234
235 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
236 if (stream.get() != NULL) {
237 bool isFixedWidth;
238 SkString name;
239 fontRec.fIsValid = find_name_and_attributes(stream.get(), &name,
240 &fontRec.fStyle, &isFixedWidth);
241 } else {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000242 if (!family->fIsFallbackFont) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000243 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
244 }
245 }
246
247 if (fontRec.fIsValid) {
248 DEBUG_FONT(("---- SystemFonts[%d][%d] fallback=%d file=%s",
249 i, fFonts.count() - 1, fontRec.fIsFallbackFont, filename.c_str()));
250 } else {
251 DEBUG_FONT(("---- SystemFonts[%d][%d] fallback=%d file=%s (INVALID)",
252 i, fFonts.count() - 1, fontRec.fIsFallbackFont, filename.c_str()));
253 continue;
254 }
255
256 // create a familyRec now that we know that at least one font in
257 // the family is valid
258 if (familyRec == NULL) {
259 familyRec = &fFontFamilies.push_back();
260 familyRecID = fFontFamilies.count() - 1;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000261 fontRec.fFamilyRecID = familyRecID;
djsollen@google.comb27eba72013-09-06 12:59:50 +0000262
263 familyRec->fIsFallbackFont = family->fIsFallbackFont;
264 familyRec->fPaintOptions = family->fFontFiles[j]->fPaintOptions;
265
266 // if this is a fallback font then add it to the appropriate fallback chains
267 if (familyRec->fIsFallbackFont) {
268 addFallbackFamily(familyRecID);
269 }
270 } else if (familyRec->fPaintOptions != family->fFontFiles[j]->fPaintOptions) {
271 SkDebugf("Every font file within a family must have identical"
272 "language and variant attributes");
273 sk_throw();
djsollen@google.combfae9d32013-05-21 16:53:50 +0000274 }
275
276 // add this font to the current familyRec
277 if (INVALID_FONT_REC_ID != familyRec->fFontRecID[fontRec.fStyle]) {
278 DEBUG_FONT(("Overwriting familyRec for style[%d] old,new:(%d,%d)",
279 fontRec.fStyle, familyRec->fFontRecID[fontRec.fStyle],
280 fontRecID));
281 }
282 familyRec->fFontRecID[fontRec.fStyle] = fontRecID;
283
djsollen@google.combfae9d32013-05-21 16:53:50 +0000284 // add the fallback file name to the name dictionary. This is needed
285 // by getFallbackFamilyNameForChar() so that fallback families can be
286 // requested by the filenames of the fonts they contain.
djsollen@google.comb27eba72013-09-06 12:59:50 +0000287 if (familyRec && familyRec->fIsFallbackFont) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000288 insert_into_name_dict(fFamilyNameDict, fontRec.fFileName.c_str(), familyRecID);
289 }
290 }
291
292 // add the names that map to this family to the dictionary for easy lookup
djsollen@google.comb27eba72013-09-06 12:59:50 +0000293 if (familyRec && !familyRec->fIsFallbackFont) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000294 SkTDArray<const char*> names = family->fNames;
295 if (names.isEmpty()) {
296 SkDEBUGFAIL("ERROR: non-fallback font with no name");
297 continue;
298 }
299
300 for (int i = 0; i < names.count(); i++) {
301 insert_into_name_dict(fFamilyNameDict, names[i], familyRecID);
302 }
303 }
304
305 }
306
307 DEBUG_FONT(("---- We have %d system fonts", fFonts.count()));
308
309 if (fFontFamilies.count() > 0) {
310 fDefaultFamilyRecID = 0;
311 }
312
313 // scans the default fallback font chain, adding every entry to every other
314 // fallback font chain to which it does not belong. this results in every
315 // language-specific fallback font chain having all of its fallback fonts at
316 // the front of the chain, and everything else at the end.
317 FallbackFontList* fallbackList;
318 SkTDict<FallbackFontList*>::Iter iter(fFallbackFontDict);
319 const char* fallbackLang = iter.next(&fallbackList);
320 while(fallbackLang != NULL) {
321 for (int i = 0; i < fDefaultFallbackList.count(); i++) {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000322 FamilyRecID familyRecID = fDefaultFallbackList[i];
323 const SkString& fontLang = fFontFamilies[familyRecID].fPaintOptions.getLanguage().getTag();
djsollen@google.combfae9d32013-05-21 16:53:50 +0000324 if (strcmp(fallbackLang, fontLang.c_str()) != 0) {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000325 fallbackList->push(familyRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000326 }
327 }
328 // move to the next fallback list in the dictionary
329 fallbackLang = iter.next(&fallbackList);
330 }
331}
332
333SkFontConfigInterfaceAndroid::~SkFontConfigInterfaceAndroid() {
334 // iterate through and cleanup fFallbackFontDict
335 SkTDict<FallbackFontList*>::Iter iter(fFallbackFontDict);
336 FallbackFontList* fallbackList;
337 while(iter.next(&fallbackList) != NULL) {
338 SkDELETE(fallbackList);
339 }
340}
341
djsollen@google.comb27eba72013-09-06 12:59:50 +0000342void SkFontConfigInterfaceAndroid::addFallbackFamily(FamilyRecID familyRecID) {
343 SkASSERT(familyRecID < fFontFamilies.count());
344 const FamilyRec& familyRec = fFontFamilies[familyRecID];
345 SkASSERT(familyRec.fIsFallbackFont);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000346
347 // add to the default fallback list
djsollen@google.comb27eba72013-09-06 12:59:50 +0000348 fDefaultFallbackList.push(familyRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000349
350 // stop here if it is the default language tag
djsollen@google.comb27eba72013-09-06 12:59:50 +0000351 const SkString& languageTag = familyRec.fPaintOptions.getLanguage().getTag();
djsollen@google.combfae9d32013-05-21 16:53:50 +0000352 if (languageTag.isEmpty()) {
353 return;
354 }
355
356 // add to the appropriate language's custom fallback list
357 FallbackFontList* customList = NULL;
358 if (!fFallbackFontDict.find(languageTag.c_str(), &customList)) {
359 DEBUG_FONT(("---- Created fallback list for \"%s\"", languageTag.c_str()));
360 customList = SkNEW(FallbackFontList);
361 fFallbackFontDict.set(languageTag.c_str(), customList);
362 }
363 SkASSERT(customList != NULL);
djsollen@google.comb27eba72013-09-06 12:59:50 +0000364 customList->push(familyRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000365}
366
367
368static FontRecID find_best_style(const FamilyRec& family, SkTypeface::Style style) {
369
370 const FontRecID* fontRecIDs = family.fFontRecID;
371
372 if (fontRecIDs[style] != INVALID_FONT_REC_ID) { // exact match
373 return fontRecIDs[style];
374 }
375 // look for a matching bold
376 style = (SkTypeface::Style)(style ^ SkTypeface::kItalic);
377 if (fontRecIDs[style] != INVALID_FONT_REC_ID) {
378 return fontRecIDs[style];
379 }
380 // look for the plain
381 if (fontRecIDs[SkTypeface::kNormal] != INVALID_FONT_REC_ID) {
382 return fontRecIDs[SkTypeface::kNormal];
383 }
384 // look for anything
385 for (int i = 0; i < FamilyRec::FONT_STYLE_COUNT; i++) {
386 if (fontRecIDs[i] != INVALID_FONT_REC_ID) {
387 return fontRecIDs[i];
388 }
389 }
390 // should never get here, since the fontRecID list should not be empty
391 SkDEBUGFAIL("No valid fonts exist for this family");
392 return -1;
393}
394
395bool SkFontConfigInterfaceAndroid::matchFamilyName(const char familyName[],
396 SkTypeface::Style style,
397 FontIdentity* outFontIdentifier,
398 SkString* outFamilyName,
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000399 SkTypeface::Style* outStyle) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000400 // clip to legal style bits
401 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
402
403 bool exactNameMatch = false;
404
405 FamilyRecID familyRecID = INVALID_FAMILY_REC_ID;
406 if (NULL != familyName) {
djsollen@google.com2e08f192013-05-21 20:08:10 +0000407 SkAutoAsciiToLC tolc(familyName);
408 if (fFamilyNameDict.find(tolc.lc(), &familyRecID)) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000409 exactNameMatch = true;
410 }
411 } else {
412 familyRecID = fDefaultFamilyRecID;
413
414 }
415
416 if (INVALID_FAMILY_REC_ID == familyRecID) {
417 //TODO this ensures that we always return something
418 familyRecID = fDefaultFamilyRecID;
419 //return false;
420 }
421
422 FontRecID fontRecID = find_best_style(fFontFamilies[familyRecID], style);
423 FontRec& fontRec = fFonts[fontRecID];
424
425 if (NULL != outFontIdentifier) {
426 outFontIdentifier->fID = fontRecID;
427 outFontIdentifier->fTTCIndex = 0;
428 outFontIdentifier->fString.set(fontRec.fFileName);
429// outFontIdentifier->fStyle = fontRec.fStyle;
430 }
431
432 if (NULL != outFamilyName) {
433 if (exactNameMatch) {
434 outFamilyName->set(familyName);
435 } else {
436 // find familyName from list of names
437 const char* familyName = NULL;
djsollen@google.comab6eeb92013-05-21 17:15:27 +0000438 SkAssertResult(fFamilyNameDict.findKey(familyRecID, &familyName));
439 SkASSERT(familyName);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000440 outFamilyName->set(familyName);
441 }
442 }
443
444 if (NULL != outStyle) {
445 *outStyle = fontRec.fStyle;
446 }
447
448 return true;
449}
450
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000451SkStream* SkFontConfigInterfaceAndroid::openStream(const FontIdentity& identity) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000452 return SkStream::NewFromFile(identity.fString.c_str());
453}
454
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000455SkDataTable* SkFontConfigInterfaceAndroid::getFamilyNames() {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000456 SkTDArray<const char*> names;
457 SkTDArray<size_t> sizes;
458
459 SkTDict<FamilyRecID>::Iter iter(fFamilyNameDict);
460 const char* familyName = iter.next(NULL);
461 while(familyName != NULL) {
462 *names.append() = familyName;
463 *sizes.append() = strlen(familyName) + 1;
464
465 // move to the next familyName in the dictionary
466 familyName = iter.next(NULL);
467 }
468
469 return SkDataTable::NewCopyArrays((const void*const*)names.begin(),
470 sizes.begin(), names.count());
471}
472
473bool SkFontConfigInterfaceAndroid::matchFamilySet(const char inFamilyName[],
474 SkString* outFamilyName,
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000475 SkTArray<FontIdentity>*) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000476 return false;
477}
478
djsollen@google.com40078cb2013-05-24 20:31:57 +0000479static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
480 const FontRecID* fontRecID = (const FontRecID*)ctx;
481 FontRecID currFontRecID = ((FontConfigTypeface*)face)->getIdentity().fID;
482 return currFontRecID == *fontRecID;
483}
484
485SkTypeface* SkFontConfigInterfaceAndroid::getTypefaceForFontRec(FontRecID fontRecID) {
486 FontRec& fontRec = fFonts[fontRecID];
djsollen@google.combfae9d32013-05-21 16:53:50 +0000487 SkTypeface* face = fontRec.fTypeface.get();
488 if (!face) {
djsollen@google.com40078cb2013-05-24 20:31:57 +0000489 // look for it in the typeface cache
490 face = SkTypefaceCache::FindByProcAndRef(find_proc, &fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000491
492 // if it is not in the cache then create it
djsollen@google.com40078cb2013-05-24 20:31:57 +0000493 if (!face) {
494 const char* familyName = NULL;
495 SkAssertResult(fFamilyNameDict.findKey(fontRec.fFamilyRecID, &familyName));
496 SkASSERT(familyName);
497 face = SkTypeface::CreateFromName(familyName, fontRec.fStyle);
498 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000499
500 // store the result for subsequent lookups
501 fontRec.fTypeface = face;
502 }
503 SkASSERT(face);
504 return face;
505}
506
507bool SkFontConfigInterfaceAndroid::getFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
djsollen@google.com9a70f342013-06-25 18:07:45 +0000508 FallbackFontList* fallbackFontList = this->getCurrentLocaleFallbackFontList();
509 for (int i = 0; i < fallbackFontList->count(); i++) {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000510 FamilyRecID familyRecID = fallbackFontList->getAt(i);
511 FontRecID fontRecID = find_best_style(fFontFamilies[familyRecID], SkTypeface::kNormal);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000512 SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000513
514 SkPaint paint;
515 paint.setTypeface(face);
516 paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
517
518 uint16_t glyphID;
519 paint.textToGlyphs(&uni, sizeof(uni), &glyphID);
520 if (glyphID != 0) {
521 name->set(fFonts[fontRecID].fFileName);
522 return true;
523 }
524 }
525 return false;
526}
527
528SkTypeface* SkFontConfigInterfaceAndroid::getTypefaceForChar(SkUnichar uni,
529 SkTypeface::Style style,
530 SkPaintOptionsAndroid::FontVariant fontVariant) {
531 FontRecID fontRecID = find_best_style(fFontFamilies[fDefaultFamilyRecID], style);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000532 SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000533
534 SkPaintOptionsAndroid paintOptions;
535 paintOptions.setFontVariant(fontVariant);
536 paintOptions.setUseFontFallbacks(true);
537
538 SkPaint paint;
539 paint.setTypeface(face);
540 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
541 paint.setPaintOptionsAndroid(paintOptions);
542
543 SkAutoGlyphCache autoCache(paint, NULL, NULL);
544 SkGlyphCache* cache = autoCache.getCache();
545
546 SkScalerContext* ctx = cache->getScalerContext();
547 if (ctx) {
548 SkFontID fontID = ctx->findTypefaceIdForChar(uni);
549 return SkTypefaceCache::FindByID(fontID);
550 }
551 return NULL;
552}
553
djsollen@google.com9a70f342013-06-25 18:07:45 +0000554FallbackFontList* SkFontConfigInterfaceAndroid::getCurrentLocaleFallbackFontList() {
555 SkString locale = SkFontConfigParser::GetLocale();
556 if (NULL == fLocaleFallbackFontList || locale != fCachedLocale) {
557 fCachedLocale = locale;
558 fLocaleFallbackFontList = this->findFallbackFontList(locale);
559 }
560 return fLocaleFallbackFontList;
561}
562
djsollen@google.com40078cb2013-05-24 20:31:57 +0000563FallbackFontList* SkFontConfigInterfaceAndroid::findFallbackFontList(const SkLanguage& lang,
564 bool isOriginal) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000565 const SkString& langTag = lang.getTag();
566 if (langTag.isEmpty()) {
567 return &fDefaultFallbackList;
568 }
569
570 FallbackFontList* fallbackFontList;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000571 if (fFallbackFontDict.find(langTag.c_str(), langTag.size(), &fallbackFontList) ||
572 fFallbackFontAliasDict.find(langTag.c_str(), langTag.size(), &fallbackFontList)) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000573 return fallbackFontList;
574 }
575
576 // attempt a recursive fuzzy match
djsollen@google.combfae9d32013-05-21 16:53:50 +0000577 SkLanguage parent = lang.getParent();
djsollen@google.com40078cb2013-05-24 20:31:57 +0000578 fallbackFontList = findFallbackFontList(parent, false);
579
580 // cache the original lang so we don't have to do the recursion again.
581 if (isOriginal) {
582 DEBUG_FONT(("---- Created fallback list alias for \"%s\"", langTag.c_str()));
583 fFallbackFontAliasDict.set(langTag.c_str(), fallbackFontList);
584 }
585 return fallbackFontList;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000586}
587
588SkTypeface* SkFontConfigInterfaceAndroid::nextLogicalTypeface(SkFontID currFontID,
589 SkFontID origFontID,
590 const SkPaintOptionsAndroid& opts) {
591 // Skia does not support font fallback by default. This enables clients such
592 // as WebKit to customize their font selection. In any case, clients can use
593 // GetFallbackFamilyNameForChar() to get the fallback font for individual
594 // characters.
595 if (!opts.isUsingFontFallbacks()) {
596 return NULL;
597 }
598
djsollen@google.combfae9d32013-05-21 16:53:50 +0000599 FallbackFontList* currentFallbackList = findFallbackFontList(opts.getLanguage());
600 SkASSERT(currentFallbackList);
601
djsollen@google.comb27eba72013-09-06 12:59:50 +0000602 SkTypeface::Style origStyle = SkTypeface::kNormal;
603 const SkTypeface* origTypeface = SkTypefaceCache::FindByID(origFontID);
604 if (NULL != origTypeface) {
605 origStyle = origTypeface->style();
606 }
607
djsollen@google.combfae9d32013-05-21 16:53:50 +0000608 // we must convert currTypeface into a FontRecID
djsollen@google.come47e7d12013-06-06 21:25:09 +0000609 FontRecID currFontRecID = INVALID_FONT_REC_ID;
610 const SkTypeface* currTypeface = SkTypefaceCache::FindByID(currFontID);
611 // non-system fonts are not in the font cache so if we are asked to fallback
612 // for a non-system font we will start at the front of the chain.
djsollen@google.com29bf8622013-07-31 15:48:10 +0000613 if (NULL != currTypeface && currFontID != origFontID) {
djsollen@google.come47e7d12013-06-06 21:25:09 +0000614 currFontRecID = ((FontConfigTypeface*)currTypeface)->getIdentity().fID;
615 SkASSERT(INVALID_FONT_REC_ID != currFontRecID);
616 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000617
djsollen@google.comb27eba72013-09-06 12:59:50 +0000618 FamilyRecID currFamilyRecID = INVALID_FAMILY_REC_ID;
619 if (INVALID_FONT_REC_ID != currFontRecID) {
620 currFamilyRecID = fFonts[currFontRecID].fFamilyRecID;
621 }
622
djsollen@google.come47e7d12013-06-06 21:25:09 +0000623 // lookup the index next font in the chain
djsollen@google.comb27eba72013-09-06 12:59:50 +0000624 int currFallbackFontIndex = currentFallbackList->find(currFamilyRecID);
djsollen@google.come47e7d12013-06-06 21:25:09 +0000625 // We add 1 to the returned index for 2 reasons: (1) if find succeeds it moves
626 // our index to the next entry in the list; (2) if find() fails it returns
627 // -1 and incrementing it will set our starting index to 0 (the head of the list)
djsollen@google.combfae9d32013-05-21 16:53:50 +0000628 int nextFallbackFontIndex = currFallbackFontIndex + 1;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000629
djsollen@google.com40078cb2013-05-24 20:31:57 +0000630 if(nextFallbackFontIndex >= currentFallbackList->count()) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000631 return NULL;
632 }
633
634 // If a rec object is set to prefer "kDefault_Variant" it means they have no preference
635 // In this case, we set the value to "kCompact_Variant"
636 SkPaintOptionsAndroid::FontVariant variant = opts.getFontVariant();
637 if (variant == SkPaintOptionsAndroid::kDefault_Variant) {
638 variant = SkPaintOptionsAndroid::kCompact_Variant;
639 }
640
641 int32_t acceptedVariants = SkPaintOptionsAndroid::kDefault_Variant | variant;
642
643 SkTypeface* nextLogicalTypeface = 0;
644 while (nextFallbackFontIndex < currentFallbackList->count()) {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000645 FamilyRecID familyRecID = currentFallbackList->getAt(nextFallbackFontIndex);
646 if ((fFontFamilies[familyRecID].fPaintOptions.getFontVariant() & acceptedVariants) != 0) {
647 FontRecID matchedFont = find_best_style(fFontFamilies[familyRecID], origStyle);
648 nextLogicalTypeface = this->getTypefaceForFontRec(matchedFont);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000649 break;
650 }
651 nextFallbackFontIndex++;
652 }
653
654 DEBUG_FONT(("---- nextLogicalFont: currFontID=%d, origFontID=%d, currRecID=%d, "
djsollen@google.com40078cb2013-05-24 20:31:57 +0000655 "lang=%s, variant=%d, nextFallbackIndex[%d,%d] => nextLogicalTypeface=%d",
djsollen@google.combfae9d32013-05-21 16:53:50 +0000656 currFontID, origFontID, currFontRecID, opts.getLanguage().getTag().c_str(),
djsollen@google.com40078cb2013-05-24 20:31:57 +0000657 variant, nextFallbackFontIndex, currentFallbackList->getAt(nextFallbackFontIndex),
djsollen@google.combfae9d32013-05-21 16:53:50 +0000658 (nextLogicalTypeface) ? nextLogicalTypeface->uniqueID() : 0));
659 return SkSafeRef(nextLogicalTypeface);
660}
661
662///////////////////////////////////////////////////////////////////////////////
663
664bool SkGetFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
665 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
666 return fontConfig->getFallbackFamilyNameForChar(uni, name);
667}
668
669void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf,
670 const char* fontsdir) {
671 gTestMainConfigFile = mainconf;
672 gTestFallbackConfigFile = fallbackconf;
673 gTestFontFilePrefix = fontsdir;
674 SkASSERT(gTestMainConfigFile);
675 SkASSERT(gTestFallbackConfigFile);
676 SkASSERT(gTestFontFilePrefix);
677 SkDEBUGF(("Use Test Config File Main %s, Fallback %s, Font Dir %s",
678 gTestMainConfigFile, gTestFallbackConfigFile, gTestFontFilePrefix));
679}
680
681SkTypeface* SkAndroidNextLogicalTypeface(SkFontID currFontID, SkFontID origFontID,
682 const SkPaintOptionsAndroid& options) {
683 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
684 return fontConfig->nextLogicalTypeface(currFontID, origFontID, options);
685
686}
687
688///////////////////////////////////////////////////////////////////////////////
689
djsollen@google.com40078cb2013-05-24 20:31:57 +0000690#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
691
692struct HB_UnicodeMapping {
693 // TODO: when the WebView no longer needs harfbuzz_old, remove
694 HB_Script script_old;
695 hb_script_t script;
696 const SkUnichar unicode;
697};
698
699/*
700 * The following scripts are not complex fonts and we do not expect them to be parsed by this table
701 * HB_SCRIPT_COMMON,
702 * HB_SCRIPT_GREEK,
703 * HB_SCRIPT_CYRILLIC,
704 * HB_SCRIPT_HANGUL
705 * HB_SCRIPT_INHERITED
706 */
707
708/* Harfbuzz (old) is missing a number of scripts in its table. For these,
709 * we include a value which can never happen. We won't get complex script
710 * shaping in these cases, but the library wouldn't know how to shape
711 * them anyway. */
712#define HB_Script_Unknown HB_ScriptCount
713
714static HB_UnicodeMapping HB_UnicodeMappingArray[] = {
715 {HB_Script_Armenian, HB_SCRIPT_ARMENIAN, 0x0531},
716 {HB_Script_Hebrew, HB_SCRIPT_HEBREW, 0x0591},
717 {HB_Script_Arabic, HB_SCRIPT_ARABIC, 0x0600},
718 {HB_Script_Syriac, HB_SCRIPT_SYRIAC, 0x0710},
719 {HB_Script_Thaana, HB_SCRIPT_THAANA, 0x0780},
720 {HB_Script_Nko, HB_SCRIPT_NKO, 0x07C0},
721 {HB_Script_Devanagari, HB_SCRIPT_DEVANAGARI, 0x0901},
722 {HB_Script_Bengali, HB_SCRIPT_BENGALI, 0x0981},
723 {HB_Script_Gurmukhi, HB_SCRIPT_GURMUKHI, 0x0A10},
724 {HB_Script_Gujarati, HB_SCRIPT_GUJARATI, 0x0A90},
725 {HB_Script_Oriya, HB_SCRIPT_ORIYA, 0x0B10},
726 {HB_Script_Tamil, HB_SCRIPT_TAMIL, 0x0B82},
727 {HB_Script_Telugu, HB_SCRIPT_TELUGU, 0x0C10},
728 {HB_Script_Kannada, HB_SCRIPT_KANNADA, 0x0C90},
729 {HB_Script_Malayalam, HB_SCRIPT_MALAYALAM, 0x0D10},
730 {HB_Script_Sinhala, HB_SCRIPT_SINHALA, 0x0D90},
731 {HB_Script_Thai, HB_SCRIPT_THAI, 0x0E01},
732 {HB_Script_Lao, HB_SCRIPT_LAO, 0x0E81},
733 {HB_Script_Tibetan, HB_SCRIPT_TIBETAN, 0x0F00},
734 {HB_Script_Myanmar, HB_SCRIPT_MYANMAR, 0x1000},
735 {HB_Script_Georgian, HB_SCRIPT_GEORGIAN, 0x10A0},
736 {HB_Script_Unknown, HB_SCRIPT_ETHIOPIC, 0x1200},
737 {HB_Script_Unknown, HB_SCRIPT_CHEROKEE, 0x13A0},
738 {HB_Script_Ogham, HB_SCRIPT_OGHAM, 0x1680},
739 {HB_Script_Runic, HB_SCRIPT_RUNIC, 0x16A0},
740 {HB_Script_Khmer, HB_SCRIPT_KHMER, 0x1780},
741 {HB_Script_Unknown, HB_SCRIPT_TAI_LE, 0x1950},
742 {HB_Script_Unknown, HB_SCRIPT_NEW_TAI_LUE, 0x1980},
743 {HB_Script_Unknown, HB_SCRIPT_TAI_THAM, 0x1A20},
744 {HB_Script_Unknown, HB_SCRIPT_CHAM, 0xAA00},
745};
746
747static hb_script_t getHBScriptFromHBScriptOld(HB_Script script_old) {
748 hb_script_t script = HB_SCRIPT_INVALID;
749 int numSupportedFonts = sizeof(HB_UnicodeMappingArray) / sizeof(HB_UnicodeMapping);
750 for (int i = 0; i < numSupportedFonts; i++) {
751 if (script_old == HB_UnicodeMappingArray[i].script_old) {
752 script = HB_UnicodeMappingArray[i].script;
753 break;
754 }
755 }
756 return script;
757}
758
759// returns 0 for "Not Found"
760static SkUnichar getUnicodeFromHBScript(hb_script_t script) {
761 SkUnichar unichar = 0;
762 int numSupportedFonts = sizeof(HB_UnicodeMappingArray) / sizeof(HB_UnicodeMapping);
763 for (int i = 0; i < numSupportedFonts; i++) {
764 if (script == HB_UnicodeMappingArray[i].script) {
765 unichar = HB_UnicodeMappingArray[i].unicode;
766 break;
767 }
768 }
769 return unichar;
770}
771
772struct TypefaceLookupStruct {
773 hb_script_t script;
774 SkTypeface::Style style;
775 SkPaintOptionsAndroid::FontVariant fontVariant;
776 SkTypeface* typeface;
777};
778
779SK_DECLARE_STATIC_MUTEX(gTypefaceTableMutex); // This is the mutex for gTypefaceTable
780static SkTDArray<TypefaceLookupStruct> gTypefaceTable; // This is protected by gTypefaceTableMutex
781
782static int typefaceLookupCompare(const TypefaceLookupStruct& first,
783 const TypefaceLookupStruct& second) {
784 if (first.script != second.script) {
785 return (first.script > second.script) ? 1 : -1;
786 }
787 if (first.style != second.style) {
788 return (first.style > second.style) ? 1 : -1;
789 }
790 if (first.fontVariant != second.fontVariant) {
791 return (first.fontVariant > second.fontVariant) ? 1 : -1;
792 }
793 return 0;
794}
795
796SkTypeface* SkCreateTypefaceForScriptNG(hb_script_t script, SkTypeface::Style style,
797 SkPaintOptionsAndroid::FontVariant fontVariant) {
798 SkAutoMutexAcquire ac(gTypefaceTableMutex);
799
800 TypefaceLookupStruct key;
801 key.script = script;
802 key.style = style;
803 key.fontVariant = fontVariant;
804
805 int index = SkTSearch<TypefaceLookupStruct>(
806 (const TypefaceLookupStruct*) gTypefaceTable.begin(),
807 gTypefaceTable.count(), key, sizeof(TypefaceLookupStruct),
808 typefaceLookupCompare);
809
810 SkTypeface* retTypeface = NULL;
811 if (index >= 0) {
812 retTypeface = gTypefaceTable[index].typeface;
813 }
814 else {
815 SkUnichar unichar = getUnicodeFromHBScript(script);
816 if (!unichar) {
817 return NULL;
818 }
819
820 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
821 retTypeface = fontConfig->getTypefaceForChar(unichar, style, fontVariant);
822
823 // add to the lookup table
824 key.typeface = retTypeface;
825 *gTypefaceTable.insert(~index) = key;
826 }
827
828 // we ref(), the caller is expected to unref when they are done
829 return SkSafeRef(retTypeface);
830}
831
832SkTypeface* SkCreateTypefaceForScript(HB_Script script, SkTypeface::Style style,
833 SkPaintOptionsAndroid::FontVariant fontVariant) {
834 return SkCreateTypefaceForScriptNG(getHBScriptFromHBScriptOld(script), style, fontVariant);
835}
836
837#endif
838
839///////////////////////////////////////////////////////////////////////////////
840
djsollen@google.combfae9d32013-05-21 16:53:50 +0000841SkFontMgr* SkFontMgr::Factory() {
842 return NULL;
843}