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