blob: 37e2bbe7bc9eb37bc60f8f253f40ae53060b7e4f [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 Denniston30e327e2020-10-29 16:29:22 -0400203bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
204 SkSVGAttribute attr,
205 const char* stringValue) {
206 SkSVGObjectBoundingBoxUnits objectBoundingBoxUnits;
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400207 SkSVGAttributeParser parser(stringValue);
Tyler Denniston30e327e2020-10-29 16:29:22 -0400208 if (!parser.parseObjectBoundingBoxUnits(&objectBoundingBoxUnits)) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400209 return false;
210 }
211
Tyler Denniston30e327e2020-10-29 16:29:22 -0400212 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(objectBoundingBoxUnits));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400213 return true;
214}
215
fmalita5b31f322016-08-12 12:15:33 -0700216bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
217 const char* stringValue) {
218 SkSVGPointsType points;
219 SkSVGAttributeParser parser(stringValue);
220 if (!parser.parsePoints(&points)) {
221 return false;
222 }
223
Florin Malitaf4403e72020-04-10 14:14:04 +0000224 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700225 return true;
226}
227
Florin Malitae932d4b2016-12-01 13:35:11 -0500228bool SetFillRuleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
229 const char* stringValue) {
230 SkSVGFillRule fillRule;
231 SkSVGAttributeParser parser(stringValue);
232 if (!parser.parseFillRule(&fillRule)) {
233 return false;
234 }
235
Florin Malitaf4403e72020-04-10 14:14:04 +0000236 node->setAttribute(attr, SkSVGFillRuleValue(fillRule));
Florin Malitae932d4b2016-12-01 13:35:11 -0500237 return true;
238}
239
Florin Malitaffe6ae42017-10-12 11:33:28 -0400240bool SetVisibilityAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
241 const char* stringValue) {
242 SkSVGVisibility visibility;
243 SkSVGAttributeParser parser(stringValue);
244 if (!parser.parseVisibility(&visibility)) {
245 return false;
246 }
247
Florin Malitaf4403e72020-04-10 14:14:04 +0000248 node->setAttribute(attr, SkSVGVisibilityValue(visibility));
Florin Malitaffe6ae42017-10-12 11:33:28 -0400249 return true;
250}
251
Florin Malitaf543a602017-10-13 14:07:44 -0400252bool SetDashArrayAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
253 const char* stringValue) {
254 SkSVGDashArray dashArray;
255 SkSVGAttributeParser parser(stringValue);
256 if (!parser.parseDashArray(&dashArray)) {
257 return false;
258 }
259
Florin Malitaf4403e72020-04-10 14:14:04 +0000260 node->setAttribute(attr, SkSVGDashArrayValue(dashArray));
Florin Malitaf543a602017-10-13 14:07:44 -0400261 return true;
262}
263
Florin Malita39fe8c82020-10-20 10:43:03 -0400264bool SetFontFamilyAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
265 const char* stringValue) {
266 SkSVGFontFamily family;
267 SkSVGAttributeParser parser(stringValue);
268 if (!parser.parseFontFamily(&family)) {
269 return false;
270 }
271
272 node->setAttribute(attr, SkSVGFontFamilyValue(family));
273 return true;
274}
275
276bool SetFontSizeAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
277 const char* stringValue) {
278 SkSVGFontSize size;
279 SkSVGAttributeParser parser(stringValue);
280 if (!parser.parseFontSize(&size)) {
281 return false;
282 }
283
284 node->setAttribute(attr, SkSVGFontSizeValue(size));
285 return true;
286}
287
288bool SetFontStyleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
289 const char* stringValue) {
290 SkSVGFontStyle style;
291 SkSVGAttributeParser parser(stringValue);
292 if (!parser.parseFontStyle(&style)) {
293 return false;
294 }
295
296 node->setAttribute(attr, SkSVGFontStyleValue(style));
297 return true;
298}
299
300bool SetFontWeightAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
301 const char* stringValue) {
302 SkSVGFontWeight weight;
303 SkSVGAttributeParser parser(stringValue);
304 if (!parser.parseFontWeight(&weight)) {
305 return false;
306 }
307
308 node->setAttribute(attr, SkSVGFontWeightValue(weight));
309 return true;
310}
311
Florin Malita056385b2020-10-27 22:57:56 -0400312bool SetTextAnchorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
313 const char* stringValue) {
314 SkSVGTextAnchor anchor;
315 SkSVGAttributeParser parser(stringValue);
316
317 if (!parser.parseTextAnchor(&anchor)) {
318 return false;
319 }
320
321 node->setAttribute(attr, SkSVGTextAnchorValue(anchor));
322 return true;
323}
324
Florin Malita385e7442020-10-21 16:55:46 -0400325bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
326 const char* stringValue) {
327 SkSVGPreserveAspectRatio par;
328 SkSVGAttributeParser parser(stringValue);
329 if (!parser.parsePreserveAspectRatio(&par)) {
330 return false;
331 }
332
333 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
334 return true;
335}
336
fmalita61f36b32016-08-08 13:58:50 -0700337SkString TrimmedString(const char* first, const char* last) {
338 SkASSERT(first);
339 SkASSERT(last);
340 SkASSERT(first <= last);
341
342 while (first <= last && *first <= ' ') { first++; }
343 while (first <= last && *last <= ' ') { last--; }
344
345 SkASSERT(last - first + 1 >= 0);
346 return SkString(first, SkTo<size_t>(last - first + 1));
347}
348
fmalita58649cc2016-07-29 08:52:03 -0700349// Breaks a "foo: bar; baz: ..." string into key:value pairs.
350class StyleIterator {
351public:
352 StyleIterator(const char* str) : fPos(str) { }
353
354 std::tuple<SkString, SkString> next() {
355 SkString name, value;
356
357 if (fPos) {
358 const char* sep = this->nextSeparator();
359 SkASSERT(*sep == ';' || *sep == '\0');
360
361 const char* valueSep = strchr(fPos, ':');
362 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700363 name = TrimmedString(fPos, valueSep - 1);
364 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700365 }
366
367 fPos = *sep ? sep + 1 : nullptr;
368 }
369
370 return std::make_tuple(name, value);
371 }
372
373private:
374 const char* nextSeparator() const {
375 const char* sep = fPos;
376 while (*sep != ';' && *sep != '\0') {
377 sep++;
378 }
379 return sep;
380 }
381
382 const char* fPos;
383};
384
Tyler Freemanc9911522020-05-08 13:23:10 -0700385bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700386
387bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
388 const char* stringValue) {
389
390 SkString name, value;
391 StyleIterator iter(stringValue);
392 for (;;) {
393 std::tie(name, value) = iter.next();
394 if (name.isEmpty()) {
395 break;
396 }
397 set_string_attribute(node, name.c_str(), value.c_str());
398 }
399
400 return true;
401}
402
fmalita6ceef3d2016-07-26 18:46:34 -0700403template<typename T>
404struct SortedDictionaryEntry {
405 const char* fKey;
406 const T fValue;
407};
408
409struct AttrParseInfo {
410 SkSVGAttribute fAttr;
411 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
412};
413
414SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400415 { "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
416 { "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
417 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
418 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
419 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
420 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
421 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
422 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
423 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
424 { "font-family" , { SkSVGAttribute::kFontFamily , SetFontFamilyAttribute }},
425 { "font-size" , { SkSVGAttribute::kFontSize , SetFontSizeAttribute }},
426 { "font-style" , { SkSVGAttribute::kFontStyle , SetFontStyleAttribute }},
427 { "font-weight" , { SkSVGAttribute::kFontWeight , SetFontWeightAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400428 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400429 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
430 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
431 { "gradientTransform" , { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
Tyler Denniston30e327e2020-10-29 16:29:22 -0400432 { "gradientUnits" , { SkSVGAttribute::kGradientUnits ,
433 SetObjectBoundingBoxUnitsAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400434 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
435 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
436 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
437 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
438 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
439 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
440 SetPreserveAspectRatioAttribute }},
441 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
442 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
443 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
444 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
445 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
446 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
447 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
448 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
449 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
450 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
451 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
452 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
453 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
454 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
455 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
456 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita056385b2020-10-27 22:57:56 -0400457 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetTextAnchorAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400458 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
459 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
460 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
461 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
462 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
463 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
464 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
465 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
466 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
467 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
468 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700469};
470
471SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400472 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700473 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500474 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700475 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
476 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
477 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
478 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
479 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
480 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400481 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700482 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
483 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400484 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700485 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
486 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
487 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000488 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400489 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700490};
491
492struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700493 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700494 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700495 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700496
Florin Malita39fe8c82020-10-20 10:43:03 -0400497 SkSVGNode* fParent;
498 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700499};
500
Tyler Freemanc9911522020-05-08 13:23:10 -0700501bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
fmalita58649cc2016-07-29 08:52:03 -0700502 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
503 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
504 name, sizeof(gAttributeParseInfo[0]));
505 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700506#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700507 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700508#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700509 return false;
fmalita58649cc2016-07-29 08:52:03 -0700510 }
511
512 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
513 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
514 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700515#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700516 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700517#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700518 return false;
fmalita58649cc2016-07-29 08:52:03 -0700519 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700520
521 return true;
fmalita58649cc2016-07-29 08:52:03 -0700522}
523
fmalita6ceef3d2016-07-26 18:46:34 -0700524void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700525 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700526 const char* name, *value;
527 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
528 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700529 // We're handling id attributes out of band for now.
530 if (!strcmp(name, "id")) {
531 mapper->set(SkString(value), svgNode);
532 continue;
533 }
fmalita58649cc2016-07-29 08:52:03 -0700534 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700535 }
536}
537
538sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
539 const SkDOM::Node* xmlNode) {
540 const char* elem = dom.getName(xmlNode);
541 const SkDOM::Type elemType = dom.getType(xmlNode);
542
543 if (elemType == SkDOM::kText_Type) {
544 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400545 // TODO: add type conversion helper to SkSVGNode
546 if (ctx.fParent->tag() == SkSVGTag::kText) {
547 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
548 }
fmalita6ceef3d2016-07-26 18:46:34 -0700549 return nullptr;
550 }
551
552 SkASSERT(elemType == SkDOM::kElement_Type);
553
554 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
555 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
556 elem, sizeof(gTagFactories[0]));
557 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700558#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700559 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700560#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700561 return nullptr;
562 }
563
564 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
565 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700566 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700567
568 ConstructionContext localCtx(ctx, node);
569 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
570 child = dom.getNextSibling(child)) {
571 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
572 if (childNode) {
573 node->appendChild(std::move(childNode));
574 }
575 }
576
577 return node;
578}
579
580} // anonymous namespace
581
fmalitae1baa7c2016-09-14 12:04:30 -0700582SkSVGDOM::SkSVGDOM()
583 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700584}
585
fmalitae1baa7c2016-09-14 12:04:30 -0700586sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
587 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700588
fmalita28d5b722016-09-12 17:06:47 -0700589 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700590 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
591
fmalitae1baa7c2016-09-14 12:04:30 -0700592 // Reset the default container size to match the intrinsic SVG size.
593 dom->setContainerSize(dom->intrinsicSize());
594
fmalita6ceef3d2016-07-26 18:46:34 -0700595 return dom;
596}
597
fmalitae1baa7c2016-09-14 12:04:30 -0700598sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700599 SkDOM xmlDom;
600 if (!xmlDom.build(svgStream)) {
601 return nullptr;
602 }
603
fmalitae1baa7c2016-09-14 12:04:30 -0700604 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700605}
606
607void SkSVGDOM::render(SkCanvas* canvas) const {
608 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400609 SkSVGLengthContext lctx(fContainerSize);
610 SkSVGPresentationContext pctx;
Tyler Denniston53281c72020-10-22 15:54:24 -0400611 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700612 }
613}
614
fmalitae1baa7c2016-09-14 12:04:30 -0700615SkSize SkSVGDOM::intrinsicSize() const {
616 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
617 return SkSize::Make(0, 0);
618 }
619
620 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
621 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
622 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
623}
624
625const SkSize& SkSVGDOM::containerSize() const {
626 return fContainerSize;
627}
628
fmalita6ceef3d2016-07-26 18:46:34 -0700629void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
630 // TODO: inval
631 fContainerSize = containerSize;
632}
fmalitaca39d712016-08-12 13:17:11 -0700633
Tyler Freemanc9911522020-05-08 13:23:10 -0700634sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
635 SkString idStr(id);
636 return this->fIDMapper.find(idStr);
637}
638
fmalitaca39d712016-08-12 13:17:11 -0700639void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
640 fRoot = std::move(root);
641}
Tyler Freemanc9911522020-05-08 13:23:10 -0700642
643// TODO(fuego): move this to SkSVGNode or its own CU.
644bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
645 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
646}