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