blob: 12a1918d0dc27c84ae62020b282c50f59d4d23c0 [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 Dennistona25e1a32021-01-15 12:38:29 -050019#include "modules/svg/include/SkSVGFeBlend.h"
Tyler Denniston70bb18d2020-11-06 12:07:53 -050020#include "modules/svg/include/SkSVGFeColorMatrix.h"
Tyler Dennistonb25caae2020-11-09 12:46:02 -050021#include "modules/svg/include/SkSVGFeComposite.h"
Tyler Dennistonf005c8a2021-01-25 12:56:13 -050022#include "modules/svg/include/SkSVGFeDisplacementMap.h"
Tyler Denniston8ed04432020-12-10 15:51:04 -050023#include "modules/svg/include/SkSVGFeFlood.h"
Tyler Denniston187d8112021-01-12 09:34:23 -050024#include "modules/svg/include/SkSVGFeGaussianBlur.h"
Tyler Denniston32b30892021-01-26 14:36:32 -050025#include "modules/svg/include/SkSVGFeLightSource.h"
Tyler Denniston8eedcd22021-01-27 09:18:06 -050026#include "modules/svg/include/SkSVGFeLighting.h"
Tyler Denniston4c89481be2021-01-20 09:41:22 -050027#include "modules/svg/include/SkSVGFeMorphology.h"
Tyler Denniston5878ece2021-01-15 09:17:55 -050028#include "modules/svg/include/SkSVGFeOffset.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050029#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040030#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040031#include "modules/svg/include/SkSVGG.h"
Tyler Denniston8ca46262021-02-02 16:16:21 -050032#include "modules/svg/include/SkSVGImage.h"
Florin Malitab3418102020-10-15 18:10:29 -040033#include "modules/svg/include/SkSVGLine.h"
34#include "modules/svg/include/SkSVGLinearGradient.h"
Florin Malita836c2ca2021-01-13 11:48:02 -050035#include "modules/svg/include/SkSVGMask.h"
Florin Malitab3418102020-10-15 18:10:29 -040036#include "modules/svg/include/SkSVGNode.h"
37#include "modules/svg/include/SkSVGPath.h"
38#include "modules/svg/include/SkSVGPattern.h"
39#include "modules/svg/include/SkSVGPoly.h"
40#include "modules/svg/include/SkSVGRadialGradient.h"
41#include "modules/svg/include/SkSVGRect.h"
42#include "modules/svg/include/SkSVGRenderContext.h"
43#include "modules/svg/include/SkSVGSVG.h"
44#include "modules/svg/include/SkSVGStop.h"
45#include "modules/svg/include/SkSVGText.h"
46#include "modules/svg/include/SkSVGTypes.h"
47#include "modules/svg/include/SkSVGUse.h"
48#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040049#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070051
52namespace {
53
fmalita28d5b722016-09-12 17:06:47 -070054bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
55 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050056 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
57 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070058 return false;
59 }
60
Tyler Dennistone71f5472021-01-27 13:30:59 -050061 node->setAttribute(attr, SkSVGStringValue(parseResult->iri()));
fmalita28d5b722016-09-12 17:06:47 -070062 return true;
63}
64
fmalita6ceef3d2016-07-26 18:46:34 -070065bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
66 const char* stringValue) {
67 SkPath path;
68 if (!SkParsePath::FromSVGString(stringValue, &path)) {
69 return false;
70 }
71
Florin Malitaf4403e72020-04-10 14:14:04 +000072 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070073 return true;
74}
75
Xavier Phane29cdaf2020-03-26 16:15:14 +000076bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
77 const char* stringValue) {
78 SkString str(stringValue, strlen(stringValue));
79 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000080 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000081 return true;
82}
83
fmalita6ceef3d2016-07-26 18:46:34 -070084bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
85 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050086 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
87 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070088 return false;
89 }
90
Tyler Dennistona0a51462020-11-10 13:13:28 -050091 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070092 return true;
93}
94
fmalitabffc2562016-08-03 10:21:11 -070095bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
96 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050097 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
98 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -070099 return false;
100 }
101
Tyler Dennistona0a51462020-11-10 13:13:28 -0500102 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -0700103 return true;
104}
105
fmalita397a5172016-08-08 11:38:55 -0700106bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
107 const char* stringValue) {
108 SkSVGViewBoxType viewBox;
109 SkSVGAttributeParser parser(stringValue);
110 if (!parser.parseViewBox(&viewBox)) {
111 return false;
112 }
113
Florin Malitaf4403e72020-04-10 14:14:04 +0000114 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700115 return true;
116}
117
Tyler Denniston30e327e2020-10-29 16:29:22 -0400118bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
119 SkSVGAttribute attr,
120 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500121 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
122 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400123 return false;
124 }
125
Tyler Dennistona0a51462020-11-10 13:13:28 -0500126 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400127 return true;
128}
129
Florin Malita385e7442020-10-21 16:55:46 -0400130bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
131 const char* stringValue) {
132 SkSVGPreserveAspectRatio par;
133 SkSVGAttributeParser parser(stringValue);
134 if (!parser.parsePreserveAspectRatio(&par)) {
135 return false;
136 }
137
138 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
139 return true;
140}
141
fmalita61f36b32016-08-08 13:58:50 -0700142SkString TrimmedString(const char* first, const char* last) {
143 SkASSERT(first);
144 SkASSERT(last);
145 SkASSERT(first <= last);
146
147 while (first <= last && *first <= ' ') { first++; }
148 while (first <= last && *last <= ' ') { last--; }
149
150 SkASSERT(last - first + 1 >= 0);
151 return SkString(first, SkTo<size_t>(last - first + 1));
152}
153
fmalita58649cc2016-07-29 08:52:03 -0700154// Breaks a "foo: bar; baz: ..." string into key:value pairs.
155class StyleIterator {
156public:
157 StyleIterator(const char* str) : fPos(str) { }
158
159 std::tuple<SkString, SkString> next() {
160 SkString name, value;
161
162 if (fPos) {
163 const char* sep = this->nextSeparator();
164 SkASSERT(*sep == ';' || *sep == '\0');
165
166 const char* valueSep = strchr(fPos, ':');
167 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700168 name = TrimmedString(fPos, valueSep - 1);
169 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700170 }
171
172 fPos = *sep ? sep + 1 : nullptr;
173 }
174
175 return std::make_tuple(name, value);
176 }
177
178private:
179 const char* nextSeparator() const {
180 const char* sep = fPos;
181 while (*sep != ';' && *sep != '\0') {
182 sep++;
183 }
184 return sep;
185 }
186
187 const char* fPos;
188};
189
Tyler Freemanc9911522020-05-08 13:23:10 -0700190bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700191
192bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
193 const char* stringValue) {
194
195 SkString name, value;
196 StyleIterator iter(stringValue);
197 for (;;) {
198 std::tie(name, value) = iter.next();
199 if (name.isEmpty()) {
200 break;
201 }
202 set_string_attribute(node, name.c_str(), value.c_str());
203 }
204
205 return true;
206}
207
fmalita6ceef3d2016-07-26 18:46:34 -0700208template<typename T>
209struct SortedDictionaryEntry {
210 const char* fKey;
211 const T fValue;
212};
213
214struct AttrParseInfo {
215 SkSVGAttribute fAttr;
216 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
217};
218
219SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400220 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
221 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
222 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400223 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
224 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400225 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400226 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
227 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400228 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400229 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
230 SetPreserveAspectRatioAttribute }},
231 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
232 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
233 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400234 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
235 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400236 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
237 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400238 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
239 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
240 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
241 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
242 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
243 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
244 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
245 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700246};
247
248SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Tyler Denniston8eedcd22021-01-27 09:18:06 -0500249 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
250 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
251 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
252 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
253 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
254 { "feBlend" , []() -> sk_sp<SkSVGNode> { return SkSVGFeBlend::Make(); }},
255 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
256 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
257 { "feDisplacementMap" , []() -> sk_sp<SkSVGNode> { return SkSVGFeDisplacementMap::Make(); }},
258 { "feDistantLight" , []() -> sk_sp<SkSVGNode> { return SkSVGFeDistantLight::Make(); }},
259 { "feFlood" , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make(); }},
260 { "feGaussianBlur" , []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
261 { "feMorphology" , []() -> sk_sp<SkSVGNode> { return SkSVGFeMorphology::Make(); }},
262 { "feOffset" , []() -> sk_sp<SkSVGNode> { return SkSVGFeOffset::Make(); }},
263 { "fePointLight" , []() -> sk_sp<SkSVGNode> { return SkSVGFePointLight::Make(); }},
264 { "feSpecularLighting", []() -> sk_sp<SkSVGNode> { return SkSVGFeSpecularLighting::Make(); }},
265 { "feSpotLight" , []() -> sk_sp<SkSVGNode> { return SkSVGFeSpotLight::Make(); }},
266 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
267 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
268 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
Tyler Denniston8ca46262021-02-02 16:16:21 -0500269 { "image" , []() -> sk_sp<SkSVGNode> { return SkSVGImage::Make(); }},
Tyler Denniston8eedcd22021-01-27 09:18:06 -0500270 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
271 { "linearGradient" , []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
272 { "mask" , []() -> sk_sp<SkSVGNode> { return SkSVGMask::Make(); }},
273 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
274 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
275 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
276 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
277 { "radialGradient" , []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
278 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
279 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
Florin Malitacdeabca2021-01-20 13:21:20 -0500280// "svg" handled explicitly
Tyler Denniston8eedcd22021-01-27 09:18:06 -0500281 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
282 { "textPath" , []() -> sk_sp<SkSVGNode> { return SkSVGTextPath::Make(); }},
283 { "tspan" , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make(); }},
284 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700285};
286
287struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700288 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700289 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700290 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700291
Florin Malita39fe8c82020-10-20 10:43:03 -0400292 SkSVGNode* fParent;
293 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700294};
295
Tyler Freemanc9911522020-05-08 13:23:10 -0700296bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500297 if (node->parseAndSetAttribute(name, value)) {
298 // Handled by new code path
299 return true;
300 }
301
fmalita58649cc2016-07-29 08:52:03 -0700302 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
303 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
304 name, sizeof(gAttributeParseInfo[0]));
305 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700306#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700307 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700308#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700309 return false;
fmalita58649cc2016-07-29 08:52:03 -0700310 }
311
312 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
313 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
314 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700315#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700316 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700317#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700318 return false;
fmalita58649cc2016-07-29 08:52:03 -0700319 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700320
321 return true;
fmalita58649cc2016-07-29 08:52:03 -0700322}
323
fmalita6ceef3d2016-07-26 18:46:34 -0700324void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700325 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700326 const char* name, *value;
327 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
328 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700329 // We're handling id attributes out of band for now.
330 if (!strcmp(name, "id")) {
331 mapper->set(SkString(value), svgNode);
332 continue;
333 }
fmalita58649cc2016-07-29 08:52:03 -0700334 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700335 }
336}
337
338sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
339 const SkDOM::Node* xmlNode) {
340 const char* elem = dom.getName(xmlNode);
341 const SkDOM::Type elemType = dom.getType(xmlNode);
342
343 if (elemType == SkDOM::kText_Type) {
Florin Malita512ff752020-12-06 11:50:52 -0500344 // Text literals require special handling.
fmalita6ceef3d2016-07-26 18:46:34 -0700345 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita512ff752020-12-06 11:50:52 -0500346 auto txt = SkSVGTextLiteral::Make();
347 txt->setText(SkString(dom.getName(xmlNode)));
348 ctx.fParent->appendChild(std::move(txt));
349
fmalita6ceef3d2016-07-26 18:46:34 -0700350 return nullptr;
351 }
352
353 SkASSERT(elemType == SkDOM::kElement_Type);
354
Florin Malitacdeabca2021-01-20 13:21:20 -0500355 auto make_node = [](const ConstructionContext& ctx, const char* elem) -> sk_sp<SkSVGNode> {
356 if (strcmp(elem, "svg") == 0) {
357 // Outermost SVG element must be tagged as such.
358 return SkSVGSVG::Make(ctx.fParent ? SkSVGSVG::Type::kInner
359 : SkSVGSVG::Type::kRoot);
360 }
361
362 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
363 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
364 elem, sizeof(gTagFactories[0]));
365 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700366#if defined(SK_VERBOSE_SVG_PARSING)
Florin Malitacdeabca2021-01-20 13:21:20 -0500367 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700368#endif
Florin Malitacdeabca2021-01-20 13:21:20 -0500369 return nullptr;
370 }
371 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
372
373 return gTagFactories[tagIndex].fValue();
374 };
375
376 auto node = make_node(ctx, elem);
377 if (!node) {
fmalita6ceef3d2016-07-26 18:46:34 -0700378 return nullptr;
379 }
380
fmalita28d5b722016-09-12 17:06:47 -0700381 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700382
383 ConstructionContext localCtx(ctx, node);
384 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
385 child = dom.getNextSibling(child)) {
386 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
387 if (childNode) {
388 node->appendChild(std::move(childNode));
389 }
390 }
391
392 return node;
393}
394
395} // anonymous namespace
396
Florin Malita7006e152020-11-10 15:24:59 -0500397SkSVGDOM::Builder& SkSVGDOM::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
398 fFontMgr = std::move(fmgr);
399 return *this;
fmalita6ceef3d2016-07-26 18:46:34 -0700400}
401
Florin Malita24df67d2021-01-26 18:45:34 -0500402SkSVGDOM::Builder& SkSVGDOM::Builder::setResourceProvider(sk_sp<skresources::ResourceProvider> rp) {
403 fResourceProvider = std::move(rp);
404 return *this;
405}
406
Florin Malita7006e152020-11-10 15:24:59 -0500407sk_sp<SkSVGDOM> SkSVGDOM::Builder::make(SkStream& str) const {
fmalita6ceef3d2016-07-26 18:46:34 -0700408 SkDOM xmlDom;
Florin Malita7006e152020-11-10 15:24:59 -0500409 if (!xmlDom.build(str)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700410 return nullptr;
411 }
412
Florin Malita7006e152020-11-10 15:24:59 -0500413 SkSVGIDMapper mapper;
414 ConstructionContext ctx(&mapper);
415
416 auto root = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
417 if (!root || root->tag() != SkSVGTag::kSvg) {
418 return nullptr;
419 }
420
Florin Malita24df67d2021-01-26 18:45:34 -0500421 class NullResourceProvider final : public skresources::ResourceProvider {
422 sk_sp<SkData> load(const char[], const char[]) const override { return nullptr; }
423 };
424
425 auto resource_provider = fResourceProvider ? fResourceProvider
426 : sk_make_sp<NullResourceProvider>();
427
Florin Malita7006e152020-11-10 15:24:59 -0500428 return sk_sp<SkSVGDOM>(new SkSVGDOM(sk_sp<SkSVGSVG>(static_cast<SkSVGSVG*>(root.release())),
Florin Malita24df67d2021-01-26 18:45:34 -0500429 std::move(fFontMgr), std::move(resource_provider),
430 std::move(mapper)));
fmalita6ceef3d2016-07-26 18:46:34 -0700431}
432
Florin Malita24df67d2021-01-26 18:45:34 -0500433SkSVGDOM::SkSVGDOM(sk_sp<SkSVGSVG> root, sk_sp<SkFontMgr> fmgr,
434 sk_sp<skresources::ResourceProvider> rp, SkSVGIDMapper&& mapper)
Florin Malita7006e152020-11-10 15:24:59 -0500435 : fRoot(std::move(root))
436 , fFontMgr(std::move(fmgr))
Florin Malita24df67d2021-01-26 18:45:34 -0500437 , fResourceProvider(std::move(rp))
Florin Malita7006e152020-11-10 15:24:59 -0500438 , fIDMapper(std::move(mapper))
439 , fContainerSize(fRoot->intrinsicSize(SkSVGLengthContext(SkSize::Make(0, 0))))
Florin Malita24df67d2021-01-26 18:45:34 -0500440{
441 SkASSERT(fResourceProvider);
442}
Florin Malita7006e152020-11-10 15:24:59 -0500443
fmalita6ceef3d2016-07-26 18:46:34 -0700444void SkSVGDOM::render(SkCanvas* canvas) const {
445 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400446 SkSVGLengthContext lctx(fContainerSize);
447 SkSVGPresentationContext pctx;
Florin Malita24df67d2021-01-26 18:45:34 -0500448 fRoot->render(SkSVGRenderContext(canvas, fFontMgr, fResourceProvider, fIDMapper, lctx, pctx,
449 nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700450 }
451}
452
fmalitae1baa7c2016-09-14 12:04:30 -0700453const SkSize& SkSVGDOM::containerSize() const {
454 return fContainerSize;
455}
456
fmalita6ceef3d2016-07-26 18:46:34 -0700457void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
458 // TODO: inval
459 fContainerSize = containerSize;
460}
fmalitaca39d712016-08-12 13:17:11 -0700461
Tyler Freemanc9911522020-05-08 13:23:10 -0700462sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
463 SkString idStr(id);
464 return this->fIDMapper.find(idStr);
465}
466
Tyler Freemanc9911522020-05-08 13:23:10 -0700467// TODO(fuego): move this to SkSVGNode or its own CU.
468bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
469 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
470}