| 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 |
// Despite the name and location, this is portable code. |
| 9 |
|
| 10 |
#include "include/core/SkFontMgr.h" |
| 11 |
#include "include/core/SkStream.h" |
| 12 |
#include "include/private/base/SkFixed.h" |
| 13 |
#include "include/private/base/SkMalloc.h" |
| 14 |
#include "include/private/base/SkTDArray.h" |
| 15 |
#include "include/private/base/SkTLogic.h" |
| 16 |
#include "include/private/base/SkTemplates.h" |
| 17 |
#include "src/base/SkTSearch.h" |
| 18 |
#include "src/core/SkOSFile.h" |
| 19 |
#include "src/ports/SkFontMgr_android_parser.h" |
| 20 |
|
| 21 |
#include <expat.h> |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <memory> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
using namespace skia_private; |
| 30 |
|
| 31 |
#define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" |
| 32 |
#define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" |
| 33 |
#define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
| 34 |
#define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" |
| 35 |
|
| 36 |
#define LOCALE_FALLBACK_FONTS_SYSTEM_DIR "/system/etc" |
| 37 |
#define LOCALE_FALLBACK_FONTS_VENDOR_DIR "/vendor/etc" |
| 38 |
#define LOCALE_FALLBACK_FONTS_PREFIX "fallback_fonts-" |
| 39 |
#define LOCALE_FALLBACK_FONTS_SUFFIX ".xml" |
| 40 |
|
| 41 |
/** |
| 42 |
* This file contains TWO 'familyset' handlers: |
| 43 |
* One for JB and earlier which works with |
| 44 |
* /system/etc/system_fonts.xml |
| 45 |
* /system/etc/fallback_fonts.xml |
| 46 |
* /vendor/etc/fallback_fonts.xml |
| 47 |
* /system/etc/fallback_fonts-XX.xml |
| 48 |
* /vendor/etc/fallback_fonts-XX.xml |
| 49 |
* and the other for LMP and later which works with |
| 50 |
* /system/etc/fonts.xml |
| 51 |
* |
| 52 |
* If the 'familyset' 'version' attribute is 21 or higher the LMP parser is used, otherwise the JB. |
| 53 |
* |
| 54 |
* API 15 4.0.4_r2.1 system_fonts.xml, vendor_fonts.xml, fallback_fonts.xml (system+vendor) no lang. |
| 55 |
* API 16 4.1.1_r1 fallback_fonts-xx-XX.xml are added. Use the xx list in order. |
| 56 |
* API 17 4.2.2_r1.1 fallback_fonts-xx.xml are removed and 'lang' is added. |
| 57 |
* API 21 5.0.0_r1.0.1 fonts.xml replaces the other files. |
| 58 |
* API 29 10.0.0_r1 /product/etc/fonts_customization.xml with base /product/fonts is added. |
| 59 |
* The NDK interface is added and reading the files directly is discouraged. |
| 60 |
*/ |
| 61 |
|
| 62 |
struct FamilyData; |
| 63 |
|
| 64 |
struct TagHandler { |
| 65 |
/** Called at the start tag. |
| 66 |
* Called immediately after the parent tag retuns this handler from a call to 'tag'. |
| 67 |
* Allows setting up for handling the tag content and processing attributes. |
| 68 |
* If nullptr, will not be called. |
| 69 |
*/ |
| 70 |
void (*start)(FamilyData* data, const char* tag, const char** attributes); |
| 71 |
|
| 72 |
/** Called at the end tag. |
| 73 |
* Allows post-processing of any accumulated information. |
| 74 |
* This will be the last call made in relation to the current tag. |
| 75 |
* If nullptr, will not be called. |
| 76 |
*/ |
| 77 |
void (*end)(FamilyData* data, const char* tag); |
| 78 |
|
| 79 |
/** Called when a nested tag is encountered. |
| 80 |
* This is responsible for determining how to handle the tag. |
| 81 |
* If the tag is not recognized, return nullptr to skip the tag. |
| 82 |
* If nullptr, all nested tags will be skipped. |
| 83 |
*/ |
| 84 |
const TagHandler* (*tag)(FamilyData* data, const char* tag, const char** attributes); |
| 85 |
|
| 86 |
/** The character handler for this tag. |
| 87 |
* This is only active for character data contained directly in this tag (not sub-tags). |
| 88 |
* The first parameter will be castable to a FamilyData*. |
| 89 |
* If nullptr, any character data in this tag will be ignored. |
| 90 |
*/ |
| 91 |
XML_CharacterDataHandler chars; |
| 92 |
}; |
| 93 |
|
| 94 |
/** Represents the current parsing state. */ |
| 95 |
struct FamilyData { |
| 96 |
FamilyData(XML_Parser parser, std::vector<std::unique_ptr<FontFamily>>& families, |
| 97 |
const SkString& basePath, bool isFallback, const char* filename, |
| 98 |
const TagHandler* topLevelHandler) |
| 99 |
: fParser(parser) |
| 100 |
, fFamilies(families) |
| 101 |
, fCurrentFamily(nullptr) |
| 102 |
, fCurrentFontInfo(nullptr) |
| 103 |
, fVersion(0) |
| 104 |
, fBasePath(basePath) |
| 105 |
, fIsFallback(isFallback) |
| 106 |
, fFilename(filename) |
| 107 |
, fDepth(1) |
| 108 |
, fSkip(0) |
| 109 |
, fHandler(&topLevelHandler, 1) |
| 110 |
{ } |
| 111 |
|
| 112 |
XML_Parser fParser; // The expat parser doing the work, owned by caller |
| 113 |
std::vector<std::unique_ptr<FontFamily>>& fFamilies; // Collection of families, owned by caller |
| 114 |
std::unique_ptr<FontFamily> fCurrentFamily; // The family being created, owned by this |
| 115 |
FontFileInfo* fCurrentFontInfo; // The info being created, owned by fCurrentFamily |
| 116 |
int fVersion; // The version of the file parsed. |
| 117 |
const SkString& fBasePath; // The current base path. |
| 118 |
const bool fIsFallback; // The file being parsed is a fallback file |
| 119 |
const char* fFilename; // The name of the file currently being parsed. |
| 120 |
|
| 121 |
int fDepth; // The current element depth of the parse. |
| 122 |
int fSkip; // The depth to stop skipping, 0 if not skipping. |
| 123 |
SkTDArray<const TagHandler*> fHandler; // The stack of current tag handlers. |
| 124 |
}; |
| 125 |
|
| 126 |
static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { |
| 127 |
return n1 == n2 && 0 == memcmp(s1, s2, n1); |
| 128 |
} |
| 129 |
#define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) |
| 130 |
|
| 131 |
#define ATTS_NON_NULL(a, i) (a[i] != nullptr && a[i+1] != nullptr) |
| 132 |
|
| 133 |
#define SK_FONTMGR_ANDROID_PARSER_PREFIX "[SkFontMgr Android Parser] " |
| 134 |
|
| 135 |
#define SK_FONTCONFIGPARSER_WARNING(message, ...) \ |
| 136 |
SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "%s:%d:%d: warning: " message "\n", \ |
| 137 |
self->fFilename, \ |
| 138 |
(int)XML_GetCurrentLineNumber(self->fParser), \ |
| 139 |
(int)XML_GetCurrentColumnNumber(self->fParser), \ |
| 140 |
##__VA_ARGS__) |
| 141 |
|
| 142 |
static bool is_whitespace(char c) { |
| 143 |
return c == ' ' || c == '\n'|| c == '\r' || c == '\t'; |
| 144 |
} |
| 145 |
|
| 146 |
static void trim_string(SkString* s) { |
| 147 |
char* str = s->data(); |
| 148 |
const char* start = str; // start is inclusive |
| 149 |
const char* end = start + s->size(); // end is exclusive |
| 150 |
while (is_whitespace(*start)) { ++start; } |
| 151 |
if (start != end) { |
| 152 |
--end; // make end inclusive |
| 153 |
while (is_whitespace(*end)) { --end; } |
| 154 |
++end; // make end exclusive |
| 155 |
} |
| 156 |
size_t len = end - start; |
| 157 |
memmove(str, start, len); |
| 158 |
s->resize(len); |
| 159 |
} |
| 160 |
|
| 161 |
static void parse_space_separated_languages(const char* value, size_t valueLen, |
| 162 |
TArray<SkLanguage, true>& languages) |
| 163 |
{ |
| 164 |
size_t i = 0; |
| 165 |
while (true) { |
| 166 |
for (; i < valueLen && is_whitespace(value[i]); ++i) { } |
| 167 |
if (i == valueLen) { break; } |
| 168 |
size_t j; |
| 169 |
for (j = i + 1; j < valueLen && !is_whitespace(value[j]); ++j) { } |
| 170 |
languages.emplace_back(value + i, j - i); |
| 171 |
i = j; |
| 172 |
if (i == valueLen) { break; } |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
namespace lmpParser { |
| 177 |
|
| 178 |
static const TagHandler axisHandler = { |
| 179 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 180 |
FontFileInfo& file = *self->fCurrentFontInfo; |
| 181 |
SkFourByteTag axisTag = SkSetFourByteTag('\0','\0','\0','\0'); |
| 182 |
SkFixed axisStyleValue = 0; |
| 183 |
bool axisTagIsValid = false; |
| 184 |
bool axisStyleValueIsValid = false; |
| 185 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 186 |
const char* name = attributes[i]; |
| 187 |
const char* value = attributes[i+1]; |
| 188 |
size_t nameLen = strlen(name); |
| 189 |
if (MEMEQ("tag", name, nameLen)) { |
| 190 |
size_t valueLen = strlen(value); |
| 191 |
if (valueLen == 4) { |
| 192 |
axisTag = SkSetFourByteTag(value[0], value[1], value[2], value[3]); |
| 193 |
axisTagIsValid = true; |
| 194 |
for (int j = 0; j < file.fVariationDesignPosition.size() - 1; ++j) { |
| 195 |
if (file.fVariationDesignPosition[j].axis == axisTag) { |
| 196 |
axisTagIsValid = false; |
| 197 |
SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified more than once", |
| 198 |
(char)((axisTag >> 24) & 0xFF), |
| 199 |
(char)((axisTag >> 16) & 0xFF), |
| 200 |
(char)((axisTag >> 8) & 0xFF), |
| 201 |
(char)((axisTag ) & 0xFF)); |
| 202 |
} |
| 203 |
} |
| 204 |
} else { |
| 205 |
SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value); |
| 206 |
} |
| 207 |
} else if (MEMEQ("stylevalue", name, nameLen)) { |
| 208 |
if (parse_fixed<16>(value, &axisStyleValue)) { |
| 209 |
axisStyleValueIsValid = true; |
| 210 |
} else { |
| 211 |
SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue", value); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
if (axisTagIsValid && axisStyleValueIsValid) { |
| 216 |
auto& coordinate = file.fVariationDesignPosition.push_back(); |
| 217 |
coordinate.axis = axisTag; |
| 218 |
coordinate.value = SkFixedToScalar(axisStyleValue); |
| 219 |
} |
| 220 |
}, |
| 221 |
/*end*/nullptr, |
| 222 |
/*tag*/nullptr, |
| 223 |
/*chars*/nullptr, |
| 224 |
}; |
| 225 |
|
| 226 |
static const TagHandler fontHandler = { |
| 227 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 228 |
// 'weight' (non-negative integer) [default 0] |
| 229 |
// 'style' ("normal", "italic") [default "auto"] |
| 230 |
// 'index' (non-negative integer) [default 0] |
| 231 |
// The character data should be a filename. |
| 232 |
FontFileInfo& file = self->fCurrentFamily->fFonts.push_back(); |
| 233 |
self->fCurrentFontInfo = &file; |
| 234 |
SkString fallbackFor; |
| 235 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 236 |
const char* name = attributes[i]; |
| 237 |
const char* value = attributes[i+1]; |
| 238 |
size_t nameLen = strlen(name); |
| 239 |
if (MEMEQ("weight", name, nameLen)) { |
| 240 |
if (!parse_non_negative_integer(value, &file.fWeight)) { |
| 241 |
SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid weight", value); |
| 242 |
} |
| 243 |
} else if (MEMEQ("style", name, nameLen)) { |
| 244 |
size_t valueLen = strlen(value); |
| 245 |
if (MEMEQ("normal", value, valueLen)) { |
| 246 |
file.fStyle = FontFileInfo::Style::kNormal; |
| 247 |
} else if (MEMEQ("italic", value, valueLen)) { |
| 248 |
file.fStyle = FontFileInfo::Style::kItalic; |
| 249 |
} |
| 250 |
} else if (MEMEQ("index", name, nameLen)) { |
| 251 |
if (!parse_non_negative_integer(value, &file.fIndex)) { |
| 252 |
SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid index", value); |
| 253 |
} |
| 254 |
} else if (MEMEQ("fallbackFor", name, nameLen)) { |
| 255 |
/** fallbackFor specifies a family fallback and should have been on family. */ |
| 256 |
fallbackFor = value; |
| 257 |
} |
| 258 |
} |
| 259 |
if (!fallbackFor.isEmpty()) { |
| 260 |
std::unique_ptr<FontFamily>* fallbackFamily = |
| 261 |
self->fCurrentFamily->fallbackFamilies.find(fallbackFor); |
| 262 |
if (!fallbackFamily) { |
| 263 |
std::unique_ptr<FontFamily> newFallbackFamily( |
| 264 |
new FontFamily(self->fCurrentFamily->fBasePath, true)); |
| 265 |
fallbackFamily = self->fCurrentFamily->fallbackFamilies.set( |
| 266 |
fallbackFor, std::move(newFallbackFamily)); |
| 267 |
(*fallbackFamily)->fLanguages = self->fCurrentFamily->fLanguages; |
| 268 |
(*fallbackFamily)->fVariant = self->fCurrentFamily->fVariant; |
| 269 |
(*fallbackFamily)->fOrder = self->fCurrentFamily->fOrder; |
| 270 |
(*fallbackFamily)->fFallbackFor = fallbackFor; |
| 271 |
} |
| 272 |
self->fCurrentFontInfo = &(*fallbackFamily)->fFonts.emplace_back(file); |
| 273 |
self->fCurrentFamily->fFonts.pop_back(); |
| 274 |
} |
| 275 |
}, |
| 276 |
/*end*/[](FamilyData* self, const char* tag) { |
| 277 |
trim_string(&self->fCurrentFontInfo->fFileName); |
| 278 |
}, |
| 279 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 280 |
size_t len = strlen(tag); |
| 281 |
if (MEMEQ("axis", tag, len)) { |
| 282 |
return &axisHandler; |
| 283 |
} |
| 284 |
return nullptr; |
| 285 |
}, |
| 286 |
/*chars*/[](void* data, const char* s, int len) { |
| 287 |
FamilyData* self = static_cast<FamilyData*>(data); |
| 288 |
self->fCurrentFontInfo->fFileName.append(s, len); |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
static const TagHandler familyHandler = { |
| 293 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 294 |
// 'name' (string) [optional] |
| 295 |
// 'lang' (space separated string) [default ""] |
| 296 |
// 'variant' ("elegant", "compact") [default "default"] |
| 297 |
// If there is no name, this is a fallback only font. |
| 298 |
FontFamily* family = new FontFamily(self->fBasePath, true); |
| 299 |
self->fCurrentFamily.reset(family); |
| 300 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 301 |
const char* name = attributes[i]; |
| 302 |
const char* value = attributes[i+1]; |
| 303 |
size_t nameLen = strlen(name); |
| 304 |
size_t valueLen = strlen(value); |
| 305 |
if (MEMEQ("name", name, nameLen)) { |
| 306 |
SkAutoAsciiToLC tolc(value); |
| 307 |
family->fNames.push_back().set(tolc.lc()); |
| 308 |
family->fIsFallbackFont = false; |
| 309 |
} else if (MEMEQ("lang", name, nameLen)) { |
| 310 |
parse_space_separated_languages(value, valueLen, family->fLanguages); |
| 311 |
} else if (MEMEQ("variant", name, nameLen)) { |
| 312 |
if (MEMEQ("elegant", value, valueLen)) { |
| 313 |
family->fVariant = kElegant_FontVariant; |
| 314 |
} else if (MEMEQ("compact", value, valueLen)) { |
| 315 |
family->fVariant = kCompact_FontVariant; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
}, |
| 320 |
/*end*/[](FamilyData* self, const char* tag) { |
| 321 |
self->fFamilies.push_back(std::move(self->fCurrentFamily)); |
| 322 |
}, |
| 323 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 324 |
size_t len = strlen(tag); |
| 325 |
if (MEMEQ("font", tag, len)) { |
| 326 |
return &fontHandler; |
| 327 |
} |
| 328 |
return nullptr; |
| 329 |
}, |
| 330 |
/*chars*/nullptr, |
| 331 |
}; |
| 332 |
|
| 333 |
static FontFamily* find_family(FamilyData* self, const SkString& familyName) { |
| 334 |
for (std::unique_ptr<FontFamily>& candidate : self->fFamilies) { |
| 335 |
for (const SkString& candidateName : candidate->fNames) { |
| 336 |
if (candidateName == familyName) { |
| 337 |
return &*candidate; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
return nullptr; |
| 342 |
} |
| 343 |
|
| 344 |
static const TagHandler aliasHandler = { |
| 345 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 346 |
// 'name' (string) introduces a new family name. |
| 347 |
// 'to' (string) specifies which (previous) family to alias |
| 348 |
// 'weight' (non-negative integer) [optional] |
| 349 |
// If it *does not* have a weight, 'name' is an alias for the entire 'to' family. |
| 350 |
// If it *does* have a weight, 'name' is a new family consisting of |
| 351 |
// the font(s) with 'weight' from the 'to' family. |
| 352 |
|
| 353 |
SkString aliasName; |
| 354 |
SkString to; |
| 355 |
int weight = 0; |
| 356 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 357 |
const char* name = attributes[i]; |
| 358 |
const char* value = attributes[i+1]; |
| 359 |
size_t nameLen = strlen(name); |
| 360 |
if (MEMEQ("name", name, nameLen)) { |
| 361 |
SkAutoAsciiToLC tolc(value); |
| 362 |
aliasName.set(tolc.lc()); |
| 363 |
} else if (MEMEQ("to", name, nameLen)) { |
| 364 |
to.set(value); |
| 365 |
} else if (MEMEQ("weight", name, nameLen)) { |
| 366 |
if (!parse_non_negative_integer(value, &weight)) { |
| 367 |
SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid weight", value); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
// Assumes that the named family is already declared |
| 373 |
FontFamily* targetFamily = find_family(self, to); |
| 374 |
if (!targetFamily) { |
| 375 |
SK_FONTCONFIGPARSER_WARNING("'%s' alias target not found", to.c_str()); |
| 376 |
return; |
| 377 |
} |
| 378 |
|
| 379 |
if (weight) { |
| 380 |
std::unique_ptr<FontFamily> family( |
| 381 |
new FontFamily(targetFamily->fBasePath, self->fIsFallback)); |
| 382 |
family->fNames.push_back().set(aliasName); |
| 383 |
|
| 384 |
for (int i = 0; i < targetFamily->fFonts.size(); i++) { |
| 385 |
if (targetFamily->fFonts[i].fWeight == weight) { |
| 386 |
family->fFonts.push_back(targetFamily->fFonts[i]); |
| 387 |
} |
| 388 |
} |
| 389 |
self->fFamilies.push_back(std::move(family)); |
| 390 |
} else { |
| 391 |
targetFamily->fNames.push_back().set(aliasName); |
| 392 |
} |
| 393 |
}, |
| 394 |
/*end*/nullptr, |
| 395 |
/*tag*/nullptr, |
| 396 |
/*chars*/nullptr, |
| 397 |
}; |
| 398 |
|
| 399 |
static const TagHandler familySetHandler = { |
| 400 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { }, |
| 401 |
/*end*/nullptr, |
| 402 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 403 |
size_t len = strlen(tag); |
| 404 |
if (MEMEQ("family", tag, len)) { |
| 405 |
return &familyHandler; |
| 406 |
} else if (MEMEQ("alias", tag, len)) { |
| 407 |
return &aliasHandler; |
| 408 |
} |
| 409 |
return nullptr; |
| 410 |
}, |
| 411 |
/*chars*/nullptr, |
| 412 |
}; |
| 413 |
|
| 414 |
} // namespace lmpParser |
| 415 |
|
| 416 |
namespace jbParser { |
| 417 |
|
| 418 |
static const TagHandler fileHandler = { |
| 419 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 420 |
// 'variant' ("elegant", "compact") [default "default"] |
| 421 |
// 'lang' (string) [default ""] |
| 422 |
// 'index' (non-negative integer) [default 0] |
| 423 |
// The character data should be a filename. |
| 424 |
FontFamily& currentFamily = *self->fCurrentFamily; |
| 425 |
FontFileInfo& newFileInfo = currentFamily.fFonts.push_back(); |
| 426 |
if (attributes) { |
| 427 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 428 |
const char* name = attributes[i]; |
| 429 |
const char* value = attributes[i+1]; |
| 430 |
size_t nameLen = strlen(name); |
| 431 |
size_t valueLen = strlen(value); |
| 432 |
if (MEMEQ("variant", name, nameLen)) { |
| 433 |
const FontVariant prevVariant = currentFamily.fVariant; |
| 434 |
if (MEMEQ("elegant", value, valueLen)) { |
| 435 |
currentFamily.fVariant = kElegant_FontVariant; |
| 436 |
} else if (MEMEQ("compact", value, valueLen)) { |
| 437 |
currentFamily.fVariant = kCompact_FontVariant; |
| 438 |
} |
| 439 |
if (currentFamily.fFonts.size() > 1 && currentFamily.fVariant != prevVariant) { |
| 440 |
SK_FONTCONFIGPARSER_WARNING("'%s' unexpected variant found\n" |
| 441 |
"Note: Every font file within a family must have identical variants.", |
| 442 |
value); |
| 443 |
} |
| 444 |
|
| 445 |
} else if (MEMEQ("lang", name, nameLen)) { // JB MR1 (API level 17) |
| 446 |
SkLanguage currentLanguage = SkLanguage(value, valueLen); |
| 447 |
bool showWarning = false; |
| 448 |
if (currentFamily.fLanguages.empty()) { |
| 449 |
showWarning = (currentFamily.fFonts.size() > 1); |
| 450 |
currentFamily.fLanguages.push_back(std::move(currentLanguage)); |
| 451 |
} else if (currentFamily.fLanguages[0] != currentLanguage) { |
| 452 |
showWarning = true; |
| 453 |
currentFamily.fLanguages[0] = std::move(currentLanguage); |
| 454 |
} |
| 455 |
if (showWarning) { |
| 456 |
SK_FONTCONFIGPARSER_WARNING("'%s' unexpected language found\n" |
| 457 |
"Note: Every font file within a family must have identical languages.", |
| 458 |
value); |
| 459 |
} |
| 460 |
|
| 461 |
} else if (MEMEQ("index", name, nameLen)) { |
| 462 |
if (!parse_non_negative_integer(value, &newFileInfo.fIndex)) { |
| 463 |
SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid index", value); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
self->fCurrentFontInfo = &newFileInfo; |
| 469 |
}, |
| 470 |
/*end*/nullptr, |
| 471 |
/*tag*/nullptr, |
| 472 |
/*chars*/[](void* data, const char* s, int len) { |
| 473 |
FamilyData* self = static_cast<FamilyData*>(data); |
| 474 |
self->fCurrentFontInfo->fFileName.append(s, len); |
| 475 |
} |
| 476 |
}; |
| 477 |
|
| 478 |
static const TagHandler fileSetHandler = { |
| 479 |
/*start*/nullptr, |
| 480 |
/*end*/nullptr, |
| 481 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 482 |
size_t len = strlen(tag); |
| 483 |
if (MEMEQ("file", tag, len)) { |
| 484 |
return &fileHandler; |
| 485 |
} |
| 486 |
return nullptr; |
| 487 |
}, |
| 488 |
/*chars*/nullptr, |
| 489 |
}; |
| 490 |
|
| 491 |
static const TagHandler nameHandler = { |
| 492 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 493 |
// The character data should be a name for the font. |
| 494 |
self->fCurrentFamily->fNames.push_back(); |
| 495 |
}, |
| 496 |
/*end*/nullptr, |
| 497 |
/*tag*/nullptr, |
| 498 |
/*chars*/[](void* data, const char* s, int len) { |
| 499 |
FamilyData* self = static_cast<FamilyData*>(data); |
| 500 |
SkAutoAsciiToLC tolc(s, len); |
| 501 |
self->fCurrentFamily->fNames.back().append(tolc.lc(), len); |
| 502 |
} |
| 503 |
}; |
| 504 |
|
| 505 |
static const TagHandler nameSetHandler = { |
| 506 |
/*start*/nullptr, |
| 507 |
/*end*/nullptr, |
| 508 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 509 |
size_t len = strlen(tag); |
| 510 |
if (MEMEQ("name", tag, len)) { |
| 511 |
return &nameHandler; |
| 512 |
} |
| 513 |
return nullptr; |
| 514 |
}, |
| 515 |
/*chars*/nullptr, |
| 516 |
}; |
| 517 |
|
| 518 |
static const TagHandler familyHandler = { |
| 519 |
/*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 520 |
self->fCurrentFamily = std::make_unique<FontFamily>(self->fBasePath, self->fIsFallback); |
| 521 |
// 'order' (non-negative integer) [default -1] |
| 522 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 523 |
const char* value = attributes[i+1]; |
| 524 |
parse_non_negative_integer(value, &self->fCurrentFamily->fOrder); |
| 525 |
} |
| 526 |
}, |
| 527 |
/*end*/[](FamilyData* self, const char* tag) { |
| 528 |
self->fFamilies.push_back(std::move(self->fCurrentFamily)); |
| 529 |
}, |
| 530 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 531 |
size_t len = strlen(tag); |
| 532 |
if (MEMEQ("nameset", tag, len)) { |
| 533 |
return &nameSetHandler; |
| 534 |
} else if (MEMEQ("fileset", tag, len)) { |
| 535 |
return &fileSetHandler; |
| 536 |
} |
| 537 |
return nullptr; |
| 538 |
}, |
| 539 |
/*chars*/nullptr, |
| 540 |
}; |
| 541 |
|
| 542 |
static const TagHandler familySetHandler = { |
| 543 |
/*start*/nullptr, |
| 544 |
/*end*/nullptr, |
| 545 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 546 |
size_t len = strlen(tag); |
| 547 |
if (MEMEQ("family", tag, len)) { |
| 548 |
return &familyHandler; |
| 549 |
} |
| 550 |
return nullptr; |
| 551 |
}, |
| 552 |
/*chars*/nullptr, |
| 553 |
}; |
| 554 |
|
| 555 |
} // namespace jbParser |
| 556 |
|
| 557 |
static const TagHandler topLevelHandler = { |
| 558 |
/*start*/nullptr, |
| 559 |
/*end*/nullptr, |
| 560 |
/*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* { |
| 561 |
size_t len = strlen(tag); |
| 562 |
if (MEMEQ("familyset", tag, len)) { |
| 563 |
// 'version' (non-negative integer) [default 0] |
| 564 |
for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 565 |
const char* name = attributes[i]; |
| 566 |
size_t nameLen = strlen(name); |
| 567 |
if (MEMEQ("version", name, nameLen)) { |
| 568 |
const char* value = attributes[i+1]; |
| 569 |
if (parse_non_negative_integer(value, &self->fVersion)) { |
| 570 |
if (self->fVersion >= 21) { |
| 571 |
return &lmpParser::familySetHandler; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
return &jbParser::familySetHandler; |
| 577 |
} |
| 578 |
return nullptr; |
| 579 |
}, |
| 580 |
/*chars*/nullptr, |
| 581 |
}; |
| 582 |
|
| 583 |
static void XMLCALL start_element_handler(void *data, const char *tag, const char **attributes) { |
| 584 |
FamilyData* self = static_cast<FamilyData*>(data); |
| 585 |
|
| 586 |
if (!self->fSkip) { |
| 587 |
const TagHandler* parent = self->fHandler.back(); |
| 588 |
const TagHandler* child = parent->tag ? parent->tag(self, tag, attributes) : nullptr; |
| 589 |
if (child) { |
| 590 |
if (child->start) { |
| 591 |
child->start(self, tag, attributes); |
| 592 |
} |
| 593 |
self->fHandler.push_back(child); |
| 594 |
XML_SetCharacterDataHandler(self->fParser, child->chars); |
| 595 |
} else { |
| 596 |
SK_FONTCONFIGPARSER_WARNING("'%s' tag not recognized, skipping", tag); |
| 597 |
XML_SetCharacterDataHandler(self->fParser, nullptr); |
| 598 |
self->fSkip = self->fDepth; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
++self->fDepth; |
| 603 |
} |
| 604 |
|
| 605 |
static void XMLCALL end_element_handler(void* data, const char* tag) { |
| 606 |
FamilyData* self = static_cast<FamilyData*>(data); |
| 607 |
--self->fDepth; |
| 608 |
|
| 609 |
if (!self->fSkip) { |
| 610 |
const TagHandler* child = self->fHandler.back(); |
| 611 |
if (child->end) { |
| 612 |
child->end(self, tag); |
| 613 |
} |
| 614 |
self->fHandler.pop_back(); |
| 615 |
const TagHandler* parent = self->fHandler.back(); |
| 616 |
XML_SetCharacterDataHandler(self->fParser, parent->chars); |
| 617 |
} |
| 618 |
|
| 619 |
if (self->fSkip == self->fDepth) { |
| 620 |
self->fSkip = 0; |
| 621 |
const TagHandler* parent = self->fHandler.back(); |
| 622 |
XML_SetCharacterDataHandler(self->fParser, parent->chars); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
static void XMLCALL xml_entity_decl_handler(void *data, |
| 627 |
const XML_Char *entityName, |
| 628 |
int is_parameter_entity, |
| 629 |
const XML_Char *value, |
| 630 |
int value_length, |
| 631 |
const XML_Char *base, |
| 632 |
const XML_Char *systemId, |
| 633 |
const XML_Char *publicId, |
| 634 |
const XML_Char *notationName) |
| 635 |
{ |
| 636 |
FamilyData* self = static_cast<FamilyData*>(data); |
| 637 |
SK_FONTCONFIGPARSER_WARNING("'%s' entity declaration found, stopping processing", entityName); |
| 638 |
XML_StopParser(self->fParser, XML_FALSE); |
| 639 |
} |
| 640 |
|
| 641 |
static const XML_Memory_Handling_Suite sk_XML_alloc = { |
| 642 |
sk_malloc_throw, |
| 643 |
sk_realloc_throw, |
| 644 |
sk_free |
| 645 |
}; |
| 646 |
|
| 647 |
/** |
| 648 |
* This function parses the given filename and stores the results in the given |
| 649 |
* families array. Returns the version of the file, negative if the file does not exist. |
| 650 |
*/ |
| 651 |
static int parse_config_file(const char* filename, |
| 652 |
std::vector<std::unique_ptr<FontFamily>>& families, |
| 653 |
const SkString& basePath, bool isFallback) |
| 654 |
{ |
| 655 |
SkFILEStream file(filename); |
| 656 |
|
| 657 |
// Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml) |
| 658 |
// are optional - failure here is okay because one of these optional files may not exist. |
| 659 |
if (!file.isValid()) { |
| 660 |
SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "'%s' could not be opened\n", filename); |
| 661 |
return -1; |
| 662 |
} |
| 663 |
|
| 664 |
SkAutoTCallVProc<std::remove_pointer_t<XML_Parser>, XML_ParserFree> parser( |
| 665 |
XML_ParserCreate_MM(nullptr, &sk_XML_alloc, nullptr)); |
| 666 |
if (!parser) { |
| 667 |
SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "could not create XML parser\n"); |
| 668 |
return -1; |
| 669 |
} |
| 670 |
|
| 671 |
FamilyData self(parser, families, basePath, isFallback, filename, &topLevelHandler); |
| 672 |
XML_SetUserData(parser, &self); |
| 673 |
|
| 674 |
// Disable entity processing, to inhibit internal entity expansion. See expat CVE-2013-0340 |
| 675 |
XML_SetEntityDeclHandler(parser, xml_entity_decl_handler); |
| 676 |
|
| 677 |
// Start parsing oldschool; switch these in flight if we detect a newer version of the file. |
| 678 |
XML_SetElementHandler(parser, start_element_handler, end_element_handler); |
| 679 |
|
| 680 |
// One would assume it would be faster to have a buffer on the stack and call XML_Parse. |
| 681 |
// But XML_Parse will call XML_GetBuffer anyway and memmove the passed buffer into it. |
| 682 |
// (Unless XML_CONTEXT_BYTES is undefined, but all users define it.) |
| 683 |
// In debug, buffer a small odd number of bytes to detect slicing in XML_CharacterDataHandler. |
| 684 |
static const int bufferSize = 512 SkDEBUGCODE( - 507); |
| 685 |
bool done = false; |
| 686 |
while (!done) { |
| 687 |
void* buffer = XML_GetBuffer(parser, bufferSize); |
| 688 |
if (!buffer) { |
| 689 |
SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "could not buffer enough to continue\n"); |
| 690 |
return -1; |
| 691 |
} |
| 692 |
size_t len = file.read(buffer, bufferSize); |
| 693 |
done = file.isAtEnd(); |
| 694 |
XML_Status status = XML_ParseBuffer(parser, len, done); |
| 695 |
if (XML_STATUS_ERROR == status) { |
| 696 |
XML_Error error = XML_GetErrorCode(parser); |
| 697 |
int line = XML_GetCurrentLineNumber(parser); |
| 698 |
int column = XML_GetCurrentColumnNumber(parser); |
| 699 |
const XML_LChar* errorString = XML_ErrorString(error); |
| 700 |
SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "%s:%d:%d error %d: %s.\n", |
| 701 |
filename, line, column, error, errorString); |
| 702 |
return -1; |
| 703 |
} |
| 704 |
} |
| 705 |
return self.fVersion; |
| 706 |
} |
| 707 |
|
| 708 |
/** Returns the version of the system font file actually found, negative if none. */ |
| 709 |
static int append_system_font_families(std::vector<std::unique_ptr<FontFamily>>& fontFamilies, |
| 710 |
const SkString& basePath) |
| 711 |
{ |
| 712 |
size_t initialCount = fontFamilies.size(); |
| 713 |
int version = parse_config_file(LMP_SYSTEM_FONTS_FILE, fontFamilies, basePath, false); |
| 714 |
if (version < 0 || fontFamilies.size() == initialCount) { |
| 715 |
version = parse_config_file(OLD_SYSTEM_FONTS_FILE, fontFamilies, basePath, false); |
| 716 |
} |
| 717 |
return version; |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* In some versions of Android prior to Android 4.2 (JellyBean MR1 at API |
| 722 |
* Level 17) the fallback fonts for certain locales were encoded in their own |
| 723 |
* XML files with a suffix that identified the locale. We search the provided |
| 724 |
* directory for those files,add all of their entries to the fallback chain, and |
| 725 |
* include the locale as part of each entry. |
| 726 |
*/ |
| 727 |
static void append_fallback_font_families_for_locale( |
| 728 |
std::vector<std::unique_ptr<FontFamily>>& fallbackFonts, |
| 729 |
const char* dir, |
| 730 |
const SkString& basePath) |
| 731 |
{ |
| 732 |
SkOSFile::Iter iter(dir, nullptr); |
| 733 |
SkString fileName; |
| 734 |
while (iter.next(&fileName, false)) { |
| 735 |
// The size of the prefix and suffix. |
| 736 |
static const size_t fixedLen = sizeof(LOCALE_FALLBACK_FONTS_PREFIX) - 1 |
| 737 |
+ sizeof(LOCALE_FALLBACK_FONTS_SUFFIX) - 1; |
| 738 |
|
| 739 |
// The size of the prefix, suffix, and a minimum valid language code |
| 740 |
static const size_t minSize = fixedLen + 2; |
| 741 |
|
| 742 |
if (fileName.size() < minSize || |
| 743 |
!fileName.startsWith(LOCALE_FALLBACK_FONTS_PREFIX) || |
| 744 |
!fileName.endsWith(LOCALE_FALLBACK_FONTS_SUFFIX)) |
| 745 |
{ |
| 746 |
continue; |
| 747 |
} |
| 748 |
|
| 749 |
SkString locale(fileName.c_str() + sizeof(LOCALE_FALLBACK_FONTS_PREFIX) - 1, |
| 750 |
fileName.size() - fixedLen); |
| 751 |
|
| 752 |
SkString absoluteFilename; |
| 753 |
absoluteFilename.printf("%s/%s", dir, fileName.c_str()); |
| 754 |
|
| 755 |
std::vector<std::unique_ptr<FontFamily>> langSpecificFonts; |
| 756 |
parse_config_file(absoluteFilename.c_str(), langSpecificFonts, basePath, true); |
| 757 |
|
| 758 |
for (std::unique_ptr<FontFamily>& family : langSpecificFonts) { |
| 759 |
family->fLanguages.emplace_back(locale); |
| 760 |
fallbackFonts.push_back(std::move(family)); |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
static void append_system_fallback_font_families( |
| 766 |
std::vector<std::unique_ptr<FontFamily>>& fallbackFonts, |
| 767 |
const SkString& basePath) |
| 768 |
{ |
| 769 |
parse_config_file(FALLBACK_FONTS_FILE, fallbackFonts, basePath, true); |
| 770 |
append_fallback_font_families_for_locale(fallbackFonts, |
| 771 |
LOCALE_FALLBACK_FONTS_SYSTEM_DIR, |
| 772 |
basePath); |
| 773 |
} |
| 774 |
|
| 775 |
static void mixin_vendor_fallback_font_families( |
| 776 |
std::vector<std::unique_ptr<FontFamily>>& fallbackFonts, |
| 777 |
const SkString& basePath) |
| 778 |
{ |
| 779 |
std::vector<std::unique_ptr<FontFamily>> vendorFonts; |
| 780 |
parse_config_file(VENDOR_FONTS_FILE, vendorFonts, basePath, true); |
| 781 |
append_fallback_font_families_for_locale(vendorFonts, |
| 782 |
LOCALE_FALLBACK_FONTS_VENDOR_DIR, |
| 783 |
basePath); |
| 784 |
|
| 785 |
// This loop inserts the vendor fallback fonts in the correct order in the |
| 786 |
// overall fallbacks list. |
| 787 |
int currentOrder = -1; |
| 788 |
for (std::unique_ptr<FontFamily>& family : vendorFonts) { |
| 789 |
int order = family->fOrder; |
| 790 |
if (order < 0) { |
| 791 |
if (currentOrder < 0) { |
| 792 |
// Default case - just add it to the end of the fallback list |
| 793 |
fallbackFonts.push_back(std::move(family)); |
| 794 |
} else { |
| 795 |
// no order specified on this font, but we're incrementing the order |
| 796 |
// based on an earlier order insertion request |
| 797 |
fallbackFonts.insert(fallbackFonts.begin() + currentOrder, std::move(family)); |
| 798 |
++currentOrder; |
| 799 |
} |
| 800 |
} else { |
| 801 |
// Add the font into the fallback list in the specified order. Set |
| 802 |
// currentOrder for correct placement of other fonts in the vendor list. |
| 803 |
fallbackFonts.insert(fallbackFonts.begin() + order, std::move(family)); |
| 804 |
currentOrder = order + 1; |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
void SkFontMgr_Android_Parser::GetSystemFontFamilies( |
| 810 |
std::vector<std::unique_ptr<FontFamily>>& fontFamilies) { |
| 811 |
static constexpr char kFontFilePrefix[] = "/fonts/"; |
| 812 |
|
| 813 |
// Version 21 of the system font configuration does not need any fallback configuration files. |
| 814 |
SkString basePath(getenv("ANDROID_ROOT")); |
| 815 |
basePath.append(kFontFilePrefix); |
| 816 |
|
| 817 |
if (append_system_font_families(fontFamilies, basePath) >= 21) { |
| 818 |
return; |
| 819 |
} |
| 820 |
|
| 821 |
// Append all the fallback fonts to system fonts |
| 822 |
std::vector<std::unique_ptr<FontFamily>> fallbackFonts; |
| 823 |
append_system_fallback_font_families(fallbackFonts, basePath); |
| 824 |
mixin_vendor_fallback_font_families(fallbackFonts, basePath); |
| 825 |
fontFamilies.insert(fontFamilies.end(), |
| 826 |
std::make_move_iterator(fallbackFonts.begin()), |
| 827 |
std::make_move_iterator(fallbackFonts.end())); |
| 828 |
} |
| 829 |
|
| 830 |
void SkFontMgr_Android_Parser::GetCustomFontFamilies( |
| 831 |
std::vector<std::unique_ptr<FontFamily>>& fontFamilies, |
| 832 |
const SkString& basePath, |
| 833 |
const char* fontsXml, |
| 834 |
const char* fallbackFontsXml, |
| 835 |
const char* langFallbackFontsDir) |
| 836 |
{ |
| 837 |
if (fontsXml) { |
| 838 |
parse_config_file(fontsXml, fontFamilies, basePath, false); |
| 839 |
} |
| 840 |
if (fallbackFontsXml) { |
| 841 |
parse_config_file(fallbackFontsXml, fontFamilies, basePath, true); |
| 842 |
} |
| 843 |
if (langFallbackFontsDir) { |
| 844 |
append_fallback_font_families_for_locale(fontFamilies, |
| 845 |
langFallbackFontsDir, |
| 846 |
basePath); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
SkLanguage SkLanguage::getParent() const { |
| 851 |
SkASSERT(!fTag.isEmpty()); |
| 852 |
const char* tag = fTag.c_str(); |
| 853 |
|
| 854 |
// strip off the rightmost "-.*" |
| 855 |
const char* parentTagEnd = strrchr(tag, '-'); |
| 856 |
if (parentTagEnd == nullptr) { |
| 857 |
return SkLanguage(); |
| 858 |
} |
| 859 |
size_t parentTagLen = parentTagEnd - tag; |
| 860 |
return SkLanguage(tag, parentTagLen); |
| 861 |
} |