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