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(const char* 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;
154 switch (key) {
155 case SP_ATTR_U1:
156 if (glyphkern->u1) delete glyphkern->u1;
157 glyphkern->u1 = new UnicodeRange(value);
158 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
159 break;
160 case SP_ATTR_U2:
161 if (glyphkern->u2) delete glyphkern->u2;
162 glyphkern->u2 = new UnicodeRange(value);
163 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
164 break;
165 case SP_ATTR_G1:
166 if (glyphkern->g1) delete glyphkern->g1;
167 glyphkern->g1 = new GlyphNames(value);
168 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
169 break;
170 case SP_ATTR_G2:
171 if (glyphkern->g2) delete glyphkern->g2;
172 glyphkern->g2 = new GlyphNames(value);
173 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
174 break;
175 case SP_ATTR_K:
176 number = helperfns_read_number(value);
177 if (number != glyphkern->k){
178 glyphkern->k = number;
179 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
180 }
181 break;
182 default:
183 if (((SPObjectClass *) (parent_class))->set) {
184 ((SPObjectClass *) (parent_class))->set(object, key, value);
185 }
186 break;
187 }
188 }
190 /**
191 * * Receives update notifications.
192 * */
193 static void
194 sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags)
195 {
196 SPGlyphKerning *glyph = (SPGlyphKerning *)object;
197 (void)glyph;
199 if (flags & SP_OBJECT_MODIFIED_FLAG) {
200 /* do something to trigger redisplay, updates? */
201 sp_object_read_attr(object, "u1");
202 sp_object_read_attr(object, "u2");
203 sp_object_read_attr(object, "g2");
204 sp_object_read_attr(object, "k");
205 }
207 if (((SPObjectClass *) parent_class)->update) {
208 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
209 }
210 }
212 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
214 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
215 {
216 // SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
218 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
219 repr = xml_doc->createElement("svg:glyphkerning");//fix this!
220 }
222 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
223 repr->setAttribute("unicode", glyph->unicode);
224 repr->setAttribute("glyph-name", glyph->glyph_name);
225 repr->setAttribute("d", glyph->d);
226 sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
227 sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
228 repr->setAttribute("lang", glyph->lang);
229 sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
230 sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
231 sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
232 sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
233 */
234 if (repr != SP_OBJECT_REPR(object)) {
235 COPY_ATTR(repr, object->repr, "u1");
236 COPY_ATTR(repr, object->repr, "g1");
237 COPY_ATTR(repr, object->repr, "u2");
238 COPY_ATTR(repr, object->repr, "g2");
239 COPY_ATTR(repr, object->repr, "k");
240 }
242 if (((SPObjectClass *) (parent_class))->write) {
243 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
244 }
246 return repr;
247 }
248 #endif //#ifdef ENABLE_SVG_FONTS
249 /*
250 Local Variables:
251 mode:c++
252 c-file-style:"stroustrup"
253 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254 indent-tabs-mode:nil
255 fill-column:99
256 End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :