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