1 #define __SP_GLYPH_C__
3 /*
4 * SVG <glyph> element implementation
5 *
6 * Author:
7 * Felipe C. da S. Sanches <felipe.sanches@gmail.com>
8 *
9 * Copyright (C) 2008, Felipe C. da S. Sanches
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "xml/repr.h"
15 #include "attributes.h"
16 #include "sp-glyph.h"
17 #include "document.h"
18 #include "helper-fns.h"
20 static void sp_glyph_class_init(SPGlyphClass *gc);
21 static void sp_glyph_init(SPGlyph *glyph);
23 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
24 static void sp_glyph_release(SPObject *object);
25 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value);
26 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
27 static void sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags);
29 static SPObjectClass *parent_class;
31 GType sp_glyph_get_type(void)
32 {
33 static GType type = 0;
35 if (!type) {
36 GTypeInfo info = {
37 sizeof(SPGlyphClass),
38 NULL, /* base_init */
39 NULL, /* base_finalize */
40 (GClassInitFunc) sp_glyph_class_init,
41 NULL, /* class_finalize */
42 NULL, /* class_data */
43 sizeof(SPGlyph),
44 16, /* n_preallocs */
45 (GInstanceInitFunc) sp_glyph_init,
46 NULL, /* value_table */
47 };
48 type = g_type_register_static(SP_TYPE_OBJECT, "SPGlyph", &info, (GTypeFlags) 0);
49 }
51 return type;
52 }
54 static void sp_glyph_class_init(SPGlyphClass *gc)
55 {
56 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
58 parent_class = (SPObjectClass*)g_type_class_peek_parent(gc);
60 sp_object_class->build = sp_glyph_build;
61 sp_object_class->release = sp_glyph_release;
62 sp_object_class->set = sp_glyph_set;
63 sp_object_class->write = sp_glyph_write;
64 sp_object_class->update = sp_glyph_update;
65 }
67 static void sp_glyph_init(SPGlyph *glyph)
68 {
69 //TODO: correct these values:
70 glyph->unicode = NULL;
71 glyph->glyph_name = NULL;
72 glyph->d = NULL;
73 glyph->orientation = GLYPH_ORIENTATION_BOTH;
74 glyph->arabic_form = GLYPH_ARABIC_FORM_INITIAL;
75 glyph->lang = NULL;
76 glyph->horiz_adv_x = 0;
77 glyph->vert_origin_x = 0;
78 glyph->vert_origin_y = 0;
79 glyph->vert_adv_y = 0;
80 }
82 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
83 {
84 if (((SPObjectClass *) (parent_class))->build) {
85 ((SPObjectClass *) (parent_class))->build(object, document, repr);
86 }
88 sp_object_read_attr(object, "unicode");
89 sp_object_read_attr(object, "glyph-name");
90 sp_object_read_attr(object, "d");
91 sp_object_read_attr(object, "orientation");
92 sp_object_read_attr(object, "arabic-form");
93 sp_object_read_attr(object, "lang");
94 sp_object_read_attr(object, "horiz-adv-x");
95 sp_object_read_attr(object, "vert-origin-x");
96 sp_object_read_attr(object, "vert-origin-y");
97 sp_object_read_attr(object, "vert-adv-y");
98 }
100 static void sp_glyph_release(SPObject *object)
101 {
102 //SPGlyph *glyph = SP_GLYPH(object);
104 if (((SPObjectClass *) parent_class)->release) {
105 ((SPObjectClass *) parent_class)->release(object);
106 }
107 }
109 static glyphArabicForm sp_glyph_read_arabic_form(gchar const *value){
110 if (!value) return GLYPH_ARABIC_FORM_INITIAL; //TODO: verify which is the default default (for me, the spec is not clear)
111 switch(value[0]){
112 case 'i':
113 if (strncmp(value, "initial", 7) == 0) return GLYPH_ARABIC_FORM_INITIAL;
114 if (strncmp(value, "isolated", 8) == 0) return GLYPH_ARABIC_FORM_ISOLATED;
115 break;
116 case 'm':
117 if (strncmp(value, "medial", 6) == 0) return GLYPH_ARABIC_FORM_MEDIAL;
118 break;
119 case 't':
120 if (strncmp(value, "terminal", 8) == 0) return GLYPH_ARABIC_FORM_TERMINAL;
121 break;
122 }
123 return GLYPH_ARABIC_FORM_INITIAL; //TODO: VERIFY DEFAULT!
124 }
126 static glyphOrientation sp_glyph_read_orientation(gchar const *value){
127 if (!value) return GLYPH_ORIENTATION_BOTH;
128 switch(value[0]){
129 case 'h':
130 return GLYPH_ORIENTATION_HORIZONTAL;
131 break;
132 case 'v':
133 return GLYPH_ORIENTATION_VERTICAL;
134 break;
135 }
136 //ERROR? TODO: VERIFY PROPER ERROR HANDLING
137 return GLYPH_ORIENTATION_BOTH;
138 }
140 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
141 {
142 SPGlyph *glyph = SP_GLYPH(object);
143 double number;
144 glyphOrientation orient;
145 glyphArabicForm form;
147 switch (key) {
148 case SP_ATTR_UNICODE:
149 if (glyph->unicode) g_free(glyph->unicode);
150 glyph->unicode = g_strdup(value);
151 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
152 g_warning("<glyph>: SP_ATTR_UNICODE: %s", value);
153 break;
154 case SP_ATTR_GLYPH_NAME:
155 if (glyph->glyph_name) g_free(glyph->glyph_name);
156 glyph->glyph_name = g_strdup(value);
157 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
158 g_warning("<glyph>: SP_ATTR_GLYPH_NAME: %s", value);
159 break;
160 case SP_ATTR_D:
161 if (glyph->d) g_free(glyph->d);
162 glyph->d = g_strdup(value);
163 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
164 g_warning("<glyph>: SP_ATTR_D: %s", value);
165 break;
166 case SP_ATTR_ORIENTATION:
167 orient = sp_glyph_read_orientation(value);
168 if (glyph->orientation != orient){
169 glyph->orientation = orient;
170 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
171 }
172 g_warning("<glyph>: SP_ATTR_ORIENTATION: %d", orient);
173 break;
174 case SP_ATTR_ARABIC_FORM:
175 form = sp_glyph_read_arabic_form(value);
176 if (glyph->arabic_form != form){
177 glyph->arabic_form = form;
178 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
179 }
180 g_warning("<glyph>: SP_ATTR_ARABIC_FORM: %d", glyph->arabic_form);
181 break;
182 case SP_ATTR_LANG:
183 if (glyph->lang) g_free(glyph->lang);
184 glyph->lang = g_strdup(value);
185 g_warning("<glyph>: SP_ATTR_LANG: %s", value);
186 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
187 break;
188 case SP_ATTR_HORIZ_ADV_X:
189 number = helperfns_read_number(value);
190 if (number != glyph->horiz_adv_x){
191 glyph->horiz_adv_x = number;
192 g_warning("<glyph>: SP_ATTR_HORIZ_ADV_X: %f", number);
193 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
194 }
195 break;
196 case SP_ATTR_VERT_ORIGIN_X:
197 number = helperfns_read_number(value);
198 if (number != glyph->vert_origin_x){
199 glyph->vert_origin_x = number;
200 g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_X: %f", number);
201 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
202 }
203 break;
204 case SP_ATTR_VERT_ORIGIN_Y:
205 number = helperfns_read_number(value);
206 if (number != glyph->vert_origin_y){
207 glyph->vert_origin_y = number;
208 g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_Y: %f", number);
209 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
210 }
211 break;
212 case SP_ATTR_VERT_ADV_Y:
213 number = helperfns_read_number(value);
214 if (number != glyph->vert_adv_y){
215 glyph->vert_adv_y = number;
216 g_warning("<glyph>: SP_ATTR_VERT_ADV_Y: %f", number);
217 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
218 }
219 break;
220 default:
221 if (((SPObjectClass *) (parent_class))->set) {
222 ((SPObjectClass *) (parent_class))->set(object, key, value);
223 }
224 break;
225 }
226 }
228 /**
229 * * Receives update notifications.
230 * */
231 static void
232 sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
233 {
234 SPGlyph *glyph = SP_GLYPH(object);
235 (void)glyph;
237 if (flags & SP_OBJECT_MODIFIED_FLAG) {
238 /* do something to trigger redisplay, updates? */
239 sp_object_read_attr(object, "unicode");
240 sp_object_read_attr(object, "glyph-name");
241 sp_object_read_attr(object, "d");
242 sp_object_read_attr(object, "orientation");
243 sp_object_read_attr(object, "arabic-form");
244 sp_object_read_attr(object, "lang");
245 sp_object_read_attr(object, "horiz-adv-x");
246 sp_object_read_attr(object, "vert-origin-x");
247 sp_object_read_attr(object, "vert-origin-y");
248 sp_object_read_attr(object, "vert-adv-y");
249 }
251 if (((SPObjectClass *) parent_class)->update) {
252 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
253 }
254 }
256 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
258 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
259 {
260 // SPGlyph *glyph = SP_GLYPH(object);
262 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
263 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
264 repr = xml_doc->createElement("svg:glyph");
265 }
267 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
268 repr->setAttribute("unicode", glyph->unicode);
269 repr->setAttribute("glyph-name", glyph->glyph_name);
270 repr->setAttribute("d", glyph->d);
271 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
272 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
273 repr->setAttribute("lang", glyph->lang);
274 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
275 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
276 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
277 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
278 */
279 if (repr != SP_OBJECT_REPR(object)) {
280 COPY_ATTR(repr, object->repr, "unicode");
281 COPY_ATTR(repr, object->repr, "glyph-name");
282 COPY_ATTR(repr, object->repr, "d");
283 COPY_ATTR(repr, object->repr, "orientation");
284 COPY_ATTR(repr, object->repr, "arabic-form");
285 COPY_ATTR(repr, object->repr, "lang");
286 COPY_ATTR(repr, object->repr, "horiz-adv-x");
287 COPY_ATTR(repr, object->repr, "vert-origin-x");
288 COPY_ATTR(repr, object->repr, "vert-origin-y");
289 COPY_ATTR(repr, object->repr, "vert-adv-y");
290 }
292 if (((SPObjectClass *) (parent_class))->write) {
293 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
294 }
296 return repr;
297 }
299 /*
300 Local Variables:
301 mode:c++
302 c-file-style:"stroustrup"
303 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
304 indent-tabs-mode:nil
305 fill-column:99
306 End:
307 */
308 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :