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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 size_t valueLen = strlen(value); | 102 size_t valueLen = strlen(value); |
103 if (nameLen == 4 && !strncmp("name", name, nameLen)) { | 103 if (nameLen == 4 && !strncmp("name", name, nameLen)) { |
104 SkAutoAsciiToLC tolc(value); | 104 SkAutoAsciiToLC tolc(value); |
105 family->fNames.push_back().set(tolc.lc()); | 105 family->fNames.push_back().set(tolc.lc()); |
106 family->fIsFallbackFont = false; | 106 family->fIsFallbackFont = false; |
107 } else if (nameLen == 4 && !strncmp("lang", name, nameLen)) { | 107 } else if (nameLen == 4 && !strncmp("lang", name, nameLen)) { |
108 family->fLanguage = SkLanguage (value); | 108 family->fLanguage = SkLanguage (value); |
109 } else if (nameLen == 7 && !strncmp("variant", name, nameLen)) { | 109 } else if (nameLen == 7 && !strncmp("variant", name, nameLen)) { |
110 // Value should be either elegant or compact. | 110 // Value should be either elegant or compact. |
111 if (valueLen == 7 && !strncmp("elegant", value, valueLen)) { | 111 if (valueLen == 7 && !strncmp("elegant", value, valueLen)) { |
112 family->fVariant = SkPaintOptionsAndroid::kElegant_Variant; | 112 family->fVariant = kElegant_FontVariant; |
113 } else if (valueLen == 7 && !strncmp("compact", value, valueLen)) { | 113 } else if (valueLen == 7 && !strncmp("compact", value, valueLen)) { |
114 family->fVariant = SkPaintOptionsAndroid::kCompact_Variant; | 114 family->fVariant = kCompact_FontVariant; |
115 } | 115 } |
116 } | 116 } |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 void fontFileNameHandler(void* data, const char* s, int len) { | 120 void fontFileNameHandler(void* data, const char* s, int len) { |
121 FamilyData* familyData = (FamilyData*) data; | 121 FamilyData* familyData = (FamilyData*) data; |
122 familyData->currentFontInfo->fFileName.set(s, len); | 122 familyData->currentFontInfo->fFileName.set(s, len); |
123 } | 123 } |
124 | 124 |
125 void familyElementEndHandler(FontFamily* family) { | |
126 for (int i = 0; i < family->fFonts.count(); i++) { | |
127 family->fFonts[i].fPaintOptions.setLanguage(family->fLanguage); | |
128 family->fFonts[i].fPaintOptions.setFontVariant(family->fVariant); | |
129 } | |
130 } | |
131 | |
132 void fontElementHandler(XML_Parser* parser, FontFileInfo* file, const char** att
ributes) { | 125 void fontElementHandler(XML_Parser* parser, FontFileInfo* file, const char** att
ributes) { |
133 // A <font> should have weight (integer) and style (normal, italic) attribut
es. | 126 // A <font> should have weight (integer) and style (normal, italic) attribut
es. |
134 // NOTE: we ignore the style. | 127 // NOTE: we ignore the style. |
135 // The element should contain a filename. | 128 // The element should contain a filename. |
136 for (size_t i = 0; attributes[i] != NULL && | 129 for (size_t i = 0; attributes[i] != NULL && |
137 attributes[i+1] != NULL; i += 2) { | 130 attributes[i+1] != NULL; i += 2) { |
138 const char* name = attributes[i]; | 131 const char* name = attributes[i]; |
139 const char* value = attributes[i+1]; | 132 const char* value = attributes[i+1]; |
140 size_t nameLen = strlen(name); | 133 size_t nameLen = strlen(name); |
141 if (nameLen == 6 && !strncmp("weight", name, nameLen)) { | 134 if (nameLen == 6 && !strncmp("weight", name, nameLen)) { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 aliasElementHandler(familyData, attributes); | 264 aliasElementHandler(familyData, attributes); |
272 } | 265 } |
273 } | 266 } |
274 | 267 |
275 void endElementHandler(void* data, const char* tag) { | 268 void endElementHandler(void* data, const char* tag) { |
276 FamilyData* familyData = (FamilyData*) data; | 269 FamilyData* familyData = (FamilyData*) data; |
277 size_t len = strlen(tag); | 270 size_t len = strlen(tag); |
278 if (len == 9 && strncmp(tag, "familyset", len) == 0) { | 271 if (len == 9 && strncmp(tag, "familyset", len) == 0) { |
279 familysetElementEndHandler(familyData); | 272 familysetElementEndHandler(familyData); |
280 } else if (len == 6 && strncmp(tag, "family", len) == 0) { | 273 } else if (len == 6 && strncmp(tag, "family", len) == 0) { |
281 familyElementEndHandler(familyData->currentFamily); | |
282 *familyData->families.append() = familyData->currentFamily; | 274 *familyData->families.append() = familyData->currentFamily; |
283 familyData->currentFamily = NULL; | 275 familyData->currentFamily = NULL; |
284 } else if (len == 4 && !strncmp(tag, "font", len)) { | 276 } else if (len == 4 && !strncmp(tag, "font", len)) { |
285 XML_SetCharacterDataHandler(*familyData->parser, NULL); | 277 XML_SetCharacterDataHandler(*familyData->parser, NULL); |
286 } | 278 } |
287 } | 279 } |
288 | 280 |
289 } // lmpParser | 281 } // lmpParser |
290 | 282 |
291 namespace jbParser { | 283 namespace jbParser { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 FontFileInfo& newFileInfo = familyData->currentFamily->fFonts.push_back(); | 317 FontFileInfo& newFileInfo = familyData->currentFamily->fFonts.push_back(); |
326 if (attributes) { | 318 if (attributes) { |
327 size_t currentAttributeIndex = 0; | 319 size_t currentAttributeIndex = 0; |
328 while (attributes[currentAttributeIndex] && | 320 while (attributes[currentAttributeIndex] && |
329 attributes[currentAttributeIndex + 1]) { | 321 attributes[currentAttributeIndex + 1]) { |
330 const char* attributeName = attributes[currentAttributeIndex]; | 322 const char* attributeName = attributes[currentAttributeIndex]; |
331 const char* attributeValue = attributes[currentAttributeIndex+1]; | 323 const char* attributeValue = attributes[currentAttributeIndex+1]; |
332 size_t nameLength = strlen(attributeName); | 324 size_t nameLength = strlen(attributeName); |
333 size_t valueLength = strlen(attributeValue); | 325 size_t valueLength = strlen(attributeValue); |
334 if (nameLength == 7 && strncmp(attributeName, "variant", nameLength)
== 0) { | 326 if (nameLength == 7 && strncmp(attributeName, "variant", nameLength)
== 0) { |
| 327 const FontVariant prevVariant = familyData->currentFamily->fVari
ant; |
335 if (valueLength == 7 && strncmp(attributeValue, "elegant", value
Length) == 0) { | 328 if (valueLength == 7 && strncmp(attributeValue, "elegant", value
Length) == 0) { |
336 newFileInfo.fPaintOptions.setFontVariant(SkPaintOptionsAndro
id::kElegant_Variant); | 329 familyData->currentFamily->fVariant = kElegant_FontVariant; |
337 } else if (valueLength == 7 && | 330 } else if (valueLength == 7 && |
338 strncmp(attributeValue, "compact", valueLength) == 0)
{ | 331 strncmp(attributeValue, "compact", valueLength) == 0)
{ |
339 newFileInfo.fPaintOptions.setFontVariant(SkPaintOptionsAndro
id::kCompact_Variant); | 332 familyData->currentFamily->fVariant = kCompact_FontVariant; |
340 } | 333 } |
| 334 if (familyData->currentFamily->fFonts.count() > 1 && |
| 335 familyData->currentFamily->fVariant != prevVariant) { |
| 336 SkDebugf("Every font file within a family must have identica
l variants"); |
| 337 sk_throw(); |
| 338 } |
| 339 |
341 } else if (nameLength == 4 && strncmp(attributeName, "lang", nameLen
gth) == 0) { | 340 } else if (nameLength == 4 && strncmp(attributeName, "lang", nameLen
gth) == 0) { |
342 newFileInfo.fPaintOptions.setLanguage(attributeValue); | 341 SkLanguage prevLang = familyData->currentFamily->fLanguage; |
| 342 familyData->currentFamily->fLanguage = SkLanguage(attributeValue
); |
| 343 if (familyData->currentFamily->fFonts.count() > 1 && |
| 344 familyData->currentFamily->fLanguage != prevLang) { |
| 345 SkDebugf("Every font file within a family must have identica
l languages"); |
| 346 sk_throw(); |
| 347 } |
343 } else if (nameLength == 5 && strncmp(attributeName, "index", nameLe
ngth) == 0) { | 348 } else if (nameLength == 5 && strncmp(attributeName, "index", nameLe
ngth) == 0) { |
344 int value; | 349 int value; |
345 if (parseNonNegativeInteger(attributeValue, &value)) { | 350 if (parseNonNegativeInteger(attributeValue, &value)) { |
346 newFileInfo.fIndex = value; | 351 newFileInfo.fIndex = value; |
347 } else { | 352 } else { |
348 SkDebugf("---- SystemFonts index=%s (INVALID)", attributeVal
ue); | 353 SkDebugf("---- SystemFonts index=%s (INVALID)", attributeVal
ue); |
349 } | 354 } |
350 } | 355 } |
351 //each element is a pair of attributeName/attributeValue string pair
s | 356 //each element is a pair of attributeName/attributeValue string pair
s |
352 currentAttributeIndex += 2; | 357 currentAttributeIndex += 2; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 fileName.size() - fixedLen); | 511 fileName.size() - fixedLen); |
507 | 512 |
508 SkString absoluteFilename; | 513 SkString absoluteFilename; |
509 absoluteFilename.printf("%s/%s", dir, fileName.c_str()); | 514 absoluteFilename.printf("%s/%s", dir, fileName.c_str()); |
510 | 515 |
511 SkTDArray<FontFamily*> langSpecificFonts; | 516 SkTDArray<FontFamily*> langSpecificFonts; |
512 parseConfigFile(absoluteFilename.c_str(), langSpecificFonts); | 517 parseConfigFile(absoluteFilename.c_str(), langSpecificFonts); |
513 | 518 |
514 for (int i = 0; i < langSpecificFonts.count(); ++i) { | 519 for (int i = 0; i < langSpecificFonts.count(); ++i) { |
515 FontFamily* family = langSpecificFonts[i]; | 520 FontFamily* family = langSpecificFonts[i]; |
516 for (int j = 0; j < family->fFonts.count(); ++j) { | 521 family->fLanguage = SkLanguage(locale); |
517 family->fFonts[j].fPaintOptions.setLanguage(locale); | |
518 } | |
519 *fallbackFonts.append() = family; | 522 *fallbackFonts.append() = family; |
520 } | 523 } |
521 } | 524 } |
522 | 525 |
523 // proceed to the next entry in the directory | 526 // proceed to the next entry in the directory |
524 dirEntry = readdir(fontDirectory); | 527 dirEntry = readdir(fontDirectory); |
525 } | 528 } |
526 // cleanup the directory reference | 529 // cleanup the directory reference |
527 closedir(fontDirectory); | 530 closedir(fontDirectory); |
528 } | 531 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 if (NULL != testFallbackConfigFile) { | 589 if (NULL != testFallbackConfigFile) { |
587 parseConfigFile(testFallbackConfigFile, fallbackFonts); | 590 parseConfigFile(testFallbackConfigFile, fallbackFonts); |
588 } | 591 } |
589 | 592 |
590 // Append all fallback fonts to system fonts | 593 // Append all fallback fonts to system fonts |
591 for (int i = 0; i < fallbackFonts.count(); ++i) { | 594 for (int i = 0; i < fallbackFonts.count(); ++i) { |
592 fallbackFonts[i]->fIsFallbackFont = true; | 595 fallbackFonts[i]->fIsFallbackFont = true; |
593 *fontFamilies.append() = fallbackFonts[i]; | 596 *fontFamilies.append() = fallbackFonts[i]; |
594 } | 597 } |
595 } | 598 } |
| 599 |
| 600 SkLanguage SkLanguage::getParent() const { |
| 601 SkASSERT(!fTag.isEmpty()); |
| 602 const char* tag = fTag.c_str(); |
| 603 |
| 604 // strip off the rightmost "-.*" |
| 605 const char* parentTagEnd = strrchr(tag, '-'); |
| 606 if (parentTagEnd == NULL) { |
| 607 return SkLanguage(); |
| 608 } |
| 609 size_t parentTagLen = parentTagEnd - tag; |
| 610 return SkLanguage(tag, parentTagLen); |
| 611 } |
OLD | NEW |