1 #include "config.h"
2 #ifdef ENABLE_SVG_FONTS
3 #define __SP_GLYPH_C__
5 /*
6 * SVG <glyph> element implementation
7 *
8 * Author:
9 * Felipe C. da S. Sanches <felipe.sanches@gmail.com>
10 *
11 * Copyright (C) 2008, Felipe C. da S. Sanches
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "xml/repr.h"
17 #include "attributes.h"
18 #include "sp-glyph.h"
19 #include "document.h"
20 #include "helper-fns.h"
22 static void sp_glyph_class_init(SPGlyphClass *gc);
23 static void sp_glyph_init(SPGlyph *glyph);
25 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
26 static void sp_glyph_release(SPObject *object);
27 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value);
28 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
29 static void sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags);
31 static SPObjectClass *parent_class;
33 GType sp_glyph_get_type(void)
34 {
35 static GType type = 0;
37 if (!type) {
38 GTypeInfo info = {
39 sizeof(SPGlyphClass),
40 NULL, /* base_init */
41 NULL, /* base_finalize */
42 (GClassInitFunc) sp_glyph_class_init,
43 NULL, /* class_finalize */
44 NULL, /* class_data */
45 sizeof(SPGlyph),
46 16, /* n_preallocs */
47 (GInstanceInitFunc) sp_glyph_init,
48 NULL, /* value_table */
49 };
50 type = g_type_register_static(SP_TYPE_OBJECT, "SPGlyph", &info, (GTypeFlags) 0);
51 }
53 return type;
54 }
56 static void sp_glyph_class_init(SPGlyphClass *gc)
57 {
58 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
60 parent_class = (SPObjectClass*)g_type_class_peek_parent(gc);
62 sp_object_class->build = sp_glyph_build;
63 sp_object_class->release = sp_glyph_release;
64 sp_object_class->set = sp_glyph_set;
65 sp_object_class->write = sp_glyph_write;
66 sp_object_class->update = sp_glyph_update;
67 }
69 static void sp_glyph_init(SPGlyph *glyph)
70 {
71 //TODO: correct these values:
72 glyph->unicode = NULL;
73 glyph->glyph_name = NULL;
74 glyph->d = NULL;
75 glyph->orientation = GLYPH_ORIENTATION_BOTH;
76 glyph->arabic_form = GLYPH_ARABIC_FORM_INITIAL;
77 glyph->lang = NULL;
78 glyph->horiz_adv_x = 0;
79 glyph->vert_origin_x = 0;
80 glyph->vert_origin_y = 0;
81 glyph->vert_adv_y = 0;
82 }
84 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
85 {
86 if (((SPObjectClass *) (parent_class))->build) {
87 ((SPObjectClass *) (parent_class))->build(object, document, repr);
88 }
90 sp_object_read_attr(object, "unicode");
91 sp_object_read_attr(object, "glyph-name");
92 sp_object_read_attr(object, "d");
93 sp_object_read_attr(object, "orientation");
94 sp_object_read_attr(object, "arabic-form");
95 sp_object_read_attr(object, "lang");
96 sp_object_read_attr(object, "horiz-adv-x");
97 sp_object_read_attr(object, "vert-origin-x");
98 sp_object_read_attr(object, "vert-origin-y");
99 sp_object_read_attr(object, "vert-adv-y");
100 }
102 static void sp_glyph_release(SPObject *object)
103 {
104 //SPGlyph *glyph = SP_GLYPH(object);
106 if (((SPObjectClass *) parent_class)->release) {
107 ((SPObjectClass *) parent_class)->release(object);
108 }
109 }
111 static glyphArabicForm sp_glyph_read_arabic_form(gchar const *value){
112 if (!value) return GLYPH_ARABIC_FORM_INITIAL; //TODO: verify which is the default default (for me, the spec is not clear)
113 switch(value[0]){
114 case 'i':
115 if (strncmp(value, "initial", 7) == 0) return GLYPH_ARABIC_FORM_INITIAL;
116 if (strncmp(value, "isolated", 8) == 0) return GLYPH_ARABIC_FORM_ISOLATED;
117 break;
118 case 'm':
119 if (strncmp(value, "medial", 6) == 0) return GLYPH_ARABIC_FORM_MEDIAL;
120 break;
121 case 't':
122 if (strncmp(value, "terminal", 8) == 0) return GLYPH_ARABIC_FORM_TERMINAL;
123 break;
124 }
125 return GLYPH_ARABIC_FORM_INITIAL; //TODO: VERIFY DEFAULT!
126 }
128 static glyphOrientation sp_glyph_read_orientation(gchar const *value){
129 if (!value) return GLYPH_ORIENTATION_BOTH;
130 switch(value[0]){
131 case 'h':
132 return GLYPH_ORIENTATION_HORIZONTAL;
133 break;
134 case 'v':
135 return GLYPH_ORIENTATION_VERTICAL;
136 break;
137 }
138 //ERROR? TODO: VERIFY PROPER ERROR HANDLING
139 return GLYPH_ORIENTATION_BOTH;
140 }
142 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
143 {
144 SPGlyph *glyph = SP_GLYPH(object);
145 double number;
146 glyphOrientation orient;
147 glyphArabicForm form;
149 switch (key) {
150 case SP_ATTR_UNICODE:
151 if (glyph->unicode) g_free(glyph->unicode);
152 glyph->unicode = g_strdup(value);
153 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
154 g_warning("<glyph>: SP_ATTR_UNICODE: %s", value);
155 break;
156 case SP_ATTR_GLYPH_NAME:
157 if (glyph->glyph_name) g_free(glyph->glyph_name);
158 glyph->glyph_name = g_strdup(value);
159 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
160 g_warning("<glyph>: SP_ATTR_GLYPH_NAME: %s", value);
161 break;
162 case SP_ATTR_D:
163 if (glyph->d) g_free(glyph->d);
164 glyph->d = g_strdup(value);
165 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
166 g_warning("<glyph>: SP_ATTR_D: %s", value);
167 break;
168 case SP_ATTR_ORIENTATION:
169 orient = sp_glyph_read_orientation(value);
170 if (glyph->orientation != orient){
171 glyph->orientation = orient;
172 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
173 }
174 g_warning("<glyph>: SP_ATTR_ORIENTATION: %d", orient);
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 g_warning("<glyph>: SP_ATTR_ARABIC_FORM: %d", glyph->arabic_form);
183 break;
184 case SP_ATTR_LANG:
185 if (glyph->lang) g_free(glyph->lang);
186 glyph->lang = g_strdup(value);
187 g_warning("<glyph>: SP_ATTR_LANG: %s", value);
188 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
189 break;
190 case SP_ATTR_HORIZ_ADV_X:
191 number = helperfns_read_number(value);
192 if (number != glyph->horiz_adv_x){
193 glyph->horiz_adv_x = number;
194 g_warning("<glyph>: SP_ATTR_HORIZ_ADV_X: %f", number);
195 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
196 }
197 break;
198 case SP_ATTR_VERT_ORIGIN_X:
199 number = helperfns_read_number(value);
200 if (number != glyph->vert_origin_x){
201 glyph->vert_origin_x = number;
202 g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_X: %f", number);
203 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
204 }
205 break;
206 case SP_ATTR_VERT_ORIGIN_Y:
207 number = helperfns_read_number(value);
208 if (number != glyph->vert_origin_y){
209 glyph->vert_origin_y = number;
210 g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_Y: %f", number);
211 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
212 }
213 break;
214 case SP_ATTR_VERT_ADV_Y:
215 number = helperfns_read_number(value);
216 if (number != glyph->vert_adv_y){
217 glyph->vert_adv_y = number;
218 g_warning("<glyph>: SP_ATTR_VERT_ADV_Y: %f", number);
219 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
220 }
221 break;
222 default:
223 if (((SPObjectClass *) (parent_class))->set) {
224 ((SPObjectClass *) (parent_class))->set(object, key, value);
225 }
226 break;
227 }
228 }
230 /**
231 * * Receives update notifications.
232 * */
233 static void
234 sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
235 {
236 SPGlyph *glyph = SP_GLYPH(object);
237 (void)glyph;
239 if (flags & SP_OBJECT_MODIFIED_FLAG) {
240 /* do something to trigger redisplay, updates? */
241 sp_object_read_attr(object, "unicode");
242 sp_object_read_attr(object, "glyph-name");
243 sp_object_read_attr(object, "d");
244 sp_object_read_attr(object, "orientation");
245 sp_object_read_attr(object, "arabic-form");
246 sp_object_read_attr(object, "lang");
247 sp_object_read_attr(object, "horiz-adv-x");
248 sp_object_read_attr(object, "vert-origin-x");
249 sp_object_read_attr(object, "vert-origin-y");
250 sp_object_read_attr(object, "vert-adv-y");
251 }
253 if (((SPObjectClass *) parent_class)->update) {
254 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
255 }
256 }
258 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
260 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
261 {
262 // SPGlyph *glyph = SP_GLYPH(object);
264 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
265 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
266 repr = xml_doc->createElement("svg:glyph");
267 }
269 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
270 repr->setAttribute("unicode", glyph->unicode);
271 repr->setAttribute("glyph-name", glyph->glyph_name);
272 repr->setAttribute("d", glyph->d);
273 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
274 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
275 repr->setAttribute("lang", glyph->lang);
276 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
277 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
278 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
279 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
280 */
281 if (repr != SP_OBJECT_REPR(object)) {
282 COPY_ATTR(repr, object->repr, "unicode");
283 COPY_ATTR(repr, object->repr, "glyph-name");
284 COPY_ATTR(repr, object->repr, "d");
285 COPY_ATTR(repr, object->repr, "orientation");
286 COPY_ATTR(repr, object->repr, "arabic-form");
287 COPY_ATTR(repr, object->repr, "lang");
288 COPY_ATTR(repr, object->repr, "horiz-adv-x");
289 COPY_ATTR(repr, object->repr, "vert-origin-x");
290 COPY_ATTR(repr, object->repr, "vert-origin-y");
291 COPY_ATTR(repr, object->repr, "vert-adv-y");
292 }
294 if (((SPObjectClass *) (parent_class))->write) {
295 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
296 }
298 return repr;
299 }
300 #endif //#ifdef ENABLE_SVG_FONTS
301 /*
302 Local Variables:
303 mode:c++
304 c-file-style:"stroustrup"
305 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
306 indent-tabs-mode:nil
307 fill-column:99
308 End:
309 */
310 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :