blob: 6e7b42746913209b22194e0ae7fd56c8e9d03154 [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 SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
45 const char* stringValue) {
46 SkSVGColorType color;
47 SkSVGAttributeParser parser(stringValue);
48 if (!parser.parseColor(&color)) {
49 return false;
50 }
51
Florin Malitaf4403e72020-04-10 14:14:04 +000052 node->setAttribute(attr, SkSVGColorValue(color));
fmalita28d5b722016-09-12 17:06:47 -070053 return true;
54}
55
56bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
57 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050058 auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
59 if (!parseResult.isValid()) {
fmalita28d5b722016-09-12 17:06:47 -070060 return false;
61 }
62
Tyler Dennistona0a51462020-11-10 13:13:28 -050063 node->setAttribute(attr, SkSVGStringValue(parseResult->fIRI));
fmalita28d5b722016-09-12 17:06:47 -070064 return true;
65}
66
fmalita6ceef3d2016-07-26 18:46:34 -070067bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
68 const char* stringValue) {
69 SkPath path;
70 if (!SkParsePath::FromSVGString(stringValue, &path)) {
71 return false;
72 }
73
Florin Malitaf4403e72020-04-10 14:14:04 +000074 node->setAttribute(attr, SkSVGPathValue(path));
fmalita6ceef3d2016-07-26 18:46:34 -070075 return true;
76}
77
Xavier Phane29cdaf2020-03-26 16:15:14 +000078bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
79 const char* stringValue) {
80 SkString str(stringValue, strlen(stringValue));
81 SkSVGStringType strType = SkSVGStringType(str);
Florin Malitaf4403e72020-04-10 14:14:04 +000082 node->setAttribute(attr, SkSVGStringValue(strType));
Xavier Phane29cdaf2020-03-26 16:15:14 +000083 return true;
84}
85
fmalita6ceef3d2016-07-26 18:46:34 -070086bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
87 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050088 auto parseResult = SkSVGAttributeParser::parse<SkSVGTransformType>(stringValue);
89 if (!parseResult.isValid()) {
fmalitac97796b2016-08-08 12:58:57 -070090 return false;
91 }
92
Tyler Dennistona0a51462020-11-10 13:13:28 -050093 node->setAttribute(attr, SkSVGTransformValue(*parseResult));
fmalita6ceef3d2016-07-26 18:46:34 -070094 return true;
95}
96
fmalitabffc2562016-08-03 10:21:11 -070097bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
98 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050099 auto parseResult = SkSVGAttributeParser::parse<SkSVGLength>(stringValue);
100 if (!parseResult.isValid()) {
fmalitabffc2562016-08-03 10:21:11 -0700101 return false;
102 }
103
Tyler Dennistona0a51462020-11-10 13:13:28 -0500104 node->setAttribute(attr, SkSVGLengthValue(*parseResult));
fmalitabffc2562016-08-03 10:21:11 -0700105 return true;
106}
107
fmalita2d961e02016-08-11 09:16:29 -0700108bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
109 const char* stringValue) {
110 SkSVGNumberType number;
111 SkSVGAttributeParser parser(stringValue);
112 if (!parser.parseNumber(&number)) {
113 return false;
114 }
115
Florin Malitaf4403e72020-04-10 14:14:04 +0000116 node->setAttribute(attr, SkSVGNumberValue(number));
fmalita2d961e02016-08-11 09:16:29 -0700117 return true;
118}
119
fmalita397a5172016-08-08 11:38:55 -0700120bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
121 const char* stringValue) {
122 SkSVGViewBoxType viewBox;
123 SkSVGAttributeParser parser(stringValue);
124 if (!parser.parseViewBox(&viewBox)) {
125 return false;
126 }
127
Florin Malitaf4403e72020-04-10 14:14:04 +0000128 node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
fmalita397a5172016-08-08 11:38:55 -0700129 return true;
130}
131
Tyler Denniston308c0722020-04-14 10:53:41 -0400132bool SetStopColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
133 const char* stringValue) {
134 SkSVGStopColor stopColor;
135 SkSVGAttributeParser parser(stringValue);
136 if (!parser.parseStopColor(&stopColor)) {
137 return false;
138 }
139
140 node->setAttribute(attr, SkSVGStopColorValue(stopColor));
141 return true;
142}
143
Tyler Denniston30e327e2020-10-29 16:29:22 -0400144bool SetObjectBoundingBoxUnitsAttribute(const sk_sp<SkSVGNode>& node,
145 SkSVGAttribute attr,
146 const char* stringValue) {
Tyler Dennistona0a51462020-11-10 13:13:28 -0500147 auto parseResult = SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(stringValue);
148 if (!parseResult.isValid()) {
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400149 return false;
150 }
151
Tyler Dennistona0a51462020-11-10 13:13:28 -0500152 node->setAttribute(attr, SkSVGObjectBoundingBoxUnitsValue(*parseResult));
Tyler Dennistonab76ab42020-10-21 15:08:45 -0400153 return true;
154}
155
fmalita5b31f322016-08-12 12:15:33 -0700156bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
157 const char* stringValue) {
158 SkSVGPointsType points;
159 SkSVGAttributeParser parser(stringValue);
160 if (!parser.parsePoints(&points)) {
161 return false;
162 }
163
Florin Malitaf4403e72020-04-10 14:14:04 +0000164 node->setAttribute(attr, SkSVGPointsValue(points));
fmalita5b31f322016-08-12 12:15:33 -0700165 return true;
166}
167
Florin Malita385e7442020-10-21 16:55:46 -0400168bool SetPreserveAspectRatioAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
169 const char* stringValue) {
170 SkSVGPreserveAspectRatio par;
171 SkSVGAttributeParser parser(stringValue);
172 if (!parser.parsePreserveAspectRatio(&par)) {
173 return false;
174 }
175
176 node->setAttribute(attr, SkSVGPreserveAspectRatioValue(par));
177 return true;
178}
179
fmalita61f36b32016-08-08 13:58:50 -0700180SkString TrimmedString(const char* first, const char* last) {
181 SkASSERT(first);
182 SkASSERT(last);
183 SkASSERT(first <= last);
184
185 while (first <= last && *first <= ' ') { first++; }
186 while (first <= last && *last <= ' ') { last--; }
187
188 SkASSERT(last - first + 1 >= 0);
189 return SkString(first, SkTo<size_t>(last - first + 1));
190}
191
fmalita58649cc2016-07-29 08:52:03 -0700192// Breaks a "foo: bar; baz: ..." string into key:value pairs.
193class StyleIterator {
194public:
195 StyleIterator(const char* str) : fPos(str) { }
196
197 std::tuple<SkString, SkString> next() {
198 SkString name, value;
199
200 if (fPos) {
201 const char* sep = this->nextSeparator();
202 SkASSERT(*sep == ';' || *sep == '\0');
203
204 const char* valueSep = strchr(fPos, ':');
205 if (valueSep && valueSep < sep) {
fmalita61f36b32016-08-08 13:58:50 -0700206 name = TrimmedString(fPos, valueSep - 1);
207 value = TrimmedString(valueSep + 1, sep - 1);
fmalita58649cc2016-07-29 08:52:03 -0700208 }
209
210 fPos = *sep ? sep + 1 : nullptr;
211 }
212
213 return std::make_tuple(name, value);
214 }
215
216private:
217 const char* nextSeparator() const {
218 const char* sep = fPos;
219 while (*sep != ';' && *sep != '\0') {
220 sep++;
221 }
222 return sep;
223 }
224
225 const char* fPos;
226};
227
Tyler Freemanc9911522020-05-08 13:23:10 -0700228bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
fmalita58649cc2016-07-29 08:52:03 -0700229
230bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
231 const char* stringValue) {
232
233 SkString name, value;
234 StyleIterator iter(stringValue);
235 for (;;) {
236 std::tie(name, value) = iter.next();
237 if (name.isEmpty()) {
238 break;
239 }
240 set_string_attribute(node, name.c_str(), value.c_str());
241 }
242
243 return true;
244}
245
fmalita6ceef3d2016-07-26 18:46:34 -0700246template<typename T>
247struct SortedDictionaryEntry {
248 const char* fKey;
249 const T fValue;
250};
251
252struct AttrParseInfo {
253 SkSVGAttribute fAttr;
254 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
255};
256
257SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
Florin Malita385e7442020-10-21 16:55:46 -0400258 { "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
259 { "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
260 { "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
261 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400262 { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400263 { "filterUnits" , { SkSVGAttribute::kFilterUnits ,
264 SetObjectBoundingBoxUnitsAttribute }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400265 // focal point x & y
Florin Malita385e7442020-10-21 16:55:46 -0400266 { "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
267 { "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400268 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
269 { "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
270 { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
271 { "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
272 { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
273 { "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
274 SetPreserveAspectRatioAttribute }},
275 { "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
276 { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
277 { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400278 { "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
279 { "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400280 { "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400281 { "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
282 { "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
283 { "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
284 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
285 { "text" , { SkSVGAttribute::kText , SetStringAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400286 { "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
287 { "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
Florin Malita385e7442020-10-21 16:55:46 -0400288 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
289 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
290 { "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
291 { "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
292 { "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
293 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
294 { "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
295 { "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
fmalita6ceef3d2016-07-26 18:46:34 -0700296};
297
298SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
Florin Malitaf6143ff2017-10-10 09:16:52 -0400299 { "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700300 { "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
Florin Malitace8840e2016-12-08 09:26:47 -0500301 { "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700302 { "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
303 { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
Tyler Denniston70bb18d2020-11-06 12:07:53 -0500304 { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
Tyler Dennistonb25caae2020-11-09 12:46:02 -0500305 { "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
Tyler Dennistondada9602020-11-03 10:04:25 -0500306 { "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
Tyler Dennistondf208a32020-10-30 16:01:54 -0400307 { "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700308 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
309 { "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
310 { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
311 { "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
Florin Malita1aa1bb62017-10-11 14:34:33 -0400312 { "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700313 { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
314 { "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
Florin Malitacc6cc292017-10-09 16:05:30 -0400315 { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
fmalita28d5b722016-09-12 17:06:47 -0700316 { "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
317 { "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
318 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
Xavier Phane29cdaf2020-03-26 16:15:14 +0000319 { "text" , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make(); }},
Florin Malita6a69c052017-10-11 14:02:11 -0400320 { "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
fmalita6ceef3d2016-07-26 18:46:34 -0700321};
322
323struct ConstructionContext {
fmalita28d5b722016-09-12 17:06:47 -0700324 ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700325 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
fmalita28d5b722016-09-12 17:06:47 -0700326 : fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
fmalita6ceef3d2016-07-26 18:46:34 -0700327
Florin Malita39fe8c82020-10-20 10:43:03 -0400328 SkSVGNode* fParent;
329 SkSVGIDMapper* fIDMapper;
fmalita6ceef3d2016-07-26 18:46:34 -0700330};
331
Tyler Freemanc9911522020-05-08 13:23:10 -0700332bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
Tyler Denniston57154992020-11-04 16:08:30 -0500333 if (node->parseAndSetAttribute(name, value)) {
334 // Handled by new code path
335 return true;
336 }
337
fmalita58649cc2016-07-29 08:52:03 -0700338 const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
339 SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
340 name, sizeof(gAttributeParseInfo[0]));
341 if (attrIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700342#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700343 SkDebugf("unhandled attribute: %s\n", name);
fmalitafea704e2016-08-10 16:25:32 -0700344#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700345 return false;
fmalita58649cc2016-07-29 08:52:03 -0700346 }
347
348 SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
349 const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
350 if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
fmalitafea704e2016-08-10 16:25:32 -0700351#if defined(SK_VERBOSE_SVG_PARSING)
fmalita58649cc2016-07-29 08:52:03 -0700352 SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
fmalitafea704e2016-08-10 16:25:32 -0700353#endif
Tyler Freemanc9911522020-05-08 13:23:10 -0700354 return false;
fmalita58649cc2016-07-29 08:52:03 -0700355 }
Tyler Freemanc9911522020-05-08 13:23:10 -0700356
357 return true;
fmalita58649cc2016-07-29 08:52:03 -0700358}
359
fmalita6ceef3d2016-07-26 18:46:34 -0700360void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
fmalita28d5b722016-09-12 17:06:47 -0700361 const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
fmalita6ceef3d2016-07-26 18:46:34 -0700362 const char* name, *value;
363 SkDOM::AttrIter attrIter(xmlDom, xmlNode);
364 while ((name = attrIter.next(&value))) {
fmalita28d5b722016-09-12 17:06:47 -0700365 // We're handling id attributes out of band for now.
366 if (!strcmp(name, "id")) {
367 mapper->set(SkString(value), svgNode);
368 continue;
369 }
fmalita58649cc2016-07-29 08:52:03 -0700370 set_string_attribute(svgNode, name, value);
fmalita6ceef3d2016-07-26 18:46:34 -0700371 }
372}
373
374sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
375 const SkDOM::Node* xmlNode) {
376 const char* elem = dom.getName(xmlNode);
377 const SkDOM::Type elemType = dom.getType(xmlNode);
378
379 if (elemType == SkDOM::kText_Type) {
380 SkASSERT(dom.countChildren(xmlNode) == 0);
Florin Malita39fe8c82020-10-20 10:43:03 -0400381 // TODO: add type conversion helper to SkSVGNode
382 if (ctx.fParent->tag() == SkSVGTag::kText) {
383 static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
384 }
fmalita6ceef3d2016-07-26 18:46:34 -0700385 return nullptr;
386 }
387
388 SkASSERT(elemType == SkDOM::kElement_Type);
389
390 const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
391 SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
392 elem, sizeof(gTagFactories[0]));
393 if (tagIndex < 0) {
fmalitafea704e2016-08-10 16:25:32 -0700394#if defined(SK_VERBOSE_SVG_PARSING)
fmalita6ceef3d2016-07-26 18:46:34 -0700395 SkDebugf("unhandled element: <%s>\n", elem);
fmalitafea704e2016-08-10 16:25:32 -0700396#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700397 return nullptr;
398 }
399
400 SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
401 sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
fmalita28d5b722016-09-12 17:06:47 -0700402 parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
fmalita6ceef3d2016-07-26 18:46:34 -0700403
404 ConstructionContext localCtx(ctx, node);
405 for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
406 child = dom.getNextSibling(child)) {
407 sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
408 if (childNode) {
409 node->appendChild(std::move(childNode));
410 }
411 }
412
413 return node;
414}
415
416} // anonymous namespace
417
Florin Malita7006e152020-11-10 15:24:59 -0500418SkSVGDOM::Builder& SkSVGDOM::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
419 fFontMgr = std::move(fmgr);
420 return *this;
fmalita6ceef3d2016-07-26 18:46:34 -0700421}
422
Florin Malita7006e152020-11-10 15:24:59 -0500423sk_sp<SkSVGDOM> SkSVGDOM::Builder::make(SkStream& str) const {
fmalita6ceef3d2016-07-26 18:46:34 -0700424 SkDOM xmlDom;
Florin Malita7006e152020-11-10 15:24:59 -0500425 if (!xmlDom.build(str)) {
fmalita6ceef3d2016-07-26 18:46:34 -0700426 return nullptr;
427 }
428
Florin Malita7006e152020-11-10 15:24:59 -0500429 SkSVGIDMapper mapper;
430 ConstructionContext ctx(&mapper);
431
432 auto root = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
433 if (!root || root->tag() != SkSVGTag::kSvg) {
434 return nullptr;
435 }
436
437 return sk_sp<SkSVGDOM>(new SkSVGDOM(sk_sp<SkSVGSVG>(static_cast<SkSVGSVG*>(root.release())),
438 std::move(fFontMgr), std::move(mapper)));
fmalita6ceef3d2016-07-26 18:46:34 -0700439}
440
Florin Malita7006e152020-11-10 15:24:59 -0500441SkSVGDOM::SkSVGDOM(sk_sp<SkSVGSVG> root, sk_sp<SkFontMgr> fmgr, SkSVGIDMapper&& mapper)
442 : fRoot(std::move(root))
443 , fFontMgr(std::move(fmgr))
444 , fIDMapper(std::move(mapper))
445 , fContainerSize(fRoot->intrinsicSize(SkSVGLengthContext(SkSize::Make(0, 0))))
446{}
447
fmalita6ceef3d2016-07-26 18:46:34 -0700448void SkSVGDOM::render(SkCanvas* canvas) const {
449 if (fRoot) {
Florin Malitaebca0dd2017-09-09 09:39:07 -0400450 SkSVGLengthContext lctx(fContainerSize);
451 SkSVGPresentationContext pctx;
Florin Malita7006e152020-11-10 15:24:59 -0500452 fRoot->render(SkSVGRenderContext(canvas, fFontMgr, fIDMapper, lctx, pctx, nullptr));
fmalita6ceef3d2016-07-26 18:46:34 -0700453 }
454}
455
fmalitae1baa7c2016-09-14 12:04:30 -0700456const SkSize& SkSVGDOM::containerSize() const {
457 return fContainerSize;
458}
459
fmalita6ceef3d2016-07-26 18:46:34 -0700460void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
461 // TODO: inval
462 fContainerSize = containerSize;
463}
fmalitaca39d712016-08-12 13:17:11 -0700464
Tyler Freemanc9911522020-05-08 13:23:10 -0700465sk_sp<SkSVGNode>* SkSVGDOM::findNodeById(const char* id) {
466 SkString idStr(id);
467 return this->fIDMapper.find(idStr);
468}
469
Tyler Freemanc9911522020-05-08 13:23:10 -0700470// TODO(fuego): move this to SkSVGNode or its own CU.
471bool SkSVGNode::setAttribute(const char* attributeName, const char* attributeValue) {
472 return set_string_attribute(sk_ref_sp(this), attributeName, attributeValue);
473}