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