Code

A simple layout document as to what, why and how is cppification.
[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 <juca@members.fsf.org>
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"
24 static void sp_glyph_class_init(SPGlyphClass *gc);
25 static void sp_glyph_init(SPGlyph *glyph);
27 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
28 static void sp_glyph_release(SPObject *object);
29 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value);
30 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
31 static void sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags);
33 static SPObjectClass *parent_class;
35 GType sp_glyph_get_type(void)
36 {
37     static GType type = 0;
39     if (!type) {
40         GTypeInfo info = {
41             sizeof(SPGlyphClass),
42             NULL,       /* base_init */
43             NULL,       /* base_finalize */
44             (GClassInitFunc) sp_glyph_class_init,
45             NULL,       /* class_finalize */
46             NULL,       /* class_data */
47             sizeof(SPGlyph),
48             16, /* n_preallocs */
49             (GInstanceInitFunc) sp_glyph_init,
50             NULL,       /* value_table */
51         };
52         type = g_type_register_static(SP_TYPE_OBJECT, "SPGlyph", &info, (GTypeFlags) 0);
53     }
55     return type;
56 }
58 static void sp_glyph_class_init(SPGlyphClass *gc)
59 {
60     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
62     parent_class = (SPObjectClass*)g_type_class_peek_parent(gc);
64     sp_object_class->build = sp_glyph_build;
65     sp_object_class->release = sp_glyph_release;
66     sp_object_class->set = sp_glyph_set;
67     sp_object_class->write = sp_glyph_write;
68     sp_object_class->update = sp_glyph_update;
69 }
71 static void sp_glyph_init(SPGlyph *glyph)
72 {
73 //TODO: correct these values:
75     new (&glyph->unicode) Glib::ustring();
76     new (&glyph->glyph_name) Glib::ustring();
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     object->readAttr( "unicode");
94     object->readAttr( "glyph-name");
95     object->readAttr( "d");
96     object->readAttr( "orientation");
97     object->readAttr( "arabic-form");
98     object->readAttr( "lang");
99     object->readAttr( "horiz-adv-x");
100     object->readAttr( "vert-origin-x");
101     object->readAttr( "vert-origin-y");
102     object->readAttr( "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);
149     switch (key) {
150         case SP_ATTR_UNICODE:
151         {
152             glyph->unicode.clear();
153             if (value) glyph->unicode.append(value);
154             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
155             break;
156         }
157         case SP_ATTR_GLYPH_NAME:
158         {
159             glyph->glyph_name.clear();
160             if (value) glyph->glyph_name.append(value);
161             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
162             break;
163         }
164         case SP_ATTR_D:
165         {
166             if (glyph->d) g_free(glyph->d);
167             glyph->d = g_strdup(value);
168             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
169             break;
170         }
171         case SP_ATTR_ORIENTATION:
172         {
173             glyphOrientation orient = sp_glyph_read_orientation(value);
174             if (glyph->orientation != orient){
175                 glyph->orientation = orient;
176                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
177             }
178             break;
179         }
180         case SP_ATTR_ARABIC_FORM:
181         {
182             glyphArabicForm form = sp_glyph_read_arabic_form(value);
183             if (glyph->arabic_form != form){
184                 glyph->arabic_form = form;
185                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
186             }
187             break;
188         }
189         case SP_ATTR_LANG:
190         {
191             if (glyph->lang) g_free(glyph->lang);
192             glyph->lang = g_strdup(value);
193             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
194             break;
195         }
196         case SP_ATTR_HORIZ_ADV_X:
197         {
198             double number = value ? g_ascii_strtod(value, 0) : 0;
199             if (number != glyph->horiz_adv_x){
200                 glyph->horiz_adv_x = number;
201                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
202             }
203             break;
204         }
205         case SP_ATTR_VERT_ORIGIN_X:
206         {
207             double number = value ? g_ascii_strtod(value, 0) : 0;
208             if (number != glyph->vert_origin_x){
209                 glyph->vert_origin_x = number;
210                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
211             }
212             break;
213         }
214         case SP_ATTR_VERT_ORIGIN_Y:
215         {
216             double number = value ? g_ascii_strtod(value, 0) : 0;
217             if (number != glyph->vert_origin_y){
218                 glyph->vert_origin_y = number;
219                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
220             }
221             break;
222         }
223         case SP_ATTR_VERT_ADV_Y:
224         {
225             double number = value ? g_ascii_strtod(value, 0) : 0;
226             if (number != glyph->vert_adv_y){
227                 glyph->vert_adv_y = number;
228                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
229             }
230             break;
231         }
232         default:
233         {
234             if (((SPObjectClass *) (parent_class))->set) {
235                 ((SPObjectClass *) (parent_class))->set(object, key, value);
236             }
237             break;
238         }
239     }
242 /**
243  *  * Receives update notifications.
244  *   */
245 static void
246 sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
248     SPGlyph *glyph = SP_GLYPH(object);
249     (void)glyph;
251     if (flags & SP_OBJECT_MODIFIED_FLAG) {
252         /* do something to trigger redisplay, updates? */
253             object->readAttr( "unicode");
254             object->readAttr( "glyph-name");
255             object->readAttr( "d");
256             object->readAttr( "orientation");
257             object->readAttr( "arabic-form");
258             object->readAttr( "lang");
259             object->readAttr( "horiz-adv-x");
260             object->readAttr( "vert-origin-x");
261             object->readAttr( "vert-origin-y");
262             object->readAttr( "vert-adv-y");
263     }
265     if (((SPObjectClass *) parent_class)->update) {
266         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
267     }
270 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
272 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
274 //    SPGlyph *glyph = SP_GLYPH(object);
276     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
277         repr = xml_doc->createElement("svg:glyph");
278     }
280 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
281     repr->setAttribute("unicode", glyph->unicode);
282     repr->setAttribute("glyph-name", glyph->glyph_name);
283     repr->setAttribute("d", glyph->d);
284     sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
285     sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
286     repr->setAttribute("lang", glyph->lang);
287     sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
288     sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
289     sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
290     sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
291 */
292     if (repr != SP_OBJECT_REPR(object)) {
293                 /* All the COPY_ATTR functions below use
294                    XML Tree directly while they shouldn't. */
295         COPY_ATTR(repr, object->getRepr(), "unicode");
296         COPY_ATTR(repr, object->getRepr(), "glyph-name");
297         COPY_ATTR(repr, object->getRepr(), "d");
298         COPY_ATTR(repr, object->getRepr(), "orientation");
299         COPY_ATTR(repr, object->getRepr(), "arabic-form");
300         COPY_ATTR(repr, object->getRepr(), "lang");
301         COPY_ATTR(repr, object->getRepr(), "horiz-adv-x");
302         COPY_ATTR(repr, object->getRepr(), "vert-origin-x");
303         COPY_ATTR(repr, object->getRepr(), "vert-origin-y");
304         COPY_ATTR(repr, object->getRepr(), "vert-adv-y");
305     }
307     if (((SPObjectClass *) (parent_class))->write) {
308         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
309     }
311     return repr;
313 #endif //#ifdef ENABLE_SVG_FONTS
314 /*
315   Local Variables:
316   mode:c++
317   c-file-style:"stroustrup"
318   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
319   indent-tabs-mode:nil
320   fill-column:99
321   End:
322 */
323 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :