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