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"
19 static void sp_glyph_class_init(SPGlyphClass *gc);
20 static void sp_glyph_init(SPGlyph *glyph);
22 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
23 static void sp_glyph_release(SPObject *object);
24 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value);
25 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
27 static SPObjectClass *parent_class;
29 GType sp_glyph_get_type(void)
30 {
31 static GType type = 0;
33 if (!type) {
34 GTypeInfo info = {
35 sizeof(SPGlyphClass),
36 NULL, /* base_init */
37 NULL, /* base_finalize */
38 (GClassInitFunc) sp_glyph_class_init,
39 NULL, /* class_finalize */
40 NULL, /* class_data */
41 sizeof(SPGlyph),
42 16, /* n_preallocs */
43 (GInstanceInitFunc) sp_glyph_init,
44 NULL, /* value_table */
45 };
46 type = g_type_register_static(SP_TYPE_OBJECT, "SPGlyph", &info, (GTypeFlags) 0);
47 }
49 return type;
50 }
52 static void sp_glyph_class_init(SPGlyphClass *gc)
53 {
54 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
56 parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
58 sp_object_class->build = sp_glyph_build;
59 sp_object_class->release = sp_glyph_release;
60 sp_object_class->set = sp_glyph_set;
61 sp_object_class->write = sp_glyph_write;
62 }
64 static void sp_glyph_init(SPGlyph *glyph)
65 {
66 //TODO: correct these values:
67 glyph->unicode = NULL;
68 glyph->glyph_name = NULL;
69 glyph->d = NULL;
70 glyph->orientation = NULL;
71 glyph->arabic_form = NULL;
72 glyph->lang = NULL;
73 glyph->horiz_adv_x = 0;
74 glyph->vert_origin_x = 0;
75 glyph->vert_origin_y = 0;
76 glyph->vert_adv_y = 0;
77 }
79 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
80 {
81 if (((SPObjectClass *) (parent_class))->build) {
82 ((SPObjectClass *) (parent_class))->build(object, document, repr);
83 }
85 sp_object_read_attr(object, "unicode");
86 sp_object_read_attr(object, "glyph-name");
87 sp_object_read_attr(object, "d");
88 sp_object_read_attr(object, "orientation");
89 sp_object_read_attr(object, "arabic-form");
90 sp_object_read_attr(object, "lang");
91 sp_object_read_attr(object, "horiz-adv-x");
92 sp_object_read_attr(object, "vert-origin-x");
93 sp_object_read_attr(object, "vert-origin-y");
94 sp_object_read_attr(object, "vert-adv-y");
95 }
97 static void sp_glyph_release(SPObject *object)
98 {
99 //SPGlyph *glyph = SP_GLYPH(object);
101 if (((SPObjectClass *) parent_class)->release) {
102 ((SPObjectClass *) parent_class)->release(object);
103 }
104 }
106 //ToDo: use helper-fns.h
107 inline double helperfns_read_number(gchar const *value) {
108 if (!value) return 0;
109 char *end;
110 double ret = g_ascii_strtod(value, &end);
111 if (*end) {
112 g_warning("Unable to convert \"%s\" to number", value);
113 // We could leave this out, too. If strtod can't convert
114 // anything, it will return zero.
115 ret = 0;
116 }
117 return ret;
118 }
120 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
121 {
122 SPGlyph *glyph = SP_GLYPH(object);
123 double number;
125 switch (key) {
126 case SP_ATTR_UNICODE:
127 if (glyph->unicode) g_free(glyph->unicode);
128 glyph->unicode = g_strdup(value);
129 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
130 g_warning("SP_ATTR_UNICODE: %s", value);
131 break;
132 case SP_ATTR_GLYPH_NAME:
133 if (glyph->glyph_name) g_free(glyph->glyph_name);
134 glyph->glyph_name = g_strdup(value);
135 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
136 g_warning("SP_ATTR_GLYPH_NAME: %s", value);
137 break;
138 case SP_ATTR_D:
139 if (glyph->d) g_free(glyph->d);
140 glyph->d = g_strdup(value);
141 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
142 g_warning("SP_ATTR_D: %s", value);
143 break;
144 case SP_ATTR_ORIENTATION:
145 if (glyph->orientation) g_free(glyph->orientation);
146 glyph->orientation = g_strdup(value);
147 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
148 g_warning("SP_ATTR_ORIENTATION: %s", value);
149 break;
150 case SP_ATTR_ARABIC_FORM:
151 if (glyph->arabic_form) g_free(glyph->arabic_form);
152 glyph->arabic_form = g_strdup(value);
153 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
154 g_warning("SP_ATTR_ARABIC_FORM: %s", value);
155 break;
156 case SP_ATTR_LANG:
157 if (glyph->lang) g_free(glyph->lang);
158 glyph->lang = g_strdup(value);
159 g_warning("SP_ATTR_LANG: %s", value);
160 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
161 break;
162 case SP_ATTR_HORIZ_ADV_X:
163 number = helperfns_read_number(value);
164 if (number != glyph->horiz_adv_x){
165 glyph->horiz_adv_x = number;
166 g_warning("SP_ATTR_HORIZ_ADV_X: %f", number);
167 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
168 }
169 break;
170 case SP_ATTR_VERT_ORIGIN_X:
171 number = helperfns_read_number(value);
172 if (number != glyph->vert_origin_x){
173 glyph->vert_origin_x = number;
174 g_warning("SP_ATTR_VERT_ORIGIN_X: %f", number);
175 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
176 }
177 break;
178 case SP_ATTR_VERT_ORIGIN_Y:
179 number = helperfns_read_number(value);
180 if (number != glyph->vert_origin_y){
181 glyph->vert_origin_y = number;
182 g_warning("SP_ATTR_VERT_ORIGIN_Y: %f", number);
183 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
184 }
185 break;
186 case SP_ATTR_VERT_ADV_Y:
187 number = helperfns_read_number(value);
188 if (number != glyph->vert_adv_y){
189 glyph->vert_adv_y = number;
190 g_warning("SP_ATTR_VERT_ADV_Y: %f", number);
191 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
192 }
193 break;
194 default:
195 if (((SPObjectClass *) (parent_class))->set) {
196 ((SPObjectClass *) (parent_class))->set(object, key, value);
197 }
198 break;
199 }
200 }
202 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
204 static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
205 {
206 SPGlyph *glyph = SP_GLYPH(object);
208 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
209 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
210 repr = xml_doc->createElement("svg:glyph");
211 }
213 repr->setAttribute("unicode", glyph->unicode);
214 repr->setAttribute("glyph-name", glyph->glyph_name);
215 repr->setAttribute("d", glyph->d);
216 repr->setAttribute("orientation", glyph->orientation);
217 repr->setAttribute("arabic-form", glyph->arabic_form);
218 repr->setAttribute("lang", glyph->lang);
219 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
220 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
221 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
222 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
224 if (repr != SP_OBJECT_REPR(object)) {
225 COPY_ATTR(repr, object->repr, "unicode");
226 COPY_ATTR(repr, object->repr, "glyph-name");
227 COPY_ATTR(repr, object->repr, "d");
228 COPY_ATTR(repr, object->repr, "orientation");
229 COPY_ATTR(repr, object->repr, "arabic-form");
230 COPY_ATTR(repr, object->repr, "lang");
231 COPY_ATTR(repr, object->repr, "horiz-adv-x");
232 COPY_ATTR(repr, object->repr, "vert-origin-x");
233 COPY_ATTR(repr, object->repr, "vert-origin-y");
234 COPY_ATTR(repr, object->repr, "vert-adv-y");
235 }
237 if (((SPObjectClass *) (parent_class))->write) {
238 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
239 }
241 return repr;
242 }
244 /*
245 Local Variables:
246 mode:c++
247 c-file-style:"stroustrup"
248 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
249 indent-tabs-mode:nil
250 fill-column:99
251 End:
252 */
253 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :