1 #define __SP_OBJECT_REPR_C__
3 /*
4 * Object type dictionary and build frontend
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2003 Lauris Kaplinski
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "sp-defs.h"
15 #include "sp-symbol.h"
16 #include "marker.h"
17 #include "sp-use.h"
18 #include "sp-root.h"
19 #include "sp-image.h"
20 #include "sp-linear-gradient-fns.h"
21 #include "sp-path.h"
22 #include "sp-radial-gradient-fns.h"
23 #include "sp-rect.h"
24 #include "box3d.h"
25 #include "box3d-side.h"
26 #include "persp3d.h"
27 #include "sp-ellipse.h"
28 #include "sp-star.h"
29 #include "sp-stop-fns.h"
30 #include "sp-spiral.h"
31 #include "sp-offset.h"
32 #include "sp-line.h"
33 #include "sp-metadata.h"
34 #include "sp-polyline.h"
35 #include "sp-textpath.h"
36 #include "sp-tref.h"
37 #include "sp-tspan.h"
38 #include "sp-pattern.h"
39 #include "sp-clippath.h"
40 #include "sp-mask.h"
41 #include "sp-anchor.h"
42 //#include "sp-animation.h"
43 #include "sp-flowdiv.h"
44 #include "sp-flowregion.h"
45 #include "sp-flowtext.h"
46 #include "sp-font.h"
47 #include "sp-glyph.h"
48 #include "sp-missing-glyph.h"
49 #include "sp-glyph-kerning.h"
50 #include "sp-style-elem.h"
51 #include "sp-switch.h"
52 #include "color-profile-fns.h"
53 #include "xml/repr.h"
54 #include "sp-filter.h"
55 #include "sp-gaussian-blur.h"
56 #include "sp-feblend.h"
57 #include "sp-fecolormatrix.h"
58 #include "sp-fecomponenttransfer.h"
59 #include "sp-fecomponenttransfer-funcnode.h"
60 #include "sp-fecomposite.h"
61 #include "sp-feconvolvematrix.h"
62 #include "sp-fediffuselighting.h"
63 #include "sp-fedistantlight.h"
64 #include "sp-fedisplacementmap.h"
65 #include "sp-feflood.h"
66 #include "sp-feimage.h"
67 #include "sp-femerge.h"
68 #include "sp-femorphology.h"
69 #include "sp-feoffset.h"
70 #include "sp-fepointlight.h"
71 #include "sp-fespecularlighting.h"
72 #include "sp-fespotlight.h"
73 #include "sp-fetile.h"
74 #include "sp-feturbulence.h"
75 #include "sp-femergenode.h"
76 #include "live_effects/lpeobject.h"
79 enum NameType { REPR_NAME, SODIPODI_TYPE };
80 static unsigned const N_NAME_TYPES = SODIPODI_TYPE + 1;
82 static GType name_to_gtype(NameType name_type, gchar const *name);
84 /**
85 * Construct an SPRoot and all its descendents from the given repr.
86 */
87 SPObject *
88 sp_object_repr_build_tree(SPDocument *document, Inkscape::XML::Node *repr)
89 {
90 g_assert(document != NULL);
91 g_assert(repr != NULL);
93 gchar const * const name = repr->name();
94 g_assert(name != NULL);
95 GType const type = name_to_gtype(REPR_NAME, name);
96 g_assert(g_type_is_a(type, SP_TYPE_ROOT));
97 gpointer newobj = g_object_new(type, 0);
98 g_assert(newobj != NULL);
99 SPObject *const object = SP_OBJECT(newobj);
100 g_assert(object != NULL);
101 sp_object_invoke_build(object, document, repr, FALSE);
103 return object;
104 }
106 GType
107 sp_repr_type_lookup(Inkscape::XML::Node *repr)
108 {
109 if ( repr->type() == Inkscape::XML::TEXT_NODE ) {
110 return SP_TYPE_STRING;
111 } else if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
112 gchar const * const type_name = repr->attribute("sodipodi:type");
113 return ( type_name
114 ? name_to_gtype(SODIPODI_TYPE, type_name)
115 : name_to_gtype(REPR_NAME, repr->name()) );
116 } else {
117 return 0;
118 }
119 }
121 static GHashTable *t2dtable[N_NAME_TYPES] = {NULL};
123 static void
124 populate_dtables()
125 {
126 struct NameTypeEntry { char const *const name; GType const type_id; };
127 NameTypeEntry const repr_name_entries[] = {
128 { "svg:a", SP_TYPE_ANCHOR },
129 //{ "svg:animate", SP_TYPE_ANIMATE },
130 { "svg:circle", SP_TYPE_CIRCLE },
131 { "svg:color-profile", COLORPROFILE_TYPE },
132 { "svg:clipPath", SP_TYPE_CLIPPATH },
133 { "svg:defs", SP_TYPE_DEFS },
134 { "svg:ellipse", SP_TYPE_ELLIPSE },
135 { "svg:filter", SP_TYPE_FILTER },
136 /* Note: flow* elements are proposed additions for SVG 1.2, they aren't in
137 SVG 1.1. */
138 { "svg:flowDiv", SP_TYPE_FLOWDIV },
139 { "svg:flowLine", SP_TYPE_FLOWLINE },
140 { "svg:flowPara", SP_TYPE_FLOWPARA },
141 { "svg:flowRegion", SP_TYPE_FLOWREGION },
142 { "svg:flowRegionBreak", SP_TYPE_FLOWREGIONBREAK },
143 { "svg:flowRegionExclude", SP_TYPE_FLOWREGIONEXCLUDE },
144 { "svg:flowRoot", SP_TYPE_FLOWTEXT },
145 { "svg:flowSpan", SP_TYPE_FLOWTSPAN },
146 { "svg:font", SP_TYPE_FONT },
147 { "svg:glyph", SP_TYPE_GLYPH },
148 { "svg:missing-glyph", SP_TYPE_MISSING_GLYPH },
149 { "svg:hkern", SP_TYPE_HKERN },
150 { "svg:vkern", SP_TYPE_VKERN },
151 { "svg:g", SP_TYPE_GROUP },
152 { "svg:feBlend", SP_TYPE_FEBLEND },
153 { "svg:feColorMatrix", SP_TYPE_FECOLORMATRIX },
154 { "svg:feComponentTransfer", SP_TYPE_FECOMPONENTTRANSFER },
155 { "svg:feComposite", SP_TYPE_FECOMPOSITE },
156 { "svg:feConvolveMatrix", SP_TYPE_FECONVOLVEMATRIX },
157 { "svg:feDiffuseLighting", SP_TYPE_FEDIFFUSELIGHTING },
158 { "svg:feDistantLight", SP_TYPE_FEDISTANTLIGHT },
159 { "svg:feDisplacementMap", SP_TYPE_FEDISPLACEMENTMAP },
160 { "svg:feFlood", SP_TYPE_FEFLOOD },
161 { "svg:feFuncR", SP_TYPE_FEFUNCR },
162 { "svg:feFuncG", SP_TYPE_FEFUNCG },
163 { "svg:feFuncB", SP_TYPE_FEFUNCB },
164 { "svg:feFuncA", SP_TYPE_FEFUNCA },
165 { "svg:feGaussianBlur", SP_TYPE_GAUSSIANBLUR },
166 { "svg:feImage", SP_TYPE_FEIMAGE },
167 { "svg:feMerge", SP_TYPE_FEMERGE },
168 { "svg:feMorphology", SP_TYPE_FEMORPHOLOGY },
169 { "svg:feOffset", SP_TYPE_FEOFFSET },
170 { "svg:fePointLight", SP_TYPE_FEPOINTLIGHT },
171 { "svg:feSpecularLighting", SP_TYPE_FESPECULARLIGHTING },
172 { "svg:feSpotLight", SP_TYPE_FESPOTLIGHT },
173 { "svg:feTile", SP_TYPE_FETILE },
174 { "svg:feTurbulence", SP_TYPE_FETURBULENCE },
175 { "svg:feMergeNode", SP_TYPE_FEMERGENODE },
176 { "svg:image", SP_TYPE_IMAGE },
177 { "svg:line", SP_TYPE_LINE },
178 { "svg:linearGradient", SP_TYPE_LINEARGRADIENT },
179 { "svg:marker", SP_TYPE_MARKER },
180 { "svg:mask", SP_TYPE_MASK },
181 { "svg:metadata", SP_TYPE_METADATA },
182 { "svg:path", SP_TYPE_PATH },
183 { "svg:pattern", SP_TYPE_PATTERN },
184 { "svg:polygon", SP_TYPE_POLYGON },
185 { "svg:polyline", SP_TYPE_POLYLINE },
186 { "svg:radialGradient", SP_TYPE_RADIALGRADIENT },
187 { "svg:rect", SP_TYPE_RECT },
188 { "svg:stop", SP_TYPE_STOP },
189 { "svg:svg", SP_TYPE_ROOT },
190 { "svg:style", SP_TYPE_STYLE_ELEM },
191 { "svg:switch", SP_TYPE_SWITCH },
192 { "svg:symbol", SP_TYPE_SYMBOL },
193 { "svg:text", SP_TYPE_TEXT },
194 { "svg:textPath", SP_TYPE_TEXTPATH },
195 { "svg:tref", SP_TYPE_TREF },
196 { "svg:tspan", SP_TYPE_TSPAN },
197 { "svg:use", SP_TYPE_USE },
198 { "inkscape:path-effect", TYPE_LIVEPATHEFFECT }
199 };
200 NameTypeEntry const sodipodi_name_entries[] = {
201 { "arc", SP_TYPE_ARC },
202 { "inkscape:offset", SP_TYPE_OFFSET },
203 { "spiral", SP_TYPE_SPIRAL },
204 { "star", SP_TYPE_STAR },
205 { "inkscape:box3d", SP_TYPE_BOX3D },
206 { "inkscape:box3dside", SP_TYPE_BOX3D_SIDE },
207 { "inkscape:persp3d", SP_TYPE_PERSP3D }
208 };
210 NameTypeEntry const *const t2entries[] = {
211 repr_name_entries,
212 sodipodi_name_entries
213 };
214 unsigned const t2n_entries[] = {
215 G_N_ELEMENTS(repr_name_entries),
216 G_N_ELEMENTS(sodipodi_name_entries)
217 };
219 for (unsigned nt = 0; nt < N_NAME_TYPES; ++nt) {
220 NameTypeEntry const *const entries = t2entries[nt];
221 unsigned const n_entries = t2n_entries[nt];
222 GHashTable *&dtable = t2dtable[nt];
224 dtable = g_hash_table_new(g_str_hash, g_str_equal);
225 for (unsigned i = 0; i < n_entries; ++i) {
226 g_hash_table_insert(dtable,
227 (void *)entries[i].name,
228 (gpointer) entries[i].type_id);
229 }
230 }
231 }
233 static inline void
234 ensure_dtables_populated()
235 {
236 if (!*t2dtable) {
237 populate_dtables();
238 }
239 }
241 static GType
242 name_to_gtype(NameType const name_type, gchar const *name)
243 {
244 ensure_dtables_populated();
246 gpointer const data = g_hash_table_lookup(t2dtable[name_type], name);
247 return ( ( data == NULL )
248 ? SP_TYPE_OBJECT
249 : (GType) data );
250 }
252 void
253 sp_object_type_register(gchar const *name, GType const gtype)
254 {
255 GType const current = name_to_gtype(REPR_NAME, name);
256 if (current == SP_TYPE_OBJECT) {
257 g_hash_table_insert(t2dtable[REPR_NAME],
258 const_cast<gchar *>(name),
259 (gpointer) gtype);
260 } else {
261 /* Already registered. */
262 if (current != gtype) {
263 g_warning("repr type `%s' already registered as type #%lu, ignoring attempt to re-register as #%lu.",
264 name, current, gtype);
265 }
266 }
267 }
269 /*
270 Local Variables:
271 mode:c++
272 c-file-style:"stroustrup"
273 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
274 indent-tabs-mode:nil
275 fill-column:99
276 End:
277 */
278 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :