Code

Prevent localized doubles from being written into filter matrices
[inkscape.git] / src / sp-glyph-kerning.cpp
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  *   Abhishek Sharma
15  *
16  * Copyright (C) 2008, Felipe C. da S. Sanches
17  *
18  * Released under GNU GPL, read the file 'COPYING' for more information
19  */
21 #include "xml/repr.h"
22 #include "attributes.h"
23 #include "sp-glyph-kerning.h"
25 #include "document.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;
108 static void sp_glyph_kerning_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
110     if (((SPObjectClass *) (parent_class))->build) {
111         ((SPObjectClass *) (parent_class))->build(object, document, repr);
112     }
114     object->readAttr( "u1" );
115     object->readAttr( "g1" );
116     object->readAttr( "u2" );
117     object->readAttr( "g2" );
118     object->readAttr( "k" );
121 static void sp_glyph_kerning_release(SPObject *object)
123     //SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
125     if (((SPObjectClass *) parent_class)->release) {
126         ((SPObjectClass *) parent_class)->release(object);
127     }
130 GlyphNames::GlyphNames(const gchar* value){
131         if (value) this->names = strdup(value);
134 GlyphNames::~GlyphNames(){
135     if (this->names) g_free(this->names);
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;
149 static void sp_glyph_kerning_set(SPObject *object, unsigned int key, const gchar *value)
151     SPGlyphKerning * glyphkern = (SPGlyphKerning*) object; //even if it is a VKern this will work. I did it this way just to avoind warnings.
153     switch (key) {
154         case SP_ATTR_U1:
155         {
156             if (glyphkern->u1) {
157                 delete glyphkern->u1;
158             }
159             glyphkern->u1 = new UnicodeRange(value);
160             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
161             break;
162         }
163         case SP_ATTR_U2:
164         {
165             if (glyphkern->u2) {
166                 delete glyphkern->u2;
167             }
168             glyphkern->u2 = new UnicodeRange(value);
169             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
170             break;
171         }
172         case SP_ATTR_G1:
173         {
174             if (glyphkern->g1) {
175                 delete glyphkern->g1;
176             }
177             glyphkern->g1 = new GlyphNames(value);
178             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
179             break;
180         }
181         case SP_ATTR_G2:
182         {
183             if (glyphkern->g2) {
184                 delete glyphkern->g2;
185             }
186             glyphkern->g2 = new GlyphNames(value);
187             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
188              break;
189         }
190         case SP_ATTR_K:
191         {
192             double number = value ? g_ascii_strtod(value, 0) : 0;
193             if (number != glyphkern->k){
194                 glyphkern->k = number;
195                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
196             }
197             break;
198         }
199         default:
200         {
201             if (((SPObjectClass *) (parent_class))->set) {
202                 ((SPObjectClass *) (parent_class))->set(object, key, value);
203             }
204             break;
205         }
206     }
209 /**
210  *  * Receives update notifications.
211  *   */
212 static void
213 sp_glyph_kerning_update(SPObject *object, SPCtx *ctx, guint flags)
215     SPGlyphKerning *glyph = (SPGlyphKerning *)object;
216     (void)glyph;
218     if (flags & SP_OBJECT_MODIFIED_FLAG) {
219         /* do something to trigger redisplay, updates? */
220             object->readAttr( "u1" );
221             object->readAttr( "u2" );
222             object->readAttr( "g2" );
223             object->readAttr( "k" );
224     }
226     if (((SPObjectClass *) parent_class)->update) {
227         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
228     }
231 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
233 static Inkscape::XML::Node *sp_glyph_kerning_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
235 //    SPGlyphKerning *glyph = SP_GLYPH_KERNING(object);
237     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
238         repr = xml_doc->createElement("svg:glyphkerning");//fix this!
239     }
241 /* I am commenting out this part because I am not certain how does it work. I will have to study it later. Juca
242     repr->setAttribute("unicode", glyph->unicode);
243     repr->setAttribute("glyph-name", glyph->glyph_name);
244     repr->setAttribute("d", glyph->d);
245     sp_repr_set_svg_double(repr, "orientation", (double) glyph->orientation);
246     sp_repr_set_svg_double(repr, "arabic-form", (double) glyph->arabic_form);
247     repr->setAttribute("lang", glyph->lang);
248     sp_repr_set_svg_double(repr, "horiz-adv-x", glyph->horiz_adv_x);
249     sp_repr_set_svg_double(repr, "vert-origin-x", glyph->vert_origin_x);
250     sp_repr_set_svg_double(repr, "vert-origin-y", glyph->vert_origin_y);
251     sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
252 */
253     if (repr != SP_OBJECT_REPR(object)) {
254         // All the COPY_ATTR functions below use
255         //   XML Tree directly, while they shouldn't.
256         COPY_ATTR(repr, object->getRepr(), "u1");
257         COPY_ATTR(repr, object->getRepr(), "g1");
258         COPY_ATTR(repr, object->getRepr(), "u2");
259         COPY_ATTR(repr, object->getRepr(), "g2");
260         COPY_ATTR(repr, object->getRepr(), "k");
261     }
263     if (((SPObjectClass *) (parent_class))->write) {
264         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
265     }
267     return repr;
269 #endif //#ifdef ENABLE_SVG_FONTS
270 /*
271   Local Variables:
272   mode:c++
273   c-file-style:"stroustrup"
274   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
275   indent-tabs-mode:nil
276   fill-column:99
277   End:
278 */
279 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :