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 | |
| 36 | SkSVGAttributeParser::SkSVGAttributeParser(const char attributeString[]) |
| 37 | : fCurPos(attributeString) {} |
| 38 | |
| 39 | template <typename F> |
| 40 | inline bool SkSVGAttributeParser::advanceWhile(F f) { |
| 41 | auto initial = fCurPos; |
| 42 | while (f(*fCurPos)) { |
| 43 | fCurPos++; |
| 44 | } |
| 45 | return fCurPos != initial; |
| 46 | } |
| 47 | |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 48 | bool SkSVGAttributeParser::matchStringToken(const char* token, const char** newPos) const { |
| 49 | const char* c = fCurPos; |
| 50 | |
| 51 | while (*c && *token && *c == *token) { |
| 52 | c++; |
| 53 | token++; |
| 54 | } |
| 55 | |
| 56 | if (*token) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if (newPos) { |
| 61 | *newPos = c; |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 67 | bool SkSVGAttributeParser::parseEOSToken() { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 68 | return is_eos(*fCurPos); |
| 69 | } |
| 70 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 71 | bool SkSVGAttributeParser::parseSepToken() { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 72 | return this->advanceWhile(is_sep); |
| 73 | } |
| 74 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 75 | bool SkSVGAttributeParser::parseWSToken() { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 76 | return this->advanceWhile(is_ws); |
| 77 | } |
| 78 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 79 | bool SkSVGAttributeParser::parseCommaWspToken() { |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 80 | // comma-wsp: |
| 81 | // (wsp+ comma? wsp*) | (comma wsp*) |
| 82 | return this->parseWSToken() || this->parseExpectedStringToken(","); |
| 83 | } |
| 84 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 85 | bool SkSVGAttributeParser::parseExpectedStringToken(const char* expected) { |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 86 | const char* newPos; |
| 87 | if (!matchStringToken(expected, &newPos)) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 91 | fCurPos = newPos; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool SkSVGAttributeParser::parseScalarToken(SkScalar* res) { |
| 96 | if (const char* next = SkParse::FindScalar(fCurPos, res)) { |
| 97 | fCurPos = next; |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 103 | bool SkSVGAttributeParser::parseInt32Token(int32_t* res) { |
| 104 | if (const char* next = SkParse::FindS32(fCurPos, res)) { |
| 105 | fCurPos = next; |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 111 | bool SkSVGAttributeParser::parseHexToken(uint32_t* res) { |
| 112 | if (const char* next = SkParse::FindHex(fCurPos, res)) { |
| 113 | fCurPos = next; |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool SkSVGAttributeParser::parseLengthUnitToken(SkSVGLength::Unit* unit) { |
| 120 | static const struct { |
| 121 | const char* fUnitName; |
| 122 | SkSVGLength::Unit fUnit; |
| 123 | } gUnitInfo[] = { |
| 124 | { "%" , SkSVGLength::Unit::kPercentage }, |
| 125 | { "em", SkSVGLength::Unit::kEMS }, |
| 126 | { "ex", SkSVGLength::Unit::kEXS }, |
| 127 | { "px", SkSVGLength::Unit::kPX }, |
| 128 | { "cm", SkSVGLength::Unit::kCM }, |
| 129 | { "mm", SkSVGLength::Unit::kMM }, |
| 130 | { "in", SkSVGLength::Unit::kIN }, |
| 131 | { "pt", SkSVGLength::Unit::kPT }, |
| 132 | { "pc", SkSVGLength::Unit::kPC }, |
| 133 | }; |
| 134 | |
| 135 | for (size_t i = 0; i < SK_ARRAY_COUNT(gUnitInfo); ++i) { |
| 136 | if (this->parseExpectedStringToken(gUnitInfo[i].fUnitName)) { |
| 137 | *unit = gUnitInfo[i].fUnit; |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 144 | // https://www.w3.org/TR/SVG11/types.html#DataTypeColor |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 145 | bool SkSVGAttributeParser::parseNamedColorToken(SkColor* c) { |
| 146 | if (const char* next = SkParse::FindNamedColor(fCurPos, strlen(fCurPos), c)) { |
| 147 | fCurPos = next; |
| 148 | return true; |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | bool SkSVGAttributeParser::parseHexColorToken(SkColor* c) { |
| 154 | uint32_t v; |
| 155 | const char* initial = fCurPos; |
| 156 | |
| 157 | if (!this->parseExpectedStringToken("#") || !this->parseHexToken(&v)) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | switch (fCurPos - initial) { |
| 162 | case 7: |
| 163 | // matched #xxxxxxx |
| 164 | break; |
| 165 | case 4: |
| 166 | // matched '#xxx; |
| 167 | v = ((v << 12) & 0x00f00000) | |
| 168 | ((v << 8) & 0x000ff000) | |
| 169 | ((v << 4) & 0x00000ff0) | |
| 170 | ((v << 0) & 0x0000000f); |
| 171 | break; |
| 172 | default: |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | *c = v | 0xff000000; |
| 177 | return true; |
| 178 | } |
| 179 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 180 | bool SkSVGAttributeParser::parseColorComponentToken(int32_t* c) { |
Tyler Denniston | 8ac25c4 | 2020-04-15 11:05:27 -0400 | [diff] [blame] | 181 | const auto parseIntegral = [this](int32_t* c) -> bool { |
| 182 | const char* p = SkParse::FindS32(fCurPos, c); |
| 183 | if (!p || *p == '.') { |
| 184 | // No value parsed, or fractional value. |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | if (*p == '%') { |
| 189 | *c = SkScalarRoundToInt(*c * 255.0f / 100); |
| 190 | p++; |
| 191 | } |
| 192 | |
| 193 | fCurPos = p; |
| 194 | return true; |
| 195 | }; |
| 196 | |
| 197 | const auto parseFractional = [this](int32_t* c) -> bool { |
| 198 | SkScalar s; |
| 199 | const char* p = SkParse::FindScalar(fCurPos, &s); |
| 200 | if (!p || *p != '%') { |
| 201 | // Floating point must be a percentage (CSS2 rgb-percent syntax). |
| 202 | return false; |
| 203 | } |
| 204 | p++; // Skip '%' |
| 205 | |
| 206 | *c = SkScalarRoundToInt(s * 255.0f / 100); |
| 207 | fCurPos = p; |
| 208 | return true; |
| 209 | }; |
| 210 | |
| 211 | if (!parseIntegral(c) && !parseFractional(c)) { |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
Tyler Denniston | 8ac25c4 | 2020-04-15 11:05:27 -0400 | [diff] [blame] | 215 | *c = SkTPin<int32_t>(*c, 0, 255); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
| 219 | bool SkSVGAttributeParser::parseRGBColorToken(SkColor* c) { |
| 220 | return this->parseParenthesized("rgb", [this](SkColor* c) -> bool { |
| 221 | int32_t r, g, b; |
| 222 | if (this->parseColorComponentToken(&r) && |
| 223 | this->parseSepToken() && |
| 224 | this->parseColorComponentToken(&g) && |
| 225 | this->parseSepToken() && |
| 226 | this->parseColorComponentToken(&b)) { |
| 227 | |
| 228 | *c = SkColorSetRGB(static_cast<uint8_t>(r), |
| 229 | static_cast<uint8_t>(g), |
| 230 | static_cast<uint8_t>(b)); |
| 231 | return true; |
| 232 | } |
| 233 | return false; |
| 234 | }, c); |
| 235 | } |
| 236 | |
Tyler Denniston | 8ac25c4 | 2020-04-15 11:05:27 -0400 | [diff] [blame] | 237 | // https://www.w3.org/TR/SVG11/types.html#DataTypeColor |
| 238 | // And https://www.w3.org/TR/CSS2/syndata.html#color-units for the alternative |
| 239 | // forms supported by SVG (e.g. RGB percentages). |
Tyler Denniston | 4c6f57a | 2020-11-30 15:31:32 -0500 | [diff] [blame] | 240 | template <> |
| 241 | bool SkSVGAttributeParser::parse(SkSVGColorType* color) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 242 | SkColor c; |
| 243 | |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 244 | // consume preceding whitespace |
| 245 | this->parseWSToken(); |
| 246 | |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 247 | bool parsedValue = false; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 248 | if (this->parseHexColorToken(&c) |
| 249 | || this->parseNamedColorToken(&c) |
| 250 | || this->parseRGBColorToken(&c)) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 251 | *color = SkSVGColorType(c); |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 252 | parsedValue = true; |
| 253 | |
| 254 | // consume trailing whitespace |
| 255 | this->parseWSToken(); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 256 | } |
| 257 | |
fmalita | 61f36b3 | 2016-08-08 13:58:50 -0700 | [diff] [blame] | 258 | return parsedValue && this->parseEOSToken(); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Tyler Denniston | 7416571 | 2020-12-09 14:16:12 -0500 | [diff] [blame] | 261 | // https://www.w3.org/TR/SVG11/types.html#InterfaceSVGColor |
| 262 | template <> |
| 263 | bool SkSVGAttributeParser::parse(SkSVGColor* color) { |
| 264 | SkSVGColorType c; |
| 265 | bool parsedValue = false; |
| 266 | |
| 267 | if (this->parse(&c)) { |
| 268 | *color = SkSVGColor(c); |
| 269 | parsedValue = true; |
| 270 | } else if (this->parseExpectedStringToken("currentColor")) { |
| 271 | *color = SkSVGColor(SkSVGColor::Type::kCurrentColor); |
| 272 | parsedValue = true; |
| 273 | } |
| 274 | |
| 275 | return parsedValue && this->parseEOSToken(); |
| 276 | } |
| 277 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 278 | // https://www.w3.org/TR/SVG11/linking.html#IRIReference |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 279 | template <> |
| 280 | bool SkSVGAttributeParser::parse(SkSVGIRI* iri) { |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 281 | // consume preceding whitespace |
| 282 | this->parseWSToken(); |
| 283 | |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 284 | SkSVGIRI::Type iriType; |
| 285 | if (this->parseExpectedStringToken("#")) { |
| 286 | iriType = SkSVGIRI::Type::kLocal; |
| 287 | } else if (this->matchStringToken("data:")) { |
| 288 | iriType = SkSVGIRI::Type::kDataURI; |
| 289 | } else { |
| 290 | iriType = SkSVGIRI::Type::kNonlocal; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 291 | } |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 292 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 293 | const auto* start = fCurPos; |
| 294 | this->advanceWhile([](char c) -> bool { return !is_eos(c) && c != ')'; }); |
| 295 | if (start == fCurPos) { |
| 296 | return false; |
| 297 | } |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 298 | *iri = SkSVGIRI(iriType, SkString(start, fCurPos - start)); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 299 | return true; |
| 300 | } |
| 301 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 302 | // https://www.w3.org/TR/SVG11/types.html#DataTypeFuncIRI |
Tyler Denniston | e71f547 | 2021-01-27 13:30:59 -0500 | [diff] [blame] | 303 | bool SkSVGAttributeParser::parseFuncIRI(SkSVGFuncIRI* iri) { |
| 304 | return this->parseParenthesized("url", [this](SkSVGFuncIRI* iriResult) -> bool { |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 305 | SkSVGIRI iri; |
| 306 | if (this->parse(&iri)) { |
Tyler Denniston | e71f547 | 2021-01-27 13:30:59 -0500 | [diff] [blame] | 307 | *iriResult = SkSVGFuncIRI(std::move(iri)); |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | return false; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 311 | }, iri); |
| 312 | } |
| 313 | |
Tyler Denniston | b25caae | 2020-11-09 12:46:02 -0500 | [diff] [blame] | 314 | template <> |
| 315 | bool SkSVGAttributeParser::parse(SkSVGStringType* result) { |
| 316 | if (this->parseEOSToken()) { |
| 317 | return false; |
| 318 | } |
| 319 | *result = SkSVGStringType(fCurPos); |
| 320 | fCurPos += result->size(); |
| 321 | return this->parseEOSToken(); |
| 322 | } |
| 323 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 324 | // https://www.w3.org/TR/SVG11/types.html#DataTypeNumber |
Tyler Denniston | 4c6f57a | 2020-11-30 15:31:32 -0500 | [diff] [blame] | 325 | template <> |
| 326 | bool SkSVGAttributeParser::parse(SkSVGNumberType* number) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 327 | // consume WS |
| 328 | this->parseWSToken(); |
| 329 | |
| 330 | SkScalar s; |
| 331 | if (this->parseScalarToken(&s)) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 332 | *number = SkSVGNumberType(s); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 333 | // consume trailing separators |
| 334 | this->parseSepToken(); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | return false; |
| 339 | } |
| 340 | |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 341 | // https://www.w3.org/TR/SVG11/types.html#DataTypeInteger |
| 342 | bool SkSVGAttributeParser::parseInteger(SkSVGIntegerType* number) { |
| 343 | // consume WS |
| 344 | this->parseWSToken(); |
| 345 | |
| 346 | // consume optional '+' |
| 347 | this->parseExpectedStringToken("+"); |
| 348 | |
| 349 | SkSVGIntegerType i; |
| 350 | if (this->parseInt32Token(&i)) { |
| 351 | *number = SkSVGNumberType(i); |
| 352 | // consume trailing separators |
| 353 | this->parseSepToken(); |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 360 | // https://www.w3.org/TR/SVG11/types.html#DataTypeLength |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 361 | template <> |
| 362 | bool SkSVGAttributeParser::parse(SkSVGLength* length) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 363 | SkScalar s; |
| 364 | SkSVGLength::Unit u = SkSVGLength::Unit::kNumber; |
| 365 | |
| 366 | if (this->parseScalarToken(&s) && |
| 367 | (this->parseLengthUnitToken(&u) || this->parseSepToken() || this->parseEOSToken())) { |
| 368 | *length = SkSVGLength(s, u); |
| 369 | // consume trailing separators |
| 370 | this->parseSepToken(); |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | return false; |
| 375 | } |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 376 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 377 | // https://www.w3.org/TR/SVG11/coords.html#ViewBoxAttribute |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 378 | bool SkSVGAttributeParser::parseViewBox(SkSVGViewBoxType* vb) { |
| 379 | SkScalar x, y, w, h; |
| 380 | this->parseWSToken(); |
| 381 | |
| 382 | bool parsedValue = false; |
| 383 | if (this->parseScalarToken(&x) && this->parseSepToken() && |
| 384 | this->parseScalarToken(&y) && this->parseSepToken() && |
| 385 | this->parseScalarToken(&w) && this->parseSepToken() && |
| 386 | this->parseScalarToken(&h)) { |
| 387 | |
| 388 | *vb = SkSVGViewBoxType(SkRect::MakeXYWH(x, y, w, h)); |
| 389 | parsedValue = true; |
| 390 | // consume trailing whitespace |
| 391 | this->parseWSToken(); |
| 392 | } |
| 393 | return parsedValue && this->parseEOSToken(); |
| 394 | } |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 395 | |
| 396 | template <typename Func, typename T> |
| 397 | bool SkSVGAttributeParser::parseParenthesized(const char* prefix, Func f, T* result) { |
| 398 | this->parseWSToken(); |
| 399 | if (prefix && !this->parseExpectedStringToken(prefix)) { |
| 400 | return false; |
| 401 | } |
| 402 | this->parseWSToken(); |
| 403 | if (!this->parseExpectedStringToken("(")) { |
| 404 | return false; |
| 405 | } |
| 406 | this->parseWSToken(); |
| 407 | |
| 408 | if (!f(result)) { |
| 409 | return false; |
| 410 | } |
| 411 | this->parseWSToken(); |
| 412 | |
| 413 | return this->parseExpectedStringToken(")"); |
| 414 | } |
| 415 | |
| 416 | bool SkSVGAttributeParser::parseMatrixToken(SkMatrix* matrix) { |
| 417 | return this->parseParenthesized("matrix", [this](SkMatrix* m) -> bool { |
| 418 | SkScalar scalars[6]; |
| 419 | for (int i = 0; i < 6; ++i) { |
| 420 | if (!(this->parseScalarToken(scalars + i) && |
| 421 | (i > 4 || this->parseSepToken()))) { |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | m->setAll(scalars[0], scalars[2], scalars[4], scalars[1], scalars[3], scalars[5], 0, 0, 1); |
| 427 | return true; |
| 428 | }, matrix); |
| 429 | } |
| 430 | |
| 431 | bool SkSVGAttributeParser::parseTranslateToken(SkMatrix* matrix) { |
| 432 | return this->parseParenthesized("translate", [this](SkMatrix* m) -> bool { |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 433 | SkScalar tx = 0.0, ty = 0.0; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 434 | this->parseWSToken(); |
| 435 | if (!this->parseScalarToken(&tx)) { |
| 436 | return false; |
| 437 | } |
| 438 | |
Tyler Denniston | a625f92 | 2020-04-15 15:52:01 -0400 | [diff] [blame] | 439 | if (!this->parseSepToken() || !this->parseScalarToken(&ty)) { |
| 440 | ty = 0.0; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | m->setTranslate(tx, ty); |
| 444 | return true; |
| 445 | }, matrix); |
| 446 | } |
| 447 | |
| 448 | bool SkSVGAttributeParser::parseScaleToken(SkMatrix* matrix) { |
| 449 | return this->parseParenthesized("scale", [this](SkMatrix* m) -> bool { |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 450 | SkScalar sx = 0.0, sy = 0.0; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 451 | if (!this->parseScalarToken(&sx)) { |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | if (!(this->parseSepToken() && this->parseScalarToken(&sy))) { |
| 456 | sy = sx; |
| 457 | } |
| 458 | |
| 459 | m->setScale(sx, sy); |
| 460 | return true; |
| 461 | }, matrix); |
| 462 | } |
| 463 | |
| 464 | bool SkSVGAttributeParser::parseRotateToken(SkMatrix* matrix) { |
| 465 | return this->parseParenthesized("rotate", [this](SkMatrix* m) -> bool { |
| 466 | SkScalar angle; |
| 467 | if (!this->parseScalarToken(&angle)) { |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | SkScalar cx = 0; |
| 472 | SkScalar cy = 0; |
| 473 | // optional [<cx> <cy>] |
| 474 | if (this->parseSepToken() && this->parseScalarToken(&cx)) { |
| 475 | if (!(this->parseSepToken() && this->parseScalarToken(&cy))) { |
| 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | m->setRotate(angle, cx, cy); |
| 481 | return true; |
| 482 | }, matrix); |
| 483 | } |
| 484 | |
| 485 | bool SkSVGAttributeParser::parseSkewXToken(SkMatrix* matrix) { |
| 486 | return this->parseParenthesized("skewX", [this](SkMatrix* m) -> bool { |
| 487 | SkScalar angle; |
| 488 | if (!this->parseScalarToken(&angle)) { |
| 489 | return false; |
| 490 | } |
Tyler Denniston | d1e5b03 | 2020-04-08 13:02:41 -0400 | [diff] [blame] | 491 | m->setSkewX(tanf(SkDegreesToRadians(angle))); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 492 | return true; |
| 493 | }, matrix); |
| 494 | } |
| 495 | |
| 496 | bool SkSVGAttributeParser::parseSkewYToken(SkMatrix* matrix) { |
| 497 | return this->parseParenthesized("skewY", [this](SkMatrix* m) -> bool { |
| 498 | SkScalar angle; |
| 499 | if (!this->parseScalarToken(&angle)) { |
| 500 | return false; |
| 501 | } |
Tyler Denniston | d1e5b03 | 2020-04-08 13:02:41 -0400 | [diff] [blame] | 502 | m->setSkewY(tanf(SkDegreesToRadians(angle))); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 503 | return true; |
| 504 | }, matrix); |
| 505 | } |
| 506 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 507 | // https://www.w3.org/TR/SVG11/coords.html#TransformAttribute |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 508 | template <> |
| 509 | bool SkSVGAttributeParser::parse(SkSVGTransformType* t) { |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 510 | SkMatrix matrix = SkMatrix::I(); |
| 511 | |
| 512 | bool parsed = false; |
| 513 | while (true) { |
| 514 | SkMatrix m; |
| 515 | |
| 516 | if (!( this->parseMatrixToken(&m) |
| 517 | || this->parseTranslateToken(&m) |
| 518 | || this->parseScaleToken(&m) |
| 519 | || this->parseRotateToken(&m) |
| 520 | || this->parseSkewXToken(&m) |
| 521 | || this->parseSkewYToken(&m))) { |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | matrix.preConcat(m); |
| 526 | parsed = true; |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 527 | |
| 528 | this->parseCommaWspToken(); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | this->parseWSToken(); |
| 532 | if (!parsed || !this->parseEOSToken()) { |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | *t = SkSVGTransformType(matrix); |
| 537 | return true; |
| 538 | } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 539 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 540 | // https://www.w3.org/TR/SVG11/painting.html#SpecifyingPaint |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 541 | template <> |
| 542 | bool SkSVGAttributeParser::parse(SkSVGPaint* paint) { |
Tyler Denniston | 7416571 | 2020-12-09 14:16:12 -0500 | [diff] [blame] | 543 | SkSVGColor c; |
Tyler Denniston | e71f547 | 2021-01-27 13:30:59 -0500 | [diff] [blame] | 544 | SkSVGFuncIRI iri; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 545 | bool parsedValue = false; |
Tyler Denniston | 4c6f57a | 2020-11-30 15:31:32 -0500 | [diff] [blame] | 546 | if (this->parse(&c)) { |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 547 | *paint = SkSVGPaint(c); |
| 548 | parsedValue = true; |
| 549 | } else if (this->parseExpectedStringToken("none")) { |
| 550 | *paint = SkSVGPaint(SkSVGPaint::Type::kNone); |
| 551 | parsedValue = true; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 552 | } else if (this->parseFuncIRI(&iri)) { |
Tyler Denniston | e71f547 | 2021-01-27 13:30:59 -0500 | [diff] [blame] | 553 | *paint = SkSVGPaint(iri.iri()); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 554 | parsedValue = true; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 555 | } |
| 556 | return parsedValue && this->parseEOSToken(); |
| 557 | } |
| 558 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 559 | // https://www.w3.org/TR/SVG11/masking.html#ClipPathProperty |
Florin Malita | 836c2ca | 2021-01-13 11:48:02 -0500 | [diff] [blame] | 560 | // https://www.w3.org/TR/SVG11/masking.html#MaskProperty |
| 561 | // https://www.w3.org/TR/SVG11/filters.html#FilterProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 562 | template <> |
Florin Malita | 836c2ca | 2021-01-13 11:48:02 -0500 | [diff] [blame] | 563 | bool SkSVGAttributeParser::parse(SkSVGFuncIRI* firi) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 564 | SkSVGStringType iri; |
| 565 | bool parsedValue = false; |
| 566 | |
| 567 | if (this->parseExpectedStringToken("none")) { |
Florin Malita | 836c2ca | 2021-01-13 11:48:02 -0500 | [diff] [blame] | 568 | *firi = SkSVGFuncIRI(); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 569 | parsedValue = true; |
Tyler Denniston | e71f547 | 2021-01-27 13:30:59 -0500 | [diff] [blame] | 570 | } else if (this->parseFuncIRI(firi)) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 571 | parsedValue = true; |
| 572 | } |
| 573 | |
| 574 | return parsedValue && this->parseEOSToken(); |
| 575 | } |
| 576 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 577 | // https://www.w3.org/TR/SVG11/painting.html#StrokeLinecapProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 578 | template <> |
| 579 | bool SkSVGAttributeParser::parse(SkSVGLineCap* cap) { |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 580 | static const struct { |
Tyler Denniston | 041f665 | 2020-12-03 11:14:16 -0500 | [diff] [blame] | 581 | SkSVGLineCap fType; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 582 | const char* fName; |
| 583 | } gCapInfo[] = { |
Tyler Denniston | 041f665 | 2020-12-03 11:14:16 -0500 | [diff] [blame] | 584 | { SkSVGLineCap::kButt , "butt" }, |
| 585 | { SkSVGLineCap::kRound , "round" }, |
| 586 | { SkSVGLineCap::kSquare , "square" }, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 587 | }; |
| 588 | |
| 589 | bool parsedValue = false; |
| 590 | for (size_t i = 0; i < SK_ARRAY_COUNT(gCapInfo); ++i) { |
| 591 | if (this->parseExpectedStringToken(gCapInfo[i].fName)) { |
| 592 | *cap = SkSVGLineCap(gCapInfo[i].fType); |
| 593 | parsedValue = true; |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return parsedValue && this->parseEOSToken(); |
| 599 | } |
| 600 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 601 | // https://www.w3.org/TR/SVG11/painting.html#StrokeLinejoinProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 602 | template <> |
| 603 | bool SkSVGAttributeParser::parse(SkSVGLineJoin* join) { |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 604 | static const struct { |
| 605 | SkSVGLineJoin::Type fType; |
| 606 | const char* fName; |
| 607 | } gJoinInfo[] = { |
| 608 | { SkSVGLineJoin::Type::kMiter , "miter" }, |
| 609 | { SkSVGLineJoin::Type::kRound , "round" }, |
| 610 | { SkSVGLineJoin::Type::kBevel , "bevel" }, |
| 611 | { SkSVGLineJoin::Type::kInherit, "inherit" }, |
| 612 | }; |
| 613 | |
| 614 | bool parsedValue = false; |
| 615 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoinInfo); ++i) { |
| 616 | if (this->parseExpectedStringToken(gJoinInfo[i].fName)) { |
| 617 | *join = SkSVGLineJoin(gJoinInfo[i].fType); |
| 618 | parsedValue = true; |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | return parsedValue && this->parseEOSToken(); |
| 624 | } |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 625 | |
Tyler Denniston | 30e327e | 2020-10-29 16:29:22 -0400 | [diff] [blame] | 626 | // https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 627 | template <> |
| 628 | bool SkSVGAttributeParser::parse(SkSVGObjectBoundingBoxUnits* objectBoundingBoxUnits) { |
Tyler Denniston | ab76ab4 | 2020-10-21 15:08:45 -0400 | [diff] [blame] | 629 | bool parsedValue = false; |
| 630 | if (this->parseExpectedStringToken("userSpaceOnUse")) { |
Tyler Denniston | 30e327e | 2020-10-29 16:29:22 -0400 | [diff] [blame] | 631 | *objectBoundingBoxUnits = |
| 632 | SkSVGObjectBoundingBoxUnits(SkSVGObjectBoundingBoxUnits::Type::kUserSpaceOnUse); |
Tyler Denniston | ab76ab4 | 2020-10-21 15:08:45 -0400 | [diff] [blame] | 633 | parsedValue = true; |
| 634 | } else if (this->parseExpectedStringToken("objectBoundingBox")) { |
Tyler Denniston | 30e327e | 2020-10-29 16:29:22 -0400 | [diff] [blame] | 635 | *objectBoundingBoxUnits = |
| 636 | SkSVGObjectBoundingBoxUnits(SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox); |
Tyler Denniston | ab76ab4 | 2020-10-21 15:08:45 -0400 | [diff] [blame] | 637 | parsedValue = true; |
| 638 | } |
| 639 | return parsedValue && this->parseEOSToken(); |
| 640 | } |
| 641 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 642 | // https://www.w3.org/TR/SVG11/shapes.html#PolygonElementPointsAttribute |
Tyler Denniston | c683482 | 2021-02-08 15:07:03 -0500 | [diff] [blame^] | 643 | template <> |
| 644 | bool SkSVGAttributeParser::parse(SkSVGPointsType* points) { |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 645 | SkTDArray<SkPoint> pts; |
| 646 | |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 647 | // Skip initial wsp. |
| 648 | // list-of-points: |
| 649 | // wsp* coordinate-pairs? wsp* |
| 650 | this->advanceWhile(is_ws); |
| 651 | |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 652 | bool parsedValue = false; |
| 653 | for (;;) { |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 654 | // Adjacent coordinate-pairs separated by comma-wsp. |
| 655 | // coordinate-pairs: |
| 656 | // coordinate-pair |
| 657 | // | coordinate-pair comma-wsp coordinate-pairs |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 658 | if (parsedValue && !this->parseCommaWspToken()) { |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 659 | break; |
| 660 | } |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 661 | |
| 662 | SkScalar x, y; |
| 663 | if (!this->parseScalarToken(&x)) { |
| 664 | break; |
| 665 | } |
| 666 | |
Tyler Denniston | b96bb97 | 2020-04-08 17:36:11 -0400 | [diff] [blame] | 667 | // Coordinate values separated by comma-wsp or '-'. |
| 668 | // coordinate-pair: |
| 669 | // coordinate comma-wsp coordinate |
| 670 | // | coordinate negative-coordinate |
Tyler Denniston | 2d65b73 | 2020-04-15 16:08:26 -0400 | [diff] [blame] | 671 | if (!this->parseCommaWspToken() && !this->parseEOSToken() && *fCurPos != '-') { |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 672 | break; |
| 673 | } |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 674 | |
| 675 | if (!this->parseScalarToken(&y)) { |
| 676 | break; |
| 677 | } |
| 678 | |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 679 | pts.push_back(SkPoint::Make(x, y)); |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 680 | parsedValue = true; |
| 681 | } |
| 682 | |
| 683 | if (parsedValue && this->parseEOSToken()) { |
| 684 | *points = pts; |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | return false; |
| 689 | } |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 690 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 691 | // https://www.w3.org/TR/SVG11/painting.html#FillRuleProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 692 | template <> |
| 693 | bool SkSVGAttributeParser::parse(SkSVGFillRule* fillRule) { |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 694 | static const struct { |
| 695 | SkSVGFillRule::Type fType; |
| 696 | const char* fName; |
| 697 | } gFillRuleInfo[] = { |
| 698 | { SkSVGFillRule::Type::kNonZero, "nonzero" }, |
| 699 | { SkSVGFillRule::Type::kEvenOdd, "evenodd" }, |
| 700 | { SkSVGFillRule::Type::kInherit, "inherit" }, |
| 701 | }; |
| 702 | |
| 703 | bool parsedValue = false; |
| 704 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFillRuleInfo); ++i) { |
| 705 | if (this->parseExpectedStringToken(gFillRuleInfo[i].fName)) { |
| 706 | *fillRule = SkSVGFillRule(gFillRuleInfo[i].fType); |
| 707 | parsedValue = true; |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | return parsedValue && this->parseEOSToken(); |
| 713 | } |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 714 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 715 | // https://www.w3.org/TR/SVG11/painting.html#VisibilityProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 716 | template <> |
| 717 | bool SkSVGAttributeParser::parse(SkSVGVisibility* visibility) { |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 718 | static const struct { |
| 719 | SkSVGVisibility::Type fType; |
| 720 | const char* fName; |
| 721 | } gVisibilityInfo[] = { |
| 722 | { SkSVGVisibility::Type::kVisible , "visible" }, |
| 723 | { SkSVGVisibility::Type::kHidden , "hidden" }, |
| 724 | { SkSVGVisibility::Type::kCollapse, "collapse" }, |
| 725 | { SkSVGVisibility::Type::kInherit , "inherit" }, |
| 726 | }; |
| 727 | |
| 728 | bool parsedValue = false; |
| 729 | for (const auto& parseInfo : gVisibilityInfo) { |
| 730 | if (this->parseExpectedStringToken(parseInfo.fName)) { |
| 731 | *visibility = SkSVGVisibility(parseInfo.fType); |
| 732 | parsedValue = true; |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | return parsedValue && this->parseEOSToken(); |
| 738 | } |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 739 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 740 | // https://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 741 | template <> |
| 742 | bool SkSVGAttributeParser::parse(SkSVGDashArray* dashArray) { |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 743 | bool parsedValue = false; |
| 744 | if (this->parseExpectedStringToken("none")) { |
| 745 | *dashArray = SkSVGDashArray(SkSVGDashArray::Type::kNone); |
| 746 | parsedValue = true; |
| 747 | } else if (this->parseExpectedStringToken("inherit")) { |
| 748 | *dashArray = SkSVGDashArray(SkSVGDashArray::Type::kInherit); |
| 749 | parsedValue = true; |
| 750 | } else { |
| 751 | SkTDArray<SkSVGLength> dashes; |
| 752 | for (;;) { |
| 753 | SkSVGLength dash; |
| 754 | // parseLength() also consumes trailing separators. |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 755 | if (!this->parse(&dash)) { |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 756 | break; |
| 757 | } |
| 758 | |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 759 | dashes.push_back(dash); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 760 | parsedValue = true; |
| 761 | } |
| 762 | |
| 763 | if (parsedValue) { |
| 764 | *dashArray = SkSVGDashArray(std::move(dashes)); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return parsedValue && this->parseEOSToken(); |
| 769 | } |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 770 | |
| 771 | // https://www.w3.org/TR/SVG11/text.html#FontFamilyProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 772 | template <> |
| 773 | bool SkSVGAttributeParser::parse(SkSVGFontFamily* family) { |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 774 | bool parsedValue = false; |
| 775 | if (this->parseExpectedStringToken("inherit")) { |
| 776 | *family = SkSVGFontFamily(); |
| 777 | parsedValue = true; |
| 778 | } else { |
| 779 | // The spec allows specifying a comma-separated list for explicit fallback order. |
| 780 | // For now, we only use the first entry and rely on the font manager to handle fallback. |
| 781 | const auto* comma = strchr(fCurPos, ','); |
| 782 | auto family_name = comma ? SkString(fCurPos, comma - fCurPos) |
| 783 | : SkString(fCurPos); |
| 784 | *family = SkSVGFontFamily(family_name.c_str()); |
| 785 | fCurPos += strlen(fCurPos); |
| 786 | parsedValue = true; |
| 787 | } |
| 788 | |
| 789 | return parsedValue && this->parseEOSToken(); |
| 790 | } |
| 791 | |
| 792 | // https://www.w3.org/TR/SVG11/text.html#FontSizeProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 793 | template <> |
| 794 | bool SkSVGAttributeParser::parse(SkSVGFontSize* size) { |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 795 | bool parsedValue = false; |
| 796 | if (this->parseExpectedStringToken("inherit")) { |
| 797 | *size = SkSVGFontSize(); |
| 798 | parsedValue = true; |
| 799 | } else { |
| 800 | SkSVGLength length; |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 801 | if (this->parse(&length)) { |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 802 | *size = SkSVGFontSize(length); |
| 803 | parsedValue = true; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | return parsedValue && this->parseEOSToken(); |
| 808 | } |
| 809 | |
| 810 | // https://www.w3.org/TR/SVG11/text.html#FontStyleProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 811 | template <> |
| 812 | bool SkSVGAttributeParser::parse(SkSVGFontStyle* style) { |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 813 | static constexpr std::tuple<const char*, SkSVGFontStyle::Type> gStyleMap[] = { |
| 814 | { "normal" , SkSVGFontStyle::Type::kNormal }, |
| 815 | { "italic" , SkSVGFontStyle::Type::kItalic }, |
| 816 | { "oblique", SkSVGFontStyle::Type::kOblique }, |
| 817 | { "inherit", SkSVGFontStyle::Type::kInherit }, |
| 818 | }; |
| 819 | |
| 820 | bool parsedValue = false; |
| 821 | SkSVGFontStyle::Type type; |
| 822 | |
| 823 | if (this->parseEnumMap(gStyleMap, &type)) { |
| 824 | *style = SkSVGFontStyle(type); |
| 825 | parsedValue = true; |
| 826 | } |
| 827 | |
| 828 | return parsedValue && this->parseEOSToken(); |
| 829 | } |
| 830 | |
| 831 | // https://www.w3.org/TR/SVG11/text.html#FontWeightProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 832 | template <> |
| 833 | bool SkSVGAttributeParser::parse(SkSVGFontWeight* weight) { |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 834 | static constexpr std::tuple<const char*, SkSVGFontWeight::Type> gWeightMap[] = { |
| 835 | { "normal" , SkSVGFontWeight::Type::kNormal }, |
| 836 | { "bold" , SkSVGFontWeight::Type::kBold }, |
| 837 | { "bolder" , SkSVGFontWeight::Type::kBolder }, |
| 838 | { "lighter", SkSVGFontWeight::Type::kLighter }, |
| 839 | { "100" , SkSVGFontWeight::Type::k100 }, |
| 840 | { "200" , SkSVGFontWeight::Type::k200 }, |
| 841 | { "300" , SkSVGFontWeight::Type::k300 }, |
| 842 | { "400" , SkSVGFontWeight::Type::k400 }, |
| 843 | { "500" , SkSVGFontWeight::Type::k500 }, |
| 844 | { "600" , SkSVGFontWeight::Type::k600 }, |
| 845 | { "700" , SkSVGFontWeight::Type::k700 }, |
| 846 | { "800" , SkSVGFontWeight::Type::k800 }, |
| 847 | { "900" , SkSVGFontWeight::Type::k900 }, |
| 848 | { "inherit", SkSVGFontWeight::Type::kInherit }, |
| 849 | }; |
| 850 | |
| 851 | bool parsedValue = false; |
| 852 | SkSVGFontWeight::Type type; |
| 853 | |
| 854 | if (this->parseEnumMap(gWeightMap, &type)) { |
| 855 | *weight = SkSVGFontWeight(type); |
| 856 | parsedValue = true; |
| 857 | } |
| 858 | |
| 859 | return parsedValue && this->parseEOSToken(); |
| 860 | } |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 861 | |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame] | 862 | // https://www.w3.org/TR/SVG11/text.html#TextAnchorProperty |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 863 | template <> |
| 864 | bool SkSVGAttributeParser::parse(SkSVGTextAnchor* anchor) { |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame] | 865 | static constexpr std::tuple<const char*, SkSVGTextAnchor::Type> gAnchorMap[] = { |
| 866 | { "start" , SkSVGTextAnchor::Type::kStart }, |
| 867 | { "middle" , SkSVGTextAnchor::Type::kMiddle }, |
| 868 | { "end" , SkSVGTextAnchor::Type::kEnd }, |
| 869 | { "inherit", SkSVGTextAnchor::Type::kInherit}, |
| 870 | }; |
| 871 | |
| 872 | bool parsedValue = false; |
| 873 | SkSVGTextAnchor::Type type; |
| 874 | |
| 875 | if (this->parseEnumMap(gAnchorMap, &type)) { |
| 876 | *anchor = SkSVGTextAnchor(type); |
| 877 | parsedValue = true; |
| 878 | } |
| 879 | |
| 880 | return parsedValue && this->parseEOSToken(); |
| 881 | } |
| 882 | |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 883 | // https://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute |
| 884 | bool SkSVGAttributeParser::parsePreserveAspectRatio(SkSVGPreserveAspectRatio* par) { |
| 885 | static constexpr std::tuple<const char*, SkSVGPreserveAspectRatio::Align> gAlignMap[] = { |
| 886 | { "none" , SkSVGPreserveAspectRatio::kNone }, |
| 887 | { "xMinYMin", SkSVGPreserveAspectRatio::kXMinYMin }, |
| 888 | { "xMidYMin", SkSVGPreserveAspectRatio::kXMidYMin }, |
| 889 | { "xMaxYMin", SkSVGPreserveAspectRatio::kXMaxYMin }, |
| 890 | { "xMinYMid", SkSVGPreserveAspectRatio::kXMinYMid }, |
| 891 | { "xMidYMid", SkSVGPreserveAspectRatio::kXMidYMid }, |
| 892 | { "xMaxYMid", SkSVGPreserveAspectRatio::kXMaxYMid }, |
| 893 | { "xMinYMax", SkSVGPreserveAspectRatio::kXMinYMax }, |
| 894 | { "xMidYMax", SkSVGPreserveAspectRatio::kXMidYMax }, |
| 895 | { "xMaxYMax", SkSVGPreserveAspectRatio::kXMaxYMax }, |
| 896 | }; |
| 897 | |
| 898 | static constexpr std::tuple<const char*, SkSVGPreserveAspectRatio::Scale> gScaleMap[] = { |
| 899 | { "meet" , SkSVGPreserveAspectRatio::kMeet }, |
| 900 | { "slice", SkSVGPreserveAspectRatio::kSlice }, |
| 901 | }; |
| 902 | |
| 903 | bool parsedValue = false; |
| 904 | |
| 905 | // ignoring optional 'defer' |
| 906 | this->parseExpectedStringToken("defer"); |
| 907 | this->parseWSToken(); |
| 908 | |
| 909 | if (this->parseEnumMap(gAlignMap, &par->fAlign)) { |
| 910 | parsedValue = true; |
| 911 | |
| 912 | // optional scaling selector |
| 913 | this->parseWSToken(); |
| 914 | this->parseEnumMap(gScaleMap, &par->fScale); |
| 915 | } |
| 916 | |
| 917 | return parsedValue && this->parseEOSToken(); |
| 918 | } |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 919 | |
Tyler Denniston | 1f4cd07 | 2021-02-05 09:08:33 -0500 | [diff] [blame] | 920 | template <> |
| 921 | bool SkSVGAttributeParser::parse(SkSVGPreserveAspectRatio* par) { |
| 922 | return this->parsePreserveAspectRatio(par); |
| 923 | } |
| 924 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 925 | // https://www.w3.org/TR/SVG11/types.html#DataTypeCoordinates |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 926 | template <typename T> |
| 927 | bool SkSVGAttributeParser::parseList(std::vector<T>* vals) { |
| 928 | SkASSERT(vals->empty()); |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 929 | |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 930 | T v; |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 931 | for (;;) { |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 932 | if (!this->parse(&v)) { |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 933 | break; |
| 934 | } |
| 935 | |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 936 | vals->push_back(v); |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 937 | |
| 938 | this->parseCommaWspToken(); |
| 939 | } |
| 940 | |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 941 | return !vals->empty() && this->parseEOSToken(); |
| 942 | } |
| 943 | |
| 944 | template <> |
| 945 | bool SkSVGAttributeParser::parse(std::vector<SkSVGLength>* lengths) { |
| 946 | return this->parseList(lengths); |
| 947 | } |
| 948 | |
| 949 | template <> |
| 950 | bool SkSVGAttributeParser::parse(std::vector<SkSVGNumberType>* numbers) { |
| 951 | return this->parseList(numbers); |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 952 | } |
Tyler Denniston | 7bb85db | 2021-01-13 12:08:04 -0500 | [diff] [blame] | 953 | |
| 954 | template <> |
| 955 | bool SkSVGAttributeParser::parse(SkSVGColorspace* colorspace) { |
| 956 | static constexpr std::tuple<const char*, SkSVGColorspace> gColorspaceMap[] = { |
| 957 | { "auto" , SkSVGColorspace::kAuto }, |
| 958 | { "sRGB" , SkSVGColorspace::kSRGB }, |
| 959 | { "linearRGB", SkSVGColorspace::kLinearRGB }, |
| 960 | }; |
| 961 | |
| 962 | return this->parseEnumMap(gColorspaceMap, colorspace) && this->parseEOSToken(); |
| 963 | } |