Code

a78f92cc00733abbd40f9d0d787a36194275eee2
[inkscape.git] / src / sp-font.cpp
1 #define __SP_FONT_C__
3 /*
4  * SVG <font> element implementation
5  *
6  * Author:
7  *   Felipe C. da S. Sanches <felipe.sanches@gmail.com>
8  *
9  * Copyright (C) 2008, Felipe C. da S. Sanches
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "xml/repr.h"
15 #include "attributes.h"
16 #include "sp-font.h"
17 #include "sp-glyph.h"
18 #include "sp-missing-glyph.h"
19 #include "document.h"
20 #include "helper-fns.h"
22 static void sp_font_class_init(SPFontClass *fc);
23 static void sp_font_init(SPFont *font);
25 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
26 static void sp_font_release(SPObject *object);
27 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value);
28 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
30 static void sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
31 static void sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child);
32 static void sp_font_update(SPObject *object, SPCtx *ctx, guint flags);
34 // static gchar *sp_font_description(SPItem *item);
36 static SPObjectClass *parent_class;
38 GType sp_font_get_type(void)
39 {
40     static GType type = 0;
42     if (!type) {
43         GTypeInfo info = {
44             sizeof(SPFontClass),
45             NULL,       /* base_init */
46             NULL,       /* base_finalize */
47             (GClassInitFunc) sp_font_class_init,
48             NULL,       /* class_finalize */
49             NULL,       /* class_data */
50             sizeof(SPFont),
51             16, /* n_preallocs */
52             (GInstanceInitFunc) sp_font_init,
53             NULL,       /* value_table */
54         };
55         type = g_type_register_static(SP_TYPE_OBJECT, "SPFont", &info, (GTypeFlags) 0);
56     }
58     return type;
59 }
61 static void sp_font_class_init(SPFontClass *fc)
62 {
63     SPObjectClass *sp_object_class = (SPObjectClass *) fc;
65     parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
67     sp_object_class->build = sp_font_build;
68     sp_object_class->release = sp_font_release;
69     sp_object_class->set = sp_font_set;
70     sp_object_class->write = sp_font_write;
71     sp_object_class->child_added = sp_font_child_added;
72     sp_object_class->remove_child = sp_font_remove_child;
73     sp_object_class->update = sp_font_update;
74 }
76 static void sp_font_init(SPFont *font)
77 {
78     font->horiz_origin_x = 0;
79     font->horiz_origin_y = 0;
80     font->horiz_adv_x = 0;
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
82     font->vert_origin_x = 0;
83     font->vert_origin_y = 0;
84     font->vert_adv_y = 0;
85 }
87 static void sp_font_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
88 {
89     if (((SPObjectClass *) (parent_class))->build) {
90         ((SPObjectClass *) (parent_class))->build(object, document, repr);
91     }
93     sp_object_read_attr(object, "horiz-origin-x");
94     sp_object_read_attr(object, "horiz-origin-y");
95     sp_object_read_attr(object, "horiz-adv-x");
96     sp_object_read_attr(object, "vert-origin-x");
97     sp_object_read_attr(object, "vert-origin-y");
98     sp_object_read_attr(object, "vert-adv-y");
99 }
102 static void sp_font_children_modified(SPFont *sp_font)
104     SPObject* node = sp_font->children;
105     for(;node;node=node->next){
106         if (SP_IS_GLYPH(node)){
107             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 );
108             
109         }
110         if (SP_IS_MISSING_GLYPH(node)){
111 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 );
112         }
113 //        if (SP_IS_FONT_FACE_SRC(node)){
114 //        }
115     }
118 /**
119  * Callback for child_added event.
120  */
121 static void
122 sp_font_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
124     SPFont *f = SP_FONT(object);
126     if (((SPObjectClass *) parent_class)->child_added)
127         (* ((SPObjectClass *) parent_class)->child_added)(object, child, ref);
129     sp_font_children_modified(f);
130     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
134 /**
135  * Callback for remove_child event.
136  */
137 static void
138 sp_font_remove_child(SPObject *object, Inkscape::XML::Node *child)
140     SPFont *f = SP_FONT(object);
142     if (((SPObjectClass *) parent_class)->remove_child)
143         (* ((SPObjectClass *) parent_class)->remove_child)(object, child);
145     sp_font_children_modified(f);
146     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
149 static void sp_font_release(SPObject *object)
151     //SPFont *font = SP_FONT(object);
153     if (((SPObjectClass *) parent_class)->release) {
154         ((SPObjectClass *) parent_class)->release(object);
155     }
158 static void sp_font_set(SPObject *object, unsigned int key, const gchar *value)
160     SPFont *font = SP_FONT(object);
161     double number;
163     switch (key) {
164         case SP_ATTR_HORIZ_ORIGIN_X:
165             number = helperfns_read_number(value);
166             if (number != font->horiz_origin_x){
167                 font->horiz_origin_x = number;
168 g_warning("<font>: SP_ATTR_HORIZ_ORIGIN_X: %f", number);
169                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
170             }
171             break;
172         case SP_ATTR_HORIZ_ORIGIN_Y:
173             number = helperfns_read_number(value);
174             if (number != font->horiz_origin_y){
175                 font->horiz_origin_y = number;
176 g_warning("<font>: SP_ATTR_HORIZ_ORIGIN_Y: %f", number);
177                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
178             }
179             break;
180         case SP_ATTR_HORIZ_ADV_X:
181             number = helperfns_read_number(value);
182             if (number != font->horiz_adv_x){
183                 font->horiz_adv_x = number;
184 g_warning("<font>: SP_ATTR_HORIZ_ADV_X: %f", number);
185                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
186             }
187             break;
188         case SP_ATTR_VERT_ORIGIN_X:
189             number = helperfns_read_number(value);
190             if (number != font->vert_origin_x){
191                 font->vert_origin_x = number;
192 g_warning("<font>: SP_ATTR_VERT_ORIGIN_X: %f", number);
193                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
194             }
195             break;
196         case SP_ATTR_VERT_ORIGIN_Y:
197             number = helperfns_read_number(value);
198             if (number != font->vert_origin_y){
199                 font->vert_origin_y = number;
200 g_warning("<font>: SP_ATTR_VERT_ORIGIN_Y: %f", number);
201                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
202             }
203             break;
204         case SP_ATTR_VERT_ADV_Y:
205             number = helperfns_read_number(value);
206             if (number != font->vert_adv_y){
207                 font->vert_adv_y = number;
208 g_warning("<font>: SP_ATTR_VERT_ADV_Y: %f", number);
209                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
210             }
211             break;
212         default:
213             if (((SPObjectClass *) (parent_class))->set) {
214                 ((SPObjectClass *) (parent_class))->set(object, key, value);
215             }
216             break;
217     }
220 /**
221  * Receives update notifications.
222  */
223 static void
224 sp_font_update(SPObject *object, SPCtx *ctx, guint flags)
226     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
227         sp_object_read_attr(object, "horiz-origin-x");
228         sp_object_read_attr(object, "horiz-origin-y");
229         sp_object_read_attr(object, "horiz-adv-x");
230         sp_object_read_attr(object, "vert-origin-x");
231         sp_object_read_attr(object, "vert-origin-y");
232         sp_object_read_attr(object, "vert-adv-y");
233     }
235     if (((SPObjectClass *) parent_class)->update) {
236         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
237     }
240 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
242 static Inkscape::XML::Node *sp_font_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
244     SPFont *font = SP_FONT(object);
246     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
247         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
248         repr = xml_doc->createElement("svg:font");
249     }
251     sp_repr_set_svg_double(repr, "horiz-origin-x", font->horiz_origin_x);
252     sp_repr_set_svg_double(repr, "horiz-origin-y", font->horiz_origin_y);
253     sp_repr_set_svg_double(repr, "horiz-adv-x", font->horiz_adv_x);
254     sp_repr_set_svg_double(repr, "vert-origin-x", font->vert_origin_x);
255     sp_repr_set_svg_double(repr, "vert-origin-y", font->vert_origin_y);
256     sp_repr_set_svg_double(repr, "vert-adv-y", font->vert_adv_y);
258     if (repr != SP_OBJECT_REPR(object)) {
259         COPY_ATTR(repr, object->repr, "horiz-origin-x");
260         COPY_ATTR(repr, object->repr, "horiz-origin-y");
261         COPY_ATTR(repr, object->repr, "horiz-adv-x");
262         COPY_ATTR(repr, object->repr, "vert-origin-x");
263         COPY_ATTR(repr, object->repr, "vert-origin-y");
264         COPY_ATTR(repr, object->repr, "vert-adv-y");
265     }
267     if (((SPObjectClass *) (parent_class))->write) {
268         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
269     }
271     return repr;
274 /*
275   Local Variables:
276   mode:c++
277   c-file-style:"stroustrup"
278   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
279   indent-tabs-mode:nil
280   fill-column:99
281   End:
282 */
283 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :