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