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