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 <felipe.sanches@gmail.com>
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 "helper-fns.h"
26 #include <string>
28 static void sp_glyph_kerning_class_init(SPGlyphKerningClass *gc);
29 static void sp_glyph_kerning_init(SPGlyphKerning *glyph);
31 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static void sp_glyph_kerning_release(SPObject *object);
33 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value);
34 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
35 static void sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags);
37 static SPObjectClass *parent_class;
39 GType sp_glyph_kerning_h_get_type(void)
40 {
41 static GType type = 0;
43 if (!type) {
44 GTypeInfo info = {
45 sizeof(SPGlyphKerningClass),
46 NULL, /* base_init */
47 NULL, /* base_finalize */
48 (GClassInitFunc) sp_glyph_kerning_class_init,
49 NULL, /* class_finalize */
50 NULL, /* class_data */
51 sizeof(SPHkern),
52 16, /* n_preallocs */
53 (GInstanceInitFunc) sp_glyph_kerning_init,
54 NULL, /* value_table */
55 };
56 type = g_type_register_static(SP_TYPE_OBJECT, "SPHkern", &info, (GTypeFlags) 0);
57 }
59 return type;
60 }
62 GType sp_glyph_kerning_v_get_type(void)
63 {
64 static GType type = 0;
66 if (!type) {
67 GTypeInfo info = {
68 sizeof(SPGlyphKerningClass),
69 NULL, /* base_init */
70 NULL, /* base_finalize */
71 (GClassInitFunc) sp_glyph_kerning_class_init,
72 NULL, /* class_finalize */
73 NULL, /* class_data */
74 sizeof(SPVkern),
75 16, /* n_preallocs */
76 (GInstanceInitFunc) sp_glyph_kerning_init,
77 NULL, /* value_table */
78 };
79 type = g_type_register_static(SP_TYPE_OBJECT, "SPVkern", &info, (GTypeFlags) 0);
80 }
82 return type;
83 }
85 static void sp_glyph_kerning_class_init(SPGlyphKerningClass *gc)
86 {
87 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
89 parent_class = (SPObjectClass*)g_type_class_peek_parent(gc);
91 sp_object_class->build = sp_glyph_kerning_build;
92 sp_object_class->release = sp_glyph_kerning_release;
93 sp_object_class->set = sp_glyph_kerning_set;
94 sp_object_class->write = sp_glyph_kerning_write;
95 sp_object_class->update = sp_glyph_kerning_update;
96 }
98 static void sp_glyph_kerning_init(SPGlyphKerning *glyph)
99 {
100 //TODO: correct these values:
101 glyph->u1 = NULL;
102 glyph->g1 = NULL;
103 glyph->u2 = NULL;
104 glyph->g2 = NULL;
105 glyph->k = 0;
106 }
108 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
109 {
110 if (((SPObjectClass *) (parent_class))->build) {
111 ((SPObjectClass *) (parent_class))->build(object, document, repr);
112 }
114 sp_object_read_attr(object, "u1");
115 sp_object_read_attr(object, "g1");
116 sp_object_read_attr(object, "u2");
117 sp_object_read_attr(object, "g2");
118 sp_object_read_attr(object, "k");
119 }
121 static void sp_glyph_kerning_release(SPObject *object)
122 {
123 //SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
125 if (((SPObjectClass *) parent_class)->release) {
126 ((SPObjectClass *) parent_class)->release(object);
127 }
128 }
130 GlyphNames::GlyphNames(const gchar* value){
131 if (value) this->names = strdup(value);
132 }
134 GlyphNames::~GlyphNames(){
135 if (this->names) g_free(this->names);
136 }
138 bool GlyphNames::contains(gchar* name){
139 if (!(this->names) || !name) return false;
140 std::istringstream is(this->names);
141 std::string str;
142 std::string s(name);
143 while (is >> str){
144 if (str == s) return true;
145 }
146 return false;
147 }
149 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value)
150 {
151 SPGlyphKerning * glyphkern = (SPGlyphKerning*) object; //even if it is a VKern this will work. I did it this way just to avoind warnings.
152 double number;
153 const char* tag = (SP_IS_HKERN(object) ? "hkern" : "vkern");
155 switch (key) {
156 case SP_ATTR_U1:
157 if (glyphkern->u1) delete glyphkern->u1;
158 glyphkern->u1 = new UnicodeRange(value);
159 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
160 g_warning("<%s>: SP_ATTR_U1: %s", tag, value);
161 break;
162 case SP_ATTR_U2:
163 if (glyphkern->u2) delete glyphkern->u2;
164 glyphkern->u2 = new UnicodeRange(value);
165 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
166 g_warning("<%s>: SP_ATTR_U2: %s", tag, value);
167 break;
168 case SP_ATTR_G1:
169 if (glyphkern->g1) delete glyphkern->g1;
170 glyphkern->g1 = new GlyphNames(value);
171 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
172 g_warning("<%s>: SP_ATTR_G1: %s", tag, value);
173 break;
174 case SP_ATTR_G2:
175 if (glyphkern->g2) delete glyphkern->g2;
176 glyphkern->g2 = new GlyphNames(value);
177 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
178 g_warning("<%s>: SP_ATTR_G2: %s", tag, value);
179 break;
180 case SP_ATTR_K:
181 number = helperfns_read_number(value);
182 if (number != glyphkern->k){
183 glyphkern->k = number;
184 g_warning("<%s>: SP_ATTR_K: %f", tag, number);
185 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
186 }
187 break;
188 default:
189 if (((SPObjectClass *) (parent_class))->set) {
190 ((SPObjectClass *) (parent_class))->set(object, key, value);
191 }
192 break;
193 }
194 ///should free tag?
195 }
197 /**
198 * * Receives update notifications.
199 * */
200 static void
201 sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags)
202 {
203 SPGlyphKerning *glyph = (SPGlyphKerning *)object;
204 (void)glyph;
206 if (flags & SP_OBJECT_MODIFIED_FLAG) {
207 /* do something to trigger redisplay, updates? */
208 sp_object_read_attr(object, "u1");
209 sp_object_read_attr(object, "g1");
210 sp_object_read_attr(object, "u2");
211 sp_object_read_attr(object, "g2");
212 sp_object_read_attr(object, "k");
213 }
215 if (((SPObjectClass *) parent_class)->update) {
216 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
217 }
218 }
220 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
222 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
223 {
224 // SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
226 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
227 repr = xml_doc->createElement("svg:glyphkerning");//fix this!
228 }
230 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
231 repr->setAttribute("unicode", glyph->unicode);
232 repr->setAttribute("glyph-name", glyph->glyph_name);
233 repr->setAttribute("d", glyph->d);
234 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
235 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
236 repr->setAttribute("lang", glyph->lang);
237 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
238 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
239 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
240 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
241 */
242 if (repr != SP_OBJECT_REPR(object)) {
243 COPY_ATTR(repr, object->repr, "u1");
244 COPY_ATTR(repr, object->repr, "g1");
245 COPY_ATTR(repr, object->repr, "u2");
246 COPY_ATTR(repr, object->repr, "g2");
247 COPY_ATTR(repr, object->repr, "k");
248 }
250 if (((SPObjectClass *) (parent_class))->write) {
251 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
252 }
254 return repr;
255 }
256 #endif //#ifdef ENABLE_SVG_FONTS
257 /*
258 Local Variables:
259 mode:c++
260 c-file-style:"stroustrup"
261 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
262 indent-tabs-mode:nil
263 fill-column:99
264 End:
265 */
266 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :