blob: 94ab6cf943bb1d288d71901474f8876d440f59bd [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 "experimental/svg/model/SkSVGAttributeParser.h"
9#include "experimental/svg/model/SkSVGCircle.h"
10#include "experimental/svg/model/SkSVGClipPath.h"
11#include "experimental/svg/model/SkSVGDOM.h"
12#include "experimental/svg/model/SkSVGDefs.h"
13#include "experimental/svg/model/SkSVGEllipse.h"
14#include "experimental/svg/model/SkSVGG.h"
15#include "experimental/svg/model/SkSVGLine.h"
16#include "experimental/svg/model/SkSVGLinearGradient.h"
17#include "experimental/svg/model/SkSVGNode.h"
18#include "experimental/svg/model/SkSVGPath.h"
19#include "experimental/svg/model/SkSVGPattern.h"
20#include "experimental/svg/model/SkSVGPoly.h"
21#include "experimental/svg/model/SkSVGRadialGradient.h"
22#include "experimental/svg/model/SkSVGRect.h"
23#include "experimental/svg/model/SkSVGRenderContext.h"
24#include "experimental/svg/model/SkSVGSVG.h"
25#include "experimental/svg/model/SkSVGStop.h"
Xavier Phane29cdaf2020-03-26 16:15:14 +000026#include "experimental/svg/model/SkSVGText.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "experimental/svg/model/SkSVGTypes.h"
28#include "experimental/svg/model/SkSVGUse.h"
29#include "experimental/svg/model/SkSVGValue.h"
30#include "include/core/SkCanvas.h"
31#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "include/private/SkTo.h"
33#include "include/utils/SkParsePath.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
fmalita2d961e02016-08-11 09:16:29 -070047 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
59 node->setAttribute(attr, SkSVGColorValue(color));
60 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
71 node->setAttribute(attr, SkSVGStringValue(iri));
72 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
83 node->setAttribute(attr, SkSVGClipValue(clip));
84 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
95 node->setAttribute(attr, SkSVGPathValue(path));
96 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);
103 node->setAttribute(attr, SkSVGStringValue(strType));
104 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
115 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
127 node->setAttribute(attr, SkSVGLengthValue(length));
128 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
139 node->setAttribute(attr, SkSVGNumberValue(number));
140 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
151 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
152 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
163 node->setAttribute(attr, SkSVGLineCapValue(lineCap));
164 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
175 node->setAttribute(attr, SkSVGLineJoinValue(lineJoin));
176 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
187 node->setAttribute(attr, SkSVGSpreadMethodValue(spread));
188 return true;
189}
190
fmalita5b31f322016-08-12 12:15:33 -0700191bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
192 const char* stringValue) {
193 SkSVGPointsType points;
194 SkSVGAttributeParser parser(stringValue);
195 if (!parser.parsePoints(&points)) {
196 return false;
197 }
198
199 node->setAttribute(attr, SkSVGPointsValue(points));
200 return true;
201}
202
Florin Malitae932d4b2016-12-01 13:35:11 -0500203bool SetFillRuleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
204 const char* stringValue) {
205 SkSVGFillRule fillRule;
206 SkSVGAttributeParser parser(stringValue);
207 if (!parser.parseFillRule(&fillRule)) {
208 return false;
209 }
210
211 node->setAttribute(attr, SkSVGFillRuleValue(fillRule));
212 return true;
213}
214
Florin Malitaffe6ae42017-10-12 11:33:28 -0400215bool SetVisibilityAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
216 const char* stringValue) {
217 SkSVGVisibility visibility;
218 SkSVGAttributeParser parser(stringValue);
219 if (!parser.parseVisibility(&visibility)) {
220 return false;
221 }
222
223 node->setAttribute(attr, SkSVGVisibilityValue(visibility));
224 return true;
225}
226
Florin Malitaf543a602017-10-13 14:07:44 -0400227bool SetDashArrayAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
228 const char* stringValue) {
229 SkSVGDashArray dashArray;
230 SkSVGAttributeParser parser(stringValue);
231 if (!parser.parseDashArray(&dashArray)) {
232 return false;
233 }
234
235 node->setAttribute(attr, SkSVGDashArrayValue(dashArray));
236 return true;
237}
238
fmalita61f36b32016-08-08 13:58:50 -0700239SkString TrimmedString(const char* first, const char* last) {
240 SkASSERT(first);
241 SkASSERT(last);
242 SkASSERT(first <= last);
243
244 while (first <= last && *first <= ' ') { first++; }
245 while (first <= last && *last <= ' ') { last--; }
246
247 SkASSERT(last - first + 1 >= 0);
248 return SkString(first, SkTo<size_t>(last - first + 1));
249}
250
fmalita58649cc2016-07-29 08:52:03 -0700251// Breaks a "foo: bar; baz: ..." string into key:value pairs.
252class StyleIterator {
253public:
254 StyleIterator(const char* str) : fPos(str) { }
255
256 std::tuple<SkString, SkString> next() {
257 SkString name, value;
258
259 if (fPos) {
260 const char* sep = this->nextSeparator();
261 SkASSERT(*sep == ';' || *sep == '\0');
262
263 const char* valueSep = strchr(fPos, ':');
264 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700265 name = TrimmedString(fPos, valueSep - 1);
266 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700267 }
268
269 fPos = *sep ? sep + 1 : nullptr;
270 }
271
272 return std::make_tuple(name, value);
273 }
274
275private:
276 const char* nextSeparator() const {
277 const char* sep = fPos;
278 while (*sep != ';' && *sep != '\0') {
279 sep++;
280 }
281 return sep;
282 }
283
284 const char* fPos;
285};
286
287void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
288
289bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
290 const char* stringValue) {
291
292 SkString name, value;
293 StyleIterator iter(stringValue);
294 for (;;) {
295 std::tie(name, value) = iter.next();
296 if (name.isEmpty()) {
297 break;
298 }
299 set_string_attribute(node, name.c_str(), value.c_str());
300 }
301
302 return true;
303}
304
fmalita6ceef3d2016-07-26 18:46:34 -0700305template<typename T>
306struct SortedDictionaryEntry {
307 const char* fKey;
308 const T fValue;
309};
310
311struct AttrParseInfo {
312 SkSVGAttribute fAttr;
313 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
314};
315
316SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malitace8840e2016-12-08 09:26:47 -0500317 { "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
Florin Malita57a0edf2017-10-10 11:22:08 -0400318 { "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400319 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700320 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
321 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
322 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
323 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
324 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
Florin Malitae932d4b2016-12-01 13:35:11 -0500325 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000326 { "font-family" , { SkSVGAttribute::kFontFamily , SetStringAttribute }},
327 { "font-size" , { SkSVGAttribute::kFontSize , SetLengthAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400328 // focal point x & y
329 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
330 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700331 { "gradientTransform", { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
332 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
333 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
334 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400335 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700336 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
337 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
338 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
339 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
340 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
341 { "stop-color" , { SkSVGAttribute::kStopColor , SetColorAttribute }},
342 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
343 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
Florin Malitaf543a602017-10-13 14:07:44 -0400344 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
Florin Malitae1dadd72017-10-13 18:18:32 -0400345 { "stroke-dashoffset", { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700346 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
347 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
Florin Malita4de426b2017-10-09 12:57:41 -0400348 { "stroke-miterlimit", { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700349 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
350 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
351 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000352 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
353 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetStringAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700354 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
355 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malitaffe6ae42017-10-12 11:33:28 -0400356 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700357 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
358 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
359 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
360 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
361 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
362 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
363 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
364 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700365};
366
367SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400368 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700369 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500370 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700371 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
372 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
373 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
374 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
375 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
376 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400377 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700378 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
379 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400380 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700381 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
382 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
383 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000384 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400385 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700386};
387
388struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700389 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700390 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700391 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700392
393 const SkSVGNode* fParent;
fmalita28d5b722016-09-12 17:06:47 -0700394 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700395};
396
fmalita58649cc2016-07-29 08:52:03 -0700397void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
398 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
399 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
400 name, sizeof(gAttributeParseInfo[0]));
401 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700402#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700403 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700404#endif
fmalita58649cc2016-07-29 08:52:03 -0700405 return;
406 }
407
408 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
409 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
410 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700411#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700412 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700413#endif
fmalita58649cc2016-07-29 08:52:03 -0700414 }
415}
416
fmalita6ceef3d2016-07-26 18:46:34 -0700417void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700418 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700419 const char* name, *value;
420 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
421 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700422 // We're handling id attributes out of band for now.
423 if (!strcmp(name, "id")) {
424 mapper->set(SkString(value), svgNode);
425 continue;
426 }
fmalita58649cc2016-07-29 08:52:03 -0700427 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700428 }
429}
430
431sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
432 const SkDOM::Node* xmlNode) {
433 const char* elem = dom.getName(xmlNode);
434 const SkDOM::Type elemType = dom.getType(xmlNode);
435
436 if (elemType == SkDOM::kText_Type) {
437 SkASSERT(dom.countChildren(xmlNode) == 0);
438 // TODO: text handling
439 return nullptr;
440 }
441
442 SkASSERT(elemType == SkDOM::kElement_Type);
443
444 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
445 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
446 elem, sizeof(gTagFactories[0]));
447 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700448#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700449 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700450#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700451 return nullptr;
452 }
453
454 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
455 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700456 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700457
458 ConstructionContext localCtx(ctx, node);
459 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
460 child = dom.getNextSibling(child)) {
461 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
462 if (childNode) {
463 node->appendChild(std::move(childNode));
464 }
465 }
466
467 return node;
468}
469
470} // anonymous namespace
471
fmalitae1baa7c2016-09-14 12:04:30 -0700472SkSVGDOM::SkSVGDOM()
473 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700474}
475
fmalitae1baa7c2016-09-14 12:04:30 -0700476sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
477 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700478
fmalita28d5b722016-09-12 17:06:47 -0700479 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700480 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
481
fmalitae1baa7c2016-09-14 12:04:30 -0700482 // Reset the default container size to match the intrinsic SVG size.
483 dom->setContainerSize(dom->intrinsicSize());
484
fmalita6ceef3d2016-07-26 18:46:34 -0700485 return dom;
486}
487
fmalitae1baa7c2016-09-14 12:04:30 -0700488sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700489 SkDOM xmlDom;
490 if (!xmlDom.build(svgStream)) {
491 return nullptr;
492 }
493
fmalitae1baa7c2016-09-14 12:04:30 -0700494 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700495}
496
497void SkSVGDOM::render(SkCanvas* canvas) const {
498 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400499 SkSVGLengthContext lctx(fContainerSize);
500 SkSVGPresentationContext pctx;
501 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx));
fmalita6ceef3d2016-07-26 18:46:34 -0700502 }
503}
504
fmalitae1baa7c2016-09-14 12:04:30 -0700505SkSize SkSVGDOM::intrinsicSize() const {
506 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
507 return SkSize::Make(0, 0);
508 }
509
510 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
511 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
512 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
513}
514
515const SkSize& SkSVGDOM::containerSize() const {
516 return fContainerSize;
517}
518
fmalita6ceef3d2016-07-26 18:46:34 -0700519void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
520 // TODO: inval
521 fContainerSize = containerSize;
522}
fmalitaca39d712016-08-12 13:17:11 -0700523
524void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
525 fRoot = std::move(root);
526}