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