Code

Make curvature work again by fixing a minor omission
[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 <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);
110 static void sp_font_children_modified(SPFont */*sp_font*/)
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)
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);
130 /**
131  * Callback for remove_child event.
132  */
133 static void
134 sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child)
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);
145 static void sp_font_release(SPObject *object)
147     //SPFont *font = SP_FONT(object);
148     sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "font", object);
150     if (((SPObjectClass *) parent_class)->release) {
151         ((SPObjectClass *) parent_class)->release(object);
152     }
155 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value)
157     SPFont *font = SP_FONT(object);
158     double number;
160     switch (key) {
161         case SP_ATTR_HORIZ_ORIGIN_X:
162             number = helperfns_read_number(value);
163             if (number != font->horiz_origin_x){
164                 font->horiz_origin_x = number;
165                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
166             }
167             break;
168         case SP_ATTR_HORIZ_ORIGIN_Y:
169             number = helperfns_read_number(value);
170             if (number != font->horiz_origin_y){
171                 font->horiz_origin_y = number;
172                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
173             }
174             break;
175         case SP_ATTR_HORIZ_ADV_X:
176             number = helperfns_read_number(value);
177             if (number != font->horiz_adv_x){
178                 font->horiz_adv_x = number;
179                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
180             }
181             break;
182         case SP_ATTR_VERT_ORIGIN_X:
183             number = helperfns_read_number(value);
184             if (number != font->vert_origin_x){
185                 font->vert_origin_x = number;
186                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
187             }
188             break;
189         case SP_ATTR_VERT_ORIGIN_Y:
190             number = helperfns_read_number(value);
191             if (number != font->vert_origin_y){
192                 font->vert_origin_y = number;
193                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
194             }
195             break;
196         case SP_ATTR_VERT_ADV_Y:
197             number = helperfns_read_number(value);
198             if (number != font->vert_adv_y){
199                 font->vert_adv_y = number;
200                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
201             }
202             break;
203         default:
204             if (((SPObjectClass *) (parent_class))->set) {
205                 ((SPObjectClass *) (parent_class))->set(object, key, value);
206             }
207             break;
208     }
211 /**
212  * Receives update notifications.
213  */
214 static void
215 sp_font_update(SPObject *object, SPCtx *ctx, guint flags)
217     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
218         sp_object_read_attr(object, "horiz-origin-x");
219         sp_object_read_attr(object, "horiz-origin-y");
220         sp_object_read_attr(object, "horiz-adv-x");
221         sp_object_read_attr(object, "vert-origin-x");
222         sp_object_read_attr(object, "vert-origin-y");
223         sp_object_read_attr(object, "vert-adv-y");
224     }
226     if (((SPObjectClass *) parent_class)->update) {
227         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
228     }
231 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
233 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
235     SPFont *font = SP_FONT(object);
237     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
238         repr = xml_doc->createElement("svg:font");
239     }
241     sp_repr_set_svg_double(repr, "horiz-origin-x", font->horiz_origin_x);
242     sp_repr_set_svg_double(repr, "horiz-origin-y", font->horiz_origin_y);
243     sp_repr_set_svg_double(repr, "horiz-adv-x", font->horiz_adv_x);
244     sp_repr_set_svg_double(repr, "vert-origin-x", font->vert_origin_x);
245     sp_repr_set_svg_double(repr, "vert-origin-y", font->vert_origin_y);
246     sp_repr_set_svg_double(repr, "vert-adv-y", font->vert_adv_y);
248     if (repr != SP_OBJECT_REPR(object)) {
249         COPY_ATTR(repr, object->repr, "horiz-origin-x");
250         COPY_ATTR(repr, object->repr, "horiz-origin-y");
251         COPY_ATTR(repr, object->repr, "horiz-adv-x");
252         COPY_ATTR(repr, object->repr, "vert-origin-x");
253         COPY_ATTR(repr, object->repr, "vert-origin-y");
254         COPY_ATTR(repr, object->repr, "vert-adv-y");
255     }
257     if (((SPObjectClass *) (parent_class))->write) {
258         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
259     }
261     return repr;
263 #endif //#ifdef ENABLE_SVG_FONTS
264 /*
265   Local Variables:
266   mode:c++
267   c-file-style:"stroustrup"
268   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
269   indent-tabs-mode:nil
270   fill-column:99
271   End:
272 */
273 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :