| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkFontConfigParser_android.h" | 8 #include "SkFontConfigParser_android.h" |
| 9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
| 10 #include "SkFontHost_FreeType_common.h" | 10 #include "SkFontHost_FreeType_common.h" |
| 11 #include "SkFontMgr.h" | 11 #include "SkFontMgr.h" |
| 12 #include "SkFontMgr_android.h" |
| 12 #include "SkFontStyle.h" | 13 #include "SkFontStyle.h" |
| 13 #include "SkStream.h" | 14 #include "SkStream.h" |
| 14 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
| 15 #include "SkTSearch.h" | 16 #include "SkTSearch.h" |
| 16 #include "SkTypeface.h" | 17 #include "SkTypeface.h" |
| 17 #include "SkTypeface_android.h" | 18 #include "SkTypeface_android.h" |
| 18 #include "SkTypefaceCache.h" | 19 #include "SkTypefaceCache.h" |
| 19 | 20 |
| 20 #include <limits> | 21 #include <limits> |
| 21 #include <stdlib.h> | |
| 22 | |
| 23 #ifndef SK_FONT_FILE_PREFIX | |
| 24 # define SK_FONT_FILE_PREFIX "/fonts/" | |
| 25 #endif | |
| 26 | 22 |
| 27 #ifndef SK_DEBUG_FONTS | 23 #ifndef SK_DEBUG_FONTS |
| 28 #define SK_DEBUG_FONTS 0 | 24 #define SK_DEBUG_FONTS 0 |
| 29 #endif | 25 #endif |
| 30 | 26 |
| 31 #if SK_DEBUG_FONTS | 27 #if SK_DEBUG_FONTS |
| 32 # define DEBUG_FONT(args) SkDebugf args | 28 # define DEBUG_FONT(args) SkDebugf args |
| 33 #else | 29 #else |
| 34 # define DEBUG_FONT(args) | 30 # define DEBUG_FONT(args) |
| 35 #endif | 31 #endif |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 *ttcIndex = fIndex; | 115 *ttcIndex = fIndex; |
| 120 return fStream->duplicate(); | 116 return fStream->duplicate(); |
| 121 } | 117 } |
| 122 | 118 |
| 123 private: | 119 private: |
| 124 SkAutoTDelete<SkStreamAsset> fStream; | 120 SkAutoTDelete<SkStreamAsset> fStream; |
| 125 | 121 |
| 126 typedef SkTypeface_Android INHERITED; | 122 typedef SkTypeface_Android INHERITED; |
| 127 }; | 123 }; |
| 128 | 124 |
| 129 void get_path_for_sys_fonts(const char* basePath, const SkString& name, SkString
* full) { | |
| 130 if (basePath) { | |
| 131 full->set(basePath); | |
| 132 } else { | |
| 133 full->set(getenv("ANDROID_ROOT")); | |
| 134 full->append(SK_FONT_FILE_PREFIX); | |
| 135 } | |
| 136 full->append(name); | |
| 137 } | |
| 138 | |
| 139 class SkFontStyleSet_Android : public SkFontStyleSet { | 125 class SkFontStyleSet_Android : public SkFontStyleSet { |
| 140 public: | 126 public: |
| 141 explicit SkFontStyleSet_Android(const FontFamily& family, const char* basePa
th, | 127 explicit SkFontStyleSet_Android(const FontFamily& family, |
| 142 const SkTypeface_FreeType::Scanner& scanner) | 128 const SkTypeface_FreeType::Scanner& scanner) |
| 143 { | 129 { |
| 144 const SkString* cannonicalFamilyName = NULL; | 130 const SkString* cannonicalFamilyName = NULL; |
| 145 if (family.fNames.count() > 0) { | 131 if (family.fNames.count() > 0) { |
| 146 cannonicalFamilyName = &family.fNames[0]; | 132 cannonicalFamilyName = &family.fNames[0]; |
| 147 } | 133 } |
| 148 // TODO? make this lazy | 134 // TODO? make this lazy |
| 149 for (int i = 0; i < family.fFonts.count(); ++i) { | 135 for (int i = 0; i < family.fFonts.count(); ++i) { |
| 150 const FontFileInfo& fontFile = family.fFonts[i]; | 136 const FontFileInfo& fontFile = family.fFonts[i]; |
| 151 | 137 |
| 152 SkString pathName; | 138 SkString pathName(family.fBasePath); |
| 153 get_path_for_sys_fonts(basePath, fontFile.fFileName, &pathName); | 139 pathName.append(fontFile.fFileName); |
| 154 | 140 |
| 155 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str(
))); | 141 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str(
))); |
| 156 if (!stream.get()) { | 142 if (!stream.get()) { |
| 157 DEBUG_FONT(("---- SystemFonts[%d] file=%s (NOT EXIST)", i, pathN
ame.c_str())); | 143 DEBUG_FONT(("---- SystemFonts[%d] file=%s (NOT EXIST)", i, pathN
ame.c_str())); |
| 158 continue; | 144 continue; |
| 159 } | 145 } |
| 160 | 146 |
| 161 const int ttcIndex = fontFile.fIndex; | 147 const int ttcIndex = fontFile.fIndex; |
| 162 SkString familyName; | 148 SkString familyName; |
| 163 SkFontStyle style; | 149 SkFontStyle style; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 * (non-replicated) set of typefaces. | 244 * (non-replicated) set of typefaces. |
| 259 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. | 245 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. |
| 260 */ | 246 */ |
| 261 struct NameToFamily { | 247 struct NameToFamily { |
| 262 SkString name; | 248 SkString name; |
| 263 SkFontStyleSet_Android* styleSet; | 249 SkFontStyleSet_Android* styleSet; |
| 264 }; | 250 }; |
| 265 | 251 |
| 266 class SkFontMgr_Android : public SkFontMgr { | 252 class SkFontMgr_Android : public SkFontMgr { |
| 267 public: | 253 public: |
| 268 SkFontMgr_Android() { | 254 SkFontMgr_Android(SkFontMgr_Android_CustomFonts* custom) { |
| 269 SkTDArray<FontFamily*> fontFamilies; | 255 SkTDArray<FontFamily*> families; |
| 270 SkFontConfigParser::GetFontFamilies(fontFamilies); | 256 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->sy
stemFontUse) { |
| 271 this->buildNameToFamilyMap(fontFamilies, NULL); | 257 SkString base(custom->basePath); |
| 258 SkFontConfigParser::GetCustomFontFamilies(families, base, |
| 259 custom->fontsXml, custom->
fallbackFontsXml); |
| 260 } |
| 261 if (!custom || |
| 262 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->sys
temFontUse)) |
| 263 { |
| 264 SkFontConfigParser::GetSystemFontFamilies(families); |
| 265 } |
| 266 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->sy
stemFontUse) { |
| 267 SkString base(custom->basePath); |
| 268 SkFontConfigParser::GetCustomFontFamilies(families, base, |
| 269 custom->fontsXml, custom->
fallbackFontsXml); |
| 270 } |
| 271 this->buildNameToFamilyMap(families); |
| 272 this->findDefaultFont(); | 272 this->findDefaultFont(); |
| 273 } | 273 families.deleteAll(); |
| 274 SkFontMgr_Android(const char* mainConfigFile, const char* fallbackConfigFile
, | |
| 275 const char* basePath) | |
| 276 { | |
| 277 SkTDArray<FontFamily*> fontFamilies; | |
| 278 SkFontConfigParser::GetTestFontFamilies(fontFamilies, mainConfigFile, fa
llbackConfigFile); | |
| 279 this->buildNameToFamilyMap(fontFamilies, basePath); | |
| 280 this->findDefaultFont(); | |
| 281 } | 274 } |
| 282 | 275 |
| 283 protected: | 276 protected: |
| 284 /** Returns not how many families we have, but how many unique names | 277 /** Returns not how many families we have, but how many unique names |
| 285 * exist among the families. | 278 * exist among the families. |
| 286 */ | 279 */ |
| 287 int onCountFamilies() const SK_OVERRIDE { | 280 int onCountFamilies() const SK_OVERRIDE { |
| 288 return fNameToFamilyMap.count(); | 281 return fNameToFamilyMap.count(); |
| 289 } | 282 } |
| 290 | 283 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 | 442 |
| 450 SkTypeface_FreeType::Scanner fScanner; | 443 SkTypeface_FreeType::Scanner fScanner; |
| 451 | 444 |
| 452 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; | 445 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
| 453 SkFontStyleSet* fDefaultFamily; | 446 SkFontStyleSet* fDefaultFamily; |
| 454 SkTypeface* fDefaultTypeface; | 447 SkTypeface* fDefaultTypeface; |
| 455 | 448 |
| 456 SkTDArray<NameToFamily> fNameToFamilyMap; | 449 SkTDArray<NameToFamily> fNameToFamilyMap; |
| 457 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; | 450 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
| 458 | 451 |
| 459 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const char* baseP
ath) { | 452 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { |
| 460 for (int i = 0; i < families.count(); i++) { | 453 for (int i = 0; i < families.count(); i++) { |
| 461 FontFamily& family = *families[i]; | 454 FontFamily& family = *families[i]; |
| 462 | 455 |
| 463 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; | 456 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; |
| 464 if (family.fIsFallbackFont) { | 457 if (family.fIsFallbackFont) { |
| 465 nameToFamily = &fFallbackNameToFamilyMap; | 458 nameToFamily = &fFallbackNameToFamilyMap; |
| 466 | 459 |
| 467 if (0 == family.fNames.count()) { | 460 if (0 == family.fNames.count()) { |
| 468 SkString& fallbackName = family.fNames.push_back(); | 461 SkString& fallbackName = family.fNames.push_back(); |
| 469 fallbackName.printf("%.2x##fallback", i); | 462 fallbackName.printf("%.2x##fallback", i); |
| 470 } | 463 } |
| 471 } | 464 } |
| 472 | 465 |
| 473 SkFontStyleSet_Android* newSet = | 466 SkFontStyleSet_Android* newSet = |
| 474 SkNEW_ARGS(SkFontStyleSet_Android, (family, basePath, fScanner))
; | 467 SkNEW_ARGS(SkFontStyleSet_Android, (family, fScanner)); |
| 475 if (0 == newSet->count()) { | 468 if (0 == newSet->count()) { |
| 476 SkDELETE(newSet); | 469 SkDELETE(newSet); |
| 477 continue; | 470 continue; |
| 478 } | 471 } |
| 479 fFontStyleSets.push_back().reset(newSet); | 472 fFontStyleSets.push_back().reset(newSet); |
| 480 | 473 |
| 481 for (int j = 0; j < family.fNames.count(); j++) { | 474 for (int j = 0; j < family.fNames.count(); j++) { |
| 482 NameToFamily* nextEntry = nameToFamily->append(); | 475 NameToFamily* nextEntry = nameToFamily->append(); |
| 483 SkNEW_PLACEMENT_ARGS(&nextEntry->name, SkString, (family.fNames[
j])); | 476 SkNEW_PLACEMENT_ARGS(&nextEntry->name, SkString, (family.fNames[
j])); |
| 484 nextEntry->styleSet = newSet; | 477 nextEntry->styleSet = newSet; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 509 } | 502 } |
| 510 SkASSERT(fDefaultFamily); | 503 SkASSERT(fDefaultFamily); |
| 511 SkASSERT(fDefaultTypeface); | 504 SkASSERT(fDefaultTypeface); |
| 512 } | 505 } |
| 513 | 506 |
| 514 typedef SkFontMgr INHERITED; | 507 typedef SkFontMgr INHERITED; |
| 515 }; | 508 }; |
| 516 | 509 |
| 517 /////////////////////////////////////////////////////////////////////////////// | 510 /////////////////////////////////////////////////////////////////////////////// |
| 518 | 511 |
| 512 SkFontMgr* SkFontMgr_New_Android(SkFontMgr_Android_CustomFonts* custom) { |
| 513 #define COMMA , |
| 514 SkDEBUGCODE(static char const * const SystemFontUseStrings[] = { |
| 515 "OnlyCustom" COMMA "PreferCustom" COMMA "PreferSystem" |
| 516 };) |
| 517 #undef COMMA |
| 518 if (custom) { |
| 519 SkASSERT(0 <= custom->systemFontUse); |
| 520 SkASSERT( custom->systemFontUse < SK_ARRAY_COUNT(SystemFontUseString
s)); |
| 521 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s", |
| 522 SystemFontUseStrings[custom->systemFontUse], |
| 523 custom->basePath, |
| 524 custom->fontsXml, |
| 525 custom->fallbackFontsXml)); |
| 526 } |
| 527 |
| 528 return SkNEW_ARGS(SkFontMgr_Android, (custom)); |
| 529 } |
| 530 |
| 519 SkFontMgr* SkFontMgr::Factory() { | 531 SkFontMgr* SkFontMgr::Factory() { |
| 520 // The call to SkGetTestFontConfiguration is so that Chromium can override t
he environment. | 532 // The call to SkGetTestFontConfiguration is so that Chromium can override t
he environment. |
| 521 // TODO: these globals need to be removed, in favor of a constructor / separ
ate Factory | 533 // TODO: these globals need to be removed, in favor of a constructor / separ
ate Factory |
| 522 // which can be used instead. | 534 // which can be used instead. |
| 523 const char* mainConfigFile; | 535 const char* fontsXml; |
| 524 const char* fallbackConfigFile; | 536 const char* fallbackFontsXml; |
| 525 const char* basePath; | 537 const char* basePath; |
| 526 SkGetTestFontConfiguration(&mainConfigFile, &fallbackConfigFile, &basePath); | 538 SkGetTestFontConfiguration(&fontsXml, &fallbackFontsXml, &basePath); |
| 527 if (mainConfigFile) { | 539 if ((fontsXml || fallbackFontsXml) && basePath) { |
| 528 return SkNEW_ARGS(SkFontMgr_Android, (mainConfigFile, fallbackConfigFile
, basePath)); | 540 SkFontMgr_Android_CustomFonts custom = { |
| 541 SkFontMgr_Android_CustomFonts::kOnlyCustom, basePath, fontsXml, fall
backFontsXml |
| 542 }; |
| 543 return SkFontMgr_New_Android(&custom); |
| 529 } | 544 } |
| 530 | 545 |
| 531 return SkNEW(SkFontMgr_Android); | 546 return SkFontMgr_New_Android(NULL); |
| 532 } | 547 } |
| 533 | 548 |
| 534 void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf, | 549 void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf, |
| 535 const char* fontsdir) { | 550 const char* fontsdir) { |
| 536 gTestMainConfigFile = mainconf; | 551 gTestMainConfigFile = mainconf; |
| 537 gTestFallbackConfigFile = fallbackconf; | 552 gTestFallbackConfigFile = fallbackconf; |
| 538 gTestFontFilePrefix = fontsdir; | 553 gTestFontFilePrefix = fontsdir; |
| 539 SkASSERT(gTestMainConfigFile); | 554 SkASSERT(gTestMainConfigFile); |
| 540 SkASSERT(gTestFallbackConfigFile); | 555 SkASSERT(gTestFallbackConfigFile); |
| 541 SkASSERT(gTestFontFilePrefix); | 556 SkASSERT(gTestFontFilePrefix); |
| 542 SkDEBUGF(("Use Test Config File Main %s, Fallback %s, Font Dir %s", | 557 SkDEBUGF(("Use Test Config File Main %s, Fallback %s, Font Dir %s", |
| 543 gTestMainConfigFile, gTestFallbackConfigFile, gTestFontFilePrefix)
); | 558 gTestMainConfigFile, gTestFallbackConfigFile, gTestFontFilePrefix)
); |
| 544 } | 559 } |
| 545 | 560 |
| 546 void SkGetTestFontConfiguration(const char** mainconf, const char** fallbackconf
, | 561 void SkGetTestFontConfiguration(const char** mainconf, const char** fallbackconf
, |
| 547 const char** fontsdir) { | 562 const char** fontsdir) { |
| 548 *mainconf = gTestMainConfigFile; | 563 *mainconf = gTestMainConfigFile; |
| 549 *fallbackconf = gTestFallbackConfigFile; | 564 *fallbackconf = gTestFallbackConfigFile; |
| 550 *fontsdir = gTestFontFilePrefix; | 565 *fontsdir = gTestFontFilePrefix; |
| 551 } | 566 } |
| OLD | NEW |