| 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 | |
| 27 #ifndef SK_DEBUG_FONTS | |
| 28 #define SK_DEBUG_FONTS 0 | |
| 29 #endif | |
| 30 | |
| 31 #if SK_DEBUG_FONTS | |
| 32 # define DEBUG_FONT(args) SkDebugf args | |
| 33 #else | |
| 34 # define DEBUG_FONT(args) | |
| 35 #endif | |
| 36 | 22 |
| 37 // For test only. | 23 // For test only. |
| 38 static const char* gTestMainConfigFile = NULL; | 24 static const char* gTestFontsXml = NULL; |
| 39 static const char* gTestFallbackConfigFile = NULL; | 25 static const char* gTestFallbackFontsXml = NULL; |
| 40 static const char* gTestFontFilePrefix = NULL; | 26 static const char* gTestBasePath = NULL; |
| 41 | 27 |
| 42 class SkTypeface_Android : public SkTypeface_FreeType { | 28 class SkTypeface_Android : public SkTypeface_FreeType { |
| 43 public: | 29 public: |
| 44 SkTypeface_Android(int index, | 30 SkTypeface_Android(int index, |
| 45 const SkFontStyle& style, | 31 const SkFontStyle& style, |
| 46 bool isFixedPitch, | 32 bool isFixedPitch, |
| 47 const SkString& familyName) | 33 const SkString& familyName) |
| 48 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
| 49 , fIndex(index) | 35 , fIndex(index) |
| 50 , fFamilyName(familyName) { } | 36 , fFamilyName(familyName) { } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 *ttcIndex = fIndex; | 105 *ttcIndex = fIndex; |
| 120 return fStream->duplicate(); | 106 return fStream->duplicate(); |
| 121 } | 107 } |
| 122 | 108 |
| 123 private: | 109 private: |
| 124 SkAutoTDelete<SkStreamAsset> fStream; | 110 SkAutoTDelete<SkStreamAsset> fStream; |
| 125 | 111 |
| 126 typedef SkTypeface_Android INHERITED; | 112 typedef SkTypeface_Android INHERITED; |
| 127 }; | 113 }; |
| 128 | 114 |
| 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 { | 115 class SkFontStyleSet_Android : public SkFontStyleSet { |
| 140 public: | 116 public: |
| 141 explicit SkFontStyleSet_Android(const FontFamily& family, const char* basePa
th, | 117 explicit SkFontStyleSet_Android(const FontFamily& family, |
| 142 const SkTypeface_FreeType::Scanner& scanner) | 118 const SkTypeface_FreeType::Scanner& scanner) |
| 143 { | 119 { |
| 144 const SkString* cannonicalFamilyName = NULL; | 120 const SkString* cannonicalFamilyName = NULL; |
| 145 if (family.fNames.count() > 0) { | 121 if (family.fNames.count() > 0) { |
| 146 cannonicalFamilyName = &family.fNames[0]; | 122 cannonicalFamilyName = &family.fNames[0]; |
| 147 } | 123 } |
| 148 // TODO? make this lazy | 124 // TODO? make this lazy |
| 149 for (int i = 0; i < family.fFonts.count(); ++i) { | 125 for (int i = 0; i < family.fFonts.count(); ++i) { |
| 150 const FontFileInfo& fontFile = family.fFonts[i]; | 126 const FontFileInfo& fontFile = family.fFonts[i]; |
| 151 | 127 |
| 152 SkString pathName; | 128 SkString pathName(family.fBasePath); |
| 153 get_path_for_sys_fonts(basePath, fontFile.fFileName, &pathName); | 129 pathName.append(fontFile.fFileName); |
| 154 | 130 |
| 155 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str(
))); | 131 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str(
))); |
| 156 if (!stream.get()) { | 132 if (!stream.get()) { |
| 157 DEBUG_FONT(("---- SystemFonts[%d] file=%s (NOT EXIST)", i, pathN
ame.c_str())); | 133 SkDEBUGF(("Requested font file %s does not exist or cannot be op
ened.\n", |
| 134 pathName.c_str())); |
| 158 continue; | 135 continue; |
| 159 } | 136 } |
| 160 | 137 |
| 161 const int ttcIndex = fontFile.fIndex; | 138 const int ttcIndex = fontFile.fIndex; |
| 162 SkString familyName; | 139 SkString familyName; |
| 163 SkFontStyle style; | 140 SkFontStyle style; |
| 164 bool isFixedWidth; | 141 bool isFixedWidth; |
| 165 if (!scanner.scanFont(stream.get(), ttcIndex, &familyName, &style, &
isFixedWidth)) { | 142 if (!scanner.scanFont(stream.get(), ttcIndex, &familyName, &style, &
isFixedWidth)) { |
| 166 DEBUG_FONT(("---- SystemFonts[%d] file=%s (INVALID)", i, pathNam
e.c_str())); | 143 SkDEBUGF(("Requested font file %s exists, but is not a valid fon
t.\n", |
| 144 pathName.c_str())); |
| 167 continue; | 145 continue; |
| 168 } | 146 } |
| 169 | 147 |
| 170 if (fontFile.fWeight != 0) { | 148 if (fontFile.fWeight != 0) { |
| 171 style = SkFontStyle(fontFile.fWeight, style.width(), style.slant
()); | 149 style = SkFontStyle(fontFile.fWeight, style.width(), style.slant
()); |
| 172 } | 150 } |
| 173 | 151 |
| 174 const SkLanguage& lang = family.fLanguage; | 152 const SkLanguage& lang = family.fLanguage; |
| 175 uint32_t variant = family.fVariant; | 153 uint32_t variant = family.fVariant; |
| 176 if (kDefault_FontVariant == variant) { | 154 if (kDefault_FontVariant == variant) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 * (non-replicated) set of typefaces. | 236 * (non-replicated) set of typefaces. |
| 259 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. | 237 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. |
| 260 */ | 238 */ |
| 261 struct NameToFamily { | 239 struct NameToFamily { |
| 262 SkString name; | 240 SkString name; |
| 263 SkFontStyleSet_Android* styleSet; | 241 SkFontStyleSet_Android* styleSet; |
| 264 }; | 242 }; |
| 265 | 243 |
| 266 class SkFontMgr_Android : public SkFontMgr { | 244 class SkFontMgr_Android : public SkFontMgr { |
| 267 public: | 245 public: |
| 268 SkFontMgr_Android() { | 246 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| 269 SkTDArray<FontFamily*> fontFamilies; | 247 SkTDArray<FontFamily*> families; |
| 270 SkFontConfigParser::GetFontFamilies(fontFamilies); | 248 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS
ystemFontUse) { |
| 271 this->buildNameToFamilyMap(fontFamilies, NULL); | 249 SkString base(custom->fBasePath); |
| 250 SkFontConfigParser::GetCustomFontFamilies(families, base, |
| 251 custom->fFontsXml, custom-
>fFallbackFontsXml); |
| 252 } |
| 253 if (!custom || |
| 254 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy
stemFontUse)) |
| 255 { |
| 256 SkFontConfigParser::GetSystemFontFamilies(families); |
| 257 } |
| 258 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fS
ystemFontUse) { |
| 259 SkString base(custom->fBasePath); |
| 260 SkFontConfigParser::GetCustomFontFamilies(families, base, |
| 261 custom->fFontsXml, custom-
>fFallbackFontsXml); |
| 262 } |
| 263 this->buildNameToFamilyMap(families); |
| 272 this->findDefaultFont(); | 264 this->findDefaultFont(); |
| 273 } | 265 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 } | 266 } |
| 282 | 267 |
| 283 protected: | 268 protected: |
| 284 /** Returns not how many families we have, but how many unique names | 269 /** Returns not how many families we have, but how many unique names |
| 285 * exist among the families. | 270 * exist among the families. |
| 286 */ | 271 */ |
| 287 int onCountFamilies() const SK_OVERRIDE { | 272 int onCountFamilies() const SK_OVERRIDE { |
| 288 return fNameToFamilyMap.count(); | 273 return fNameToFamilyMap.count(); |
| 289 } | 274 } |
| 290 | 275 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 | 434 |
| 450 SkTypeface_FreeType::Scanner fScanner; | 435 SkTypeface_FreeType::Scanner fScanner; |
| 451 | 436 |
| 452 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; | 437 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
| 453 SkFontStyleSet* fDefaultFamily; | 438 SkFontStyleSet* fDefaultFamily; |
| 454 SkTypeface* fDefaultTypeface; | 439 SkTypeface* fDefaultTypeface; |
| 455 | 440 |
| 456 SkTDArray<NameToFamily> fNameToFamilyMap; | 441 SkTDArray<NameToFamily> fNameToFamilyMap; |
| 457 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; | 442 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
| 458 | 443 |
| 459 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const char* baseP
ath) { | 444 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { |
| 460 for (int i = 0; i < families.count(); i++) { | 445 for (int i = 0; i < families.count(); i++) { |
| 461 FontFamily& family = *families[i]; | 446 FontFamily& family = *families[i]; |
| 462 | 447 |
| 463 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; | 448 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; |
| 464 if (family.fIsFallbackFont) { | 449 if (family.fIsFallbackFont) { |
| 465 nameToFamily = &fFallbackNameToFamilyMap; | 450 nameToFamily = &fFallbackNameToFamilyMap; |
| 466 | 451 |
| 467 if (0 == family.fNames.count()) { | 452 if (0 == family.fNames.count()) { |
| 468 SkString& fallbackName = family.fNames.push_back(); | 453 SkString& fallbackName = family.fNames.push_back(); |
| 469 fallbackName.printf("%.2x##fallback", i); | 454 fallbackName.printf("%.2x##fallback", i); |
| 470 } | 455 } |
| 471 } | 456 } |
| 472 | 457 |
| 473 SkFontStyleSet_Android* newSet = | 458 SkFontStyleSet_Android* newSet = |
| 474 SkNEW_ARGS(SkFontStyleSet_Android, (family, basePath, fScanner))
; | 459 SkNEW_ARGS(SkFontStyleSet_Android, (family, fScanner)); |
| 475 if (0 == newSet->count()) { | 460 if (0 == newSet->count()) { |
| 476 SkDELETE(newSet); | 461 SkDELETE(newSet); |
| 477 continue; | 462 continue; |
| 478 } | 463 } |
| 479 fFontStyleSets.push_back().reset(newSet); | 464 fFontStyleSets.push_back().reset(newSet); |
| 480 | 465 |
| 481 for (int j = 0; j < family.fNames.count(); j++) { | 466 for (int j = 0; j < family.fNames.count(); j++) { |
| 482 NameToFamily* nextEntry = nameToFamily->append(); | 467 NameToFamily* nextEntry = nameToFamily->append(); |
| 483 SkNEW_PLACEMENT_ARGS(&nextEntry->name, SkString, (family.fNames[
j])); | 468 SkNEW_PLACEMENT_ARGS(&nextEntry->name, SkString, (family.fNames[
j])); |
| 484 nextEntry->styleSet = newSet; | 469 nextEntry->styleSet = newSet; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 508 fDefaultTypeface = fDefaultFamily->createTypeface(0); | 493 fDefaultTypeface = fDefaultFamily->createTypeface(0); |
| 509 } | 494 } |
| 510 SkASSERT(fDefaultFamily); | 495 SkASSERT(fDefaultFamily); |
| 511 SkASSERT(fDefaultTypeface); | 496 SkASSERT(fDefaultTypeface); |
| 512 } | 497 } |
| 513 | 498 |
| 514 typedef SkFontMgr INHERITED; | 499 typedef SkFontMgr INHERITED; |
| 515 }; | 500 }; |
| 516 | 501 |
| 517 /////////////////////////////////////////////////////////////////////////////// | 502 /////////////////////////////////////////////////////////////////////////////// |
| 503 #ifdef SK_DEBUG |
| 504 static char const * const gSystemFontUseStrings[] = { |
| 505 "OnlyCustom", "PreferCustom", "PreferSystem" |
| 506 }; |
| 507 #endif |
| 508 SkFontMgr* SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) { |
| 509 if (custom) { |
| 510 SkASSERT(0 <= custom->fSystemFontUse); |
| 511 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings))
; |
| 512 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n"
, |
| 513 gSystemFontUseStrings[custom->fSystemFontUse], |
| 514 custom->fBasePath, |
| 515 custom->fFontsXml, |
| 516 custom->fFallbackFontsXml)); |
| 517 } |
| 518 |
| 519 return SkNEW_ARGS(SkFontMgr_Android, (custom)); |
| 520 } |
| 518 | 521 |
| 519 SkFontMgr* SkFontMgr::Factory() { | 522 SkFontMgr* SkFontMgr::Factory() { |
| 520 // The call to SkGetTestFontConfiguration is so that Chromium can override t
he environment. | 523 // These globals exist so that Chromium can override the environment. |
| 521 // TODO: these globals need to be removed, in favor of a constructor / separ
ate Factory | 524 // TODO: these globals need to be removed, and Chromium use SkFontMgr_New_An
droid instead. |
| 522 // which can be used instead. | 525 if ((gTestFontsXml || gTestFallbackFontsXml) && gTestBasePath) { |
| 523 const char* mainConfigFile; | 526 SkFontMgr_Android_CustomFonts custom = { |
| 524 const char* fallbackConfigFile; | 527 SkFontMgr_Android_CustomFonts::kOnlyCustom, |
| 525 const char* basePath; | 528 gTestBasePath, |
| 526 SkGetTestFontConfiguration(&mainConfigFile, &fallbackConfigFile, &basePath); | 529 gTestFontsXml, |
| 527 if (mainConfigFile) { | 530 gTestFallbackFontsXml |
| 528 return SkNEW_ARGS(SkFontMgr_Android, (mainConfigFile, fallbackConfigFile
, basePath)); | 531 }; |
| 532 return SkFontMgr_New_Android(&custom); |
| 529 } | 533 } |
| 530 | 534 |
| 531 return SkNEW(SkFontMgr_Android); | 535 return SkFontMgr_New_Android(NULL); |
| 532 } | 536 } |
| 533 | 537 |
| 534 void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf, | 538 void SkUseTestFontConfigFile(const char* fontsXml, const char* fallbackFontsXml, |
| 535 const char* fontsdir) { | 539 const char* basePath) |
| 536 gTestMainConfigFile = mainconf; | 540 { |
| 537 gTestFallbackConfigFile = fallbackconf; | 541 gTestFontsXml = fontsXml; |
| 538 gTestFontFilePrefix = fontsdir; | 542 gTestFallbackFontsXml = fallbackFontsXml; |
| 539 SkASSERT(gTestMainConfigFile); | 543 gTestBasePath = basePath; |
| 540 SkASSERT(gTestFallbackConfigFile); | 544 SkASSERT(gTestFontsXml); |
| 541 SkASSERT(gTestFontFilePrefix); | 545 SkASSERT(gTestFallbackFontsXml); |
| 542 SkDEBUGF(("Use Test Config File Main %s, Fallback %s, Font Dir %s", | 546 SkASSERT(gTestBasePath); |
| 543 gTestMainConfigFile, gTestFallbackConfigFile, gTestFontFilePrefix)
); | 547 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", |
| 548 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); |
| 544 } | 549 } |
| 545 | |
| 546 void SkGetTestFontConfiguration(const char** mainconf, const char** fallbackconf
, | |
| 547 const char** fontsdir) { | |
| 548 *mainconf = gTestMainConfigFile; | |
| 549 *fallbackconf = gTestFallbackConfigFile; | |
| 550 *fontsdir = gTestFontFilePrefix; | |
| 551 } | |
| OLD | NEW |