1 #define __SP_STRING_C__
3 /*
4 * SVG <text> and <tspan> implementation
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
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);
100 }
102 static void
103 sp_string_release(SPObject *object)
104 {
105 SPString *string = SP_STRING(object);
107 string->string.~ustring();
109 if (((SPObjectClass *) string_parent_class)->release)
110 ((SPObjectClass *) string_parent_class)->release(object);
111 }
113 static void
114 sp_string_read_content(SPObject *object)
115 {
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') c = ' ';
127 string->string += c;
128 }
129 }
130 else {
131 bool whitespace = false;
132 for ( ; *xml_string ; xml_string = g_utf8_next_char(xml_string) ) {
133 gunichar c = g_utf8_get_char(xml_string);
134 if (c == 0xa || c == 0xd) continue;
135 if (c == ' ' || c == '\t') whitespace = true;
136 else {
137 if (whitespace && (!string->string.empty() || SP_OBJECT_PREV(object) != NULL))
138 string->string += ' ';
139 string->string += c;
140 whitespace = false;
141 }
142 }
143 if (whitespace && SP_OBJECT_REPR(object)->next() != NULL) // can't use SP_OBJECT_NEXT() when the SPObject tree is still being built
144 string->string += ' ';
145 }
146 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
147 }
149 static void
150 sp_string_update(SPObject *object, SPCtx *ctx, unsigned flags)
151 {
152 if (((SPObjectClass *) string_parent_class)->update)
153 ((SPObjectClass *) string_parent_class)->update(object, ctx, flags);
155 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG)) {
156 /* Parent style or we ourselves changed, so recalculate */
157 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // won't be "just a transformation" anymore, we're going to recompute "x" and "y" attributes
158 }
159 }
162 /*
163 Local Variables:
164 mode:c++
165 c-file-style:"stroustrup"
166 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
167 indent-tabs-mode:nil
168 fill-column:99
169 End:
170 */
171 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :