Index: src/ports/SkFontConfigParser_android.cpp |
diff --git a/src/ports/SkFontConfigParser_android.cpp b/src/ports/SkFontConfigParser_android.cpp |
index e60f4e4ea307a3e74e58495c4ca6f80be0f2b668..bb81c6800d6da32a455e0f294e5312abfe28032f 100644 |
--- a/src/ports/SkFontConfigParser_android.cpp |
+++ b/src/ports/SkFontConfigParser_android.cpp |
@@ -16,6 +16,7 @@ |
#include <dirent.h> |
#include <stdio.h> |
+#include <climits> |
#include <limits> |
#include <stdlib.h> |
@@ -97,7 +98,7 @@ template <typename T> static bool parse_non_negative_integer(const char* s, T* v |
if (*s < '0' || '9' < *s) { |
return false; |
} |
- int d = *s - '0'; |
+ T d = *s - '0'; |
// Check for overflow |
if (n > nMax || (n == nMax && d > dMax)) { |
return false; |
@@ -108,6 +109,101 @@ template <typename T> static bool parse_non_negative_integer(const char* s, T* v |
return true; |
} |
+/** Parses a null terminated string into a signed fixed point value with bias N. |
+ * |
+ * Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def , |
+ * but may start with '.' and does not support 'e'. '-?:digit:*(.:digit:+)?' |
+ * |
+ * Checks for overflow. |
+ * Low bit rounding is not defined (is currently truncate). |
+ * Bias (N) required to allow for the sign bit and 4 bits of integer. |
+ * |
+ * If the string cannot be parsed into 'value', returns false and does not change 'value'. |
+ */ |
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
|
+template <int N, typename T> static bool parse_fixed(const char* s, T* value) { |
+ SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); |
+ SK_COMPILE_ASSERT(std::numeric_limits<T>::is_signed, T_must_be_signed); |
+ SK_COMPILE_ASSERT(sizeof(T) * CHAR_BIT - N >= 5, N_must_leave_four_bits_plus_sign); |
+ |
+ bool negate = false; |
+ if (*s == '-') { ++s; negate = true; } |
+ |
+ const T nMax = (std::numeric_limits<T>::max() >> N) / 10; |
+ const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10); |
+ |
+ T n = 0; |
+ T frac = 0; |
+ for (; *s; ++s) { |
+ // Check if digit |
+ if (*s < '0' || '9' < *s) { |
+ // If it wasn't a digit, check if it is a '.' followed by a digit. |
+ if (*s == '.' && '0' <= s[1] && s[1] <= '9') { |
+ // Find the end, verify digits. |
+ for (++s; *s; ++s) { |
+ if (*s < '0' || '9' < *s) { |
+ return false; |
+ } |
+ } |
+ // Read back toward the '.'. |
+ for (--s; *s != '.'; --s) { |
+ T d = *s - '0'; |
+ frac = (frac + (d << N)) / 10; // This requires four bits overhead. |
+ } |
+ break; |
+ } |
+ return false; |
+ } |
+ T d = *s - '0'; |
+ // Check for overflow |
+ if (n > nMax || (n == nMax && d > dMax)) { |
+ return false; |
+ } |
+ n = (n * 10) + d; |
+ } |
+ if (negate) { n = -n; frac = -frac; } |
+ *value = (n << N) + frac; |
+ return true; |
+} |
+ |
+#if 0 |
+#include "math.h" |
+template <int N, typename T> static void test_parse_fixed_r(double low, double high, double inc) { |
+ SkDEBUGCODE(double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);) |
+ double SK_FixedEpsilon_double = (1.0 / (1 << N)); |
+ double maxError = 0; |
+ char buffer[64]; |
+ for (double f = low; f < high; f += inc) { |
+ SkString s; |
+ sprintf(buffer, "%.20f", f); |
+ T fix; |
+ bool b = parse_fixed<N>(buffer, &fix); |
+ if (b) { |
+ double f2 = fix * SK_FixedEpsilon_double; |
+ SkASSERT(fabs(f - f2) <= SK_FixedEpsilon_double); |
+ maxError = SkTMax(maxError, fabs(f - f2)); |
+ } else { |
+ SkASSERT(f > SK_FixedMax_double || f < -SK_FixedMax_double); |
+ } |
+ } |
+ |
+ SkDebugf("maxError: %.20f\n", maxError); |
+} |
+ |
+static void test_parse_fixed() { |
+ test_parse_fixed_r<27, int32_t>(-17.1, 17.1, 0.000001); |
+ test_parse_fixed_r<16, int32_t>(-1.125, 1.125, 1.0 / (1 << 19)); |
+ test_parse_fixed_r<16, int32_t>(-32769.0, -32766.0, 1.0 / (1 << 19)); |
+ test_parse_fixed_r<16, int32_t>(32766.0, 32769.0, 1.0 / (1 << 19)); |
+ test_parse_fixed_r<16, int32_t>(-40000.0, 40000.0, 1.0 / (1 << 4)); |
+ test_parse_fixed_r<16, int32_t>(-1.1, 1.1, 0.0001); |
+ |
+ SkFixed fix; |
+ SkASSERT(!parse_fixed<16>("123.", &fix)); |
+ SkASSERT(!parse_fixed<16>("a", &fix)); |
+ SkASSERT(!parse_fixed<16>(".123a", &fix)); |
+} |
+#endif |
+ |
static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { |
return n1 == n2 && 0 == memcmp(s1, s2, n1); |
} |
@@ -158,6 +254,38 @@ static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { |
self->fCurrentFontInfo->fFileName.append(s, len); |
} |
+static void axis_element_handler(FamilyData* self, const char** attributes) { |
+ FontFileInfo& file = *self->fCurrentFontInfo; |
+ FontFileInfo::Axis& axis = file.fAxes.push_back(); |
+ for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
+ const char* name = attributes[i]; |
+ const char* value = attributes[i+1]; |
+ size_t nameLen = strlen(name); |
+ if (MEMEQ("tag", name, nameLen)) { |
+ size_t valueLen = strlen(value); |
+ if (valueLen == 4) { |
+ SkFourByteTag tag = SkSetFourByteTag(value[0], value[1], value[2], value[3]); |
+ for (int j = 0; j < file.fAxes.count() - 1; ++j) { |
+ if (file.fAxes[j].fTag == tag) { |
+ SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specified more than once", |
+ (tag >> 24) & 0xFF, |
+ (tag >> 16) & 0xFF, |
+ (tag >> 8) & 0xFF, |
+ (tag ) & 0xFF); |
+ } |
+ } |
+ axis.fTag = SkSetFourByteTag(value[0], value[1], value[2], value[3]); |
+ } else { |
+ SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", value); |
+ } |
+ } else if (MEMEQ("stylevalue", name, nameLen)) { |
+ if (!parse_fixed<16>(value, &axis.fValue)) { |
+ SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis stylevalue", value); |
+ } |
+ } |
+ } |
+} |
+ |
static void font_element_handler(FamilyData* self, const char** attributes) { |
// A <font> element should be contained in a <family> element. |
// The element may have 'weight' (non-negative integer), 'style' ("normal", "italic"), |
@@ -260,6 +388,8 @@ static void XMLCALL start_element_handler(void* data, const char* tag, const cha |
font_element_handler(self, attributes); |
} else if (MEMEQ("alias", tag, len)) { |
alias_element_handler(self, attributes); |
+ } else if (MEMEQ("axis", tag, len)) { |
+ axis_element_handler(self, attributes); |
} |
} |