fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame] | 8 | #include "include/private/SkTPin.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/utils/SkParse.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 10 | #include "modules/svg/include/SkSVGAttributeParser.h" |
| 11 | #include "modules/svg/include/SkSVGTypes.h" |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // TODO: these should be shared with SkParse.cpp |
| 16 | |
| 17 | inline bool is_between(char c, char min, char max) { |
| 18 | SkASSERT(min <= max); |
| 19 | return (unsigned)(c - min) <= (unsigned)(max - min); |
| 20 | } |
| 21 | |
| 22 | inline bool is_eos(char c) { |
| 23 | return !c; |
| 24 | } |
| 25 | |
| 26 | inline bool is_ws(char c) { |
| 27 | return is_between(c, 1, 32); |
| 28 | } |
| 29 | |
| 30 | inline bool is_sep(char c) { |
| 31 | return is_ws(c) || c == ',' || c == ';'; |
| 32 | } |
| 33 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 34 | } // namespace |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 35 | |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame^] | 36 | template <typename T, typename TArray> |
| 37 | bool SkSVGAttributeParser::parseEnumMap(const TArray& arr, T* result) { |
| 38 | for (size_t i = 0; i < SK_ARRAY_COUNT(arr); ++i) { |
| 39 | if (this->parseExpectedStringToken(std::get<0>(arr[i]))) { |
| 40 | *result = std::get<1>(arr[i]); |
| 41 | return true; |
| 42 | } |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 47 | SkSVGAttributeParser::SkSVGAttributeParser(const char attributeString[]) |
| 48 | : fCurPos(attributeString) {} |
| 49 | |
| 50 | template <typename F> |
| 51 | inline bool SkSVGAttributeParser::advanceWhile(F f) { |
| 52 | auto initial = fCurPos; |
| 53 | while (f(*fCurPos)) { |
| 54 | fCurPos++; |
| 55 | } |
| 56 | return fCurPos != initial; |
| 57 | } |
| 58 | |
| 59 | inline bool SkSVGAttributeParser::parseEOSToken() { |
| 60 | return is_eos(*fCurPos); |
| 61 | } |
| 62 | |
| 63 | inline bool SkSVGAttributeParser::parseSepToken() { |
| 64 | return this->advanceWhile(is_sep); |
| 65 | } |
| 66 | |
| 67 | inline bool SkSVGAttributeParser::parseWSToken() { |
| 68 | return this->advanceWhile(is_ws); |
| 69 | } |
| 70 | |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 71 | inline bool SkSVGAttributeParser::parseCommaWspToken() { |
| 72 | // comma-wsp: |
| 73 | // (wsp+ comma? wsp*) | (comma wsp*) |
| 74 | return this->parseWSToken() || this->parseExpectedStringToken(","); |
| 75 | } |
| 76 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 77 | inline bool SkSVGAttributeParser::parseExpectedStringToken(const char* expected) { |
| 78 | const char* c = fCurPos; |
| 79 | |
| 80 | while (*c && *expected && *c == *expected) { |
| 81 | c++; |
| 82 | expected++; |
| 83 | } |
| 84 | |
| 85 | if (*expected) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | fCurPos = c; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool SkSVGAttributeParser::parseScalarToken(SkScalar* res) { |
| 94 | if (const char* next = SkParse::FindScalar(fCurPos, res)) { |
| 95 | fCurPos = next; |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | bool SkSVGAttributeParser::parseHexToken(uint32_t* res) { |
| 102 | if (const char* next = SkParse::FindHex(fCurPos, res)) { |
| 103 | fCurPos = next; |
| 104 | return true; |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | bool SkSVGAttributeParser::parseLengthUnitToken(SkSVGLength::Unit* unit) { |
| 110 | static const struct { |
| 111 | const char* fUnitName; |
| 112 | SkSVGLength::Unit fUnit; |
| 113 | } gUnitInfo[] = { |
| 114 | { "%" , SkSVGLength::Unit::kPercentage }, |
| 115 | { "em", SkSVGLength::Unit::kEMS }, |
| 116 | { "ex", SkSVGLength::Unit::kEXS }, |
| 117 | { "px", SkSVGLength::Unit::kPX }, |
| 118 | { "cm", SkSVGLength::Unit::kCM }, |
| 119 | { "mm", SkSVGLength::Unit::kMM }, |
| 120 | { "in", SkSVGLength::Unit::kIN }, |
| 121 | { "pt", SkSVGLength::Unit::kPT }, |
| 122 | { "pc", SkSVGLength::Unit::kPC }, |
| 123 | }; |
| 124 | |
| 125 | for (size_t i = 0; i < SK_ARRAY_COUNT(gUnitInfo); ++i) { |
| 126 | if (this->parseExpectedStringToken(gUnitInfo[i].fUnitName)) { |
| 127 | *unit = gUnitInfo[i].fUnit; |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 134 | // https://www.w3.org/TR/SVG11/types.html#DataTypeColor |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 135 | bool SkSVGAttributeParser::parseNamedColorToken(SkColor* c) { |
| 136 | if (const char* next = SkParse::FindNamedColor(fCurPos, strlen(fCurPos), c)) { |
| 137 | fCurPos = next; |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | bool SkSVGAttributeParser::parseHexColorToken(SkColor* c) { |
| 144 | uint32_t v; |
| 145 | const char* initial = fCurPos; |
| 146 | |
| 147 | if (!this->parseExpectedStringToken("#") || !this->parseHexToken(&v)) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | switch (fCurPos - initial) { |
| 152 | case 7: |
| 153 | // matched #xxxxxxx |
| 154 | break; |
| 155 | case 4: |
| 156 | // matched '#xxx; |
| 157 | v = ((v << 12) & 0x00f00000) | |
| 158 | ((v << 8) & 0x000ff000) | |
| 159 | ((v << 4) & 0x00000ff0) | |
| 160 | ((v << 0) & 0x0000000f); |
| 161 | break; |
| 162 | default: |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | *c = v | 0xff000000; |
| 167 | return true; |
| 168 | } |
| 169 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 170 | bool SkSVGAttributeParser::parseColorComponentToken(int32_t* c) { |
Tyler Denniston | 8ac25c4 | 2020-04-15 11:05:27 -0400 | [diff] [blame] | 171 | const auto parseIntegral = [this](int32_t* c) -> bool { |
| 172 | const char* p = SkParse::FindS32(fCurPos, c); |
| 173 | if (!p || *p == '.') { |
| 174 | // No value parsed, or fractional value. |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if (*p == '%') { |
| 179 | *c = SkScalarRoundToInt(*c * 255.0f / 100); |
| 180 | p++; |
| 181 | } |
| 182 | |
| 183 | fCurPos = p; |
| 184 | return true; |
| 185 | }; |
| 186 | |
| 187 | const auto parseFractional = [this](int32_t* c) -> bool { |
| 188 | SkScalar s; |
| 189 | const char* p = SkParse::FindScalar(fCurPos, &s); |
| 190 | if (!p || *p != '%') { |
| 191 | // Floating point must be a percentage (CSS2 rgb-percent syntax). |
| 192 | return false; |
| 193 | } |
| 194 | p++; // Skip '%' |
| 195 | |
| 196 | *c = SkScalarRoundToInt(s * 255.0f / 100); |
| 197 | fCurPos = p; |
| 198 | return true; |
| 199 | }; |
| 200 | |
| 201 | if (!parseIntegral(c) && !parseFractional(c)) { |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
Tyler Denniston | 8ac25c4 | 2020-04-15 11:05:27 -0400 | [diff] [blame] | 205 | *c = SkTPin<int32_t>(*c, 0, 255); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
| 209 | bool SkSVGAttributeParser::parseRGBColorToken(SkColor* c) { |
| 210 | return this->parseParenthesized("rgb", [this](SkColor* c) -> bool { |
| 211 | int32_t r, g, b; |
| 212 | if (this->parseColorComponentToken(&r) && |
| 213 | this->parseSepToken() && |
| 214 | this->parseColorComponentToken(&g) && |
| 215 | this->parseSepToken() && |
| 216 | this->parseColorComponentToken(&b)) { |
| 217 | |
| 218 | *c = SkColorSetRGB(static_cast<uint8_t>(r), |
| 219 | static_cast<uint8_t>(g), |
| 220 | static_cast<uint8_t>(b)); |
| 221 | return true; |
| 222 | } |
| 223 | return false; |
| 224 | }, c); |
| 225 | } |
| 226 | |
Tyler Denniston | 8ac25c4 | 2020-04-15 11:05:27 -0400 | [diff] [blame] | 227 | // https://www.w3.org/TR/SVG11/types.html#DataTypeColor |
| 228 | // And https://www.w3.org/TR/CSS2/syndata.html#color-units for the alternative |
| 229 | // forms supported by SVG (e.g. RGB percentages). |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 230 | bool SkSVGAttributeParser::parseColor(SkSVGColorType* color) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 231 | SkColor c; |
| 232 | |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 233 | // consume preceding whitespace |
| 234 | this->parseWSToken(); |
| 235 | |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 236 | bool parsedValue = false; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 237 | if (this->parseHexColorToken(&c) |
| 238 | || this->parseNamedColorToken(&c) |
| 239 | || this->parseRGBColorToken(&c)) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 240 | *color = SkSVGColorType(c); |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 241 | parsedValue = true; |
| 242 | |
| 243 | // consume trailing whitespace |
| 244 | this->parseWSToken(); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 245 | } |
| 246 | |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 247 | return parsedValue && this->parseEOSToken(); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 250 | // https://www.w3.org/TR/SVG11/linking.html#IRIReference |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 251 | bool SkSVGAttributeParser::parseIRI(SkSVGStringType* iri) { |
| 252 | // consume preceding whitespace |
| 253 | this->parseWSToken(); |
| 254 | |
| 255 | // we only support local fragments |
| 256 | if (!this->parseExpectedStringToken("#")) { |
| 257 | return false; |
| 258 | } |
| 259 | const auto* start = fCurPos; |
| 260 | this->advanceWhile([](char c) -> bool { return !is_eos(c) && c != ')'; }); |
| 261 | if (start == fCurPos) { |
| 262 | return false; |
| 263 | } |
| 264 | *iri = SkString(start, fCurPos - start); |
| 265 | return true; |
| 266 | } |
| 267 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 268 | // https://www.w3.org/TR/SVG11/types.html#DataTypeFuncIRI |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 269 | bool SkSVGAttributeParser::parseFuncIRI(SkSVGStringType* iri) { |
| 270 | return this->parseParenthesized("url", [this](SkSVGStringType* iri) -> bool { |
| 271 | return this->parseIRI(iri); |
| 272 | }, iri); |
| 273 | } |
| 274 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 275 | // https://www.w3.org/TR/SVG11/types.html#DataTypeNumber |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 276 | bool SkSVGAttributeParser::parseNumber(SkSVGNumberType* number) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 277 | // consume WS |
| 278 | this->parseWSToken(); |
| 279 | |
| 280 | SkScalar s; |
| 281 | if (this->parseScalarToken(&s)) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 282 | *number = SkSVGNumberType(s); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 283 | // consume trailing separators |
| 284 | this->parseSepToken(); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 291 | // https://www.w3.org/TR/SVG11/types.html#DataTypeLength |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 292 | bool SkSVGAttributeParser::parseLength(SkSVGLength* length) { |
| 293 | SkScalar s; |
| 294 | SkSVGLength::Unit u = SkSVGLength::Unit::kNumber; |
| 295 | |
| 296 | if (this->parseScalarToken(&s) && |
| 297 | (this->parseLengthUnitToken(&u) || this->parseSepToken() || this->parseEOSToken())) { |
| 298 | *length = SkSVGLength(s, u); |
| 299 | // consume trailing separators |
| 300 | this->parseSepToken(); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 306 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 307 | // https://www.w3.org/TR/SVG11/coords.html#ViewBoxAttribute |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 308 | bool SkSVGAttributeParser::parseViewBox(SkSVGViewBoxType* vb) { |
| 309 | SkScalar x, y, w, h; |
| 310 | this->parseWSToken(); |
| 311 | |
| 312 | bool parsedValue = false; |
| 313 | if (this->parseScalarToken(&x) && this->parseSepToken() && |
| 314 | this->parseScalarToken(&y) && this->parseSepToken() && |
| 315 | this->parseScalarToken(&w) && this->parseSepToken() && |
| 316 | this->parseScalarToken(&h)) { |
| 317 | |
| 318 | *vb = SkSVGViewBoxType(SkRect::MakeXYWH(x, y, w, h)); |
| 319 | parsedValue = true; |
| 320 | // consume trailing whitespace |
| 321 | this->parseWSToken(); |
| 322 | } |
| 323 | return parsedValue && this->parseEOSToken(); |
| 324 | } |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 325 | |
| 326 | template <typename Func, typename T> |
| 327 | bool SkSVGAttributeParser::parseParenthesized(const char* prefix, Func f, T* result) { |
| 328 | this->parseWSToken(); |
| 329 | if (prefix && !this->parseExpectedStringToken(prefix)) { |
| 330 | return false; |
| 331 | } |
| 332 | this->parseWSToken(); |
| 333 | if (!this->parseExpectedStringToken("(")) { |
| 334 | return false; |
| 335 | } |
| 336 | this->parseWSToken(); |
| 337 | |
| 338 | if (!f(result)) { |
| 339 | return false; |
| 340 | } |
| 341 | this->parseWSToken(); |
| 342 | |
| 343 | return this->parseExpectedStringToken(")"); |
| 344 | } |
| 345 | |
| 346 | bool SkSVGAttributeParser::parseMatrixToken(SkMatrix* matrix) { |
| 347 | return this->parseParenthesized("matrix", [this](SkMatrix* m) -> bool { |
| 348 | SkScalar scalars[6]; |
| 349 | for (int i = 0; i < 6; ++i) { |
| 350 | if (!(this->parseScalarToken(scalars + i) && |
| 351 | (i > 4 || this->parseSepToken()))) { |
| 352 | return false; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | m->setAll(scalars[0], scalars[2], scalars[4], scalars[1], scalars[3], scalars[5], 0, 0, 1); |
| 357 | return true; |
| 358 | }, matrix); |
| 359 | } |
| 360 | |
| 361 | bool SkSVGAttributeParser::parseTranslateToken(SkMatrix* matrix) { |
| 362 | return this->parseParenthesized("translate", [this](SkMatrix* m) -> bool { |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 363 | SkScalar tx = 0.0, ty = 0.0; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 364 | this->parseWSToken(); |
| 365 | if (!this->parseScalarToken(&tx)) { |
| 366 | return false; |
| 367 | } |
| 368 | |
Tyler Denniston | a625f92 | 2020-04-15 15:52:01 -0400 | [diff] [blame] | 369 | if (!this->parseSepToken() || !this->parseScalarToken(&ty)) { |
| 370 | ty = 0.0; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | m->setTranslate(tx, ty); |
| 374 | return true; |
| 375 | }, matrix); |
| 376 | } |
| 377 | |
| 378 | bool SkSVGAttributeParser::parseScaleToken(SkMatrix* matrix) { |
| 379 | return this->parseParenthesized("scale", [this](SkMatrix* m) -> bool { |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 380 | SkScalar sx = 0.0, sy = 0.0; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 381 | if (!this->parseScalarToken(&sx)) { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | if (!(this->parseSepToken() && this->parseScalarToken(&sy))) { |
| 386 | sy = sx; |
| 387 | } |
| 388 | |
| 389 | m->setScale(sx, sy); |
| 390 | return true; |
| 391 | }, matrix); |
| 392 | } |
| 393 | |
| 394 | bool SkSVGAttributeParser::parseRotateToken(SkMatrix* matrix) { |
| 395 | return this->parseParenthesized("rotate", [this](SkMatrix* m) -> bool { |
| 396 | SkScalar angle; |
| 397 | if (!this->parseScalarToken(&angle)) { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | SkScalar cx = 0; |
| 402 | SkScalar cy = 0; |
| 403 | // optional [<cx> <cy>] |
| 404 | if (this->parseSepToken() && this->parseScalarToken(&cx)) { |
| 405 | if (!(this->parseSepToken() && this->parseScalarToken(&cy))) { |
| 406 | return false; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | m->setRotate(angle, cx, cy); |
| 411 | return true; |
| 412 | }, matrix); |
| 413 | } |
| 414 | |
| 415 | bool SkSVGAttributeParser::parseSkewXToken(SkMatrix* matrix) { |
| 416 | return this->parseParenthesized("skewX", [this](SkMatrix* m) -> bool { |
| 417 | SkScalar angle; |
| 418 | if (!this->parseScalarToken(&angle)) { |
| 419 | return false; |
| 420 | } |
Tyler Denniston | d1e5b03 | 2020-04-08 13:02:41 -0400 | [diff] [blame] | 421 | m->setSkewX(tanf(SkDegreesToRadians(angle))); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 422 | return true; |
| 423 | }, matrix); |
| 424 | } |
| 425 | |
| 426 | bool SkSVGAttributeParser::parseSkewYToken(SkMatrix* matrix) { |
| 427 | return this->parseParenthesized("skewY", [this](SkMatrix* m) -> bool { |
| 428 | SkScalar angle; |
| 429 | if (!this->parseScalarToken(&angle)) { |
| 430 | return false; |
| 431 | } |
Tyler Denniston | d1e5b03 | 2020-04-08 13:02:41 -0400 | [diff] [blame] | 432 | m->setSkewY(tanf(SkDegreesToRadians(angle))); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 433 | return true; |
| 434 | }, matrix); |
| 435 | } |
| 436 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 437 | // https://www.w3.org/TR/SVG11/coords.html#TransformAttribute |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 438 | bool SkSVGAttributeParser::parseTransform(SkSVGTransformType* t) { |
| 439 | SkMatrix matrix = SkMatrix::I(); |
| 440 | |
| 441 | bool parsed = false; |
| 442 | while (true) { |
| 443 | SkMatrix m; |
| 444 | |
| 445 | if (!( this->parseMatrixToken(&m) |
| 446 | || this->parseTranslateToken(&m) |
| 447 | || this->parseScaleToken(&m) |
| 448 | || this->parseRotateToken(&m) |
| 449 | || this->parseSkewXToken(&m) |
| 450 | || this->parseSkewYToken(&m))) { |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | matrix.preConcat(m); |
| 455 | parsed = true; |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 456 | |
| 457 | this->parseCommaWspToken(); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | this->parseWSToken(); |
| 461 | if (!parsed || !this->parseEOSToken()) { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | *t = SkSVGTransformType(matrix); |
| 466 | return true; |
| 467 | } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 468 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 469 | // https://www.w3.org/TR/SVG11/painting.html#SpecifyingPaint |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 470 | bool SkSVGAttributeParser::parsePaint(SkSVGPaint* paint) { |
| 471 | SkSVGColorType c; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 472 | SkSVGStringType iri; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 473 | bool parsedValue = false; |
| 474 | if (this->parseColor(&c)) { |
| 475 | *paint = SkSVGPaint(c); |
| 476 | parsedValue = true; |
| 477 | } else if (this->parseExpectedStringToken("none")) { |
| 478 | *paint = SkSVGPaint(SkSVGPaint::Type::kNone); |
| 479 | parsedValue = true; |
| 480 | } else if (this->parseExpectedStringToken("currentColor")) { |
| 481 | *paint = SkSVGPaint(SkSVGPaint::Type::kCurrentColor); |
| 482 | parsedValue = true; |
| 483 | } else if (this->parseExpectedStringToken("inherit")) { |
| 484 | *paint = SkSVGPaint(SkSVGPaint::Type::kInherit); |
| 485 | parsedValue = true; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 486 | } else if (this->parseFuncIRI(&iri)) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 487 | *paint = SkSVGPaint(iri); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 488 | parsedValue = true; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 489 | } |
| 490 | return parsedValue && this->parseEOSToken(); |
| 491 | } |
| 492 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 493 | // https://www.w3.org/TR/SVG11/masking.html#ClipPathProperty |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 494 | bool SkSVGAttributeParser::parseClipPath(SkSVGClip* clip) { |
| 495 | SkSVGStringType iri; |
| 496 | bool parsedValue = false; |
| 497 | |
| 498 | if (this->parseExpectedStringToken("none")) { |
| 499 | *clip = SkSVGClip(SkSVGClip::Type::kNone); |
| 500 | parsedValue = true; |
| 501 | } else if (this->parseExpectedStringToken("inherit")) { |
| 502 | *clip = SkSVGClip(SkSVGClip::Type::kInherit); |
| 503 | parsedValue = true; |
| 504 | } else if (this->parseFuncIRI(&iri)) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 505 | *clip = SkSVGClip(iri); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 506 | parsedValue = true; |
| 507 | } |
| 508 | |
| 509 | return parsedValue && this->parseEOSToken(); |
| 510 | } |
| 511 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 512 | // https://www.w3.org/TR/SVG11/painting.html#StrokeLinecapProperty |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 513 | bool SkSVGAttributeParser::parseLineCap(SkSVGLineCap* cap) { |
| 514 | static const struct { |
| 515 | SkSVGLineCap::Type fType; |
| 516 | const char* fName; |
| 517 | } gCapInfo[] = { |
| 518 | { SkSVGLineCap::Type::kButt , "butt" }, |
| 519 | { SkSVGLineCap::Type::kRound , "round" }, |
| 520 | { SkSVGLineCap::Type::kSquare , "square" }, |
| 521 | { SkSVGLineCap::Type::kInherit, "inherit" }, |
| 522 | }; |
| 523 | |
| 524 | bool parsedValue = false; |
| 525 | for (size_t i = 0; i < SK_ARRAY_COUNT(gCapInfo); ++i) { |
| 526 | if (this->parseExpectedStringToken(gCapInfo[i].fName)) { |
| 527 | *cap = SkSVGLineCap(gCapInfo[i].fType); |
| 528 | parsedValue = true; |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return parsedValue && this->parseEOSToken(); |
| 534 | } |
| 535 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 536 | // https://www.w3.org/TR/SVG11/painting.html#StrokeLinejoinProperty |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 537 | bool SkSVGAttributeParser::parseLineJoin(SkSVGLineJoin* join) { |
| 538 | static const struct { |
| 539 | SkSVGLineJoin::Type fType; |
| 540 | const char* fName; |
| 541 | } gJoinInfo[] = { |
| 542 | { SkSVGLineJoin::Type::kMiter , "miter" }, |
| 543 | { SkSVGLineJoin::Type::kRound , "round" }, |
| 544 | { SkSVGLineJoin::Type::kBevel , "bevel" }, |
| 545 | { SkSVGLineJoin::Type::kInherit, "inherit" }, |
| 546 | }; |
| 547 | |
| 548 | bool parsedValue = false; |
| 549 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoinInfo); ++i) { |
| 550 | if (this->parseExpectedStringToken(gJoinInfo[i].fName)) { |
| 551 | *join = SkSVGLineJoin(gJoinInfo[i].fType); |
| 552 | parsedValue = true; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return parsedValue && this->parseEOSToken(); |
| 558 | } |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 559 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 560 | // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementSpreadMethodAttribute |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame] | 561 | bool SkSVGAttributeParser::parseSpreadMethod(SkSVGSpreadMethod* spread) { |
| 562 | static const struct { |
| 563 | SkSVGSpreadMethod::Type fType; |
| 564 | const char* fName; |
| 565 | } gSpreadInfo[] = { |
| 566 | { SkSVGSpreadMethod::Type::kPad , "pad" }, |
| 567 | { SkSVGSpreadMethod::Type::kReflect, "reflect" }, |
| 568 | { SkSVGSpreadMethod::Type::kRepeat , "repeat" }, |
| 569 | }; |
| 570 | |
| 571 | bool parsedValue = false; |
| 572 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSpreadInfo); ++i) { |
| 573 | if (this->parseExpectedStringToken(gSpreadInfo[i].fName)) { |
| 574 | *spread = SkSVGSpreadMethod(gSpreadInfo[i].fType); |
| 575 | parsedValue = true; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | return parsedValue && this->parseEOSToken(); |
| 581 | } |
| 582 | |
Tyler Denniston | 308c072 | 2020-04-14 10:53:41 -0400 | [diff] [blame] | 583 | // https://www.w3.org/TR/SVG11/pservers.html#StopElement |
| 584 | bool SkSVGAttributeParser::parseStopColor(SkSVGStopColor* stopColor) { |
| 585 | SkSVGColorType c; |
| 586 | bool parsedValue = false; |
| 587 | if (this->parseColor(&c)) { |
| 588 | *stopColor = SkSVGStopColor(c); |
| 589 | parsedValue = true; |
| 590 | } else if (this->parseExpectedStringToken("currentColor")) { |
| 591 | *stopColor = SkSVGStopColor(SkSVGStopColor::Type::kCurrentColor); |
| 592 | parsedValue = true; |
| 593 | } else if (this->parseExpectedStringToken("inherit")) { |
| 594 | *stopColor = SkSVGStopColor(SkSVGStopColor::Type::kInherit); |
| 595 | parsedValue = true; |
| 596 | } |
| 597 | return parsedValue && this->parseEOSToken(); |
| 598 | } |
| 599 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 600 | // https://www.w3.org/TR/SVG11/shapes.html#PolygonElementPointsAttribute |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 601 | bool SkSVGAttributeParser::parsePoints(SkSVGPointsType* points) { |
| 602 | SkTDArray<SkPoint> pts; |
| 603 | |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 604 | // Skip initial wsp. |
| 605 | // list-of-points: |
| 606 | // wsp* coordinate-pairs? wsp* |
| 607 | this->advanceWhile(is_ws); |
| 608 | |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 609 | bool parsedValue = false; |
| 610 | for (;;) { |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 611 | // Adjacent coordinate-pairs separated by comma-wsp. |
| 612 | // coordinate-pairs: |
| 613 | // coordinate-pair |
| 614 | // | coordinate-pair comma-wsp coordinate-pairs |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 615 | if (parsedValue && !this->parseCommaWspToken()) { |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 616 | break; |
| 617 | } |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 618 | |
| 619 | SkScalar x, y; |
| 620 | if (!this->parseScalarToken(&x)) { |
| 621 | break; |
| 622 | } |
| 623 | |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 624 | // Coordinate values separated by comma-wsp or '-'. |
| 625 | // coordinate-pair: |
| 626 | // coordinate comma-wsp coordinate |
| 627 | // | coordinate negative-coordinate |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 628 | if (!this->parseCommaWspToken() && !this->parseEOSToken() && *fCurPos != '-') { |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 629 | break; |
| 630 | } |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 631 | |
| 632 | if (!this->parseScalarToken(&y)) { |
| 633 | break; |
| 634 | } |
| 635 | |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 636 | pts.push_back(SkPoint::Make(x, y)); |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 637 | parsedValue = true; |
| 638 | } |
| 639 | |
| 640 | if (parsedValue && this->parseEOSToken()) { |
| 641 | *points = pts; |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | return false; |
| 646 | } |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 647 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 648 | // https://www.w3.org/TR/SVG11/painting.html#FillRuleProperty |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 649 | bool SkSVGAttributeParser::parseFillRule(SkSVGFillRule* fillRule) { |
| 650 | static const struct { |
| 651 | SkSVGFillRule::Type fType; |
| 652 | const char* fName; |
| 653 | } gFillRuleInfo[] = { |
| 654 | { SkSVGFillRule::Type::kNonZero, "nonzero" }, |
| 655 | { SkSVGFillRule::Type::kEvenOdd, "evenodd" }, |
| 656 | { SkSVGFillRule::Type::kInherit, "inherit" }, |
| 657 | }; |
| 658 | |
| 659 | bool parsedValue = false; |
| 660 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFillRuleInfo); ++i) { |
| 661 | if (this->parseExpectedStringToken(gFillRuleInfo[i].fName)) { |
| 662 | *fillRule = SkSVGFillRule(gFillRuleInfo[i].fType); |
| 663 | parsedValue = true; |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return parsedValue && this->parseEOSToken(); |
| 669 | } |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 670 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 671 | // https://www.w3.org/TR/SVG11/painting.html#VisibilityProperty |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 672 | bool SkSVGAttributeParser::parseVisibility(SkSVGVisibility* visibility) { |
| 673 | static const struct { |
| 674 | SkSVGVisibility::Type fType; |
| 675 | const char* fName; |
| 676 | } gVisibilityInfo[] = { |
| 677 | { SkSVGVisibility::Type::kVisible , "visible" }, |
| 678 | { SkSVGVisibility::Type::kHidden , "hidden" }, |
| 679 | { SkSVGVisibility::Type::kCollapse, "collapse" }, |
| 680 | { SkSVGVisibility::Type::kInherit , "inherit" }, |
| 681 | }; |
| 682 | |
| 683 | bool parsedValue = false; |
| 684 | for (const auto& parseInfo : gVisibilityInfo) { |
| 685 | if (this->parseExpectedStringToken(parseInfo.fName)) { |
| 686 | *visibility = SkSVGVisibility(parseInfo.fType); |
| 687 | parsedValue = true; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return parsedValue && this->parseEOSToken(); |
| 693 | } |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 694 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 695 | // https://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 696 | bool SkSVGAttributeParser::parseDashArray(SkSVGDashArray* dashArray) { |
| 697 | bool parsedValue = false; |
| 698 | if (this->parseExpectedStringToken("none")) { |
| 699 | *dashArray = SkSVGDashArray(SkSVGDashArray::Type::kNone); |
| 700 | parsedValue = true; |
| 701 | } else if (this->parseExpectedStringToken("inherit")) { |
| 702 | *dashArray = SkSVGDashArray(SkSVGDashArray::Type::kInherit); |
| 703 | parsedValue = true; |
| 704 | } else { |
| 705 | SkTDArray<SkSVGLength> dashes; |
| 706 | for (;;) { |
| 707 | SkSVGLength dash; |
| 708 | // parseLength() also consumes trailing separators. |
| 709 | if (!this->parseLength(&dash)) { |
| 710 | break; |
| 711 | } |
| 712 | |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 713 | dashes.push_back(dash); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 714 | parsedValue = true; |
| 715 | } |
| 716 | |
| 717 | if (parsedValue) { |
| 718 | *dashArray = SkSVGDashArray(std::move(dashes)); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return parsedValue && this->parseEOSToken(); |
| 723 | } |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame^] | 724 | |
| 725 | // https://www.w3.org/TR/SVG11/text.html#FontFamilyProperty |
| 726 | bool SkSVGAttributeParser::parseFontFamily(SkSVGFontFamily* family) { |
| 727 | bool parsedValue = false; |
| 728 | if (this->parseExpectedStringToken("inherit")) { |
| 729 | *family = SkSVGFontFamily(); |
| 730 | parsedValue = true; |
| 731 | } else { |
| 732 | // The spec allows specifying a comma-separated list for explicit fallback order. |
| 733 | // For now, we only use the first entry and rely on the font manager to handle fallback. |
| 734 | const auto* comma = strchr(fCurPos, ','); |
| 735 | auto family_name = comma ? SkString(fCurPos, comma - fCurPos) |
| 736 | : SkString(fCurPos); |
| 737 | *family = SkSVGFontFamily(family_name.c_str()); |
| 738 | fCurPos += strlen(fCurPos); |
| 739 | parsedValue = true; |
| 740 | } |
| 741 | |
| 742 | return parsedValue && this->parseEOSToken(); |
| 743 | } |
| 744 | |
| 745 | // https://www.w3.org/TR/SVG11/text.html#FontSizeProperty |
| 746 | bool SkSVGAttributeParser::parseFontSize(SkSVGFontSize* size) { |
| 747 | bool parsedValue = false; |
| 748 | if (this->parseExpectedStringToken("inherit")) { |
| 749 | *size = SkSVGFontSize(); |
| 750 | parsedValue = true; |
| 751 | } else { |
| 752 | SkSVGLength length; |
| 753 | if (this->parseLength(&length)) { |
| 754 | *size = SkSVGFontSize(length); |
| 755 | parsedValue = true; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | return parsedValue && this->parseEOSToken(); |
| 760 | } |
| 761 | |
| 762 | // https://www.w3.org/TR/SVG11/text.html#FontStyleProperty |
| 763 | bool SkSVGAttributeParser::parseFontStyle(SkSVGFontStyle* style) { |
| 764 | static constexpr std::tuple<const char*, SkSVGFontStyle::Type> gStyleMap[] = { |
| 765 | { "normal" , SkSVGFontStyle::Type::kNormal }, |
| 766 | { "italic" , SkSVGFontStyle::Type::kItalic }, |
| 767 | { "oblique", SkSVGFontStyle::Type::kOblique }, |
| 768 | { "inherit", SkSVGFontStyle::Type::kInherit }, |
| 769 | }; |
| 770 | |
| 771 | bool parsedValue = false; |
| 772 | SkSVGFontStyle::Type type; |
| 773 | |
| 774 | if (this->parseEnumMap(gStyleMap, &type)) { |
| 775 | *style = SkSVGFontStyle(type); |
| 776 | parsedValue = true; |
| 777 | } |
| 778 | |
| 779 | return parsedValue && this->parseEOSToken(); |
| 780 | } |
| 781 | |
| 782 | // https://www.w3.org/TR/SVG11/text.html#FontWeightProperty |
| 783 | bool SkSVGAttributeParser::parseFontWeight(SkSVGFontWeight* weight) { |
| 784 | static constexpr std::tuple<const char*, SkSVGFontWeight::Type> gWeightMap[] = { |
| 785 | { "normal" , SkSVGFontWeight::Type::kNormal }, |
| 786 | { "bold" , SkSVGFontWeight::Type::kBold }, |
| 787 | { "bolder" , SkSVGFontWeight::Type::kBolder }, |
| 788 | { "lighter", SkSVGFontWeight::Type::kLighter }, |
| 789 | { "100" , SkSVGFontWeight::Type::k100 }, |
| 790 | { "200" , SkSVGFontWeight::Type::k200 }, |
| 791 | { "300" , SkSVGFontWeight::Type::k300 }, |
| 792 | { "400" , SkSVGFontWeight::Type::k400 }, |
| 793 | { "500" , SkSVGFontWeight::Type::k500 }, |
| 794 | { "600" , SkSVGFontWeight::Type::k600 }, |
| 795 | { "700" , SkSVGFontWeight::Type::k700 }, |
| 796 | { "800" , SkSVGFontWeight::Type::k800 }, |
| 797 | { "900" , SkSVGFontWeight::Type::k900 }, |
| 798 | { "inherit", SkSVGFontWeight::Type::kInherit }, |
| 799 | }; |
| 800 | |
| 801 | bool parsedValue = false; |
| 802 | SkSVGFontWeight::Type type; |
| 803 | |
| 804 | if (this->parseEnumMap(gWeightMap, &type)) { |
| 805 | *weight = SkSVGFontWeight(type); |
| 806 | parsedValue = true; |
| 807 | } |
| 808 | |
| 809 | return parsedValue && this->parseEOSToken(); |
| 810 | } |