Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-string.cpp
1 /*
2  * SVG <text> and <tspan> implementation
3  *
4  * Author:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Jon A. Cruz <jon@joncruz.org>
7  *   Abhishek Sharma
8  *
9  * Copyright (C) 1999-2002 Lauris Kaplinski
10  * Copyright (C) 2000-2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 /*
16  * fixme:
17  *
18  * These subcomponents should not be items, or alternately
19  * we have to invent set of flags to mark, whether standard
20  * attributes are applicable to given item (I even like this
21  * idea somewhat - Lauris)
22  *
23  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
32 #include "sp-string.h"
33 #include "xml/repr.h"
36 /*#####################################################
37 #  SPSTRING
38 #####################################################*/
40 static void sp_string_class_init(SPStringClass *classname);
41 static void sp_string_init(SPString *string);
43 static void sp_string_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
44 static void sp_string_release(SPObject *object);
45 static void sp_string_read_content(SPObject *object);
46 static void sp_string_update(SPObject *object, SPCtx *ctx, unsigned flags);
48 static SPObjectClass *string_parent_class;
50 GType
51 sp_string_get_type()
52 {
53     static GType type = 0;
54     if (!type) {
55         GTypeInfo info = {
56             sizeof(SPStringClass),
57             NULL,    /* base_init */
58             NULL,    /* base_finalize */
59             (GClassInitFunc) sp_string_class_init,
60             NULL,    /* class_finalize */
61             NULL,    /* class_data */
62             sizeof(SPString),
63             16,    /* n_preallocs */
64             (GInstanceInitFunc) sp_string_init,
65             NULL,    /* value_table */
66         };
67         type = g_type_register_static(SP_TYPE_OBJECT, "SPString", &info, (GTypeFlags)0);
68     }
69     return type;
70 }
72 static void
73 sp_string_class_init(SPStringClass *classname)
74 {
75     SPObjectClass *sp_object_class;
77     sp_object_class = (SPObjectClass *) classname;
79     string_parent_class = (SPObjectClass*)g_type_class_ref(SP_TYPE_OBJECT);
81     sp_object_class->build        = sp_string_build;
82     sp_object_class->release      = sp_string_release;
83     sp_object_class->read_content = sp_string_read_content;
84     sp_object_class->update       = sp_string_update;
85 }
87 static void
88 sp_string_init(SPString *string)
89 {
90     new (&string->string) Glib::ustring();
91 }
93 static void
94 sp_string_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
95 {
96     sp_string_read_content(object);
98     if (((SPObjectClass *) string_parent_class)->build)
99         ((SPObjectClass *) string_parent_class)->build(object, doc, repr);
102 static void
103 sp_string_release(SPObject *object)
105     SPString *string = SP_STRING(object);
107     string->string.~ustring();
109     if (((SPObjectClass *) string_parent_class)->release)
110         ((SPObjectClass *) string_parent_class)->release(object);
113 static void
114 sp_string_read_content(SPObject *object)
116     SPString *string = SP_STRING(object);
118     string->string.clear();
120     //XML Tree being used directly here while it shouldn't be.
121     gchar const *xml_string = string->getRepr()->content();
122     // see algorithms described in svg 1.1 section 10.15
123     if (object->xml_space.value == SP_XML_SPACE_PRESERVE) {
124         for ( ; *xml_string ; xml_string = g_utf8_next_char(xml_string) ) {
125             gunichar c = g_utf8_get_char(xml_string);
126             if ((c == 0xa) || (c == 0xd) || (c == '\t')) {
127                 c = ' ';
128             }
129             string->string += c;
130         }
131     }
132     else {
133         bool whitespace = false;
134         for ( ; *xml_string ; xml_string = g_utf8_next_char(xml_string) ) {
135             gunichar c = g_utf8_get_char(xml_string);
136             if ((c == 0xa) || (c == 0xd)) {
137                 continue;
138             }
139             if ((c == ' ') || (c == '\t')) {
140                 whitespace = true;
141             } else {
142                 if (whitespace && (!string->string.empty() || (object->getPrev() != NULL))) {
143                     string->string += ' ';
144                 }
145                 string->string += c;
146                 whitespace = false;
147             }
148         }
149         if (whitespace && SP_OBJECT_REPR(object)->next() != NULL) {   // can't use SPObject::getNext() when the SPObject tree is still being built
150             string->string += ' ';
151         }
152     }
153     object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
156 static void
157 sp_string_update(SPObject *object, SPCtx *ctx, unsigned flags)
159     if (((SPObjectClass *) string_parent_class)->update)
160         ((SPObjectClass *) string_parent_class)->update(object, ctx, flags);
162     if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG)) {
163         /* Parent style or we ourselves changed, so recalculate */
164         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // won't be "just a transformation" anymore, we're going to recompute "x" and "y" attributes
165     }
169 /*
170   Local Variables:
171   mode:c++
172   c-file-style:"stroustrup"
173   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
174   indent-tabs-mode:nil
175   fill-column:99
176   End:
177 */
178 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :