| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2002, 2003 The Karbon Developers | 2 * Copyright (C) 2002, 2003 The Karbon Developers |
| 3 * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org> | 3 * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org> |
| 4 * Copyright (C) 2006, 2007 Rob Buis <buis@kde.org> | 4 * Copyright (C) 2006, 2007 Rob Buis <buis@kde.org> |
| 5 * Copyright (C) 2007, 2009, 2013 Apple Inc. All rights reserved. | 5 * Copyright (C) 2007, 2009, 2013 Apple Inc. All rights reserved. |
| 6 * | 6 * |
| 7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 static inline bool isValidRange(const FloatType& x) | 36 static inline bool isValidRange(const FloatType& x) |
| 37 { | 37 { |
| 38 static const FloatType max = std::numeric_limits<FloatType>::max(); | 38 static const FloatType max = std::numeric_limits<FloatType>::max(); |
| 39 return x >= -max && x <= max; | 39 return x >= -max && x <= max; |
| 40 } | 40 } |
| 41 | 41 |
| 42 // We use this generic parseNumber function to allow the Path parsing code to wo
rk | 42 // We use this generic parseNumber function to allow the Path parsing code to wo
rk |
| 43 // at a higher precision internally, without any unnecessary runtime cost or cod
e | 43 // at a higher precision internally, without any unnecessary runtime cost or cod
e |
| 44 // complexity. | 44 // complexity. |
| 45 template <typename CharType, typename FloatType> | 45 template <typename CharType, typename FloatType> |
| 46 static bool genericParseNumber(const CharType*& ptr, const CharType* end, FloatT
ype& number, WhitespaceMode mode) | 46 static bool genericParseNumber(const CharType*& ptr, const CharType* end, FloatT
ype& number, bool skip) |
| 47 { | 47 { |
| 48 FloatType integer, decimal, frac, exponent; | 48 FloatType integer, decimal, frac, exponent; |
| 49 int sign, expsign; | 49 int sign, expsign; |
| 50 const CharType* start = ptr; | 50 const CharType* start = ptr; |
| 51 | 51 |
| 52 exponent = 0; | 52 exponent = 0; |
| 53 integer = 0; | 53 integer = 0; |
| 54 frac = 1; | 54 frac = 1; |
| 55 decimal = 0; | 55 decimal = 0; |
| 56 sign = 1; | 56 sign = 1; |
| 57 expsign = 1; | 57 expsign = 1; |
| 58 | 58 |
| 59 if (mode & AllowLeadingWhitespace) | |
| 60 skipOptionalSVGSpaces(ptr, end); | |
| 61 | |
| 62 // read the sign | 59 // read the sign |
| 63 if (ptr < end && *ptr == '+') | 60 if (ptr < end && *ptr == '+') |
| 64 ptr++; | 61 ptr++; |
| 65 else if (ptr < end && *ptr == '-') { | 62 else if (ptr < end && *ptr == '-') { |
| 66 ptr++; | 63 ptr++; |
| 67 sign = -1; | 64 sign = -1; |
| 68 } | 65 } |
| 69 | 66 |
| 70 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) | 67 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) |
| 71 // The first character of a number must be one of [0-9+-.] | 68 // The first character of a number must be one of [0-9+-.] |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 if (exponent) | 129 if (exponent) |
| 133 number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(ex
ponent))); | 130 number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(ex
ponent))); |
| 134 | 131 |
| 135 // Don't return Infinity() or NaN(). | 132 // Don't return Infinity() or NaN(). |
| 136 if (!isValidRange(number)) | 133 if (!isValidRange(number)) |
| 137 return false; | 134 return false; |
| 138 | 135 |
| 139 if (start == ptr) | 136 if (start == ptr) |
| 140 return false; | 137 return false; |
| 141 | 138 |
| 142 if (mode & AllowTrailingWhitespace) | 139 if (skip) |
| 143 skipOptionalSVGSpacesOrDelimiter(ptr, end); | 140 skipOptionalSVGSpacesOrDelimiter(ptr, end); |
| 144 | 141 |
| 145 return true; | 142 return true; |
| 146 } | 143 } |
| 147 | 144 |
| 148 template <typename CharType> | 145 template <typename CharType> |
| 149 bool parseSVGNumber(CharType* begin, size_t length, double& number) | 146 bool parseSVGNumber(CharType* begin, size_t length, double& number) |
| 150 { | 147 { |
| 151 const CharType* ptr = begin; | 148 const CharType* ptr = begin; |
| 152 const CharType* end = ptr + length; | 149 const CharType* end = ptr + length; |
| 153 return genericParseNumber(ptr, end, number, AllowLeadingAndTrailingWhitespac
e); | 150 return genericParseNumber(ptr, end, number, false); |
| 154 } | 151 } |
| 155 | 152 |
| 156 // Explicitly instantiate the two flavors of parseSVGNumber() to satisfy externa
l callers | 153 // Explicitly instantiate the two flavors of parseSVGNumber() to satisfy externa
l callers |
| 157 template bool parseSVGNumber(LChar* begin, size_t length, double&); | 154 template bool parseSVGNumber(LChar* begin, size_t length, double&); |
| 158 template bool parseSVGNumber(UChar* begin, size_t length, double&); | 155 template bool parseSVGNumber(UChar* begin, size_t length, double&); |
| 159 | 156 |
| 160 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM
ode mode) | 157 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, bool skip) |
| 161 { | 158 { |
| 162 return genericParseNumber(ptr, end, number, mode); | 159 return genericParseNumber(ptr, end, number, skip); |
| 163 } | 160 } |
| 164 | 161 |
| 165 bool parseNumber(const UChar*& ptr, const UChar* end, float& number, WhitespaceM
ode mode) | 162 bool parseNumber(const UChar*& ptr, const UChar* end, float& number, bool skip) |
| 166 { | 163 { |
| 167 return genericParseNumber(ptr, end, number, mode); | 164 return genericParseNumber(ptr, end, number, skip); |
| 168 } | 165 } |
| 169 | 166 |
| 170 // only used to parse largeArcFlag and sweepFlag which must be a "0" or "1" | 167 // only used to parse largeArcFlag and sweepFlag which must be a "0" or "1" |
| 171 // and might not have any whitespace/comma after it | 168 // and might not have any whitespace/comma after it |
| 172 template <typename CharType> | 169 template <typename CharType> |
| 173 bool genericParseArcFlag(const CharType*& ptr, const CharType* end, bool& flag) | 170 bool genericParseArcFlag(const CharType*& ptr, const CharType* end, bool& flag) |
| 174 { | 171 { |
| 175 if (ptr >= end) | 172 if (ptr >= end) |
| 176 return false; | 173 return false; |
| 177 const CharType flagChar = *ptr++; | 174 const CharType flagChar = *ptr++; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 198 } | 195 } |
| 199 | 196 |
| 200 template<typename CharType> | 197 template<typename CharType> |
| 201 static bool genericParseNumberOptionalNumber(const CharType*& ptr, const CharTyp
e* end, float& x, float& y) | 198 static bool genericParseNumberOptionalNumber(const CharType*& ptr, const CharTyp
e* end, float& x, float& y) |
| 202 { | 199 { |
| 203 if (!parseNumber(ptr, end, x)) | 200 if (!parseNumber(ptr, end, x)) |
| 204 return false; | 201 return false; |
| 205 | 202 |
| 206 if (ptr == end) | 203 if (ptr == end) |
| 207 y = x; | 204 y = x; |
| 208 else if (!parseNumber(ptr, end, y, AllowLeadingAndTrailingWhitespace)) | 205 else if (!parseNumber(ptr, end, y, false)) |
| 209 return false; | 206 return false; |
| 210 | 207 |
| 211 return ptr == end; | 208 return ptr == end; |
| 212 } | 209 } |
| 213 | 210 |
| 214 bool parseNumberOptionalNumber(const String& string, float& x, float& y) | 211 bool parseNumberOptionalNumber(const String& string, float& x, float& y) |
| 215 { | 212 { |
| 216 if (string.isEmpty()) | 213 if (string.isEmpty()) |
| 217 return false; | 214 return false; |
| 218 | |
| 219 if (string.is8Bit()) { | 215 if (string.is8Bit()) { |
| 220 const LChar* ptr = string.characters8(); | 216 const LChar* ptr = string.characters8(); |
| 221 const LChar* end = ptr + string.length(); | 217 const LChar* end = ptr + string.length(); |
| 222 return genericParseNumberOptionalNumber(ptr, end, x, y); | 218 return genericParseNumberOptionalNumber(ptr, end, x, y); |
| 223 } | 219 } |
| 224 const UChar* ptr = string.characters16(); | 220 const UChar* ptr = string.characters16(); |
| 225 const UChar* end = ptr + string.length(); | 221 const UChar* end = ptr + string.length(); |
| 226 return genericParseNumberOptionalNumber(ptr, end, x, y); | 222 return genericParseNumberOptionalNumber(ptr, end, x, y); |
| 227 } | 223 } |
| 228 | 224 |
| 229 template<typename CharType> | 225 template<typename CharType> |
| 230 bool genericParseNumberOrPercentage(const CharType*& ptr, const CharType* end, f
loat& number) | |
| 231 { | |
| 232 if (genericParseNumber(ptr, end, number, AllowLeadingWhitespace)) { | |
| 233 if (ptr == end) | |
| 234 return true; | |
| 235 | |
| 236 bool isPercentage = (*ptr == '%'); | |
| 237 if (isPercentage) | |
| 238 ptr++; | |
| 239 | |
| 240 skipOptionalSVGSpaces(ptr, end); | |
| 241 | |
| 242 if (isPercentage) | |
| 243 number /= 100.f; | |
| 244 | |
| 245 return ptr == end; | |
| 246 } | |
| 247 | |
| 248 return false; | |
| 249 } | |
| 250 | |
| 251 bool parseNumberOrPercentage(const String& string, float& number) | |
| 252 { | |
| 253 if (string.isEmpty()) | |
| 254 return false; | |
| 255 | |
| 256 if (string.is8Bit()) { | |
| 257 const LChar* ptr = string.characters8(); | |
| 258 const LChar* end = ptr + string.length(); | |
| 259 return genericParseNumberOrPercentage(ptr, end, number); | |
| 260 } | |
| 261 const UChar* ptr = string.characters16(); | |
| 262 const UChar* end = ptr + string.length(); | |
| 263 return genericParseNumberOrPercentage(ptr, end, number); | |
| 264 } | |
| 265 | |
| 266 template<typename CharType> | |
| 267 static bool parseGlyphName(const CharType*& ptr, const CharType* end, HashSet<St
ring>& values) | 226 static bool parseGlyphName(const CharType*& ptr, const CharType* end, HashSet<St
ring>& values) |
| 268 { | 227 { |
| 269 skipOptionalSVGSpaces(ptr, end); | 228 skipOptionalSVGSpaces(ptr, end); |
| 270 | 229 |
| 271 while (ptr < end) { | 230 while (ptr < end) { |
| 272 // Leading and trailing white space, and white space before and after se
parators, will be ignored. | 231 // Leading and trailing white space, and white space before and after se
parators, will be ignored. |
| 273 const CharType* inputStart = ptr; | 232 const CharType* inputStart = ptr; |
| 274 while (ptr < end && *ptr != ',') | 233 while (ptr < end && *ptr != ',') |
| 275 ++ptr; | 234 ++ptr; |
| 276 | 235 |
| 277 if (ptr == inputStart) | 236 if (ptr == inputStart) |
| 278 break; | 237 break; |
| 279 | 238 |
| 280 // walk backwards from the ; to ignore any whitespace | 239 // walk backwards from the ; to ignore any whitespace |
| 281 const CharType* inputEnd = ptr - 1; | 240 const CharType* inputEnd = ptr - 1; |
| 282 while (inputStart < inputEnd && isHTMLSpace<CharType>(*inputEnd)) | 241 while (inputStart < inputEnd && isSVGSpace(*inputEnd)) |
| 283 --inputEnd; | 242 --inputEnd; |
| 284 | 243 |
| 285 values.add(String(inputStart, inputEnd - inputStart + 1)); | 244 values.add(String(inputStart, inputEnd - inputStart + 1)); |
| 286 skipOptionalSVGSpacesOrDelimiter(ptr, end, ','); | 245 skipOptionalSVGSpacesOrDelimiter(ptr, end, ','); |
| 287 } | 246 } |
| 288 | 247 |
| 289 return true; | 248 return true; |
| 290 } | 249 } |
| 291 | 250 |
| 292 bool parseGlyphName(const String& input, HashSet<String>& values) | 251 bool parseGlyphName(const String& input, HashSet<String>& values) |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 // Leading and trailing white space, and white space before and after se
micolon separators, will be ignored. | 383 // Leading and trailing white space, and white space before and after se
micolon separators, will be ignored. |
| 425 const CharType* inputStart = ptr; | 384 const CharType* inputStart = ptr; |
| 426 while (ptr < end && *ptr != seperator) // careful not to ignore whitespa
ce inside inputs | 385 while (ptr < end && *ptr != seperator) // careful not to ignore whitespa
ce inside inputs |
| 427 ptr++; | 386 ptr++; |
| 428 | 387 |
| 429 if (ptr == inputStart) | 388 if (ptr == inputStart) |
| 430 break; | 389 break; |
| 431 | 390 |
| 432 // walk backwards from the ; to ignore any whitespace | 391 // walk backwards from the ; to ignore any whitespace |
| 433 const CharType* inputEnd = ptr - 1; | 392 const CharType* inputEnd = ptr - 1; |
| 434 while (inputStart < inputEnd && isHTMLSpace<CharType>(*inputEnd)) | 393 while (inputStart < inputEnd && isSVGSpace(*inputEnd)) |
| 435 inputEnd--; | 394 inputEnd--; |
| 436 | 395 |
| 437 values.append(String(inputStart, inputEnd - inputStart + 1)); | 396 values.append(String(inputStart, inputEnd - inputStart + 1)); |
| 438 skipOptionalSVGSpacesOrDelimiter(ptr, end, seperator); | 397 skipOptionalSVGSpacesOrDelimiter(ptr, end, seperator); |
| 439 } | 398 } |
| 440 | 399 |
| 441 return values; | 400 return values; |
| 442 } | 401 } |
| 443 | 402 |
| 444 Vector<String> parseDelimitedString(const String& input, const char seperator) | 403 Vector<String> parseDelimitedString(const String& input, const char seperator) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 parseAndSkipTransformType(ptr, end, type); | 522 parseAndSkipTransformType(ptr, end, type); |
| 564 } else { | 523 } else { |
| 565 const UChar* ptr = string.characters16(); | 524 const UChar* ptr = string.characters16(); |
| 566 const UChar* end = ptr + string.length(); | 525 const UChar* end = ptr + string.length(); |
| 567 parseAndSkipTransformType(ptr, end, type); | 526 parseAndSkipTransformType(ptr, end, type); |
| 568 } | 527 } |
| 569 return type; | 528 return type; |
| 570 } | 529 } |
| 571 | 530 |
| 572 } | 531 } |
| OLD | NEW |