Code

SVG 1.1 Conditional Processing Module rendering support (<switch> element, requiredRe...
[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 "sp-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"
47 enum NameType { REPR_NAME, SODIPODI_TYPE };
48 static unsigned const N_NAME_TYPES = SODIPODI_TYPE + 1;
50 static GType name_to_gtype(NameType name_type, gchar const *name);
52 /**
53  * Construct an SPRoot and all its descendents from the given repr.
54  */
55 SPObject *
56 sp_object_repr_build_tree(SPDocument *document, Inkscape::XML::Node *repr)
57 {
58     g_assert(document != NULL);
59     g_assert(repr != NULL);
61     gchar const * const name = repr->name();
62     g_assert(name != NULL);
63     GType const type = name_to_gtype(REPR_NAME, name);
64     g_assert(g_type_is_a(type, SP_TYPE_ROOT));
65     gpointer newobj = g_object_new(type, 0);
66     g_assert(newobj != NULL);
67     SPObject *const object = SP_OBJECT(newobj);
68     g_assert(object != NULL);
69     sp_object_invoke_build(object, document, repr, FALSE);
71     return object;
72 }
74 GType
75 sp_repr_type_lookup(Inkscape::XML::Node *repr)
76 {
77     if ( repr->type() == Inkscape::XML::TEXT_NODE ) {
78         return SP_TYPE_STRING;
79     } else if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
80         gchar const * const type_name = repr->attribute("sodipodi:type");
81         return ( type_name
82                  ? name_to_gtype(SODIPODI_TYPE, type_name)
83                  : name_to_gtype(REPR_NAME, repr->name()) );
84     } else {
85         return 0;
86     }
87 }
89 static GHashTable *t2dtable[N_NAME_TYPES] = {NULL};
91 static void
92 populate_dtables()
93 {
94     struct NameTypeEntry { char const *const name; GType const type_id; };
95     NameTypeEntry const repr_name_entries[] = {
96         { "svg:a", SP_TYPE_ANCHOR },
97         //{ "svg:animate", SP_TYPE_ANIMATE },
98         { "svg:circle", SP_TYPE_CIRCLE },
99         { "svg:color-profile", COLORPROFILE_TYPE },
100         { "svg:clipPath", SP_TYPE_CLIPPATH },
101         { "svg:defs", SP_TYPE_DEFS },
102         { "svg:ellipse", SP_TYPE_ELLIPSE },
103         /* Note: flow* elements are proposed additions for SVG 1.2, they aren't in
104            SVG 1.1. */
105         { "svg:flowDiv", SP_TYPE_FLOWDIV },
106         { "svg:flowLine", SP_TYPE_FLOWLINE },
107         { "svg:flowPara", SP_TYPE_FLOWPARA },
108         { "svg:flowRegion", SP_TYPE_FLOWREGION },
109         { "svg:flowRegionBreak", SP_TYPE_FLOWREGIONBREAK },
110         { "svg:flowRegionExclude", SP_TYPE_FLOWREGIONEXCLUDE },
111         { "svg:flowRoot", SP_TYPE_FLOWTEXT },
112         { "svg:flowSpan", SP_TYPE_FLOWTSPAN },
113         { "svg:g", SP_TYPE_GROUP },
114         { "svg:image", SP_TYPE_IMAGE },
115         { "svg:line", SP_TYPE_LINE },
116         { "svg:linearGradient", SP_TYPE_LINEARGRADIENT },
117         { "svg:marker", SP_TYPE_MARKER },
118         { "svg:mask", SP_TYPE_MASK },
119         { "svg:metadata", SP_TYPE_METADATA },
120         { "svg:path", SP_TYPE_PATH },
121         { "svg:pattern", SP_TYPE_PATTERN },
122         { "svg:polygon", SP_TYPE_POLYGON },
123         { "svg:polyline", SP_TYPE_POLYLINE },
124         { "svg:radialGradient", SP_TYPE_RADIALGRADIENT },
125         { "svg:rect", SP_TYPE_RECT },
126         { "svg:stop", SP_TYPE_STOP },
127         { "svg:svg", SP_TYPE_ROOT },
128         { "svg:style", SP_TYPE_STYLE_ELEM },
129         { "svg:switch", SP_TYPE_SWITCH },
130         { "svg:symbol", SP_TYPE_SYMBOL },
131         { "svg:text", SP_TYPE_TEXT },
132         { "svg:textPath", SP_TYPE_TEXTPATH },
133         { "svg:tspan", SP_TYPE_TSPAN },
134         { "svg:use", SP_TYPE_USE }
135     };
136     NameTypeEntry const sodipodi_name_entries[] = {
137         { "arc", SP_TYPE_ARC },
138         { "inkscape:offset", SP_TYPE_OFFSET },
139         { "spiral", SP_TYPE_SPIRAL },
140         { "star", SP_TYPE_STAR }
141     };
143     NameTypeEntry const *const t2entries[] = {
144         repr_name_entries,
145         sodipodi_name_entries
146     };
147     unsigned const t2n_entries[] = {
148         G_N_ELEMENTS(repr_name_entries),
149         G_N_ELEMENTS(sodipodi_name_entries)
150     };
152     for (unsigned nt = 0; nt < N_NAME_TYPES; ++nt) {
153         NameTypeEntry const *const entries = t2entries[nt];
154         unsigned const n_entries = t2n_entries[nt];
155         GHashTable *&dtable = t2dtable[nt];
157         dtable = g_hash_table_new(g_str_hash, g_str_equal);
158         for (unsigned i = 0; i < n_entries; ++i) {
159             g_hash_table_insert(dtable,
160                                 (void *)entries[i].name,
161                                 (gpointer) entries[i].type_id);
162         }
163     }
166 static inline void
167 ensure_dtables_populated()
169     if (!*t2dtable) {
170         populate_dtables();
171     }
174 static GType
175 name_to_gtype(NameType const name_type, gchar const *name)
177     ensure_dtables_populated();
179     gpointer const data = g_hash_table_lookup(t2dtable[name_type], name);
180     return ( ( data == NULL )
181              ? SP_TYPE_OBJECT
182              : (GType) data );
185 void
186 sp_object_type_register(gchar const *name, GType const gtype)
188     GType const current = name_to_gtype(REPR_NAME, name);
189     if (current == SP_TYPE_OBJECT) {
190         g_hash_table_insert(t2dtable[REPR_NAME],
191                             const_cast<gchar *>(name),
192                             (gpointer) gtype);
193     } else {
194         /* Already registered. */
195         if (current != gtype) {
196             g_warning("repr type `%s' already registered as type #%lu, ignoring attempt to re-register as #%lu.",
197                       name, current, gtype);
198         }
199     }
202 /*
203   Local Variables:
204   mode:c++
205   c-file-style:"stroustrup"
206   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207   indent-tabs-mode:nil
208   fill-column:99
209   End:
210 */
211 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :