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> | 17 #include <stdio.h> |
18 | 18 |
19 #include <climits> | |
19 #include <limits> | 20 #include <limits> |
20 #include <stdlib.h> | 21 #include <stdlib.h> |
21 | 22 |
22 // From Android version LMP onwards, all font files collapse into | 23 // From Android version LMP onwards, all font files collapse into |
23 // /system/etc/fonts.xml. Instead of trying to detect which version | 24 // /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 | 25 // we're on, try to open fonts.xml; if that fails, fall back to the |
25 // older filename. | 26 // older filename. |
26 #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" | 27 #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" |
27 #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" | 28 #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" |
28 #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" | 29 #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 template <typename T> static bool parse_non_negative_integer(const char* s, T* v alue) { | 91 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 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 nMax = std::numeric_limits<T>::max() / 10; |
93 const T dMax = std::numeric_limits<T>::max() - (nMax * 10); | 94 const T dMax = std::numeric_limits<T>::max() - (nMax * 10); |
94 T n = 0; | 95 T n = 0; |
95 for (; *s; ++s) { | 96 for (; *s; ++s) { |
96 // Check if digit | 97 // Check if digit |
97 if (*s < '0' || '9' < *s) { | 98 if (*s < '0' || '9' < *s) { |
98 return false; | 99 return false; |
99 } | 100 } |
100 int d = *s - '0'; | 101 T d = *s - '0'; |
101 // Check for overflow | 102 // Check for overflow |
102 if (n > nMax || (n == nMax && d > dMax)) { | 103 if (n > nMax || (n == nMax && d > dMax)) { |
103 return false; | 104 return false; |
104 } | 105 } |
105 n = (n * 10) + d; | 106 n = (n * 10) + d; |
106 } | 107 } |
107 *value = n; | 108 *value = n; |
108 return true; | 109 return true; |
109 } | 110 } |
110 | 111 |
112 /** Parses a null terminated string into a signed fixed point value with bias N. | |
113 * | |
114 * Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def , | |
115 * but may start with '.' and does not support 'e'. '-?:digit:*(.:digit:+)?' | |
116 * | |
117 * Checks for overflow. | |
118 * Low bit rounding is not defined (is currently truncate). | |
119 * Bias (N) required to allow for the sign bit and 4 bits of integer. | |
120 * | |
121 * If the string cannot be parsed into 'value', returns false and does not chan ge 'value'. | |
122 */ | |
reed1
2015/04/29 15:36:46
why not cal sscanf and convert the double to a fix
bungeman-skia
2015/04/29 22:12:44
Because it's evil and should never be used.
https
| |
123 template <int N, typename T> static bool parse_fixed(const char* s, T* value) { | |
124 SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); | |
125 SK_COMPILE_ASSERT(std::numeric_limits<T>::is_signed, T_must_be_signed); | |
126 SK_COMPILE_ASSERT(sizeof(T) * CHAR_BIT - N >= 5, N_must_leave_four_bits_plus _sign); | |
127 | |
128 bool negate = false; | |
129 if (*s == '-') { ++s; negate = true; } | |
130 | |
131 const T nMax = (std::numeric_limits<T>::max() >> N) / 10; | |
132 const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10); | |
133 | |
134 T n = 0; | |
135 T frac = 0; | |
136 for (; *s; ++s) { | |
137 // Check if digit | |
138 if (*s < '0' || '9' < *s) { | |
139 // If it wasn't a digit, check if it is a '.' followed by a digit. | |
140 if (*s == '.' && '0' <= s[1] && s[1] <= '9') { | |
141 // Find the end, verify digits. | |
142 for (++s; *s; ++s) { | |
143 if (*s < '0' || '9' < *s) { | |
144 return false; | |
145 } | |
146 } | |
147 // Read back toward the '.'. | |
148 for (--s; *s != '.'; --s) { | |
149 T d = *s - '0'; | |
150 frac = (frac + (d << N)) / 10; // This requires four bits ov erhead. | |
151 } | |
152 break; | |
153 } | |
154 return false; | |
155 } | |
156 T d = *s - '0'; | |
157 // Check for overflow | |
158 if (n > nMax || (n == nMax && d > dMax)) { | |
159 return false; | |
160 } | |
161 n = (n * 10) + d; | |
162 } | |
163 if (negate) { n = -n; frac = -frac; } | |
164 *value = (n << N) + frac; | |
165 return true; | |
166 } | |
167 | |
168 #if 0 | |
169 #include "math.h" | |
170 template <int N, typename T> static void test_parse_fixed_r(double low, double h igh, double inc) { | |
171 SkDEBUGCODE(double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);) | |
172 double SK_FixedEpsilon_double = (1.0 / (1 << N)); | |
173 double maxError = 0; | |
174 char buffer[64]; | |
175 for (double f = low; f < high; f += inc) { | |
176 SkString s; | |
177 sprintf(buffer, "%.20f", f); | |
178 T fix; | |
179 bool b = parse_fixed<N>(buffer, &fix); | |
180 if (b) { | |
181 double f2 = fix * SK_FixedEpsilon_double; | |
182 SkASSERT(fabs(f - f2) <= SK_FixedEpsilon_double); | |
183 maxError = SkTMax(maxError, fabs(f - f2)); | |
184 } else { | |
185 SkASSERT(f > SK_FixedMax_double || f < -SK_FixedMax_double); | |
186 } | |
187 } | |
188 | |
189 SkDebugf("maxError: %.20f\n", maxError); | |
190 } | |
191 | |
192 static void test_parse_fixed() { | |
193 test_parse_fixed_r<27, int32_t>(-17.1, 17.1, 0.000001); | |
194 test_parse_fixed_r<16, int32_t>(-1.125, 1.125, 1.0 / (1 << 19)); | |
195 test_parse_fixed_r<16, int32_t>(-32769.0, -32766.0, 1.0 / (1 << 19)); | |
196 test_parse_fixed_r<16, int32_t>(32766.0, 32769.0, 1.0 / (1 << 19)); | |
197 test_parse_fixed_r<16, int32_t>(-40000.0, 40000.0, 1.0 / (1 << 4)); | |
198 test_parse_fixed_r<16, int32_t>(-1.1, 1.1, 0.0001); | |
199 | |
200 SkFixed fix; | |
201 SkASSERT(!parse_fixed<16>("123.", &fix)); | |
202 SkASSERT(!parse_fixed<16>("a", &fix)); | |
203 SkASSERT(!parse_fixed<16>(".123a", &fix)); | |
204 } | |
205 #endif | |
206 | |
111 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { | 207 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { |
112 return n1 == n2 && 0 == memcmp(s1, s2, n1); | 208 return n1 == n2 && 0 == memcmp(s1, s2, n1); |
113 } | 209 } |
114 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) | 210 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) |
115 | 211 |
116 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) | 212 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) |
117 | 213 |
118 #define SK_FONTCONFIGPARSER_PREFIX "[SkFontConfigParser] " | 214 #define SK_FONTCONFIGPARSER_PREFIX "[SkFontConfigParser] " |
119 | 215 |
120 #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \ | 216 #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \ |
(...skipping 30 matching lines...) Expand all Loading... | |
151 } | 247 } |
152 } | 248 } |
153 } | 249 } |
154 } | 250 } |
155 | 251 |
156 static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { | 252 static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { |
157 FamilyData* self = static_cast<FamilyData*>(data); | 253 FamilyData* self = static_cast<FamilyData*>(data); |
158 self->fCurrentFontInfo->fFileName.append(s, len); | 254 self->fCurrentFontInfo->fFileName.append(s, len); |
159 } | 255 } |
160 | 256 |
257 static void axis_element_handler(FamilyData* self, const char** attributes) { | |
258 FontFileInfo& file = *self->fCurrentFontInfo; | |
259 FontFileInfo::Axis& axis = file.fAxes.push_back(); | |
260 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { | |
261 const char* name = attributes[i]; | |
262 const char* value = attributes[i+1]; | |
263 size_t nameLen = strlen(name); | |
264 if (MEMEQ("tag", name, nameLen)) { | |
265 size_t valueLen = strlen(value); | |
266 if (valueLen == 4) { | |
267 SkFourByteTag tag = SkSetFourByteTag(value[0], value[1], value[2 ], value[3]); | |
268 for (int j = 0; j < file.fAxes.count() - 1; ++j) { | |
269 if (file.fAxes[j].fTag == tag) { | |
270 SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified m ore than once", | |
271 (tag >> 24) & 0xFF, | |
272 (tag >> 16) & 0xFF, | |
273 (tag >> 8) & 0xFF, | |
274 (tag ) & 0xFF); | |
275 } | |
276 } | |
277 axis.fTag = SkSetFourByteTag(value[0], value[1], value[2], value [3]); | |
278 } else { | |
279 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value ); | |
280 } | |
281 } else if (MEMEQ("stylevalue", name, nameLen)) { | |
282 if (!parse_fixed<16>(value, &axis.fValue)) { | |
283 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue" , value); | |
284 } | |
285 } | |
286 } | |
287 } | |
288 | |
161 static void font_element_handler(FamilyData* self, const char** attributes) { | 289 static void font_element_handler(FamilyData* self, const char** attributes) { |
162 // A <font> element should be contained in a <family> element. | 290 // A <font> element should be contained in a <family> element. |
163 // The element may have 'weight' (non-negative integer), 'style' ("normal", "italic"), | 291 // The element may have 'weight' (non-negative integer), 'style' ("normal", "italic"), |
164 // and 'index' (non-negative integer) attributes. | 292 // and 'index' (non-negative integer) attributes. |
165 // The element should contain a filename. | 293 // The element should contain a filename. |
166 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back(); | 294 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back(); |
167 self->fCurrentFontInfo = &file; | 295 self->fCurrentFontInfo = &file; |
168 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { | 296 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
169 const char* name = attributes[i]; | 297 const char* name = attributes[i]; |
170 const char* value = attributes[i+1]; | 298 const char* value = attributes[i+1]; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 | 381 |
254 static void XMLCALL start_element_handler(void* data, const char* tag, const cha r** attributes) { | 382 static void XMLCALL start_element_handler(void* data, const char* tag, const cha r** attributes) { |
255 FamilyData* self = static_cast<FamilyData*>(data); | 383 FamilyData* self = static_cast<FamilyData*>(data); |
256 size_t len = strlen(tag); | 384 size_t len = strlen(tag); |
257 if (MEMEQ("family", tag, len)) { | 385 if (MEMEQ("family", tag, len)) { |
258 family_element_handler(self, attributes); | 386 family_element_handler(self, attributes); |
259 } else if (MEMEQ("font", tag, len)) { | 387 } else if (MEMEQ("font", tag, len)) { |
260 font_element_handler(self, attributes); | 388 font_element_handler(self, attributes); |
261 } else if (MEMEQ("alias", tag, len)) { | 389 } else if (MEMEQ("alias", tag, len)) { |
262 alias_element_handler(self, attributes); | 390 alias_element_handler(self, attributes); |
391 } else if (MEMEQ("axis", tag, len)) { | |
392 axis_element_handler(self, attributes); | |
263 } | 393 } |
264 } | 394 } |
265 | 395 |
266 static void XMLCALL end_element_handler(void* data, const char* tag) { | 396 static void XMLCALL end_element_handler(void* data, const char* tag) { |
267 FamilyData* self = static_cast<FamilyData*>(data); | 397 FamilyData* self = static_cast<FamilyData*>(data); |
268 size_t len = strlen(tag); | 398 size_t len = strlen(tag); |
269 if (MEMEQ("family", tag, len)) { | 399 if (MEMEQ("family", tag, len)) { |
270 *self->fFamilies.append() = self->fCurrentFamily.detach(); | 400 *self->fFamilies.append() = self->fCurrentFamily.detach(); |
271 } else if (MEMEQ("font", tag, len)) { | 401 } else if (MEMEQ("font", tag, len)) { |
272 XML_SetCharacterDataHandler(self->fParser, NULL); | 402 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(); | 780 const char* tag = fTag.c_str(); |
651 | 781 |
652 // strip off the rightmost "-.*" | 782 // strip off the rightmost "-.*" |
653 const char* parentTagEnd = strrchr(tag, '-'); | 783 const char* parentTagEnd = strrchr(tag, '-'); |
654 if (parentTagEnd == NULL) { | 784 if (parentTagEnd == NULL) { |
655 return SkLanguage(); | 785 return SkLanguage(); |
656 } | 786 } |
657 size_t parentTagLen = parentTagEnd - tag; | 787 size_t parentTagLen = parentTagEnd - tag; |
658 return SkLanguage(tag, parentTagLen); | 788 return SkLanguage(tag, parentTagLen); |
659 } | 789 } |
OLD | NEW |