Code

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