blob: 23a33550c7ac91d580da3860269d3315118f8387 [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 }},
fmalitaceb93ab2016-09-13 13:59:05 -0700319 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
320 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
321 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
322 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
323 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
Florin Malitae932d4b2016-12-01 13:35:11 -0500324 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000325 { "font-family" , { SkSVGAttribute::kFontFamily , SetStringAttribute }},
326 { "font-size" , { SkSVGAttribute::kFontSize , SetLengthAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400327 // focal point x & y
328 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
329 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700330 { "gradientTransform", { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
331 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
332 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
333 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400334 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700335 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
336 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
337 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
338 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
339 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
340 { "stop-color" , { SkSVGAttribute::kStopColor , SetColorAttribute }},
341 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
342 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
Florin Malitaf543a602017-10-13 14:07:44 -0400343 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
Florin Malitae1dadd72017-10-13 18:18:32 -0400344 { "stroke-dashoffset", { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700345 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
346 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
Florin Malita4de426b2017-10-09 12:57:41 -0400347 { "stroke-miterlimit", { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700348 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
349 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
350 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000351 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
352 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetStringAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700353 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
354 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malitaffe6ae42017-10-12 11:33:28 -0400355 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700356 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
357 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
358 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
359 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
360 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
361 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
362 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
363 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700364};
365
366SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400367 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700368 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500369 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700370 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
371 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
372 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
373 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
374 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
375 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400376 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700377 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
378 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400379 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700380 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
381 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
382 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000383 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400384 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700385};
386
387struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700388 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700389 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700390 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700391
392 const SkSVGNode* fParent;
fmalita28d5b722016-09-12 17:06:47 -0700393 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700394};
395
fmalita58649cc2016-07-29 08:52:03 -0700396void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
397 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
398 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
399 name, sizeof(gAttributeParseInfo[0]));
400 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700401#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700402 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700403#endif
fmalita58649cc2016-07-29 08:52:03 -0700404 return;
405 }
406
407 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
408 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
409 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700410#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700411 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700412#endif
fmalita58649cc2016-07-29 08:52:03 -0700413 }
414}
415
fmalita6ceef3d2016-07-26 18:46:34 -0700416void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700417 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700418 const char* name, *value;
419 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
420 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700421 // We're handling id attributes out of band for now.
422 if (!strcmp(name, "id")) {
423 mapper->set(SkString(value), svgNode);
424 continue;
425 }
fmalita58649cc2016-07-29 08:52:03 -0700426 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700427 }
428}
429
430sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
431 const SkDOM::Node* xmlNode) {
432 const char* elem = dom.getName(xmlNode);
433 const SkDOM::Type elemType = dom.getType(xmlNode);
434
435 if (elemType == SkDOM::kText_Type) {
436 SkASSERT(dom.countChildren(xmlNode) == 0);
437 // TODO: text handling
438 return nullptr;
439 }
440
441 SkASSERT(elemType == SkDOM::kElement_Type);
442
443 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
444 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
445 elem, sizeof(gTagFactories[0]));
446 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700447#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700448 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700449#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700450 return nullptr;
451 }
452
453 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
454 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700455 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700456
457 ConstructionContext localCtx(ctx, node);
458 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
459 child = dom.getNextSibling(child)) {
460 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
461 if (childNode) {
462 node->appendChild(std::move(childNode));
463 }
464 }
465
466 return node;
467}
468
469} // anonymous namespace
470
fmalitae1baa7c2016-09-14 12:04:30 -0700471SkSVGDOM::SkSVGDOM()
472 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700473}
474
fmalitae1baa7c2016-09-14 12:04:30 -0700475sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
476 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700477
fmalita28d5b722016-09-12 17:06:47 -0700478 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700479 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
480
fmalitae1baa7c2016-09-14 12:04:30 -0700481 // Reset the default container size to match the intrinsic SVG size.
482 dom->setContainerSize(dom->intrinsicSize());
483
fmalita6ceef3d2016-07-26 18:46:34 -0700484 return dom;
485}
486
fmalitae1baa7c2016-09-14 12:04:30 -0700487sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700488 SkDOM xmlDom;
489 if (!xmlDom.build(svgStream)) {
490 return nullptr;
491 }
492
fmalitae1baa7c2016-09-14 12:04:30 -0700493 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700494}
495
496void SkSVGDOM::render(SkCanvas* canvas) const {
497 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400498 SkSVGLengthContext lctx(fContainerSize);
499 SkSVGPresentationContext pctx;
500 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx));
fmalita6ceef3d2016-07-26 18:46:34 -0700501 }
502}
503
fmalitae1baa7c2016-09-14 12:04:30 -0700504SkSize SkSVGDOM::intrinsicSize() const {
505 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
506 return SkSize::Make(0, 0);
507 }
508
509 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
510 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
511 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
512}
513
514const SkSize& SkSVGDOM::containerSize() const {
515 return fContainerSize;
516}
517
fmalita6ceef3d2016-07-26 18:46:34 -0700518void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
519 // TODO: inval
520 fContainerSize = containerSize;
521}
fmalitaca39d712016-08-12 13:17:11 -0700522
523void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
524 fRoot = std::move(root);
525}