Code

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