blob: 342b8617d8fd2040ef11b19430d4b36ac351b539 [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 Denniston70bb18d2020-11-06 12:07:53 -050018#include "modules/svg/include/SkSVGFeColorMatrix.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050019#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040020#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040021#include "modules/svg/include/SkSVGG.h"
22#include "modules/svg/include/SkSVGLine.h"
23#include "modules/svg/include/SkSVGLinearGradient.h"
24#include "modules/svg/include/SkSVGNode.h"
25#include "modules/svg/include/SkSVGPath.h"
26#include "modules/svg/include/SkSVGPattern.h"
27#include "modules/svg/include/SkSVGPoly.h"
28#include "modules/svg/include/SkSVGRadialGradient.h"
29#include "modules/svg/include/SkSVGRect.h"
30#include "modules/svg/include/SkSVGRenderContext.h"
31#include "modules/svg/include/SkSVGSVG.h"
32#include "modules/svg/include/SkSVGStop.h"
33#include "modules/svg/include/SkSVGText.h"
34#include "modules/svg/include/SkSVGTypes.h"
35#include "modules/svg/include/SkSVGUse.h"
36#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040037#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070039
40namespace {
41
fmalita28d5b722016-09-12 17:06:47 -070042bool SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
43 const char* stringValue) {
44 SkSVGColorType color;
45 SkSVGAttributeParser parser(stringValue);
46 if (!parser.parseColor(&color)) {
47 return false;
48 }
49
Florin Malitaf4403e72020-04-10 14:14:04 +000050 node->setAttribute(attr, SkSVGColorValue(color));
fmalita28d5b722016-09-12 17:06:47 -070051 return true;
52}
53
54bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
55 const char* stringValue) {
56 SkSVGStringType iri;
57 SkSVGAttributeParser parser(stringValue);
58 if (!parser.parseIRI(&iri)) {
59 return false;
60 }
61
Florin Malitaf4403e72020-04-10 14:14:04 +000062 node->setAttribute(attr, SkSVGStringValue(iri));
fmalita28d5b722016-09-12 17:06:47 -070063 return true;
64}
65
fmalita6ceef3d2016-07-26 18:46:34 -070066bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
67 const char* stringValue) {
68 SkPath path;
69 if (!SkParsePath::FromSVGString(stringValue, &path)) {
70 return false;
71 }
72
Florin Malitaf4403e72020-04-10 14:14:04 +000073 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070074 return true;
75}
76
Xavier Phane29cdaf2020-03-26 16:15:14 +000077bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
78 const char* stringValue) {
79 SkString str(stringValue, strlen(stringValue));
80 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000081 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000082 return true;
83}
84
fmalita6ceef3d2016-07-26 18:46:34 -070085bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
86 const char* stringValue) {
fmalitac97796b2016-08-08 12:58:57 -070087 SkSVGTransformType transform;
88 SkSVGAttributeParser parser(stringValue);
89 if (!parser.parseTransform(&transform)) {
90 return false;
91 }
92
Florin Malitaf4403e72020-04-10 14:14:04 +000093 node->setAttribute(attr, SkSVGTransformValue(transform));
fmalita6ceef3d2016-07-26 18:46:34 -070094 return true;
95}
96
fmalitabffc2562016-08-03 10:21:11 -070097bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
98 const char* stringValue) {
99 SkSVGLength length;
100 SkSVGAttributeParser parser(stringValue);
101 if (!parser.parseLength(&length)) {
102 return false;
103 }
104
Florin Malitaf4403e72020-04-10 14:14:04 +0000105 node->setAttribute(attr, SkSVGLengthValue(length));
fmalitabffc2562016-08-03 10:21:11 -0700106 return true;
107}
108
fmalita2d961e02016-08-11 09:16:29 -0700109bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
110 const char* stringValue) {
111 SkSVGNumberType number;
112 SkSVGAttributeParser parser(stringValue);
113 if (!parser.parseNumber(&number)) {
114 return false;
115 }
116
Florin Malitaf4403e72020-04-10 14:14:04 +0000117 node->setAttribute(attr, SkSVGNumberValue(number));
fmalita2d961e02016-08-11 09:16:29 -0700118 return true;
119}
120
fmalita397a5172016-08-08 11:38:55 -0700121bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
122 const char* stringValue) {
123 SkSVGViewBoxType viewBox;
124 SkSVGAttributeParser parser(stringValue);
125 if (!parser.parseViewBox(&viewBox)) {
126 return false;
127 }
128
Florin Malitaf4403e72020-04-10 14:14:04 +0000129 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700130 return true;
131}
132
fmalitacecd6172016-09-13 12:56:11 -0700133bool SetSpreadMethodAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
134 const char* stringValue) {
135 SkSVGSpreadMethod spread;
136 SkSVGAttributeParser parser(stringValue);
137 if (!parser.parseSpreadMethod(&spread)) {
138 return false;
139 }
140
Florin Malitaf4403e72020-04-10 14:14:04 +0000141 node->setAttribute(attr, SkSVGSpreadMethodValue(spread));
fmalitacecd6172016-09-13 12:56:11 -0700142 return true;
143}
144
Tyler Denniston308c0722020-04-14 10:53:41 -0400145bool SetStopColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
146 const char* stringValue) {
147 SkSVGStopColor stopColor;
148 SkSVGAttributeParser parser(stringValue);
149 if (!parser.parseStopColor(&stopColor)) {
150 return false;
151 }
152
153 node->setAttribute(attr, SkSVGStopColorValue(stopColor));
154 return true;
155}
156
Tyler Denniston30e327e2020-10-29 16:29:22 -0400157bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
158 SkSVGAttribute attr,
159 const char* stringValue) {
160 SkSVGObjectBoundingBoxUnits objectBoundingBoxUnits;
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400161 SkSVGAttributeParser parser(stringValue);
Tyler Denniston30e327e2020-10-29 16:29:22 -0400162 if (!parser.parseObjectBoundingBoxUnits(&objectBoundingBoxUnits)) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400163 return false;
164 }
165
Tyler Denniston30e327e2020-10-29 16:29:22 -0400166 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(objectBoundingBoxUnits));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400167 return true;
168}
169
fmalita5b31f322016-08-12 12:15:33 -0700170bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
171 const char* stringValue) {
172 SkSVGPointsType points;
173 SkSVGAttributeParser parser(stringValue);
174 if (!parser.parsePoints(&points)) {
175 return false;
176 }
177
Florin Malitaf4403e72020-04-10 14:14:04 +0000178 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700179 return true;
180}
181
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400182bool SetFilterAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
183 const char* stringValue) {
184 SkSVGFilterType filter;
185 SkSVGAttributeParser parser(stringValue);
186 if (!parser.parseFilter(&filter)) {
187 return false;
188 }
189
190 node->setAttribute(attr, SkSVGFilterValue(filter));
191 return true;
192}
193
Florin Malita385e7442020-10-21 16:55:46 -0400194bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
195 const char* stringValue) {
196 SkSVGPreserveAspectRatio par;
197 SkSVGAttributeParser parser(stringValue);
198 if (!parser.parsePreserveAspectRatio(&par)) {
199 return false;
200 }
201
202 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
203 return true;
204}
205
fmalita61f36b32016-08-08 13:58:50 -0700206SkString TrimmedString(const char* first, const char* last) {
207 SkASSERT(first);
208 SkASSERT(last);
209 SkASSERT(first <= last);
210
211 while (first <= last && *first <= ' ') { first++; }
212 while (first <= last && *last <= ' ') { last--; }
213
214 SkASSERT(last - first + 1 >= 0);
215 return SkString(first, SkTo<size_t>(last - first + 1));
216}
217
fmalita58649cc2016-07-29 08:52:03 -0700218// Breaks a "foo: bar; baz: ..." string into key:value pairs.
219class StyleIterator {
220public:
221 StyleIterator(const char* str) : fPos(str) { }
222
223 std::tuple<SkString, SkString> next() {
224 SkString name, value;
225
226 if (fPos) {
227 const char* sep = this->nextSeparator();
228 SkASSERT(*sep == ';' || *sep == '\0');
229
230 const char* valueSep = strchr(fPos, ':');
231 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700232 name = TrimmedString(fPos, valueSep - 1);
233 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700234 }
235
236 fPos = *sep ? sep + 1 : nullptr;
237 }
238
239 return std::make_tuple(name, value);
240 }
241
242private:
243 const char* nextSeparator() const {
244 const char* sep = fPos;
245 while (*sep != ';' && *sep != '\0') {
246 sep++;
247 }
248 return sep;
249 }
250
251 const char* fPos;
252};
253
Tyler Freemanc9911522020-05-08 13:23:10 -0700254bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700255
256bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
257 const char* stringValue) {
258
259 SkString name, value;
260 StyleIterator iter(stringValue);
261 for (;;) {
262 std::tie(name, value) = iter.next();
263 if (name.isEmpty()) {
264 break;
265 }
266 set_string_attribute(node, name.c_str(), value.c_str());
267 }
268
269 return true;
270}
271
fmalita6ceef3d2016-07-26 18:46:34 -0700272template<typename T>
273struct SortedDictionaryEntry {
274 const char* fKey;
275 const T fValue;
276};
277
278struct AttrParseInfo {
279 SkSVGAttribute fAttr;
280 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
281};
282
283SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400284 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
285 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
286 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
287 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400288 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400289 { "filter" , { SkSVGAttribute::kFilter , SetFilterAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400290 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
291 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400292 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400293 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
294 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
295 { "gradientTransform" , { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
Tyler Denniston30e327e2020-10-29 16:29:22 -0400296 { "gradientUnits" , { SkSVGAttribute::kGradientUnits ,
297 SetObjectBoundingBoxUnitsAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400298 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
299 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
300 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
301 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
302 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
303 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
304 SetPreserveAspectRatioAttribute }},
305 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
306 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
307 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
308 { "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
309 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
310 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400311 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400312 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
313 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
314 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
315 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
316 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400317 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
318 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400319 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
320 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
321 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
322 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
323 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
324 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
325 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
326 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700327};
328
329SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400330 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700331 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500332 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700333 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
334 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
Tyler Denniston70bb18d2020-11-06 12:07:53 -0500335 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
Tyler Dennistondada9602020-11-03 10:04:25 -0500336 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400337 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700338 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
339 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
340 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
341 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400342 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700343 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
344 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400345 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700346 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
347 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
348 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000349 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400350 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700351};
352
353struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700354 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700355 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700356 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700357
Florin Malita39fe8c82020-10-20 10:43:03 -0400358 SkSVGNode* fParent;
359 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700360};
361
Tyler Freemanc9911522020-05-08 13:23:10 -0700362bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500363 if (node->parseAndSetAttribute(name, value)) {
364 // Handled by new code path
365 return true;
366 }
367
fmalita58649cc2016-07-29 08:52:03 -0700368 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
369 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
370 name, sizeof(gAttributeParseInfo[0]));
371 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700372#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700373 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700374#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700375 return false;
fmalita58649cc2016-07-29 08:52:03 -0700376 }
377
378 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
379 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
380 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700381#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700382 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700383#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700384 return false;
fmalita58649cc2016-07-29 08:52:03 -0700385 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700386
387 return true;
fmalita58649cc2016-07-29 08:52:03 -0700388}
389
fmalita6ceef3d2016-07-26 18:46:34 -0700390void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700391 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700392 const char* name, *value;
393 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
394 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700395 // We're handling id attributes out of band for now.
396 if (!strcmp(name, "id")) {
397 mapper->set(SkString(value), svgNode);
398 continue;
399 }
fmalita58649cc2016-07-29 08:52:03 -0700400 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700401 }
402}
403
404sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
405 const SkDOM::Node* xmlNode) {
406 const char* elem = dom.getName(xmlNode);
407 const SkDOM::Type elemType = dom.getType(xmlNode);
408
409 if (elemType == SkDOM::kText_Type) {
410 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400411 // TODO: add type conversion helper to SkSVGNode
412 if (ctx.fParent->tag() == SkSVGTag::kText) {
413 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
414 }
fmalita6ceef3d2016-07-26 18:46:34 -0700415 return nullptr;
416 }
417
418 SkASSERT(elemType == SkDOM::kElement_Type);
419
420 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
421 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
422 elem, sizeof(gTagFactories[0]));
423 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700424#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700425 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700426#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700427 return nullptr;
428 }
429
430 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
431 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700432 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700433
434 ConstructionContext localCtx(ctx, node);
435 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
436 child = dom.getNextSibling(child)) {
437 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
438 if (childNode) {
439 node->appendChild(std::move(childNode));
440 }
441 }
442
443 return node;
444}
445
446} // anonymous namespace
447
fmalitae1baa7c2016-09-14 12:04:30 -0700448SkSVGDOM::SkSVGDOM()
449 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700450}
451
fmalitae1baa7c2016-09-14 12:04:30 -0700452sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
453 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700454
fmalita28d5b722016-09-12 17:06:47 -0700455 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700456 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
457
fmalitae1baa7c2016-09-14 12:04:30 -0700458 // Reset the default container size to match the intrinsic SVG size.
459 dom->setContainerSize(dom->intrinsicSize());
460
fmalita6ceef3d2016-07-26 18:46:34 -0700461 return dom;
462}
463
fmalitae1baa7c2016-09-14 12:04:30 -0700464sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700465 SkDOM xmlDom;
466 if (!xmlDom.build(svgStream)) {
467 return nullptr;
468 }
469
fmalitae1baa7c2016-09-14 12:04:30 -0700470 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700471}
472
473void SkSVGDOM::render(SkCanvas* canvas) const {
474 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400475 SkSVGLengthContext lctx(fContainerSize);
476 SkSVGPresentationContext pctx;
Tyler Denniston53281c72020-10-22 15:54:24 -0400477 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700478 }
479}
480
fmalitae1baa7c2016-09-14 12:04:30 -0700481SkSize SkSVGDOM::intrinsicSize() const {
482 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
483 return SkSize::Make(0, 0);
484 }
485
486 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
487 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
488 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
489}
490
491const SkSize& SkSVGDOM::containerSize() const {
492 return fContainerSize;
493}
494
fmalita6ceef3d2016-07-26 18:46:34 -0700495void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
496 // TODO: inval
497 fContainerSize = containerSize;
498}
fmalitaca39d712016-08-12 13:17:11 -0700499
Tyler Freemanc9911522020-05-08 13:23:10 -0700500sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
501 SkString idStr(id);
502 return this->fIDMapper.find(idStr);
503}
504
fmalitaca39d712016-08-12 13:17:11 -0700505void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
506 fRoot = std::move(root);
507}
Tyler Freemanc9911522020-05-08 13:23:10 -0700508
509// TODO(fuego): move this to SkSVGNode or its own CU.
510bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
511 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
512}