Code

A simple layout document as to what, why and how is cppification.
[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  *
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"
25 #include "display/nr-svgfonts.h"
27 static void sp_font_class_init(SPFontClass *fc);
28 static void sp_font_init(SPFont *font);
30 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
31 static void sp_font_release(SPObject *object);
32 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value);
33 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
35 static void sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
36 static void sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child);
37 static void sp_font_update(SPObject *object, SPCtx *ctx, guint flags);
39 // static gchar *sp_font_description(SPItem *item);
41 static SPObjectClass *parent_class;
43 GType sp_font_get_type(void)
44 {
45     static GType type = 0;
47     if (!type) {
48         GTypeInfo info = {
49             sizeof(SPFontClass),
50             NULL,       /* base_init */
51             NULL,       /* base_finalize */
52             (GClassInitFunc) sp_font_class_init,
53             NULL,       /* class_finalize */
54             NULL,       /* class_data */
55             sizeof(SPFont),
56             16, /* n_preallocs */
57             (GInstanceInitFunc) sp_font_init,
58             NULL,       /* value_table */
59         };
60         type = g_type_register_static(SP_TYPE_OBJECT, "SPFont", &info, (GTypeFlags) 0);
61     }
63     return type;
64 }
66 static void sp_font_class_init(SPFontClass *fc)
67 {
68     SPObjectClass *sp_object_class = (SPObjectClass *) fc;
70     parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
72     sp_object_class->build = sp_font_build;
73     sp_object_class->release = sp_font_release;
74     sp_object_class->set = sp_font_set;
75     sp_object_class->write = sp_font_write;
76     sp_object_class->child_added = sp_font_child_added;
77     sp_object_class->remove_child = sp_font_remove_child;
78     sp_object_class->update = sp_font_update;
79 }
81 //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
83 // TODO determine better values and/or make these dynamic:
84 double FNT_DEFAULT_ADV = 90; // TODO determine proper default
85 double FNT_DEFAULT_ASCENT = 90; // TODO determine proper default
86 double FNT_UNITS_PER_EM = 90; // TODO determine proper default
88 static void sp_font_init(SPFont *font)
89 {
90     font->horiz_origin_x = 0;
91     font->horiz_origin_y = 0;
92     font->horiz_adv_x = FNT_DEFAULT_ADV;
93     font->vert_origin_x = FNT_DEFAULT_ADV / 2.0;
94     font->vert_origin_y = FNT_DEFAULT_ASCENT;
95     font->vert_adv_y = FNT_UNITS_PER_EM;
96 }
98 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
99 {
100     if (((SPObjectClass *) (parent_class))->build) {
101         ((SPObjectClass *) (parent_class))->build(object, document, repr);
102     }
104     object->readAttr( "horiz-origin-x");
105     object->readAttr( "horiz-origin-y");
106     object->readAttr( "horiz-adv-x");
107     object->readAttr( "vert-origin-x");
108     object->readAttr( "vert-origin-y");
109     object->readAttr( "vert-adv-y");
111     document->add_resource("font", object);
115 static void sp_font_children_modified(SPFont */*sp_font*/)
119 /**
120  * Callback for child_added event.
121  */
122 static void
123 sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
125     SPFont *f = SP_FONT(object);
127     if (((SPObjectClass *) parent_class)->child_added)
128         (* ((SPObjectClass *) parent_class)->child_added)(object, child, ref);
130     sp_font_children_modified(f);
131     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
135 /**
136  * Callback for remove_child event.
137  */
138 static void
139 sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child)
141     SPFont *f = SP_FONT(object);
143     if (((SPObjectClass *) parent_class)->remove_child)
144         (* ((SPObjectClass *) parent_class)->remove_child)(object, child);
146     sp_font_children_modified(f);
147     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
150 static void sp_font_release(SPObject *object)
152     //SPFont *font = SP_FONT(object);
153     SP_OBJECT_DOCUMENT(object)->remove_resource("font", object);
155     if (((SPObjectClass *) parent_class)->release) {
156         ((SPObjectClass *) parent_class)->release(object);
157     }
160 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value)
162     SPFont *font = SP_FONT(object);
164     // TODO these are floating point, so some epsilon comparison would be good
165     switch (key) {
166         case SP_ATTR_HORIZ_ORIGIN_X:
167         {
168             double number = value ? g_ascii_strtod(value, 0) : 0;
169             if (number != font->horiz_origin_x){
170                 font->horiz_origin_x = number;
171                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
172             }
173             break;
174         }
175         case SP_ATTR_HORIZ_ORIGIN_Y:
176         {
177             double number = value ? g_ascii_strtod(value, 0) : 0;
178             if (number != font->horiz_origin_y){
179                 font->horiz_origin_y = number;
180                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
181             }
182             break;
183         }
184         case SP_ATTR_HORIZ_ADV_X:
185         {
186             double number = value ? g_ascii_strtod(value, 0) : FNT_DEFAULT_ADV;
187             if (number != font->horiz_adv_x){
188                 font->horiz_adv_x = number;
189                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
190             }
191             break;
192         }
193         case SP_ATTR_VERT_ORIGIN_X:
194         {
195             double number = value ? g_ascii_strtod(value, 0) : FNT_DEFAULT_ADV / 2.0;
196             if (number != font->vert_origin_x){
197                 font->vert_origin_x = number;
198                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
199             }
200             break;
201         }
202         case SP_ATTR_VERT_ORIGIN_Y:
203         {
204             double number = value ? g_ascii_strtod(value, 0) : FNT_DEFAULT_ASCENT;
205             if (number != font->vert_origin_y){
206                 font->vert_origin_y = number;
207                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
208             }
209             break;
210         }
211         case SP_ATTR_VERT_ADV_Y:
212         {
213             double number = value ? g_ascii_strtod(value, 0) : FNT_UNITS_PER_EM;
214             if (number != font->vert_adv_y){
215                 font->vert_adv_y = number;
216                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
217             }
218             break;
219         }
220         default:
221             if (((SPObjectClass *) (parent_class))->set) {
222                 ((SPObjectClass *) (parent_class))->set(object, key, value);
223             }
224             break;
225     }
228 /**
229  * Receives update notifications.
230  */
231 static void
232 sp_font_update(SPObject *object, SPCtx *ctx, guint flags)
234     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
235         object->readAttr( "horiz-origin-x");
236         object->readAttr( "horiz-origin-y");
237         object->readAttr( "horiz-adv-x");
238         object->readAttr( "vert-origin-x");
239         object->readAttr( "vert-origin-y");
240         object->readAttr( "vert-adv-y");
241     }
243     if (((SPObjectClass *) parent_class)->update) {
244         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
245     }
248 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
250 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
252     SPFont *font = SP_FONT(object);
254     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
255         repr = xml_doc->createElement("svg:font");
256     }
258     sp_repr_set_svg_double(repr, "horiz-origin-x", font->horiz_origin_x);
259     sp_repr_set_svg_double(repr, "horiz-origin-y", font->horiz_origin_y);
260     sp_repr_set_svg_double(repr, "horiz-adv-x", font->horiz_adv_x);
261     sp_repr_set_svg_double(repr, "vert-origin-x", font->vert_origin_x);
262     sp_repr_set_svg_double(repr, "vert-origin-y", font->vert_origin_y);
263     sp_repr_set_svg_double(repr, "vert-adv-y", font->vert_adv_y);
265     if (repr != SP_OBJECT_REPR(object)) {
266                 /*All the below COPY_ATTR funtions are directly using 
267                   the XML Tree while they shouldn't*/
268         COPY_ATTR(repr, object->getRepr(), "horiz-origin-x");
269         COPY_ATTR(repr, object->getRepr(), "horiz-origin-y");
270         COPY_ATTR(repr, object->getRepr(), "horiz-adv-x");
271         COPY_ATTR(repr, object->getRepr(), "vert-origin-x");
272         COPY_ATTR(repr, object->getRepr(), "vert-origin-y");
273         COPY_ATTR(repr, object->getRepr(), "vert-adv-y");
274     }
276     if (((SPObjectClass *) (parent_class))->write) {
277         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
278     }
280     return repr;
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 :