diff --git a/src/ports/FontHostConfiguration_android.cpp b/src/ports/FontHostConfiguration_android.cpp index aed7001bfe..6d03414b08 100644 --- a/src/ports/FontHostConfiguration_android.cpp +++ b/src/ports/FontHostConfiguration_android.cpp @@ -28,13 +28,17 @@ * can read these variables that are relevant to the current parsing. */ struct FamilyData { - FamilyData(XML_Parser *parserRef, SkTDArray &familiesRef) : - parser(parserRef), families(familiesRef), currentTag(NO_TAG) {}; + FamilyData(XML_Parser *parserRef, SkTDArray &familiesRef, const AndroidLocale &localeRef) : + parser(parserRef), families(familiesRef), currentTag(NO_TAG), + locale(localeRef), currentFamilyLangMatch(false), familyLangMatchCount(0) {} XML_Parser *parser; // The expat parser doing the work SkTDArray &families; // The array that each family is put into as it is parsed FontFamily *currentFamily; // The current family being created int currentTag; // A flag to indicate whether we're in nameset/fileset tags + const AndroidLocale &locale; // The locale to which we compare the "lang" attribute of File. + bool currentFamilyLangMatch; // If currentFamily's File has a "lang" attribute and matches locale. + int familyLangMatchCount; // Number of families containing File which has a "lang" attribute and matches locale. }; /** @@ -90,10 +94,28 @@ void startElementHandler(void *data, const char *tag, const char **atts) { familyData->currentTag = NAMESET_TAG; } else if (len == 7 && strncmp(tag, "fileset", len) == 0) { familyData->currentTag = FILESET_TAG; - } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) || - (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) { - // If it's a Name, parse the text inside + } else if (strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) { XML_SetCharacterDataHandler(*familyData->parser, textHandler); + } else if (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG) { + // From JB MR1, the File tag has a "lang" attribute to specify a language specific font file + // and the family entry has higher priority than the others without "lang" attribute. + bool includeTheEntry = true; + for (int i = 0; atts[i] != NULL; i += 2) { + const char* attribute = atts[i]; + const char* value = atts[i+1]; + if (strncmp(attribute, "lang", 4) == 0) { + if (strcmp(value, familyData->locale.language) == 0) { + // Found matching "lang" attribute. The current Family will have higher priority in the family list. + familyData->currentFamilyLangMatch = true; + } else { + // Don't include the entry if "lang" is specified but not matching. + includeTheEntry = false; + } + } + } + if (includeTheEntry) { + XML_SetCharacterDataHandler(*familyData->parser, textHandler); + } } } @@ -106,7 +128,12 @@ void endElementHandler(void *data, const char *tag) { int len = strlen(tag); if (strncmp(tag, "family", len)== 0) { // Done parsing a Family - store the created currentFamily in the families array - *familyData->families.append() = familyData->currentFamily; + if (familyData->currentFamilyLangMatch) { + *familyData->families.insert(familyData->familyLangMatchCount++) = familyData->currentFamily; + familyData->currentFamilyLangMatch = false; + } else { + *familyData->families.append() = familyData->currentFamily; + } familyData->currentFamily = NULL; } else if (len == 7 && strncmp(tag, "nameset", len)== 0) { familyData->currentTag = NO_TAG; @@ -150,18 +177,16 @@ void getLocale(AndroidLocale &locale) * /system/etc/fallback_fonts-ja.xml * /system/etc/fallback_fonts.xml */ -FILE* openLocalizedFile(const char* origname) { +FILE* openLocalizedFile(const char* origname, const AndroidLocale& locale) { FILE* file = 0; SkString basename; SkString filename; - AndroidLocale locale; basename.set(origname); // Remove the .xml suffix. We'll add it back in a moment. if (basename.endsWith(".xml")) { basename.resize(basename.size()-4); } - getLocale(locale); // Try first with language and region filename.printf("%s-%s-%s.xml", basename.c_str(), locale.language, locale.region); file = fopen(filename.c_str(), "r"); @@ -183,11 +208,13 @@ FILE* openLocalizedFile(const char* origname) { * families array. */ void parseConfigFile(const char *filename, SkTDArray &families) { + AndroidLocale locale; + getLocale(locale); XML_Parser parser = XML_ParserCreate(NULL); - FamilyData *familyData = new FamilyData(&parser, families); - XML_SetUserData(parser, familyData); + FamilyData familyData(&parser, families, locale); + XML_SetUserData(parser, &familyData); XML_SetElementHandler(parser, startElementHandler, endElementHandler); - FILE *file = openLocalizedFile(filename); + FILE *file = openLocalizedFile(filename, locale); // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml) // are optional - failure here is okay because one of these optional files may not exist. if (file == NULL) { @@ -203,6 +230,8 @@ void parseConfigFile(const char *filename, SkTDArray &families) { } XML_Parse(parser, buffer, len, done); } + fclose(file); + XML_ParserFree(parser); } void getSystemFontFamilies(SkTDArray &fontFamilies) {