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