blob: 0ec1b1388b9763dfb109ef56f6e1ecd3f7a0aada [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 Dennistonb25caae2020-11-09 12:46:02 -050019#include "modules/svg/include/SkSVGFeComposite.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050020#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040021#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040022#include "modules/svg/include/SkSVGG.h"
23#include "modules/svg/include/SkSVGLine.h"
24#include "modules/svg/include/SkSVGLinearGradient.h"
25#include "modules/svg/include/SkSVGNode.h"
26#include "modules/svg/include/SkSVGPath.h"
27#include "modules/svg/include/SkSVGPattern.h"
28#include "modules/svg/include/SkSVGPoly.h"
29#include "modules/svg/include/SkSVGRadialGradient.h"
30#include "modules/svg/include/SkSVGRect.h"
31#include "modules/svg/include/SkSVGRenderContext.h"
32#include "modules/svg/include/SkSVGSVG.h"
33#include "modules/svg/include/SkSVGStop.h"
34#include "modules/svg/include/SkSVGText.h"
35#include "modules/svg/include/SkSVGTypes.h"
36#include "modules/svg/include/SkSVGUse.h"
37#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040038#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070040
41namespace {
42
fmalita28d5b722016-09-12 17:06:47 -070043bool SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
44 const char* stringValue) {
45 SkSVGColorType color;
46 SkSVGAttributeParser parser(stringValue);
47 if (!parser.parseColor(&color)) {
48 return false;
49 }
50
Florin Malitaf4403e72020-04-10 14:14:04 +000051 node->setAttribute(attr, SkSVGColorValue(color));
fmalita28d5b722016-09-12 17:06:47 -070052 return true;
53}
54
55bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
56 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050057 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
58 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070059 return false;
60 }
61
Tyler Dennistona0a51462020-11-10 13:13:28 -050062 node->setAttribute(attr, SkSVGStringValue(parseResult->fIRI));
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) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050087 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
88 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070089 return false;
90 }
91
Tyler Dennistona0a51462020-11-10 13:13:28 -050092 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070093 return true;
94}
95
fmalitabffc2562016-08-03 10:21:11 -070096bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
97 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050098 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
99 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -0700100 return false;
101 }
102
Tyler Dennistona0a51462020-11-10 13:13:28 -0500103 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -0700104 return true;
105}
106
fmalita2d961e02016-08-11 09:16:29 -0700107bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
108 const char* stringValue) {
109 SkSVGNumberType number;
110 SkSVGAttributeParser parser(stringValue);
111 if (!parser.parseNumber(&number)) {
112 return false;
113 }
114
Florin Malitaf4403e72020-04-10 14:14:04 +0000115 node->setAttribute(attr, SkSVGNumberValue(number));
fmalita2d961e02016-08-11 09:16:29 -0700116 return true;
117}
118
fmalita397a5172016-08-08 11:38:55 -0700119bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
120 const char* stringValue) {
121 SkSVGViewBoxType viewBox;
122 SkSVGAttributeParser parser(stringValue);
123 if (!parser.parseViewBox(&viewBox)) {
124 return false;
125 }
126
Florin Malitaf4403e72020-04-10 14:14:04 +0000127 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700128 return true;
129}
130
Tyler Denniston308c0722020-04-14 10:53:41 -0400131bool SetStopColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
132 const char* stringValue) {
133 SkSVGStopColor stopColor;
134 SkSVGAttributeParser parser(stringValue);
135 if (!parser.parseStopColor(&stopColor)) {
136 return false;
137 }
138
139 node->setAttribute(attr, SkSVGStopColorValue(stopColor));
140 return true;
141}
142
Tyler Denniston30e327e2020-10-29 16:29:22 -0400143bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
144 SkSVGAttribute attr,
145 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500146 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
147 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400148 return false;
149 }
150
Tyler Dennistona0a51462020-11-10 13:13:28 -0500151 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400152 return true;
153}
154
fmalita5b31f322016-08-12 12:15:33 -0700155bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
156 const char* stringValue) {
157 SkSVGPointsType points;
158 SkSVGAttributeParser parser(stringValue);
159 if (!parser.parsePoints(&points)) {
160 return false;
161 }
162
Florin Malitaf4403e72020-04-10 14:14:04 +0000163 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700164 return true;
165}
166
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400167bool SetFilterAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
168 const char* stringValue) {
169 SkSVGFilterType filter;
170 SkSVGAttributeParser parser(stringValue);
171 if (!parser.parseFilter(&filter)) {
172 return false;
173 }
174
175 node->setAttribute(attr, SkSVGFilterValue(filter));
176 return true;
177}
178
Florin Malita385e7442020-10-21 16:55:46 -0400179bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
180 const char* stringValue) {
181 SkSVGPreserveAspectRatio par;
182 SkSVGAttributeParser parser(stringValue);
183 if (!parser.parsePreserveAspectRatio(&par)) {
184 return false;
185 }
186
187 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
188 return true;
189}
190
fmalita61f36b32016-08-08 13:58:50 -0700191SkString TrimmedString(const char* first, const char* last) {
192 SkASSERT(first);
193 SkASSERT(last);
194 SkASSERT(first <= last);
195
196 while (first <= last && *first <= ' ') { first++; }
197 while (first <= last && *last <= ' ') { last--; }
198
199 SkASSERT(last - first + 1 >= 0);
200 return SkString(first, SkTo<size_t>(last - first + 1));
201}
202
fmalita58649cc2016-07-29 08:52:03 -0700203// Breaks a "foo: bar; baz: ..." string into key:value pairs.
204class StyleIterator {
205public:
206 StyleIterator(const char* str) : fPos(str) { }
207
208 std::tuple<SkString, SkString> next() {
209 SkString name, value;
210
211 if (fPos) {
212 const char* sep = this->nextSeparator();
213 SkASSERT(*sep == ';' || *sep == '\0');
214
215 const char* valueSep = strchr(fPos, ':');
216 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700217 name = TrimmedString(fPos, valueSep - 1);
218 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700219 }
220
221 fPos = *sep ? sep + 1 : nullptr;
222 }
223
224 return std::make_tuple(name, value);
225 }
226
227private:
228 const char* nextSeparator() const {
229 const char* sep = fPos;
230 while (*sep != ';' && *sep != '\0') {
231 sep++;
232 }
233 return sep;
234 }
235
236 const char* fPos;
237};
238
Tyler Freemanc9911522020-05-08 13:23:10 -0700239bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700240
241bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
242 const char* stringValue) {
243
244 SkString name, value;
245 StyleIterator iter(stringValue);
246 for (;;) {
247 std::tie(name, value) = iter.next();
248 if (name.isEmpty()) {
249 break;
250 }
251 set_string_attribute(node, name.c_str(), value.c_str());
252 }
253
254 return true;
255}
256
fmalita6ceef3d2016-07-26 18:46:34 -0700257template<typename T>
258struct SortedDictionaryEntry {
259 const char* fKey;
260 const T fValue;
261};
262
263struct AttrParseInfo {
264 SkSVGAttribute fAttr;
265 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
266};
267
268SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400269 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
270 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
271 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
272 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400273 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400274 { "filter" , { SkSVGAttribute::kFilter , SetFilterAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400275 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
276 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400277 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400278 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
279 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400280 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
281 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
282 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
283 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
284 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
285 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
286 SetPreserveAspectRatioAttribute }},
287 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
288 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
289 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400290 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
291 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400292 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400293 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
294 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
295 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
296 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
297 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400298 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
299 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400300 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
301 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
302 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
303 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
304 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
305 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
306 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
307 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700308};
309
310SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400311 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700312 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500313 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700314 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
315 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
Tyler Denniston70bb18d2020-11-06 12:07:53 -0500316 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
Tyler Dennistonb25caae2020-11-09 12:46:02 -0500317 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
Tyler Dennistondada9602020-11-03 10:04:25 -0500318 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400319 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700320 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
321 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
322 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
323 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400324 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700325 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
326 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400327 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700328 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
329 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
330 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000331 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400332 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700333};
334
335struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700336 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700337 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700338 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700339
Florin Malita39fe8c82020-10-20 10:43:03 -0400340 SkSVGNode* fParent;
341 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700342};
343
Tyler Freemanc9911522020-05-08 13:23:10 -0700344bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500345 if (node->parseAndSetAttribute(name, value)) {
346 // Handled by new code path
347 return true;
348 }
349
fmalita58649cc2016-07-29 08:52:03 -0700350 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
351 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
352 name, sizeof(gAttributeParseInfo[0]));
353 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700354#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700355 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700356#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700357 return false;
fmalita58649cc2016-07-29 08:52:03 -0700358 }
359
360 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
361 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
362 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700363#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700364 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700365#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700366 return false;
fmalita58649cc2016-07-29 08:52:03 -0700367 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700368
369 return true;
fmalita58649cc2016-07-29 08:52:03 -0700370}
371
fmalita6ceef3d2016-07-26 18:46:34 -0700372void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700373 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700374 const char* name, *value;
375 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
376 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700377 // We're handling id attributes out of band for now.
378 if (!strcmp(name, "id")) {
379 mapper->set(SkString(value), svgNode);
380 continue;
381 }
fmalita58649cc2016-07-29 08:52:03 -0700382 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700383 }
384}
385
386sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
387 const SkDOM::Node* xmlNode) {
388 const char* elem = dom.getName(xmlNode);
389 const SkDOM::Type elemType = dom.getType(xmlNode);
390
391 if (elemType == SkDOM::kText_Type) {
392 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400393 // TODO: add type conversion helper to SkSVGNode
394 if (ctx.fParent->tag() == SkSVGTag::kText) {
395 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
396 }
fmalita6ceef3d2016-07-26 18:46:34 -0700397 return nullptr;
398 }
399
400 SkASSERT(elemType == SkDOM::kElement_Type);
401
402 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
403 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
404 elem, sizeof(gTagFactories[0]));
405 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700406#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700407 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700408#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700409 return nullptr;
410 }
411
412 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
413 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700414 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700415
416 ConstructionContext localCtx(ctx, node);
417 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
418 child = dom.getNextSibling(child)) {
419 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
420 if (childNode) {
421 node->appendChild(std::move(childNode));
422 }
423 }
424
425 return node;
426}
427
428} // anonymous namespace
429
fmalitae1baa7c2016-09-14 12:04:30 -0700430SkSVGDOM::SkSVGDOM()
431 : fContainerSize(SkSize::Make(0, 0)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700432}
433
fmalitae1baa7c2016-09-14 12:04:30 -0700434sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
435 sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
fmalita6ceef3d2016-07-26 18:46:34 -0700436
fmalita28d5b722016-09-12 17:06:47 -0700437 ConstructionContext ctx(&dom->fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700438 dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
439
fmalitae1baa7c2016-09-14 12:04:30 -0700440 // Reset the default container size to match the intrinsic SVG size.
441 dom->setContainerSize(dom->intrinsicSize());
442
fmalita6ceef3d2016-07-26 18:46:34 -0700443 return dom;
444}
445
fmalitae1baa7c2016-09-14 12:04:30 -0700446sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
fmalita6ceef3d2016-07-26 18:46:34 -0700447 SkDOM xmlDom;
448 if (!xmlDom.build(svgStream)) {
449 return nullptr;
450 }
451
fmalitae1baa7c2016-09-14 12:04:30 -0700452 return MakeFromDOM(xmlDom);
fmalita6ceef3d2016-07-26 18:46:34 -0700453}
454
455void SkSVGDOM::render(SkCanvas* canvas) const {
456 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400457 SkSVGLengthContext lctx(fContainerSize);
458 SkSVGPresentationContext pctx;
Tyler Denniston53281c72020-10-22 15:54:24 -0400459 fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700460 }
461}
462
fmalitae1baa7c2016-09-14 12:04:30 -0700463SkSize SkSVGDOM::intrinsicSize() const {
464 if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
465 return SkSize::Make(0, 0);
466 }
467
468 // Intrinsic sizes are never relative, so the viewport size is irrelevant.
469 const SkSVGLengthContext lctx(SkSize::Make(0, 0));
470 return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
471}
472
473const SkSize& SkSVGDOM::containerSize() const {
474 return fContainerSize;
475}
476
fmalita6ceef3d2016-07-26 18:46:34 -0700477void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
478 // TODO: inval
479 fContainerSize = containerSize;
480}
fmalitaca39d712016-08-12 13:17:11 -0700481
Tyler Freemanc9911522020-05-08 13:23:10 -0700482sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
483 SkString idStr(id);
484 return this->fIDMapper.find(idStr);
485}
486
fmalitaca39d712016-08-12 13:17:11 -0700487void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
488 fRoot = std::move(root);
489}
Tyler Freemanc9911522020-05-08 13:23:10 -0700490
491// TODO(fuego): move this to SkSVGNode or its own CU.
492bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
493 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
494}