blob: f56308dda94fab7624599ee5f767e5ac710158fd [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
djsollen@google.com1f584ed2013-09-19 12:08:40 +0000416 // If no matching family name is found then return false. This allows clients
417 // to be able to search for other fonts instead of forcing them to use the
418 // default font.
djsollen@google.combfae9d32013-05-21 16:53:50 +0000419 if (INVALID_FAMILY_REC_ID == familyRecID) {
djsollen@google.com1f584ed2013-09-19 12:08:40 +0000420 return false;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000421 }
422
423 FontRecID fontRecID = find_best_style(fFontFamilies[familyRecID], style);
424 FontRec& fontRec = fFonts[fontRecID];
425
426 if (NULL != outFontIdentifier) {
427 outFontIdentifier->fID = fontRecID;
428 outFontIdentifier->fTTCIndex = 0;
429 outFontIdentifier->fString.set(fontRec.fFileName);
430// outFontIdentifier->fStyle = fontRec.fStyle;
431 }
432
433 if (NULL != outFamilyName) {
434 if (exactNameMatch) {
435 outFamilyName->set(familyName);
436 } else {
437 // find familyName from list of names
438 const char* familyName = NULL;
djsollen@google.comab6eeb92013-05-21 17:15:27 +0000439 SkAssertResult(fFamilyNameDict.findKey(familyRecID, &familyName));
440 SkASSERT(familyName);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000441 outFamilyName->set(familyName);
442 }
443 }
444
445 if (NULL != outStyle) {
446 *outStyle = fontRec.fStyle;
447 }
448
449 return true;
450}
451
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000452SkStream* SkFontConfigInterfaceAndroid::openStream(const FontIdentity& identity) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000453 return SkStream::NewFromFile(identity.fString.c_str());
454}
455
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000456SkDataTable* SkFontConfigInterfaceAndroid::getFamilyNames() {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000457 SkTDArray<const char*> names;
458 SkTDArray<size_t> sizes;
459
460 SkTDict<FamilyRecID>::Iter iter(fFamilyNameDict);
461 const char* familyName = iter.next(NULL);
462 while(familyName != NULL) {
463 *names.append() = familyName;
464 *sizes.append() = strlen(familyName) + 1;
465
466 // move to the next familyName in the dictionary
467 familyName = iter.next(NULL);
468 }
469
470 return SkDataTable::NewCopyArrays((const void*const*)names.begin(),
471 sizes.begin(), names.count());
472}
473
474bool SkFontConfigInterfaceAndroid::matchFamilySet(const char inFamilyName[],
475 SkString* outFamilyName,
robertphillips@google.comb7457d02013-05-22 00:12:43 +0000476 SkTArray<FontIdentity>*) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000477 return false;
478}
479
djsollen@google.com40078cb2013-05-24 20:31:57 +0000480static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
481 const FontRecID* fontRecID = (const FontRecID*)ctx;
482 FontRecID currFontRecID = ((FontConfigTypeface*)face)->getIdentity().fID;
483 return currFontRecID == *fontRecID;
484}
485
486SkTypeface* SkFontConfigInterfaceAndroid::getTypefaceForFontRec(FontRecID fontRecID) {
487 FontRec& fontRec = fFonts[fontRecID];
djsollen@google.combfae9d32013-05-21 16:53:50 +0000488 SkTypeface* face = fontRec.fTypeface.get();
489 if (!face) {
djsollen@google.com40078cb2013-05-24 20:31:57 +0000490 // look for it in the typeface cache
491 face = SkTypefaceCache::FindByProcAndRef(find_proc, &fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000492
493 // if it is not in the cache then create it
djsollen@google.com40078cb2013-05-24 20:31:57 +0000494 if (!face) {
495 const char* familyName = NULL;
496 SkAssertResult(fFamilyNameDict.findKey(fontRec.fFamilyRecID, &familyName));
497 SkASSERT(familyName);
498 face = SkTypeface::CreateFromName(familyName, fontRec.fStyle);
499 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000500
501 // store the result for subsequent lookups
502 fontRec.fTypeface = face;
503 }
504 SkASSERT(face);
505 return face;
506}
507
508bool SkFontConfigInterfaceAndroid::getFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
djsollen@google.com9a70f342013-06-25 18:07:45 +0000509 FallbackFontList* fallbackFontList = this->getCurrentLocaleFallbackFontList();
510 for (int i = 0; i < fallbackFontList->count(); i++) {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000511 FamilyRecID familyRecID = fallbackFontList->getAt(i);
512 FontRecID fontRecID = find_best_style(fFontFamilies[familyRecID], SkTypeface::kNormal);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000513 SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000514
515 SkPaint paint;
516 paint.setTypeface(face);
517 paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
518
519 uint16_t glyphID;
520 paint.textToGlyphs(&uni, sizeof(uni), &glyphID);
521 if (glyphID != 0) {
522 name->set(fFonts[fontRecID].fFileName);
523 return true;
524 }
525 }
526 return false;
527}
528
529SkTypeface* SkFontConfigInterfaceAndroid::getTypefaceForChar(SkUnichar uni,
530 SkTypeface::Style style,
531 SkPaintOptionsAndroid::FontVariant fontVariant) {
532 FontRecID fontRecID = find_best_style(fFontFamilies[fDefaultFamilyRecID], style);
djsollen@google.com40078cb2013-05-24 20:31:57 +0000533 SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000534
535 SkPaintOptionsAndroid paintOptions;
536 paintOptions.setFontVariant(fontVariant);
537 paintOptions.setUseFontFallbacks(true);
538
539 SkPaint paint;
540 paint.setTypeface(face);
541 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
542 paint.setPaintOptionsAndroid(paintOptions);
543
544 SkAutoGlyphCache autoCache(paint, NULL, NULL);
545 SkGlyphCache* cache = autoCache.getCache();
546
547 SkScalerContext* ctx = cache->getScalerContext();
548 if (ctx) {
549 SkFontID fontID = ctx->findTypefaceIdForChar(uni);
550 return SkTypefaceCache::FindByID(fontID);
551 }
552 return NULL;
553}
554
djsollen@google.com9a70f342013-06-25 18:07:45 +0000555FallbackFontList* SkFontConfigInterfaceAndroid::getCurrentLocaleFallbackFontList() {
556 SkString locale = SkFontConfigParser::GetLocale();
557 if (NULL == fLocaleFallbackFontList || locale != fCachedLocale) {
558 fCachedLocale = locale;
559 fLocaleFallbackFontList = this->findFallbackFontList(locale);
560 }
561 return fLocaleFallbackFontList;
562}
563
djsollen@google.com40078cb2013-05-24 20:31:57 +0000564FallbackFontList* SkFontConfigInterfaceAndroid::findFallbackFontList(const SkLanguage& lang,
565 bool isOriginal) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000566 const SkString& langTag = lang.getTag();
567 if (langTag.isEmpty()) {
568 return &fDefaultFallbackList;
569 }
570
571 FallbackFontList* fallbackFontList;
djsollen@google.com40078cb2013-05-24 20:31:57 +0000572 if (fFallbackFontDict.find(langTag.c_str(), langTag.size(), &fallbackFontList) ||
573 fFallbackFontAliasDict.find(langTag.c_str(), langTag.size(), &fallbackFontList)) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000574 return fallbackFontList;
575 }
576
577 // attempt a recursive fuzzy match
djsollen@google.combfae9d32013-05-21 16:53:50 +0000578 SkLanguage parent = lang.getParent();
djsollen@google.com40078cb2013-05-24 20:31:57 +0000579 fallbackFontList = findFallbackFontList(parent, false);
580
581 // cache the original lang so we don't have to do the recursion again.
582 if (isOriginal) {
583 DEBUG_FONT(("---- Created fallback list alias for \"%s\"", langTag.c_str()));
584 fFallbackFontAliasDict.set(langTag.c_str(), fallbackFontList);
585 }
586 return fallbackFontList;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000587}
588
589SkTypeface* SkFontConfigInterfaceAndroid::nextLogicalTypeface(SkFontID currFontID,
590 SkFontID origFontID,
591 const SkPaintOptionsAndroid& opts) {
592 // Skia does not support font fallback by default. This enables clients such
593 // as WebKit to customize their font selection. In any case, clients can use
594 // GetFallbackFamilyNameForChar() to get the fallback font for individual
595 // characters.
596 if (!opts.isUsingFontFallbacks()) {
597 return NULL;
598 }
599
djsollen@google.combfae9d32013-05-21 16:53:50 +0000600 FallbackFontList* currentFallbackList = findFallbackFontList(opts.getLanguage());
601 SkASSERT(currentFallbackList);
602
djsollen@google.comb27eba72013-09-06 12:59:50 +0000603 SkTypeface::Style origStyle = SkTypeface::kNormal;
604 const SkTypeface* origTypeface = SkTypefaceCache::FindByID(origFontID);
605 if (NULL != origTypeface) {
606 origStyle = origTypeface->style();
607 }
608
djsollen@google.combfae9d32013-05-21 16:53:50 +0000609 // we must convert currTypeface into a FontRecID
djsollen@google.come47e7d12013-06-06 21:25:09 +0000610 FontRecID currFontRecID = INVALID_FONT_REC_ID;
611 const SkTypeface* currTypeface = SkTypefaceCache::FindByID(currFontID);
612 // non-system fonts are not in the font cache so if we are asked to fallback
613 // for a non-system font we will start at the front of the chain.
djsollen@google.com29bf8622013-07-31 15:48:10 +0000614 if (NULL != currTypeface && currFontID != origFontID) {
djsollen@google.come47e7d12013-06-06 21:25:09 +0000615 currFontRecID = ((FontConfigTypeface*)currTypeface)->getIdentity().fID;
616 SkASSERT(INVALID_FONT_REC_ID != currFontRecID);
617 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000618
djsollen@google.comb27eba72013-09-06 12:59:50 +0000619 FamilyRecID currFamilyRecID = INVALID_FAMILY_REC_ID;
620 if (INVALID_FONT_REC_ID != currFontRecID) {
621 currFamilyRecID = fFonts[currFontRecID].fFamilyRecID;
622 }
623
djsollen@google.come47e7d12013-06-06 21:25:09 +0000624 // lookup the index next font in the chain
djsollen@google.comb27eba72013-09-06 12:59:50 +0000625 int currFallbackFontIndex = currentFallbackList->find(currFamilyRecID);
djsollen@google.come47e7d12013-06-06 21:25:09 +0000626 // We add 1 to the returned index for 2 reasons: (1) if find succeeds it moves
627 // our index to the next entry in the list; (2) if find() fails it returns
628 // -1 and incrementing it will set our starting index to 0 (the head of the list)
djsollen@google.combfae9d32013-05-21 16:53:50 +0000629 int nextFallbackFontIndex = currFallbackFontIndex + 1;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000630
djsollen@google.com40078cb2013-05-24 20:31:57 +0000631 if(nextFallbackFontIndex >= currentFallbackList->count()) {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000632 return NULL;
633 }
634
635 // If a rec object is set to prefer "kDefault_Variant" it means they have no preference
636 // In this case, we set the value to "kCompact_Variant"
637 SkPaintOptionsAndroid::FontVariant variant = opts.getFontVariant();
638 if (variant == SkPaintOptionsAndroid::kDefault_Variant) {
639 variant = SkPaintOptionsAndroid::kCompact_Variant;
640 }
641
642 int32_t acceptedVariants = SkPaintOptionsAndroid::kDefault_Variant | variant;
643
644 SkTypeface* nextLogicalTypeface = 0;
645 while (nextFallbackFontIndex < currentFallbackList->count()) {
djsollen@google.comb27eba72013-09-06 12:59:50 +0000646 FamilyRecID familyRecID = currentFallbackList->getAt(nextFallbackFontIndex);
647 if ((fFontFamilies[familyRecID].fPaintOptions.getFontVariant() & acceptedVariants) != 0) {
648 FontRecID matchedFont = find_best_style(fFontFamilies[familyRecID], origStyle);
649 nextLogicalTypeface = this->getTypefaceForFontRec(matchedFont);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000650 break;
651 }
652 nextFallbackFontIndex++;
653 }
654
655 DEBUG_FONT(("---- nextLogicalFont: currFontID=%d, origFontID=%d, currRecID=%d, "
djsollen@google.com40078cb2013-05-24 20:31:57 +0000656 "lang=%s, variant=%d, nextFallbackIndex[%d,%d] => nextLogicalTypeface=%d",
djsollen@google.combfae9d32013-05-21 16:53:50 +0000657 currFontID, origFontID, currFontRecID, opts.getLanguage().getTag().c_str(),
djsollen@google.com40078cb2013-05-24 20:31:57 +0000658 variant, nextFallbackFontIndex, currentFallbackList->getAt(nextFallbackFontIndex),
djsollen@google.combfae9d32013-05-21 16:53:50 +0000659 (nextLogicalTypeface) ? nextLogicalTypeface->uniqueID() : 0));
660 return SkSafeRef(nextLogicalTypeface);
661}
662
663///////////////////////////////////////////////////////////////////////////////
664
665bool SkGetFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
666 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
667 return fontConfig->getFallbackFamilyNameForChar(uni, name);
668}
669
670void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf,
671 const char* fontsdir) {
672 gTestMainConfigFile = mainconf;
673 gTestFallbackConfigFile = fallbackconf;
674 gTestFontFilePrefix = fontsdir;
675 SkASSERT(gTestMainConfigFile);
676 SkASSERT(gTestFallbackConfigFile);
677 SkASSERT(gTestFontFilePrefix);
678 SkDEBUGF(("Use Test Config File Main %s, Fallback %s, Font Dir %s",
679 gTestMainConfigFile, gTestFallbackConfigFile, gTestFontFilePrefix));
680}
681
682SkTypeface* SkAndroidNextLogicalTypeface(SkFontID currFontID, SkFontID origFontID,
683 const SkPaintOptionsAndroid& options) {
684 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
685 return fontConfig->nextLogicalTypeface(currFontID, origFontID, options);
686
687}
688
689///////////////////////////////////////////////////////////////////////////////
690
djsollen@google.com40078cb2013-05-24 20:31:57 +0000691#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
692
693struct HB_UnicodeMapping {
694 // TODO: when the WebView no longer needs harfbuzz_old, remove
695 HB_Script script_old;
696 hb_script_t script;
697 const SkUnichar unicode;
698};
699
700/*
701 * The following scripts are not complex fonts and we do not expect them to be parsed by this table
702 * HB_SCRIPT_COMMON,
703 * HB_SCRIPT_GREEK,
704 * HB_SCRIPT_CYRILLIC,
705 * HB_SCRIPT_HANGUL
706 * HB_SCRIPT_INHERITED
707 */
708
709/* Harfbuzz (old) is missing a number of scripts in its table. For these,
710 * we include a value which can never happen. We won't get complex script
711 * shaping in these cases, but the library wouldn't know how to shape
712 * them anyway. */
713#define HB_Script_Unknown HB_ScriptCount
714
715static HB_UnicodeMapping HB_UnicodeMappingArray[] = {
716 {HB_Script_Armenian, HB_SCRIPT_ARMENIAN, 0x0531},
717 {HB_Script_Hebrew, HB_SCRIPT_HEBREW, 0x0591},
718 {HB_Script_Arabic, HB_SCRIPT_ARABIC, 0x0600},
719 {HB_Script_Syriac, HB_SCRIPT_SYRIAC, 0x0710},
720 {HB_Script_Thaana, HB_SCRIPT_THAANA, 0x0780},
721 {HB_Script_Nko, HB_SCRIPT_NKO, 0x07C0},
722 {HB_Script_Devanagari, HB_SCRIPT_DEVANAGARI, 0x0901},
723 {HB_Script_Bengali, HB_SCRIPT_BENGALI, 0x0981},
724 {HB_Script_Gurmukhi, HB_SCRIPT_GURMUKHI, 0x0A10},
725 {HB_Script_Gujarati, HB_SCRIPT_GUJARATI, 0x0A90},
726 {HB_Script_Oriya, HB_SCRIPT_ORIYA, 0x0B10},
727 {HB_Script_Tamil, HB_SCRIPT_TAMIL, 0x0B82},
728 {HB_Script_Telugu, HB_SCRIPT_TELUGU, 0x0C10},
729 {HB_Script_Kannada, HB_SCRIPT_KANNADA, 0x0C90},
730 {HB_Script_Malayalam, HB_SCRIPT_MALAYALAM, 0x0D10},
731 {HB_Script_Sinhala, HB_SCRIPT_SINHALA, 0x0D90},
732 {HB_Script_Thai, HB_SCRIPT_THAI, 0x0E01},
733 {HB_Script_Lao, HB_SCRIPT_LAO, 0x0E81},
734 {HB_Script_Tibetan, HB_SCRIPT_TIBETAN, 0x0F00},
735 {HB_Script_Myanmar, HB_SCRIPT_MYANMAR, 0x1000},
736 {HB_Script_Georgian, HB_SCRIPT_GEORGIAN, 0x10A0},
737 {HB_Script_Unknown, HB_SCRIPT_ETHIOPIC, 0x1200},
738 {HB_Script_Unknown, HB_SCRIPT_CHEROKEE, 0x13A0},
739 {HB_Script_Ogham, HB_SCRIPT_OGHAM, 0x1680},
740 {HB_Script_Runic, HB_SCRIPT_RUNIC, 0x16A0},
741 {HB_Script_Khmer, HB_SCRIPT_KHMER, 0x1780},
742 {HB_Script_Unknown, HB_SCRIPT_TAI_LE, 0x1950},
743 {HB_Script_Unknown, HB_SCRIPT_NEW_TAI_LUE, 0x1980},
744 {HB_Script_Unknown, HB_SCRIPT_TAI_THAM, 0x1A20},
745 {HB_Script_Unknown, HB_SCRIPT_CHAM, 0xAA00},
746};
747
748static hb_script_t getHBScriptFromHBScriptOld(HB_Script script_old) {
749 hb_script_t script = HB_SCRIPT_INVALID;
750 int numSupportedFonts = sizeof(HB_UnicodeMappingArray) / sizeof(HB_UnicodeMapping);
751 for (int i = 0; i < numSupportedFonts; i++) {
752 if (script_old == HB_UnicodeMappingArray[i].script_old) {
753 script = HB_UnicodeMappingArray[i].script;
754 break;
755 }
756 }
757 return script;
758}
759
760// returns 0 for "Not Found"
761static SkUnichar getUnicodeFromHBScript(hb_script_t script) {
762 SkUnichar unichar = 0;
763 int numSupportedFonts = sizeof(HB_UnicodeMappingArray) / sizeof(HB_UnicodeMapping);
764 for (int i = 0; i < numSupportedFonts; i++) {
765 if (script == HB_UnicodeMappingArray[i].script) {
766 unichar = HB_UnicodeMappingArray[i].unicode;
767 break;
768 }
769 }
770 return unichar;
771}
772
773struct TypefaceLookupStruct {
774 hb_script_t script;
775 SkTypeface::Style style;
776 SkPaintOptionsAndroid::FontVariant fontVariant;
777 SkTypeface* typeface;
778};
779
780SK_DECLARE_STATIC_MUTEX(gTypefaceTableMutex); // This is the mutex for gTypefaceTable
781static SkTDArray<TypefaceLookupStruct> gTypefaceTable; // This is protected by gTypefaceTableMutex
782
783static int typefaceLookupCompare(const TypefaceLookupStruct& first,
784 const TypefaceLookupStruct& second) {
785 if (first.script != second.script) {
786 return (first.script > second.script) ? 1 : -1;
787 }
788 if (first.style != second.style) {
789 return (first.style > second.style) ? 1 : -1;
790 }
791 if (first.fontVariant != second.fontVariant) {
792 return (first.fontVariant > second.fontVariant) ? 1 : -1;
793 }
794 return 0;
795}
796
797SkTypeface* SkCreateTypefaceForScriptNG(hb_script_t script, SkTypeface::Style style,
798 SkPaintOptionsAndroid::FontVariant fontVariant) {
799 SkAutoMutexAcquire ac(gTypefaceTableMutex);
800
801 TypefaceLookupStruct key;
802 key.script = script;
803 key.style = style;
804 key.fontVariant = fontVariant;
805
806 int index = SkTSearch<TypefaceLookupStruct>(
807 (const TypefaceLookupStruct*) gTypefaceTable.begin(),
808 gTypefaceTable.count(), key, sizeof(TypefaceLookupStruct),
809 typefaceLookupCompare);
810
811 SkTypeface* retTypeface = NULL;
812 if (index >= 0) {
813 retTypeface = gTypefaceTable[index].typeface;
814 }
815 else {
816 SkUnichar unichar = getUnicodeFromHBScript(script);
817 if (!unichar) {
818 return NULL;
819 }
820
821 SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
822 retTypeface = fontConfig->getTypefaceForChar(unichar, style, fontVariant);
823
824 // add to the lookup table
825 key.typeface = retTypeface;
826 *gTypefaceTable.insert(~index) = key;
827 }
828
829 // we ref(), the caller is expected to unref when they are done
830 return SkSafeRef(retTypeface);
831}
832
833SkTypeface* SkCreateTypefaceForScript(HB_Script script, SkTypeface::Style style,
834 SkPaintOptionsAndroid::FontVariant fontVariant) {
835 return SkCreateTypefaceForScriptNG(getHBScriptFromHBScriptOld(script), style, fontVariant);
836}
837
838#endif
839
840///////////////////////////////////////////////////////////////////////////////
841
djsollen@google.combfae9d32013-05-21 16:53:50 +0000842SkFontMgr* SkFontMgr::Factory() {
843 return NULL;
844}