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