blob: f9327a301a03fa79b7f4db10b378e984b79a37ae [file] [log] [blame]
fmalita6ceef3d2016-07-26 18:46:34 -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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkTo.h"
11#include "include/utils/SkParsePath.h"
Florin Malitab3418102020-10-15 18:10:29 -040012#include "modules/svg/include/SkSVGAttributeParser.h"
13#include "modules/svg/include/SkSVGCircle.h"
14#include "modules/svg/include/SkSVGClipPath.h"
15#include "modules/svg/include/SkSVGDOM.h"
16#include "modules/svg/include/SkSVGDefs.h"
17#include "modules/svg/include/SkSVGEllipse.h"
18#include "modules/svg/include/SkSVGG.h"
19#include "modules/svg/include/SkSVGLine.h"
20#include "modules/svg/include/SkSVGLinearGradient.h"
21#include "modules/svg/include/SkSVGNode.h"
22#include "modules/svg/include/SkSVGPath.h"
23#include "modules/svg/include/SkSVGPattern.h"
24#include "modules/svg/include/SkSVGPoly.h"
25#include "modules/svg/include/SkSVGRadialGradient.h"
26#include "modules/svg/include/SkSVGRect.h"
27#include "modules/svg/include/SkSVGRenderContext.h"
28#include "modules/svg/include/SkSVGSVG.h"
29#include "modules/svg/include/SkSVGStop.h"
30#include "modules/svg/include/SkSVGText.h"
31#include "modules/svg/include/SkSVGTypes.h"
32#include "modules/svg/include/SkSVGUse.h"
33#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040034#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070036
37namespace {
38
fmalita6ceef3d2016-07-26 18:46:34 -070039bool SetPaintAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
40 const char* stringValue) {
fmalita2d961e02016-08-11 09:16:29 -070041 SkSVGPaint paint;
fmalitabffc2562016-08-03 10:21:11 -070042 SkSVGAttributeParser parser(stringValue);
fmalita2d961e02016-08-11 09:16:29 -070043 if (!parser.parsePaint(&paint)) {
fmalita28d5b722016-09-12 17:06:47 -070044 return false;
fmalitabffc2562016-08-03 10:21:11 -070045 }
46
Florin Malitaf4403e72020-04-10 14:14:04 +000047 node->setAttribute(attr, SkSVGPaintValue(paint));
fmalita6ceef3d2016-07-26 18:46:34 -070048 return true;
49}
50
fmalita28d5b722016-09-12 17:06:47 -070051bool SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
52 const char* stringValue) {
53 SkSVGColorType color;
54 SkSVGAttributeParser parser(stringValue);
55 if (!parser.parseColor(&color)) {
56 return false;
57 }
58
Florin Malitaf4403e72020-04-10 14:14:04 +000059 node->setAttribute(attr, SkSVGColorValue(color));
fmalita28d5b722016-09-12 17:06:47 -070060 return true;
61}
62
63bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
64 const char* stringValue) {
65 SkSVGStringType iri;
66 SkSVGAttributeParser parser(stringValue);
67 if (!parser.parseIRI(&iri)) {
68 return false;
69 }
70
Florin Malitaf4403e72020-04-10 14:14:04 +000071 node->setAttribute(attr, SkSVGStringValue(iri));
fmalita28d5b722016-09-12 17:06:47 -070072 return true;
73}
74
Florin Malitace8840e2016-12-08 09:26:47 -050075bool SetClipPathAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
76 const char* stringValue) {
77 SkSVGClip clip;
78 SkSVGAttributeParser parser(stringValue);
79 if (!parser.parseClipPath(&clip)) {
80 return false;
81 }
82
Florin Malitaf4403e72020-04-10 14:14:04 +000083 node->setAttribute(attr, SkSVGClipValue(clip));
Florin Malitace8840e2016-12-08 09:26:47 -050084 return true;
85}
86
87
fmalita6ceef3d2016-07-26 18:46:34 -070088bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
89 const char* stringValue) {
90 SkPath path;
91 if (!SkParsePath::FromSVGString(stringValue, &path)) {
92 return false;
93 }
94
Florin Malitaf4403e72020-04-10 14:14:04 +000095 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070096 return true;
97}
98
Xavier Phane29cdaf2020-03-26 16:15:14 +000099bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
100 const char* stringValue) {
101 SkString str(stringValue, strlen(stringValue));
102 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +0000103 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +0000104 return true;
105}
106
fmalita6ceef3d2016-07-26 18:46:34 -0700107bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
108 const char* stringValue) {
fmalitac97796b2016-08-08 12:58:57 -0700109 SkSVGTransformType transform;
110 SkSVGAttributeParser parser(stringValue);
111 if (!parser.parseTransform(&transform)) {
112 return false;
113 }
114
Florin Malitaf4403e72020-04-10 14:14:04 +0000115 node->setAttribute(attr, SkSVGTransformValue(transform));
fmalita6ceef3d2016-07-26 18:46:34 -0700116 return true;
117}
118
fmalitabffc2562016-08-03 10:21:11 -0700119bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
120 const char* stringValue) {
121 SkSVGLength length;
122 SkSVGAttributeParser parser(stringValue);
123 if (!parser.parseLength(&length)) {
124 return false;
125 }
126
Florin Malitaf4403e72020-04-10 14:14:04 +0000127 node->setAttribute(attr, SkSVGLengthValue(length));
fmalitabffc2562016-08-03 10:21:11 -0700128 return true;
129}
130
fmalita2d961e02016-08-11 09:16:29 -0700131bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
132 const char* stringValue) {
133 SkSVGNumberType number;
134 SkSVGAttributeParser parser(stringValue);
135 if (!parser.parseNumber(&number)) {
136 return false;
137 }
138
Florin Malitaf4403e72020-04-10 14:14:04 +0000139 node->setAttribute(attr, SkSVGNumberValue(number));
fmalita2d961e02016-08-11 09:16:29 -0700140 return true;
141}
142
fmalita397a5172016-08-08 11:38:55 -0700143bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
144 const char* stringValue) {
145 SkSVGViewBoxType viewBox;
146 SkSVGAttributeParser parser(stringValue);
147 if (!parser.parseViewBox(&viewBox)) {
148 return false;
149 }
150
Florin Malitaf4403e72020-04-10 14:14:04 +0000151 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700152 return true;
153}
154
fmalita2d961e02016-08-11 09:16:29 -0700155bool SetLineCapAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
156 const char* stringValue) {
157 SkSVGLineCap lineCap;
158 SkSVGAttributeParser parser(stringValue);
159 if (!parser.parseLineCap(&lineCap)) {
160 return false;
161 }
162
Florin Malitaf4403e72020-04-10 14:14:04 +0000163 node->setAttribute(attr, SkSVGLineCapValue(lineCap));
fmalita2d961e02016-08-11 09:16:29 -0700164 return true;
165}
166
167bool SetLineJoinAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
168 const char* stringValue) {
169 SkSVGLineJoin lineJoin;
170 SkSVGAttributeParser parser(stringValue);
171 if (!parser.parseLineJoin(&lineJoin)) {
172 return false;
173 }
174
Florin Malitaf4403e72020-04-10 14:14:04 +0000175 node->setAttribute(attr, SkSVGLineJoinValue(lineJoin));
fmalita2d961e02016-08-11 09:16:29 -0700176 return true;
177}
178
fmalitacecd6172016-09-13 12:56:11 -0700179bool SetSpreadMethodAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
180 const char* stringValue) {
181 SkSVGSpreadMethod spread;
182 SkSVGAttributeParser parser(stringValue);
183 if (!parser.parseSpreadMethod(&spread)) {
184 return false;
185 }
186
Florin Malitaf4403e72020-04-10 14:14:04 +0000187 node->setAttribute(attr, SkSVGSpreadMethodValue(spread));
fmalitacecd6172016-09-13 12:56:11 -0700188 return true;
189}
190
Tyler Denniston308c0722020-04-14 10:53:41 -0400191bool SetStopColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
192 const char* stringValue) {
193 SkSVGStopColor stopColor;
194 SkSVGAttributeParser parser(stringValue);
195 if (!parser.parseStopColor(&stopColor)) {
196 return false;
197 }
198
199 node->setAttribute(attr, SkSVGStopColorValue(stopColor));
200 return true;
201}
202
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400203bool SetGradientUnitsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
204 const char* stringValue) {
205 SkSVGGradientUnits gradientUnits;
206 SkSVGAttributeParser parser(stringValue);
207 if (!parser.parseGradientUnits(&gradientUnits)) {
208 return false;
209 }
210
211 node->setAttribute(attr, SkSVGGradientUnitsValue(gradientUnits));
212 return true;
213}
214
fmalita5b31f322016-08-12 12:15:33 -0700215bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
216 const char* stringValue) {
217 SkSVGPointsType points;
218 SkSVGAttributeParser parser(stringValue);
219 if (!parser.parsePoints(&points)) {
220 return false;
221 }
222
Florin Malitaf4403e72020-04-10 14:14:04 +0000223 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700224 return true;
225}
226
Florin Malitae932d4b2016-12-01 13:35:11 -0500227bool SetFillRuleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
228 const char* stringValue) {
229 SkSVGFillRule fillRule;
230 SkSVGAttributeParser parser(stringValue);
231 if (!parser.parseFillRule(&fillRule)) {
232 return false;
233 }
234
Florin Malitaf4403e72020-04-10 14:14:04 +0000235 node->setAttribute(attr, SkSVGFillRuleValue(fillRule));
Florin Malitae932d4b2016-12-01 13:35:11 -0500236 return true;
237}
238
Florin Malitaffe6ae42017-10-12 11:33:28 -0400239bool SetVisibilityAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
240 const char* stringValue) {
241 SkSVGVisibility visibility;
242 SkSVGAttributeParser parser(stringValue);
243 if (!parser.parseVisibility(&visibility)) {
244 return false;
245 }
246
Florin Malitaf4403e72020-04-10 14:14:04 +0000247 node->setAttribute(attr, SkSVGVisibilityValue(visibility));
Florin Malitaffe6ae42017-10-12 11:33:28 -0400248 return true;
249}
250
Florin Malitaf543a602017-10-13 14:07:44 -0400251bool SetDashArrayAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
252 const char* stringValue) {
253 SkSVGDashArray dashArray;
254 SkSVGAttributeParser parser(stringValue);
255 if (!parser.parseDashArray(&dashArray)) {
256 return false;
257 }
258
Florin Malitaf4403e72020-04-10 14:14:04 +0000259 node->setAttribute(attr, SkSVGDashArrayValue(dashArray));
Florin Malitaf543a602017-10-13 14:07:44 -0400260 return true;
261}
262
Florin Malita39fe8c82020-10-20 10:43:03 -0400263bool SetFontFamilyAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
264 const char* stringValue) {
265 SkSVGFontFamily family;
266 SkSVGAttributeParser parser(stringValue);
267 if (!parser.parseFontFamily(&family)) {
268 return false;
269 }
270
271 node->setAttribute(attr, SkSVGFontFamilyValue(family));
272 return true;
273}
274
275bool SetFontSizeAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
276 const char* stringValue) {
277 SkSVGFontSize size;
278 SkSVGAttributeParser parser(stringValue);
279 if (!parser.parseFontSize(&size)) {
280 return false;
281 }
282
283 node->setAttribute(attr, SkSVGFontSizeValue(size));
284 return true;
285}
286
287bool SetFontStyleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
288 const char* stringValue) {
289 SkSVGFontStyle style;
290 SkSVGAttributeParser parser(stringValue);
291 if (!parser.parseFontStyle(&style)) {
292 return false;
293 }
294
295 node->setAttribute(attr, SkSVGFontStyleValue(style));
296 return true;
297}
298
299bool SetFontWeightAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
300 const char* stringValue) {
301 SkSVGFontWeight weight;
302 SkSVGAttributeParser parser(stringValue);
303 if (!parser.parseFontWeight(&weight)) {
304 return false;
305 }
306
307 node->setAttribute(attr, SkSVGFontWeightValue(weight));
308 return true;
309}
310
Florin Malita056385b2020-10-27 22:57:56 -0400311bool SetTextAnchorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
312 const char* stringValue) {
313 SkSVGTextAnchor anchor;
314 SkSVGAttributeParser parser(stringValue);
315
316 if (!parser.parseTextAnchor(&anchor)) {
317 return false;
318 }
319
320 node->setAttribute(attr, SkSVGTextAnchorValue(anchor));
321 return true;
322}
323
Florin Malita385e7442020-10-21 16:55:46 -0400324bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
325 const char* stringValue) {
326 SkSVGPreserveAspectRatio par;
327 SkSVGAttributeParser parser(stringValue);
328 if (!parser.parsePreserveAspectRatio(&par)) {
329 return false;
330 }
331
332 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
333 return true;
334}
335
fmalita61f36b32016-08-08 13:58:50 -0700336SkString TrimmedString(const char* first, const char* last) {
337 SkASSERT(first);
338 SkASSERT(last);
339 SkASSERT(first <= last);
340
341 while (first <= last && *first <= ' ') { first++; }
342 while (first <= last && *last <= ' ') { last--; }
343
344 SkASSERT(last - first + 1 >= 0);
345 return SkString(first, SkTo<size_t>(last - first + 1));
346}
347
fmalita58649cc2016-07-29 08:52:03 -0700348// Breaks a "foo: bar; baz: ..." string into key:value pairs.
349class StyleIterator {
350public:
351 StyleIterator(const char* str) : fPos(str) { }
352
353 std::tuple<SkString, SkString> next() {
354 SkString name, value;
355
356 if (fPos) {
357 const char* sep = this->nextSeparator();
358 SkASSERT(*sep == ';' || *sep == '\0');
359
360 const char* valueSep = strchr(fPos, ':');
361 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700362 name = TrimmedString(fPos, valueSep - 1);
363 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700364 }
365
366 fPos = *sep ? sep + 1 : nullptr;
367 }
368
369 return std::make_tuple(name, value);
370 }
371
372private:
373 const char* nextSeparator() const {
374 const char* sep = fPos;
375 while (*sep != ';' && *sep != '\0') {
376 sep++;
377 }
378 return sep;
379 }
380
381 const char* fPos;
382};
383
Tyler Freemanc9911522020-05-08 13:23:10 -0700384bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700385
386bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
387 const char* stringValue) {
388
389 SkString name, value;
390 StyleIterator iter(stringValue);
391 for (;;) {
392 std::tie(name, value) = iter.next();
393 if (name.isEmpty()) {
394 break;
395 }
396 set_string_attribute(node, name.c_str(), value.c_str());
397 }
398
399 return true;
400}
401
fmalita6ceef3d2016-07-26 18:46:34 -0700402template<typename T>
403struct SortedDictionaryEntry {
404 const char* fKey;
405 const T fValue;
406};
407
408struct AttrParseInfo {
409 SkSVGAttribute fAttr;
410 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
411};
412
413SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400414 { "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
415 { "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
416 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
417 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
418 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
419 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
420 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
421 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
422 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
423 { "font-family" , { SkSVGAttribute::kFontFamily , SetFontFamilyAttribute }},
424 { "font-size" , { SkSVGAttribute::kFontSize , SetFontSizeAttribute }},
425 { "font-style" , { SkSVGAttribute::kFontStyle , SetFontStyleAttribute }},
426 { "font-weight" , { SkSVGAttribute::kFontWeight , SetFontWeightAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400427 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400428 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
429 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
430 { "gradientTransform" , { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
431 { "gradientUnits" , { SkSVGAttribute::kGradientUnits , SetGradientUnitsAttribute}},
432 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
433 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
434 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
435 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
436 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
437 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
438 SetPreserveAspectRatioAttribute }},
439 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
440 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
441 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
442 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
443 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
444 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
445 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
446 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
447 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
448 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
449 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
450 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
451 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
452 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
453 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
454 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita056385b2020-10-27 22:57:56 -0400455 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetTextAnchorAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400456 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
457 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
458 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
459 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
460 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
461 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
462 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
463 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
464 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
465 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
466 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700467};
468
469SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400470 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700471 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500472 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700473 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
474 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
475 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
476 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
477 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
478 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400479 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700480 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
481 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400482 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700483 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
484 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
485 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000486 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400487 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700488};
489
490struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700491 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700492 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700493 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700494
Florin Malita39fe8c82020-10-20 10:43:03 -0400495 SkSVGNode* fParent;
496 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700497};
498
Tyler Freemanc9911522020-05-08 13:23:10 -0700499bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
fmalita58649cc2016-07-29 08:52:03 -0700500 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
501 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
502 name, sizeof(gAttributeParseInfo[0]));
503 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700504#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700505 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700506#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700507 return false;
fmalita58649cc2016-07-29 08:52:03 -0700508 }
509
510 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
511 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
512 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700513#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700514 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700515#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700516 return false;
fmalita58649cc2016-07-29 08:52:03 -0700517 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700518
519 return true;
fmalita58649cc2016-07-29 08:52:03 -0700520}
521
fmalita6ceef3d2016-07-26 18:46:34 -0700522void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700523 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700524 const char* name, *value;
525 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
526 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700527 // We're handling id attributes out of band for now.
528 if (!strcmp(name, "id")) {
529 mapper->set(SkString(value), svgNode);
530 continue;
531 }
fmalita58649cc2016-07-29 08:52:03 -0700532 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700533 }
534}
535
536sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
537 const SkDOM::Node* xmlNode) {
538 const char* elem = dom.getName(xmlNode);
539 const SkDOM::Type elemType = dom.getType(xmlNode);
540
541 if (elemType == SkDOM::kText_Type) {
542 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400543 // TODO: add type conversion helper to SkSVGNode
544 if (ctx.fParent->tag() == SkSVGTag::kText) {
545 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
546 }
fmalita6ceef3d2016-07-26 18:46:34 -0700547 return nullptr;
548 }
549
550 SkASSERT(elemType == SkDOM::kElement_Type);
551
552 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
553 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
554 elem, sizeof(gTagFactories[0]));
555 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700556#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700557 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700558#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700559 return nullptr;
560 }
561
562 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
563 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700564 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700565
566 ConstructionContext localCtx(ctx, node);
567 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
568 child = dom.getNextSibling(child)) {
569 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
570 if (childNode) {
571 node->appendChild(std::move(childNode));
572 }
573 }
574
575 return node;
576}
577
578} // anonymous namespace
579
fmalitae1baa7c2016-09-14 12:04:30 -0700580SkSVGDOM::SkSVGDOM()
581 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700582}
583
fmalitae1baa7c2016-09-14 12:04:30 -0700584sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
585 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700586
fmalita28d5b722016-09-12 17:06:47 -0700587 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700588 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
589
fmalitae1baa7c2016-09-14 12:04:30 -0700590 // Reset the default container size to match the intrinsic SVG size.
591 dom->setContainerSize(dom->intrinsicSize());
592
fmalita6ceef3d2016-07-26 18:46:34 -0700593 return dom;
594}
595
fmalitae1baa7c2016-09-14 12:04:30 -0700596sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700597 SkDOM xmlDom;
598 if (!xmlDom.build(svgStream)) {
599 return nullptr;
600 }
601
fmalitae1baa7c2016-09-14 12:04:30 -0700602 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700603}
604
605void SkSVGDOM::render(SkCanvas* canvas) const {
606 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400607 SkSVGLengthContext lctx(fContainerSize);
608 SkSVGPresentationContext pctx;
Tyler Denniston53281c72020-10-22 15:54:24 -0400609 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700610 }
611}
612
fmalitae1baa7c2016-09-14 12:04:30 -0700613SkSize SkSVGDOM::intrinsicSize() const {
614 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
615 return SkSize::Make(0, 0);
616 }
617
618 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
619 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
620 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
621}
622
623const SkSize& SkSVGDOM::containerSize() const {
624 return fContainerSize;
625}
626
fmalita6ceef3d2016-07-26 18:46:34 -0700627void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
628 // TODO: inval
629 fContainerSize = containerSize;
630}
fmalitaca39d712016-08-12 13:17:11 -0700631
Tyler Freemanc9911522020-05-08 13:23:10 -0700632sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
633 SkString idStr(id);
634 return this->fIDMapper.find(idStr);
635}
636
fmalitaca39d712016-08-12 13:17:11 -0700637void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
638 fRoot = std::move(root);
639}
Tyler Freemanc9911522020-05-08 13:23:10 -0700640
641// TODO(fuego): move this to SkSVGNode or its own CU.
642bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
643 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
644}