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();
119 gchar const *xml_string = string->repr->content();
120 // see algorithms described in svg 1.1 section 10.15
121 if (object->xml_space.value == SP_XML_SPACE_PRESERVE) {
122 for ( ; *xml_string ; xml_string = g_utf8_next_char(xml_string) ) {
123 gunichar c = g_utf8_get_char(xml_string);
124 if (c == 0xa || c == 0xd || c == '\t') c = ' ';
125 string->string += c;
126 }
127 }
128 else {
129 bool whitespace = false;
130 for ( ; *xml_string ; xml_string = g_utf8_next_char(xml_string) ) {
131 gunichar c = g_utf8_get_char(xml_string);
132 if (c == 0xa || c == 0xd) continue;
133 if (c == ' ' || c == '\t') whitespace = true;
134 else {
135 if (whitespace && (!string->string.empty() || SP_OBJECT_PREV(object) != NULL))
136 string->string += ' ';
137 string->string += c;
138 whitespace = false;
139 }
140 }
141 if (whitespace && SP_OBJECT_REPR(object)->next() != NULL) // can't use SP_OBJECT_NEXT() when the SPObject tree is still being built
142 string->string += ' ';
143 }
144 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
145 }
147 static void
148 sp_string_update(SPObject *object, SPCtx *ctx, unsigned flags)
149 {
150 if (((SPObjectClass *) string_parent_class)->update)
151 ((SPObjectClass *) string_parent_class)->update(object, ctx, flags);
153 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG)) {
154 /* Parent style or we ourselves changed, so recalculate */
155 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // won't be "just a transformation" anymore, we're going to recompute "x" and "y" attributes
156 }
157 }
160 /*
161 Local Variables:
162 mode:c++
163 c-file-style:"stroustrup"
164 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165 indent-tabs-mode:nil
166 fill-column:99
167 End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :