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