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 SvgFont* svgfont = new SvgFont(SP_FONT(object));
108 sp_document_add_resource(document, "font", object);
109 }
112 static void sp_font_children_modified(SPFont *sp_font)
113 {
114 SPObject* node = sp_font->children;
115 for(;node;node=node->next){
116 if (SP_IS_GLYPH(node)){
117 g_warning("We have a <glyph> childnode:\n\td=%s\n\tvert-origin-x=%f\n\tvert-origin-y=%f\n\tvert-adv-y=%f", ((SPGlyph*)node)->d, ((SPGlyph*)node)->vert_origin_x, ((SPGlyph*)node)->vert_origin_y, ((SPGlyph*)node)->vert_adv_y );
119 }
120 if (SP_IS_MISSING_GLYPH(node)){
121 g_warning("We have a <missing-glyph> childnode:\n\td=%s\n\thoriz-origin-x=%f\n\thoriz-origin-y=%f\n\thoriz-adv-x=%f", ((SPMissingGlyph*)node)->d, ((SPMissingGlyph*)node)->vert_origin_x, ((SPMissingGlyph*)node)->vert_origin_y, ((SPMissingGlyph*)node)->vert_adv_y );
122 }
123 // if (SP_IS_FONT_FACE_SRC(node)){
124 // }
125 }
126 }
128 /**
129 * Callback for child_added event.
130 */
131 static void
132 sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
133 {
134 SPFont *f = SP_FONT(object);
136 if (((SPObjectClass *) parent_class)->child_added)
137 (* ((SPObjectClass *) parent_class)->child_added)(object, child, ref);
139 sp_font_children_modified(f);
140 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
141 }
144 /**
145 * Callback for remove_child event.
146 */
147 static void
148 sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child)
149 {
150 SPFont *f = SP_FONT(object);
152 if (((SPObjectClass *) parent_class)->remove_child)
153 (* ((SPObjectClass *) parent_class)->remove_child)(object, child);
155 sp_font_children_modified(f);
156 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
157 }
159 static void sp_font_release(SPObject *object)
160 {
161 //SPFont *font = SP_FONT(object);
163 if (((SPObjectClass *) parent_class)->release) {
164 ((SPObjectClass *) parent_class)->release(object);
165 }
166 }
168 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value)
169 {
170 SPFont *font = SP_FONT(object);
171 double number;
173 switch (key) {
174 case SP_ATTR_HORIZ_ORIGIN_X:
175 number = helperfns_read_number(value);
176 if (number != font->horiz_origin_x){
177 font->horiz_origin_x = number;
178 g_warning("<font>: SP_ATTR_HORIZ_ORIGIN_X: %f", number);
179 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
180 }
181 break;
182 case SP_ATTR_HORIZ_ORIGIN_Y:
183 number = helperfns_read_number(value);
184 if (number != font->horiz_origin_y){
185 font->horiz_origin_y = number;
186 g_warning("<font>: SP_ATTR_HORIZ_ORIGIN_Y: %f", number);
187 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
188 }
189 break;
190 case SP_ATTR_HORIZ_ADV_X:
191 number = helperfns_read_number(value);
192 if (number != font->horiz_adv_x){
193 font->horiz_adv_x = number;
194 g_warning("<font>: 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 != font->vert_origin_x){
201 font->vert_origin_x = number;
202 g_warning("<font>: 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 != font->vert_origin_y){
209 font->vert_origin_y = number;
210 g_warning("<font>: 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 != font->vert_adv_y){
217 font->vert_adv_y = number;
218 g_warning("<font>: 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_font_update(SPObject *object, SPCtx *ctx, guint flags)
235 {
236 if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
237 sp_object_read_attr(object, "horiz-origin-x");
238 sp_object_read_attr(object, "horiz-origin-y");
239 sp_object_read_attr(object, "horiz-adv-x");
240 sp_object_read_attr(object, "vert-origin-x");
241 sp_object_read_attr(object, "vert-origin-y");
242 sp_object_read_attr(object, "vert-adv-y");
243 }
245 if (((SPObjectClass *) parent_class)->update) {
246 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
247 }
248 }
250 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
252 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
253 {
254 SPFont *font = SP_FONT(object);
256 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
257 repr = xml_doc->createElement("svg:font");
258 }
260 sp_repr_set_svg_double(repr, "horiz-origin-x", font->horiz_origin_x);
261 sp_repr_set_svg_double(repr, "horiz-origin-y", font->horiz_origin_y);
262 sp_repr_set_svg_double(repr, "horiz-adv-x", font->horiz_adv_x);
263 sp_repr_set_svg_double(repr, "vert-origin-x", font->vert_origin_x);
264 sp_repr_set_svg_double(repr, "vert-origin-y", font->vert_origin_y);
265 sp_repr_set_svg_double(repr, "vert-adv-y", font->vert_adv_y);
267 if (repr != SP_OBJECT_REPR(object)) {
268 COPY_ATTR(repr, object->repr, "horiz-origin-x");
269 COPY_ATTR(repr, object->repr, "horiz-origin-y");
270 COPY_ATTR(repr, object->repr, "horiz-adv-x");
271 COPY_ATTR(repr, object->repr, "vert-origin-x");
272 COPY_ATTR(repr, object->repr, "vert-origin-y");
273 COPY_ATTR(repr, object->repr, "vert-adv-y");
274 }
276 if (((SPObjectClass *) (parent_class))->write) {
277 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
278 }
280 return repr;
281 }
282 #endif //#ifdef ENABLE_SVG_FONTS
283 /*
284 Local Variables:
285 mode:c++
286 c-file-style:"stroustrup"
287 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
288 indent-tabs-mode:nil
289 fill-column:99
290 End:
291 */
292 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :