Code

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