blob: 11bf2efc7bb6b9287cd187f5d099818ee0280022 [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"
Tyler Dennistondada9602020-11-03 10:04:25 -050018#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040019#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040020#include "modules/svg/include/SkSVGG.h"
21#include "modules/svg/include/SkSVGLine.h"
22#include "modules/svg/include/SkSVGLinearGradient.h"
23#include "modules/svg/include/SkSVGNode.h"
24#include "modules/svg/include/SkSVGPath.h"
25#include "modules/svg/include/SkSVGPattern.h"
26#include "modules/svg/include/SkSVGPoly.h"
27#include "modules/svg/include/SkSVGRadialGradient.h"
28#include "modules/svg/include/SkSVGRect.h"
29#include "modules/svg/include/SkSVGRenderContext.h"
30#include "modules/svg/include/SkSVGSVG.h"
31#include "modules/svg/include/SkSVGStop.h"
32#include "modules/svg/include/SkSVGText.h"
33#include "modules/svg/include/SkSVGTypes.h"
34#include "modules/svg/include/SkSVGUse.h"
35#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040036#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070038
39namespace {
40
fmalita6ceef3d2016-07-26 18:46:34 -070041bool SetPaintAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
42 const char* stringValue) {
fmalita2d961e02016-08-11 09:16:29 -070043 SkSVGPaint paint;
fmalitabffc2562016-08-03 10:21:11 -070044 SkSVGAttributeParser parser(stringValue);
fmalita2d961e02016-08-11 09:16:29 -070045 if (!parser.parsePaint(&paint)) {
fmalita28d5b722016-09-12 17:06:47 -070046 return false;
fmalitabffc2562016-08-03 10:21:11 -070047 }
48
Florin Malitaf4403e72020-04-10 14:14:04 +000049 node->setAttribute(attr, SkSVGPaintValue(paint));
fmalita6ceef3d2016-07-26 18:46:34 -070050 return true;
51}
52
fmalita28d5b722016-09-12 17:06:47 -070053bool SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
54 const char* stringValue) {
55 SkSVGColorType color;
56 SkSVGAttributeParser parser(stringValue);
57 if (!parser.parseColor(&color)) {
58 return false;
59 }
60
Florin Malitaf4403e72020-04-10 14:14:04 +000061 node->setAttribute(attr, SkSVGColorValue(color));
fmalita28d5b722016-09-12 17:06:47 -070062 return true;
63}
64
65bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
66 const char* stringValue) {
67 SkSVGStringType iri;
68 SkSVGAttributeParser parser(stringValue);
69 if (!parser.parseIRI(&iri)) {
70 return false;
71 }
72
Florin Malitaf4403e72020-04-10 14:14:04 +000073 node->setAttribute(attr, SkSVGStringValue(iri));
fmalita28d5b722016-09-12 17:06:47 -070074 return true;
75}
76
Florin Malitace8840e2016-12-08 09:26:47 -050077bool SetClipPathAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
78 const char* stringValue) {
79 SkSVGClip clip;
80 SkSVGAttributeParser parser(stringValue);
81 if (!parser.parseClipPath(&clip)) {
82 return false;
83 }
84
Florin Malitaf4403e72020-04-10 14:14:04 +000085 node->setAttribute(attr, SkSVGClipValue(clip));
Florin Malitace8840e2016-12-08 09:26:47 -050086 return true;
87}
88
89
fmalita6ceef3d2016-07-26 18:46:34 -070090bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
91 const char* stringValue) {
92 SkPath path;
93 if (!SkParsePath::FromSVGString(stringValue, &path)) {
94 return false;
95 }
96
Florin Malitaf4403e72020-04-10 14:14:04 +000097 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070098 return true;
99}
100
Xavier Phane29cdaf2020-03-26 16:15:14 +0000101bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
102 const char* stringValue) {
103 SkString str(stringValue, strlen(stringValue));
104 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +0000105 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +0000106 return true;
107}
108
fmalita6ceef3d2016-07-26 18:46:34 -0700109bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
110 const char* stringValue) {
fmalitac97796b2016-08-08 12:58:57 -0700111 SkSVGTransformType transform;
112 SkSVGAttributeParser parser(stringValue);
113 if (!parser.parseTransform(&transform)) {
114 return false;
115 }
116
Florin Malitaf4403e72020-04-10 14:14:04 +0000117 node->setAttribute(attr, SkSVGTransformValue(transform));
fmalita6ceef3d2016-07-26 18:46:34 -0700118 return true;
119}
120
fmalitabffc2562016-08-03 10:21:11 -0700121bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
122 const char* stringValue) {
123 SkSVGLength length;
124 SkSVGAttributeParser parser(stringValue);
125 if (!parser.parseLength(&length)) {
126 return false;
127 }
128
Florin Malitaf4403e72020-04-10 14:14:04 +0000129 node->setAttribute(attr, SkSVGLengthValue(length));
fmalitabffc2562016-08-03 10:21:11 -0700130 return true;
131}
132
fmalita2d961e02016-08-11 09:16:29 -0700133bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
134 const char* stringValue) {
135 SkSVGNumberType number;
136 SkSVGAttributeParser parser(stringValue);
137 if (!parser.parseNumber(&number)) {
138 return false;
139 }
140
Florin Malitaf4403e72020-04-10 14:14:04 +0000141 node->setAttribute(attr, SkSVGNumberValue(number));
fmalita2d961e02016-08-11 09:16:29 -0700142 return true;
143}
144
fmalita397a5172016-08-08 11:38:55 -0700145bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
146 const char* stringValue) {
147 SkSVGViewBoxType viewBox;
148 SkSVGAttributeParser parser(stringValue);
149 if (!parser.parseViewBox(&viewBox)) {
150 return false;
151 }
152
Florin Malitaf4403e72020-04-10 14:14:04 +0000153 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700154 return true;
155}
156
fmalita2d961e02016-08-11 09:16:29 -0700157bool SetLineCapAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
158 const char* stringValue) {
159 SkSVGLineCap lineCap;
160 SkSVGAttributeParser parser(stringValue);
161 if (!parser.parseLineCap(&lineCap)) {
162 return false;
163 }
164
Florin Malitaf4403e72020-04-10 14:14:04 +0000165 node->setAttribute(attr, SkSVGLineCapValue(lineCap));
fmalita2d961e02016-08-11 09:16:29 -0700166 return true;
167}
168
169bool SetLineJoinAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
170 const char* stringValue) {
171 SkSVGLineJoin lineJoin;
172 SkSVGAttributeParser parser(stringValue);
173 if (!parser.parseLineJoin(&lineJoin)) {
174 return false;
175 }
176
Florin Malitaf4403e72020-04-10 14:14:04 +0000177 node->setAttribute(attr, SkSVGLineJoinValue(lineJoin));
fmalita2d961e02016-08-11 09:16:29 -0700178 return true;
179}
180
fmalitacecd6172016-09-13 12:56:11 -0700181bool SetSpreadMethodAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
182 const char* stringValue) {
183 SkSVGSpreadMethod spread;
184 SkSVGAttributeParser parser(stringValue);
185 if (!parser.parseSpreadMethod(&spread)) {
186 return false;
187 }
188
Florin Malitaf4403e72020-04-10 14:14:04 +0000189 node->setAttribute(attr, SkSVGSpreadMethodValue(spread));
fmalitacecd6172016-09-13 12:56:11 -0700190 return true;
191}
192
Tyler Denniston308c0722020-04-14 10:53:41 -0400193bool SetStopColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
194 const char* stringValue) {
195 SkSVGStopColor stopColor;
196 SkSVGAttributeParser parser(stringValue);
197 if (!parser.parseStopColor(&stopColor)) {
198 return false;
199 }
200
201 node->setAttribute(attr, SkSVGStopColorValue(stopColor));
202 return true;
203}
204
Tyler Denniston30e327e2020-10-29 16:29:22 -0400205bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
206 SkSVGAttribute attr,
207 const char* stringValue) {
208 SkSVGObjectBoundingBoxUnits objectBoundingBoxUnits;
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400209 SkSVGAttributeParser parser(stringValue);
Tyler Denniston30e327e2020-10-29 16:29:22 -0400210 if (!parser.parseObjectBoundingBoxUnits(&objectBoundingBoxUnits)) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400211 return false;
212 }
213
Tyler Denniston30e327e2020-10-29 16:29:22 -0400214 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(objectBoundingBoxUnits));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400215 return true;
216}
217
fmalita5b31f322016-08-12 12:15:33 -0700218bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
219 const char* stringValue) {
220 SkSVGPointsType points;
221 SkSVGAttributeParser parser(stringValue);
222 if (!parser.parsePoints(&points)) {
223 return false;
224 }
225
Florin Malitaf4403e72020-04-10 14:14:04 +0000226 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700227 return true;
228}
229
Florin Malitae932d4b2016-12-01 13:35:11 -0500230bool SetFillRuleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
231 const char* stringValue) {
232 SkSVGFillRule fillRule;
233 SkSVGAttributeParser parser(stringValue);
234 if (!parser.parseFillRule(&fillRule)) {
235 return false;
236 }
237
Florin Malitaf4403e72020-04-10 14:14:04 +0000238 node->setAttribute(attr, SkSVGFillRuleValue(fillRule));
Florin Malitae932d4b2016-12-01 13:35:11 -0500239 return true;
240}
241
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400242bool SetFilterAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
243 const char* stringValue) {
244 SkSVGFilterType filter;
245 SkSVGAttributeParser parser(stringValue);
246 if (!parser.parseFilter(&filter)) {
247 return false;
248 }
249
250 node->setAttribute(attr, SkSVGFilterValue(filter));
251 return true;
252}
253
Florin Malitaffe6ae42017-10-12 11:33:28 -0400254bool SetVisibilityAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
255 const char* stringValue) {
256 SkSVGVisibility visibility;
257 SkSVGAttributeParser parser(stringValue);
258 if (!parser.parseVisibility(&visibility)) {
259 return false;
260 }
261
Florin Malitaf4403e72020-04-10 14:14:04 +0000262 node->setAttribute(attr, SkSVGVisibilityValue(visibility));
Florin Malitaffe6ae42017-10-12 11:33:28 -0400263 return true;
264}
265
Florin Malitaf543a602017-10-13 14:07:44 -0400266bool SetDashArrayAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
267 const char* stringValue) {
268 SkSVGDashArray dashArray;
269 SkSVGAttributeParser parser(stringValue);
270 if (!parser.parseDashArray(&dashArray)) {
271 return false;
272 }
273
Florin Malitaf4403e72020-04-10 14:14:04 +0000274 node->setAttribute(attr, SkSVGDashArrayValue(dashArray));
Florin Malitaf543a602017-10-13 14:07:44 -0400275 return true;
276}
277
Florin Malita39fe8c82020-10-20 10:43:03 -0400278bool SetFontFamilyAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
279 const char* stringValue) {
280 SkSVGFontFamily family;
281 SkSVGAttributeParser parser(stringValue);
282 if (!parser.parseFontFamily(&family)) {
283 return false;
284 }
285
286 node->setAttribute(attr, SkSVGFontFamilyValue(family));
287 return true;
288}
289
290bool SetFontSizeAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
291 const char* stringValue) {
292 SkSVGFontSize size;
293 SkSVGAttributeParser parser(stringValue);
294 if (!parser.parseFontSize(&size)) {
295 return false;
296 }
297
298 node->setAttribute(attr, SkSVGFontSizeValue(size));
299 return true;
300}
301
302bool SetFontStyleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
303 const char* stringValue) {
304 SkSVGFontStyle style;
305 SkSVGAttributeParser parser(stringValue);
306 if (!parser.parseFontStyle(&style)) {
307 return false;
308 }
309
310 node->setAttribute(attr, SkSVGFontStyleValue(style));
311 return true;
312}
313
314bool SetFontWeightAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
315 const char* stringValue) {
316 SkSVGFontWeight weight;
317 SkSVGAttributeParser parser(stringValue);
318 if (!parser.parseFontWeight(&weight)) {
319 return false;
320 }
321
322 node->setAttribute(attr, SkSVGFontWeightValue(weight));
323 return true;
324}
325
Florin Malita056385b2020-10-27 22:57:56 -0400326bool SetTextAnchorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
327 const char* stringValue) {
328 SkSVGTextAnchor anchor;
329 SkSVGAttributeParser parser(stringValue);
330
331 if (!parser.parseTextAnchor(&anchor)) {
332 return false;
333 }
334
335 node->setAttribute(attr, SkSVGTextAnchorValue(anchor));
336 return true;
337}
338
Florin Malita385e7442020-10-21 16:55:46 -0400339bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
340 const char* stringValue) {
341 SkSVGPreserveAspectRatio par;
342 SkSVGAttributeParser parser(stringValue);
343 if (!parser.parsePreserveAspectRatio(&par)) {
344 return false;
345 }
346
347 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
348 return true;
349}
350
fmalita61f36b32016-08-08 13:58:50 -0700351SkString TrimmedString(const char* first, const char* last) {
352 SkASSERT(first);
353 SkASSERT(last);
354 SkASSERT(first <= last);
355
356 while (first <= last && *first <= ' ') { first++; }
357 while (first <= last && *last <= ' ') { last--; }
358
359 SkASSERT(last - first + 1 >= 0);
360 return SkString(first, SkTo<size_t>(last - first + 1));
361}
362
fmalita58649cc2016-07-29 08:52:03 -0700363// Breaks a "foo: bar; baz: ..." string into key:value pairs.
364class StyleIterator {
365public:
366 StyleIterator(const char* str) : fPos(str) { }
367
368 std::tuple<SkString, SkString> next() {
369 SkString name, value;
370
371 if (fPos) {
372 const char* sep = this->nextSeparator();
373 SkASSERT(*sep == ';' || *sep == '\0');
374
375 const char* valueSep = strchr(fPos, ':');
376 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700377 name = TrimmedString(fPos, valueSep - 1);
378 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700379 }
380
381 fPos = *sep ? sep + 1 : nullptr;
382 }
383
384 return std::make_tuple(name, value);
385 }
386
387private:
388 const char* nextSeparator() const {
389 const char* sep = fPos;
390 while (*sep != ';' && *sep != '\0') {
391 sep++;
392 }
393 return sep;
394 }
395
396 const char* fPos;
397};
398
Tyler Freemanc9911522020-05-08 13:23:10 -0700399bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700400
401bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
402 const char* stringValue) {
403
404 SkString name, value;
405 StyleIterator iter(stringValue);
406 for (;;) {
407 std::tie(name, value) = iter.next();
408 if (name.isEmpty()) {
409 break;
410 }
411 set_string_attribute(node, name.c_str(), value.c_str());
412 }
413
414 return true;
415}
416
fmalita6ceef3d2016-07-26 18:46:34 -0700417template<typename T>
418struct SortedDictionaryEntry {
419 const char* fKey;
420 const T fValue;
421};
422
423struct AttrParseInfo {
424 SkSVGAttribute fAttr;
425 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
426};
427
428SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400429 { "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
430 { "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
431 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
432 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
433 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
434 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
435 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
436 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
437 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400438 { "filter" , { SkSVGAttribute::kFilter , SetFilterAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400439 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
440 SetObjectBoundingBoxUnitsAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400441 { "font-family" , { SkSVGAttribute::kFontFamily , SetFontFamilyAttribute }},
442 { "font-size" , { SkSVGAttribute::kFontSize , SetFontSizeAttribute }},
443 { "font-style" , { SkSVGAttribute::kFontStyle , SetFontStyleAttribute }},
444 { "font-weight" , { SkSVGAttribute::kFontWeight , SetFontWeightAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400445 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400446 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
447 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
448 { "gradientTransform" , { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
Tyler Denniston30e327e2020-10-29 16:29:22 -0400449 { "gradientUnits" , { SkSVGAttribute::kGradientUnits ,
450 SetObjectBoundingBoxUnitsAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400451 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
452 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
453 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
454 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
455 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
456 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
457 SetPreserveAspectRatioAttribute }},
458 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
459 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
460 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
461 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
462 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
463 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
464 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
465 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
466 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
467 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
468 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
469 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
470 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
471 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
472 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
473 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita056385b2020-10-27 22:57:56 -0400474 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetTextAnchorAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400475 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
476 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
477 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
478 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
479 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
480 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
481 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
482 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
483 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
484 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
485 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700486};
487
488SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400489 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700490 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500491 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700492 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
493 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
Tyler Dennistondada9602020-11-03 10:04:25 -0500494 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400495 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700496 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
497 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
498 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
499 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400500 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700501 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
502 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400503 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700504 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
505 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
506 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000507 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400508 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700509};
510
511struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700512 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700513 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700514 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700515
Florin Malita39fe8c82020-10-20 10:43:03 -0400516 SkSVGNode* fParent;
517 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700518};
519
Tyler Freemanc9911522020-05-08 13:23:10 -0700520bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500521 if (node->parseAndSetAttribute(name, value)) {
522 // Handled by new code path
523 return true;
524 }
525
fmalita58649cc2016-07-29 08:52:03 -0700526 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
527 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
528 name, sizeof(gAttributeParseInfo[0]));
529 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700530#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700531 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700532#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700533 return false;
fmalita58649cc2016-07-29 08:52:03 -0700534 }
535
536 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
537 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
538 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700539#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700540 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700541#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700542 return false;
fmalita58649cc2016-07-29 08:52:03 -0700543 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700544
545 return true;
fmalita58649cc2016-07-29 08:52:03 -0700546}
547
fmalita6ceef3d2016-07-26 18:46:34 -0700548void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700549 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700550 const char* name, *value;
551 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
552 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700553 // We're handling id attributes out of band for now.
554 if (!strcmp(name, "id")) {
555 mapper->set(SkString(value), svgNode);
556 continue;
557 }
fmalita58649cc2016-07-29 08:52:03 -0700558 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700559 }
560}
561
562sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
563 const SkDOM::Node* xmlNode) {
564 const char* elem = dom.getName(xmlNode);
565 const SkDOM::Type elemType = dom.getType(xmlNode);
566
567 if (elemType == SkDOM::kText_Type) {
568 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400569 // TODO: add type conversion helper to SkSVGNode
570 if (ctx.fParent->tag() == SkSVGTag::kText) {
571 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
572 }
fmalita6ceef3d2016-07-26 18:46:34 -0700573 return nullptr;
574 }
575
576 SkASSERT(elemType == SkDOM::kElement_Type);
577
578 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
579 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
580 elem, sizeof(gTagFactories[0]));
581 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700582#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700583 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700584#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700585 return nullptr;
586 }
587
588 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
589 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700590 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700591
592 ConstructionContext localCtx(ctx, node);
593 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
594 child = dom.getNextSibling(child)) {
595 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
596 if (childNode) {
597 node->appendChild(std::move(childNode));
598 }
599 }
600
601 return node;
602}
603
604} // anonymous namespace
605
fmalitae1baa7c2016-09-14 12:04:30 -0700606SkSVGDOM::SkSVGDOM()
607 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700608}
609
fmalitae1baa7c2016-09-14 12:04:30 -0700610sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
611 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700612
fmalita28d5b722016-09-12 17:06:47 -0700613 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700614 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
615
fmalitae1baa7c2016-09-14 12:04:30 -0700616 // Reset the default container size to match the intrinsic SVG size.
617 dom->setContainerSize(dom->intrinsicSize());
618
fmalita6ceef3d2016-07-26 18:46:34 -0700619 return dom;
620}
621
fmalitae1baa7c2016-09-14 12:04:30 -0700622sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700623 SkDOM xmlDom;
624 if (!xmlDom.build(svgStream)) {
625 return nullptr;
626 }
627
fmalitae1baa7c2016-09-14 12:04:30 -0700628 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700629}
630
631void SkSVGDOM::render(SkCanvas* canvas) const {
632 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400633 SkSVGLengthContext lctx(fContainerSize);
634 SkSVGPresentationContext pctx;
Tyler Denniston53281c72020-10-22 15:54:24 -0400635 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700636 }
637}
638
fmalitae1baa7c2016-09-14 12:04:30 -0700639SkSize SkSVGDOM::intrinsicSize() const {
640 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
641 return SkSize::Make(0, 0);
642 }
643
644 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
645 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
646 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
647}
648
649const SkSize& SkSVGDOM::containerSize() const {
650 return fContainerSize;
651}
652
fmalita6ceef3d2016-07-26 18:46:34 -0700653void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
654 // TODO: inval
655 fContainerSize = containerSize;
656}
fmalitaca39d712016-08-12 13:17:11 -0700657
Tyler Freemanc9911522020-05-08 13:23:10 -0700658sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
659 SkString idStr(id);
660 return this->fIDMapper.find(idStr);
661}
662
fmalitaca39d712016-08-12 13:17:11 -0700663void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
664 fRoot = std::move(root);
665}
Tyler Freemanc9911522020-05-08 13:23:10 -0700666
667// TODO(fuego): move this to SkSVGNode or its own CU.
668bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
669 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
670}