blob: 356de27e0ec8c7b8864d4b9fff29eae84ea81c90 [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"
Florin Malita7006e152020-11-10 15:24:59 -05009#include "include/core/SkFontMgr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTo.h"
12#include "include/utils/SkParsePath.h"
Florin Malitab3418102020-10-15 18:10:29 -040013#include "modules/svg/include/SkSVGAttributeParser.h"
14#include "modules/svg/include/SkSVGCircle.h"
15#include "modules/svg/include/SkSVGClipPath.h"
16#include "modules/svg/include/SkSVGDOM.h"
17#include "modules/svg/include/SkSVGDefs.h"
18#include "modules/svg/include/SkSVGEllipse.h"
Tyler Denniston70bb18d2020-11-06 12:07:53 -050019#include "modules/svg/include/SkSVGFeColorMatrix.h"
Tyler Dennistonb25caae2020-11-09 12:46:02 -050020#include "modules/svg/include/SkSVGFeComposite.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050021#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040022#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040023#include "modules/svg/include/SkSVGG.h"
24#include "modules/svg/include/SkSVGLine.h"
25#include "modules/svg/include/SkSVGLinearGradient.h"
26#include "modules/svg/include/SkSVGNode.h"
27#include "modules/svg/include/SkSVGPath.h"
28#include "modules/svg/include/SkSVGPattern.h"
29#include "modules/svg/include/SkSVGPoly.h"
30#include "modules/svg/include/SkSVGRadialGradient.h"
31#include "modules/svg/include/SkSVGRect.h"
32#include "modules/svg/include/SkSVGRenderContext.h"
33#include "modules/svg/include/SkSVGSVG.h"
34#include "modules/svg/include/SkSVGStop.h"
35#include "modules/svg/include/SkSVGText.h"
36#include "modules/svg/include/SkSVGTypes.h"
37#include "modules/svg/include/SkSVGUse.h"
38#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040039#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070041
42namespace {
43
fmalita28d5b722016-09-12 17:06:47 -070044bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
45 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050046 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
47 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070048 return false;
49 }
50
Tyler Dennistona0a51462020-11-10 13:13:28 -050051 node->setAttribute(attr, SkSVGStringValue(parseResult->fIRI));
fmalita28d5b722016-09-12 17:06:47 -070052 return true;
53}
54
fmalita6ceef3d2016-07-26 18:46:34 -070055bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
56 const char* stringValue) {
57 SkPath path;
58 if (!SkParsePath::FromSVGString(stringValue, &path)) {
59 return false;
60 }
61
Florin Malitaf4403e72020-04-10 14:14:04 +000062 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070063 return true;
64}
65
Xavier Phane29cdaf2020-03-26 16:15:14 +000066bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
67 const char* stringValue) {
68 SkString str(stringValue, strlen(stringValue));
69 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000070 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000071 return true;
72}
73
fmalita6ceef3d2016-07-26 18:46:34 -070074bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
75 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050076 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
77 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070078 return false;
79 }
80
Tyler Dennistona0a51462020-11-10 13:13:28 -050081 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070082 return true;
83}
84
fmalitabffc2562016-08-03 10:21:11 -070085bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
86 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050087 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
88 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -070089 return false;
90 }
91
Tyler Dennistona0a51462020-11-10 13:13:28 -050092 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -070093 return true;
94}
95
fmalita2d961e02016-08-11 09:16:29 -070096bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
97 const char* stringValue) {
Tyler Denniston4c6f57a2020-11-30 15:31:32 -050098 auto parseResult = SkSVGAttributeParser::parse<SkSVGNumberType>(stringValue);
99 if (!parseResult.isValid()) {
fmalita2d961e02016-08-11 09:16:29 -0700100 return false;
101 }
102
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500103 node->setAttribute(attr, SkSVGNumberValue(*parseResult));
fmalita2d961e02016-08-11 09:16:29 -0700104 return true;
105}
106
fmalita397a5172016-08-08 11:38:55 -0700107bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
108 const char* stringValue) {
109 SkSVGViewBoxType viewBox;
110 SkSVGAttributeParser parser(stringValue);
111 if (!parser.parseViewBox(&viewBox)) {
112 return false;
113 }
114
Florin Malitaf4403e72020-04-10 14:14:04 +0000115 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700116 return true;
117}
118
Tyler Denniston308c0722020-04-14 10:53:41 -0400119bool SetStopColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
120 const char* stringValue) {
121 SkSVGStopColor stopColor;
122 SkSVGAttributeParser parser(stringValue);
123 if (!parser.parseStopColor(&stopColor)) {
124 return false;
125 }
126
127 node->setAttribute(attr, SkSVGStopColorValue(stopColor));
128 return true;
129}
130
Tyler Denniston30e327e2020-10-29 16:29:22 -0400131bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
132 SkSVGAttribute attr,
133 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500134 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
135 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400136 return false;
137 }
138
Tyler Dennistona0a51462020-11-10 13:13:28 -0500139 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400140 return true;
141}
142
fmalita5b31f322016-08-12 12:15:33 -0700143bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
144 const char* stringValue) {
145 SkSVGPointsType points;
146 SkSVGAttributeParser parser(stringValue);
147 if (!parser.parsePoints(&points)) {
148 return false;
149 }
150
Florin Malitaf4403e72020-04-10 14:14:04 +0000151 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700152 return true;
153}
154
Florin Malita385e7442020-10-21 16:55:46 -0400155bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
156 const char* stringValue) {
157 SkSVGPreserveAspectRatio par;
158 SkSVGAttributeParser parser(stringValue);
159 if (!parser.parsePreserveAspectRatio(&par)) {
160 return false;
161 }
162
163 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
164 return true;
165}
166
fmalita61f36b32016-08-08 13:58:50 -0700167SkString TrimmedString(const char* first, const char* last) {
168 SkASSERT(first);
169 SkASSERT(last);
170 SkASSERT(first <= last);
171
172 while (first <= last && *first <= ' ') { first++; }
173 while (first <= last && *last <= ' ') { last--; }
174
175 SkASSERT(last - first + 1 >= 0);
176 return SkString(first, SkTo<size_t>(last - first + 1));
177}
178
fmalita58649cc2016-07-29 08:52:03 -0700179// Breaks a "foo: bar; baz: ..." string into key:value pairs.
180class StyleIterator {
181public:
182 StyleIterator(const char* str) : fPos(str) { }
183
184 std::tuple<SkString, SkString> next() {
185 SkString name, value;
186
187 if (fPos) {
188 const char* sep = this->nextSeparator();
189 SkASSERT(*sep == ';' || *sep == '\0');
190
191 const char* valueSep = strchr(fPos, ':');
192 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700193 name = TrimmedString(fPos, valueSep - 1);
194 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700195 }
196
197 fPos = *sep ? sep + 1 : nullptr;
198 }
199
200 return std::make_tuple(name, value);
201 }
202
203private:
204 const char* nextSeparator() const {
205 const char* sep = fPos;
206 while (*sep != ';' && *sep != '\0') {
207 sep++;
208 }
209 return sep;
210 }
211
212 const char* fPos;
213};
214
Tyler Freemanc9911522020-05-08 13:23:10 -0700215bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700216
217bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
218 const char* stringValue) {
219
220 SkString name, value;
221 StyleIterator iter(stringValue);
222 for (;;) {
223 std::tie(name, value) = iter.next();
224 if (name.isEmpty()) {
225 break;
226 }
227 set_string_attribute(node, name.c_str(), value.c_str());
228 }
229
230 return true;
231}
232
fmalita6ceef3d2016-07-26 18:46:34 -0700233template<typename T>
234struct SortedDictionaryEntry {
235 const char* fKey;
236 const T fValue;
237};
238
239struct AttrParseInfo {
240 SkSVGAttribute fAttr;
241 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
242};
243
244SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400245 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
246 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
247 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400248 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
249 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400250 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400251 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
252 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400253 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
254 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400255 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
256 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
257 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
258 SetPreserveAspectRatioAttribute }},
259 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
260 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
261 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400262 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
263 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400264 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
265 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400266 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
267 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400268 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
269 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
270 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
271 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
272 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
273 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
274 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
275 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700276};
277
278SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400279 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700280 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500281 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700282 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
283 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
Tyler Denniston70bb18d2020-11-06 12:07:53 -0500284 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
Tyler Dennistonb25caae2020-11-09 12:46:02 -0500285 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
Tyler Dennistondada9602020-11-03 10:04:25 -0500286 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400287 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700288 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
289 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
290 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
291 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400292 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700293 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
294 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400295 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700296 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
297 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
298 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000299 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita512ff752020-12-06 11:50:52 -0500300 { "tspan" , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400301 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700302};
303
304struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700305 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700306 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700307 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700308
Florin Malita39fe8c82020-10-20 10:43:03 -0400309 SkSVGNode* fParent;
310 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700311};
312
Tyler Freemanc9911522020-05-08 13:23:10 -0700313bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500314 if (node->parseAndSetAttribute(name, value)) {
315 // Handled by new code path
316 return true;
317 }
318
fmalita58649cc2016-07-29 08:52:03 -0700319 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
320 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
321 name, sizeof(gAttributeParseInfo[0]));
322 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700323#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700324 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700325#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700326 return false;
fmalita58649cc2016-07-29 08:52:03 -0700327 }
328
329 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
330 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
331 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700332#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700333 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700334#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700335 return false;
fmalita58649cc2016-07-29 08:52:03 -0700336 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700337
338 return true;
fmalita58649cc2016-07-29 08:52:03 -0700339}
340
fmalita6ceef3d2016-07-26 18:46:34 -0700341void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700342 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700343 const char* name, *value;
344 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
345 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700346 // We're handling id attributes out of band for now.
347 if (!strcmp(name, "id")) {
348 mapper->set(SkString(value), svgNode);
349 continue;
350 }
fmalita58649cc2016-07-29 08:52:03 -0700351 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700352 }
353}
354
355sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
356 const SkDOM::Node* xmlNode) {
357 const char* elem = dom.getName(xmlNode);
358 const SkDOM::Type elemType = dom.getType(xmlNode);
359
360 if (elemType == SkDOM::kText_Type) {
Florin Malita512ff752020-12-06 11:50:52 -0500361 // Text literals require special handling.
fmalita6ceef3d2016-07-26 18:46:34 -0700362 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita512ff752020-12-06 11:50:52 -0500363 auto txt = SkSVGTextLiteral::Make();
364 txt->setText(SkString(dom.getName(xmlNode)));
365 ctx.fParent->appendChild(std::move(txt));
366
fmalita6ceef3d2016-07-26 18:46:34 -0700367 return nullptr;
368 }
369
370 SkASSERT(elemType == SkDOM::kElement_Type);
371
372 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
373 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
374 elem, sizeof(gTagFactories[0]));
375 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700376#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700377 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700378#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700379 return nullptr;
380 }
381
382 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
383 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700384 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700385
386 ConstructionContext localCtx(ctx, node);
387 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
388 child = dom.getNextSibling(child)) {
389 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
390 if (childNode) {
391 node->appendChild(std::move(childNode));
392 }
393 }
394
395 return node;
396}
397
398} // anonymous namespace
399
Florin Malita7006e152020-11-10 15:24:59 -0500400SkSVGDOM::Builder& SkSVGDOM::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
401 fFontMgr = std::move(fmgr);
402 return *this;
fmalita6ceef3d2016-07-26 18:46:34 -0700403}
404
Florin Malita7006e152020-11-10 15:24:59 -0500405sk_sp<SkSVGDOM> SkSVGDOM::Builder::make(SkStream& str) const {
fmalita6ceef3d2016-07-26 18:46:34 -0700406 SkDOM xmlDom;
Florin Malita7006e152020-11-10 15:24:59 -0500407 if (!xmlDom.build(str)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700408 return nullptr;
409 }
410
Florin Malita7006e152020-11-10 15:24:59 -0500411 SkSVGIDMapper mapper;
412 ConstructionContext ctx(&mapper);
413
414 auto root = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
415 if (!root || root->tag() != SkSVGTag::kSvg) {
416 return nullptr;
417 }
418
419 return sk_sp<SkSVGDOM>(new SkSVGDOM(sk_sp<SkSVGSVG>(static_cast<SkSVGSVG*>(root.release())),
420 std::move(fFontMgr), std::move(mapper)));
fmalita6ceef3d2016-07-26 18:46:34 -0700421}
422
Florin Malita7006e152020-11-10 15:24:59 -0500423SkSVGDOM::SkSVGDOM(sk_sp<SkSVGSVG> root, sk_sp<SkFontMgr> fmgr, SkSVGIDMapper&& mapper)
424 : fRoot(std::move(root))
425 , fFontMgr(std::move(fmgr))
426 , fIDMapper(std::move(mapper))
427 , fContainerSize(fRoot->intrinsicSize(SkSVGLengthContext(SkSize::Make(0, 0))))
428{}
429
fmalita6ceef3d2016-07-26 18:46:34 -0700430void SkSVGDOM::render(SkCanvas* canvas) const {
431 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400432 SkSVGLengthContext lctx(fContainerSize);
433 SkSVGPresentationContext pctx;
Florin Malita512ff752020-12-06 11:50:52 -0500434 fRoot->render(SkSVGRenderContext(canvas, fFontMgr, fIDMapper,
435 lctx, pctx, nullptr, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700436 }
437}
438
fmalitae1baa7c2016-09-14 12:04:30 -0700439const SkSize& SkSVGDOM::containerSize() const {
440 return fContainerSize;
441}
442
fmalita6ceef3d2016-07-26 18:46:34 -0700443void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
444 // TODO: inval
445 fContainerSize = containerSize;
446}
fmalitaca39d712016-08-12 13:17:11 -0700447
Tyler Freemanc9911522020-05-08 13:23:10 -0700448sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
449 SkString idStr(id);
450 return this->fIDMapper.find(idStr);
451}
452
Tyler Freemanc9911522020-05-08 13:23:10 -0700453// TODO(fuego): move this to SkSVGNode or its own CU.
454bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
455 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
456}