blob: 3c7e3d562cffe7564f7d78bc77743ebc244fe79a [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"
32#include "modules/svg/include/SkSVGLine.h"
33#include "modules/svg/include/SkSVGLinearGradient.h"
Florin Malita836c2ca2021-01-13 11:48:02 -050034#include "modules/svg/include/SkSVGMask.h"
Florin Malitab3418102020-10-15 18:10:29 -040035#include "modules/svg/include/SkSVGNode.h"
36#include "modules/svg/include/SkSVGPath.h"
37#include "modules/svg/include/SkSVGPattern.h"
38#include "modules/svg/include/SkSVGPoly.h"
39#include "modules/svg/include/SkSVGRadialGradient.h"
40#include "modules/svg/include/SkSVGRect.h"
41#include "modules/svg/include/SkSVGRenderContext.h"
42#include "modules/svg/include/SkSVGSVG.h"
43#include "modules/svg/include/SkSVGStop.h"
44#include "modules/svg/include/SkSVGText.h"
45#include "modules/svg/include/SkSVGTypes.h"
46#include "modules/svg/include/SkSVGUse.h"
47#include "modules/svg/include/SkSVGValue.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040048#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070050
51namespace {
52
fmalita28d5b722016-09-12 17:06:47 -070053bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
54 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050055 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
56 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070057 return false;
58 }
59
Tyler Dennistona0a51462020-11-10 13:13:28 -050060 node->setAttribute(attr, SkSVGStringValue(parseResult->fIRI));
fmalita28d5b722016-09-12 17:06:47 -070061 return true;
62}
63
fmalita6ceef3d2016-07-26 18:46:34 -070064bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
65 const char* stringValue) {
66 SkPath path;
67 if (!SkParsePath::FromSVGString(stringValue, &path)) {
68 return false;
69 }
70
Florin Malitaf4403e72020-04-10 14:14:04 +000071 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070072 return true;
73}
74
Xavier Phane29cdaf2020-03-26 16:15:14 +000075bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
76 const char* stringValue) {
77 SkString str(stringValue, strlen(stringValue));
78 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000079 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000080 return true;
81}
82
fmalita6ceef3d2016-07-26 18:46:34 -070083bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
84 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050085 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
86 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070087 return false;
88 }
89
Tyler Dennistona0a51462020-11-10 13:13:28 -050090 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070091 return true;
92}
93
fmalitabffc2562016-08-03 10:21:11 -070094bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
95 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050096 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
97 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -070098 return false;
99 }
100
Tyler Dennistona0a51462020-11-10 13:13:28 -0500101 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -0700102 return true;
103}
104
fmalita397a5172016-08-08 11:38:55 -0700105bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
106 const char* stringValue) {
107 SkSVGViewBoxType viewBox;
108 SkSVGAttributeParser parser(stringValue);
109 if (!parser.parseViewBox(&viewBox)) {
110 return false;
111 }
112
Florin Malitaf4403e72020-04-10 14:14:04 +0000113 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700114 return true;
115}
116
Tyler Denniston30e327e2020-10-29 16:29:22 -0400117bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
118 SkSVGAttribute attr,
119 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500120 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
121 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400122 return false;
123 }
124
Tyler Dennistona0a51462020-11-10 13:13:28 -0500125 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400126 return true;
127}
128
fmalita5b31f322016-08-12 12:15:33 -0700129bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
130 const char* stringValue) {
131 SkSVGPointsType points;
132 SkSVGAttributeParser parser(stringValue);
133 if (!parser.parsePoints(&points)) {
134 return false;
135 }
136
Florin Malitaf4403e72020-04-10 14:14:04 +0000137 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700138 return true;
139}
140
Florin Malita385e7442020-10-21 16:55:46 -0400141bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
142 const char* stringValue) {
143 SkSVGPreserveAspectRatio par;
144 SkSVGAttributeParser parser(stringValue);
145 if (!parser.parsePreserveAspectRatio(&par)) {
146 return false;
147 }
148
149 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
150 return true;
151}
152
fmalita61f36b32016-08-08 13:58:50 -0700153SkString TrimmedString(const char* first, const char* last) {
154 SkASSERT(first);
155 SkASSERT(last);
156 SkASSERT(first <= last);
157
158 while (first <= last && *first <= ' ') { first++; }
159 while (first <= last && *last <= ' ') { last--; }
160
161 SkASSERT(last - first + 1 >= 0);
162 return SkString(first, SkTo<size_t>(last - first + 1));
163}
164
fmalita58649cc2016-07-29 08:52:03 -0700165// Breaks a "foo: bar; baz: ..." string into key:value pairs.
166class StyleIterator {
167public:
168 StyleIterator(const char* str) : fPos(str) { }
169
170 std::tuple<SkString, SkString> next() {
171 SkString name, value;
172
173 if (fPos) {
174 const char* sep = this->nextSeparator();
175 SkASSERT(*sep == ';' || *sep == '\0');
176
177 const char* valueSep = strchr(fPos, ':');
178 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700179 name = TrimmedString(fPos, valueSep - 1);
180 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700181 }
182
183 fPos = *sep ? sep + 1 : nullptr;
184 }
185
186 return std::make_tuple(name, value);
187 }
188
189private:
190 const char* nextSeparator() const {
191 const char* sep = fPos;
192 while (*sep != ';' && *sep != '\0') {
193 sep++;
194 }
195 return sep;
196 }
197
198 const char* fPos;
199};
200
Tyler Freemanc9911522020-05-08 13:23:10 -0700201bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700202
203bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
204 const char* stringValue) {
205
206 SkString name, value;
207 StyleIterator iter(stringValue);
208 for (;;) {
209 std::tie(name, value) = iter.next();
210 if (name.isEmpty()) {
211 break;
212 }
213 set_string_attribute(node, name.c_str(), value.c_str());
214 }
215
216 return true;
217}
218
fmalita6ceef3d2016-07-26 18:46:34 -0700219template<typename T>
220struct SortedDictionaryEntry {
221 const char* fKey;
222 const T fValue;
223};
224
225struct AttrParseInfo {
226 SkSVGAttribute fAttr;
227 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
228};
229
230SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400231 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
232 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
233 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400234 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
235 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400236 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400237 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
238 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400239 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
240 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400241 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
242 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
243 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
244 SetPreserveAspectRatioAttribute }},
245 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
246 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
247 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400248 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
249 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400250 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
251 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400252 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
253 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
254 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
255 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
256 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
257 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
258 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
259 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700260};
261
262SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Tyler Denniston8eedcd22021-01-27 09:18:06 -0500263 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
264 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
265 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
266 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
267 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
268 { "feBlend" , []() -> sk_sp<SkSVGNode> { return SkSVGFeBlend::Make(); }},
269 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
270 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
271 { "feDisplacementMap" , []() -> sk_sp<SkSVGNode> { return SkSVGFeDisplacementMap::Make(); }},
272 { "feDistantLight" , []() -> sk_sp<SkSVGNode> { return SkSVGFeDistantLight::Make(); }},
273 { "feFlood" , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make(); }},
274 { "feGaussianBlur" , []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
275 { "feMorphology" , []() -> sk_sp<SkSVGNode> { return SkSVGFeMorphology::Make(); }},
276 { "feOffset" , []() -> sk_sp<SkSVGNode> { return SkSVGFeOffset::Make(); }},
277 { "fePointLight" , []() -> sk_sp<SkSVGNode> { return SkSVGFePointLight::Make(); }},
278 { "feSpecularLighting", []() -> sk_sp<SkSVGNode> { return SkSVGFeSpecularLighting::Make(); }},
279 { "feSpotLight" , []() -> sk_sp<SkSVGNode> { return SkSVGFeSpotLight::Make(); }},
280 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
281 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
282 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
283 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
284 { "linearGradient" , []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
285 { "mask" , []() -> sk_sp<SkSVGNode> { return SkSVGMask::Make(); }},
286 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
287 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
288 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
289 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
290 { "radialGradient" , []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
291 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
292 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
Florin Malitacdeabca2021-01-20 13:21:20 -0500293// "svg" handled explicitly
Tyler Denniston8eedcd22021-01-27 09:18:06 -0500294 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
295 { "textPath" , []() -> sk_sp<SkSVGNode> { return SkSVGTextPath::Make(); }},
296 { "tspan" , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make(); }},
297 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700298};
299
300struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700301 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700302 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700303 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700304
Florin Malita39fe8c82020-10-20 10:43:03 -0400305 SkSVGNode* fParent;
306 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700307};
308
Tyler Freemanc9911522020-05-08 13:23:10 -0700309bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500310 if (node->parseAndSetAttribute(name, value)) {
311 // Handled by new code path
312 return true;
313 }
314
fmalita58649cc2016-07-29 08:52:03 -0700315 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
316 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
317 name, sizeof(gAttributeParseInfo[0]));
318 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700319#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700320 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700321#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700322 return false;
fmalita58649cc2016-07-29 08:52:03 -0700323 }
324
325 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
326 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
327 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700328#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700329 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700330#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700331 return false;
fmalita58649cc2016-07-29 08:52:03 -0700332 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700333
334 return true;
fmalita58649cc2016-07-29 08:52:03 -0700335}
336
fmalita6ceef3d2016-07-26 18:46:34 -0700337void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700338 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700339 const char* name, *value;
340 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
341 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700342 // We're handling id attributes out of band for now.
343 if (!strcmp(name, "id")) {
344 mapper->set(SkString(value), svgNode);
345 continue;
346 }
fmalita58649cc2016-07-29 08:52:03 -0700347 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700348 }
349}
350
351sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
352 const SkDOM::Node* xmlNode) {
353 const char* elem = dom.getName(xmlNode);
354 const SkDOM::Type elemType = dom.getType(xmlNode);
355
356 if (elemType == SkDOM::kText_Type) {
Florin Malita512ff752020-12-06 11:50:52 -0500357 // Text literals require special handling.
fmalita6ceef3d2016-07-26 18:46:34 -0700358 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita512ff752020-12-06 11:50:52 -0500359 auto txt = SkSVGTextLiteral::Make();
360 txt->setText(SkString(dom.getName(xmlNode)));
361 ctx.fParent->appendChild(std::move(txt));
362
fmalita6ceef3d2016-07-26 18:46:34 -0700363 return nullptr;
364 }
365
366 SkASSERT(elemType == SkDOM::kElement_Type);
367
Florin Malitacdeabca2021-01-20 13:21:20 -0500368 auto make_node = [](const ConstructionContext& ctx, const char* elem) -> sk_sp<SkSVGNode> {
369 if (strcmp(elem, "svg") == 0) {
370 // Outermost SVG element must be tagged as such.
371 return SkSVGSVG::Make(ctx.fParent ? SkSVGSVG::Type::kInner
372 : SkSVGSVG::Type::kRoot);
373 }
374
375 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
376 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
377 elem, sizeof(gTagFactories[0]));
378 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700379#if defined(SK_VERBOSE_SVG_PARSING)
Florin Malitacdeabca2021-01-20 13:21:20 -0500380 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700381#endif
Florin Malitacdeabca2021-01-20 13:21:20 -0500382 return nullptr;
383 }
384 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
385
386 return gTagFactories[tagIndex].fValue();
387 };
388
389 auto node = make_node(ctx, elem);
390 if (!node) {
fmalita6ceef3d2016-07-26 18:46:34 -0700391 return nullptr;
392 }
393
fmalita28d5b722016-09-12 17:06:47 -0700394 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700395
396 ConstructionContext localCtx(ctx, node);
397 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
398 child = dom.getNextSibling(child)) {
399 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
400 if (childNode) {
401 node->appendChild(std::move(childNode));
402 }
403 }
404
405 return node;
406}
407
408} // anonymous namespace
409
Florin Malita7006e152020-11-10 15:24:59 -0500410SkSVGDOM::Builder& SkSVGDOM::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
411 fFontMgr = std::move(fmgr);
412 return *this;
fmalita6ceef3d2016-07-26 18:46:34 -0700413}
414
Florin Malita7006e152020-11-10 15:24:59 -0500415sk_sp<SkSVGDOM> SkSVGDOM::Builder::make(SkStream& str) const {
fmalita6ceef3d2016-07-26 18:46:34 -0700416 SkDOM xmlDom;
Florin Malita7006e152020-11-10 15:24:59 -0500417 if (!xmlDom.build(str)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700418 return nullptr;
419 }
420
Florin Malita7006e152020-11-10 15:24:59 -0500421 SkSVGIDMapper mapper;
422 ConstructionContext ctx(&mapper);
423
424 auto root = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
425 if (!root || root->tag() != SkSVGTag::kSvg) {
426 return nullptr;
427 }
428
429 return sk_sp<SkSVGDOM>(new SkSVGDOM(sk_sp<SkSVGSVG>(static_cast<SkSVGSVG*>(root.release())),
Florin Malita64400d92021-01-27 18:51:44 +0000430 std::move(fFontMgr), std::move(mapper)));
fmalita6ceef3d2016-07-26 18:46:34 -0700431}
432
Florin Malita64400d92021-01-27 18:51:44 +0000433SkSVGDOM::SkSVGDOM(sk_sp<SkSVGSVG> root, sk_sp<SkFontMgr> fmgr, SkSVGIDMapper&& mapper)
Florin Malita7006e152020-11-10 15:24:59 -0500434 : fRoot(std::move(root))
435 , fFontMgr(std::move(fmgr))
436 , fIDMapper(std::move(mapper))
437 , fContainerSize(fRoot->intrinsicSize(SkSVGLengthContext(SkSize::Make(0, 0))))
Florin Malita64400d92021-01-27 18:51:44 +0000438{}
Florin Malita7006e152020-11-10 15:24:59 -0500439
fmalita6ceef3d2016-07-26 18:46:34 -0700440void SkSVGDOM::render(SkCanvas* canvas) const {
441 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400442 SkSVGLengthContext lctx(fContainerSize);
443 SkSVGPresentationContext pctx;
Florin Malita64400d92021-01-27 18:51:44 +0000444 fRoot->render(SkSVGRenderContext(canvas, fFontMgr, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700445 }
446}
447
fmalitae1baa7c2016-09-14 12:04:30 -0700448const SkSize& SkSVGDOM::containerSize() const {
449 return fContainerSize;
450}
451
fmalita6ceef3d2016-07-26 18:46:34 -0700452void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
453 // TODO: inval
454 fContainerSize = containerSize;
455}
fmalitaca39d712016-08-12 13:17:11 -0700456
Tyler Freemanc9911522020-05-08 13:23:10 -0700457sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
458 SkString idStr(id);
459 return this->fIDMapper.find(idStr);
460}
461
Tyler Freemanc9911522020-05-08 13:23:10 -0700462// TODO(fuego): move this to SkSVGNode or its own CU.
463bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
464 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
465}