Code

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