OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The Android Open Source Project | 2 * Copyright 2011 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFontConfigParser_android.h" | 8 #include "SkFontConfigParser_android.h" |
9 #include "SkFontMgr_android.h" | 9 #include "SkFontMgr_android.h" |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
11 #include "SkTDArray.h" | 11 #include "SkTDArray.h" |
12 #include "SkTSearch.h" | 12 #include "SkTSearch.h" |
13 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
14 | 14 |
15 #include <expat.h> | 15 #include <expat.h> |
16 #include <dirent.h> | 16 #include <dirent.h> |
17 #include <stdio.h> | |
18 | 17 |
19 #include <limits> | |
20 #include <stdlib.h> | 18 #include <stdlib.h> |
21 | 19 |
22 // From Android version LMP onwards, all font files collapse into | 20 // From Android version LMP onwards, all font files collapse into |
23 // /system/etc/fonts.xml. Instead of trying to detect which version | 21 // /system/etc/fonts.xml. Instead of trying to detect which version |
24 // we're on, try to open fonts.xml; if that fails, fall back to the | 22 // we're on, try to open fonts.xml; if that fails, fall back to the |
25 // older filename. | 23 // older filename. |
26 #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" | 24 #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" |
27 #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" | 25 #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" |
28 #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" | 26 #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
29 #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" | 27 #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 SkTDArray<FontFamily*>& fFamilies; // The array to append families, o
wned by caller | 73 SkTDArray<FontFamily*>& fFamilies; // The array to append families, o
wned by caller |
76 SkAutoTDelete<FontFamily> fCurrentFamily; // The family being created, owned
by this | 74 SkAutoTDelete<FontFamily> fCurrentFamily; // The family being created, owned
by this |
77 FontFileInfo* fCurrentFontInfo; // The fontInfo being created, own
ed by fCurrentFamily | 75 FontFileInfo* fCurrentFontInfo; // The fontInfo being created, own
ed by fCurrentFamily |
78 CurrentTag fCurrentTag; // The kind of tag currently being
parsed. | 76 CurrentTag fCurrentTag; // The kind of tag currently being
parsed. |
79 int fVersion; // The version of the file parsed. | 77 int fVersion; // The version of the file parsed. |
80 const SkString& fBasePath; // The current base path. | 78 const SkString& fBasePath; // The current base path. |
81 const bool fIsFallback; // Indicates the file being parsed
is a fallback file | 79 const bool fIsFallback; // Indicates the file being parsed
is a fallback file |
82 const char* fFilename; // The name of the file currently
being parsed. | 80 const char* fFilename; // The name of the file currently
being parsed. |
83 }; | 81 }; |
84 | 82 |
85 /** Parses a null terminated string into an integer type, checking for overflow. | |
86 * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-nega
tive-def | |
87 * | |
88 * If the string cannot be parsed into 'value', returns false and does not chan
ge 'value'. | |
89 */ | |
90 template <typename T> static bool parse_non_negative_integer(const char* s, T* v
alue) { | |
91 SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); | |
92 const T nMax = std::numeric_limits<T>::max() / 10; | |
93 const T dMax = std::numeric_limits<T>::max() - (nMax * 10); | |
94 T n = 0; | |
95 for (; *s; ++s) { | |
96 // Check if digit | |
97 if (*s < '0' || '9' < *s) { | |
98 return false; | |
99 } | |
100 int d = *s - '0'; | |
101 // Check for overflow | |
102 if (n > nMax || (n == nMax && d > dMax)) { | |
103 return false; | |
104 } | |
105 n = (n * 10) + d; | |
106 } | |
107 *value = n; | |
108 return true; | |
109 } | |
110 | |
111 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { | 83 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { |
112 return n1 == n2 && 0 == memcmp(s1, s2, n1); | 84 return n1 == n2 && 0 == memcmp(s1, s2, n1); |
113 } | 85 } |
114 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) | 86 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) |
115 | 87 |
116 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) | 88 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) |
117 | 89 |
118 #define SK_FONTCONFIGPARSER_PREFIX "[SkFontConfigParser] " | 90 #define SK_FONTCONFIGPARSER_PREFIX "[SkFontConfigParser] " |
119 | 91 |
120 #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \ | 92 #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \ |
(...skipping 30 matching lines...) Expand all Loading... |
151 } | 123 } |
152 } | 124 } |
153 } | 125 } |
154 } | 126 } |
155 | 127 |
156 static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { | 128 static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { |
157 FamilyData* self = static_cast<FamilyData*>(data); | 129 FamilyData* self = static_cast<FamilyData*>(data); |
158 self->fCurrentFontInfo->fFileName.append(s, len); | 130 self->fCurrentFontInfo->fFileName.append(s, len); |
159 } | 131 } |
160 | 132 |
| 133 static void axis_element_handler(FamilyData* self, const char** attributes) { |
| 134 FontFileInfo& file = *self->fCurrentFontInfo; |
| 135 FontFileInfo::Axis& axis = file.fAxes.push_back(); |
| 136 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
| 137 const char* name = attributes[i]; |
| 138 const char* value = attributes[i+1]; |
| 139 size_t nameLen = strlen(name); |
| 140 if (MEMEQ("tag", name, nameLen)) { |
| 141 size_t valueLen = strlen(value); |
| 142 if (valueLen == 4) { |
| 143 SkFourByteTag tag = SkSetFourByteTag(value[0], value[1], value[2
], value[3]); |
| 144 for (int j = 0; j < file.fAxes.count() - 1; ++j) { |
| 145 if (file.fAxes[j].fTag == tag) { |
| 146 SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified m
ore than once", |
| 147 (tag >> 24) & 0xFF, |
| 148 (tag >> 16) & 0xFF, |
| 149 (tag >> 8) & 0xFF, |
| 150 (tag ) & 0xFF); |
| 151 } |
| 152 } |
| 153 axis.fTag = SkSetFourByteTag(value[0], value[1], value[2], value
[3]); |
| 154 } else { |
| 155 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value
); |
| 156 } |
| 157 } else if (MEMEQ("stylevalue", name, nameLen)) { |
| 158 if (!parse_fixed<16>(value, &axis.fValue)) { |
| 159 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue"
, value); |
| 160 } |
| 161 } |
| 162 } |
| 163 } |
| 164 |
161 static void font_element_handler(FamilyData* self, const char** attributes) { | 165 static void font_element_handler(FamilyData* self, const char** attributes) { |
162 // A <font> element should be contained in a <family> element. | 166 // A <font> element should be contained in a <family> element. |
163 // The element may have 'weight' (non-negative integer), 'style' ("normal",
"italic"), | 167 // The element may have 'weight' (non-negative integer), 'style' ("normal",
"italic"), |
164 // and 'index' (non-negative integer) attributes. | 168 // and 'index' (non-negative integer) attributes. |
165 // The element should contain a filename. | 169 // The element should contain a filename. |
166 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back(); | 170 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back(); |
167 self->fCurrentFontInfo = &file; | 171 self->fCurrentFontInfo = &file; |
168 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { | 172 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
169 const char* name = attributes[i]; | 173 const char* name = attributes[i]; |
170 const char* value = attributes[i+1]; | 174 const char* value = attributes[i+1]; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 257 |
254 static void XMLCALL start_element_handler(void* data, const char* tag, const cha
r** attributes) { | 258 static void XMLCALL start_element_handler(void* data, const char* tag, const cha
r** attributes) { |
255 FamilyData* self = static_cast<FamilyData*>(data); | 259 FamilyData* self = static_cast<FamilyData*>(data); |
256 size_t len = strlen(tag); | 260 size_t len = strlen(tag); |
257 if (MEMEQ("family", tag, len)) { | 261 if (MEMEQ("family", tag, len)) { |
258 family_element_handler(self, attributes); | 262 family_element_handler(self, attributes); |
259 } else if (MEMEQ("font", tag, len)) { | 263 } else if (MEMEQ("font", tag, len)) { |
260 font_element_handler(self, attributes); | 264 font_element_handler(self, attributes); |
261 } else if (MEMEQ("alias", tag, len)) { | 265 } else if (MEMEQ("alias", tag, len)) { |
262 alias_element_handler(self, attributes); | 266 alias_element_handler(self, attributes); |
| 267 } else if (MEMEQ("axis", tag, len)) { |
| 268 axis_element_handler(self, attributes); |
263 } | 269 } |
264 } | 270 } |
265 | 271 |
266 static void XMLCALL end_element_handler(void* data, const char* tag) { | 272 static void XMLCALL end_element_handler(void* data, const char* tag) { |
267 FamilyData* self = static_cast<FamilyData*>(data); | 273 FamilyData* self = static_cast<FamilyData*>(data); |
268 size_t len = strlen(tag); | 274 size_t len = strlen(tag); |
269 if (MEMEQ("family", tag, len)) { | 275 if (MEMEQ("family", tag, len)) { |
270 *self->fFamilies.append() = self->fCurrentFamily.detach(); | 276 *self->fFamilies.append() = self->fCurrentFamily.detach(); |
271 } else if (MEMEQ("font", tag, len)) { | 277 } else if (MEMEQ("font", tag, len)) { |
272 XML_SetCharacterDataHandler(self->fParser, NULL); | 278 XML_SetCharacterDataHandler(self->fParser, NULL); |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 const char* tag = fTag.c_str(); | 656 const char* tag = fTag.c_str(); |
651 | 657 |
652 // strip off the rightmost "-.*" | 658 // strip off the rightmost "-.*" |
653 const char* parentTagEnd = strrchr(tag, '-'); | 659 const char* parentTagEnd = strrchr(tag, '-'); |
654 if (parentTagEnd == NULL) { | 660 if (parentTagEnd == NULL) { |
655 return SkLanguage(); | 661 return SkLanguage(); |
656 } | 662 } |
657 size_t parentTagLen = parentTagEnd - tag; | 663 size_t parentTagLen = parentTagEnd - tag; |
658 return SkLanguage(tag, parentTagLen); | 664 return SkLanguage(tag, parentTagLen); |
659 } | 665 } |
OLD | NEW |