blob: 27e2540773c7b7d2593c2720a98a446e826ff2cf [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 Malita385e7442020-10-21 16:55:46 -0400311bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
312 const char* stringValue) {
313 SkSVGPreserveAspectRatio par;
314 SkSVGAttributeParser parser(stringValue);
315 if (!parser.parsePreserveAspectRatio(&par)) {
316 return false;
317 }
318
319 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
320 return true;
321}
322
fmalita61f36b32016-08-08 13:58:50 -0700323SkString TrimmedString(const char* first, const char* last) {
324 SkASSERT(first);
325 SkASSERT(last);
326 SkASSERT(first <= last);
327
328 while (first <= last && *first <= ' ') { first++; }
329 while (first <= last && *last <= ' ') { last--; }
330
331 SkASSERT(last - first + 1 >= 0);
332 return SkString(first, SkTo<size_t>(last - first + 1));
333}
334
fmalita58649cc2016-07-29 08:52:03 -0700335// Breaks a "foo: bar; baz: ..." string into key:value pairs.
336class StyleIterator {
337public:
338 StyleIterator(const char* str) : fPos(str) { }
339
340 std::tuple<SkString, SkString> next() {
341 SkString name, value;
342
343 if (fPos) {
344 const char* sep = this->nextSeparator();
345 SkASSERT(*sep == ';' || *sep == '\0');
346
347 const char* valueSep = strchr(fPos, ':');
348 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700349 name = TrimmedString(fPos, valueSep - 1);
350 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700351 }
352
353 fPos = *sep ? sep + 1 : nullptr;
354 }
355
356 return std::make_tuple(name, value);
357 }
358
359private:
360 const char* nextSeparator() const {
361 const char* sep = fPos;
362 while (*sep != ';' && *sep != '\0') {
363 sep++;
364 }
365 return sep;
366 }
367
368 const char* fPos;
369};
370
Tyler Freemanc9911522020-05-08 13:23:10 -0700371bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700372
373bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
374 const char* stringValue) {
375
376 SkString name, value;
377 StyleIterator iter(stringValue);
378 for (;;) {
379 std::tie(name, value) = iter.next();
380 if (name.isEmpty()) {
381 break;
382 }
383 set_string_attribute(node, name.c_str(), value.c_str());
384 }
385
386 return true;
387}
388
fmalita6ceef3d2016-07-26 18:46:34 -0700389template<typename T>
390struct SortedDictionaryEntry {
391 const char* fKey;
392 const T fValue;
393};
394
395struct AttrParseInfo {
396 SkSVGAttribute fAttr;
397 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
398};
399
400SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400401 { "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
402 { "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
403 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
404 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
405 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
406 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
407 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
408 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
409 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
410 { "font-family" , { SkSVGAttribute::kFontFamily , SetFontFamilyAttribute }},
411 { "font-size" , { SkSVGAttribute::kFontSize , SetFontSizeAttribute }},
412 { "font-style" , { SkSVGAttribute::kFontStyle , SetFontStyleAttribute }},
413 { "font-weight" , { SkSVGAttribute::kFontWeight , SetFontWeightAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400414 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400415 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
416 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
417 { "gradientTransform" , { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
418 { "gradientUnits" , { SkSVGAttribute::kGradientUnits , SetGradientUnitsAttribute}},
419 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
420 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
421 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
422 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
423 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
424 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
425 SetPreserveAspectRatioAttribute }},
426 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
427 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
428 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
429 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
430 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
431 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
432 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
433 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
434 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
435 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
436 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
437 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
438 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
439 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
440 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
441 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
442 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetStringAttribute }},
443 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
444 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
445 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
446 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
447 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
448 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
449 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
450 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
451 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
452 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
453 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700454};
455
456SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400457 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700458 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500459 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700460 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
461 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
462 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
463 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
464 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
465 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400466 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700467 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
468 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400469 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700470 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
471 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
472 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000473 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400474 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700475};
476
477struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700478 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700479 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700480 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700481
Florin Malita39fe8c82020-10-20 10:43:03 -0400482 SkSVGNode* fParent;
483 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700484};
485
Tyler Freemanc9911522020-05-08 13:23:10 -0700486bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
fmalita58649cc2016-07-29 08:52:03 -0700487 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
488 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
489 name, sizeof(gAttributeParseInfo[0]));
490 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700491#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700492 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700493#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700494 return false;
fmalita58649cc2016-07-29 08:52:03 -0700495 }
496
497 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
498 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
499 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700500#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700501 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700502#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700503 return false;
fmalita58649cc2016-07-29 08:52:03 -0700504 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700505
506 return true;
fmalita58649cc2016-07-29 08:52:03 -0700507}
508
fmalita6ceef3d2016-07-26 18:46:34 -0700509void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700510 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700511 const char* name, *value;
512 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
513 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700514 // We're handling id attributes out of band for now.
515 if (!strcmp(name, "id")) {
516 mapper->set(SkString(value), svgNode);
517 continue;
518 }
fmalita58649cc2016-07-29 08:52:03 -0700519 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700520 }
521}
522
523sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
524 const SkDOM::Node* xmlNode) {
525 const char* elem = dom.getName(xmlNode);
526 const SkDOM::Type elemType = dom.getType(xmlNode);
527
528 if (elemType == SkDOM::kText_Type) {
529 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400530 // TODO: add type conversion helper to SkSVGNode
531 if (ctx.fParent->tag() == SkSVGTag::kText) {
532 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
533 }
fmalita6ceef3d2016-07-26 18:46:34 -0700534 return nullptr;
535 }
536
537 SkASSERT(elemType == SkDOM::kElement_Type);
538
539 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
540 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
541 elem, sizeof(gTagFactories[0]));
542 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700543#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700544 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700545#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700546 return nullptr;
547 }
548
549 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
550 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700551 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700552
553 ConstructionContext localCtx(ctx, node);
554 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
555 child = dom.getNextSibling(child)) {
556 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
557 if (childNode) {
558 node->appendChild(std::move(childNode));
559 }
560 }
561
562 return node;
563}
564
565} // anonymous namespace
566
fmalitae1baa7c2016-09-14 12:04:30 -0700567SkSVGDOM::SkSVGDOM()
568 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700569}
570
fmalitae1baa7c2016-09-14 12:04:30 -0700571sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
572 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700573
fmalita28d5b722016-09-12 17:06:47 -0700574 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700575 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
576
fmalitae1baa7c2016-09-14 12:04:30 -0700577 // Reset the default container size to match the intrinsic SVG size.
578 dom->setContainerSize(dom->intrinsicSize());
579
fmalita6ceef3d2016-07-26 18:46:34 -0700580 return dom;
581}
582
fmalitae1baa7c2016-09-14 12:04:30 -0700583sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700584 SkDOM xmlDom;
585 if (!xmlDom.build(svgStream)) {
586 return nullptr;
587 }
588
fmalitae1baa7c2016-09-14 12:04:30 -0700589 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700590}
591
592void SkSVGDOM::render(SkCanvas* canvas) const {
593 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400594 SkSVGLengthContext lctx(fContainerSize);
595 SkSVGPresentationContext pctx;
596 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx));
fmalita6ceef3d2016-07-26 18:46:34 -0700597 }
598}
599
fmalitae1baa7c2016-09-14 12:04:30 -0700600SkSize SkSVGDOM::intrinsicSize() const {
601 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
602 return SkSize::Make(0, 0);
603 }
604
605 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
606 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
607 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
608}
609
610const SkSize& SkSVGDOM::containerSize() const {
611 return fContainerSize;
612}
613
fmalita6ceef3d2016-07-26 18:46:34 -0700614void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
615 // TODO: inval
616 fContainerSize = containerSize;
617}
fmalitaca39d712016-08-12 13:17:11 -0700618
Tyler Freemanc9911522020-05-08 13:23:10 -0700619sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
620 SkString idStr(id);
621 return this->fIDMapper.find(idStr);
622}
623
fmalitaca39d712016-08-12 13:17:11 -0700624void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
625 fRoot = std::move(root);
626}
Tyler Freemanc9911522020-05-08 13:23:10 -0700627
628// TODO(fuego): move this to SkSVGNode or its own CU.
629bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
630 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
631}