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.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-script.h"
47 #include "config.h"
49 #ifdef ENABLE_SVG_FONTS
50 #include "sp-font.h"
51 #include "sp-font-face.h"
52 #include "sp-glyph.h"
53 #include "sp-missing-glyph.h"
54 #include "sp-glyph-kerning.h"
55 #endif
57 #include "sp-style-elem.h"
58 #include "sp-switch.h"
59 #include "color-profile-fns.h"
60 #include "xml/repr.h"
61 #include "sp-filter.h"
62 #include "sp-gaussian-blur.h"
63 #include "filters/blend.h"
64 #include "filters/colormatrix.h"
65 #include "filters/componenttransfer.h"
66 #include "filters/componenttransfer-funcnode.h"
67 #include "filters/composite.h"
68 #include "filters/convolvematrix.h"
69 #include "filters/diffuselighting.h"
70 #include "filters/distantlight.h"
71 #include "filters/displacementmap.h"
72 #include "filters/flood.h"
73 #include "filters/image.h"
74 #include "filters/merge.h"
75 #include "filters/morphology.h"
76 #include "filters/offset.h"
77 #include "filters/pointlight.h"
78 #include "filters/specularlighting.h"
79 #include "filters/spotlight.h"
80 #include "filters/tile.h"
81 #include "filters/turbulence.h"
82 #include "filters/mergenode.h"
83 #include "live_effects/lpeobject.h"
84 #include "sp-title.h"
85 #include "sp-desc.h"
88 enum NameType { REPR_NAME, SODIPODI_TYPE };
89 static unsigned const N_NAME_TYPES = SODIPODI_TYPE + 1;
91 static GType name_to_gtype(NameType name_type, gchar const *name);
93 /**
94 * Construct an SPRoot and all its descendents from the given XML representation.
95 */
96 void
97 sp_object_repr_build_tree(SPDocument *document, Inkscape::XML::Node *repr)
98 {
99 g_assert(document != NULL);
100 g_assert(repr != NULL);
102 gchar const * const name = repr->name();
103 g_assert(name != NULL);
104 GType const type = name_to_gtype(REPR_NAME, name);
105 g_assert(g_type_is_a(type, SP_TYPE_ROOT));
107 // create and assign root
108 SPObject *root = SP_OBJECT(g_object_new(type, 0));
109 g_assert(root != NULL);
110 document->root = root;
112 // recursively create SP tree elements
113 sp_object_invoke_build(root, document, repr, FALSE);
114 }
116 GType
117 sp_repr_type_lookup(Inkscape::XML::Node *repr)
118 {
119 if ( repr->type() == Inkscape::XML::TEXT_NODE ) {
120 return SP_TYPE_STRING;
121 } else if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
122 gchar const * const type_name = repr->attribute("sodipodi:type");
123 return ( type_name
124 ? name_to_gtype(SODIPODI_TYPE, type_name)
125 : name_to_gtype(REPR_NAME, repr->name()) );
126 } else {
127 return 0;
128 }
129 }
131 static GHashTable *t2dtable[N_NAME_TYPES] = {NULL};
133 static void
134 populate_dtables()
135 {
136 struct NameTypeEntry { char const *const name; GType const type_id; };
137 NameTypeEntry const repr_name_entries[] = {
138 { "svg:a", SP_TYPE_ANCHOR },
139 //{ "svg:animate", SP_TYPE_ANIMATE },
140 { "svg:circle", SP_TYPE_CIRCLE },
141 { "svg:color-profile", COLORPROFILE_TYPE },
142 { "svg:clipPath", SP_TYPE_CLIPPATH },
143 { "svg:defs", SP_TYPE_DEFS },
144 { "svg:desc", SP_TYPE_DESC },
145 { "svg:ellipse", SP_TYPE_ELLIPSE },
146 { "svg:filter", SP_TYPE_FILTER },
147 /* Note: flow* elements are proposed additions for SVG 1.2, they aren't in
148 SVG 1.1. */
149 { "svg:flowDiv", SP_TYPE_FLOWDIV },
150 { "svg:flowLine", SP_TYPE_FLOWLINE },
151 { "svg:flowPara", SP_TYPE_FLOWPARA },
152 { "svg:flowRegion", SP_TYPE_FLOWREGION },
153 { "svg:flowRegionBreak", SP_TYPE_FLOWREGIONBREAK },
154 { "svg:flowRegionExclude", SP_TYPE_FLOWREGIONEXCLUDE },
155 { "svg:flowRoot", SP_TYPE_FLOWTEXT },
156 { "svg:flowSpan", SP_TYPE_FLOWTSPAN },
157 #ifdef ENABLE_SVG_FONTS
158 { "svg:font", SP_TYPE_FONT },
159 { "svg:font-face", SP_TYPE_FONTFACE },
160 { "svg:glyph", SP_TYPE_GLYPH },
161 { "svg:missing-glyph", SP_TYPE_MISSING_GLYPH },
162 { "svg:hkern", SP_TYPE_HKERN },
163 { "svg:vkern", SP_TYPE_VKERN },
164 #endif
165 { "svg:g", SP_TYPE_GROUP },
166 { "svg:feBlend", SP_TYPE_FEBLEND },
167 { "svg:feColorMatrix", SP_TYPE_FECOLORMATRIX },
168 { "svg:feComponentTransfer", SP_TYPE_FECOMPONENTTRANSFER },
169 { "svg:feComposite", SP_TYPE_FECOMPOSITE },
170 { "svg:feConvolveMatrix", SP_TYPE_FECONVOLVEMATRIX },
171 { "svg:feDiffuseLighting", SP_TYPE_FEDIFFUSELIGHTING },
172 { "svg:feDistantLight", SP_TYPE_FEDISTANTLIGHT },
173 { "svg:feDisplacementMap", SP_TYPE_FEDISPLACEMENTMAP },
174 { "svg:feFlood", SP_TYPE_FEFLOOD },
175 { "svg:feFuncR", SP_TYPE_FEFUNCR },
176 { "svg:feFuncG", SP_TYPE_FEFUNCG },
177 { "svg:feFuncB", SP_TYPE_FEFUNCB },
178 { "svg:feFuncA", SP_TYPE_FEFUNCA },
179 { "svg:feGaussianBlur", SP_TYPE_GAUSSIANBLUR },
180 { "svg:feImage", SP_TYPE_FEIMAGE },
181 { "svg:feMerge", SP_TYPE_FEMERGE },
182 { "svg:feMorphology", SP_TYPE_FEMORPHOLOGY },
183 { "svg:feOffset", SP_TYPE_FEOFFSET },
184 { "svg:fePointLight", SP_TYPE_FEPOINTLIGHT },
185 { "svg:feSpecularLighting", SP_TYPE_FESPECULARLIGHTING },
186 { "svg:feSpotLight", SP_TYPE_FESPOTLIGHT },
187 { "svg:feTile", SP_TYPE_FETILE },
188 { "svg:feTurbulence", SP_TYPE_FETURBULENCE },
189 { "svg:feMergeNode", SP_TYPE_FEMERGENODE },
190 { "svg:image", SP_TYPE_IMAGE },
191 { "svg:line", SP_TYPE_LINE },
192 { "svg:linearGradient", SP_TYPE_LINEARGRADIENT },
193 { "svg:marker", SP_TYPE_MARKER },
194 { "svg:mask", SP_TYPE_MASK },
195 { "svg:metadata", SP_TYPE_METADATA },
196 { "svg:path", SP_TYPE_PATH },
197 { "svg:pattern", SP_TYPE_PATTERN },
198 { "svg:polygon", SP_TYPE_POLYGON },
199 { "svg:polyline", SP_TYPE_POLYLINE },
200 { "svg:radialGradient", SP_TYPE_RADIALGRADIENT },
201 { "svg:rect", SP_TYPE_RECT },
202 { "svg:stop", SP_TYPE_STOP },
203 { "svg:script", SP_TYPE_SCRIPT },
204 { "svg:svg", SP_TYPE_ROOT },
205 { "svg:style", SP_TYPE_STYLE_ELEM },
206 { "svg:switch", SP_TYPE_SWITCH },
207 { "svg:symbol", SP_TYPE_SYMBOL },
208 { "svg:text", SP_TYPE_TEXT },
209 { "svg:textPath", SP_TYPE_TEXTPATH },
210 { "svg:title", SP_TYPE_TITLE },
211 { "svg:tref", SP_TYPE_TREF },
212 { "svg:tspan", SP_TYPE_TSPAN },
213 { "svg:use", SP_TYPE_USE },
214 { "inkscape:path-effect", TYPE_LIVEPATHEFFECT }
215 };
216 NameTypeEntry const sodipodi_name_entries[] = {
217 { "arc", SP_TYPE_ARC },
218 { "inkscape:offset", SP_TYPE_OFFSET },
219 { "spiral", SP_TYPE_SPIRAL },
220 { "star", SP_TYPE_STAR },
221 { "inkscape:box3d", SP_TYPE_BOX3D },
222 { "inkscape:box3dside", SP_TYPE_BOX3D_SIDE },
223 { "inkscape:persp3d", SP_TYPE_PERSP3D }
224 };
226 NameTypeEntry const *const t2entries[] = {
227 repr_name_entries,
228 sodipodi_name_entries
229 };
230 unsigned const t2n_entries[] = {
231 G_N_ELEMENTS(repr_name_entries),
232 G_N_ELEMENTS(sodipodi_name_entries)
233 };
235 for (unsigned nt = 0; nt < N_NAME_TYPES; ++nt) {
236 NameTypeEntry const *const entries = t2entries[nt];
237 unsigned const n_entries = t2n_entries[nt];
238 GHashTable *&dtable = t2dtable[nt];
240 dtable = g_hash_table_new(g_str_hash, g_str_equal);
241 for (unsigned i = 0; i < n_entries; ++i) {
242 g_hash_table_insert(dtable,
243 (void *)entries[i].name,
244 (gpointer) entries[i].type_id);
245 }
246 }
247 }
249 static inline void
250 ensure_dtables_populated()
251 {
252 if (!*t2dtable) {
253 populate_dtables();
254 }
255 }
257 static GType
258 name_to_gtype(NameType const name_type, gchar const *name)
259 {
260 ensure_dtables_populated();
262 gpointer const data = g_hash_table_lookup(t2dtable[name_type], name);
263 return ( ( data == NULL )
264 ? SP_TYPE_OBJECT
265 : (GType) data );
266 }
268 void
269 sp_object_type_register(gchar const *name, GType const gtype)
270 {
271 GType const current = name_to_gtype(REPR_NAME, name);
272 if (current == SP_TYPE_OBJECT) {
273 g_hash_table_insert(t2dtable[REPR_NAME],
274 const_cast<gchar *>(name),
275 (gpointer) gtype);
276 } else {
277 /* Already registered. */
278 if (current != gtype) {
279 g_warning("repr type `%s' already registered as type #%lu, ignoring attempt to re-register as #%lu.",
280 name, current, gtype);
281 }
282 }
283 }
285 /*
286 Local Variables:
287 mode:c++
288 c-file-style:"stroustrup"
289 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
290 indent-tabs-mode:nil
291 fill-column:99
292 End:
293 */
294 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :