1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
5 #ifdef ENABLE_SVG_FONTS
6 #define __SP_ANCHOR_C__
8 /*
9 * SVG <hkern> and <vkern> elements implementation
10 * W3C SVG 1.1 spec, page 476, section 20.7
11 *
12 * Author:
13 * Felipe C. da S. Sanches <juca@members.fsf.org>
14 *
15 * Copyright (C) 2008, Felipe C. da S. Sanches
16 *
17 * Released under GNU GPL, read the file 'COPYING' for more information
18 */
20 #include "xml/repr.h"
21 #include "attributes.h"
22 #include "sp-glyph-kerning.h"
24 #include "document.h"
25 #include <string>
27 static void sp_glyph_kerning_class_init(SPGlyphKerningClass *gc);
28 static void sp_glyph_kerning_init(SPGlyphKerning *glyph);
30 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
31 static void sp_glyph_kerning_release(SPObject *object);
32 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value);
33 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
34 static void sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags);
36 static SPObjectClass *parent_class;
38 GType sp_glyph_kerning_h_get_type(void)
39 {
40 static GType type = 0;
42 if (!type) {
43 GTypeInfo info = {
44 sizeof(SPGlyphKerningClass),
45 NULL, /* base_init */
46 NULL, /* base_finalize */
47 (GClassInitFunc) sp_glyph_kerning_class_init,
48 NULL, /* class_finalize */
49 NULL, /* class_data */
50 sizeof(SPHkern),
51 16, /* n_preallocs */
52 (GInstanceInitFunc) sp_glyph_kerning_init,
53 NULL, /* value_table */
54 };
55 type = g_type_register_static(SP_TYPE_OBJECT, "SPHkern", &info, (GTypeFlags) 0);
56 }
58 return type;
59 }
61 GType sp_glyph_kerning_v_get_type(void)
62 {
63 static GType type = 0;
65 if (!type) {
66 GTypeInfo info = {
67 sizeof(SPGlyphKerningClass),
68 NULL, /* base_init */
69 NULL, /* base_finalize */
70 (GClassInitFunc) sp_glyph_kerning_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
73 sizeof(SPVkern),
74 16, /* n_preallocs */
75 (GInstanceInitFunc) sp_glyph_kerning_init,
76 NULL, /* value_table */
77 };
78 type = g_type_register_static(SP_TYPE_OBJECT, "SPVkern", &info, (GTypeFlags) 0);
79 }
81 return type;
82 }
84 static void sp_glyph_kerning_class_init(SPGlyphKerningClass *gc)
85 {
86 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
88 parent_class = (SPObjectClass*)g_type_class_peek_parent(gc);
90 sp_object_class->build = sp_glyph_kerning_build;
91 sp_object_class->release = sp_glyph_kerning_release;
92 sp_object_class->set = sp_glyph_kerning_set;
93 sp_object_class->write = sp_glyph_kerning_write;
94 sp_object_class->update = sp_glyph_kerning_update;
95 }
97 static void sp_glyph_kerning_init(SPGlyphKerning *glyph)
98 {
99 //TODO: correct these values:
100 glyph->u1 = NULL;
101 glyph->g1 = NULL;
102 glyph->u2 = NULL;
103 glyph->g2 = NULL;
104 glyph->k = 0;
105 }
107 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
108 {
109 if (((SPObjectClass *) (parent_class))->build) {
110 ((SPObjectClass *) (parent_class))->build(object, document, repr);
111 }
113 sp_object_read_attr(object, "u1");
114 sp_object_read_attr(object, "g1");
115 sp_object_read_attr(object, "u2");
116 sp_object_read_attr(object, "g2");
117 sp_object_read_attr(object, "k");
118 }
120 static void sp_glyph_kerning_release(SPObject *object)
121 {
122 //SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
124 if (((SPObjectClass *) parent_class)->release) {
125 ((SPObjectClass *) parent_class)->release(object);
126 }
127 }
129 GlyphNames::GlyphNames(const gchar* value){
130 if (value) this->names = strdup(value);
131 }
133 GlyphNames::~GlyphNames(){
134 if (this->names) g_free(this->names);
135 }
137 bool GlyphNames::contains(const char* name){
138 if (!(this->names) || !name) return false;
139 std::istringstream is(this->names);
140 std::string str;
141 std::string s(name);
142 while (is >> str){
143 if (str == s) return true;
144 }
145 return false;
146 }
148 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value)
149 {
150 SPGlyphKerning * glyphkern = (SPGlyphKerning*) object; //even if it is a VKern this will work. I did it this way just to avoind warnings.
152 switch (key) {
153 case SP_ATTR_U1:
154 {
155 if (glyphkern->u1) {
156 delete glyphkern->u1;
157 }
158 glyphkern->u1 = new UnicodeRange(value);
159 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
160 break;
161 }
162 case SP_ATTR_U2:
163 {
164 if (glyphkern->u2) {
165 delete glyphkern->u2;
166 }
167 glyphkern->u2 = new UnicodeRange(value);
168 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
169 break;
170 }
171 case SP_ATTR_G1:
172 {
173 if (glyphkern->g1) {
174 delete glyphkern->g1;
175 }
176 glyphkern->g1 = new GlyphNames(value);
177 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
178 break;
179 }
180 case SP_ATTR_G2:
181 {
182 if (glyphkern->g2) {
183 delete glyphkern->g2;
184 }
185 glyphkern->g2 = new GlyphNames(value);
186 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
187 break;
188 }
189 case SP_ATTR_K:
190 {
191 double number = value ? g_ascii_strtod(value, 0) : 0;
192 if (number != glyphkern->k){
193 glyphkern->k = number;
194 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
195 }
196 break;
197 }
198 default:
199 {
200 if (((SPObjectClass *) (parent_class))->set) {
201 ((SPObjectClass *) (parent_class))->set(object, key, value);
202 }
203 break;
204 }
205 }
206 }
208 /**
209 * * Receives update notifications.
210 * */
211 static void
212 sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags)
213 {
214 SPGlyphKerning *glyph = (SPGlyphKerning *)object;
215 (void)glyph;
217 if (flags & SP_OBJECT_MODIFIED_FLAG) {
218 /* do something to trigger redisplay, updates? */
219 sp_object_read_attr(object, "u1");
220 sp_object_read_attr(object, "u2");
221 sp_object_read_attr(object, "g2");
222 sp_object_read_attr(object, "k");
223 }
225 if (((SPObjectClass *) parent_class)->update) {
226 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
227 }
228 }
230 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
232 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
233 {
234 // SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
236 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
237 repr = xml_doc->createElement("svg:glyphkerning");//fix this!
238 }
240 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
241 repr->setAttribute("unicode", glyph->unicode);
242 repr->setAttribute("glyph-name", glyph->glyph_name);
243 repr->setAttribute("d", glyph->d);
244 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
245 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
246 repr->setAttribute("lang", glyph->lang);
247 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
248 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
249 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
250 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
251 */
252 if (repr != SP_OBJECT_REPR(object)) {
253 COPY_ATTR(repr, object->repr, "u1");
254 COPY_ATTR(repr, object->repr, "g1");
255 COPY_ATTR(repr, object->repr, "u2");
256 COPY_ATTR(repr, object->repr, "g2");
257 COPY_ATTR(repr, object->repr, "k");
258 }
260 if (((SPObjectClass *) (parent_class))->write) {
261 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
262 }
264 return repr;
265 }
266 #endif //#ifdef ENABLE_SVG_FONTS
267 /*
268 Local Variables:
269 mode:c++
270 c-file-style:"stroustrup"
271 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
272 indent-tabs-mode:nil
273 fill-column:99
274 End:
275 */
276 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :