| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The Android Open Source Project | 2 * Copyright 2011 The Android Open Source Project |
| 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 "SkTDArray.h" | 9 #include "SkTDArray.h" |
| 10 #include "SkTSearch.h" | 10 #include "SkTSearch.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 const char* value = attributes[i+1]; | 90 const char* value = attributes[i+1]; |
| 91 int nameLen = strlen(name); | 91 int nameLen = strlen(name); |
| 92 int valueLen = strlen(value); | 92 int valueLen = strlen(value); |
| 93 if (nameLen == 4 && !strncmp("name", name, nameLen)) { | 93 if (nameLen == 4 && !strncmp("name", name, nameLen)) { |
| 94 family->fNames.push_back().set(value); | 94 family->fNames.push_back().set(value); |
| 95 } else if (nameLen == 4 && !strncmp("lang", name, nameLen)) { | 95 } else if (nameLen == 4 && !strncmp("lang", name, nameLen)) { |
| 96 family->fLanguage = SkLanguage (value); | 96 family->fLanguage = SkLanguage (value); |
| 97 } else if (nameLen == 7 && !strncmp("variant", name, nameLen)) { | 97 } else if (nameLen == 7 && !strncmp("variant", name, nameLen)) { |
| 98 // Value should be either elegant or compact. | 98 // Value should be either elegant or compact. |
| 99 if (valueLen == 7 && !strncmp("elegant", value, valueLen)) { | 99 if (valueLen == 7 && !strncmp("elegant", value, valueLen)) { |
| 100 family->fVariant = kElegant_FontVariant; | 100 family->fVariant = SkPaintOptionsAndroid::kElegant_Variant; |
| 101 } else if (valueLen == 7 && !strncmp("compact", value, valueLen)) { | 101 } else if (valueLen == 7 && !strncmp("compact", value, valueLen)) { |
| 102 family->fVariant = kCompact_FontVariant; | 102 family->fVariant = SkPaintOptionsAndroid::kCompact_Variant; |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 void fontFileNameHandler(void *data, const char *s, int len) { | 108 void fontFileNameHandler(void *data, const char *s, int len) { |
| 109 FamilyData *familyData = (FamilyData*) data; | 109 FamilyData *familyData = (FamilyData*) data; |
| 110 familyData->currentFontInfo->fFileName.set(s, len); | 110 familyData->currentFontInfo->fFileName.set(s, len); |
| 111 } | 111 } |
| 112 | 112 |
| 113 void familyElementEndHandler(FontFamily* family) { |
| 114 for (int i = 0; i < family->fFontFiles.count(); i++) { |
| 115 family->fFontFiles[i].fPaintOptions.setLanguage(family->fLanguage); |
| 116 family->fFontFiles[i].fPaintOptions.setFontVariant(family->fVariant); |
| 117 } |
| 118 } |
| 119 |
| 113 void fontElementHandler(XML_Parser* parser, FontFileInfo* file, const char** att
ributes) { | 120 void fontElementHandler(XML_Parser* parser, FontFileInfo* file, const char** att
ributes) { |
| 114 // A <font> should have weight (integer) and style (normal, italic) attribut
es. | 121 // A <font> should have weight (integer) and style (normal, italic) attribut
es. |
| 115 // NOTE: we ignore the style. | 122 // NOTE: we ignore the style. |
| 116 // The element should contain a filename. | 123 // The element should contain a filename. |
| 117 for (int i = 0; attributes[i] != NULL; i += 2) { | 124 for (int i = 0; attributes[i] != NULL; i += 2) { |
| 118 const char* name = attributes[i]; | 125 const char* name = attributes[i]; |
| 119 const char* value = attributes[i+1]; | 126 const char* value = attributes[i+1]; |
| 120 int nameLen = strlen(name); | 127 int nameLen = strlen(name); |
| 121 if (nameLen == 6 && !strncmp("weight", name, nameLen)) { | 128 if (nameLen == 6 && !strncmp("weight", name, nameLen)) { |
| 122 parseNonNegativeInteger(value, &file->fWeight); | 129 parseNonNegativeInteger(value, &file->fWeight); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 aliasElementHandler(familyData, attributes); | 253 aliasElementHandler(familyData, attributes); |
| 247 } | 254 } |
| 248 } | 255 } |
| 249 | 256 |
| 250 void endElementHandler(void* data, const char* tag) { | 257 void endElementHandler(void* data, const char* tag) { |
| 251 FamilyData *familyData = (FamilyData*) data; | 258 FamilyData *familyData = (FamilyData*) data; |
| 252 int len = strlen(tag); | 259 int len = strlen(tag); |
| 253 if (len == 9 && strncmp(tag, "familyset", len) == 0) { | 260 if (len == 9 && strncmp(tag, "familyset", len) == 0) { |
| 254 familysetElementEndHandler(familyData); | 261 familysetElementEndHandler(familyData); |
| 255 } else if (len == 6 && strncmp(tag, "family", len) == 0) { | 262 } else if (len == 6 && strncmp(tag, "family", len) == 0) { |
| 263 familyElementEndHandler(familyData->currentFamily); |
| 256 *familyData->families.append() = familyData->currentFamily; | 264 *familyData->families.append() = familyData->currentFamily; |
| 257 familyData->currentFamily = NULL; | 265 familyData->currentFamily = NULL; |
| 258 } else if (len == 4 && !strncmp(tag, "font", len)) { | 266 } else if (len == 4 && !strncmp(tag, "font", len)) { |
| 259 XML_SetCharacterDataHandler(*familyData->parser, NULL); | 267 XML_SetCharacterDataHandler(*familyData->parser, NULL); |
| 260 } | 268 } |
| 261 } | 269 } |
| 262 | 270 |
| 263 } // lmpParser | 271 } // lmpParser |
| 264 | 272 |
| 265 namespace jbParser { | 273 namespace jbParser { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 static void fontFileElementHandler(FamilyData *familyData, const char **attribut
es) { | 306 static void fontFileElementHandler(FamilyData *familyData, const char **attribut
es) { |
| 299 FontFileInfo& newFileInfo = familyData->currentFamily->fFontFiles.push_back(
); | 307 FontFileInfo& newFileInfo = familyData->currentFamily->fFontFiles.push_back(
); |
| 300 if (attributes) { | 308 if (attributes) { |
| 301 int currentAttributeIndex = 0; | 309 int currentAttributeIndex = 0; |
| 302 while (attributes[currentAttributeIndex]) { | 310 while (attributes[currentAttributeIndex]) { |
| 303 const char* attributeName = attributes[currentAttributeIndex]; | 311 const char* attributeName = attributes[currentAttributeIndex]; |
| 304 const char* attributeValue = attributes[currentAttributeIndex+1]; | 312 const char* attributeValue = attributes[currentAttributeIndex+1]; |
| 305 int nameLength = strlen(attributeName); | 313 int nameLength = strlen(attributeName); |
| 306 int valueLength = strlen(attributeValue); | 314 int valueLength = strlen(attributeValue); |
| 307 if (nameLength == 7 && strncmp(attributeName, "variant", nameLength)
== 0) { | 315 if (nameLength == 7 && strncmp(attributeName, "variant", nameLength)
== 0) { |
| 308 const FontVariant prevVariant = familyData->currentFamily->fVari
ant; | |
| 309 if (valueLength == 7 && strncmp(attributeValue, "elegant", value
Length) == 0) { | 316 if (valueLength == 7 && strncmp(attributeValue, "elegant", value
Length) == 0) { |
| 310 familyData->currentFamily->fVariant = kElegant_FontVariant; | 317 newFileInfo.fPaintOptions.setFontVariant(SkPaintOptionsAndro
id::kElegant_Variant); |
| 311 } else if (valueLength == 7 && | 318 } else if (valueLength == 7 && |
| 312 strncmp(attributeValue, "compact", valueLength) == 0)
{ | 319 strncmp(attributeValue, "compact", valueLength) == 0)
{ |
| 313 familyData->currentFamily->fVariant = kCompact_FontVariant; | 320 newFileInfo.fPaintOptions.setFontVariant(SkPaintOptionsAndro
id::kCompact_Variant); |
| 314 } | 321 } |
| 315 if (familyData->currentFamily->fFontFiles.count() > 1 && | |
| 316 familyData->currentFamily->fVariant != prevVariant) { | |
| 317 SkDebugf("Every font file within a family must have identica
l variants"); | |
| 318 sk_throw(); | |
| 319 } | |
| 320 | |
| 321 } else if (nameLength == 4 && strncmp(attributeName, "lang", nameLen
gth) == 0) { | 322 } else if (nameLength == 4 && strncmp(attributeName, "lang", nameLen
gth) == 0) { |
| 322 SkLanguage prevLang = familyData->currentFamily->fLanguage; | 323 newFileInfo.fPaintOptions.setLanguage(attributeValue); |
| 323 familyData->currentFamily->fLanguage = SkLanguage(attributeValue
); | |
| 324 if (familyData->currentFamily->fFontFiles.count() > 1 && | |
| 325 familyData->currentFamily->fLanguage != prevLang) { | |
| 326 SkDebugf("Every font file within a family must have identica
l languages"); | |
| 327 sk_throw(); | |
| 328 } | |
| 329 } else if (nameLength == 5 && strncmp(attributeName, "index", nameLe
ngth) == 0) { | 324 } else if (nameLength == 5 && strncmp(attributeName, "index", nameLe
ngth) == 0) { |
| 330 int value; | 325 int value; |
| 331 if (parseNonNegativeInteger(attributeValue, &value)) { | 326 if (parseNonNegativeInteger(attributeValue, &value)) { |
| 332 newFileInfo.fIndex = value; | 327 newFileInfo.fIndex = value; |
| 333 } else { | 328 } else { |
| 334 SkDebugf("---- SystemFonts index=%s (INVALID)", attributeVal
ue); | 329 SkDebugf("---- SystemFonts index=%s (INVALID)", attributeVal
ue); |
| 335 } | 330 } |
| 336 } | 331 } |
| 337 //each element is a pair of attributeName/attributeValue string pair
s | 332 //each element is a pair of attributeName/attributeValue string pair
s |
| 338 currentAttributeIndex += 2; | 333 currentAttributeIndex += 2; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 fileName.size() - fixedLen); | 482 fileName.size() - fixedLen); |
| 488 | 483 |
| 489 SkString absoluteFilename; | 484 SkString absoluteFilename; |
| 490 absoluteFilename.printf("%s/%s", dir, fileName.c_str()); | 485 absoluteFilename.printf("%s/%s", dir, fileName.c_str()); |
| 491 | 486 |
| 492 SkTDArray<FontFamily*> langSpecificFonts; | 487 SkTDArray<FontFamily*> langSpecificFonts; |
| 493 parseConfigFile(absoluteFilename.c_str(), langSpecificFonts); | 488 parseConfigFile(absoluteFilename.c_str(), langSpecificFonts); |
| 494 | 489 |
| 495 for (int i = 0; i < langSpecificFonts.count(); ++i) { | 490 for (int i = 0; i < langSpecificFonts.count(); ++i) { |
| 496 FontFamily* family = langSpecificFonts[i]; | 491 FontFamily* family = langSpecificFonts[i]; |
| 497 family->fLanguage = SkLanguage(locale); | 492 for (int j = 0; j < family->fFontFiles.count(); ++j) { |
| 493 family->fFontFiles[j].fPaintOptions.setLanguage(locale); |
| 494 } |
| 498 *fallbackFonts.append() = family; | 495 *fallbackFonts.append() = family; |
| 499 } | 496 } |
| 500 } | 497 } |
| 501 | 498 |
| 502 // proceed to the next entry in the directory | 499 // proceed to the next entry in the directory |
| 503 dirEntry = readdir(fontDirectory); | 500 dirEntry = readdir(fontDirectory); |
| 504 } | 501 } |
| 505 // cleanup the directory reference | 502 // cleanup the directory reference |
| 506 closedir(fontDirectory); | 503 closedir(fontDirectory); |
| 507 } | 504 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 if (NULL != testFallbackConfigFile) { | 562 if (NULL != testFallbackConfigFile) { |
| 566 parseConfigFile(testFallbackConfigFile, fallbackFonts); | 563 parseConfigFile(testFallbackConfigFile, fallbackFonts); |
| 567 } | 564 } |
| 568 | 565 |
| 569 // Append all fallback fonts to system fonts | 566 // Append all fallback fonts to system fonts |
| 570 for (int i = 0; i < fallbackFonts.count(); ++i) { | 567 for (int i = 0; i < fallbackFonts.count(); ++i) { |
| 571 fallbackFonts[i]->fIsFallbackFont = true; | 568 fallbackFonts[i]->fIsFallbackFont = true; |
| 572 *fontFamilies.append() = fallbackFonts[i]; | 569 *fontFamilies.append() = fallbackFonts[i]; |
| 573 } | 570 } |
| 574 } | 571 } |
| 575 | |
| 576 SkLanguage SkLanguage::getParent() const { | |
| 577 SkASSERT(!fTag.isEmpty()); | |
| 578 const char* tag = fTag.c_str(); | |
| 579 | |
| 580 // strip off the rightmost "-.*" | |
| 581 const char* parentTagEnd = strrchr(tag, '-'); | |
| 582 if (parentTagEnd == NULL) { | |
| 583 return SkLanguage(); | |
| 584 } | |
| 585 size_t parentTagLen = parentTagEnd - tag; | |
| 586 return SkLanguage(tag, parentTagLen); | |
| 587 } | |
| OLD | NEW |