1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
* Copyright (C) 2002, 2003 The Karbon Developers
* Copyright (C) 2006 Alexander Kellett <lypanov@kde.org>
* Copyright (C) 2006, 2007 Rob Buis <buis@kde.org>
* Copyright (C) 2007, 2009, 2013 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "core/svg/SVGParserUtilities.h"
#include "wtf/MathExtras.h"
#include <limits>
namespace blink {
template <typename FloatType>
static inline bool isValidRange(const FloatType x) {
static const FloatType max = std::numeric_limits<FloatType>::max();
return x >= -max && x <= max;
}
template <typename FloatType>
static inline bool isValidExponent(const FloatType x) {
return x >= std::numeric_limits<FloatType>::min_exponent10 &&
x <= std::numeric_limits<FloatType>::max_exponent10;
}
// We use this generic parseNumber function to allow the Path parsing code to
// work at a higher precision internally, without any unnecessary runtime cost
// or code complexity.
template <typename CharType, typename FloatType>
static bool genericParseNumber(const CharType*& cursor,
const CharType* end,
FloatType& number,
WhitespaceMode mode) {
if (mode & AllowLeadingWhitespace)
skipOptionalSVGSpaces(cursor, end);
const CharType* ptr = cursor;
// read the sign
int sign = 1;
if (ptr < end && *ptr == '+')
ptr++;
else if (ptr < end && *ptr == '-') {
ptr++;
sign = -1;
}
if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.'))
// The first character of a number must be one of [0-9+-.]
return false;
// read the integer part, build right-to-left
const CharType* digitsStart = ptr;
while (ptr < end && *ptr >= '0' && *ptr <= '9')
++ptr; // Advance to first non-digit.
FloatType integer = 0;
if (ptr != digitsStart) {
const CharType* ptrScanIntPart = ptr - 1;
FloatType multiplier = 1;
while (ptrScanIntPart >= digitsStart) {
integer += multiplier * static_cast<FloatType>(*(ptrScanIntPart--) - '0');
multiplier *= 10;
}
// Bail out early if this overflows.
if (!isValidRange(integer))
return false;
}
FloatType decimal = 0;
if (ptr < end && *ptr == '.') { // read the decimals
ptr++;
// There must be a least one digit following the .
if (ptr >= end || *ptr < '0' || *ptr > '9')
return false;
FloatType frac = 1;
while (ptr < end && *ptr >= '0' && *ptr <= '9') {
frac *= static_cast<FloatType>(0.1);
decimal += (*(ptr++) - '0') * frac;
}
}
// When we get here we should have consumed either a digit for the integer
// part or a fractional part (with at least one digit after the '.'.)
DCHECK_NE(digitsStart, ptr);
number = integer + decimal;
number *= sign;
// read the exponent part
if (ptr + 1 < end && (*ptr == 'e' || *ptr == 'E') &&
(ptr[1] != 'x' && ptr[1] != 'm')) {
ptr++;
// read the sign of the exponent
bool exponentIsNegative = false;
if (*ptr == '+')
ptr++;
else if (*ptr == '-') {
ptr++;
exponentIsNegative = true;
}
// There must be an exponent
if (ptr >= end || *ptr < '0' || *ptr > '9')
return false;
FloatType exponent = 0;
while (ptr < end && *ptr >= '0' && *ptr <= '9') {
exponent *= static_cast<FloatType>(10);
exponent += *ptr - '0';
ptr++;
}
if (exponentIsNegative)
exponent = -exponent;
// Make sure exponent is valid.
if (!isValidExponent(exponent))
return false;
if (exponent)
number *= static_cast<FloatType>(pow(10.0, static_cast<int>(exponent)));
}
// Don't return Infinity() or NaN().
if (!isValidRange(number))
return false;
// A valid number has been parsed. Commit cursor.
cursor = ptr;
if (mode & AllowTrailingWhitespace)
skipOptionalSVGSpacesOrDelimiter(cursor, end);
return true;
}
bool parseNumber(const LChar*& ptr,
const LChar* end,
float& number,
WhitespaceMode mode) {
return genericParseNumber(ptr, end, number, mode);
}
bool parseNumber(const UChar*& ptr,
const UChar* end,
float& number,
WhitespaceMode mode) {
return genericParseNumber(ptr, end, number, mode);
}
// only used to parse largeArcFlag and sweepFlag which must be a "0" or "1"
// and might not have any whitespace/comma after it
template <typename CharType>
bool genericParseArcFlag(const CharType*& ptr,
const CharType* end,
bool& flag) {
if (ptr >= end)
return false;
const CharType flagChar = *ptr;
if (flagChar == '0')
flag = false;
else if (flagChar == '1')
flag = true;
else
return false;
ptr++;
skipOptionalSVGSpacesOrDelimiter(ptr, end);
return true;
}
bool parseArcFlag(const LChar*& ptr, const LChar* end, bool& flag) {
return genericParseArcFlag(ptr, end, flag);
}
bool parseArcFlag(const UChar*& ptr, const UChar* end, bool& flag) {
return genericParseArcFlag(ptr, end, flag);
}
template <typename CharType>
static bool genericParseNumberOptionalNumber(const CharType*& ptr,
const CharType* end,
float& x,
float& y) {
if (!parseNumber(ptr, end, x))
return false;
if (ptr == end)
y = x;
else if (!parseNumber(ptr, end, y, AllowLeadingAndTrailingWhitespace))
return false;
return ptr == end;
}
bool parseNumberOptionalNumber(const String& string, float& x, float& y) {
if (string.isEmpty())
return false;
if (string.is8Bit()) {
const LChar* ptr = string.characters8();
const LChar* end = ptr + string.length();
return genericParseNumberOptionalNumber(ptr, end, x, y);
}
const UChar* ptr = string.characters16();
const UChar* end = ptr + string.length();
return genericParseNumberOptionalNumber(ptr, end, x, y);
}
} // namespace blink
|