blob: e169e9fd83fd7ab626c17aa516d405cf22678848 [file] [log] [blame]
fmalitabffc2562016-08-03 10:21:11 -07001/*
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 Klein8aa0edf2020-10-16 11:04:18 -05008#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/utils/SkParse.h"
Florin Malitab3418102020-10-15 18:10:29 -040010#include "modules/svg/include/SkSVGAttributeParser.h"
11#include "modules/svg/include/SkSVGTypes.h"
fmalitabffc2562016-08-03 10:21:11 -070012
13namespace {
14
15// TODO: these should be shared with SkParse.cpp
16
17inline bool is_between(char c, char min, char max) {
18 SkASSERT(min <= max);
19 return (unsigned)(c - min) <= (unsigned)(max - min);
20}
21
22inline bool is_eos(char c) {
23 return !c;
24}
25
26inline bool is_ws(char c) {
27 return is_between(c, 1, 32);
28}
29
30inline bool is_sep(char c) {
31 return is_ws(c) || c == ',' || c == ';';
32}
33
John Stilesa6841be2020-08-06 14:11:56 -040034} // namespace
fmalitabffc2562016-08-03 10:21:11 -070035
36SkSVGAttributeParser::SkSVGAttributeParser(const char attributeString[])
37 : fCurPos(attributeString) {}
38
39template <typename F>
40inline bool SkSVGAttributeParser::advanceWhile(F f) {
41 auto initial = fCurPos;
42 while (f(*fCurPos)) {
43 fCurPos++;
44 }
45 return fCurPos != initial;
46}
47
Tyler Denniston57154992020-11-04 16:08:30 -050048bool SkSVGAttributeParser::parseEOSToken() {
fmalitabffc2562016-08-03 10:21:11 -070049 return is_eos(*fCurPos);
50}
51
Tyler Denniston57154992020-11-04 16:08:30 -050052bool SkSVGAttributeParser::parseSepToken() {
fmalitabffc2562016-08-03 10:21:11 -070053 return this->advanceWhile(is_sep);
54}
55
Tyler Denniston57154992020-11-04 16:08:30 -050056bool SkSVGAttributeParser::parseWSToken() {
fmalitabffc2562016-08-03 10:21:11 -070057 return this->advanceWhile(is_ws);
58}
59
Tyler Denniston57154992020-11-04 16:08:30 -050060bool SkSVGAttributeParser::parseCommaWspToken() {
Tyler Denniston2d65b732020-04-15 16:08:26 -040061 // comma-wsp:
62 // (wsp+ comma? wsp*) | (comma wsp*)
63 return this->parseWSToken() || this->parseExpectedStringToken(",");
64}
65
Tyler Denniston57154992020-11-04 16:08:30 -050066bool SkSVGAttributeParser::parseExpectedStringToken(const char* expected) {
fmalitabffc2562016-08-03 10:21:11 -070067 const char* c = fCurPos;
68
69 while (*c && *expected && *c == *expected) {
70 c++;
71 expected++;
72 }
73
74 if (*expected) {
75 return false;
76 }
77
78 fCurPos = c;
79 return true;
80}
81
82bool SkSVGAttributeParser::parseScalarToken(SkScalar* res) {
83 if (const char* next = SkParse::FindScalar(fCurPos, res)) {
84 fCurPos = next;
85 return true;
86 }
87 return false;
88}
89
Tyler Dennistondada9602020-11-03 10:04:25 -050090bool SkSVGAttributeParser::parseInt32Token(int32_t* res) {
91 if (const char* next = SkParse::FindS32(fCurPos, res)) {
92 fCurPos = next;
93 return true;
94 }
95 return false;
96}
97
fmalitabffc2562016-08-03 10:21:11 -070098bool SkSVGAttributeParser::parseHexToken(uint32_t* res) {
99 if (const char* next = SkParse::FindHex(fCurPos, res)) {
100 fCurPos = next;
101 return true;
102 }
103 return false;
104}
105
106bool SkSVGAttributeParser::parseLengthUnitToken(SkSVGLength::Unit* unit) {
107 static const struct {
108 const char* fUnitName;
109 SkSVGLength::Unit fUnit;
110 } gUnitInfo[] = {
111 { "%" , SkSVGLength::Unit::kPercentage },
112 { "em", SkSVGLength::Unit::kEMS },
113 { "ex", SkSVGLength::Unit::kEXS },
114 { "px", SkSVGLength::Unit::kPX },
115 { "cm", SkSVGLength::Unit::kCM },
116 { "mm", SkSVGLength::Unit::kMM },
117 { "in", SkSVGLength::Unit::kIN },
118 { "pt", SkSVGLength::Unit::kPT },
119 { "pc", SkSVGLength::Unit::kPC },
120 };
121
122 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnitInfo); ++i) {
123 if (this->parseExpectedStringToken(gUnitInfo[i].fUnitName)) {
124 *unit = gUnitInfo[i].fUnit;
125 return true;
126 }
127 }
128 return false;
129}
130
Florin Malitaf005c252020-04-08 10:10:53 -0400131// https://www.w3.org/TR/SVG11/types.html#DataTypeColor
fmalitabffc2562016-08-03 10:21:11 -0700132bool SkSVGAttributeParser::parseNamedColorToken(SkColor* c) {
133 if (const char* next = SkParse::FindNamedColor(fCurPos, strlen(fCurPos), c)) {
134 fCurPos = next;
135 return true;
136 }
137 return false;
138}
139
140bool SkSVGAttributeParser::parseHexColorToken(SkColor* c) {
141 uint32_t v;
142 const char* initial = fCurPos;
143
144 if (!this->parseExpectedStringToken("#") || !this->parseHexToken(&v)) {
145 return false;
146 }
147
148 switch (fCurPos - initial) {
149 case 7:
150 // matched #xxxxxxx
151 break;
152 case 4:
153 // matched '#xxx;
154 v = ((v << 12) & 0x00f00000) |
155 ((v << 8) & 0x000ff000) |
156 ((v << 4) & 0x00000ff0) |
157 ((v << 0) & 0x0000000f);
158 break;
159 default:
160 return false;
161 }
162
163 *c = v | 0xff000000;
164 return true;
165}
166
fmalita28d5b722016-09-12 17:06:47 -0700167bool SkSVGAttributeParser::parseColorComponentToken(int32_t* c) {
Tyler Denniston8ac25c42020-04-15 11:05:27 -0400168 const auto parseIntegral = [this](int32_t* c) -> bool {
169 const char* p = SkParse::FindS32(fCurPos, c);
170 if (!p || *p == '.') {
171 // No value parsed, or fractional value.
172 return false;
173 }
174
175 if (*p == '%') {
176 *c = SkScalarRoundToInt(*c * 255.0f / 100);
177 p++;
178 }
179
180 fCurPos = p;
181 return true;
182 };
183
184 const auto parseFractional = [this](int32_t* c) -> bool {
185 SkScalar s;
186 const char* p = SkParse::FindScalar(fCurPos, &s);
187 if (!p || *p != '%') {
188 // Floating point must be a percentage (CSS2 rgb-percent syntax).
189 return false;
190 }
191 p++; // Skip '%'
192
193 *c = SkScalarRoundToInt(s * 255.0f / 100);
194 fCurPos = p;
195 return true;
196 };
197
198 if (!parseIntegral(c) && !parseFractional(c)) {
fmalita28d5b722016-09-12 17:06:47 -0700199 return false;
200 }
201
Tyler Denniston8ac25c42020-04-15 11:05:27 -0400202 *c = SkTPin<int32_t>(*c, 0, 255);
fmalita28d5b722016-09-12 17:06:47 -0700203 return true;
204}
205
206bool SkSVGAttributeParser::parseRGBColorToken(SkColor* c) {
207 return this->parseParenthesized("rgb", [this](SkColor* c) -> bool {
208 int32_t r, g, b;
209 if (this->parseColorComponentToken(&r) &&
210 this->parseSepToken() &&
211 this->parseColorComponentToken(&g) &&
212 this->parseSepToken() &&
213 this->parseColorComponentToken(&b)) {
214
215 *c = SkColorSetRGB(static_cast<uint8_t>(r),
216 static_cast<uint8_t>(g),
217 static_cast<uint8_t>(b));
218 return true;
219 }
220 return false;
221 }, c);
222}
223
Tyler Denniston8ac25c42020-04-15 11:05:27 -0400224// https://www.w3.org/TR/SVG11/types.html#DataTypeColor
225// And https://www.w3.org/TR/CSS2/syndata.html#color-units for the alternative
226// forms supported by SVG (e.g. RGB percentages).
fmalita397a5172016-08-08 11:38:55 -0700227bool SkSVGAttributeParser::parseColor(SkSVGColorType* color) {
fmalitabffc2562016-08-03 10:21:11 -0700228 SkColor c;
229
fmalita61f36b32016-08-08 13:58:50 -0700230 // consume preceding whitespace
231 this->parseWSToken();
232
fmalita61f36b32016-08-08 13:58:50 -0700233 bool parsedValue = false;
fmalita28d5b722016-09-12 17:06:47 -0700234 if (this->parseHexColorToken(&c)
235 || this->parseNamedColorToken(&c)
236 || this->parseRGBColorToken(&c)) {
fmalita397a5172016-08-08 11:38:55 -0700237 *color = SkSVGColorType(c);
fmalita61f36b32016-08-08 13:58:50 -0700238 parsedValue = true;
239
240 // consume trailing whitespace
241 this->parseWSToken();
fmalitabffc2562016-08-03 10:21:11 -0700242 }
243
fmalita61f36b32016-08-08 13:58:50 -0700244 return parsedValue && this->parseEOSToken();
fmalitabffc2562016-08-03 10:21:11 -0700245}
246
Florin Malitaf005c252020-04-08 10:10:53 -0400247// https://www.w3.org/TR/SVG11/linking.html#IRIReference
fmalita28d5b722016-09-12 17:06:47 -0700248bool SkSVGAttributeParser::parseIRI(SkSVGStringType* iri) {
249 // consume preceding whitespace
250 this->parseWSToken();
251
252 // we only support local fragments
253 if (!this->parseExpectedStringToken("#")) {
254 return false;
255 }
256 const auto* start = fCurPos;
257 this->advanceWhile([](char c) -> bool { return !is_eos(c) && c != ')'; });
258 if (start == fCurPos) {
259 return false;
260 }
261 *iri = SkString(start, fCurPos - start);
262 return true;
263}
264
Florin Malitaf005c252020-04-08 10:10:53 -0400265// https://www.w3.org/TR/SVG11/types.html#DataTypeFuncIRI
fmalita28d5b722016-09-12 17:06:47 -0700266bool SkSVGAttributeParser::parseFuncIRI(SkSVGStringType* iri) {
267 return this->parseParenthesized("url", [this](SkSVGStringType* iri) -> bool {
268 return this->parseIRI(iri);
269 }, iri);
270}
271
Florin Malitaf005c252020-04-08 10:10:53 -0400272// https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
fmalita397a5172016-08-08 11:38:55 -0700273bool SkSVGAttributeParser::parseNumber(SkSVGNumberType* number) {
fmalitabffc2562016-08-03 10:21:11 -0700274 // consume WS
275 this->parseWSToken();
276
277 SkScalar s;
278 if (this->parseScalarToken(&s)) {
fmalita397a5172016-08-08 11:38:55 -0700279 *number = SkSVGNumberType(s);
fmalitabffc2562016-08-03 10:21:11 -0700280 // consume trailing separators
281 this->parseSepToken();
282 return true;
283 }
284
285 return false;
286}
287
Tyler Dennistondada9602020-11-03 10:04:25 -0500288// https://www.w3.org/TR/SVG11/types.html#DataTypeInteger
289bool SkSVGAttributeParser::parseInteger(SkSVGIntegerType* number) {
290 // consume WS
291 this->parseWSToken();
292
293 // consume optional '+'
294 this->parseExpectedStringToken("+");
295
296 SkSVGIntegerType i;
297 if (this->parseInt32Token(&i)) {
298 *number = SkSVGNumberType(i);
299 // consume trailing separators
300 this->parseSepToken();
301 return true;
302 }
303
304 return false;
305}
306
Florin Malitaf005c252020-04-08 10:10:53 -0400307// https://www.w3.org/TR/SVG11/types.html#DataTypeLength
fmalitabffc2562016-08-03 10:21:11 -0700308bool SkSVGAttributeParser::parseLength(SkSVGLength* length) {
309 SkScalar s;
310 SkSVGLength::Unit u = SkSVGLength::Unit::kNumber;
311
312 if (this->parseScalarToken(&s) &&
313 (this->parseLengthUnitToken(&u) || this->parseSepToken() || this->parseEOSToken())) {
314 *length = SkSVGLength(s, u);
315 // consume trailing separators
316 this->parseSepToken();
317 return true;
318 }
319
320 return false;
321}
fmalita397a5172016-08-08 11:38:55 -0700322
Florin Malitaf005c252020-04-08 10:10:53 -0400323// https://www.w3.org/TR/SVG11/coords.html#ViewBoxAttribute
fmalita397a5172016-08-08 11:38:55 -0700324bool SkSVGAttributeParser::parseViewBox(SkSVGViewBoxType* vb) {
325 SkScalar x, y, w, h;
326 this->parseWSToken();
327
328 bool parsedValue = false;
329 if (this->parseScalarToken(&x) && this->parseSepToken() &&
330 this->parseScalarToken(&y) && this->parseSepToken() &&
331 this->parseScalarToken(&w) && this->parseSepToken() &&
332 this->parseScalarToken(&h)) {
333
334 *vb = SkSVGViewBoxType(SkRect::MakeXYWH(x, y, w, h));
335 parsedValue = true;
336 // consume trailing whitespace
337 this->parseWSToken();
338 }
339 return parsedValue && this->parseEOSToken();
340}
fmalitac97796b2016-08-08 12:58:57 -0700341
342template <typename Func, typename T>
343bool SkSVGAttributeParser::parseParenthesized(const char* prefix, Func f, T* result) {
344 this->parseWSToken();
345 if (prefix && !this->parseExpectedStringToken(prefix)) {
346 return false;
347 }
348 this->parseWSToken();
349 if (!this->parseExpectedStringToken("(")) {
350 return false;
351 }
352 this->parseWSToken();
353
354 if (!f(result)) {
355 return false;
356 }
357 this->parseWSToken();
358
359 return this->parseExpectedStringToken(")");
360}
361
362bool SkSVGAttributeParser::parseMatrixToken(SkMatrix* matrix) {
363 return this->parseParenthesized("matrix", [this](SkMatrix* m) -> bool {
364 SkScalar scalars[6];
365 for (int i = 0; i < 6; ++i) {
366 if (!(this->parseScalarToken(scalars + i) &&
367 (i > 4 || this->parseSepToken()))) {
368 return false;
369 }
370 }
371
372 m->setAll(scalars[0], scalars[2], scalars[4], scalars[1], scalars[3], scalars[5], 0, 0, 1);
373 return true;
374 }, matrix);
375}
376
377bool SkSVGAttributeParser::parseTranslateToken(SkMatrix* matrix) {
378 return this->parseParenthesized("translate", [this](SkMatrix* m) -> bool {
Kevin Lubick42846132018-01-05 10:11:11 -0500379 SkScalar tx = 0.0, ty = 0.0;
fmalitac97796b2016-08-08 12:58:57 -0700380 this->parseWSToken();
381 if (!this->parseScalarToken(&tx)) {
382 return false;
383 }
384
Tyler Dennistona625f922020-04-15 15:52:01 -0400385 if (!this->parseSepToken() || !this->parseScalarToken(&ty)) {
386 ty = 0.0;
fmalitac97796b2016-08-08 12:58:57 -0700387 }
388
389 m->setTranslate(tx, ty);
390 return true;
391 }, matrix);
392}
393
394bool SkSVGAttributeParser::parseScaleToken(SkMatrix* matrix) {
395 return this->parseParenthesized("scale", [this](SkMatrix* m) -> bool {
Kevin Lubick42846132018-01-05 10:11:11 -0500396 SkScalar sx = 0.0, sy = 0.0;
fmalitac97796b2016-08-08 12:58:57 -0700397 if (!this->parseScalarToken(&sx)) {
398 return false;
399 }
400
401 if (!(this->parseSepToken() && this->parseScalarToken(&sy))) {
402 sy = sx;
403 }
404
405 m->setScale(sx, sy);
406 return true;
407 }, matrix);
408}
409
410bool SkSVGAttributeParser::parseRotateToken(SkMatrix* matrix) {
411 return this->parseParenthesized("rotate", [this](SkMatrix* m) -> bool {
412 SkScalar angle;
413 if (!this->parseScalarToken(&angle)) {
414 return false;
415 }
416
417 SkScalar cx = 0;
418 SkScalar cy = 0;
419 // optional [<cx> <cy>]
420 if (this->parseSepToken() && this->parseScalarToken(&cx)) {
421 if (!(this->parseSepToken() && this->parseScalarToken(&cy))) {
422 return false;
423 }
424 }
425
426 m->setRotate(angle, cx, cy);
427 return true;
428 }, matrix);
429}
430
431bool SkSVGAttributeParser::parseSkewXToken(SkMatrix* matrix) {
432 return this->parseParenthesized("skewX", [this](SkMatrix* m) -> bool {
433 SkScalar angle;
434 if (!this->parseScalarToken(&angle)) {
435 return false;
436 }
Tyler Dennistond1e5b032020-04-08 13:02:41 -0400437 m->setSkewX(tanf(SkDegreesToRadians(angle)));
fmalitac97796b2016-08-08 12:58:57 -0700438 return true;
439 }, matrix);
440}
441
442bool SkSVGAttributeParser::parseSkewYToken(SkMatrix* matrix) {
443 return this->parseParenthesized("skewY", [this](SkMatrix* m) -> bool {
444 SkScalar angle;
445 if (!this->parseScalarToken(&angle)) {
446 return false;
447 }
Tyler Dennistond1e5b032020-04-08 13:02:41 -0400448 m->setSkewY(tanf(SkDegreesToRadians(angle)));
fmalitac97796b2016-08-08 12:58:57 -0700449 return true;
450 }, matrix);
451}
452
Florin Malitaf005c252020-04-08 10:10:53 -0400453// https://www.w3.org/TR/SVG11/coords.html#TransformAttribute
fmalitac97796b2016-08-08 12:58:57 -0700454bool SkSVGAttributeParser::parseTransform(SkSVGTransformType* t) {
455 SkMatrix matrix = SkMatrix::I();
456
457 bool parsed = false;
458 while (true) {
459 SkMatrix m;
460
461 if (!( this->parseMatrixToken(&m)
462 || this->parseTranslateToken(&m)
463 || this->parseScaleToken(&m)
464 || this->parseRotateToken(&m)
465 || this->parseSkewXToken(&m)
466 || this->parseSkewYToken(&m))) {
467 break;
468 }
469
470 matrix.preConcat(m);
471 parsed = true;
Tyler Denniston2d65b732020-04-15 16:08:26 -0400472
473 this->parseCommaWspToken();
fmalitac97796b2016-08-08 12:58:57 -0700474 }
475
476 this->parseWSToken();
477 if (!parsed || !this->parseEOSToken()) {
478 return false;
479 }
480
481 *t = SkSVGTransformType(matrix);
482 return true;
483}
fmalita2d961e02016-08-11 09:16:29 -0700484
Florin Malitaf005c252020-04-08 10:10:53 -0400485// https://www.w3.org/TR/SVG11/painting.html#SpecifyingPaint
fmalita2d961e02016-08-11 09:16:29 -0700486bool SkSVGAttributeParser::parsePaint(SkSVGPaint* paint) {
487 SkSVGColorType c;
fmalita28d5b722016-09-12 17:06:47 -0700488 SkSVGStringType iri;
fmalita2d961e02016-08-11 09:16:29 -0700489 bool parsedValue = false;
490 if (this->parseColor(&c)) {
491 *paint = SkSVGPaint(c);
492 parsedValue = true;
493 } else if (this->parseExpectedStringToken("none")) {
494 *paint = SkSVGPaint(SkSVGPaint::Type::kNone);
495 parsedValue = true;
496 } else if (this->parseExpectedStringToken("currentColor")) {
497 *paint = SkSVGPaint(SkSVGPaint::Type::kCurrentColor);
498 parsedValue = true;
499 } else if (this->parseExpectedStringToken("inherit")) {
500 *paint = SkSVGPaint(SkSVGPaint::Type::kInherit);
501 parsedValue = true;
fmalita28d5b722016-09-12 17:06:47 -0700502 } else if (this->parseFuncIRI(&iri)) {
Florin Malitaa3626692020-04-09 14:36:45 -0400503 *paint = SkSVGPaint(iri);
fmalita28d5b722016-09-12 17:06:47 -0700504 parsedValue = true;
fmalita2d961e02016-08-11 09:16:29 -0700505 }
506 return parsedValue && this->parseEOSToken();
507}
508
Florin Malitaf005c252020-04-08 10:10:53 -0400509// https://www.w3.org/TR/SVG11/masking.html#ClipPathProperty
Florin Malitace8840e2016-12-08 09:26:47 -0500510bool SkSVGAttributeParser::parseClipPath(SkSVGClip* clip) {
511 SkSVGStringType iri;
512 bool parsedValue = false;
513
514 if (this->parseExpectedStringToken("none")) {
515 *clip = SkSVGClip(SkSVGClip::Type::kNone);
516 parsedValue = true;
517 } else if (this->parseExpectedStringToken("inherit")) {
518 *clip = SkSVGClip(SkSVGClip::Type::kInherit);
519 parsedValue = true;
520 } else if (this->parseFuncIRI(&iri)) {
Florin Malitaa3626692020-04-09 14:36:45 -0400521 *clip = SkSVGClip(iri);
Florin Malitace8840e2016-12-08 09:26:47 -0500522 parsedValue = true;
523 }
524
525 return parsedValue && this->parseEOSToken();
526}
527
Florin Malitaf005c252020-04-08 10:10:53 -0400528// https://www.w3.org/TR/SVG11/painting.html#StrokeLinecapProperty
fmalita2d961e02016-08-11 09:16:29 -0700529bool SkSVGAttributeParser::parseLineCap(SkSVGLineCap* cap) {
530 static const struct {
531 SkSVGLineCap::Type fType;
532 const char* fName;
533 } gCapInfo[] = {
534 { SkSVGLineCap::Type::kButt , "butt" },
535 { SkSVGLineCap::Type::kRound , "round" },
536 { SkSVGLineCap::Type::kSquare , "square" },
537 { SkSVGLineCap::Type::kInherit, "inherit" },
538 };
539
540 bool parsedValue = false;
541 for (size_t i = 0; i < SK_ARRAY_COUNT(gCapInfo); ++i) {
542 if (this->parseExpectedStringToken(gCapInfo[i].fName)) {
543 *cap = SkSVGLineCap(gCapInfo[i].fType);
544 parsedValue = true;
545 break;
546 }
547 }
548
549 return parsedValue && this->parseEOSToken();
550}
551
Florin Malitaf005c252020-04-08 10:10:53 -0400552// https://www.w3.org/TR/SVG11/painting.html#StrokeLinejoinProperty
fmalita2d961e02016-08-11 09:16:29 -0700553bool SkSVGAttributeParser::parseLineJoin(SkSVGLineJoin* join) {
554 static const struct {
555 SkSVGLineJoin::Type fType;
556 const char* fName;
557 } gJoinInfo[] = {
558 { SkSVGLineJoin::Type::kMiter , "miter" },
559 { SkSVGLineJoin::Type::kRound , "round" },
560 { SkSVGLineJoin::Type::kBevel , "bevel" },
561 { SkSVGLineJoin::Type::kInherit, "inherit" },
562 };
563
564 bool parsedValue = false;
565 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoinInfo); ++i) {
566 if (this->parseExpectedStringToken(gJoinInfo[i].fName)) {
567 *join = SkSVGLineJoin(gJoinInfo[i].fType);
568 parsedValue = true;
569 break;
570 }
571 }
572
573 return parsedValue && this->parseEOSToken();
574}
fmalita5b31f322016-08-12 12:15:33 -0700575
Florin Malitaf005c252020-04-08 10:10:53 -0400576// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementSpreadMethodAttribute
fmalitacecd6172016-09-13 12:56:11 -0700577bool SkSVGAttributeParser::parseSpreadMethod(SkSVGSpreadMethod* spread) {
578 static const struct {
579 SkSVGSpreadMethod::Type fType;
580 const char* fName;
581 } gSpreadInfo[] = {
582 { SkSVGSpreadMethod::Type::kPad , "pad" },
583 { SkSVGSpreadMethod::Type::kReflect, "reflect" },
584 { SkSVGSpreadMethod::Type::kRepeat , "repeat" },
585 };
586
587 bool parsedValue = false;
588 for (size_t i = 0; i < SK_ARRAY_COUNT(gSpreadInfo); ++i) {
589 if (this->parseExpectedStringToken(gSpreadInfo[i].fName)) {
590 *spread = SkSVGSpreadMethod(gSpreadInfo[i].fType);
591 parsedValue = true;
592 break;
593 }
594 }
595
596 return parsedValue && this->parseEOSToken();
597}
598
Tyler Denniston308c0722020-04-14 10:53:41 -0400599// https://www.w3.org/TR/SVG11/pservers.html#StopElement
600bool SkSVGAttributeParser::parseStopColor(SkSVGStopColor* stopColor) {
601 SkSVGColorType c;
602 bool parsedValue = false;
603 if (this->parseColor(&c)) {
604 *stopColor = SkSVGStopColor(c);
605 parsedValue = true;
606 } else if (this->parseExpectedStringToken("currentColor")) {
607 *stopColor = SkSVGStopColor(SkSVGStopColor::Type::kCurrentColor);
608 parsedValue = true;
609 } else if (this->parseExpectedStringToken("inherit")) {
610 *stopColor = SkSVGStopColor(SkSVGStopColor::Type::kInherit);
611 parsedValue = true;
612 }
613 return parsedValue && this->parseEOSToken();
614}
615
Tyler Denniston30e327e2020-10-29 16:29:22 -0400616// https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits
617bool SkSVGAttributeParser::parseObjectBoundingBoxUnits(
618 SkSVGObjectBoundingBoxUnits* objectBoundingBoxUnits) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400619 bool parsedValue = false;
620 if (this->parseExpectedStringToken("userSpaceOnUse")) {
Tyler Denniston30e327e2020-10-29 16:29:22 -0400621 *objectBoundingBoxUnits =
622 SkSVGObjectBoundingBoxUnits(SkSVGObjectBoundingBoxUnits::Type::kUserSpaceOnUse);
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400623 parsedValue = true;
624 } else if (this->parseExpectedStringToken("objectBoundingBox")) {
Tyler Denniston30e327e2020-10-29 16:29:22 -0400625 *objectBoundingBoxUnits =
626 SkSVGObjectBoundingBoxUnits(SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox);
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400627 parsedValue = true;
628 }
629 return parsedValue && this->parseEOSToken();
630}
631
Florin Malitaf005c252020-04-08 10:10:53 -0400632// https://www.w3.org/TR/SVG11/shapes.html#PolygonElementPointsAttribute
fmalita5b31f322016-08-12 12:15:33 -0700633bool SkSVGAttributeParser::parsePoints(SkSVGPointsType* points) {
634 SkTDArray<SkPoint> pts;
635
Tyler Dennistonb96bb972020-04-08 17:36:11 -0400636 // Skip initial wsp.
637 // list-of-points:
638 // wsp* coordinate-pairs? wsp*
639 this->advanceWhile(is_ws);
640
fmalita5b31f322016-08-12 12:15:33 -0700641 bool parsedValue = false;
642 for (;;) {
Tyler Dennistonb96bb972020-04-08 17:36:11 -0400643 // Adjacent coordinate-pairs separated by comma-wsp.
644 // coordinate-pairs:
645 // coordinate-pair
646 // | coordinate-pair comma-wsp coordinate-pairs
Tyler Denniston2d65b732020-04-15 16:08:26 -0400647 if (parsedValue && !this->parseCommaWspToken()) {
Tyler Dennistonb96bb972020-04-08 17:36:11 -0400648 break;
649 }
fmalita5b31f322016-08-12 12:15:33 -0700650
651 SkScalar x, y;
652 if (!this->parseScalarToken(&x)) {
653 break;
654 }
655
Tyler Dennistonb96bb972020-04-08 17:36:11 -0400656 // Coordinate values separated by comma-wsp or '-'.
657 // coordinate-pair:
658 // coordinate comma-wsp coordinate
659 // | coordinate negative-coordinate
Tyler Denniston2d65b732020-04-15 16:08:26 -0400660 if (!this->parseCommaWspToken() && !this->parseEOSToken() && *fCurPos != '-') {
fmalita5b31f322016-08-12 12:15:33 -0700661 break;
662 }
fmalita5b31f322016-08-12 12:15:33 -0700663
664 if (!this->parseScalarToken(&y)) {
665 break;
666 }
667
Mike Reed5edcd312018-08-08 11:23:41 -0400668 pts.push_back(SkPoint::Make(x, y));
fmalita5b31f322016-08-12 12:15:33 -0700669 parsedValue = true;
670 }
671
672 if (parsedValue && this->parseEOSToken()) {
673 *points = pts;
674 return true;
675 }
676
677 return false;
678}
Florin Malitae932d4b2016-12-01 13:35:11 -0500679
Florin Malitaf005c252020-04-08 10:10:53 -0400680// https://www.w3.org/TR/SVG11/painting.html#FillRuleProperty
Florin Malitae932d4b2016-12-01 13:35:11 -0500681bool SkSVGAttributeParser::parseFillRule(SkSVGFillRule* fillRule) {
682 static const struct {
683 SkSVGFillRule::Type fType;
684 const char* fName;
685 } gFillRuleInfo[] = {
686 { SkSVGFillRule::Type::kNonZero, "nonzero" },
687 { SkSVGFillRule::Type::kEvenOdd, "evenodd" },
688 { SkSVGFillRule::Type::kInherit, "inherit" },
689 };
690
691 bool parsedValue = false;
692 for (size_t i = 0; i < SK_ARRAY_COUNT(gFillRuleInfo); ++i) {
693 if (this->parseExpectedStringToken(gFillRuleInfo[i].fName)) {
694 *fillRule = SkSVGFillRule(gFillRuleInfo[i].fType);
695 parsedValue = true;
696 break;
697 }
698 }
699
700 return parsedValue && this->parseEOSToken();
701}
Florin Malitaffe6ae42017-10-12 11:33:28 -0400702
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400703// https://www.w3.org/TR/SVG11/filters.html#FilterProperty
704bool SkSVGAttributeParser::parseFilter(SkSVGFilterType* filter) {
705 SkSVGStringType iri;
706 bool parsedValue = false;
707
708 if (this->parseExpectedStringToken("none")) {
709 *filter = SkSVGFilterType(SkSVGFilterType::Type::kNone);
710 parsedValue = true;
711 } else if (this->parseExpectedStringToken("inherit")) {
712 *filter = SkSVGFilterType(SkSVGFilterType::Type::kInherit);
713 parsedValue = true;
714 } else if (this->parseFuncIRI(&iri)) {
715 *filter = SkSVGFilterType(iri);
716 parsedValue = true;
717 }
718
719 return parsedValue && this->parseEOSToken();
720}
721
Florin Malitaf005c252020-04-08 10:10:53 -0400722// https://www.w3.org/TR/SVG11/painting.html#VisibilityProperty
Florin Malitaffe6ae42017-10-12 11:33:28 -0400723bool SkSVGAttributeParser::parseVisibility(SkSVGVisibility* visibility) {
724 static const struct {
725 SkSVGVisibility::Type fType;
726 const char* fName;
727 } gVisibilityInfo[] = {
728 { SkSVGVisibility::Type::kVisible , "visible" },
729 { SkSVGVisibility::Type::kHidden , "hidden" },
730 { SkSVGVisibility::Type::kCollapse, "collapse" },
731 { SkSVGVisibility::Type::kInherit , "inherit" },
732 };
733
734 bool parsedValue = false;
735 for (const auto& parseInfo : gVisibilityInfo) {
736 if (this->parseExpectedStringToken(parseInfo.fName)) {
737 *visibility = SkSVGVisibility(parseInfo.fType);
738 parsedValue = true;
739 break;
740 }
741 }
742
743 return parsedValue && this->parseEOSToken();
744}
Florin Malitaf543a602017-10-13 14:07:44 -0400745
Florin Malitaf005c252020-04-08 10:10:53 -0400746// https://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty
Florin Malitaf543a602017-10-13 14:07:44 -0400747bool SkSVGAttributeParser::parseDashArray(SkSVGDashArray* dashArray) {
748 bool parsedValue = false;
749 if (this->parseExpectedStringToken("none")) {
750 *dashArray = SkSVGDashArray(SkSVGDashArray::Type::kNone);
751 parsedValue = true;
752 } else if (this->parseExpectedStringToken("inherit")) {
753 *dashArray = SkSVGDashArray(SkSVGDashArray::Type::kInherit);
754 parsedValue = true;
755 } else {
756 SkTDArray<SkSVGLength> dashes;
757 for (;;) {
758 SkSVGLength dash;
759 // parseLength() also consumes trailing separators.
760 if (!this->parseLength(&dash)) {
761 break;
762 }
763
Mike Reed5edcd312018-08-08 11:23:41 -0400764 dashes.push_back(dash);
Florin Malitaf543a602017-10-13 14:07:44 -0400765 parsedValue = true;
766 }
767
768 if (parsedValue) {
769 *dashArray = SkSVGDashArray(std::move(dashes));
770 }
771 }
772
773 return parsedValue && this->parseEOSToken();
774}
Florin Malita39fe8c82020-10-20 10:43:03 -0400775
776// https://www.w3.org/TR/SVG11/text.html#FontFamilyProperty
777bool SkSVGAttributeParser::parseFontFamily(SkSVGFontFamily* family) {
778 bool parsedValue = false;
779 if (this->parseExpectedStringToken("inherit")) {
780 *family = SkSVGFontFamily();
781 parsedValue = true;
782 } else {
783 // The spec allows specifying a comma-separated list for explicit fallback order.
784 // For now, we only use the first entry and rely on the font manager to handle fallback.
785 const auto* comma = strchr(fCurPos, ',');
786 auto family_name = comma ? SkString(fCurPos, comma - fCurPos)
787 : SkString(fCurPos);
788 *family = SkSVGFontFamily(family_name.c_str());
789 fCurPos += strlen(fCurPos);
790 parsedValue = true;
791 }
792
793 return parsedValue && this->parseEOSToken();
794}
795
796// https://www.w3.org/TR/SVG11/text.html#FontSizeProperty
797bool SkSVGAttributeParser::parseFontSize(SkSVGFontSize* size) {
798 bool parsedValue = false;
799 if (this->parseExpectedStringToken("inherit")) {
800 *size = SkSVGFontSize();
801 parsedValue = true;
802 } else {
803 SkSVGLength length;
804 if (this->parseLength(&length)) {
805 *size = SkSVGFontSize(length);
806 parsedValue = true;
807 }
808 }
809
810 return parsedValue && this->parseEOSToken();
811}
812
813// https://www.w3.org/TR/SVG11/text.html#FontStyleProperty
814bool SkSVGAttributeParser::parseFontStyle(SkSVGFontStyle* style) {
815 static constexpr std::tuple<const char*, SkSVGFontStyle::Type> gStyleMap[] = {
816 { "normal" , SkSVGFontStyle::Type::kNormal },
817 { "italic" , SkSVGFontStyle::Type::kItalic },
818 { "oblique", SkSVGFontStyle::Type::kOblique },
819 { "inherit", SkSVGFontStyle::Type::kInherit },
820 };
821
822 bool parsedValue = false;
823 SkSVGFontStyle::Type type;
824
825 if (this->parseEnumMap(gStyleMap, &type)) {
826 *style = SkSVGFontStyle(type);
827 parsedValue = true;
828 }
829
830 return parsedValue && this->parseEOSToken();
831}
832
833// https://www.w3.org/TR/SVG11/text.html#FontWeightProperty
834bool SkSVGAttributeParser::parseFontWeight(SkSVGFontWeight* weight) {
835 static constexpr std::tuple<const char*, SkSVGFontWeight::Type> gWeightMap[] = {
836 { "normal" , SkSVGFontWeight::Type::kNormal },
837 { "bold" , SkSVGFontWeight::Type::kBold },
838 { "bolder" , SkSVGFontWeight::Type::kBolder },
839 { "lighter", SkSVGFontWeight::Type::kLighter },
840 { "100" , SkSVGFontWeight::Type::k100 },
841 { "200" , SkSVGFontWeight::Type::k200 },
842 { "300" , SkSVGFontWeight::Type::k300 },
843 { "400" , SkSVGFontWeight::Type::k400 },
844 { "500" , SkSVGFontWeight::Type::k500 },
845 { "600" , SkSVGFontWeight::Type::k600 },
846 { "700" , SkSVGFontWeight::Type::k700 },
847 { "800" , SkSVGFontWeight::Type::k800 },
848 { "900" , SkSVGFontWeight::Type::k900 },
849 { "inherit", SkSVGFontWeight::Type::kInherit },
850 };
851
852 bool parsedValue = false;
853 SkSVGFontWeight::Type type;
854
855 if (this->parseEnumMap(gWeightMap, &type)) {
856 *weight = SkSVGFontWeight(type);
857 parsedValue = true;
858 }
859
860 return parsedValue && this->parseEOSToken();
861}
Florin Malita385e7442020-10-21 16:55:46 -0400862
Florin Malita056385b2020-10-27 22:57:56 -0400863// https://www.w3.org/TR/SVG11/text.html#TextAnchorProperty
864bool SkSVGAttributeParser::parseTextAnchor(SkSVGTextAnchor* anchor) {
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 Malita385e7442020-10-21 16:55:46 -0400883// https://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute
884bool 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}