Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-factory.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Factory for SPObject tree
4 *
5 * Authors:
6 * Markus Engel
7 * PBS <pbs3141@gmail.com>
8 *
9 * Copyright (C) 2022 Authors
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include "sp-factory.h"
14
15// primary
16#include "box3d.h"
17#include "box3d-side.h"
18#include "color-profile.h"
19#include "persp3d.h"
20#include "sp-anchor.h"
21#include "sp-clippath.h"
22#include "sp-defs.h"
23#include "sp-desc.h"
24#include "sp-ellipse.h"
25#include "sp-filter.h"
26#include "sp-flowdiv.h"
27#include "sp-flowregion.h"
28#include "sp-flowtext.h"
29#include "sp-font.h"
30#include "sp-font-face.h"
31#include "sp-glyph.h"
32#include "sp-glyph-kerning.h"
33#include "sp-grid.h"
34#include "sp-guide.h"
35#include "sp-hatch.h"
36#include "sp-hatch-path.h"
37#include "sp-image.h"
38#include "sp-line.h"
39#include "sp-linear-gradient.h"
40#include "sp-marker.h"
41#include "sp-mask.h"
42#include "sp-mesh-gradient.h"
43#include "sp-mesh-patch.h"
44#include "sp-mesh-row.h"
45#include "sp-metadata.h"
46#include "sp-missing-glyph.h"
47#include "sp-namedview.h"
48#include "sp-offset.h"
49#include "sp-page.h"
50#include "sp-path.h"
51#include "sp-pattern.h"
52#include "sp-polyline.h"
53#include "sp-radial-gradient.h"
54#include "sp-rect.h"
55#include "sp-root.h"
56#include "sp-script.h"
57#include "sp-solid-color.h"
58#include "sp-spiral.h"
59#include "sp-star.h"
60#include "sp-stop.h"
61#include "sp-string.h"
62#include "sp-style-elem.h"
63#include "sp-switch.h"
64#include "sp-symbol.h"
65#include "sp-tag.h"
66#include "sp-tag-use.h"
67#include "sp-text.h"
68#include "sp-textpath.h"
69#include "sp-title.h"
70#include "sp-tref.h"
71#include "sp-tspan.h"
72#include "sp-use.h"
74
75// filters
76#include "filters/blend.h"
77#include "filters/colormatrix.h"
80#include "filters/composite.h"
85#include "filters/flood.h"
87#include "filters/image.h"
88#include "filters/merge.h"
89#include "filters/mergenode.h"
90#include "filters/morphology.h"
91#include "filters/offset.h"
92#include "filters/pointlight.h"
94#include "filters/spotlight.h"
95#include "filters/tile.h"
96#include "filters/turbulence.h"
97
98#include <unordered_map>
99
100namespace {
101
102class Factory
103{
104public:
105 SPObject *create(std::string const &id) const
106 {
107 auto it = map.find(id);
108
109 if (it == map.end()) {
110 std::cerr << "WARNING: unknown type: " << id << std::endl;
111 return nullptr;
112 }
113
114 return it->second();
115 }
116
117 bool supportsId(std::string const &id) const
118 {
119 return map.find(id) != map.end();
120 }
121
122 static Factory const &get()
123 {
124 static Factory const singleton;
125 return singleton;
126 }
127
128private:
129 using Func = SPObject*(*)();
130
131 template <typename T>
132 static Func constexpr make = [] () -> SPObject* { return new T; };
133 static Func constexpr null = [] () -> SPObject* { return nullptr; };
134
135 std::unordered_map<std::string, Func> const map =
136 {
137 // primary
138 { "inkscape:box3d", make<SPBox3D> },
139 { "inkscape:box3dside", make<Box3DSide> },
140 { "svg:color-profile", make<Inkscape::ColorProfile> },
141 { "inkscape:persp3d", make<Persp3D> },
142 { "svg:a", make<SPAnchor> },
143 { "svg:clipPath", make<SPClipPath> },
144 { "svg:defs", make<SPDefs> },
145 { "svg:desc", make<SPDesc> },
146 { "svg:ellipse", [] () -> SPObject* {
147 auto e = new SPGenericEllipse;
149 return e;
150 }},
151 { "svg:circle", [] () -> SPObject* {
152 auto c = new SPGenericEllipse;
154 return c;
155 }},
156 { "arc", [] () -> SPObject* {
157 auto a = new SPGenericEllipse;
159 return a;
160 }},
161 { "svg:filter", make<SPFilter> },
162 { "svg:flowDiv", make<SPFlowdiv> },
163 { "svg:flowSpan", make<SPFlowtspan> },
164 { "svg:flowPara", make<SPFlowpara> },
165 { "svg:flowLine", make<SPFlowline> },
166 { "svg:flowRegionBreak", make<SPFlowregionbreak> },
167 { "svg:flowRegion", make<SPFlowregion> },
168 { "svg:flowRegionExclude", make<SPFlowregionExclude> },
169 { "svg:flowRoot", make<SPFlowtext> },
170 { "svg:font", make<SPFont> },
171 { "svg:font-face", make<SPFontFace> },
172 { "svg:glyph", make<SPGlyph> },
173 { "svg:hkern", make<SPHkern> },
174 { "svg:vkern", make<SPVkern> },
175 { "sodipodi:guide", make<SPGuide> },
176 { "inkscape:page", make<SPPage> },
177 { "svg:hatch", make<SPHatch> },
178 { "svg:hatchpath", make<SPHatchPath> },
179 { "svg:hatchPath", [] () -> SPObject* {
180 std::cerr << "Warning: <hatchPath> has been renamed <hatchpath>" << std::endl;
181 return new SPHatchPath;
182 }},
183 { "svg:image", make<SPImage> },
184 { "svg:g", make<SPGroup> },
185 { "svg:line", make<SPLine> },
186 { "svg:linearGradient", make<SPLinearGradient> },
187 { "svg:marker", make<SPMarker> },
188 { "svg:mask", make<SPMask> },
189 { "svg:mesh", [] () -> SPObject* { // SVG 2 old
190 std::cerr << "Warning: <mesh> has been renamed <meshgradient>." << std::endl;
191 std::cerr << "Warning: <mesh> has been repurposed as a shape that tightly wraps a <meshgradient>." << std::endl;
192 return new SPMeshGradient;
193 }},
194 { "svg:meshGradient", [] () -> SPObject* { // SVG 2 old
195 std::cerr << "Warning: <meshGradient> has been renamed <meshgradient>" << std::endl;
196 return new SPMeshGradient;
197 }},
198 { "svg:meshgradient", [] () -> SPObject* { // SVG 2
199 return new SPMeshGradient;
200 }},
201 { "svg:meshPatch", [] () -> SPObject* {
202 std::cerr << "Warning: <meshPatch> and <meshRow> have been renamed <meshpatch> and <meshrow>" << std::endl;
203 return new SPMeshpatch;
204 }},
205 { "svg:meshpatch", make<SPMeshpatch> },
206 { "svg:meshRow", make<SPMeshrow> },
207 { "svg:meshrow", make<SPMeshrow> },
208 { "svg:metadata", make<SPMetadata> },
209 { "svg:missing-glyph", make<SPMissingGlyph> },
210 { "sodipodi:namedview", make<SPNamedView> },
211 { "inkscape:offset", make<SPOffset> },
212 { "svg:path", make<SPPath> },
213 { "svg:pattern", make<SPPattern> },
214 { "svg:polygon", make<SPPolygon> },
215 { "svg:polyline", make<SPPolyLine> },
216 { "svg:radialGradient", make<SPRadialGradient> },
217 { "svg:rect", make<SPRect> },
218 { "rect", make<SPRect> }, // LPE rect;
219 { "svg:svg", make<SPRoot> },
220 { "svg:script", make<SPScript> },
221 { "svg:solidColor", [] () -> SPObject* {
222 std::cerr << "Warning: <solidColor> has been renamed <solidcolor>" << std::endl;
223 return new SPSolidColor;
224 }},
225 { "svg:solidColor", [] () -> SPObject* {
226 std::cerr << "Warning: <solidColor> has been renamed <solidcolor>" << std::endl;
227 return new SPSolidColor;
228 }},
229 { "svg:solidcolor", make<SPSolidColor> },
230 { "spiral", make<SPSpiral> },
231 { "star", make<SPStar> },
232 { "svg:stop", make<SPStop> },
233 { "string", make<SPString> },
234 { "svg:style", make<SPStyleElem> },
235 { "svg:switch", make<SPSwitch> },
236 { "svg:symbol", make<SPSymbol> },
237 { "inkscape:tag", make<SPTag> },
238 { "inkscape:tagref", make<SPTagUse> },
239 { "svg:text", make<SPText> },
240 { "svg:title", make<SPTitle> },
241 { "svg:tref", make<SPTRef> },
242 { "svg:tspan", make<SPTSpan> },
243 { "svg:textPath", make<SPTextPath> },
244 { "svg:use", make<SPUse> },
245 { "inkscape:path-effect", make<LivePathEffectObject> },
246
247 // filters
248 { "svg:feBlend", make<SPFeBlend> },
249 { "svg:feColorMatrix", make<SPFeColorMatrix> },
250 { "svg:feComponentTransfer", make<SPFeComponentTransfer> },
251 { "svg:feFuncR", [] () -> SPObject* { return new SPFeFuncNode(SPFeFuncNode::R); }},
252 { "svg:feFuncG", [] () -> SPObject* { return new SPFeFuncNode(SPFeFuncNode::G); }},
253 { "svg:feFuncB", [] () -> SPObject* { return new SPFeFuncNode(SPFeFuncNode::B); }},
254 { "svg:feFuncA", [] () -> SPObject* { return new SPFeFuncNode(SPFeFuncNode::A); }},
255 { "svg:feComposite", make<SPFeComposite> },
256 { "svg:feConvolveMatrix", make<SPFeConvolveMatrix> },
257 { "svg:feDiffuseLighting", make<SPFeDiffuseLighting> },
258 { "svg:feDisplacementMap", make<SPFeDisplacementMap> },
259 { "svg:feDistantLight", make<SPFeDistantLight> },
260 { "svg:feFlood", make<SPFeFlood> },
261 { "svg:feGaussianBlur", make<SPGaussianBlur> },
262 { "svg:feImage", make<SPFeImage> },
263 { "svg:feMerge", make<SPFeMerge> },
264 { "svg:feMergeNode", make<SPFeMergeNode> },
265 { "svg:feMorphology", make<SPFeMorphology> },
266 { "svg:feOffset", make<SPFeOffset> },
267 { "svg:fePointLight", make<SPFePointLight> },
268 { "svg:feSpecularLighting", make<SPFeSpecularLighting> },
269 { "svg:feSpotLight", make<SPFeSpotLight> },
270 { "svg:feTile", make<SPFeTile> },
271 { "svg:feTurbulence", make<SPFeTurbulence> },
272 { "inkscape:grid", make<SPGrid> },
273
274 // ignore
275 { "rdf:RDF", null }, // no SP node yet
276 { "inkscape:clipboard", null }, // SP node not necessary
277 { "inkscape:templateinfo", null }, // metadata for templates
278 { "inkscape:_templateinfo", null }, // metadata for templates
279 { "", null } // comments
280 };
281};
282
283} // namespace
284
285SPObject *SPFactory::createObject(std::string const &id)
286{
287 return Factory::get().create(id);
288}
289
290bool SPFactory::supportsType(std::string const &id)
291{
292 return Factory::get().supportsId(id);
293}
294
296{
297 switch (node.type()) {
299 return "string";
301 if (auto sptype = node.attribute("sodipodi:type")) {
302 return sptype;
303 }
304 return node.name();
305 default:
306 return "";
307 }
308}
309
310/*
311 Local Variables:
312 mode:c++
313 c-file-style:"stroustrup"
314 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
315 indent-tabs-mode:nil
316 fill-column:99
317 End:
318*/
319// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
SVG blend filter effect.
Interface for refcounted XML nodes.
Definition node.h:80
virtual char const * name() const =0
Get the name of the element node.
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
virtual NodeType type() const =0
Get the type of the node.
GenericEllipseType type
Definition sp-ellipse.h:54
Mesh gradient.
Gradient Meshpatch.
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
Gradient SolidColor.
SPObject of the color-profile object found a direct child of defs.
SVG color matrix filter effect.
SVG <filter> implementation, see sp-filter.cpp.
SVG component transferfilter effect.
SVG composite filter effect.
SVG matrix convolution filter effect.
std::unordered_map< std::string, std::unique_ptr< SPDocument > > map
double c[8][4]
SVG diffuse lighting filter effect.
SVG displacement map filter effect.
SVG <filter> implementation, see sp-filter.cpp.
SVG flood filter effect.
SVG Gaussian blur filter effect.
Inkscape::XML::Node * node
SVG merge filter effect.
feMergeNode implementation.
T * get(GValue *value)
Returns a borrowed pointer to the T held by a value if it holds one, else nullptr.
Definition value-utils.h:64
@ ELEMENT_NODE
Regular element node, e.g. <group />.
@ TEXT_NODE
Text node, e.g. "Some text" in <group>Some text</group> is represented by a text node.
SVG image filter effect.
SVG morphology filter effect.
SVG tile filter effect.
SVG offset filter effect.
SVG <filter> implementation, see sp-filter.cpp.
@ SP_GENERIC_ELLIPSE_CIRCLE
Definition sp-ellipse.h:26
@ SP_GENERIC_ELLIPSE_ARC
Definition sp-ellipse.h:25
@ SP_GENERIC_ELLIPSE_ELLIPSE
Definition sp-ellipse.h:27
SVG <filter> element.
TODO: insert short description here.
TODO: insert short description here.
TODO: insert short description here.
SPGuide – a guideline.
SVG <hatchPath> implementation.
SVG <hatch> implementation.
SVG <image> implementation.
TODO: insert short description here.
TODO: insert short description here.
SPMeshpatch: SVG <meshpatch> implementation.
SPMeshrow: SVG <meshrow> implementation.
SPPage – a page object.
SVG <pattern> implementation.
TODO: insert short description here.
TODO: insert short description here.
SPRoot: SVG <svg> implementation.
SPSolidColor: SVG <solidColor> implementation.
TODO: insert short description here.
TODO: insert short description here.
TODO: insert short description here.
SVG <inkscape:tag> implementation.
TODO: insert short description here.
SVG <tref> implementation, see sp-tref.cpp.
TODO: insert short description here.
SVG specular lighting filter effect.
SVG <filter> implementation, see sp-filter.cpp.
static std::string get_type_string(Inkscape::XML::Node const &node)
static SPObject * createObject(std::string const &id)
static bool supportsType(std::string const &id)
std::unique_ptr< Toolbar >(* create)()
Definition toolbars.cpp:56
SVG turbulence filter effect.