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:
76 new (&glyph->unicode) Glib::ustring();
77 new (&glyph->glyph_name) Glib::ustring();
78 glyph->d = NULL;
79 glyph->orientation = GLYPH_ORIENTATION_BOTH;
80 glyph->arabic_form = GLYPH_ARABIC_FORM_INITIAL;
81 glyph->lang = NULL;
82 glyph->horiz_adv_x = 0;
83 glyph->vert_origin_x = 0;
84 glyph->vert_origin_y = 0;
85 glyph->vert_adv_y = 0;
86 }
88 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
89 {
90 if (((SPObjectClass *) (parent_class))->build) {
91 ((SPObjectClass *) (parent_class))->build(object, document, repr);
92 }
94 sp_object_read_attr(object, "unicode");
95 sp_object_read_attr(object, "glyph-name");
96 sp_object_read_attr(object, "d");
97 sp_object_read_attr(object, "orientation");
98 sp_object_read_attr(object, "arabic-form");
99 sp_object_read_attr(object, "lang");
100 sp_object_read_attr(object, "horiz-adv-x");
101 sp_object_read_attr(object, "vert-origin-x");
102 sp_object_read_attr(object, "vert-origin-y");
103 sp_object_read_attr(object, "vert-adv-y");
104 }
106 static void sp_glyph_release(SPObject *object)
107 {
108 //SPGlyph *glyph = SP_GLYPH(object);
110 if (((SPObjectClass *) parent_class)->release) {
111 ((SPObjectClass *) parent_class)->release(object);
112 }
113 }
115 static glyphArabicForm sp_glyph_read_arabic_form(gchar const *value){
116 if (!value) return GLYPH_ARABIC_FORM_INITIAL; //TODO: verify which is the default default (for me, the spec is not clear)
117 switch(value[0]){
118 case 'i':
119 if (strncmp(value, "initial", 7) == 0) return GLYPH_ARABIC_FORM_INITIAL;
120 if (strncmp(value, "isolated", 8) == 0) return GLYPH_ARABIC_FORM_ISOLATED;
121 break;
122 case 'm':
123 if (strncmp(value, "medial", 6) == 0) return GLYPH_ARABIC_FORM_MEDIAL;
124 break;
125 case 't':
126 if (strncmp(value, "terminal", 8) == 0) return GLYPH_ARABIC_FORM_TERMINAL;
127 break;
128 }
129 return GLYPH_ARABIC_FORM_INITIAL; //TODO: VERIFY DEFAULT!
130 }
132 static glyphOrientation sp_glyph_read_orientation(gchar const *value){
133 if (!value) return GLYPH_ORIENTATION_BOTH;
134 switch(value[0]){
135 case 'h':
136 return GLYPH_ORIENTATION_HORIZONTAL;
137 break;
138 case 'v':
139 return GLYPH_ORIENTATION_VERTICAL;
140 break;
141 }
142 //ERROR? TODO: VERIFY PROPER ERROR HANDLING
143 return GLYPH_ORIENTATION_BOTH;
144 }
146 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
147 {
148 SPGlyph *glyph = SP_GLYPH(object);
149 double number;
150 glyphOrientation orient;
151 glyphArabicForm form;
153 switch (key) {
154 case SP_ATTR_UNICODE:
155 glyph->unicode.clear();
156 if (value) glyph->unicode.append(value);
157 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
158 break;
159 case SP_ATTR_GLYPH_NAME:
160 glyph->glyph_name.clear();
161 if (value) glyph->glyph_name.append(value);
162 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
163 break;
164 case SP_ATTR_D:
165 if (glyph->d) g_free(glyph->d);
166 glyph->d = g_strdup(value);
167 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
168 break;
169 case SP_ATTR_ORIENTATION:
170 orient = sp_glyph_read_orientation(value);
171 if (glyph->orientation != orient){
172 glyph->orientation = orient;
173 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
174 }
175 break;
176 case SP_ATTR_ARABIC_FORM:
177 form = sp_glyph_read_arabic_form(value);
178 if (glyph->arabic_form != form){
179 glyph->arabic_form = form;
180 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
181 }
182 break;
183 case SP_ATTR_LANG:
184 if (glyph->lang) g_free(glyph->lang);
185 glyph->lang = g_strdup(value);
186 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
187 break;
188 case SP_ATTR_HORIZ_ADV_X:
189 number = helperfns_read_number(value);
190 if (number != glyph->horiz_adv_x){
191 glyph->horiz_adv_x = number;
192 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
193 }
194 break;
195 case SP_ATTR_VERT_ORIGIN_X:
196 number = helperfns_read_number(value);
197 if (number != glyph->vert_origin_x){
198 glyph->vert_origin_x = 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 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
207 }
208 break;
209 case SP_ATTR_VERT_ADV_Y:
210 number = helperfns_read_number(value);
211 if (number != glyph->vert_adv_y){
212 glyph->vert_adv_y = number;
213 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
214 }
215 break;
216 default:
217 if (((SPObjectClass *) (parent_class))->set) {
218 ((SPObjectClass *) (parent_class))->set(object, key, value);
219 }
220 break;
221 }
222 }
224 /**
225 * * Receives update notifications.
226 * */
227 static void
228 sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
229 {
230 SPGlyph *glyph = SP_GLYPH(object);
231 (void)glyph;
233 if (flags & SP_OBJECT_MODIFIED_FLAG) {
234 /* do something to trigger redisplay, updates? */
235 sp_object_read_attr(object, "unicode");
236 sp_object_read_attr(object, "glyph-name");
237 sp_object_read_attr(object, "d");
238 sp_object_read_attr(object, "orientation");
239 sp_object_read_attr(object, "arabic-form");
240 sp_object_read_attr(object, "lang");
241 sp_object_read_attr(object, "horiz-adv-x");
242 sp_object_read_attr(object, "vert-origin-x");
243 sp_object_read_attr(object, "vert-origin-y");
244 sp_object_read_attr(object, "vert-adv-y");
245 }
247 if (((SPObjectClass *) parent_class)->update) {
248 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
249 }
250 }
252 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
254 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
255 {
256 // SPGlyph *glyph = SP_GLYPH(object);
258 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
259 repr = xml_doc->createElement("svg:glyph");
260 }
262 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
263 repr->setAttribute("unicode", glyph->unicode);
264 repr->setAttribute("glyph-name", glyph->glyph_name);
265 repr->setAttribute("d", glyph->d);
266 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
267 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
268 repr->setAttribute("lang", glyph->lang);
269 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
270 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
271 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
272 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
273 */
274 if (repr != SP_OBJECT_REPR(object)) {
275 COPY_ATTR(repr, object->repr, "unicode");
276 COPY_ATTR(repr, object->repr, "glyph-name");
277 COPY_ATTR(repr, object->repr, "d");
278 COPY_ATTR(repr, object->repr, "orientation");
279 COPY_ATTR(repr, object->repr, "arabic-form");
280 COPY_ATTR(repr, object->repr, "lang");
281 COPY_ATTR(repr, object->repr, "horiz-adv-x");
282 COPY_ATTR(repr, object->repr, "vert-origin-x");
283 COPY_ATTR(repr, object->repr, "vert-origin-y");
284 COPY_ATTR(repr, object->repr, "vert-adv-y");
285 }
287 if (((SPObjectClass *) (parent_class))->write) {
288 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
289 }
291 return repr;
292 }
293 #endif //#ifdef ENABLE_SVG_FONTS
294 /*
295 Local Variables:
296 mode:c++
297 c-file-style:"stroustrup"
298 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
299 indent-tabs-mode:nil
300 fill-column:99
301 End:
302 */
303 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :