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");
103 }
105 static void sp_glyph_release(SPObject *object)
106 {
107 //SPGlyph *glyph = SP_GLYPH(object);
109 if (((SPObjectClass *) parent_class)->release) {
110 ((SPObjectClass *) parent_class)->release(object);
111 }
112 }
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!
129 }
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;
143 }
145 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
146 {
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 g_warning("<glyph>: SP_ATTR_UNICODE: %s", value);
158 break;
159 case SP_ATTR_GLYPH_NAME:
160 if (glyph->glyph_name) g_free(glyph->glyph_name);
161 glyph->glyph_name = g_strdup(value);
162 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
163 g_warning("<glyph>: SP_ATTR_GLYPH_NAME: %s", value);
164 break;
165 case SP_ATTR_D:
166 if (glyph->d) g_free(glyph->d);
167 glyph->d = g_strdup(value);
168 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
169 g_warning("<glyph>: SP_ATTR_D: %s", value);
170 break;
171 case SP_ATTR_ORIENTATION:
172 orient = sp_glyph_read_orientation(value);
173 if (glyph->orientation != orient){
174 glyph->orientation = orient;
175 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
176 }
177 g_warning("<glyph>: SP_ATTR_ORIENTATION: %d", orient);
178 break;
179 case SP_ATTR_ARABIC_FORM:
180 form = sp_glyph_read_arabic_form(value);
181 if (glyph->arabic_form != form){
182 glyph->arabic_form = form;
183 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
184 }
185 g_warning("<glyph>: SP_ATTR_ARABIC_FORM: %d", glyph->arabic_form);
186 break;
187 case SP_ATTR_LANG:
188 if (glyph->lang) g_free(glyph->lang);
189 glyph->lang = g_strdup(value);
190 g_warning("<glyph>: SP_ATTR_LANG: %s", value);
191 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
192 break;
193 case SP_ATTR_HORIZ_ADV_X:
194 number = helperfns_read_number(value);
195 if (number != glyph->horiz_adv_x){
196 glyph->horiz_adv_x = number;
197 g_warning("<glyph>: SP_ATTR_HORIZ_ADV_X: %f", number);
198 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
199 }
200 break;
201 case SP_ATTR_VERT_ORIGIN_X:
202 number = helperfns_read_number(value);
203 if (number != glyph->vert_origin_x){
204 glyph->vert_origin_x = number;
205 g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_X: %f", number);
206 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
207 }
208 break;
209 case SP_ATTR_VERT_ORIGIN_Y:
210 number = helperfns_read_number(value);
211 if (number != glyph->vert_origin_y){
212 glyph->vert_origin_y = number;
213 g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_Y: %f", number);
214 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
215 }
216 break;
217 case SP_ATTR_VERT_ADV_Y:
218 number = helperfns_read_number(value);
219 if (number != glyph->vert_adv_y){
220 glyph->vert_adv_y = number;
221 g_warning("<glyph>: SP_ATTR_VERT_ADV_Y: %f", number);
222 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
223 }
224 break;
225 default:
226 if (((SPObjectClass *) (parent_class))->set) {
227 ((SPObjectClass *) (parent_class))->set(object, key, value);
228 }
229 break;
230 }
231 }
233 /**
234 * * Receives update notifications.
235 * */
236 static void
237 sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
238 {
239 SPGlyph *glyph = SP_GLYPH(object);
240 (void)glyph;
242 if (flags & SP_OBJECT_MODIFIED_FLAG) {
243 /* do something to trigger redisplay, updates? */
244 sp_object_read_attr(object, "unicode");
245 sp_object_read_attr(object, "glyph-name");
246 sp_object_read_attr(object, "d");
247 sp_object_read_attr(object, "orientation");
248 sp_object_read_attr(object, "arabic-form");
249 sp_object_read_attr(object, "lang");
250 sp_object_read_attr(object, "horiz-adv-x");
251 sp_object_read_attr(object, "vert-origin-x");
252 sp_object_read_attr(object, "vert-origin-y");
253 sp_object_read_attr(object, "vert-adv-y");
254 }
256 if (((SPObjectClass *) parent_class)->update) {
257 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
258 }
259 }
261 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
263 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
264 {
265 // SPGlyph *glyph = SP_GLYPH(object);
267 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
268 repr = xml_doc->createElement("svg:glyph");
269 }
271 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
272 repr->setAttribute("unicode", glyph->unicode);
273 repr->setAttribute("glyph-name", glyph->glyph_name);
274 repr->setAttribute("d", glyph->d);
275 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
276 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
277 repr->setAttribute("lang", glyph->lang);
278 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
279 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
280 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
281 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
282 */
283 if (repr != SP_OBJECT_REPR(object)) {
284 COPY_ATTR(repr, object->repr, "unicode");
285 COPY_ATTR(repr, object->repr, "glyph-name");
286 COPY_ATTR(repr, object->repr, "d");
287 COPY_ATTR(repr, object->repr, "orientation");
288 COPY_ATTR(repr, object->repr, "arabic-form");
289 COPY_ATTR(repr, object->repr, "lang");
290 COPY_ATTR(repr, object->repr, "horiz-adv-x");
291 COPY_ATTR(repr, object->repr, "vert-origin-x");
292 COPY_ATTR(repr, object->repr, "vert-origin-y");
293 COPY_ATTR(repr, object->repr, "vert-adv-y");
294 }
296 if (((SPObjectClass *) (parent_class))->write) {
297 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
298 }
300 return repr;
301 }
302 #endif //#ifdef ENABLE_SVG_FONTS
303 /*
304 Local Variables:
305 mode:c++
306 c-file-style:"stroustrup"
307 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
308 indent-tabs-mode:nil
309 fill-column:99
310 End:
311 */
312 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :