blob: 08fea1f1c421413bceb80d014c728797a98bca08 [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 Denniston4c89481be2021-01-20 09:41:22 -050025#include "modules/svg/include/SkSVGFeMorphology.h"
Tyler Denniston5878ece2021-01-15 09:17:55 -050026#include "modules/svg/include/SkSVGFeOffset.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050027#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040028#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040029#include "modules/svg/include/SkSVGG.h"
30#include "modules/svg/include/SkSVGLine.h"
31#include "modules/svg/include/SkSVGLinearGradient.h"
Florin Malita836c2ca2021-01-13 11:48:02 -050032#include "modules/svg/include/SkSVGMask.h"
Florin Malitab3418102020-10-15 18:10:29 -040033#include "modules/svg/include/SkSVGNode.h"
34#include "modules/svg/include/SkSVGPath.h"
35#include "modules/svg/include/SkSVGPattern.h"
36#include "modules/svg/include/SkSVGPoly.h"
37#include "modules/svg/include/SkSVGRadialGradient.h"
38#include "modules/svg/include/SkSVGRect.h"
39#include "modules/svg/include/SkSVGRenderContext.h"
40#include "modules/svg/include/SkSVGSVG.h"
41#include "modules/svg/include/SkSVGStop.h"
42#include "modules/svg/include/SkSVGText.h"
43#include "modules/svg/include/SkSVGTypes.h"
44#include "modules/svg/include/SkSVGUse.h"
45#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040046#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070048
49namespace {
50
fmalita28d5b722016-09-12 17:06:47 -070051bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
52 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050053 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
54 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070055 return false;
56 }
57
Tyler Dennistona0a51462020-11-10 13:13:28 -050058 node->setAttribute(attr, SkSVGStringValue(parseResult->fIRI));
fmalita28d5b722016-09-12 17:06:47 -070059 return true;
60}
61
fmalita6ceef3d2016-07-26 18:46:34 -070062bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
63 const char* stringValue) {
64 SkPath path;
65 if (!SkParsePath::FromSVGString(stringValue, &path)) {
66 return false;
67 }
68
Florin Malitaf4403e72020-04-10 14:14:04 +000069 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070070 return true;
71}
72
Xavier Phane29cdaf2020-03-26 16:15:14 +000073bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
74 const char* stringValue) {
75 SkString str(stringValue, strlen(stringValue));
76 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000077 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000078 return true;
79}
80
fmalita6ceef3d2016-07-26 18:46:34 -070081bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
82 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050083 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
84 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070085 return false;
86 }
87
Tyler Dennistona0a51462020-11-10 13:13:28 -050088 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070089 return true;
90}
91
fmalitabffc2562016-08-03 10:21:11 -070092bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
93 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050094 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
95 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -070096 return false;
97 }
98
Tyler Dennistona0a51462020-11-10 13:13:28 -050099 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -0700100 return true;
101}
102
fmalita397a5172016-08-08 11:38:55 -0700103bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
104 const char* stringValue) {
105 SkSVGViewBoxType viewBox;
106 SkSVGAttributeParser parser(stringValue);
107 if (!parser.parseViewBox(&viewBox)) {
108 return false;
109 }
110
Florin Malitaf4403e72020-04-10 14:14:04 +0000111 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700112 return true;
113}
114
Tyler Denniston30e327e2020-10-29 16:29:22 -0400115bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
116 SkSVGAttribute attr,
117 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500118 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
119 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400120 return false;
121 }
122
Tyler Dennistona0a51462020-11-10 13:13:28 -0500123 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400124 return true;
125}
126
fmalita5b31f322016-08-12 12:15:33 -0700127bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
128 const char* stringValue) {
129 SkSVGPointsType points;
130 SkSVGAttributeParser parser(stringValue);
131 if (!parser.parsePoints(&points)) {
132 return false;
133 }
134
Florin Malitaf4403e72020-04-10 14:14:04 +0000135 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700136 return true;
137}
138
Florin Malita385e7442020-10-21 16:55:46 -0400139bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
140 const char* stringValue) {
141 SkSVGPreserveAspectRatio par;
142 SkSVGAttributeParser parser(stringValue);
143 if (!parser.parsePreserveAspectRatio(&par)) {
144 return false;
145 }
146
147 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
148 return true;
149}
150
fmalita61f36b32016-08-08 13:58:50 -0700151SkString TrimmedString(const char* first, const char* last) {
152 SkASSERT(first);
153 SkASSERT(last);
154 SkASSERT(first <= last);
155
156 while (first <= last && *first <= ' ') { first++; }
157 while (first <= last && *last <= ' ') { last--; }
158
159 SkASSERT(last - first + 1 >= 0);
160 return SkString(first, SkTo<size_t>(last - first + 1));
161}
162
fmalita58649cc2016-07-29 08:52:03 -0700163// Breaks a "foo: bar; baz: ..." string into key:value pairs.
164class StyleIterator {
165public:
166 StyleIterator(const char* str) : fPos(str) { }
167
168 std::tuple<SkString, SkString> next() {
169 SkString name, value;
170
171 if (fPos) {
172 const char* sep = this->nextSeparator();
173 SkASSERT(*sep == ';' || *sep == '\0');
174
175 const char* valueSep = strchr(fPos, ':');
176 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700177 name = TrimmedString(fPos, valueSep - 1);
178 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700179 }
180
181 fPos = *sep ? sep + 1 : nullptr;
182 }
183
184 return std::make_tuple(name, value);
185 }
186
187private:
188 const char* nextSeparator() const {
189 const char* sep = fPos;
190 while (*sep != ';' && *sep != '\0') {
191 sep++;
192 }
193 return sep;
194 }
195
196 const char* fPos;
197};
198
Tyler Freemanc9911522020-05-08 13:23:10 -0700199bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700200
201bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
202 const char* stringValue) {
203
204 SkString name, value;
205 StyleIterator iter(stringValue);
206 for (;;) {
207 std::tie(name, value) = iter.next();
208 if (name.isEmpty()) {
209 break;
210 }
211 set_string_attribute(node, name.c_str(), value.c_str());
212 }
213
214 return true;
215}
216
fmalita6ceef3d2016-07-26 18:46:34 -0700217template<typename T>
218struct SortedDictionaryEntry {
219 const char* fKey;
220 const T fValue;
221};
222
223struct AttrParseInfo {
224 SkSVGAttribute fAttr;
225 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
226};
227
228SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400229 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
230 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
231 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400232 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
233 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400234 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400235 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
236 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400237 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
238 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400239 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
240 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
241 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
242 SetPreserveAspectRatioAttribute }},
243 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
244 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
245 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400246 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
247 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400248 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
249 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400250 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
251 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
252 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
253 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
254 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
255 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
256 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
257 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700258};
259
260SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Tyler Dennistonf005c8a2021-01-25 12:56:13 -0500261 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
262 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
263 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
264 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
265 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
266 { "feBlend" , []() -> sk_sp<SkSVGNode> { return SkSVGFeBlend::Make(); }},
267 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
268 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
269 { "feDisplacementMap", []() -> sk_sp<SkSVGNode> { return SkSVGFeDisplacementMap::Make(); }},
270 { "feFlood" , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make(); }},
271 { "feGaussianBlur" , []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
272 { "feMorphology" , []() -> sk_sp<SkSVGNode> { return SkSVGFeMorphology::Make(); }},
273 { "feOffset" , []() -> sk_sp<SkSVGNode> { return SkSVGFeOffset::Make(); }},
274 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
275 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
276 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
277 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
278 { "linearGradient" , []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
279 { "mask" , []() -> sk_sp<SkSVGNode> { return SkSVGMask::Make(); }},
280 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
281 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
282 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
283 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
284 { "radialGradient" , []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
285 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
286 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
Florin Malitacdeabca2021-01-20 13:21:20 -0500287// "svg" handled explicitly
Tyler Dennistonf005c8a2021-01-25 12:56:13 -0500288 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
289 { "textPath" , []() -> sk_sp<SkSVGNode> { return SkSVGTextPath::Make(); }},
290 { "tspan" , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make(); }},
291 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700292};
293
294struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700295 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700296 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700297 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700298
Florin Malita39fe8c82020-10-20 10:43:03 -0400299 SkSVGNode* fParent;
300 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700301};
302
Tyler Freemanc9911522020-05-08 13:23:10 -0700303bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500304 if (node->parseAndSetAttribute(name, value)) {
305 // Handled by new code path
306 return true;
307 }
308
fmalita58649cc2016-07-29 08:52:03 -0700309 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
310 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
311 name, sizeof(gAttributeParseInfo[0]));
312 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700313#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700314 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700315#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700316 return false;
fmalita58649cc2016-07-29 08:52:03 -0700317 }
318
319 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
320 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
321 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700322#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700323 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700324#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700325 return false;
fmalita58649cc2016-07-29 08:52:03 -0700326 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700327
328 return true;
fmalita58649cc2016-07-29 08:52:03 -0700329}
330
fmalita6ceef3d2016-07-26 18:46:34 -0700331void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700332 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700333 const char* name, *value;
334 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
335 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700336 // We're handling id attributes out of band for now.
337 if (!strcmp(name, "id")) {
338 mapper->set(SkString(value), svgNode);
339 continue;
340 }
fmalita58649cc2016-07-29 08:52:03 -0700341 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700342 }
343}
344
345sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
346 const SkDOM::Node* xmlNode) {
347 const char* elem = dom.getName(xmlNode);
348 const SkDOM::Type elemType = dom.getType(xmlNode);
349
350 if (elemType == SkDOM::kText_Type) {
Florin Malita512ff752020-12-06 11:50:52 -0500351 // Text literals require special handling.
fmalita6ceef3d2016-07-26 18:46:34 -0700352 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita512ff752020-12-06 11:50:52 -0500353 auto txt = SkSVGTextLiteral::Make();
354 txt->setText(SkString(dom.getName(xmlNode)));
355 ctx.fParent->appendChild(std::move(txt));
356
fmalita6ceef3d2016-07-26 18:46:34 -0700357 return nullptr;
358 }
359
360 SkASSERT(elemType == SkDOM::kElement_Type);
361
Florin Malitacdeabca2021-01-20 13:21:20 -0500362 auto make_node = [](const ConstructionContext& ctx, const char* elem) -> sk_sp<SkSVGNode> {
363 if (strcmp(elem, "svg") == 0) {
364 // Outermost SVG element must be tagged as such.
365 return SkSVGSVG::Make(ctx.fParent ? SkSVGSVG::Type::kInner
366 : SkSVGSVG::Type::kRoot);
367 }
368
369 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
370 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
371 elem, sizeof(gTagFactories[0]));
372 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700373#if defined(SK_VERBOSE_SVG_PARSING)
Florin Malitacdeabca2021-01-20 13:21:20 -0500374 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700375#endif
Florin Malitacdeabca2021-01-20 13:21:20 -0500376 return nullptr;
377 }
378 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
379
380 return gTagFactories[tagIndex].fValue();
381 };
382
383 auto node = make_node(ctx, elem);
384 if (!node) {
fmalita6ceef3d2016-07-26 18:46:34 -0700385 return nullptr;
386 }
387
fmalita28d5b722016-09-12 17:06:47 -0700388 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700389
390 ConstructionContext localCtx(ctx, node);
391 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
392 child = dom.getNextSibling(child)) {
393 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
394 if (childNode) {
395 node->appendChild(std::move(childNode));
396 }
397 }
398
399 return node;
400}
401
402} // anonymous namespace
403
Florin Malita7006e152020-11-10 15:24:59 -0500404SkSVGDOM::Builder& SkSVGDOM::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
405 fFontMgr = std::move(fmgr);
406 return *this;
fmalita6ceef3d2016-07-26 18:46:34 -0700407}
408
Florin Malita7006e152020-11-10 15:24:59 -0500409sk_sp<SkSVGDOM> SkSVGDOM::Builder::make(SkStream& str) const {
fmalita6ceef3d2016-07-26 18:46:34 -0700410 SkDOM xmlDom;
Florin Malita7006e152020-11-10 15:24:59 -0500411 if (!xmlDom.build(str)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700412 return nullptr;
413 }
414
Florin Malita7006e152020-11-10 15:24:59 -0500415 SkSVGIDMapper mapper;
416 ConstructionContext ctx(&mapper);
417
418 auto root = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
419 if (!root || root->tag() != SkSVGTag::kSvg) {
420 return nullptr;
421 }
422
423 return sk_sp<SkSVGDOM>(new SkSVGDOM(sk_sp<SkSVGSVG>(static_cast<SkSVGSVG*>(root.release())),
424 std::move(fFontMgr), std::move(mapper)));
fmalita6ceef3d2016-07-26 18:46:34 -0700425}
426
Florin Malita7006e152020-11-10 15:24:59 -0500427SkSVGDOM::SkSVGDOM(sk_sp<SkSVGSVG> root, sk_sp<SkFontMgr> fmgr, SkSVGIDMapper&& mapper)
428 : fRoot(std::move(root))
429 , fFontMgr(std::move(fmgr))
430 , fIDMapper(std::move(mapper))
431 , fContainerSize(fRoot->intrinsicSize(SkSVGLengthContext(SkSize::Make(0, 0))))
432{}
433
fmalita6ceef3d2016-07-26 18:46:34 -0700434void SkSVGDOM::render(SkCanvas* canvas) const {
435 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400436 SkSVGLengthContext lctx(fContainerSize);
437 SkSVGPresentationContext pctx;
Florin Malitaadc68892020-12-15 10:52:26 -0500438 fRoot->render(SkSVGRenderContext(canvas, fFontMgr, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700439 }
440}
441
fmalitae1baa7c2016-09-14 12:04:30 -0700442const SkSize& SkSVGDOM::containerSize() const {
443 return fContainerSize;
444}
445
fmalita6ceef3d2016-07-26 18:46:34 -0700446void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
447 // TODO: inval
448 fContainerSize = containerSize;
449}
fmalitaca39d712016-08-12 13:17:11 -0700450
Tyler Freemanc9911522020-05-08 13:23:10 -0700451sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
452 SkString idStr(id);
453 return this->fIDMapper.find(idStr);
454}
455
Tyler Freemanc9911522020-05-08 13:23:10 -0700456// TODO(fuego): move this to SkSVGNode or its own CU.
457bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
458 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
459}