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