1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
5 #ifdef ENABLE_SVG_FONTS
7 /*
8 * SVG <font> element implementation
9 *
10 * Author:
11 * Felipe C. da S. Sanches <felipe.sanches@gmail.com>
12 *
13 * Copyright (C) 2008, Felipe C. da S. Sanches
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #include "xml/repr.h"
19 #include "attributes.h"
20 #include "sp-font.h"
21 #include "sp-glyph.h"
22 #include "sp-missing-glyph.h"
23 #include "document.h"
24 #include "helper-fns.h"
26 #include "display/nr-svgfonts.h"
28 static void sp_font_class_init(SPFontClass *fc);
29 static void sp_font_init(SPFont *font);
31 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static void sp_font_release(SPObject *object);
33 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value);
34 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
36 static void sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
37 static void sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child);
38 static void sp_font_update(SPObject *object, SPCtx *ctx, guint flags);
40 // static gchar *sp_font_description(SPItem *item);
42 static SPObjectClass *parent_class;
44 GType sp_font_get_type(void)
45 {
46 static GType type = 0;
48 if (!type) {
49 GTypeInfo info = {
50 sizeof(SPFontClass),
51 NULL, /* base_init */
52 NULL, /* base_finalize */
53 (GClassInitFunc) sp_font_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
56 sizeof(SPFont),
57 16, /* n_preallocs */
58 (GInstanceInitFunc) sp_font_init,
59 NULL, /* value_table */
60 };
61 type = g_type_register_static(SP_TYPE_OBJECT, "SPFont", &info, (GTypeFlags) 0);
62 }
64 return type;
65 }
67 static void sp_font_class_init(SPFontClass *fc)
68 {
69 SPObjectClass *sp_object_class = (SPObjectClass *) fc;
71 parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
73 sp_object_class->build = sp_font_build;
74 sp_object_class->release = sp_font_release;
75 sp_object_class->set = sp_font_set;
76 sp_object_class->write = sp_font_write;
77 sp_object_class->child_added = sp_font_child_added;
78 sp_object_class->remove_child = sp_font_remove_child;
79 sp_object_class->update = sp_font_update;
80 }
82 static void sp_font_init(SPFont *font)
83 {
84 font->horiz_origin_x = 0;
85 font->horiz_origin_y = 0;
86 font->horiz_adv_x = 0;
87 //I think we should have extra stuff here and in the set method in order to set default value as specified at http://www.w3.org/TR/SVG/fonts.html
88 font->vert_origin_x = 0;
89 font->vert_origin_y = 0;
90 font->vert_adv_y = 0;
91 }
93 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
94 {
95 if (((SPObjectClass *) (parent_class))->build) {
96 ((SPObjectClass *) (parent_class))->build(object, document, repr);
97 }
99 sp_object_read_attr(object, "horiz-origin-x");
100 sp_object_read_attr(object, "horiz-origin-y");
101 sp_object_read_attr(object, "horiz-adv-x");
102 sp_object_read_attr(object, "vert-origin-x");
103 sp_object_read_attr(object, "vert-origin-y");
104 sp_object_read_attr(object, "vert-adv-y");
106 sp_document_add_resource(document, "font", object);
107 }
110 static void sp_font_children_modified(SPFont *sp_font)
111 {
112 }
114 /**
115 * Callback for child_added event.
116 */
117 static void
118 sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
119 {
120 SPFont *f = SP_FONT(object);
122 if (((SPObjectClass *) parent_class)->child_added)
123 (* ((SPObjectClass *) parent_class)->child_added)(object, child, ref);
125 sp_font_children_modified(f);
126 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
127 }
130 /**
131 * Callback for remove_child event.
132 */
133 static void
134 sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child)
135 {
136 SPFont *f = SP_FONT(object);
138 if (((SPObjectClass *) parent_class)->remove_child)
139 (* ((SPObjectClass *) parent_class)->remove_child)(object, child);
141 sp_font_children_modified(f);
142 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
143 }
145 static void sp_font_release(SPObject *object)
146 {
147 //SPFont *font = SP_FONT(object);
149 if (((SPObjectClass *) parent_class)->release) {
150 ((SPObjectClass *) parent_class)->release(object);
151 }
152 }
154 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value)
155 {
156 SPFont *font = SP_FONT(object);
157 double number;
159 switch (key) {
160 case SP_ATTR_HORIZ_ORIGIN_X:
161 number = helperfns_read_number(value);
162 if (number != font->horiz_origin_x){
163 font->horiz_origin_x = number;
164 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
165 }
166 break;
167 case SP_ATTR_HORIZ_ORIGIN_Y:
168 number = helperfns_read_number(value);
169 if (number != font->horiz_origin_y){
170 font->horiz_origin_y = number;
171 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
172 }
173 break;
174 case SP_ATTR_HORIZ_ADV_X:
175 number = helperfns_read_number(value);
176 if (number != font->horiz_adv_x){
177 font->horiz_adv_x = number;
178 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
179 }
180 break;
181 case SP_ATTR_VERT_ORIGIN_X:
182 number = helperfns_read_number(value);
183 if (number != font->vert_origin_x){
184 font->vert_origin_x = number;
185 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
186 }
187 break;
188 case SP_ATTR_VERT_ORIGIN_Y:
189 number = helperfns_read_number(value);
190 if (number != font->vert_origin_y){
191 font->vert_origin_y = number;
192 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
193 }
194 break;
195 case SP_ATTR_VERT_ADV_Y:
196 number = helperfns_read_number(value);
197 if (number != font->vert_adv_y){
198 font->vert_adv_y = number;
199 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
200 }
201 break;
202 default:
203 if (((SPObjectClass *) (parent_class))->set) {
204 ((SPObjectClass *) (parent_class))->set(object, key, value);
205 }
206 break;
207 }
208 }
210 /**
211 * Receives update notifications.
212 */
213 static void
214 sp_font_update(SPObject *object, SPCtx *ctx, guint flags)
215 {
216 if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
217 sp_object_read_attr(object, "horiz-origin-x");
218 sp_object_read_attr(object, "horiz-origin-y");
219 sp_object_read_attr(object, "horiz-adv-x");
220 sp_object_read_attr(object, "vert-origin-x");
221 sp_object_read_attr(object, "vert-origin-y");
222 sp_object_read_attr(object, "vert-adv-y");
223 }
225 if (((SPObjectClass *) parent_class)->update) {
226 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
227 }
228 }
230 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
232 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
233 {
234 SPFont *font = SP_FONT(object);
236 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
237 repr = xml_doc->createElement("svg:font");
238 }
240 sp_repr_set_svg_double(repr, "horiz-origin-x", font->horiz_origin_x);
241 sp_repr_set_svg_double(repr, "horiz-origin-y", font->horiz_origin_y);
242 sp_repr_set_svg_double(repr, "horiz-adv-x", font->horiz_adv_x);
243 sp_repr_set_svg_double(repr, "vert-origin-x", font->vert_origin_x);
244 sp_repr_set_svg_double(repr, "vert-origin-y", font->vert_origin_y);
245 sp_repr_set_svg_double(repr, "vert-adv-y", font->vert_adv_y);
247 if (repr != SP_OBJECT_REPR(object)) {
248 COPY_ATTR(repr, object->repr, "horiz-origin-x");
249 COPY_ATTR(repr, object->repr, "horiz-origin-y");
250 COPY_ATTR(repr, object->repr, "horiz-adv-x");
251 COPY_ATTR(repr, object->repr, "vert-origin-x");
252 COPY_ATTR(repr, object->repr, "vert-origin-y");
253 COPY_ATTR(repr, object->repr, "vert-adv-y");
254 }
256 if (((SPObjectClass *) (parent_class))->write) {
257 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
258 }
260 return repr;
261 }
262 #endif //#ifdef ENABLE_SVG_FONTS
263 /*
264 Local Variables:
265 mode:c++
266 c-file-style:"stroustrup"
267 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
268 indent-tabs-mode:nil
269 fill-column:99
270 End:
271 */
272 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :