djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkFontConfigParser_android.h" |
| 9 | #include "SkTDArray.h" |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 10 | #include "SkTSearch.h" |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 11 | #include "SkTypeface.h" |
| 12 | |
| 13 | #include <expat.h> |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 14 | #include <dirent.h> |
bungeman@google.com | 18252fe | 2013-10-11 20:13:41 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 16 | |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 17 | #include <limits> |
| 18 | |
tomhudson | 94fa4b9 | 2014-08-12 11:05:29 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | // From Android version LMP onwards, all font files collapse into |
tomhudson | 7a4747f | 2014-08-13 11:06:43 -0700 | [diff] [blame^] | 22 | // /system/etc/fonts.xml. Instead of trying to detect which version |
tomhudson | 94fa4b9 | 2014-08-12 11:05:29 -0700 | [diff] [blame] | 23 | // we're on, try to open fonts.xml; if that fails, fall back to the |
| 24 | // older filename. |
| 25 | #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" |
| 26 | #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 27 | #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
| 28 | #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" |
| 29 | |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 30 | #define LOCALE_FALLBACK_FONTS_SYSTEM_DIR "/system/etc" |
| 31 | #define LOCALE_FALLBACK_FONTS_VENDOR_DIR "/vendor/etc" |
| 32 | #define LOCALE_FALLBACK_FONTS_PREFIX "fallback_fonts-" |
| 33 | #define LOCALE_FALLBACK_FONTS_SUFFIX ".xml" |
| 34 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 35 | /** |
| 36 | * This file contains TWO parsers: one for JB and earlier (system_fonts.xml / |
| 37 | * fallback_fonts.xml), one for LMP and later (fonts.xml). |
| 38 | * We start with the JB parser, and if we detect a <familyset> tag with |
| 39 | * version 21 or higher we switch to the LMP parser. |
| 40 | */ |
| 41 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 42 | // These defines are used to determine the kind of tag that we're currently |
| 43 | // populating with data. We only care about the sibling tags nameset and fileset |
| 44 | // for now. |
| 45 | #define NO_TAG 0 |
| 46 | #define NAMESET_TAG 1 |
| 47 | #define FILESET_TAG 2 |
| 48 | |
| 49 | /** |
| 50 | * The FamilyData structure is passed around by the parser so that each handler |
| 51 | * can read these variables that are relevant to the current parsing. |
| 52 | */ |
| 53 | struct FamilyData { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 54 | FamilyData(XML_Parser* parserRef, SkTDArray<FontFamily*> &familiesRef) : |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 55 | parser(parserRef), |
| 56 | families(familiesRef), |
| 57 | currentFamily(NULL), |
| 58 | currentFontInfo(NULL), |
| 59 | currentTag(NO_TAG) {}; |
| 60 | |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 61 | XML_Parser* parser; // The expat parser doing the work |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 62 | SkTDArray<FontFamily*> &families; // The array that each family is put into as it is parsed |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 63 | FontFamily* currentFamily; // The current family being created |
| 64 | FontFileInfo* currentFontInfo; // The current fontInfo being created |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 65 | int currentTag; // A flag to indicate whether we're in nameset/fileset tags |
| 66 | }; |
| 67 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 68 | /** http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def */ |
| 69 | template <typename T> static bool parseNonNegativeInteger(const char* s, T* value) { |
| 70 | SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); |
| 71 | const T nMax = std::numeric_limits<T>::max() / 10; |
| 72 | const T dMax = std::numeric_limits<T>::max() - (nMax * 10); |
| 73 | T n = 0; |
| 74 | for (; *s; ++s) { |
| 75 | // Check if digit |
| 76 | if (*s < '0' || '9' < *s) { |
| 77 | return false; |
| 78 | } |
| 79 | int d = *s - '0'; |
| 80 | // Check for overflow |
| 81 | if (n > nMax || (n == nMax && d > dMax)) { |
| 82 | return false; |
| 83 | } |
| 84 | n = (n * 10) + d; |
| 85 | } |
| 86 | *value = n; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | namespace lmpParser { |
| 91 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 92 | void familyElementHandler(FontFamily* family, const char** attributes) { |
| 93 | // A non-fallback <family> tag must have a canonical name attribute. |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 94 | // A fallback <family> tag has no name, and may have lang and variant |
| 95 | // attributes. |
| 96 | family->fIsFallbackFont = true; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 97 | for (size_t i = 0; attributes[i] != NULL && |
| 98 | attributes[i+1] != NULL; i += 2) { |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 99 | const char* name = attributes[i]; |
| 100 | const char* value = attributes[i+1]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 101 | size_t nameLen = strlen(name); |
| 102 | size_t valueLen = strlen(value); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 103 | if (nameLen == 4 && !strncmp("name", name, nameLen)) { |
tomhudson | b3d4af5 | 2014-08-12 07:42:16 -0700 | [diff] [blame] | 104 | SkAutoAsciiToLC tolc(value); |
| 105 | family->fNames.push_back().set(tolc.lc()); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 106 | family->fIsFallbackFont = false; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 107 | } else if (nameLen == 4 && !strncmp("lang", name, nameLen)) { |
| 108 | family->fLanguage = SkLanguage (value); |
| 109 | } else if (nameLen == 7 && !strncmp("variant", name, nameLen)) { |
| 110 | // Value should be either elegant or compact. |
| 111 | if (valueLen == 7 && !strncmp("elegant", value, valueLen)) { |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 112 | family->fVariant = SkPaintOptionsAndroid::kElegant_Variant; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 113 | } else if (valueLen == 7 && !strncmp("compact", value, valueLen)) { |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 114 | family->fVariant = SkPaintOptionsAndroid::kCompact_Variant; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 120 | void fontFileNameHandler(void* data, const char* s, int len) { |
| 121 | FamilyData* familyData = (FamilyData*) data; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 122 | familyData->currentFontInfo->fFileName.set(s, len); |
| 123 | } |
| 124 | |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 125 | void familyElementEndHandler(FontFamily* family) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 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); |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 132 | void fontElementHandler(XML_Parser* parser, FontFileInfo* file, const char** attributes) { |
| 133 | // A <font> should have weight (integer) and style (normal, italic) attributes. |
| 134 | // NOTE: we ignore the style. |
| 135 | // The element should contain a filename. |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 136 | for (size_t i = 0; attributes[i] != NULL && |
| 137 | attributes[i+1] != NULL; i += 2) { |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 138 | const char* name = attributes[i]; |
| 139 | const char* value = attributes[i+1]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 140 | size_t nameLen = strlen(name); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 141 | if (nameLen == 6 && !strncmp("weight", name, nameLen)) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 142 | if (!parseNonNegativeInteger(value, &file->fWeight)) { |
| 143 | SkDebugf("---- Font weight %s (INVALID)", value); |
| 144 | file->fWeight = 0; |
| 145 | } |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | XML_SetCharacterDataHandler(*parser, fontFileNameHandler); |
| 149 | } |
| 150 | |
| 151 | FontFamily* findFamily(FamilyData* familyData, const char* familyName) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 152 | size_t nameLen = strlen(familyName); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 153 | for (int i = 0; i < familyData->families.count(); i++) { |
| 154 | FontFamily* candidate = familyData->families[i]; |
| 155 | for (int j = 0; j < candidate->fNames.count(); j++) { |
| 156 | if (!strncmp(candidate->fNames[j].c_str(), familyName, nameLen) && |
| 157 | nameLen == strlen(candidate->fNames[j].c_str())) { |
| 158 | return candidate; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | void aliasElementHandler(FamilyData* familyData, const char** attributes) { |
| 167 | // An <alias> must have name and to attributes. |
| 168 | // It may have weight (integer). |
| 169 | // If it *does not* have a weight, it is a variant name for a <family>. |
| 170 | // If it *does* have a weight, it names the <font>(s) of a specific weight |
| 171 | // from a <family>. |
| 172 | |
| 173 | SkString aliasName; |
| 174 | SkString to; |
| 175 | int weight = 0; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 176 | for (size_t i = 0; attributes[i] != NULL && |
| 177 | attributes[i+1] != NULL; i += 2) { |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 178 | const char* name = attributes[i]; |
| 179 | const char* value = attributes[i+1]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 180 | size_t nameLen = strlen(name); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 181 | if (nameLen == 4 && !strncmp("name", name, nameLen)) { |
tomhudson | b3d4af5 | 2014-08-12 07:42:16 -0700 | [diff] [blame] | 182 | SkAutoAsciiToLC tolc(value); |
| 183 | aliasName.set(tolc.lc()); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 184 | } else if (nameLen == 2 && !strncmp("to", name, nameLen)) { |
| 185 | to.set(value); |
| 186 | } else if (nameLen == 6 && !strncmp("weight", name, nameLen)) { |
| 187 | parseNonNegativeInteger(value, &weight); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Assumes that the named family is already declared |
| 192 | FontFamily* targetFamily = findFamily(familyData, to.c_str()); |
| 193 | if (!targetFamily) { |
| 194 | SkDebugf("---- Font alias target %s (NOT FOUND)", to.c_str()); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if (weight) { |
| 199 | FontFamily* family = new FontFamily(); |
| 200 | family->fNames.push_back().set(aliasName); |
| 201 | |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 202 | for (int i = 0; i < targetFamily->fFonts.count(); i++) { |
| 203 | if (targetFamily->fFonts[i].fWeight == weight) { |
| 204 | family->fFonts.push_back(targetFamily->fFonts[i]); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | *familyData->families.append() = family; |
| 208 | } else { |
| 209 | targetFamily->fNames.push_back().set(aliasName); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | bool findWeight400(FontFamily* family) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 214 | for (int i = 0; i < family->fFonts.count(); i++) { |
| 215 | if (family->fFonts[i].fWeight == 400) { |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | bool desiredWeight(int weight) { |
| 223 | return (weight == 400 || weight == 700); |
| 224 | } |
| 225 | |
| 226 | int countDesiredWeight(FontFamily* family) { |
| 227 | int count = 0; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 228 | for (int i = 0; i < family->fFonts.count(); i++) { |
| 229 | if (desiredWeight(family->fFonts[i].fWeight)) { |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 230 | count++; |
| 231 | } |
| 232 | } |
| 233 | return count; |
| 234 | } |
| 235 | |
| 236 | // To meet Skia's expectations, any family that contains weight=400 |
| 237 | // fonts should *only* contain {400,700} |
| 238 | void purgeUndesiredWeights(FontFamily* family) { |
| 239 | int count = countDesiredWeight(family); |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 240 | for (int i = 1, j = 0; i < family->fFonts.count(); i++) { |
| 241 | if (desiredWeight(family->fFonts[j].fWeight)) { |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 242 | j++; |
| 243 | } |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 244 | if ((i != j) && desiredWeight(family->fFonts[i].fWeight)) { |
| 245 | family->fFonts[j] = family->fFonts[i]; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 248 | family->fFonts.resize_back(count); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void familysetElementEndHandler(FamilyData* familyData) { |
| 252 | for (int i = 0; i < familyData->families.count(); i++) { |
| 253 | if (findWeight400(familyData->families[i])) { |
| 254 | purgeUndesiredWeights(familyData->families[i]); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 259 | void startElementHandler(void* data, const char* tag, |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 260 | const char** attributes) { |
| 261 | FamilyData* familyData = (FamilyData*) data; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 262 | size_t len = strlen(tag); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 263 | if (len == 6 && !strncmp(tag, "family", len)) { |
| 264 | familyData->currentFamily = new FontFamily(); |
| 265 | familyElementHandler(familyData->currentFamily, attributes); |
| 266 | } else if (len == 4 && !strncmp(tag, "font", len)) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 267 | FontFileInfo* file = &familyData->currentFamily->fFonts.push_back(); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 268 | familyData->currentFontInfo = file; |
| 269 | fontElementHandler(familyData->parser, file, attributes); |
| 270 | } else if (len == 5 && !strncmp(tag, "alias", len)) { |
| 271 | aliasElementHandler(familyData, attributes); |
| 272 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void endElementHandler(void* data, const char* tag) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 276 | FamilyData* familyData = (FamilyData*) data; |
| 277 | size_t len = strlen(tag); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 278 | if (len == 9 && strncmp(tag, "familyset", len) == 0) { |
| 279 | familysetElementEndHandler(familyData); |
| 280 | } else if (len == 6 && strncmp(tag, "family", len) == 0) { |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 281 | familyElementEndHandler(familyData->currentFamily); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 282 | *familyData->families.append() = familyData->currentFamily; |
| 283 | familyData->currentFamily = NULL; |
| 284 | } else if (len == 4 && !strncmp(tag, "font", len)) { |
| 285 | XML_SetCharacterDataHandler(*familyData->parser, NULL); |
| 286 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | } // lmpParser |
| 290 | |
| 291 | namespace jbParser { |
| 292 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 293 | /** |
| 294 | * Handler for arbitrary text. This is used to parse the text inside each name |
| 295 | * or file tag. The resulting strings are put into the fNames or FontFileInfo arrays. |
| 296 | */ |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 297 | static void textHandler(void* data, const char* s, int len) { |
| 298 | FamilyData* familyData = (FamilyData*) data; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 299 | // Make sure we're in the right state to store this name information |
| 300 | if (familyData->currentFamily && |
| 301 | (familyData->currentTag == NAMESET_TAG || familyData->currentTag == FILESET_TAG)) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 302 | switch (familyData->currentTag) { |
bungeman | edb10e7 | 2014-07-28 08:19:45 -0700 | [diff] [blame] | 303 | case NAMESET_TAG: { |
| 304 | SkAutoAsciiToLC tolc(s, len); |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 305 | familyData->currentFamily->fNames.push_back().set(tolc.lc(), len); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 306 | break; |
bungeman | edb10e7 | 2014-07-28 08:19:45 -0700 | [diff] [blame] | 307 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 308 | case FILESET_TAG: |
| 309 | if (familyData->currentFontInfo) { |
commit-bot@chromium.org | 31db71d | 2014-04-04 18:14:39 +0000 | [diff] [blame] | 310 | familyData->currentFontInfo->fFileName.set(s, len); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 311 | } |
| 312 | break; |
| 313 | default: |
| 314 | // Noop - don't care about any text that's not in the Fonts or Names list |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Handler for font files. This processes the attributes for language and |
| 322 | * variants then lets textHandler handle the actual file name |
| 323 | */ |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 324 | static void fontFileElementHandler(FamilyData* familyData, const char** attributes) { |
| 325 | FontFileInfo& newFileInfo = familyData->currentFamily->fFonts.push_back(); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 326 | if (attributes) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 327 | size_t currentAttributeIndex = 0; |
| 328 | while (attributes[currentAttributeIndex] && |
| 329 | attributes[currentAttributeIndex + 1]) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 330 | const char* attributeName = attributes[currentAttributeIndex]; |
| 331 | const char* attributeValue = attributes[currentAttributeIndex+1]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 332 | size_t nameLength = strlen(attributeName); |
| 333 | size_t valueLength = strlen(attributeValue); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 334 | if (nameLength == 7 && strncmp(attributeName, "variant", nameLength) == 0) { |
| 335 | if (valueLength == 7 && strncmp(attributeValue, "elegant", valueLength) == 0) { |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 336 | newFileInfo.fPaintOptions.setFontVariant(SkPaintOptionsAndroid::kElegant_Variant); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 337 | } else if (valueLength == 7 && |
| 338 | strncmp(attributeValue, "compact", valueLength) == 0) { |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 339 | newFileInfo.fPaintOptions.setFontVariant(SkPaintOptionsAndroid::kCompact_Variant); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 340 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 341 | } else if (nameLength == 4 && strncmp(attributeName, "lang", nameLength) == 0) { |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 342 | newFileInfo.fPaintOptions.setLanguage(attributeValue); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 343 | } else if (nameLength == 5 && strncmp(attributeName, "index", nameLength) == 0) { |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 344 | int value; |
| 345 | if (parseNonNegativeInteger(attributeValue, &value)) { |
| 346 | newFileInfo.fIndex = value; |
| 347 | } else { |
| 348 | SkDebugf("---- SystemFonts index=%s (INVALID)", attributeValue); |
| 349 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 350 | } |
| 351 | //each element is a pair of attributeName/attributeValue string pairs |
| 352 | currentAttributeIndex += 2; |
| 353 | } |
| 354 | } |
commit-bot@chromium.org | 31db71d | 2014-04-04 18:14:39 +0000 | [diff] [blame] | 355 | familyData->currentFontInfo = &newFileInfo; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 356 | XML_SetCharacterDataHandler(*familyData->parser, textHandler); |
| 357 | } |
| 358 | |
| 359 | /** |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 360 | * Handler for the start of a tag. The only tags we expect are familyset, family, |
| 361 | * nameset, fileset, name, and file. |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 362 | */ |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 363 | static void startElementHandler(void* data, const char* tag, const char** atts) { |
| 364 | FamilyData* familyData = (FamilyData*) data; |
| 365 | size_t len = strlen(tag); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 366 | if (len == 9 && strncmp(tag, "familyset", len) == 0) { |
| 367 | // The familyset tag has an optional "version" attribute with an integer value >= 0 |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 368 | for (size_t i = 0; atts[i] != NULL && |
| 369 | atts[i+1] != NULL; i += 2) { |
| 370 | size_t nameLen = strlen(atts[i]); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 371 | if (nameLen == 7 && strncmp(atts[i], "version", nameLen)) continue; |
| 372 | const char* valueString = atts[i+1]; |
| 373 | int version; |
| 374 | if (parseNonNegativeInteger(valueString, &version) && (version >= 21)) { |
| 375 | XML_SetElementHandler(*familyData->parser, |
| 376 | lmpParser::startElementHandler, |
| 377 | lmpParser::endElementHandler); |
| 378 | } |
| 379 | } |
| 380 | } else if (len == 6 && strncmp(tag, "family", len) == 0) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 381 | familyData->currentFamily = new FontFamily(); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 382 | // The Family tag has an optional "order" attribute with an integer value >= 0 |
| 383 | // If this attribute does not exist, the default value is -1 |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 384 | for (size_t i = 0; atts[i] != NULL && |
| 385 | atts[i+1] != NULL; i += 2) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 386 | const char* valueString = atts[i+1]; |
| 387 | int value; |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 388 | if (parseNonNegativeInteger(valueString, &value)) { |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 389 | familyData->currentFamily->fOrder = value; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } else if (len == 7 && strncmp(tag, "nameset", len) == 0) { |
| 393 | familyData->currentTag = NAMESET_TAG; |
| 394 | } else if (len == 7 && strncmp(tag, "fileset", len) == 0) { |
| 395 | familyData->currentTag = FILESET_TAG; |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 396 | } else if (len == 4 && strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 397 | // If it's a Name, parse the text inside |
| 398 | XML_SetCharacterDataHandler(*familyData->parser, textHandler); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 399 | } else if (len == 4 && strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 400 | // If it's a file, parse the attributes, then parse the text inside |
| 401 | fontFileElementHandler(familyData, atts); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Handler for the end of tags. We only care about family, nameset, fileset, |
| 407 | * name, and file. |
| 408 | */ |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 409 | static void endElementHandler(void* data, const char* tag) { |
| 410 | FamilyData* familyData = (FamilyData*) data; |
| 411 | size_t len = strlen(tag); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 412 | if (len == 6 && strncmp(tag, "family", len)== 0) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 413 | // Done parsing a Family - store the created currentFamily in the families array |
| 414 | *familyData->families.append() = familyData->currentFamily; |
| 415 | familyData->currentFamily = NULL; |
| 416 | } else if (len == 7 && strncmp(tag, "nameset", len) == 0) { |
| 417 | familyData->currentTag = NO_TAG; |
| 418 | } else if (len == 7 && strncmp(tag, "fileset", len) == 0) { |
| 419 | familyData->currentTag = NO_TAG; |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 420 | } else if ((len == 4 && |
| 421 | strncmp(tag, "name", len) == 0 && |
| 422 | familyData->currentTag == NAMESET_TAG) || |
| 423 | (len == 4 && |
| 424 | strncmp(tag, "file", len) == 0 && |
| 425 | familyData->currentTag == FILESET_TAG)) { |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 426 | // Disable the arbitrary text handler installed to load Name data |
| 427 | XML_SetCharacterDataHandler(*familyData->parser, NULL); |
| 428 | } |
| 429 | } |
| 430 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 431 | } // namespace jbParser |
| 432 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 433 | /** |
| 434 | * This function parses the given filename and stores the results in the given |
| 435 | * families array. |
| 436 | */ |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 437 | static void parseConfigFile(const char* filename, SkTDArray<FontFamily*> &families) { |
djsollen@google.com | 50c9567 | 2013-08-28 12:29:45 +0000 | [diff] [blame] | 438 | |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 439 | FILE* file = fopen(filename, "r"); |
djsollen@google.com | 50c9567 | 2013-08-28 12:29:45 +0000 | [diff] [blame] | 440 | |
| 441 | // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml) |
| 442 | // are optional - failure here is okay because one of these optional files may not exist. |
| 443 | if (NULL == file) { |
| 444 | return; |
| 445 | } |
| 446 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 447 | XML_Parser parser = XML_ParserCreate(NULL); |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 448 | FamilyData* familyData = new FamilyData(&parser, families); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 449 | XML_SetUserData(parser, familyData); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 450 | // Start parsing oldschool; switch these in flight if we detect a newer version of the file. |
| 451 | XML_SetElementHandler(parser, jbParser::startElementHandler, jbParser::endElementHandler); |
djsollen@google.com | 50c9567 | 2013-08-28 12:29:45 +0000 | [diff] [blame] | 452 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 453 | char buffer[512]; |
| 454 | bool done = false; |
| 455 | while (!done) { |
| 456 | fgets(buffer, sizeof(buffer), file); |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 457 | size_t len = strlen(buffer); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 458 | if (feof(file) != 0) { |
| 459 | done = true; |
| 460 | } |
| 461 | XML_Parse(parser, buffer, len, done); |
| 462 | } |
djsollen@google.com | 286a883 | 2013-09-18 18:59:29 +0000 | [diff] [blame] | 463 | XML_ParserFree(parser); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 464 | fclose(file); |
| 465 | } |
| 466 | |
| 467 | static void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { |
tomhudson | 7a4747f | 2014-08-13 11:06:43 -0700 | [diff] [blame^] | 468 | int initialCount = fontFamilies.count(); |
tomhudson | 94fa4b9 | 2014-08-12 11:05:29 -0700 | [diff] [blame] | 469 | parseConfigFile(LMP_SYSTEM_FONTS_FILE, fontFamilies); |
| 470 | |
tomhudson | 7a4747f | 2014-08-13 11:06:43 -0700 | [diff] [blame^] | 471 | if (initialCount == fontFamilies.count()) { |
tomhudson | 94fa4b9 | 2014-08-12 11:05:29 -0700 | [diff] [blame] | 472 | parseConfigFile(OLD_SYSTEM_FONTS_FILE, fontFamilies); |
| 473 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 474 | } |
| 475 | |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 476 | /** |
| 477 | * In some versions of Android prior to Android 4.2 (JellyBean MR1 at API |
| 478 | * Level 17) the fallback fonts for certain locales were encoded in their own |
| 479 | * XML files with a suffix that identified the locale. We search the provided |
| 480 | * directory for those files,add all of their entries to the fallback chain, and |
| 481 | * include the locale as part of each entry. |
| 482 | */ |
| 483 | static void getFallbackFontFamiliesForLocale(SkTDArray<FontFamily*> &fallbackFonts, const char* dir) { |
| 484 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 485 | // The framework is beyond Android 4.2 and can therefore skip this function |
| 486 | return; |
| 487 | #endif |
| 488 | |
| 489 | DIR* fontDirectory = opendir(dir); |
| 490 | if (fontDirectory != NULL){ |
| 491 | struct dirent* dirEntry = readdir(fontDirectory); |
| 492 | while (dirEntry) { |
| 493 | |
| 494 | // The size of both the prefix, suffix, and a minimum valid language code |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 495 | static const size_t minSize = strlen(LOCALE_FALLBACK_FONTS_PREFIX) + |
| 496 | strlen(LOCALE_FALLBACK_FONTS_SUFFIX) + 2; |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 497 | |
| 498 | SkString fileName(dirEntry->d_name); |
| 499 | if (fileName.size() >= minSize && |
| 500 | fileName.startsWith(LOCALE_FALLBACK_FONTS_PREFIX) && |
| 501 | fileName.endsWith(LOCALE_FALLBACK_FONTS_SUFFIX)) { |
| 502 | |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 503 | static const size_t fixedLen = strlen(LOCALE_FALLBACK_FONTS_PREFIX) - |
| 504 | strlen(LOCALE_FALLBACK_FONTS_SUFFIX); |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 505 | |
| 506 | SkString locale(fileName.c_str() - strlen(LOCALE_FALLBACK_FONTS_PREFIX), |
| 507 | fileName.size() - fixedLen); |
| 508 | |
| 509 | SkString absoluteFilename; |
| 510 | absoluteFilename.printf("%s/%s", dir, fileName.c_str()); |
| 511 | |
| 512 | SkTDArray<FontFamily*> langSpecificFonts; |
| 513 | parseConfigFile(absoluteFilename.c_str(), langSpecificFonts); |
| 514 | |
| 515 | for (int i = 0; i < langSpecificFonts.count(); ++i) { |
| 516 | FontFamily* family = langSpecificFonts[i]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 517 | for (int j = 0; j < family->fFonts.count(); ++j) { |
| 518 | family->fFonts[j].fPaintOptions.setLanguage(locale); |
Derek Sollenberger | da7a944 | 2014-08-06 16:34:40 -0400 | [diff] [blame] | 519 | } |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 520 | *fallbackFonts.append() = family; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // proceed to the next entry in the directory |
| 525 | dirEntry = readdir(fontDirectory); |
| 526 | } |
| 527 | // cleanup the directory reference |
| 528 | closedir(fontDirectory); |
| 529 | } |
| 530 | } |
| 531 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 532 | static void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) { |
| 533 | SkTDArray<FontFamily*> vendorFonts; |
| 534 | parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts); |
| 535 | parseConfigFile(VENDOR_FONTS_FILE, vendorFonts); |
| 536 | |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 537 | getFallbackFontFamiliesForLocale(fallbackFonts, LOCALE_FALLBACK_FONTS_SYSTEM_DIR); |
| 538 | getFallbackFontFamiliesForLocale(vendorFonts, LOCALE_FALLBACK_FONTS_VENDOR_DIR); |
| 539 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 540 | // This loop inserts the vendor fallback fonts in the correct order in the |
| 541 | // overall fallbacks list. |
| 542 | int currentOrder = -1; |
| 543 | for (int i = 0; i < vendorFonts.count(); ++i) { |
| 544 | FontFamily* family = vendorFonts[i]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 545 | int order = family->fOrder; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 546 | if (order < 0) { |
| 547 | if (currentOrder < 0) { |
| 548 | // Default case - just add it to the end of the fallback list |
| 549 | *fallbackFonts.append() = family; |
| 550 | } else { |
| 551 | // no order specified on this font, but we're incrementing the order |
| 552 | // based on an earlier order insertion request |
| 553 | *fallbackFonts.insert(currentOrder++) = family; |
| 554 | } |
| 555 | } else { |
| 556 | // Add the font into the fallback list in the specified order. Set |
| 557 | // currentOrder for correct placement of other fonts in the vendor list. |
| 558 | *fallbackFonts.insert(order) = family; |
| 559 | currentOrder = order + 1; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Loads data on font families from various expected configuration files. The |
| 566 | * resulting data is returned in the given fontFamilies array. |
| 567 | */ |
| 568 | void SkFontConfigParser::GetFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { |
| 569 | |
| 570 | getSystemFontFamilies(fontFamilies); |
| 571 | |
| 572 | // Append all the fallback fonts to system fonts |
| 573 | SkTDArray<FontFamily*> fallbackFonts; |
| 574 | getFallbackFontFamilies(fallbackFonts); |
| 575 | for (int i = 0; i < fallbackFonts.count(); ++i) { |
| 576 | fallbackFonts[i]->fIsFallbackFont = true; |
| 577 | *fontFamilies.append() = fallbackFonts[i]; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void SkFontConfigParser::GetTestFontFamilies(SkTDArray<FontFamily*> &fontFamilies, |
| 582 | const char* testMainConfigFile, |
| 583 | const char* testFallbackConfigFile) { |
| 584 | parseConfigFile(testMainConfigFile, fontFamilies); |
| 585 | |
| 586 | SkTDArray<FontFamily*> fallbackFonts; |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 587 | if (NULL != testFallbackConfigFile) { |
| 588 | parseConfigFile(testFallbackConfigFile, fallbackFonts); |
| 589 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 590 | |
| 591 | // Append all fallback fonts to system fonts |
| 592 | for (int i = 0; i < fallbackFonts.count(); ++i) { |
| 593 | fallbackFonts[i]->fIsFallbackFont = true; |
| 594 | *fontFamilies.append() = fallbackFonts[i]; |
| 595 | } |
| 596 | } |