Code

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