blob: 24242830cee56c1f85b302faa30e830feb923f1f [file] [log] [blame]
djsollen@google.combfae9d32013-05-21 16:53:50 +00001/*
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
mtklein1ee76512015-11-02 10:20:27 -08008// Despite the name and location, this is portable code.
9
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
bungeman47a1e962016-02-25 11:20:01 -080011#include "SkFontMgr.h"
bungemanc5308542015-06-23 13:25:46 -070012#include "SkFontMgr_android_parser.h"
bungeman7fa87cd2015-02-06 07:59:19 -080013#include "SkStream.h"
djsollen@google.combfae9d32013-05-21 16:53:50 +000014#include "SkTDArray.h"
bungeman8d84c992014-07-24 08:05:09 -070015#include "SkTSearch.h"
bungemanf20488b2015-07-29 11:49:40 -070016#include "SkTemplates.h"
bungeman54e63082016-01-06 08:30:59 -080017#include "SkTLogic.h"
djsollen@google.combfae9d32013-05-21 16:53:50 +000018
djsollena6c27bc2014-08-06 11:01:58 -070019#include <dirent.h>
bungemanf20488b2015-07-29 11:49:40 -070020#include <expat.h>
djsollen@google.combfae9d32013-05-21 16:53:50 +000021
bungeman7fa87cd2015-02-06 07:59:19 -080022#include <stdlib.h>
bungemanf20488b2015-07-29 11:49:40 -070023#include <string.h>
bungeman8d84c992014-07-24 08:05:09 -070024
tomhudson94fa4b92014-08-12 11:05:29 -070025#define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml"
26#define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml"
djsollen@google.combfae9d32013-05-21 16:53:50 +000027#define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml"
28#define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml"
29
djsollena6c27bc2014-08-06 11:01:58 -070030#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
bungeman7fa87cd2015-02-06 07:59:19 -080035#ifndef SK_FONT_FILE_PREFIX
36# define SK_FONT_FILE_PREFIX "/fonts/"
37#endif
38
tomhudsonf79673b2014-08-05 06:36:11 -070039/**
bungeman10b063c2015-05-13 08:52:16 -070040 * This file contains TWO 'familyset' handlers:
41 * One for JB and earlier which works with
42 * /system/etc/system_fonts.xml
43 * /system/etc/fallback_fonts.xml
44 * /vendor/etc/fallback_fonts.xml
45 * /system/etc/fallback_fonts-XX.xml
46 * /vendor/etc/fallback_fonts-XX.xml
47 * and the other for LMP and later which works with
48 * /system/etc/fonts.xml
49 *
50 * If the 'familyset' 'version' attribute is 21 or higher the LMP parser is used, otherwise the JB.
tomhudsonf79673b2014-08-05 06:36:11 -070051 */
52
bungeman10b063c2015-05-13 08:52:16 -070053struct FamilyData;
54
55struct TagHandler {
56 /** Called at the start tag.
57 * Called immediately after the parent tag retuns this handler from a call to 'tag'.
58 * Allows setting up for handling the tag content and processing attributes.
halcanary96fcdcc2015-08-27 07:41:13 -070059 * If nullptr, will not be called.
bungeman10b063c2015-05-13 08:52:16 -070060 */
61 void (*start)(FamilyData* data, const char* tag, const char** attributes);
62
63 /** Called at the end tag.
64 * Allows post-processing of any accumulated information.
65 * This will be the last call made in relation to the current tag.
halcanary96fcdcc2015-08-27 07:41:13 -070066 * If nullptr, will not be called.
bungeman10b063c2015-05-13 08:52:16 -070067 */
68 void (*end)(FamilyData* data, const char* tag);
69
70 /** Called when a nested tag is encountered.
71 * This is responsible for determining how to handle the tag.
halcanary96fcdcc2015-08-27 07:41:13 -070072 * If the tag is not recognized, return nullptr to skip the tag.
73 * If nullptr, all nested tags will be skipped.
bungeman10b063c2015-05-13 08:52:16 -070074 */
75 const TagHandler* (*tag)(FamilyData* data, const char* tag, const char** attributes);
76
77 /** The character handler for this tag.
78 * This is only active for character data contained directly in this tag (not sub-tags).
79 * The first parameter will be castable to a FamilyData*.
halcanary96fcdcc2015-08-27 07:41:13 -070080 * If nullptr, any character data in this tag will be ignored.
bungeman10b063c2015-05-13 08:52:16 -070081 */
82 XML_CharacterDataHandler chars;
bungeman7fa87cd2015-02-06 07:59:19 -080083};
djsollen@google.combfae9d32013-05-21 16:53:50 +000084
bungeman10b063c2015-05-13 08:52:16 -070085/** Represents the current parsing state. */
djsollen@google.combfae9d32013-05-21 16:53:50 +000086struct FamilyData {
bungeman7fa87cd2015-02-06 07:59:19 -080087 FamilyData(XML_Parser parser, SkTDArray<FontFamily*>& families,
bungeman10b063c2015-05-13 08:52:16 -070088 const SkString& basePath, bool isFallback, const char* filename,
89 const TagHandler* topLevelHandler)
bungemanb6bed172015-01-30 15:35:09 -080090 : fParser(parser)
91 , fFamilies(families)
halcanary96fcdcc2015-08-27 07:41:13 -070092 , fCurrentFamily(nullptr)
93 , fCurrentFontInfo(nullptr)
bungemanefbad372015-02-02 11:19:44 -080094 , fVersion(0)
bungeman7fa87cd2015-02-06 07:59:19 -080095 , fBasePath(basePath)
96 , fIsFallback(isFallback)
bungemanf61475e2015-04-20 11:16:48 -070097 , fFilename(filename)
bungeman10b063c2015-05-13 08:52:16 -070098 , fDepth(1)
99 , fSkip(0)
100 , fHandler(&topLevelHandler, 1)
bungeman7fa87cd2015-02-06 07:59:19 -0800101 { };
djsollen@google.combfae9d32013-05-21 16:53:50 +0000102
bungemanb6bed172015-01-30 15:35:09 -0800103 XML_Parser fParser; // The expat parser doing the work, owned by caller
104 SkTDArray<FontFamily*>& fFamilies; // The array to append families, owned by caller
105 SkAutoTDelete<FontFamily> fCurrentFamily; // The family being created, owned by this
bungeman7fa87cd2015-02-06 07:59:19 -0800106 FontFileInfo* fCurrentFontInfo; // The fontInfo being created, owned by fCurrentFamily
bungemanefbad372015-02-02 11:19:44 -0800107 int fVersion; // The version of the file parsed.
bungeman7fa87cd2015-02-06 07:59:19 -0800108 const SkString& fBasePath; // The current base path.
109 const bool fIsFallback; // Indicates the file being parsed is a fallback file
bungemanf61475e2015-04-20 11:16:48 -0700110 const char* fFilename; // The name of the file currently being parsed.
bungeman10b063c2015-05-13 08:52:16 -0700111
112 int fDepth; // The current element depth of the parse.
113 int fSkip; // The depth to stop skipping, 0 if not skipping.
114 SkTDArray<const TagHandler*> fHandler; // The stack of current tag handlers.
djsollen@google.combfae9d32013-05-21 16:53:50 +0000115};
116
bungeman7fa87cd2015-02-06 07:59:19 -0800117static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) {
118 return n1 == n2 && 0 == memcmp(s1, s2, n1);
119}
120#define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n)
121
halcanary96fcdcc2015-08-27 07:41:13 -0700122#define ATTS_NON_NULL(a, i) (a[i] != nullptr && a[i+1] != nullptr)
bungeman7fa87cd2015-02-06 07:59:19 -0800123
bungemanc5308542015-06-23 13:25:46 -0700124#define SK_FONTMGR_ANDROID_PARSER_PREFIX "[SkFontMgr Android Parser] "
bungemanf61475e2015-04-20 11:16:48 -0700125
126#define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \
bungemanc5308542015-06-23 13:25:46 -0700127 SK_FONTMGR_ANDROID_PARSER_PREFIX "%s:%d:%d: warning: " message "\n", \
bungemanf61475e2015-04-20 11:16:48 -0700128 self->fFilename, \
129 XML_GetCurrentLineNumber(self->fParser), \
130 XML_GetCurrentColumnNumber(self->fParser), \
131 ##__VA_ARGS__);
132
bungemanc0727d12015-05-08 08:31:54 -0700133static bool is_whitespace(char c) {
134 return c == ' ' || c == '\n'|| c == '\r' || c == '\t';
135}
136
137static void trim_string(SkString* s) {
138 char* str = s->writable_str();
139 const char* start = str; // start is inclusive
140 const char* end = start + s->size(); // end is exclusive
141 while (is_whitespace(*start)) { ++start; }
142 if (start != end) {
143 --end; // make end inclusive
144 while (is_whitespace(*end)) { --end; }
145 ++end; // make end exclusive
146 }
147 size_t len = end - start;
148 memmove(str, start, len);
149 s->resize(len);
150}
151
bungeman10b063c2015-05-13 08:52:16 -0700152namespace lmpParser {
tomhudsonf79673b2014-08-05 06:36:11 -0700153
bungeman41868fe2015-05-20 09:21:04 -0700154static const TagHandler axisHandler = {
155 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
156 FontFileInfo& file = *self->fCurrentFontInfo;
bungeman47a1e962016-02-25 11:20:01 -0800157 SkFourByteTag axisTag = SkSetFourByteTag('\0','\0','\0','\0');
158 SkFixed axisStyleValue = 0;
159 bool axisTagIsValid = false;
160 bool axisStyleValueIsValid = false;
bungeman41868fe2015-05-20 09:21:04 -0700161 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
162 const char* name = attributes[i];
163 const char* value = attributes[i+1];
164 size_t nameLen = strlen(name);
165 if (MEMEQ("tag", name, nameLen)) {
166 size_t valueLen = strlen(value);
167 if (valueLen == 4) {
bungeman47a1e962016-02-25 11:20:01 -0800168 axisTag = SkSetFourByteTag(value[0], value[1], value[2], value[3]);
169 axisTagIsValid = true;
bungeman41868fe2015-05-20 09:21:04 -0700170 for (int j = 0; j < file.fAxes.count() - 1; ++j) {
bungeman47a1e962016-02-25 11:20:01 -0800171 if (file.fAxes[j].fTag == axisTag) {
172 axisTagIsValid = false;
bungeman41868fe2015-05-20 09:21:04 -0700173 SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified more than once",
bungeman47a1e962016-02-25 11:20:01 -0800174 (axisTag >> 24) & 0xFF,
175 (axisTag >> 16) & 0xFF,
176 (axisTag >> 8) & 0xFF,
177 (axisTag ) & 0xFF);
bungeman41868fe2015-05-20 09:21:04 -0700178 }
179 }
bungeman41868fe2015-05-20 09:21:04 -0700180 } else {
181 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value);
182 }
183 } else if (MEMEQ("stylevalue", name, nameLen)) {
bungeman47a1e962016-02-25 11:20:01 -0800184 if (parse_fixed<16>(value, &axisStyleValue)) {
185 axisStyleValueIsValid = true;
186 } else {
bungeman41868fe2015-05-20 09:21:04 -0700187 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue", value);
188 }
189 }
190 }
bungeman47a1e962016-02-25 11:20:01 -0800191 if (axisTagIsValid && axisStyleValueIsValid) {
192 SkFontMgr::FontParameters::Axis& axis = file.fAxes.push_back();
193 axis.fTag = axisTag;
194 axis.fStyleValue = SkFixedToScalar(axisStyleValue);
195 }
bungeman41868fe2015-05-20 09:21:04 -0700196 },
halcanary96fcdcc2015-08-27 07:41:13 -0700197 /*end*/nullptr,
198 /*tag*/nullptr,
199 /*chars*/nullptr,
bungeman41868fe2015-05-20 09:21:04 -0700200};
201
bungeman10b063c2015-05-13 08:52:16 -0700202static const TagHandler fontHandler = {
203 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
204 // 'weight' (non-negative integer) [default 0]
205 // 'style' ("normal", "italic") [default "auto"]
206 // 'index' (non-negative integer) [default 0]
207 // The character data should be a filename.
208 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back();
209 self->fCurrentFontInfo = &file;
210 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
211 const char* name = attributes[i];
212 const char* value = attributes[i+1];
213 size_t nameLen = strlen(name);
214 if (MEMEQ("weight", name, nameLen)) {
215 if (!parse_non_negative_integer(value, &file.fWeight)) {
216 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid weight", value);
217 }
218 } else if (MEMEQ("style", name, nameLen)) {
219 size_t valueLen = strlen(value);
220 if (MEMEQ("normal", value, valueLen)) {
221 file.fStyle = FontFileInfo::Style::kNormal;
222 } else if (MEMEQ("italic", value, valueLen)) {
223 file.fStyle = FontFileInfo::Style::kItalic;
224 }
225 } else if (MEMEQ("index", name, nameLen)) {
226 if (!parse_non_negative_integer(value, &file.fIndex)) {
227 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid index", value);
228 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000229 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000230 }
bungeman10b063c2015-05-13 08:52:16 -0700231 },
232 /*end*/[](FamilyData* self, const char* tag) {
233 trim_string(&self->fCurrentFontInfo->fFileName);
234 },
bungeman41868fe2015-05-20 09:21:04 -0700235 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
236 size_t len = strlen(tag);
237 if (MEMEQ("axis", tag, len)) {
238 return &axisHandler;
239 }
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return nullptr;
bungeman41868fe2015-05-20 09:21:04 -0700241 },
bungeman10b063c2015-05-13 08:52:16 -0700242 /*chars*/[](void* data, const char* s, int len) {
243 FamilyData* self = static_cast<FamilyData*>(data);
244 self->fCurrentFontInfo->fFileName.append(s, len);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000245 }
bungeman10b063c2015-05-13 08:52:16 -0700246};
djsollen@google.combfae9d32013-05-21 16:53:50 +0000247
bungeman10b063c2015-05-13 08:52:16 -0700248static const TagHandler familyHandler = {
249 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
250 // 'name' (string) [optional]
251 // 'lang' (string) [default ""]
252 // 'variant' ("elegant", "compact") [default "default"]
253 // If there is no name, this is a fallback only font.
254 FontFamily* family = new FontFamily(self->fBasePath, true);
255 self->fCurrentFamily.reset(family);
bungeman7fa87cd2015-02-06 07:59:19 -0800256 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
bungemanf61475e2015-04-20 11:16:48 -0700257 const char* name = attributes[i];
258 const char* value = attributes[i+1];
259 size_t nameLen = strlen(name);
260 size_t valueLen = strlen(value);
bungeman10b063c2015-05-13 08:52:16 -0700261 if (MEMEQ("name", name, nameLen)) {
262 SkAutoAsciiToLC tolc(value);
263 family->fNames.push_back().set(tolc.lc());
264 family->fIsFallbackFont = false;
bungemanf61475e2015-04-20 11:16:48 -0700265 } else if (MEMEQ("lang", name, nameLen)) {
bungeman10b063c2015-05-13 08:52:16 -0700266 family->fLanguage = SkLanguage(value, valueLen);
267 } else if (MEMEQ("variant", name, nameLen)) {
268 if (MEMEQ("elegant", value, valueLen)) {
269 family->fVariant = kElegant_FontVariant;
270 } else if (MEMEQ("compact", value, valueLen)) {
271 family->fVariant = kCompact_FontVariant;
bungeman8d84c992014-07-24 08:05:09 -0700272 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000273 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000274 }
bungeman10b063c2015-05-13 08:52:16 -0700275 },
276 /*end*/[](FamilyData* self, const char* tag) {
mtklein18300a32016-03-16 13:53:35 -0700277 *self->fFamilies.append() = self->fCurrentFamily.release();
bungeman10b063c2015-05-13 08:52:16 -0700278 },
279 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
280 size_t len = strlen(tag);
281 if (MEMEQ("font", tag, len)) {
282 return &fontHandler;
283 }
halcanary96fcdcc2015-08-27 07:41:13 -0700284 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700285 },
halcanary96fcdcc2015-08-27 07:41:13 -0700286 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700287};
288
289static FontFamily* find_family(FamilyData* self, const SkString& familyName) {
290 for (int i = 0; i < self->fFamilies.count(); i++) {
291 FontFamily* candidate = self->fFamilies[i];
292 for (int j = 0; j < candidate->fNames.count(); j++) {
293 if (candidate->fNames[j] == familyName) {
294 return candidate;
295 }
296 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000297 }
halcanary96fcdcc2015-08-27 07:41:13 -0700298 return nullptr;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000299}
300
bungeman10b063c2015-05-13 08:52:16 -0700301static const TagHandler aliasHandler = {
302 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
303 // 'name' (string) introduces a new family name.
304 // 'to' (string) specifies which (previous) family to alias
305 // 'weight' (non-negative integer) [optional]
306 // If it *does not* have a weight, 'name' is an alias for the entire 'to' family.
307 // If it *does* have a weight, 'name' is a new family consisting of
308 // the font(s) with 'weight' from the 'to' family.
309
310 SkString aliasName;
311 SkString to;
312 int weight = 0;
313 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
314 const char* name = attributes[i];
315 const char* value = attributes[i+1];
316 size_t nameLen = strlen(name);
317 if (MEMEQ("name", name, nameLen)) {
318 SkAutoAsciiToLC tolc(value);
319 aliasName.set(tolc.lc());
320 } else if (MEMEQ("to", name, nameLen)) {
321 to.set(value);
322 } else if (MEMEQ("weight", name, nameLen)) {
323 if (!parse_non_negative_integer(value, &weight)) {
324 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid weight", value);
325 }
326 }
327 }
328
329 // Assumes that the named family is already declared
330 FontFamily* targetFamily = find_family(self, to);
331 if (!targetFamily) {
332 SK_FONTCONFIGPARSER_WARNING("'%s' alias target not found", to.c_str());
333 return;
334 }
335
336 if (weight) {
337 FontFamily* family = new FontFamily(targetFamily->fBasePath, self->fIsFallback);
338 family->fNames.push_back().set(aliasName);
339
340 for (int i = 0; i < targetFamily->fFonts.count(); i++) {
341 if (targetFamily->fFonts[i].fWeight == weight) {
342 family->fFonts.push_back(targetFamily->fFonts[i]);
343 }
344 }
345 *self->fFamilies.append() = family;
346 } else {
347 targetFamily->fNames.push_back().set(aliasName);
348 }
349 },
halcanary96fcdcc2015-08-27 07:41:13 -0700350 /*end*/nullptr,
351 /*tag*/nullptr,
352 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700353};
354
355static const TagHandler familySetHandler = {
356 /*start*/[](FamilyData* self, const char* tag, const char** attributes) { },
halcanary96fcdcc2015-08-27 07:41:13 -0700357 /*end*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700358 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
359 size_t len = strlen(tag);
360 if (MEMEQ("family", tag, len)) {
361 return &familyHandler;
362 } else if (MEMEQ("alias", tag, len)) {
363 return &aliasHandler;
364 }
halcanary96fcdcc2015-08-27 07:41:13 -0700365 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700366 },
halcanary96fcdcc2015-08-27 07:41:13 -0700367 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700368};
369
370} // lmpParser
371
372namespace jbParser {
373
374static const TagHandler fileHandler = {
375 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
376 // 'variant' ("elegant", "compact") [default "default"]
377 // 'lang' (string) [default ""]
378 // 'index' (non-negative integer) [default 0]
379 // The character data should be a filename.
380 FontFamily& currentFamily = *self->fCurrentFamily.get();
381 FontFileInfo& newFileInfo = currentFamily.fFonts.push_back();
382 if (attributes) {
383 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
384 const char* name = attributes[i];
385 const char* value = attributes[i+1];
386 size_t nameLen = strlen(name);
387 size_t valueLen = strlen(value);
388 if (MEMEQ("variant", name, nameLen)) {
389 const FontVariant prevVariant = currentFamily.fVariant;
390 if (MEMEQ("elegant", value, valueLen)) {
391 currentFamily.fVariant = kElegant_FontVariant;
392 } else if (MEMEQ("compact", value, valueLen)) {
393 currentFamily.fVariant = kCompact_FontVariant;
394 }
395 if (currentFamily.fFonts.count() > 1 && currentFamily.fVariant != prevVariant) {
396 SK_FONTCONFIGPARSER_WARNING("'%s' unexpected variant found\n"
397 "Note: Every font file within a family must have identical variants.",
398 value);
399 }
400
401 } else if (MEMEQ("lang", name, nameLen)) {
402 SkLanguage prevLang = currentFamily.fLanguage;
403 currentFamily.fLanguage = SkLanguage(value, valueLen);
404 if (currentFamily.fFonts.count() > 1 && currentFamily.fLanguage != prevLang) {
405 SK_FONTCONFIGPARSER_WARNING("'%s' unexpected language found\n"
406 "Note: Every font file within a family must have identical languages.",
407 value);
408 }
409
410 } else if (MEMEQ("index", name, nameLen)) {
411 if (!parse_non_negative_integer(value, &newFileInfo.fIndex)) {
412 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid index", value);
413 }
414 }
415 }
416 }
417 self->fCurrentFontInfo = &newFileInfo;
418 },
halcanary96fcdcc2015-08-27 07:41:13 -0700419 /*end*/nullptr,
420 /*tag*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700421 /*chars*/[](void* data, const char* s, int len) {
422 FamilyData* self = static_cast<FamilyData*>(data);
423 self->fCurrentFontInfo->fFileName.append(s, len);
424 }
425};
426
427static const TagHandler fileSetHandler = {
halcanary96fcdcc2015-08-27 07:41:13 -0700428 /*start*/nullptr,
429 /*end*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700430 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
431 size_t len = strlen(tag);
432 if (MEMEQ("file", tag, len)) {
433 return &fileHandler;
434 }
halcanary96fcdcc2015-08-27 07:41:13 -0700435 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700436 },
halcanary96fcdcc2015-08-27 07:41:13 -0700437 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700438};
439
440static const TagHandler nameHandler = {
441 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
442 // The character data should be a name for the font.
443 self->fCurrentFamily->fNames.push_back();
444 },
halcanary96fcdcc2015-08-27 07:41:13 -0700445 /*end*/nullptr,
446 /*tag*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700447 /*chars*/[](void* data, const char* s, int len) {
448 FamilyData* self = static_cast<FamilyData*>(data);
449 SkAutoAsciiToLC tolc(s, len);
450 self->fCurrentFamily->fNames.back().append(tolc.lc(), len);
451 }
452};
453
454static const TagHandler nameSetHandler = {
halcanary96fcdcc2015-08-27 07:41:13 -0700455 /*start*/nullptr,
456 /*end*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700457 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
458 size_t len = strlen(tag);
459 if (MEMEQ("name", tag, len)) {
460 return &nameHandler;
461 }
halcanary96fcdcc2015-08-27 07:41:13 -0700462 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700463 },
halcanary96fcdcc2015-08-27 07:41:13 -0700464 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700465};
466
467static const TagHandler familyHandler = {
468 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
469 self->fCurrentFamily.reset(new FontFamily(self->fBasePath, self->fIsFallback));
470 // 'order' (non-negative integer) [default -1]
471 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
472 const char* value = attributes[i+1];
473 parse_non_negative_integer(value, &self->fCurrentFamily->fOrder);
474 }
475 },
476 /*end*/[](FamilyData* self, const char* tag) {
mtklein18300a32016-03-16 13:53:35 -0700477 *self->fFamilies.append() = self->fCurrentFamily.release();
bungeman10b063c2015-05-13 08:52:16 -0700478 },
479 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
480 size_t len = strlen(tag);
481 if (MEMEQ("nameset", tag, len)) {
482 return &nameSetHandler;
483 } else if (MEMEQ("fileset", tag, len)) {
484 return &fileSetHandler;
485 }
halcanary96fcdcc2015-08-27 07:41:13 -0700486 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700487 },
halcanary96fcdcc2015-08-27 07:41:13 -0700488 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700489};
490
491static const TagHandler familySetHandler = {
halcanary96fcdcc2015-08-27 07:41:13 -0700492 /*start*/nullptr,
493 /*end*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700494 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
495 size_t len = strlen(tag);
496 if (MEMEQ("family", tag, len)) {
497 return &familyHandler;
498 }
halcanary96fcdcc2015-08-27 07:41:13 -0700499 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700500 },
halcanary96fcdcc2015-08-27 07:41:13 -0700501 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700502};
503
tomhudsonf79673b2014-08-05 06:36:11 -0700504} // namespace jbParser
505
bungeman10b063c2015-05-13 08:52:16 -0700506static const TagHandler topLevelHandler = {
halcanary96fcdcc2015-08-27 07:41:13 -0700507 /*start*/nullptr,
508 /*end*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700509 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> const TagHandler* {
510 size_t len = strlen(tag);
511 if (MEMEQ("familyset", tag, len)) {
512 // 'version' (non-negative integer) [default 0]
513 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
514 const char* name = attributes[i];
515 size_t nameLen = strlen(name);
516 if (MEMEQ("version", name, nameLen)) {
517 const char* value = attributes[i+1];
518 if (parse_non_negative_integer(value, &self->fVersion)) {
519 if (self->fVersion >= 21) {
520 return &lmpParser::familySetHandler;
521 }
522 }
523 }
524 }
525 return &jbParser::familySetHandler;
526 }
halcanary96fcdcc2015-08-27 07:41:13 -0700527 return nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700528 },
halcanary96fcdcc2015-08-27 07:41:13 -0700529 /*chars*/nullptr,
bungeman10b063c2015-05-13 08:52:16 -0700530};
531
532static void XMLCALL start_element_handler(void *data, const char *tag, const char **attributes) {
533 FamilyData* self = static_cast<FamilyData*>(data);
534
535 if (!self->fSkip) {
536 const TagHandler* parent = self->fHandler.top();
halcanary96fcdcc2015-08-27 07:41:13 -0700537 const TagHandler* child = parent->tag ? parent->tag(self, tag, attributes) : nullptr;
bungeman10b063c2015-05-13 08:52:16 -0700538 if (child) {
539 if (child->start) {
540 child->start(self, tag, attributes);
541 }
542 self->fHandler.push(child);
543 XML_SetCharacterDataHandler(self->fParser, child->chars);
544 } else {
545 SK_FONTCONFIGPARSER_WARNING("'%s' tag not recognized, skipping", tag);
halcanary96fcdcc2015-08-27 07:41:13 -0700546 XML_SetCharacterDataHandler(self->fParser, nullptr);
bungeman10b063c2015-05-13 08:52:16 -0700547 self->fSkip = self->fDepth;
548 }
549 }
550
551 ++self->fDepth;
552}
553
554static void XMLCALL end_element_handler(void* data, const char* tag) {
555 FamilyData* self = static_cast<FamilyData*>(data);
556 --self->fDepth;
557
558 if (!self->fSkip) {
559 const TagHandler* child = self->fHandler.top();
560 if (child->end) {
561 child->end(self, tag);
562 }
563 self->fHandler.pop();
564 const TagHandler* parent = self->fHandler.top();
565 XML_SetCharacterDataHandler(self->fParser, parent->chars);
566 }
567
568 if (self->fSkip == self->fDepth) {
569 self->fSkip = 0;
570 const TagHandler* parent = self->fHandler.top();
571 XML_SetCharacterDataHandler(self->fParser, parent->chars);
572 }
573}
574
bungeman7fa87cd2015-02-06 07:59:19 -0800575static void XMLCALL xml_entity_decl_handler(void *data,
576 const XML_Char *entityName,
577 int is_parameter_entity,
578 const XML_Char *value,
579 int value_length,
580 const XML_Char *base,
581 const XML_Char *systemId,
582 const XML_Char *publicId,
583 const XML_Char *notationName)
584{
585 FamilyData* self = static_cast<FamilyData*>(data);
bungemanf61475e2015-04-20 11:16:48 -0700586 SK_FONTCONFIGPARSER_WARNING("'%s' entity declaration found, stopping processing", entityName);
bungeman7fa87cd2015-02-06 07:59:19 -0800587 XML_StopParser(self->fParser, XML_FALSE);
588}
589
bungemaneb2be7f2015-02-10 07:51:12 -0800590static const XML_Memory_Handling_Suite sk_XML_alloc = {
591 sk_malloc_throw,
592 sk_realloc_throw,
593 sk_free
594};
595
djsollen@google.combfae9d32013-05-21 16:53:50 +0000596/**
597 * This function parses the given filename and stores the results in the given
bungemanefbad372015-02-02 11:19:44 -0800598 * families array. Returns the version of the file, negative if the file does not exist.
djsollen@google.combfae9d32013-05-21 16:53:50 +0000599 */
bungeman7fa87cd2015-02-06 07:59:19 -0800600static int parse_config_file(const char* filename, SkTDArray<FontFamily*>& families,
601 const SkString& basePath, bool isFallback)
602{
603 SkFILEStream file(filename);
djsollen@google.com50c95672013-08-28 12:29:45 +0000604
605 // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml)
606 // are optional - failure here is okay because one of these optional files may not exist.
bungeman7fa87cd2015-02-06 07:59:19 -0800607 if (!file.isValid()) {
bungemanc5308542015-06-23 13:25:46 -0700608 SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "'%s' could not be opened\n", filename);
bungemanefbad372015-02-02 11:19:44 -0800609 return -1;
djsollen@google.com50c95672013-08-28 12:29:45 +0000610 }
611
bungeman54e63082016-01-06 08:30:59 -0800612 SkAutoTCallVProc<skstd::remove_pointer_t<XML_Parser>, XML_ParserFree> parser(
halcanary96fcdcc2015-08-27 07:41:13 -0700613 XML_ParserCreate_MM(nullptr, &sk_XML_alloc, nullptr));
bungeman7fa87cd2015-02-06 07:59:19 -0800614 if (!parser) {
bungemanc5308542015-06-23 13:25:46 -0700615 SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "could not create XML parser\n");
bungeman7fa87cd2015-02-06 07:59:19 -0800616 return -1;
617 }
618
bungeman10b063c2015-05-13 08:52:16 -0700619 FamilyData self(parser, families, basePath, isFallback, filename, &topLevelHandler);
bungeman7fa87cd2015-02-06 07:59:19 -0800620 XML_SetUserData(parser, &self);
621
622 // Disable entity processing, to inhibit internal entity expansion. See expat CVE-2013-0340
623 XML_SetEntityDeclHandler(parser, xml_entity_decl_handler);
624
tomhudsonf79673b2014-08-05 06:36:11 -0700625 // Start parsing oldschool; switch these in flight if we detect a newer version of the file.
bungeman10b063c2015-05-13 08:52:16 -0700626 XML_SetElementHandler(parser, start_element_handler, end_element_handler);
djsollen@google.com50c95672013-08-28 12:29:45 +0000627
bungemaneb2be7f2015-02-10 07:51:12 -0800628 // One would assume it would be faster to have a buffer on the stack and call XML_Parse.
629 // But XML_Parse will call XML_GetBuffer anyway and memmove the passed buffer into it.
630 // (Unless XML_CONTEXT_BYTES is undefined, but all users define it.)
bungeman9a0808f2015-02-13 08:55:16 -0800631 // In debug, buffer a small odd number of bytes to detect slicing in XML_CharacterDataHandler.
632 static const int bufferSize = 512 SkDEBUGCODE( - 507);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000633 bool done = false;
634 while (!done) {
bungemaneb2be7f2015-02-10 07:51:12 -0800635 void* buffer = XML_GetBuffer(parser, bufferSize);
636 if (!buffer) {
bungemanc5308542015-06-23 13:25:46 -0700637 SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "could not buffer enough to continue\n");
bungemaneb2be7f2015-02-10 07:51:12 -0800638 return -1;
639 }
640 size_t len = file.read(buffer, bufferSize);
bungeman7fa87cd2015-02-06 07:59:19 -0800641 done = file.isAtEnd();
bungemaneb2be7f2015-02-10 07:51:12 -0800642 XML_Status status = XML_ParseBuffer(parser, len, done);
bungeman7fa87cd2015-02-06 07:59:19 -0800643 if (XML_STATUS_ERROR == status) {
644 XML_Error error = XML_GetErrorCode(parser);
645 int line = XML_GetCurrentLineNumber(parser);
646 int column = XML_GetCurrentColumnNumber(parser);
bungeman7fa87cd2015-02-06 07:59:19 -0800647 const XML_LChar* errorString = XML_ErrorString(error);
bungemanc5308542015-06-23 13:25:46 -0700648 SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "%s:%d:%d error %d: %s.\n",
bungemanf61475e2015-04-20 11:16:48 -0700649 filename, line, column, error, errorString);
bungeman7fa87cd2015-02-06 07:59:19 -0800650 return -1;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000651 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000652 }
bungeman7fa87cd2015-02-06 07:59:19 -0800653 return self.fVersion;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000654}
655
bungemanefbad372015-02-02 11:19:44 -0800656/** Returns the version of the system font file actually found, negative if none. */
bungeman7fa87cd2015-02-06 07:59:19 -0800657static int append_system_font_families(SkTDArray<FontFamily*>& fontFamilies,
658 const SkString& basePath)
659{
tomhudson7a4747f2014-08-13 11:06:43 -0700660 int initialCount = fontFamilies.count();
bungeman7fa87cd2015-02-06 07:59:19 -0800661 int version = parse_config_file(LMP_SYSTEM_FONTS_FILE, fontFamilies, basePath, false);
bungemanefbad372015-02-02 11:19:44 -0800662 if (version < 0 || fontFamilies.count() == initialCount) {
bungeman7fa87cd2015-02-06 07:59:19 -0800663 version = parse_config_file(OLD_SYSTEM_FONTS_FILE, fontFamilies, basePath, false);
tomhudson94fa4b92014-08-12 11:05:29 -0700664 }
bungemanefbad372015-02-02 11:19:44 -0800665 return version;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000666}
667
djsollena6c27bc2014-08-06 11:01:58 -0700668/**
669 * In some versions of Android prior to Android 4.2 (JellyBean MR1 at API
670 * Level 17) the fallback fonts for certain locales were encoded in their own
671 * XML files with a suffix that identified the locale. We search the provided
672 * directory for those files,add all of their entries to the fallback chain, and
673 * include the locale as part of each entry.
674 */
bungemanefbad372015-02-02 11:19:44 -0800675static void append_fallback_font_families_for_locale(SkTDArray<FontFamily*>& fallbackFonts,
bungeman7fa87cd2015-02-06 07:59:19 -0800676 const char* dir,
677 const SkString& basePath)
bungemanefbad372015-02-02 11:19:44 -0800678{
bungemanc3c69432015-02-11 07:18:51 -0800679 SkAutoTCallIProc<DIR, closedir> fontDirectory(opendir(dir));
halcanary96fcdcc2015-08-27 07:41:13 -0700680 if (nullptr == fontDirectory) {
bungemanc3c69432015-02-11 07:18:51 -0800681 return;
682 }
djsollena6c27bc2014-08-06 11:01:58 -0700683
bungemanc3c69432015-02-11 07:18:51 -0800684 for (struct dirent* dirEntry; (dirEntry = readdir(fontDirectory));) {
bungeman9a0808f2015-02-13 08:55:16 -0800685 // The size of the prefix and suffix.
686 static const size_t fixedLen = sizeof(LOCALE_FALLBACK_FONTS_PREFIX) - 1
687 + sizeof(LOCALE_FALLBACK_FONTS_SUFFIX) - 1;
688
689 // The size of the prefix, suffix, and a minimum valid language code
690 static const size_t minSize = fixedLen + 2;
djsollena6c27bc2014-08-06 11:01:58 -0700691
bungemanc3c69432015-02-11 07:18:51 -0800692 SkString fileName(dirEntry->d_name);
693 if (fileName.size() < minSize ||
694 !fileName.startsWith(LOCALE_FALLBACK_FONTS_PREFIX) ||
695 !fileName.endsWith(LOCALE_FALLBACK_FONTS_SUFFIX))
696 {
697 continue;
djsollena6c27bc2014-08-06 11:01:58 -0700698 }
bungemanc3c69432015-02-11 07:18:51 -0800699
bungemanc3c69432015-02-11 07:18:51 -0800700 SkString locale(fileName.c_str() + sizeof(LOCALE_FALLBACK_FONTS_PREFIX) - 1,
701 fileName.size() - fixedLen);
702
703 SkString absoluteFilename;
704 absoluteFilename.printf("%s/%s", dir, fileName.c_str());
705
706 SkTDArray<FontFamily*> langSpecificFonts;
707 parse_config_file(absoluteFilename.c_str(), langSpecificFonts, basePath, true);
708
709 for (int i = 0; i < langSpecificFonts.count(); ++i) {
710 FontFamily* family = langSpecificFonts[i];
711 family->fLanguage = SkLanguage(locale);
712 *fallbackFonts.append() = family;
713 }
djsollena6c27bc2014-08-06 11:01:58 -0700714 }
715}
716
bungeman7fa87cd2015-02-06 07:59:19 -0800717static void append_system_fallback_font_families(SkTDArray<FontFamily*>& fallbackFonts,
718 const SkString& basePath)
719{
720 parse_config_file(FALLBACK_FONTS_FILE, fallbackFonts, basePath, true);
721 append_fallback_font_families_for_locale(fallbackFonts,
722 LOCALE_FALLBACK_FONTS_SYSTEM_DIR,
723 basePath);
bungemanefbad372015-02-02 11:19:44 -0800724}
djsollen@google.combfae9d32013-05-21 16:53:50 +0000725
bungeman7fa87cd2015-02-06 07:59:19 -0800726static void mixin_vendor_fallback_font_families(SkTDArray<FontFamily*>& fallbackFonts,
727 const SkString& basePath)
728{
bungemanefbad372015-02-02 11:19:44 -0800729 SkTDArray<FontFamily*> vendorFonts;
bungeman7fa87cd2015-02-06 07:59:19 -0800730 parse_config_file(VENDOR_FONTS_FILE, vendorFonts, basePath, true);
731 append_fallback_font_families_for_locale(vendorFonts,
732 LOCALE_FALLBACK_FONTS_VENDOR_DIR,
733 basePath);
djsollena6c27bc2014-08-06 11:01:58 -0700734
djsollen@google.combfae9d32013-05-21 16:53:50 +0000735 // This loop inserts the vendor fallback fonts in the correct order in the
736 // overall fallbacks list.
737 int currentOrder = -1;
738 for (int i = 0; i < vendorFonts.count(); ++i) {
739 FontFamily* family = vendorFonts[i];
tomhudsond3ddea22014-08-11 11:28:00 -0700740 int order = family->fOrder;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000741 if (order < 0) {
742 if (currentOrder < 0) {
743 // Default case - just add it to the end of the fallback list
744 *fallbackFonts.append() = family;
745 } else {
746 // no order specified on this font, but we're incrementing the order
747 // based on an earlier order insertion request
748 *fallbackFonts.insert(currentOrder++) = family;
749 }
750 } else {
751 // Add the font into the fallback list in the specified order. Set
752 // currentOrder for correct placement of other fonts in the vendor list.
753 *fallbackFonts.insert(order) = family;
754 currentOrder = order + 1;
755 }
756 }
757}
758
bungemanc5308542015-06-23 13:25:46 -0700759void SkFontMgr_Android_Parser::GetSystemFontFamilies(SkTDArray<FontFamily*>& fontFamilies) {
bungemanefbad372015-02-02 11:19:44 -0800760 // Version 21 of the system font configuration does not need any fallback configuration files.
bungeman7fa87cd2015-02-06 07:59:19 -0800761 SkString basePath(getenv("ANDROID_ROOT"));
762 basePath.append(SK_FONT_FILE_PREFIX, sizeof(SK_FONT_FILE_PREFIX) - 1);
763
764 if (append_system_font_families(fontFamilies, basePath) >= 21) {
bungemanefbad372015-02-02 11:19:44 -0800765 return;
766 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000767
768 // Append all the fallback fonts to system fonts
769 SkTDArray<FontFamily*> fallbackFonts;
bungeman7fa87cd2015-02-06 07:59:19 -0800770 append_system_fallback_font_families(fallbackFonts, basePath);
771 mixin_vendor_fallback_font_families(fallbackFonts, basePath);
772 fontFamilies.append(fallbackFonts.count(), fallbackFonts.begin());
djsollen@google.combfae9d32013-05-21 16:53:50 +0000773}
774
bungemanc5308542015-06-23 13:25:46 -0700775void SkFontMgr_Android_Parser::GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies,
776 const SkString& basePath,
777 const char* fontsXml,
778 const char* fallbackFontsXml,
779 const char* langFallbackFontsDir)
bungeman7fa87cd2015-02-06 07:59:19 -0800780{
781 if (fontsXml) {
782 parse_config_file(fontsXml, fontFamilies, basePath, false);
tomhudsonf79673b2014-08-05 06:36:11 -0700783 }
bungeman7fa87cd2015-02-06 07:59:19 -0800784 if (fallbackFontsXml) {
785 parse_config_file(fallbackFontsXml, fontFamilies, basePath, true);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000786 }
bungemanc3c69432015-02-11 07:18:51 -0800787 if (langFallbackFontsDir) {
788 append_fallback_font_families_for_locale(fontFamilies,
789 langFallbackFontsDir,
790 basePath);
791 }
djsollen@google.combfae9d32013-05-21 16:53:50 +0000792}
djsollen3b625542014-08-14 06:29:02 -0700793
794SkLanguage SkLanguage::getParent() const {
795 SkASSERT(!fTag.isEmpty());
796 const char* tag = fTag.c_str();
797
798 // strip off the rightmost "-.*"
799 const char* parentTagEnd = strrchr(tag, '-');
halcanary96fcdcc2015-08-27 07:41:13 -0700800 if (parentTagEnd == nullptr) {
djsollen3b625542014-08-14 06:29:02 -0700801 return SkLanguage();
802 }
803 size_t parentTagLen = parentTagEnd - tag;
804 return SkLanguage(tag, parentTagLen);
805}