Fix Japanese font issue for devices running JB MR1 and higher.

Android JB MR1 is using a new format of /system/etc/fallback_fonts.xml.
This CL preserves backwards compatibility, while also recognizing the
new fallback_fonts.xml format ("lang" attrbiute in <file/>).

NOTE: This CL was originally sumbitted for Chrome on Android (m18)

Review URL: https://codereview.appspot.com/7226053

git-svn-id: http://skia.googlecode.com/svn/trunk@7448 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
djsollen@google.com 2013-01-29 15:39:35 +00:00
parent 4025f6fed8
commit bd08473d9a

View File

@ -28,13 +28,17 @@
* can read these variables that are relevant to the current parsing.
*/
struct FamilyData {
FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef) :
parser(parserRef), families(familiesRef), currentTag(NO_TAG) {};
FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &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<FontFamily*> &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<FontFamily*> &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<FontFamily*> &families) {
}
XML_Parse(parser, buffer, len, done);
}
fclose(file);
XML_ParserFree(parser);
}
void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {