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 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);
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 }
240 }
242 /**
243 * * Receives update notifications.
244 * */
245 static void
246 sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
247 {
248 SPGlyph *glyph = SP_GLYPH(object);
249 (void)glyph;
251 if (flags & SP_OBJECT_MODIFIED_FLAG) {
252 /* do something to trigger redisplay, updates? */
253 sp_object_read_attr(object, "unicode");
254 sp_object_read_attr(object, "glyph-name");
255 sp_object_read_attr(object, "d");
256 sp_object_read_attr(object, "orientation");
257 sp_object_read_attr(object, "arabic-form");
258 sp_object_read_attr(object, "lang");
259 sp_object_read_attr(object, "horiz-adv-x");
260 sp_object_read_attr(object, "vert-origin-x");
261 sp_object_read_attr(object, "vert-origin-y");
262 sp_object_read_attr(object, "vert-adv-y");
263 }
265 if (((SPObjectClass *) parent_class)->update) {
266 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
267 }
268 }
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)
273 {
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 COPY_ATTR(repr, object->repr, "unicode");
294 COPY_ATTR(repr, object->repr, "glyph-name");
295 COPY_ATTR(repr, object->repr, "d");
296 COPY_ATTR(repr, object->repr, "orientation");
297 COPY_ATTR(repr, object->repr, "arabic-form");
298 COPY_ATTR(repr, object->repr, "lang");
299 COPY_ATTR(repr, object->repr, "horiz-adv-x");
300 COPY_ATTR(repr, object->repr, "vert-origin-x");
301 COPY_ATTR(repr, object->repr, "vert-origin-y");
302 COPY_ATTR(repr, object->repr, "vert-adv-y");
303 }
305 if (((SPObjectClass *) (parent_class))->write) {
306 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
307 }
309 return repr;
310 }
311 #endif //#ifdef ENABLE_SVG_FONTS
312 /*
313 Local Variables:
314 mode:c++
315 c-file-style:"stroustrup"
316 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
317 indent-tabs-mode:nil
318 fill-column:99
319 End:
320 */
321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :