blob: 913801e1d86488d9a7ff042420accbbb7533a89e [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"
Florin Malitaf4403e72020-04-10 14:14:04 +000029#include "experimental/svg/model/SkSVGValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#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
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
fmalita5b31f322016-08-12 12:15:33 -0700203bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
204 const char* stringValue) {
205 SkSVGPointsType points;
206 SkSVGAttributeParser parser(stringValue);
207 if (!parser.parsePoints(&points)) {
208 return false;
209 }
210
Florin Malitaf4403e72020-04-10 14:14:04 +0000211 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700212 return true;
213}
214
Florin Malitae932d4b2016-12-01 13:35:11 -0500215bool SetFillRuleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
216 const char* stringValue) {
217 SkSVGFillRule fillRule;
218 SkSVGAttributeParser parser(stringValue);
219 if (!parser.parseFillRule(&fillRule)) {
220 return false;
221 }
222
Florin Malitaf4403e72020-04-10 14:14:04 +0000223 node->setAttribute(attr, SkSVGFillRuleValue(fillRule));
Florin Malitae932d4b2016-12-01 13:35:11 -0500224 return true;
225}
226
Florin Malitaffe6ae42017-10-12 11:33:28 -0400227bool SetVisibilityAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
228 const char* stringValue) {
229 SkSVGVisibility visibility;
230 SkSVGAttributeParser parser(stringValue);
231 if (!parser.parseVisibility(&visibility)) {
232 return false;
233 }
234
Florin Malitaf4403e72020-04-10 14:14:04 +0000235 node->setAttribute(attr, SkSVGVisibilityValue(visibility));
Florin Malitaffe6ae42017-10-12 11:33:28 -0400236 return true;
237}
238
Florin Malitaf543a602017-10-13 14:07:44 -0400239bool SetDashArrayAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
240 const char* stringValue) {
241 SkSVGDashArray dashArray;
242 SkSVGAttributeParser parser(stringValue);
243 if (!parser.parseDashArray(&dashArray)) {
244 return false;
245 }
246
Florin Malitaf4403e72020-04-10 14:14:04 +0000247 node->setAttribute(attr, SkSVGDashArrayValue(dashArray));
Florin Malitaf543a602017-10-13 14:07:44 -0400248 return true;
249}
250
fmalita61f36b32016-08-08 13:58:50 -0700251SkString TrimmedString(const char* first, const char* last) {
252 SkASSERT(first);
253 SkASSERT(last);
254 SkASSERT(first <= last);
255
256 while (first <= last && *first <= ' ') { first++; }
257 while (first <= last && *last <= ' ') { last--; }
258
259 SkASSERT(last - first + 1 >= 0);
260 return SkString(first, SkTo<size_t>(last - first + 1));
261}
262
fmalita58649cc2016-07-29 08:52:03 -0700263// Breaks a "foo: bar; baz: ..." string into key:value pairs.
264class StyleIterator {
265public:
266 StyleIterator(const char* str) : fPos(str) { }
267
268 std::tuple<SkString, SkString> next() {
269 SkString name, value;
270
271 if (fPos) {
272 const char* sep = this->nextSeparator();
273 SkASSERT(*sep == ';' || *sep == '\0');
274
275 const char* valueSep = strchr(fPos, ':');
276 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700277 name = TrimmedString(fPos, valueSep - 1);
278 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700279 }
280
281 fPos = *sep ? sep + 1 : nullptr;
282 }
283
284 return std::make_tuple(name, value);
285 }
286
287private:
288 const char* nextSeparator() const {
289 const char* sep = fPos;
290 while (*sep != ';' && *sep != '\0') {
291 sep++;
292 }
293 return sep;
294 }
295
296 const char* fPos;
297};
298
299void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
300
301bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
302 const char* stringValue) {
303
304 SkString name, value;
305 StyleIterator iter(stringValue);
306 for (;;) {
307 std::tie(name, value) = iter.next();
308 if (name.isEmpty()) {
309 break;
310 }
311 set_string_attribute(node, name.c_str(), value.c_str());
312 }
313
314 return true;
315}
316
fmalita6ceef3d2016-07-26 18:46:34 -0700317template<typename T>
318struct SortedDictionaryEntry {
319 const char* fKey;
320 const T fValue;
321};
322
323struct AttrParseInfo {
324 SkSVGAttribute fAttr;
325 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
326};
327
328SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malitace8840e2016-12-08 09:26:47 -0500329 { "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
Florin Malita57a0edf2017-10-10 11:22:08 -0400330 { "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400331 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700332 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
333 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
334 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
335 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
336 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
Florin Malitae932d4b2016-12-01 13:35:11 -0500337 { "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000338 { "font-family" , { SkSVGAttribute::kFontFamily , SetStringAttribute }},
339 { "font-size" , { SkSVGAttribute::kFontSize , SetLengthAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400340 // focal point x & y
341 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
342 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700343 { "gradientTransform", { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
344 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
345 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
346 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400347 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700348 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
349 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
350 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
351 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
352 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
Tyler Denniston308c0722020-04-14 10:53:41 -0400353 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700354 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
355 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
Florin Malitaf543a602017-10-13 14:07:44 -0400356 { "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
Florin Malitae1dadd72017-10-13 18:18:32 -0400357 { "stroke-dashoffset", { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700358 { "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
359 { "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
Florin Malita4de426b2017-10-09 12:57:41 -0400360 { "stroke-miterlimit", { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700361 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
362 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
363 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000364 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
365 { "text-anchor" , { SkSVGAttribute::kTextAnchor , SetStringAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700366 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
367 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malitaffe6ae42017-10-12 11:33:28 -0400368 { "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
fmalitaceb93ab2016-09-13 13:59:05 -0700369 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
370 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
371 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
372 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
373 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
374 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
375 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
376 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700377};
378
379SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400380 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700381 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500382 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700383 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
384 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
385 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
386 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
387 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
388 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400389 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700390 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
391 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400392 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700393 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
394 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
395 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000396 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400397 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700398};
399
400struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700401 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700402 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700403 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700404
405 const SkSVGNode* fParent;
fmalita28d5b722016-09-12 17:06:47 -0700406 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700407};
408
fmalita58649cc2016-07-29 08:52:03 -0700409void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
410 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
411 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
412 name, sizeof(gAttributeParseInfo[0]));
413 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700414#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700415 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700416#endif
fmalita58649cc2016-07-29 08:52:03 -0700417 return;
418 }
419
420 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
421 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
422 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700423#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700424 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700425#endif
fmalita58649cc2016-07-29 08:52:03 -0700426 }
427}
428
fmalita6ceef3d2016-07-26 18:46:34 -0700429void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700430 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700431 const char* name, *value;
432 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
433 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700434 // We're handling id attributes out of band for now.
435 if (!strcmp(name, "id")) {
436 mapper->set(SkString(value), svgNode);
437 continue;
438 }
fmalita58649cc2016-07-29 08:52:03 -0700439 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700440 }
441}
442
443sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
444 const SkDOM::Node* xmlNode) {
445 const char* elem = dom.getName(xmlNode);
446 const SkDOM::Type elemType = dom.getType(xmlNode);
447
448 if (elemType == SkDOM::kText_Type) {
449 SkASSERT(dom.countChildren(xmlNode) == 0);
450 // TODO: text handling
451 return nullptr;
452 }
453
454 SkASSERT(elemType == SkDOM::kElement_Type);
455
456 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
457 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
458 elem, sizeof(gTagFactories[0]));
459 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700460#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700461 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700462#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700463 return nullptr;
464 }
465
466 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
467 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700468 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700469
470 ConstructionContext localCtx(ctx, node);
471 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
472 child = dom.getNextSibling(child)) {
473 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
474 if (childNode) {
475 node->appendChild(std::move(childNode));
476 }
477 }
478
479 return node;
480}
481
482} // anonymous namespace
483
fmalitae1baa7c2016-09-14 12:04:30 -0700484SkSVGDOM::SkSVGDOM()
485 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700486}
487
fmalitae1baa7c2016-09-14 12:04:30 -0700488sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
489 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700490
fmalita28d5b722016-09-12 17:06:47 -0700491 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700492 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
493
fmalitae1baa7c2016-09-14 12:04:30 -0700494 // Reset the default container size to match the intrinsic SVG size.
495 dom->setContainerSize(dom->intrinsicSize());
496
fmalita6ceef3d2016-07-26 18:46:34 -0700497 return dom;
498}
499
fmalitae1baa7c2016-09-14 12:04:30 -0700500sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700501 SkDOM xmlDom;
502 if (!xmlDom.build(svgStream)) {
503 return nullptr;
504 }
505
fmalitae1baa7c2016-09-14 12:04:30 -0700506 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700507}
508
509void SkSVGDOM::render(SkCanvas* canvas) const {
510 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400511 SkSVGLengthContext lctx(fContainerSize);
512 SkSVGPresentationContext pctx;
513 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx));
fmalita6ceef3d2016-07-26 18:46:34 -0700514 }
515}
516
fmalitae1baa7c2016-09-14 12:04:30 -0700517SkSize SkSVGDOM::intrinsicSize() const {
518 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
519 return SkSize::Make(0, 0);
520 }
521
522 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
523 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
524 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
525}
526
527const SkSize& SkSVGDOM::containerSize() const {
528 return fContainerSize;
529}
530
fmalita6ceef3d2016-07-26 18:46:34 -0700531void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
532 // TODO: inval
533 fContainerSize = containerSize;
534}
fmalitaca39d712016-08-12 13:17:11 -0700535
536void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
537 fRoot = std::move(root);
538}