blob: 738b11434e348953f27dc9b86a570e8cd6407e8b [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;
58 SkPaintOptionsAndroid fPaintOptions;
59 bool fIsFallbackFont;
60 bool fIsValid;
djsollen@google.com40078cb2013-05-24 20:31:57 +000061 FamilyRecID fFamilyRecID;
djsollen@google.combfae9d32013-05-21 16:53:50 +000062};
63
djsollen@google.combfae9d32013-05-21 16:53:50 +000064struct FamilyRec {
65 FamilyRec() {
66 memset(fFontRecID, INVALID_FONT_REC_ID, sizeof(fFontRecID));
67 }
68
69 static const int FONT_STYLE_COUNT = 4;
70 FontRecID fFontRecID[FONT_STYLE_COUNT];
71};
72
djsollen@google.combfae9d32013-05-21 16:53:50 +000073
74typedef SkTDArray<FontRecID> FallbackFontList;
75
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:
108 void addFallbackFont(FontRecID 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);
186 familyNameDict.set(tolc.lc(), familyRecID);
187}
188
189// Defined in SkFontHost_FreeType.cpp
190bool find_name_and_attributes(SkStream* stream, SkString* name,
191 SkTypeface::Style* style, bool* isFixedWidth);
192
193///////////////////////////////////////////////////////////////////////////////
194
195SkFontConfigInterfaceAndroid::SkFontConfigInterfaceAndroid(SkTDArray<FontFamily*>& fontFamilies) :
196 fFonts(fontFamilies.count()),
197 fFontFamilies(fontFamilies.count() / FamilyRec::FONT_STYLE_COUNT),
198 fFamilyNameDict(1024),
199 fDefaultFamilyRecID(INVALID_FAMILY_REC_ID),
djsollen@google.com40078cb2013-05-24 20:31:57 +0000200 fFallbackFontDict(128),
djsollen@google.com9a70f342013-06-25 18:07:45 +0000201 fFallbackFontAliasDict(128),
202 fLocaleFallbackFontList(NULL) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000203
204 for (int i = 0; i < fontFamilies.count(); ++i) {
205 FontFamily* family = fontFamilies[i];
206
207 // defer initializing the familyRec until we can be sure that at least
208 // one of it's children contains a valid font file
209 FamilyRec* familyRec = NULL;
210 FamilyRecID familyRecID = INVALID_FAMILY_REC_ID;
211
212 for (int j = 0; j < family->fFontFiles.count(); ++j) {
213 SkString filename;
214 get_path_for_sys_fonts(&filename, family->fFontFiles[j]->fFileName);
215
216 if (has_font(fFonts, filename)) {
217 SkDebugf("---- system font and fallback font files specify a duplicate "
218 "font %s, skipping the second occurrence", filename.c_str());
219 continue;
220 }
221
222 FontRec& fontRec = fFonts.push_back();
223 fontRec.fFileName = filename;
224 fontRec.fStyle = SkTypeface::kNormal;
225 fontRec.fPaintOptions = family->fFontFiles[j]->fPaintOptions;
226 fontRec.fIsFallbackFont = family->fIsFallbackFont;
227 fontRec.fIsValid = false;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000228 fontRec.fFamilyRecID = familyRecID;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000229
230 const FontRecID fontRecID = fFonts.count() - 1;
231
232 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
233 if (stream.get() != NULL) {
234 bool isFixedWidth;
235 SkString name;
236 fontRec.fIsValid = find_name_and_attributes(stream.get(), &name,
237 &fontRec.fStyle, &isFixedWidth);
238 } else {
239 if (!fontRec.fIsFallbackFont) {
240 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
241 }
242 }
243
244 if (fontRec.fIsValid) {
245 DEBUG_FONT(("---- SystemFonts[%d][%d] fallback=%d file=%s",
246 i, fFonts.count() - 1, fontRec.fIsFallbackFont, filename.c_str()));
247 } else {
248 DEBUG_FONT(("---- SystemFonts[%d][%d] fallback=%d file=%s (INVALID)",
249 i, fFonts.count() - 1, fontRec.fIsFallbackFont, filename.c_str()));
250 continue;
251 }
252
253 // create a familyRec now that we know that at least one font in
254 // the family is valid
255 if (familyRec == NULL) {
256 familyRec = &fFontFamilies.push_back();
257 familyRecID = fFontFamilies.count() - 1;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000258 fontRec.fFamilyRecID = familyRecID;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000259 }
260
261 // add this font to the current familyRec
262 if (INVALID_FONT_REC_ID != familyRec->fFontRecID[fontRec.fStyle]) {
263 DEBUG_FONT(("Overwriting familyRec for style[%d] old,new:(%d,%d)",
264 fontRec.fStyle, familyRec->fFontRecID[fontRec.fStyle],
265 fontRecID));
266 }
267 familyRec->fFontRecID[fontRec.fStyle] = fontRecID;
268
269 // if this is a fallback font then add it to the appropriate fallback chains
270 if (fontRec.fIsFallbackFont) {
271 addFallbackFont(fontRecID);
272 }
273
274 // add the fallback file name to the name dictionary. This is needed
275 // by getFallbackFamilyNameForChar() so that fallback families can be
276 // requested by the filenames of the fonts they contain.
277 if (family->fIsFallbackFont && familyRec) {
278 insert_into_name_dict(fFamilyNameDict, fontRec.fFileName.c_str(), familyRecID);
279 }
280 }
281
282 // add the names that map to this family to the dictionary for easy lookup
283 if (familyRec && !family->fIsFallbackFont) {
284 SkTDArray<const char*> names = family->fNames;
285 if (names.isEmpty()) {
286 SkDEBUGFAIL("ERROR: non-fallback font with no name");
287 continue;
288 }
289
290 for (int i = 0; i < names.count(); i++) {
291 insert_into_name_dict(fFamilyNameDict, names[i], familyRecID);
292 }
293 }
294
295 }
296
297 DEBUG_FONT(("---- We have %d system fonts", fFonts.count()));
298
299 if (fFontFamilies.count() > 0) {
300 fDefaultFamilyRecID = 0;
301 }
302
303 // scans the default fallback font chain, adding every entry to every other
304 // fallback font chain to which it does not belong. this results in every
305 // language-specific fallback font chain having all of its fallback fonts at
306 // the front of the chain, and everything else at the end.
307 FallbackFontList* fallbackList;
308 SkTDict<FallbackFontList*>::Iter iter(fFallbackFontDict);
309 const char* fallbackLang = iter.next(&fallbackList);
310 while(fallbackLang != NULL) {
311 for (int i = 0; i < fDefaultFallbackList.count(); i++) {
312 FontRecID fontRecID = fDefaultFallbackList[i];
313 const SkString& fontLang = fFonts[fontRecID].fPaintOptions.getLanguage().getTag();
314 if (strcmp(fallbackLang, fontLang.c_str()) != 0) {
315 fallbackList->push(fontRecID);
316 }
317 }
318 // move to the next fallback list in the dictionary
319 fallbackLang = iter.next(&fallbackList);
320 }
321}
322
323SkFontConfigInterfaceAndroid::~SkFontConfigInterfaceAndroid() {
324 // iterate through and cleanup fFallbackFontDict
325 SkTDict<FallbackFontList*>::Iter iter(fFallbackFontDict);
326 FallbackFontList* fallbackList;
327 while(iter.next(&fallbackList) != NULL) {
328 SkDELETE(fallbackList);
329 }
330}
331
332void SkFontConfigInterfaceAndroid::addFallbackFont(FontRecID fontRecID) {
333 SkASSERT(fontRecID < fFonts.count());
334 const FontRec& fontRec = fFonts[fontRecID];
335 SkASSERT(fontRec.fIsFallbackFont);
336
337 // add to the default fallback list
338 fDefaultFallbackList.push(fontRecID);
339
340 // stop here if it is the default language tag
341 const SkString& languageTag = fontRec.fPaintOptions.getLanguage().getTag();
342 if (languageTag.isEmpty()) {
343 return;
344 }
345
346 // add to the appropriate language's custom fallback list
347 FallbackFontList* customList = NULL;
348 if (!fFallbackFontDict.find(languageTag.c_str(), &customList)) {
349 DEBUG_FONT(("---- Created fallback list for \"%s\"", languageTag.c_str()));
350 customList = SkNEW(FallbackFontList);
351 fFallbackFontDict.set(languageTag.c_str(), customList);
352 }
353 SkASSERT(customList != NULL);
354 customList->push(fontRecID);
355}
356
357
358static FontRecID find_best_style(const FamilyRec& family, SkTypeface::Style style) {
359
360 const FontRecID* fontRecIDs = family.fFontRecID;
361
362 if (fontRecIDs[style] != INVALID_FONT_REC_ID) { // exact match
363 return fontRecIDs[style];
364 }
365 // look for a matching bold
366 style = (SkTypeface::Style)(style ^ SkTypeface::kItalic);
367 if (fontRecIDs[style] != INVALID_FONT_REC_ID) {
368 return fontRecIDs[style];
369 }
370 // look for the plain
371 if (fontRecIDs[SkTypeface::kNormal] != INVALID_FONT_REC_ID) {
372 return fontRecIDs[SkTypeface::kNormal];
373 }
374 // look for anything
375 for (int i = 0; i < FamilyRec::FONT_STYLE_COUNT; i++) {
376 if (fontRecIDs[i] != INVALID_FONT_REC_ID) {
377 return fontRecIDs[i];
378 }
379 }
380 // should never get here, since the fontRecID list should not be empty
381 SkDEBUGFAIL("No valid fonts exist for this family");
382 return -1;
383}
384
385bool SkFontConfigInterfaceAndroid::matchFamilyName(const char familyName[],
386 SkTypeface::Style style,
387 FontIdentity* outFontIdentifier,
388 SkString* outFamilyName,
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000389 SkTypeface::Style* outStyle) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000390 // clip to legal style bits
391 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
392
393 bool exactNameMatch = false;
394
395 FamilyRecID familyRecID = INVALID_FAMILY_REC_ID;
396 if (NULL != familyName) {
djsollen@google.com2e08f192013-05-21 20:08:10 +0000397 SkAutoAsciiToLC tolc(familyName);
398 if (fFamilyNameDict.find(tolc.lc(), &familyRecID)) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000399 exactNameMatch = true;
400 }
401 } else {
402 familyRecID = fDefaultFamilyRecID;
403
404 }
405
406 if (INVALID_FAMILY_REC_ID == familyRecID) {
407 //TODO this ensures that we always return something
408 familyRecID = fDefaultFamilyRecID;
409 //return false;
410 }
411
412 FontRecID fontRecID = find_best_style(fFontFamilies[familyRecID], style);
413 FontRec& fontRec = fFonts[fontRecID];
414
415 if (NULL != outFontIdentifier) {
416 outFontIdentifier->fID = fontRecID;
417 outFontIdentifier->fTTCIndex = 0;
418 outFontIdentifier->fString.set(fontRec.fFileName);
419// outFontIdentifier->fStyle = fontRec.fStyle;
420 }
421
422 if (NULL != outFamilyName) {
423 if (exactNameMatch) {
424 outFamilyName->set(familyName);
425 } else {
426 // find familyName from list of names
427 const char* familyName = NULL;
djsollen@google.comab6eeb92013-05-21 17:15:27 +0000428 SkAssertResult(fFamilyNameDict.findKey(familyRecID, &familyName));
429 SkASSERT(familyName);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000430 outFamilyName->set(familyName);
431 }
432 }
433
434 if (NULL != outStyle) {
435 *outStyle = fontRec.fStyle;
436 }
437
438 return true;
439}
440
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000441SkStream* SkFontConfigInterfaceAndroid::openStream(const FontIdentity& identity) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000442 return SkStream::NewFromFile(identity.fString.c_str());
443}
444
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000445SkDataTable* SkFontConfigInterfaceAndroid::getFamilyNames() {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000446 SkTDArray<const char*> names;
447 SkTDArray<size_t> sizes;
448
449 SkTDict<FamilyRecID>::Iter iter(fFamilyNameDict);
450 const char* familyName = iter.next(NULL);
451 while(familyName != NULL) {
452 *names.append() = familyName;
453 *sizes.append() = strlen(familyName) + 1;
454
455 // move to the next familyName in the dictionary
456 familyName = iter.next(NULL);
457 }
458
459 return SkDataTable::NewCopyArrays((const void*const*)names.begin(),
460 sizes.begin(), names.count());
461}
462
463bool SkFontConfigInterfaceAndroid::matchFamilySet(const char inFamilyName[],
464 SkString* outFamilyName,
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000465 SkTArray<FontIdentity>*) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000466 return false;
467}
468
djsollen@google.com40078cb2013-05-24 20:31:57 +0000469static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
470 const FontRecID* fontRecID = (const FontRecID*)ctx;
471 FontRecID currFontRecID = ((FontConfigTypeface*)face)->getIdentity().fID;
472 return currFontRecID == *fontRecID;
473}
474
475SkTypeface* SkFontConfigInterfaceAndroid::getTypefaceForFontRec(FontRecID fontRecID) {
476 FontRec& fontRec = fFonts[fontRecID];
djsollen@google.combfae9d32013-05-21 16:53:50 +0000477 SkTypeface* face = fontRec.fTypeface.get();
478 if (!face) {
djsollen@google.com40078cb2013-05-24 20:31:57 +0000479 // look for it in the typeface cache
480 face = SkTypefaceCache::FindByProcAndRef(find_proc, &fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000481
482 // if it is not in the cache then create it
djsollen@google.com40078cb2013-05-24 20:31:57 +0000483 if (!face) {
484 const char* familyName = NULL;
485 SkAssertResult(fFamilyNameDict.findKey(fontRec.fFamilyRecID, &familyName));
486 SkASSERT(familyName);
487 face = SkTypeface::CreateFromName(familyName, fontRec.fStyle);
488 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000489
490 // store the result for subsequent lookups
491 fontRec.fTypeface = face;
492 }
493 SkASSERT(face);
494 return face;
495}
496
497bool SkFontConfigInterfaceAndroid::getFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
djsollen@google.com9a70f342013-06-25 18:07:45 +0000498 FallbackFontList* fallbackFontList = this->getCurrentLocaleFallbackFontList();
499 for (int i = 0; i < fallbackFontList->count(); i++) {
500 FontRecID fontRecID = fallbackFontList->getAt(i);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000501 SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000502
503 SkPaint paint;
504 paint.setTypeface(face);
505 paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
506
507 uint16_t glyphID;
508 paint.textToGlyphs(&uni, sizeof(uni), &glyphID);
509 if (glyphID != 0) {
510 name->set(fFonts[fontRecID].fFileName);
511 return true;
512 }
513 }
514 return false;
515}
516
517SkTypeface* SkFontConfigInterfaceAndroid::getTypefaceForChar(SkUnichar uni,
518 SkTypeface::Style style,
519 SkPaintOptionsAndroid::FontVariant fontVariant) {
520 FontRecID fontRecID = find_best_style(fFontFamilies[fDefaultFamilyRecID], style);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000521 SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000522
523 SkPaintOptionsAndroid paintOptions;
524 paintOptions.setFontVariant(fontVariant);
525 paintOptions.setUseFontFallbacks(true);
526
527 SkPaint paint;
528 paint.setTypeface(face);
529 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
530 paint.setPaintOptionsAndroid(paintOptions);
531
532 SkAutoGlyphCache autoCache(paint, NULL, NULL);
533 SkGlyphCache* cache = autoCache.getCache();
534
535 SkScalerContext* ctx = cache->getScalerContext();
536 if (ctx) {
537 SkFontID fontID = ctx->findTypefaceIdForChar(uni);
538 return SkTypefaceCache::FindByID(fontID);
539 }
540 return NULL;
541}
542
djsollen@google.com9a70f342013-06-25 18:07:45 +0000543FallbackFontList* SkFontConfigInterfaceAndroid::getCurrentLocaleFallbackFontList() {
544 SkString locale = SkFontConfigParser::GetLocale();
545 if (NULL == fLocaleFallbackFontList || locale != fCachedLocale) {
546 fCachedLocale = locale;
547 fLocaleFallbackFontList = this->findFallbackFontList(locale);
548 }
549 return fLocaleFallbackFontList;
550}
551
djsollen@google.com40078cb2013-05-24 20:31:57 +0000552FallbackFontList* SkFontConfigInterfaceAndroid::findFallbackFontList(const SkLanguage& lang,
553 bool isOriginal) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000554 const SkString& langTag = lang.getTag();
555 if (langTag.isEmpty()) {
556 return &fDefaultFallbackList;
557 }
558
559 FallbackFontList* fallbackFontList;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000560 if (fFallbackFontDict.find(langTag.c_str(), langTag.size(), &fallbackFontList) ||
561 fFallbackFontAliasDict.find(langTag.c_str(), langTag.size(), &fallbackFontList)) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000562 return fallbackFontList;
563 }
564
565 // attempt a recursive fuzzy match
djsollen@google.combfae9d32013-05-21 16:53:50 +0000566 SkLanguage parent = lang.getParent();
djsollen@google.com40078cb2013-05-24 20:31:57 +0000567 fallbackFontList = findFallbackFontList(parent, false);
568
569 // cache the original lang so we don't have to do the recursion again.
570 if (isOriginal) {
571 DEBUG_FONT(("---- Created fallback list alias for \"%s\"", langTag.c_str()));
572 fFallbackFontAliasDict.set(langTag.c_str(), fallbackFontList);
573 }
574 return fallbackFontList;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000575}
576
577SkTypeface* SkFontConfigInterfaceAndroid::nextLogicalTypeface(SkFontID currFontID,
578 SkFontID origFontID,
579 const SkPaintOptionsAndroid& opts) {
580 // Skia does not support font fallback by default. This enables clients such
581 // as WebKit to customize their font selection. In any case, clients can use
582 // GetFallbackFamilyNameForChar() to get the fallback font for individual
583 // characters.
584 if (!opts.isUsingFontFallbacks()) {
585 return NULL;
586 }
587
djsollen@google.combfae9d32013-05-21 16:53:50 +0000588 FallbackFontList* currentFallbackList = findFallbackFontList(opts.getLanguage());
589 SkASSERT(currentFallbackList);
590
djsollen@google.combfae9d32013-05-21 16:53:50 +0000591 // we must convert currTypeface into a FontRecID
djsollen@google.come47e7d12013-06-06 21:25:09 +0000592 FontRecID currFontRecID = INVALID_FONT_REC_ID;
593 const SkTypeface* currTypeface = SkTypefaceCache::FindByID(currFontID);
594 // non-system fonts are not in the font cache so if we are asked to fallback
595 // for a non-system font we will start at the front of the chain.
596 if (NULL != currTypeface && currFontID == origFontID) {
597 currFontRecID = ((FontConfigTypeface*)currTypeface)->getIdentity().fID;
598 SkASSERT(INVALID_FONT_REC_ID != currFontRecID);
599 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000600
djsollen@google.come47e7d12013-06-06 21:25:09 +0000601 // lookup the index next font in the chain
djsollen@google.combfae9d32013-05-21 16:53:50 +0000602 int currFallbackFontIndex = currentFallbackList->find(currFontRecID);
djsollen@google.come47e7d12013-06-06 21:25:09 +0000603 // We add 1 to the returned index for 2 reasons: (1) if find succeeds it moves
604 // our index to the next entry in the list; (2) if find() fails it returns
605 // -1 and incrementing it will set our starting index to 0 (the head of the list)
djsollen@google.combfae9d32013-05-21 16:53:50 +0000606 int nextFallbackFontIndex = currFallbackFontIndex + 1;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000607
djsollen@google.com40078cb2013-05-24 20:31:57 +0000608 if(nextFallbackFontIndex >= currentFallbackList->count()) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000609 return NULL;
610 }
611
612 // If a rec object is set to prefer "kDefault_Variant" it means they have no preference
613 // In this case, we set the value to "kCompact_Variant"
614 SkPaintOptionsAndroid::FontVariant variant = opts.getFontVariant();
615 if (variant == SkPaintOptionsAndroid::kDefault_Variant) {
616 variant = SkPaintOptionsAndroid::kCompact_Variant;
617 }
618
619 int32_t acceptedVariants = SkPaintOptionsAndroid::kDefault_Variant | variant;
620
621 SkTypeface* nextLogicalTypeface = 0;
622 while (nextFallbackFontIndex < currentFallbackList->count()) {
623 FontRecID fontRecID = currentFallbackList->getAt(nextFallbackFontIndex);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000624 if ((fFonts[fontRecID].fPaintOptions.getFontVariant() & acceptedVariants) != 0) {
625 nextLogicalTypeface = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000626 break;
627 }
628 nextFallbackFontIndex++;
629 }
630
631 DEBUG_FONT(("---- nextLogicalFont: currFontID=%d, origFontID=%d, currRecID=%d, "
djsollen@google.com40078cb2013-05-24 20:31:57 +0000632 "lang=%s, variant=%d, nextFallbackIndex[%d,%d] => nextLogicalTypeface=%d",
djsollen@google.combfae9d32013-05-21 16:53:50 +0000633 currFontID, origFontID, currFontRecID, opts.getLanguage().getTag().c_str(),
djsollen@google.com40078cb2013-05-24 20:31:57 +0000634 variant, nextFallbackFontIndex, currentFallbackList->getAt(nextFallbackFontIndex),
djsollen@google.combfae9d32013-05-21 16:53:50 +0000635 (nextLogicalTypeface) ? nextLogicalTypeface->uniqueID() : 0));
636 return SkSafeRef(nextLogicalTypeface);
637}
638
639///////////////////////////////////////////////////////////////////////////////
640
641bool SkGetFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
642 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
643 return fontConfig->getFallbackFamilyNameForChar(uni, name);
644}
645
646void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf,
647 const char* fontsdir) {
648 gTestMainConfigFile = mainconf;
649 gTestFallbackConfigFile = fallbackconf;
650 gTestFontFilePrefix = fontsdir;
651 SkASSERT(gTestMainConfigFile);
652 SkASSERT(gTestFallbackConfigFile);
653 SkASSERT(gTestFontFilePrefix);
654 SkDEBUGF(("Use Test Config File Main %s, Fallback %s, Font Dir %s",
655 gTestMainConfigFile, gTestFallbackConfigFile, gTestFontFilePrefix));
656}
657
658SkTypeface* SkAndroidNextLogicalTypeface(SkFontID currFontID, SkFontID origFontID,
659 const SkPaintOptionsAndroid& options) {
660 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
661 return fontConfig->nextLogicalTypeface(currFontID, origFontID, options);
662
663}
664
665///////////////////////////////////////////////////////////////////////////////
666
djsollen@google.com40078cb2013-05-24 20:31:57 +0000667#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
668
669struct HB_UnicodeMapping {
670 // TODO: when the WebView no longer needs harfbuzz_old, remove
671 HB_Script script_old;
672 hb_script_t script;
673 const SkUnichar unicode;
674};
675
676/*
677 * The following scripts are not complex fonts and we do not expect them to be parsed by this table
678 * HB_SCRIPT_COMMON,
679 * HB_SCRIPT_GREEK,
680 * HB_SCRIPT_CYRILLIC,
681 * HB_SCRIPT_HANGUL
682 * HB_SCRIPT_INHERITED
683 */
684
685/* Harfbuzz (old) is missing a number of scripts in its table. For these,
686 * we include a value which can never happen. We won't get complex script
687 * shaping in these cases, but the library wouldn't know how to shape
688 * them anyway. */
689#define HB_Script_Unknown HB_ScriptCount
690
691static HB_UnicodeMapping HB_UnicodeMappingArray[] = {
692 {HB_Script_Armenian, HB_SCRIPT_ARMENIAN, 0x0531},
693 {HB_Script_Hebrew, HB_SCRIPT_HEBREW, 0x0591},
694 {HB_Script_Arabic, HB_SCRIPT_ARABIC, 0x0600},
695 {HB_Script_Syriac, HB_SCRIPT_SYRIAC, 0x0710},
696 {HB_Script_Thaana, HB_SCRIPT_THAANA, 0x0780},
697 {HB_Script_Nko, HB_SCRIPT_NKO, 0x07C0},
698 {HB_Script_Devanagari, HB_SCRIPT_DEVANAGARI, 0x0901},
699 {HB_Script_Bengali, HB_SCRIPT_BENGALI, 0x0981},
700 {HB_Script_Gurmukhi, HB_SCRIPT_GURMUKHI, 0x0A10},
701 {HB_Script_Gujarati, HB_SCRIPT_GUJARATI, 0x0A90},
702 {HB_Script_Oriya, HB_SCRIPT_ORIYA, 0x0B10},
703 {HB_Script_Tamil, HB_SCRIPT_TAMIL, 0x0B82},
704 {HB_Script_Telugu, HB_SCRIPT_TELUGU, 0x0C10},
705 {HB_Script_Kannada, HB_SCRIPT_KANNADA, 0x0C90},
706 {HB_Script_Malayalam, HB_SCRIPT_MALAYALAM, 0x0D10},
707 {HB_Script_Sinhala, HB_SCRIPT_SINHALA, 0x0D90},
708 {HB_Script_Thai, HB_SCRIPT_THAI, 0x0E01},
709 {HB_Script_Lao, HB_SCRIPT_LAO, 0x0E81},
710 {HB_Script_Tibetan, HB_SCRIPT_TIBETAN, 0x0F00},
711 {HB_Script_Myanmar, HB_SCRIPT_MYANMAR, 0x1000},
712 {HB_Script_Georgian, HB_SCRIPT_GEORGIAN, 0x10A0},
713 {HB_Script_Unknown, HB_SCRIPT_ETHIOPIC, 0x1200},
714 {HB_Script_Unknown, HB_SCRIPT_CHEROKEE, 0x13A0},
715 {HB_Script_Ogham, HB_SCRIPT_OGHAM, 0x1680},
716 {HB_Script_Runic, HB_SCRIPT_RUNIC, 0x16A0},
717 {HB_Script_Khmer, HB_SCRIPT_KHMER, 0x1780},
718 {HB_Script_Unknown, HB_SCRIPT_TAI_LE, 0x1950},
719 {HB_Script_Unknown, HB_SCRIPT_NEW_TAI_LUE, 0x1980},
720 {HB_Script_Unknown, HB_SCRIPT_TAI_THAM, 0x1A20},
721 {HB_Script_Unknown, HB_SCRIPT_CHAM, 0xAA00},
722};
723
724static hb_script_t getHBScriptFromHBScriptOld(HB_Script script_old) {
725 hb_script_t script = HB_SCRIPT_INVALID;
726 int numSupportedFonts = sizeof(HB_UnicodeMappingArray) / sizeof(HB_UnicodeMapping);
727 for (int i = 0; i < numSupportedFonts; i++) {
728 if (script_old == HB_UnicodeMappingArray[i].script_old) {
729 script = HB_UnicodeMappingArray[i].script;
730 break;
731 }
732 }
733 return script;
734}
735
736// returns 0 for "Not Found"
737static SkUnichar getUnicodeFromHBScript(hb_script_t script) {
738 SkUnichar unichar = 0;
739 int numSupportedFonts = sizeof(HB_UnicodeMappingArray) / sizeof(HB_UnicodeMapping);
740 for (int i = 0; i < numSupportedFonts; i++) {
741 if (script == HB_UnicodeMappingArray[i].script) {
742 unichar = HB_UnicodeMappingArray[i].unicode;
743 break;
744 }
745 }
746 return unichar;
747}
748
749struct TypefaceLookupStruct {
750 hb_script_t script;
751 SkTypeface::Style style;
752 SkPaintOptionsAndroid::FontVariant fontVariant;
753 SkTypeface* typeface;
754};
755
756SK_DECLARE_STATIC_MUTEX(gTypefaceTableMutex); // This is the mutex for gTypefaceTable
757static SkTDArray<TypefaceLookupStruct> gTypefaceTable; // This is protected by gTypefaceTableMutex
758
759static int typefaceLookupCompare(const TypefaceLookupStruct& first,
760 const TypefaceLookupStruct& second) {
761 if (first.script != second.script) {
762 return (first.script > second.script) ? 1 : -1;
763 }
764 if (first.style != second.style) {
765 return (first.style > second.style) ? 1 : -1;
766 }
767 if (first.fontVariant != second.fontVariant) {
768 return (first.fontVariant > second.fontVariant) ? 1 : -1;
769 }
770 return 0;
771}
772
773SkTypeface* SkCreateTypefaceForScriptNG(hb_script_t script, SkTypeface::Style style,
774 SkPaintOptionsAndroid::FontVariant fontVariant) {
775 SkAutoMutexAcquire ac(gTypefaceTableMutex);
776
777 TypefaceLookupStruct key;
778 key.script = script;
779 key.style = style;
780 key.fontVariant = fontVariant;
781
782 int index = SkTSearch<TypefaceLookupStruct>(
783 (const TypefaceLookupStruct*) gTypefaceTable.begin(),
784 gTypefaceTable.count(), key, sizeof(TypefaceLookupStruct),
785 typefaceLookupCompare);
786
787 SkTypeface* retTypeface = NULL;
788 if (index >= 0) {
789 retTypeface = gTypefaceTable[index].typeface;
790 }
791 else {
792 SkUnichar unichar = getUnicodeFromHBScript(script);
793 if (!unichar) {
794 return NULL;
795 }
796
797 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
798 retTypeface = fontConfig->getTypefaceForChar(unichar, style, fontVariant);
799
800 // add to the lookup table
801 key.typeface = retTypeface;
802 *gTypefaceTable.insert(~index) = key;
803 }
804
805 // we ref(), the caller is expected to unref when they are done
806 return SkSafeRef(retTypeface);
807}
808
809SkTypeface* SkCreateTypefaceForScript(HB_Script script, SkTypeface::Style style,
810 SkPaintOptionsAndroid::FontVariant fontVariant) {
811 return SkCreateTypefaceForScriptNG(getHBScriptFromHBScriptOld(script), style, fontVariant);
812}
813
814#endif
815
816///////////////////////////////////////////////////////////////////////////////
817
djsollen@google.combfae9d32013-05-21 16:53:50 +0000818SkFontMgr* SkFontMgr::Factory() {
819 return NULL;
820}