blob: 961f77aaed85bf8bd8676b49c97263d581e01849 [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 Denniston8ed04432020-12-10 15:51:04 -050021#include "modules/svg/include/SkSVGFeFlood.h"
Tyler Denniston187d8112021-01-12 09:34:23 -050022#include "modules/svg/include/SkSVGFeGaussianBlur.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050023#include "modules/svg/include/SkSVGFeTurbulence.h"
Tyler Dennistondf208a32020-10-30 16:01:54 -040024#include "modules/svg/include/SkSVGFilter.h"
Florin Malitab3418102020-10-15 18:10:29 -040025#include "modules/svg/include/SkSVGG.h"
26#include "modules/svg/include/SkSVGLine.h"
27#include "modules/svg/include/SkSVGLinearGradient.h"
28#include "modules/svg/include/SkSVGNode.h"
29#include "modules/svg/include/SkSVGPath.h"
30#include "modules/svg/include/SkSVGPattern.h"
31#include "modules/svg/include/SkSVGPoly.h"
32#include "modules/svg/include/SkSVGRadialGradient.h"
33#include "modules/svg/include/SkSVGRect.h"
34#include "modules/svg/include/SkSVGRenderContext.h"
35#include "modules/svg/include/SkSVGSVG.h"
36#include "modules/svg/include/SkSVGStop.h"
37#include "modules/svg/include/SkSVGText.h"
38#include "modules/svg/include/SkSVGTypes.h"
39#include "modules/svg/include/SkSVGUse.h"
40#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040041#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070043
44namespace {
45
fmalita28d5b722016-09-12 17:06:47 -070046bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
47 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050048 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
49 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070050 return false;
51 }
52
Tyler Dennistona0a51462020-11-10 13:13:28 -050053 node->setAttribute(attr, SkSVGStringValue(parseResult->fIRI));
fmalita28d5b722016-09-12 17:06:47 -070054 return true;
55}
56
fmalita6ceef3d2016-07-26 18:46:34 -070057bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
58 const char* stringValue) {
59 SkPath path;
60 if (!SkParsePath::FromSVGString(stringValue, &path)) {
61 return false;
62 }
63
Florin Malitaf4403e72020-04-10 14:14:04 +000064 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070065 return true;
66}
67
Xavier Phane29cdaf2020-03-26 16:15:14 +000068bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
69 const char* stringValue) {
70 SkString str(stringValue, strlen(stringValue));
71 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000072 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000073 return true;
74}
75
fmalita6ceef3d2016-07-26 18:46:34 -070076bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
77 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050078 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
79 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070080 return false;
81 }
82
Tyler Dennistona0a51462020-11-10 13:13:28 -050083 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070084 return true;
85}
86
fmalitabffc2562016-08-03 10:21:11 -070087bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
88 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050089 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
90 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -070091 return false;
92 }
93
Tyler Dennistona0a51462020-11-10 13:13:28 -050094 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -070095 return true;
96}
97
fmalita397a5172016-08-08 11:38:55 -070098bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
99 const char* stringValue) {
100 SkSVGViewBoxType viewBox;
101 SkSVGAttributeParser parser(stringValue);
102 if (!parser.parseViewBox(&viewBox)) {
103 return false;
104 }
105
Florin Malitaf4403e72020-04-10 14:14:04 +0000106 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700107 return true;
108}
109
Tyler Denniston30e327e2020-10-29 16:29:22 -0400110bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
111 SkSVGAttribute attr,
112 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500113 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
114 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400115 return false;
116 }
117
Tyler Dennistona0a51462020-11-10 13:13:28 -0500118 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400119 return true;
120}
121
fmalita5b31f322016-08-12 12:15:33 -0700122bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
123 const char* stringValue) {
124 SkSVGPointsType points;
125 SkSVGAttributeParser parser(stringValue);
126 if (!parser.parsePoints(&points)) {
127 return false;
128 }
129
Florin Malitaf4403e72020-04-10 14:14:04 +0000130 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700131 return true;
132}
133
Florin Malita385e7442020-10-21 16:55:46 -0400134bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
135 const char* stringValue) {
136 SkSVGPreserveAspectRatio par;
137 SkSVGAttributeParser parser(stringValue);
138 if (!parser.parsePreserveAspectRatio(&par)) {
139 return false;
140 }
141
142 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
143 return true;
144}
145
fmalita61f36b32016-08-08 13:58:50 -0700146SkString TrimmedString(const char* first, const char* last) {
147 SkASSERT(first);
148 SkASSERT(last);
149 SkASSERT(first <= last);
150
151 while (first <= last && *first <= ' ') { first++; }
152 while (first <= last && *last <= ' ') { last--; }
153
154 SkASSERT(last - first + 1 >= 0);
155 return SkString(first, SkTo<size_t>(last - first + 1));
156}
157
fmalita58649cc2016-07-29 08:52:03 -0700158// Breaks a "foo: bar; baz: ..." string into key:value pairs.
159class StyleIterator {
160public:
161 StyleIterator(const char* str) : fPos(str) { }
162
163 std::tuple<SkString, SkString> next() {
164 SkString name, value;
165
166 if (fPos) {
167 const char* sep = this->nextSeparator();
168 SkASSERT(*sep == ';' || *sep == '\0');
169
170 const char* valueSep = strchr(fPos, ':');
171 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700172 name = TrimmedString(fPos, valueSep - 1);
173 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700174 }
175
176 fPos = *sep ? sep + 1 : nullptr;
177 }
178
179 return std::make_tuple(name, value);
180 }
181
182private:
183 const char* nextSeparator() const {
184 const char* sep = fPos;
185 while (*sep != ';' && *sep != '\0') {
186 sep++;
187 }
188 return sep;
189 }
190
191 const char* fPos;
192};
193
Tyler Freemanc9911522020-05-08 13:23:10 -0700194bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700195
196bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
197 const char* stringValue) {
198
199 SkString name, value;
200 StyleIterator iter(stringValue);
201 for (;;) {
202 std::tie(name, value) = iter.next();
203 if (name.isEmpty()) {
204 break;
205 }
206 set_string_attribute(node, name.c_str(), value.c_str());
207 }
208
209 return true;
210}
211
fmalita6ceef3d2016-07-26 18:46:34 -0700212template<typename T>
213struct SortedDictionaryEntry {
214 const char* fKey;
215 const T fValue;
216};
217
218struct AttrParseInfo {
219 SkSVGAttribute fAttr;
220 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
221};
222
223SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400224 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
225 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
226 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400227 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
228 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400229 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400230 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
231 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400232 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
233 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400234 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
235 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
236 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
237 SetPreserveAspectRatioAttribute }},
238 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
239 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
240 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400241 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
242 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400243 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
244 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400245 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
246 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
247 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
248 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
249 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
250 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
251 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
252 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700253};
254
255SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400256 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700257 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500258 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700259 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
260 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
Tyler Denniston70bb18d2020-11-06 12:07:53 -0500261 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
Tyler Dennistonb25caae2020-11-09 12:46:02 -0500262 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
Tyler Denniston8ed04432020-12-10 15:51:04 -0500263 { "feFlood" , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make(); }},
Tyler Denniston187d8112021-01-12 09:34:23 -0500264 { "feGaussianBlur", []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
Tyler Dennistondada9602020-11-03 10:04:25 -0500265 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400266 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700267 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
268 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
269 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
270 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400271 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700272 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
273 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400274 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700275 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
276 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
277 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000278 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita512ff752020-12-06 11:50:52 -0500279 { "tspan" , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400280 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700281};
282
283struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700284 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700285 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700286 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700287
Florin Malita39fe8c82020-10-20 10:43:03 -0400288 SkSVGNode* fParent;
289 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700290};
291
Tyler Freemanc9911522020-05-08 13:23:10 -0700292bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500293 if (node->parseAndSetAttribute(name, value)) {
294 // Handled by new code path
295 return true;
296 }
297
fmalita58649cc2016-07-29 08:52:03 -0700298 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
299 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
300 name, sizeof(gAttributeParseInfo[0]));
301 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700302#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700303 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700304#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700305 return false;
fmalita58649cc2016-07-29 08:52:03 -0700306 }
307
308 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
309 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
310 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700311#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700312 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700313#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700314 return false;
fmalita58649cc2016-07-29 08:52:03 -0700315 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700316
317 return true;
fmalita58649cc2016-07-29 08:52:03 -0700318}
319
fmalita6ceef3d2016-07-26 18:46:34 -0700320void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700321 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700322 const char* name, *value;
323 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
324 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700325 // We're handling id attributes out of band for now.
326 if (!strcmp(name, "id")) {
327 mapper->set(SkString(value), svgNode);
328 continue;
329 }
fmalita58649cc2016-07-29 08:52:03 -0700330 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700331 }
332}
333
334sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
335 const SkDOM::Node* xmlNode) {
336 const char* elem = dom.getName(xmlNode);
337 const SkDOM::Type elemType = dom.getType(xmlNode);
338
339 if (elemType == SkDOM::kText_Type) {
Florin Malita512ff752020-12-06 11:50:52 -0500340 // Text literals require special handling.
fmalita6ceef3d2016-07-26 18:46:34 -0700341 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita512ff752020-12-06 11:50:52 -0500342 auto txt = SkSVGTextLiteral::Make();
343 txt->setText(SkString(dom.getName(xmlNode)));
344 ctx.fParent->appendChild(std::move(txt));
345
fmalita6ceef3d2016-07-26 18:46:34 -0700346 return nullptr;
347 }
348
349 SkASSERT(elemType == SkDOM::kElement_Type);
350
351 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
352 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
353 elem, sizeof(gTagFactories[0]));
354 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700355#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700356 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700357#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700358 return nullptr;
359 }
360
361 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
362 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700363 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700364
365 ConstructionContext localCtx(ctx, node);
366 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
367 child = dom.getNextSibling(child)) {
368 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
369 if (childNode) {
370 node->appendChild(std::move(childNode));
371 }
372 }
373
374 return node;
375}
376
377} // anonymous namespace
378
Florin Malita7006e152020-11-10 15:24:59 -0500379SkSVGDOM::Builder& SkSVGDOM::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
380 fFontMgr = std::move(fmgr);
381 return *this;
fmalita6ceef3d2016-07-26 18:46:34 -0700382}
383
Florin Malita7006e152020-11-10 15:24:59 -0500384sk_sp<SkSVGDOM> SkSVGDOM::Builder::make(SkStream& str) const {
fmalita6ceef3d2016-07-26 18:46:34 -0700385 SkDOM xmlDom;
Florin Malita7006e152020-11-10 15:24:59 -0500386 if (!xmlDom.build(str)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700387 return nullptr;
388 }
389
Florin Malita7006e152020-11-10 15:24:59 -0500390 SkSVGIDMapper mapper;
391 ConstructionContext ctx(&mapper);
392
393 auto root = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
394 if (!root || root->tag() != SkSVGTag::kSvg) {
395 return nullptr;
396 }
397
398 return sk_sp<SkSVGDOM>(new SkSVGDOM(sk_sp<SkSVGSVG>(static_cast<SkSVGSVG*>(root.release())),
399 std::move(fFontMgr), std::move(mapper)));
fmalita6ceef3d2016-07-26 18:46:34 -0700400}
401
Florin Malita7006e152020-11-10 15:24:59 -0500402SkSVGDOM::SkSVGDOM(sk_sp<SkSVGSVG> root, sk_sp<SkFontMgr> fmgr, SkSVGIDMapper&& mapper)
403 : fRoot(std::move(root))
404 , fFontMgr(std::move(fmgr))
405 , fIDMapper(std::move(mapper))
406 , fContainerSize(fRoot->intrinsicSize(SkSVGLengthContext(SkSize::Make(0, 0))))
407{}
408
fmalita6ceef3d2016-07-26 18:46:34 -0700409void SkSVGDOM::render(SkCanvas* canvas) const {
410 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400411 SkSVGLengthContext lctx(fContainerSize);
412 SkSVGPresentationContext pctx;
Florin Malitaadc68892020-12-15 10:52:26 -0500413 fRoot->render(SkSVGRenderContext(canvas, fFontMgr, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700414 }
415}
416
fmalitae1baa7c2016-09-14 12:04:30 -0700417const SkSize& SkSVGDOM::containerSize() const {
418 return fContainerSize;
419}
420
fmalita6ceef3d2016-07-26 18:46:34 -0700421void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
422 // TODO: inval
423 fContainerSize = containerSize;
424}
fmalitaca39d712016-08-12 13:17:11 -0700425
Tyler Freemanc9911522020-05-08 13:23:10 -0700426sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
427 SkString idStr(id);
428 return this->fIDMapper.find(idStr);
429}
430
Tyler Freemanc9911522020-05-08 13:23:10 -0700431// TODO(fuego): move this to SkSVGNode or its own CU.
432bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
433 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
434}