Code

6d7006f577a89e1df10445bcefb59399d136ac39
[inkscape.git] / src / sp-glyph-kerning.cpp
1 #define __SP_ANCHOR_C__
3 /*
4  * SVG <hkern> and <vkern> elements implementation
5  * W3C SVG 1.1 spec, page 476, section 20.7
6  *
7  * Author:
8  *   Felipe C. da S. Sanches <felipe.sanches@gmail.com>
9  *
10  * Copyright (C) 2008, Felipe C. da S. Sanches
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include "xml/repr.h"
16 #include "attributes.h"
17 #include "sp-glyph-kerning.h"
19 #include "document.h"
20 #include "helper-fns.h"
22 static void sp_glyph_kerning_class_init(SPGlyphKerningClass *gc);
23 static void sp_glyph_kerning_init(SPGlyphKerning *glyph);
25 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
26 static void sp_glyph_kerning_release(SPObject *object);
27 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value);
28 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
29 static void sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags);
31 static SPObjectClass *parent_class;
33 GType sp_glyph_kerning_h_get_type(void)
34 {
35     static GType type = 0;
37     if (!type) {
38         GTypeInfo info = {
39             sizeof(SPGlyphKerningClass),
40             NULL,       /* base_init */
41             NULL,       /* base_finalize */
42             (GClassInitFunc) sp_glyph_kerning_class_init,
43             NULL,       /* class_finalize */
44             NULL,       /* class_data */
45             sizeof(SPHkern),
46             16, /* n_preallocs */
47             (GInstanceInitFunc) sp_glyph_kerning_init,
48             NULL,       /* value_table */
49         };
50         type = g_type_register_static(SP_TYPE_OBJECT, "SPHkern", &info, (GTypeFlags) 0);
51     }
53     return type;
54 }
56 GType sp_glyph_kerning_v_get_type(void)
57 {
58     static GType type = 0;
60     if (!type) {
61         GTypeInfo info = {
62             sizeof(SPGlyphKerningClass),
63             NULL,       /* base_init */
64             NULL,       /* base_finalize */
65             (GClassInitFunc) sp_glyph_kerning_class_init,
66             NULL,       /* class_finalize */
67             NULL,       /* class_data */
68             sizeof(SPVkern),
69             16, /* n_preallocs */
70             (GInstanceInitFunc) sp_glyph_kerning_init,
71             NULL,       /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_OBJECT, "SPVkern", &info, (GTypeFlags) 0);
74     }
76     return type;
77 }
79 static void sp_glyph_kerning_class_init(SPGlyphKerningClass *gc)
80 {
81     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
83     parent_class = (SPObjectClass*)g_type_class_peek_parent(gc);
85     sp_object_class->build = sp_glyph_kerning_build;
86     sp_object_class->release = sp_glyph_kerning_release;
87     sp_object_class->set = sp_glyph_kerning_set;
88     sp_object_class->write = sp_glyph_kerning_write;
89     sp_object_class->update = sp_glyph_kerning_update;
90 }
92 static void sp_glyph_kerning_init(SPGlyphKerning *glyph)
93 {
94 //TODO: correct these values:
95     glyph->u1 = NULL;
96     glyph->g1 = NULL;
97     glyph->u2 = NULL;
98     glyph->g2 = NULL;
99     glyph->k = 0;
102 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
104     if (((SPObjectClass *) (parent_class))->build) {
105         ((SPObjectClass *) (parent_class))->build(object, document, repr);
106     }
108     sp_object_read_attr(object, "u1");
109     sp_object_read_attr(object, "g1");
110     sp_object_read_attr(object, "u2");
111     sp_object_read_attr(object, "g2");
112     sp_object_read_attr(object, "k");
115 static void sp_glyph_kerning_release(SPObject *object)
117     //SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
119     if (((SPObjectClass *) parent_class)->release) {
120         ((SPObjectClass *) parent_class)->release(object);
121     }
124 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value)
126     SPGlyphKerning * glyphkern = (SPGlyphKerning*) object; //even if it is a VKern this will work. I did it this way just to avoind warnings.
127     double number;
128     const char* tag = (SP_IS_HKERN(object) ? "hkern" : "vkern");
130     switch (key) {
131         case SP_ATTR_U1:
132             if (glyphkern->u1) g_free(glyphkern->u1);
133             glyphkern->u1 = g_strdup(value);//todo: 
134             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
135 g_warning("<%s>: SP_ATTR_U1: %s", tag, value);
136              break;
137         case SP_ATTR_U2:
138             if (glyphkern->u2) g_free(glyphkern->u2);
139             glyphkern->u2 = g_strdup(value);//todo: 
140             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
141 g_warning("<%s>: SP_ATTR_U2: %s", tag, value);
142              break;
143         case SP_ATTR_G1:
144             if (glyphkern->g1) g_free(glyphkern->g1);
145             glyphkern->g1 = g_strdup(value);//todo: 
146             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
147 g_warning("<%s>: SP_ATTR_G1: %s", tag, value);
148              break;
149         case SP_ATTR_G2:
150             if (glyphkern->g2) g_free(glyphkern->g2);
151             glyphkern->g2 = g_strdup(value);//todo: 
152             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
153 g_warning("<%s>: SP_ATTR_G2: %s", tag, value);
154              break;
155         case SP_ATTR_K:
156             number = helperfns_read_number(value);
157             if (number != glyphkern->k){
158                 glyphkern->k = number;
159 g_warning("<%s>: SP_ATTR_K: %f", tag, number);
160                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
161             }
162             break;
163         default:
164             if (((SPObjectClass *) (parent_class))->set) {
165                 ((SPObjectClass *) (parent_class))->set(object, key, value);
166             }
167             break;
168     }
169 ///should free tag?
172 /**
173  *  * Receives update notifications.
174  *   */
175 static void
176 sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags)
178     SPGlyphKerning *glyph = (SPGlyphKerning *)object;
179     (void)glyph;
181     if (flags & SP_OBJECT_MODIFIED_FLAG) {
182         /* do something to trigger redisplay, updates? */
183             sp_object_read_attr(object, "u1");
184             sp_object_read_attr(object, "g1");
185             sp_object_read_attr(object, "u2");
186             sp_object_read_attr(object, "g2");
187             sp_object_read_attr(object, "k");
188     }
190     if (((SPObjectClass *) parent_class)->update) {
191         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
192     }
195 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
197 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
199 //    SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
201     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
202         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
203         repr = xml_doc->createElement("svg:glyphkerning");//fix this!
204     }
206 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
207     repr->setAttribute("unicode", glyph->unicode);
208     repr->setAttribute("glyph-name", glyph->glyph_name);
209     repr->setAttribute("d", glyph->d);
210     sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
211     sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
212     repr->setAttribute("lang", glyph->lang);
213     sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
214     sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
215     sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
216     sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
217 */
218     if (repr != SP_OBJECT_REPR(object)) {
219         COPY_ATTR(repr, object->repr, "u1");
220         COPY_ATTR(repr, object->repr, "g1");
221         COPY_ATTR(repr, object->repr, "u2");
222         COPY_ATTR(repr, object->repr, "g2");
223         COPY_ATTR(repr, object->repr, "k");
224     }
226     if (((SPObjectClass *) (parent_class))->write) {
227         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
228     }
230     return repr;
233 /*
234   Local Variables:
235   mode:c++
236   c-file-style:"stroustrup"
237   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
238   indent-tabs-mode:nil
239   fill-column:99
240   End:
241 */
242 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :