djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 1 | /* libs/graphics/ports/FontHostConfiguration_android.cpp |
| 2 | ** |
| 3 | ** Copyright 2011, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "FontHostConfiguration_android.h" |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 19 | #include "SkString.h" |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 20 | #include "SkTDArray.h" |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 21 | #include <expat.h> |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 22 | #include <sys/system_properties.h> |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 23 | |
| 24 | #define SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" |
| 25 | #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
| 26 | #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" |
| 27 | |
| 28 | |
| 29 | // These defines are used to determine the kind of tag that we're currently |
| 30 | // populating with data. We only care about the sibling tags nameset and fileset |
| 31 | // for now. |
| 32 | #define NO_TAG 0 |
| 33 | #define NAMESET_TAG 1 |
| 34 | #define FILESET_TAG 2 |
| 35 | |
| 36 | /** |
| 37 | * The FamilyData structure is passed around by the parser so that each handler |
| 38 | * can read these variables that are relevant to the current parsing. |
| 39 | */ |
| 40 | struct FamilyData { |
| 41 | FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef) : |
| 42 | parser(parserRef), families(familiesRef), currentTag(NO_TAG) {}; |
| 43 | |
| 44 | XML_Parser *parser; // The expat parser doing the work |
| 45 | SkTDArray<FontFamily*> &families; // The array that each family is put into as it is parsed |
| 46 | FontFamily *currentFamily; // The current family being created |
| 47 | int currentTag; // A flag to indicate whether we're in nameset/fileset tags |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Handler for arbitrary text. This is used to parse the text inside each name |
| 52 | * or file tag. The resulting strings are put into the fNames or fFileNames arrays. |
| 53 | */ |
| 54 | void textHandler(void *data, const char *s, int len) { |
| 55 | FamilyData *familyData = (FamilyData*) data; |
| 56 | // Make sure we're in the right state to store this name information |
| 57 | if (familyData->currentFamily && |
| 58 | (familyData->currentTag == NAMESET_TAG || familyData->currentTag == FILESET_TAG)) { |
| 59 | // Malloc new buffer to store the string |
| 60 | char *buff; |
| 61 | buff = (char*) malloc((len + 1) * sizeof(char)); |
| 62 | strncpy(buff, s, len); |
| 63 | buff[len] = '\0'; |
| 64 | switch (familyData->currentTag) { |
| 65 | case NAMESET_TAG: |
| 66 | *(familyData->currentFamily->fNames.append()) = buff; |
| 67 | break; |
| 68 | case FILESET_TAG: |
| 69 | *(familyData->currentFamily->fFileNames.append()) = buff; |
| 70 | break; |
| 71 | default: |
| 72 | // Noop - don't care about any text that's not in the Fonts or Names list |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Handler for the start of a tag. The only tags we expect are family, nameset, |
| 80 | * fileset, name, and file. |
| 81 | */ |
| 82 | void startElementHandler(void *data, const char *tag, const char **atts) { |
| 83 | FamilyData *familyData = (FamilyData*) data; |
| 84 | int len = strlen(tag); |
| 85 | if (strncmp(tag, "family", len)== 0) { |
| 86 | familyData->currentFamily = new FontFamily(); |
| 87 | familyData->currentFamily->order = -1; |
| 88 | // The Family tag has an optional "order" attribute with an integer value >= 0 |
| 89 | // If this attribute does not exist, the default value is -1 |
| 90 | for (int i = 0; atts[i] != NULL; i += 2) { |
| 91 | const char* attribute = atts[i]; |
| 92 | const char* valueString = atts[i+1]; |
| 93 | int value; |
| 94 | int len = sscanf(valueString, "%d", &value); |
| 95 | if (len > 0) { |
| 96 | familyData->currentFamily->order = value; |
| 97 | } |
| 98 | } |
| 99 | } else if (len == 7 && strncmp(tag, "nameset", len)== 0) { |
| 100 | familyData->currentTag = NAMESET_TAG; |
| 101 | } else if (len == 7 && strncmp(tag, "fileset", len) == 0) { |
| 102 | familyData->currentTag = FILESET_TAG; |
| 103 | } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) || |
| 104 | (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) { |
| 105 | // If it's a Name, parse the text inside |
| 106 | XML_SetCharacterDataHandler(*familyData->parser, textHandler); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Handler for the end of tags. We only care about family, nameset, fileset, |
| 112 | * name, and file. |
| 113 | */ |
| 114 | void endElementHandler(void *data, const char *tag) { |
| 115 | FamilyData *familyData = (FamilyData*) data; |
| 116 | int len = strlen(tag); |
| 117 | if (strncmp(tag, "family", len)== 0) { |
| 118 | // Done parsing a Family - store the created currentFamily in the families array |
| 119 | *familyData->families.append() = familyData->currentFamily; |
| 120 | familyData->currentFamily = NULL; |
| 121 | } else if (len == 7 && strncmp(tag, "nameset", len)== 0) { |
| 122 | familyData->currentTag = NO_TAG; |
| 123 | } else if (len == 7 && strncmp(tag, "fileset", len)== 0) { |
| 124 | familyData->currentTag = NO_TAG; |
| 125 | } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) || |
| 126 | (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) { |
| 127 | // Disable the arbitrary text handler installed to load Name data |
| 128 | XML_SetCharacterDataHandler(*familyData->parser, NULL); |
| 129 | } |
| 130 | } |
| 131 | |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Read the persistent locale. |
| 134 | */ |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 135 | void getLocale(AndroidLocale &locale) |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 136 | { |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 137 | char propLang[PROP_VALUE_MAX], propRegn[PROP_VALUE_MAX]; |
| 138 | __system_property_get("persist.sys.language", propLang); |
| 139 | __system_property_get("persist.sys.country", propRegn); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 140 | |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 141 | if (*propLang == 0 && *propRegn == 0) { |
| 142 | /* Set to ro properties, default is en_US */ |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 143 | __system_property_get("ro.product.locale.language", propLang); |
| 144 | __system_property_get("ro.product.locale.region", propRegn); |
| 145 | if (*propLang == 0 && *propRegn == 0) { |
| 146 | strcpy(propLang, "en"); |
| 147 | strcpy(propRegn, "US"); |
| 148 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 149 | } |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 150 | strncpy(locale.language, propLang, 2); |
| 151 | locale.language[2] = '\0'; |
| 152 | strncpy(locale.region, propRegn, 2); |
| 153 | locale.region[2] = '\0'; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 154 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Use the current system locale (language and region) to open the best matching |
| 158 | * customization. For example, when the language is Japanese, the sequence might be: |
| 159 | * /system/etc/fallback_fonts-ja-JP.xml |
| 160 | * /system/etc/fallback_fonts-ja.xml |
| 161 | * /system/etc/fallback_fonts.xml |
| 162 | */ |
| 163 | FILE* openLocalizedFile(const char* origname) { |
| 164 | FILE* file = 0; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 165 | SkString basename; |
| 166 | SkString filename; |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 167 | AndroidLocale locale; |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 168 | |
| 169 | basename.set(origname); |
| 170 | // Remove the .xml suffix. We'll add it back in a moment. |
| 171 | if (basename.endsWith(".xml")) { |
| 172 | basename.resize(basename.size()-4); |
| 173 | } |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 174 | getLocale(locale); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 175 | // Try first with language and region |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 176 | filename.printf("%s-%s-%s.xml", basename.c_str(), locale.language, locale.region); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 177 | file = fopen(filename.c_str(), "r"); |
| 178 | if (!file) { |
| 179 | // If not found, try next with just language |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 180 | filename.printf("%s-%s.xml", basename.c_str(), locale.language); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 181 | file = fopen(filename.c_str(), "r"); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 182 | |
djsollen@google.com | fc9054d | 2012-05-10 16:13:38 +0000 | [diff] [blame] | 183 | if (!file) { |
| 184 | // If still not found, try just the original name |
| 185 | file = fopen(origname, "r"); |
| 186 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 187 | } |
| 188 | return file; |
| 189 | } |
| 190 | |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 191 | /** |
| 192 | * This function parses the given filename and stores the results in the given |
| 193 | * families array. |
| 194 | */ |
| 195 | void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &families) { |
| 196 | XML_Parser parser = XML_ParserCreate(NULL); |
| 197 | FamilyData *familyData = new FamilyData(&parser, families); |
| 198 | XML_SetUserData(parser, familyData); |
| 199 | XML_SetElementHandler(parser, startElementHandler, endElementHandler); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 200 | FILE *file = openLocalizedFile(filename); |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 201 | // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml) |
| 202 | // are optional - failure here is okay because one of these optional files may not exist. |
| 203 | if (file == NULL) { |
| 204 | return; |
| 205 | } |
| 206 | char buffer[512]; |
| 207 | bool done = false; |
| 208 | while (!done) { |
| 209 | fgets(buffer, sizeof(buffer), file); |
| 210 | int len = strlen(buffer); |
| 211 | if (feof(file) != 0) { |
| 212 | done = true; |
| 213 | } |
| 214 | XML_Parse(parser, buffer, len, done); |
| 215 | } |
| 216 | } |
| 217 | |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 218 | void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 219 | parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies); |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) { |
| 223 | SkTDArray<FontFamily*> vendorFonts; |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 224 | parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts); |
| 225 | parseConfigFile(VENDOR_FONTS_FILE, vendorFonts); |
| 226 | |
| 227 | // This loop inserts the vendor fallback fonts in the correct order in the |
| 228 | // overall fallbacks list. |
| 229 | int currentOrder = -1; |
| 230 | for (int i = 0; i < vendorFonts.count(); ++i) { |
| 231 | FontFamily* family = vendorFonts[i]; |
| 232 | int order = family->order; |
| 233 | if (order < 0) { |
| 234 | if (currentOrder < 0) { |
| 235 | // Default case - just add it to the end of the fallback list |
| 236 | *fallbackFonts.append() = family; |
| 237 | } else { |
| 238 | // no order specified on this font, but we're incrementing the order |
| 239 | // based on an earlier order insertion request |
| 240 | *fallbackFonts.insert(currentOrder++) = family; |
| 241 | } |
| 242 | } else { |
| 243 | // Add the font into the fallback list in the specified order. Set |
| 244 | // currentOrder for correct placement of other fonts in the vendor list. |
| 245 | *fallbackFonts.insert(order) = family; |
| 246 | currentOrder = order + 1; |
| 247 | } |
| 248 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Loads data on font families from various expected configuration files. The |
| 253 | * resulting data is returned in the given fontFamilies array. |
| 254 | */ |
| 255 | void getFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { |
| 256 | SkTDArray<FontFamily*> fallbackFonts; |
| 257 | |
| 258 | getSystemFontFamilies(fontFamilies); |
| 259 | getFallbackFontFamilies(fallbackFonts); |
| 260 | |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 261 | // Append all fallback fonts to system fonts |
| 262 | for (int i = 0; i < fallbackFonts.count(); ++i) { |
| 263 | *fontFamilies.append() = fallbackFonts[i]; |
| 264 | } |
| 265 | } |
djsollen@google.com | 5df2a99 | 2012-06-25 13:58:22 +0000 | [diff] [blame^] | 266 | |
| 267 | void getTestFontFamilies(SkTDArray<FontFamily*> &fontFamilies, |
| 268 | const char* testMainConfigFile, |
| 269 | const char* testFallbackConfigFile) { |
| 270 | parseConfigFile(testMainConfigFile, fontFamilies); |
| 271 | |
| 272 | SkTDArray<FontFamily*> fallbackFonts; |
| 273 | parseConfigFile(testFallbackConfigFile, fallbackFonts); |
| 274 | |
| 275 | // Append all fallback fonts to system fonts |
| 276 | for (int i = 0; i < fallbackFonts.count(); ++i) { |
| 277 | *fontFamilies.append() = fallbackFonts[i]; |
| 278 | } |
| 279 | } |