Code

added type enums for arabic-form and orientation attributes
[inkscape.git] / src / sp-glyph.cpp
1 #define __SP_ANCHOR_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);
28 static SPObjectClass *parent_class;
30 GType sp_glyph_get_type(void)
31 {
32     static GType type = 0;
34     if (!type) {
35         GTypeInfo info = {
36             sizeof(SPGlyphClass),
37             NULL,       /* base_init */
38             NULL,       /* base_finalize */
39             (GClassInitFunc) sp_glyph_class_init,
40             NULL,       /* class_finalize */
41             NULL,       /* class_data */
42             sizeof(SPGlyph),
43             16, /* n_preallocs */
44             (GInstanceInitFunc) sp_glyph_init,
45             NULL,       /* value_table */
46         };
47         type = g_type_register_static(SP_TYPE_OBJECT, "SPGlyph", &info, (GTypeFlags) 0);
48     }
50     return type;
51 }
53 static void sp_glyph_class_init(SPGlyphClass *gc)
54 {
55     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
57     parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
59     sp_object_class->build = sp_glyph_build;
60     sp_object_class->release = sp_glyph_release;
61     sp_object_class->set = sp_glyph_set;
62     sp_object_class->write = sp_glyph_write;
63 }
65 static void sp_glyph_init(SPGlyph *glyph)
66 {
67 //TODO: correct these values:
68     glyph->unicode = NULL;
69     glyph->glyph_name = NULL;
70     glyph->d = NULL;
71     glyph->orientation = GLYPH_ORIENTATION_BOTH;
72     glyph->arabic_form = GLYPH_ARABIC_FORM_INITIAL;
73     glyph->lang = NULL;
74     glyph->horiz_adv_x = 0;
75     glyph->vert_origin_x = 0;
76     glyph->vert_origin_y = 0;
77     glyph->vert_adv_y = 0;
78 }
80 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
81 {
82     if (((SPObjectClass *) (parent_class))->build) {
83         ((SPObjectClass *) (parent_class))->build(object, document, repr);
84     }
86     sp_object_read_attr(object, "unicode");
87     sp_object_read_attr(object, "glyph-name");
88     sp_object_read_attr(object, "d");
89     sp_object_read_attr(object, "orientation");
90     sp_object_read_attr(object, "arabic-form");
91     sp_object_read_attr(object, "lang");
92     sp_object_read_attr(object, "horiz-adv-x");
93     sp_object_read_attr(object, "vert-origin-x");
94     sp_object_read_attr(object, "vert-origin-y");
95     sp_object_read_attr(object, "vert-adv-y");
96 }
98 static void sp_glyph_release(SPObject *object)
99 {
100     //SPGlyph *glyph = SP_GLYPH(object);
102     if (((SPObjectClass *) parent_class)->release) {
103         ((SPObjectClass *) parent_class)->release(object);
104     }
107 static glyphArabicForm sp_glyph_read_arabic_form(gchar const *value){
108     if (!value) return GLYPH_ARABIC_FORM_INITIAL; //TODO: verify which is the default default (for me, the spec is not clear)
109     switch(value[0]){
110         case 'i':
111             if (strncmp(value, "initial", 7) == 0) return GLYPH_ARABIC_FORM_INITIAL;
112             if (strncmp(value, "isolated", 8) == 0) return GLYPH_ARABIC_FORM_ISOLATED;
113             break;
114         case 'm':
115             if (strncmp(value, "medial", 6) == 0) return GLYPH_ARABIC_FORM_MEDIAL;
116             break;
117         case 't':
118             if (strncmp(value, "terminal", 8) == 0) return GLYPH_ARABIC_FORM_TERMINAL;
119             break;
120     }
121     return GLYPH_ARABIC_FORM_INITIAL; //TODO: VERIFY DEFAULT!
124 static glyphOrientation sp_glyph_read_orientation(gchar const *value){
125     if (!value) return GLYPH_ORIENTATION_BOTH;
126     switch(value[0]){
127         case 'h':
128             return GLYPH_ORIENTATION_HORIZONTAL;
129             break;
130         case 'v':
131             return GLYPH_ORIENTATION_VERTICAL;
132             break;
133     }
134 //ERROR? TODO: VERIFY PROPER ERROR HANDLING
135     return GLYPH_ORIENTATION_BOTH;
138 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
140     SPGlyph *glyph = SP_GLYPH(object);
141     double number;
142     glyphOrientation orient;
143     glyphArabicForm form;
145     switch (key) {
146         case SP_ATTR_UNICODE:
147             if (glyph->unicode) g_free(glyph->unicode);
148             glyph->unicode = g_strdup(value);
149             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
150 g_warning("SP_ATTR_UNICODE: %s", value);
151              break;
152         case SP_ATTR_GLYPH_NAME:
153             if (glyph->glyph_name) g_free(glyph->glyph_name);
154             glyph->glyph_name = g_strdup(value);
155             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
156 g_warning("SP_ATTR_GLYPH_NAME: %s", value);
157              break;
158         case SP_ATTR_D:
159             if (glyph->d) g_free(glyph->d);
160             glyph->d = g_strdup(value);
161             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
162 g_warning("SP_ATTR_D: %s", value);
163             break;
164         case SP_ATTR_ORIENTATION:
165             orient = sp_glyph_read_orientation(value);
166             if (glyph->orientation != orient){
167                 glyph->orientation = orient;
168                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
169             }
170 g_warning("SP_ATTR_ORIENTATION: %d", orient);
171             break;
172         case SP_ATTR_ARABIC_FORM:
173             form = sp_glyph_read_arabic_form(value);
174             if (glyph->arabic_form != form){
175                 glyph->arabic_form = form;
176                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
177             }
178 g_warning("SP_ATTR_ARABIC_FORM: %d", glyph->arabic_form);
179             break;
180         case SP_ATTR_LANG:
181             if (glyph->lang) g_free(glyph->lang);
182             glyph->lang = g_strdup(value);
183 g_warning("SP_ATTR_LANG: %s", value);
184             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
185             break;
186         case SP_ATTR_HORIZ_ADV_X:
187             number = helperfns_read_number(value);
188             if (number != glyph->horiz_adv_x){
189                 glyph->horiz_adv_x = number;
190 g_warning("SP_ATTR_HORIZ_ADV_X: %f", 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 g_warning("SP_ATTR_VERT_ORIGIN_X: %f", number);
199                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
200             }
201             break;
202         case SP_ATTR_VERT_ORIGIN_Y:
203             number = helperfns_read_number(value);
204             if (number != glyph->vert_origin_y){
205                 glyph->vert_origin_y = number;
206 g_warning("SP_ATTR_VERT_ORIGIN_Y: %f", number);
207                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
208             }
209             break;
210         case SP_ATTR_VERT_ADV_Y:
211             number = helperfns_read_number(value);
212             if (number != glyph->vert_adv_y){
213                 glyph->vert_adv_y = number;
214 g_warning("SP_ATTR_VERT_ADV_Y: %f", number);
215                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
216             }
217             break;
218         default:
219             if (((SPObjectClass *) (parent_class))->set) {
220                 ((SPObjectClass *) (parent_class))->set(object, key, value);
221             }
222             break;
223     }
226 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
228 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
230 //    SPGlyph *glyph = SP_GLYPH(object);
232     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
233         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
234         repr = xml_doc->createElement("svg:glyph");
235     }
237 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
238     repr->setAttribute("unicode", glyph->unicode);
239     repr->setAttribute("glyph-name", glyph->glyph_name);
240     repr->setAttribute("d", glyph->d);
241     sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
242     sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
243     repr->setAttribute("lang", glyph->lang);
244     sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
245     sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
246     sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
247     sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
248 */
249     if (repr != SP_OBJECT_REPR(object)) {
250         COPY_ATTR(repr, object->repr, "unicode");
251         COPY_ATTR(repr, object->repr, "glyph-name");
252         COPY_ATTR(repr, object->repr, "d");
253         COPY_ATTR(repr, object->repr, "orientation");
254         COPY_ATTR(repr, object->repr, "arabic-form");
255         COPY_ATTR(repr, object->repr, "lang");
256         COPY_ATTR(repr, object->repr, "horiz-adv-x");
257         COPY_ATTR(repr, object->repr, "vert-origin-x");
258         COPY_ATTR(repr, object->repr, "vert-origin-y");
259         COPY_ATTR(repr, object->repr, "vert-adv-y");
260     }
262     if (((SPObjectClass *) (parent_class))->write) {
263         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
264     }
266     return repr;
269 /*
270   Local Variables:
271   mode:c++
272   c-file-style:"stroustrup"
273   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
274   indent-tabs-mode:nil
275   fill-column:99
276   End:
277 */
278 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :