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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*
* 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-2018 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 "config.h"
#include "SVGParserUtilities.h"
#include "Document.h"
#include "FloatRect.h"
#include <limits>
#include <wtf/ASCIICType.h>
#include <wtf/text/StringParsingBuffer.h>
#include <wtf/text/StringView.h>
namespace WebCore {
template <typename FloatType> static inline bool isValidRange(const FloatType& x)
{
static const FloatType max = std::numeric_limits<FloatType>::max();
return x >= -max && x <= max;
}
// 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.
// FIXME: Can this be shared/replaced with number parsing in WTF?
template <typename CharacterType, typename FloatType = float> static std::optional<FloatType> genericParseNumber(StringParsingBuffer<CharacterType>& buffer, SuffixSkippingPolicy skip = SuffixSkippingPolicy::Skip)
{
FloatType number = 0;
FloatType integer = 0;
FloatType decimal = 0;
FloatType frac = 1;
FloatType exponent = 0;
int sign = 1;
int expsign = 1;
auto start = buffer.position();
// read the sign
if (buffer.hasCharactersRemaining() && *buffer == '+')
++buffer;
else if (buffer.hasCharactersRemaining() && *buffer == '-') {
++buffer;
sign = -1;
}
if (buffer.atEnd() || (!isASCIIDigit(*buffer) && *buffer != '.'))
return std::nullopt;
// read the integer part, build right-to-left
auto ptrStartIntPart = buffer.position();
// Advance to first non-digit.
skipWhile<isASCIIDigit>(buffer);
if (buffer.position() != ptrStartIntPart) {
auto ptrScanIntPart = buffer.position() - 1;
FloatType multiplier = 1;
while (ptrScanIntPart >= ptrStartIntPart) {
integer += multiplier * static_cast<FloatType>(*(ptrScanIntPart--) - '0');
multiplier *= 10;
}
// Bail out early if this overflows.
if (!isValidRange(integer))
return std::nullopt;
}
// read the decimals
if (buffer.hasCharactersRemaining() && *buffer == '.') {
++buffer;
// There must be a least one digit following the .
if (buffer.atEnd() || !isASCIIDigit(*buffer))
return std::nullopt;
while (buffer.hasCharactersRemaining() && isASCIIDigit(*buffer))
decimal += (*(buffer++) - '0') * (frac *= static_cast<FloatType>(0.1));
}
// read the exponent part
if (buffer.position() != start && buffer.position() + 1 < buffer.end() && (*buffer == 'e' || *buffer == 'E')
&& (buffer[1] != 'x' && buffer[1] != 'm')) {
++buffer;
// read the sign of the exponent
if (*buffer == '+')
++buffer;
else if (*buffer == '-') {
++buffer;
expsign = -1;
}
// There must be an exponent
if (buffer.atEnd() || !isASCIIDigit(*buffer))
return std::nullopt;
while (buffer.hasCharactersRemaining() && isASCIIDigit(*buffer)) {
exponent *= static_cast<FloatType>(10);
exponent += *buffer++ - '0';
}
// Make sure exponent is valid.
if (!isValidRange(exponent) || exponent > std::numeric_limits<FloatType>::max_exponent)
return std::nullopt;
}
number = integer + decimal;
number *= sign;
if (exponent)
number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(exponent)));
// Don't return Infinity() or NaN().
if (!isValidRange(number))
return std::nullopt;
if (start == buffer.position())
return std::nullopt;
if (skip == SuffixSkippingPolicy::Skip)
skipOptionalSVGSpacesOrDelimiter(buffer);
return number;
}
std::optional<float> parseNumber(StringParsingBuffer<LChar>& buffer, SuffixSkippingPolicy skip)
{
return genericParseNumber(buffer, skip);
}
std::optional<float> parseNumber(StringParsingBuffer<UChar>& buffer, SuffixSkippingPolicy skip)
{
return genericParseNumber(buffer, skip);
}
std::optional<float> parseNumber(StringView string, SuffixSkippingPolicy skip)
{
return readCharactersForParsing(string, [skip](auto buffer) -> std::optional<float> {
auto result = genericParseNumber(buffer, skip);
if (!buffer.atEnd())
return std::nullopt;
return result;
});
}
// 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 CharacterType> std::optional<bool> genericParseArcFlag(StringParsingBuffer<CharacterType>& buffer)
{
if (buffer.atEnd())
return std::nullopt;
const CharacterType flagChar = *buffer;
++buffer;
bool flag;
if (flagChar == '0')
flag = false;
else if (flagChar == '1')
flag = true;
else
return std::nullopt;
skipOptionalSVGSpacesOrDelimiter(buffer);
return flag;
}
std::optional<bool> parseArcFlag(StringParsingBuffer<LChar>& buffer)
{
return genericParseArcFlag(buffer);
}
std::optional<bool> parseArcFlag(StringParsingBuffer<UChar>& buffer)
{
return genericParseArcFlag(buffer);
}
std::optional<std::pair<float, float>> parseNumberOptionalNumber(StringView string)
{
if (string.isEmpty())
return std::nullopt;
return readCharactersForParsing(string, [](auto buffer) -> std::optional<std::pair<float, float>> {
auto x = parseNumber(buffer);
if (!x)
return std::nullopt;
if (buffer.atEnd())
return std::make_pair(*x, *x);
auto y = parseNumber(buffer, SuffixSkippingPolicy::DontSkip);
if (!y)
return std::nullopt;
if (!buffer.atEnd())
return std::nullopt;
return std::make_pair(*x, *y);
});
}
std::optional<FloatPoint> parsePoint(StringView string)
{
if (string.isEmpty())
return std::nullopt;
return readCharactersForParsing(string, [](auto buffer) -> std::optional<FloatPoint> {
if (!skipOptionalSVGSpaces(buffer))
return std::nullopt;
auto point = parseFloatPoint(buffer);
if (!point)
return std::nullopt;
// Disallow anything except spaces at the end.
skipOptionalSVGSpaces(buffer);
return point;
});
}
std::optional<FloatRect> parseRect(StringView string)
{
return readCharactersForParsing(string, [](auto buffer) -> std::optional<FloatRect> {
skipOptionalSVGSpaces(buffer);
auto x = parseNumber(buffer);
if (!x)
return std::nullopt;
auto y = parseNumber(buffer);
if (!y)
return std::nullopt;
auto width = parseNumber(buffer);
if (!width)
return std::nullopt;
auto height = parseNumber(buffer, SuffixSkippingPolicy::DontSkip);
if (!height)
return std::nullopt;
return FloatRect { *x, *y, *width, *height };
});
}
std::optional<HashSet<String>> parseGlyphName(StringView string)
{
// FIXME: Parsing error detection is missing.
return readCharactersForParsing(string, [](auto buffer) -> HashSet<String> {
skipOptionalSVGSpaces(buffer);
HashSet<String> values;
while (buffer.hasCharactersRemaining()) {
// Leading and trailing white space, and white space before and after separators, will be ignored.
auto inputStart = buffer.position();
skipUntil(buffer, ',');
if (buffer.position() == inputStart)
break;
// walk backwards from the ; to ignore any whitespace
auto inputEnd = buffer.position() - 1;
while (inputStart < inputEnd && isSVGSpace(*inputEnd))
--inputEnd;
values.add(String(inputStart, inputEnd - inputStart + 1));
skipOptionalSVGSpacesOrDelimiter(buffer, ',');
}
return values;
});
}
template<typename CharacterType> static std::optional<UnicodeRange> parseUnicodeRange(StringParsingBuffer<CharacterType> buffer)
{
unsigned length = buffer.lengthRemaining();
if (length < 2 || buffer[0] != 'U' || buffer[1] != '+')
return std::nullopt;
buffer += 2;
// Parse the starting hex number (or its prefix).
unsigned startRange = 0;
unsigned startLength = 0;
while (buffer.hasCharactersRemaining()) {
if (!isASCIIHexDigit(*buffer))
break;
++startLength;
if (startLength > 6)
return std::nullopt;
startRange = (startRange << 4) | toASCIIHexValue(*buffer);
++buffer;
}
// Handle the case of ranges separated by "-" sign.
if (2 + startLength < length && *buffer == '-') {
if (!startLength)
return std::nullopt;
// Parse the ending hex number (or its prefix).
unsigned endRange = 0;
unsigned endLength = 0;
++buffer;
while (buffer.hasCharactersRemaining()) {
if (!isASCIIHexDigit(*buffer))
break;
++endLength;
if (endLength > 6)
return std::nullopt;
endRange = (endRange << 4) | toASCIIHexValue(*buffer);
++buffer;
}
if (!endLength)
return std::nullopt;
UnicodeRange range;
range.first = startRange;
range.second = endRange;
return range;
}
// Handle the case of a number with some optional trailing question marks.
unsigned endRange = startRange;
while (buffer.hasCharactersRemaining()) {
if (*buffer != '?')
break;
++startLength;
if (startLength > 6)
return std::nullopt;
startRange <<= 4;
endRange = (endRange << 4) | 0xF;
++buffer;
}
if (!startLength)
return std::nullopt;
UnicodeRange range;
range.first = startRange;
range.second = endRange;
return range;
}
std::optional<std::pair<UnicodeRanges, HashSet<String>>> parseKerningUnicodeString(StringView string)
{
// FIXME: Parsing error detection is missing.
return readCharactersForParsing(string, [](auto buffer) -> std::pair<UnicodeRanges, HashSet<String>> {
UnicodeRanges rangeList;
HashSet<String> stringList;
while (1) {
auto inputStart = buffer.position();
skipUntil(buffer, ',');
if (buffer.position() == inputStart)
break;
// Try to parse unicode range first
if (auto range = parseUnicodeRange(StringParsingBuffer { inputStart, buffer.position() }))
rangeList.append(WTFMove(*range));
else
stringList.add(String(inputStart, buffer.position() - inputStart));
if (buffer.atEnd())
break;
++buffer;
}
return std::make_pair(WTFMove(rangeList), WTFMove(stringList));
});
}
template <typename CharacterType> static std::optional<FloatPoint> genericParseFloatPoint(StringParsingBuffer<CharacterType>& buffer)
{
auto x = parseNumber(buffer);
if (!x)
return std::nullopt;
auto y = parseNumber(buffer);
if (!y)
return std::nullopt;
return FloatPoint { *x, *y };
}
std::optional<FloatPoint> parseFloatPoint(StringParsingBuffer<LChar>& buffer)
{
return genericParseFloatPoint(buffer);
}
std::optional<FloatPoint> parseFloatPoint(StringParsingBuffer<UChar>& buffer)
{
return genericParseFloatPoint(buffer);
}
}
|