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 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 8 | // Despite the name and location, this is portable code. |
| 9 | |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame^] | 10 | #include "SkFixed.h" |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 11 | #include "SkFontMgr.h" |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 12 | #include "SkFontMgr_android_parser.h" |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 13 | #include "SkStream.h" |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 15 | #include "SkTSearch.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 16 | #include "SkTemplates.h" |
bungeman | 54e6308 | 2016-01-06 08:30:59 -0800 | [diff] [blame] | 17 | #include "SkTLogic.h" |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 18 | |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 20 | #include <expat.h> |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 21 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 23 | #include <string.h> |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 24 | |
tomhudson | 94fa4b9 | 2014-08-12 11:05:29 -0700 | [diff] [blame] | 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 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 35 | #ifndef SK_FONT_FILE_PREFIX |
| 36 | # define SK_FONT_FILE_PREFIX "/fonts/" |
| 37 | #endif |
| 38 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 39 | /** |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 40 | * 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. |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 51 | */ |
| 52 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 53 | struct FamilyData; |
| 54 | |
| 55 | struct 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | * If nullptr, will not be called. |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 60 | */ |
| 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | * If nullptr, will not be called. |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 67 | */ |
| 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | * If the tag is not recognized, return nullptr to skip the tag. |
| 73 | * If nullptr, all nested tags will be skipped. |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 74 | */ |
| 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*. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | * If nullptr, any character data in this tag will be ignored. |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 81 | */ |
| 82 | XML_CharacterDataHandler chars; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 83 | }; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 84 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 85 | /** Represents the current parsing state. */ |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 86 | struct FamilyData { |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 87 | FamilyData(XML_Parser parser, SkTDArray<FontFamily*>& families, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 88 | const SkString& basePath, bool isFallback, const char* filename, |
| 89 | const TagHandler* topLevelHandler) |
bungeman | b6bed17 | 2015-01-30 15:35:09 -0800 | [diff] [blame] | 90 | : fParser(parser) |
| 91 | , fFamilies(families) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | , fCurrentFamily(nullptr) |
| 93 | , fCurrentFontInfo(nullptr) |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 94 | , fVersion(0) |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 95 | , fBasePath(basePath) |
| 96 | , fIsFallback(isFallback) |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 97 | , fFilename(filename) |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 98 | , fDepth(1) |
| 99 | , fSkip(0) |
| 100 | , fHandler(&topLevelHandler, 1) |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 101 | { }; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 102 | |
bungeman | b6bed17 | 2015-01-30 15:35:09 -0800 | [diff] [blame] | 103 | 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 |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 106 | FontFileInfo* fCurrentFontInfo; // The fontInfo being created, owned by fCurrentFamily |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 107 | int fVersion; // The version of the file parsed. |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 108 | const SkString& fBasePath; // The current base path. |
| 109 | const bool fIsFallback; // Indicates the file being parsed is a fallback file |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 110 | const char* fFilename; // The name of the file currently being parsed. |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 111 | |
| 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.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 117 | static 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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | #define ATTS_NON_NULL(a, i) (a[i] != nullptr && a[i+1] != nullptr) |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 123 | |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 124 | #define SK_FONTMGR_ANDROID_PARSER_PREFIX "[SkFontMgr Android Parser] " |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 125 | |
| 126 | #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \ |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 127 | SK_FONTMGR_ANDROID_PARSER_PREFIX "%s:%d:%d: warning: " message "\n", \ |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 128 | self->fFilename, \ |
| 129 | XML_GetCurrentLineNumber(self->fParser), \ |
| 130 | XML_GetCurrentColumnNumber(self->fParser), \ |
| 131 | ##__VA_ARGS__); |
| 132 | |
bungeman | c0727d1 | 2015-05-08 08:31:54 -0700 | [diff] [blame] | 133 | static bool is_whitespace(char c) { |
| 134 | return c == ' ' || c == '\n'|| c == '\r' || c == '\t'; |
| 135 | } |
| 136 | |
| 137 | static 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 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 152 | namespace lmpParser { |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 153 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 154 | static const TagHandler axisHandler = { |
| 155 | /*start*/[](FamilyData* self, const char* tag, const char** attributes) { |
| 156 | FontFileInfo& file = *self->fCurrentFontInfo; |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 157 | SkFourByteTag axisTag = SkSetFourByteTag('\0','\0','\0','\0'); |
| 158 | SkFixed axisStyleValue = 0; |
| 159 | bool axisTagIsValid = false; |
| 160 | bool axisStyleValueIsValid = false; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 161 | 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) { |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 168 | axisTag = SkSetFourByteTag(value[0], value[1], value[2], value[3]); |
| 169 | axisTagIsValid = true; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 170 | for (int j = 0; j < file.fAxes.count() - 1; ++j) { |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 171 | if (file.fAxes[j].fTag == axisTag) { |
| 172 | axisTagIsValid = false; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 173 | SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified more than once", |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 174 | (axisTag >> 24) & 0xFF, |
| 175 | (axisTag >> 16) & 0xFF, |
| 176 | (axisTag >> 8) & 0xFF, |
| 177 | (axisTag ) & 0xFF); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 180 | } else { |
| 181 | SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value); |
| 182 | } |
| 183 | } else if (MEMEQ("stylevalue", name, nameLen)) { |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 184 | if (parse_fixed<16>(value, &axisStyleValue)) { |
| 185 | axisStyleValueIsValid = true; |
| 186 | } else { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 187 | SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue", value); |
| 188 | } |
| 189 | } |
| 190 | } |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 191 | if (axisTagIsValid && axisStyleValueIsValid) { |
| 192 | SkFontMgr::FontParameters::Axis& axis = file.fAxes.push_back(); |
| 193 | axis.fTag = axisTag; |
| 194 | axis.fStyleValue = SkFixedToScalar(axisStyleValue); |
| 195 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 196 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | /*end*/nullptr, |
| 198 | /*tag*/nullptr, |
| 199 | /*chars*/nullptr, |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 202 | static 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.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 229 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 230 | } |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 231 | }, |
| 232 | /*end*/[](FamilyData* self, const char* tag) { |
| 233 | trim_string(&self->fCurrentFontInfo->fFileName); |
| 234 | }, |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 235 | /*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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 240 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 241 | }, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 242 | /*chars*/[](void* data, const char* s, int len) { |
| 243 | FamilyData* self = static_cast<FamilyData*>(data); |
| 244 | self->fCurrentFontInfo->fFileName.append(s, len); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 245 | } |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 246 | }; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 247 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 248 | static 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); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 256 | for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 257 | const char* name = attributes[i]; |
| 258 | const char* value = attributes[i+1]; |
| 259 | size_t nameLen = strlen(name); |
| 260 | size_t valueLen = strlen(value); |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 261 | if (MEMEQ("name", name, nameLen)) { |
| 262 | SkAutoAsciiToLC tolc(value); |
| 263 | family->fNames.push_back().set(tolc.lc()); |
| 264 | family->fIsFallbackFont = false; |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 265 | } else if (MEMEQ("lang", name, nameLen)) { |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 266 | 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; |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 272 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 273 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 274 | } |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 275 | }, |
| 276 | /*end*/[](FamilyData* self, const char* tag) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 277 | *self->fFamilies.append() = self->fCurrentFamily.release(); |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 278 | }, |
| 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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 284 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 285 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | static 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.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 297 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | return nullptr; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 299 | } |
| 300 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 301 | static 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 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 350 | /*end*/nullptr, |
| 351 | /*tag*/nullptr, |
| 352 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | static const TagHandler familySetHandler = { |
| 356 | /*start*/[](FamilyData* self, const char* tag, const char** attributes) { }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 357 | /*end*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 358 | /*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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 365 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 366 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 367 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | } // lmpParser |
| 371 | |
| 372 | namespace jbParser { |
| 373 | |
| 374 | static 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 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 419 | /*end*/nullptr, |
| 420 | /*tag*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 421 | /*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 | |
| 427 | static const TagHandler fileSetHandler = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 428 | /*start*/nullptr, |
| 429 | /*end*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 430 | /*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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 435 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 436 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 437 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | static 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 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 445 | /*end*/nullptr, |
| 446 | /*tag*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 447 | /*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 | |
| 454 | static const TagHandler nameSetHandler = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 455 | /*start*/nullptr, |
| 456 | /*end*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 457 | /*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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 462 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 463 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 464 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | static 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) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 477 | *self->fFamilies.append() = self->fCurrentFamily.release(); |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 478 | }, |
| 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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 486 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 487 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 488 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | static const TagHandler familySetHandler = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 492 | /*start*/nullptr, |
| 493 | /*end*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 494 | /*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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 499 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 500 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 501 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 504 | } // namespace jbParser |
| 505 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 506 | static const TagHandler topLevelHandler = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 507 | /*start*/nullptr, |
| 508 | /*end*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 509 | /*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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 527 | return nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 528 | }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 529 | /*chars*/nullptr, |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | static 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 537 | const TagHandler* child = parent->tag ? parent->tag(self, tag, attributes) : nullptr; |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 538 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 546 | XML_SetCharacterDataHandler(self->fParser, nullptr); |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 547 | self->fSkip = self->fDepth; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | ++self->fDepth; |
| 552 | } |
| 553 | |
| 554 | static 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 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 575 | static 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); |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 586 | SK_FONTCONFIGPARSER_WARNING("'%s' entity declaration found, stopping processing", entityName); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 587 | XML_StopParser(self->fParser, XML_FALSE); |
| 588 | } |
| 589 | |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 590 | static const XML_Memory_Handling_Suite sk_XML_alloc = { |
| 591 | sk_malloc_throw, |
| 592 | sk_realloc_throw, |
| 593 | sk_free |
| 594 | }; |
| 595 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 596 | /** |
| 597 | * This function parses the given filename and stores the results in the given |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 598 | * families array. Returns the version of the file, negative if the file does not exist. |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 599 | */ |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 600 | static int parse_config_file(const char* filename, SkTDArray<FontFamily*>& families, |
| 601 | const SkString& basePath, bool isFallback) |
| 602 | { |
| 603 | SkFILEStream file(filename); |
djsollen@google.com | 50c9567 | 2013-08-28 12:29:45 +0000 | [diff] [blame] | 604 | |
| 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. |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 607 | if (!file.isValid()) { |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 608 | SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "'%s' could not be opened\n", filename); |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 609 | return -1; |
djsollen@google.com | 50c9567 | 2013-08-28 12:29:45 +0000 | [diff] [blame] | 610 | } |
| 611 | |
bungeman | 54e6308 | 2016-01-06 08:30:59 -0800 | [diff] [blame] | 612 | SkAutoTCallVProc<skstd::remove_pointer_t<XML_Parser>, XML_ParserFree> parser( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 613 | XML_ParserCreate_MM(nullptr, &sk_XML_alloc, nullptr)); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 614 | if (!parser) { |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 615 | SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "could not create XML parser\n"); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 616 | return -1; |
| 617 | } |
| 618 | |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 619 | FamilyData self(parser, families, basePath, isFallback, filename, &topLevelHandler); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 620 | 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 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 625 | // Start parsing oldschool; switch these in flight if we detect a newer version of the file. |
bungeman | 10b063c | 2015-05-13 08:52:16 -0700 | [diff] [blame] | 626 | XML_SetElementHandler(parser, start_element_handler, end_element_handler); |
djsollen@google.com | 50c9567 | 2013-08-28 12:29:45 +0000 | [diff] [blame] | 627 | |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 628 | // 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.) |
bungeman | 9a0808f | 2015-02-13 08:55:16 -0800 | [diff] [blame] | 631 | // 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.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 633 | bool done = false; |
| 634 | while (!done) { |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 635 | void* buffer = XML_GetBuffer(parser, bufferSize); |
| 636 | if (!buffer) { |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 637 | SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "could not buffer enough to continue\n"); |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 638 | return -1; |
| 639 | } |
| 640 | size_t len = file.read(buffer, bufferSize); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 641 | done = file.isAtEnd(); |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 642 | XML_Status status = XML_ParseBuffer(parser, len, done); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 643 | if (XML_STATUS_ERROR == status) { |
| 644 | XML_Error error = XML_GetErrorCode(parser); |
| 645 | int line = XML_GetCurrentLineNumber(parser); |
| 646 | int column = XML_GetCurrentColumnNumber(parser); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 647 | const XML_LChar* errorString = XML_ErrorString(error); |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 648 | SkDebugf(SK_FONTMGR_ANDROID_PARSER_PREFIX "%s:%d:%d error %d: %s.\n", |
bungeman | f61475e | 2015-04-20 11:16:48 -0700 | [diff] [blame] | 649 | filename, line, column, error, errorString); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 650 | return -1; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 651 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 652 | } |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 653 | return self.fVersion; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 654 | } |
| 655 | |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 656 | /** Returns the version of the system font file actually found, negative if none. */ |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 657 | static int append_system_font_families(SkTDArray<FontFamily*>& fontFamilies, |
| 658 | const SkString& basePath) |
| 659 | { |
tomhudson | 7a4747f | 2014-08-13 11:06:43 -0700 | [diff] [blame] | 660 | int initialCount = fontFamilies.count(); |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 661 | int version = parse_config_file(LMP_SYSTEM_FONTS_FILE, fontFamilies, basePath, false); |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 662 | if (version < 0 || fontFamilies.count() == initialCount) { |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 663 | version = parse_config_file(OLD_SYSTEM_FONTS_FILE, fontFamilies, basePath, false); |
tomhudson | 94fa4b9 | 2014-08-12 11:05:29 -0700 | [diff] [blame] | 664 | } |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 665 | return version; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 666 | } |
| 667 | |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 668 | /** |
| 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 | */ |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 675 | static void append_fallback_font_families_for_locale(SkTDArray<FontFamily*>& fallbackFonts, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 676 | const char* dir, |
| 677 | const SkString& basePath) |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 678 | { |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 679 | SkAutoTCallIProc<DIR, closedir> fontDirectory(opendir(dir)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 680 | if (nullptr == fontDirectory) { |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 681 | return; |
| 682 | } |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 683 | |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 684 | for (struct dirent* dirEntry; (dirEntry = readdir(fontDirectory));) { |
bungeman | 9a0808f | 2015-02-13 08:55:16 -0800 | [diff] [blame] | 685 | // 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; |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 691 | |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 692 | 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; |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 698 | } |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 699 | |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 700 | 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 | } |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 717 | static 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); |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 724 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 725 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 726 | static void mixin_vendor_fallback_font_families(SkTDArray<FontFamily*>& fallbackFonts, |
| 727 | const SkString& basePath) |
| 728 | { |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 729 | SkTDArray<FontFamily*> vendorFonts; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 730 | 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); |
djsollen | a6c27bc | 2014-08-06 11:01:58 -0700 | [diff] [blame] | 734 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 735 | // 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]; |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 740 | int order = family->fOrder; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 741 | 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 | |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 759 | void SkFontMgr_Android_Parser::GetSystemFontFamilies(SkTDArray<FontFamily*>& fontFamilies) { |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 760 | // Version 21 of the system font configuration does not need any fallback configuration files. |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 761 | 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) { |
bungeman | efbad37 | 2015-02-02 11:19:44 -0800 | [diff] [blame] | 765 | return; |
| 766 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 767 | |
| 768 | // Append all the fallback fonts to system fonts |
| 769 | SkTDArray<FontFamily*> fallbackFonts; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 770 | append_system_fallback_font_families(fallbackFonts, basePath); |
| 771 | mixin_vendor_fallback_font_families(fallbackFonts, basePath); |
| 772 | fontFamilies.append(fallbackFonts.count(), fallbackFonts.begin()); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 773 | } |
| 774 | |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 775 | void SkFontMgr_Android_Parser::GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies, |
| 776 | const SkString& basePath, |
| 777 | const char* fontsXml, |
| 778 | const char* fallbackFontsXml, |
| 779 | const char* langFallbackFontsDir) |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 780 | { |
| 781 | if (fontsXml) { |
| 782 | parse_config_file(fontsXml, fontFamilies, basePath, false); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 783 | } |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 784 | if (fallbackFontsXml) { |
| 785 | parse_config_file(fallbackFontsXml, fontFamilies, basePath, true); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 786 | } |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 787 | if (langFallbackFontsDir) { |
| 788 | append_fallback_font_families_for_locale(fontFamilies, |
| 789 | langFallbackFontsDir, |
| 790 | basePath); |
| 791 | } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 792 | } |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 793 | |
| 794 | SkLanguage 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, '-'); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 800 | if (parentTagEnd == nullptr) { |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 801 | return SkLanguage(); |
| 802 | } |
| 803 | size_t parentTagLen = parentTagEnd - tag; |
| 804 | return SkLanguage(tag, parentTagLen); |
| 805 | } |