Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-font.cpp
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 <juca@members.fsf.org>
12  *   Abhishek Sharma
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-font.h"
22 #include "sp-glyph.h"
23 #include "sp-missing-glyph.h"
24 #include "document.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 //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
84 // TODO determine better values and/or make these dynamic:
85 double FNT_DEFAULT_ADV = 90; // TODO determine proper default
86 double FNT_DEFAULT_ASCENT = 90; // TODO determine proper default
87 double FNT_UNITS_PER_EM = 90; // TODO determine proper default
89 static void sp_font_init(SPFont *font)
90 {
91     font->horiz_origin_x = 0;
92     font->horiz_origin_y = 0;
93     font->horiz_adv_x = FNT_DEFAULT_ADV;
94     font->vert_origin_x = FNT_DEFAULT_ADV / 2.0;
95     font->vert_origin_y = FNT_DEFAULT_ASCENT;
96     font->vert_adv_y = FNT_UNITS_PER_EM;
97 }
99 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
101     if (((SPObjectClass *) (parent_class))->build) {
102         ((SPObjectClass *) (parent_class))->build(object, document, repr);
103     }
105     object->readAttr( "horiz-origin-x" );
106     object->readAttr( "horiz-origin-y" );
107     object->readAttr( "horiz-adv-x" );
108     object->readAttr( "vert-origin-x" );
109     object->readAttr( "vert-origin-y" );
110     object->readAttr( "vert-adv-y" );
112     document->addResource("font", object);
116 static void sp_font_children_modified(SPFont */*sp_font*/)
120 /**
121  * Callback for child_added event.
122  */
123 static void
124 sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
126     SPFont *f = SP_FONT(object);
128     if (((SPObjectClass *) parent_class)->child_added)
129         (* ((SPObjectClass *) parent_class)->child_added)(object, child, ref);
131     sp_font_children_modified(f);
132     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
136 /**
137  * Callback for remove_child event.
138  */
139 static void
140 sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child)
142     SPFont *f = SP_FONT(object);
144     if (((SPObjectClass *) parent_class)->remove_child)
145         (* ((SPObjectClass *) parent_class)->remove_child)(object, child);
147     sp_font_children_modified(f);
148     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
151 static void sp_font_release(SPObject *object)
153     //SPFont *font = SP_FONT(object);
154     SP_OBJECT_DOCUMENT(object)->removeResource("font", object);
156     if (((SPObjectClass *) parent_class)->release) {
157         ((SPObjectClass *) parent_class)->release(object);
158     }
161 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value)
163     SPFont *font = SP_FONT(object);
165     // TODO these are floating point, so some epsilon comparison would be good
166     switch (key) {
167         case SP_ATTR_HORIZ_ORIGIN_X:
168         {
169             double number = value ? g_ascii_strtod(value, 0) : 0;
170             if (number != font->horiz_origin_x){
171                 font->horiz_origin_x = number;
172                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
173             }
174             break;
175         }
176         case SP_ATTR_HORIZ_ORIGIN_Y:
177         {
178             double number = value ? g_ascii_strtod(value, 0) : 0;
179             if (number != font->horiz_origin_y){
180                 font->horiz_origin_y = number;
181                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
182             }
183             break;
184         }
185         case SP_ATTR_HORIZ_ADV_X:
186         {
187             double number = value ? g_ascii_strtod(value, 0) : FNT_DEFAULT_ADV;
188             if (number != font->horiz_adv_x){
189                 font->horiz_adv_x = number;
190                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
191             }
192             break;
193         }
194         case SP_ATTR_VERT_ORIGIN_X:
195         {
196             double number = value ? g_ascii_strtod(value, 0) : FNT_DEFAULT_ADV / 2.0;
197             if (number != font->vert_origin_x){
198                 font->vert_origin_x = number;
199                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
200             }
201             break;
202         }
203         case SP_ATTR_VERT_ORIGIN_Y:
204         {
205             double number = value ? g_ascii_strtod(value, 0) : FNT_DEFAULT_ASCENT;
206             if (number != font->vert_origin_y){
207                 font->vert_origin_y = number;
208                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
209             }
210             break;
211         }
212         case SP_ATTR_VERT_ADV_Y:
213         {
214             double number = value ? g_ascii_strtod(value, 0) : FNT_UNITS_PER_EM;
215             if (number != font->vert_adv_y){
216                 font->vert_adv_y = number;
217                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
218             }
219             break;
220         }
221         default:
222             if (((SPObjectClass *) (parent_class))->set) {
223                 ((SPObjectClass *) (parent_class))->set(object, key, value);
224             }
225             break;
226     }
229 /**
230  * Receives update notifications.
231  */
232 static void
233 sp_font_update(SPObject *object, SPCtx *ctx, guint flags)
235     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
236         object->readAttr( "horiz-origin-x" );
237         object->readAttr( "horiz-origin-y" );
238         object->readAttr( "horiz-adv-x" );
239         object->readAttr( "vert-origin-x" );
240         object->readAttr( "vert-origin-y" );
241         object->readAttr( "vert-adv-y" );
242     }
244     if (((SPObjectClass *) parent_class)->update) {
245         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
246     }
249 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
251 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
253     SPFont *font = SP_FONT(object);
255     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
256         repr = xml_doc->createElement("svg:font");
257     }
259     sp_repr_set_svg_double(repr, "horiz-origin-x", font->horiz_origin_x);
260     sp_repr_set_svg_double(repr, "horiz-origin-y", font->horiz_origin_y);
261     sp_repr_set_svg_double(repr, "horiz-adv-x", font->horiz_adv_x);
262     sp_repr_set_svg_double(repr, "vert-origin-x", font->vert_origin_x);
263     sp_repr_set_svg_double(repr, "vert-origin-y", font->vert_origin_y);
264     sp_repr_set_svg_double(repr, "vert-adv-y", font->vert_adv_y);
266     if (repr != SP_OBJECT_REPR(object)) {
267         // All the below COPY_ATTR funtions are directly using 
268         //  the XML Tree while they shouldn't
269         COPY_ATTR(repr, object->getRepr(), "horiz-origin-x");
270         COPY_ATTR(repr, object->getRepr(), "horiz-origin-y");
271         COPY_ATTR(repr, object->getRepr(), "horiz-adv-x");
272         COPY_ATTR(repr, object->getRepr(), "vert-origin-x");
273         COPY_ATTR(repr, object->getRepr(), "vert-origin-y");
274         COPY_ATTR(repr, object->getRepr(), "vert-adv-y");
275     }
277     if (((SPObjectClass *) (parent_class))->write) {
278         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
279     }
281     return repr;
283 #endif //#ifdef ENABLE_SVG_FONTS
284 /*
285   Local Variables:
286   mode:c++
287   c-file-style:"stroustrup"
288   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
289   indent-tabs-mode:nil
290   fill-column:99
291   End:
292 */
293 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :