Code

Applying patch #1415498 by James Kilfiger / zeimusu - Allow color & transparency...
[inkscape.git] / src / sp-string.cpp
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 void sp_string_calculate_dimensions(SPString *string);
50 static SPObjectClass *string_parent_class;
52 GType
53 sp_string_get_type()
54 {
55     static GType type = 0;
56     if (!type) {
57         GTypeInfo info = {
58             sizeof(SPStringClass),
59             NULL,    /* base_init */
60             NULL,    /* base_finalize */
61             (GClassInitFunc) sp_string_class_init,
62             NULL,    /* class_finalize */
63             NULL,    /* class_data */
64             sizeof(SPString),
65             16,    /* n_preallocs */
66             (GInstanceInitFunc) sp_string_init,
67             NULL,    /* value_table */
68         };
69         type = g_type_register_static(SP_TYPE_OBJECT, "SPString", &info, (GTypeFlags)0);
70     }
71     return type;
72 }
74 static void
75 sp_string_class_init(SPStringClass *classname)
76 {
77     SPObjectClass *sp_object_class;
78     SPItemClass   *item_class;
80     sp_object_class = (SPObjectClass *) classname;
81     item_class      = (SPItemClass *) classname;
83     string_parent_class = (SPObjectClass*)g_type_class_ref(SP_TYPE_OBJECT);
85     sp_object_class->build        = sp_string_build;
86     sp_object_class->release      = sp_string_release;
87     sp_object_class->read_content = sp_string_read_content;
88     sp_object_class->update       = sp_string_update;
89 }
91 static void
92 sp_string_init(SPString *string)
93 {
94     new (&string->string) Glib::ustring();
95 }
97 static void
98 sp_string_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
99 {
100     sp_string_read_content(object);
102     if (((SPObjectClass *) string_parent_class)->build)
103         ((SPObjectClass *) string_parent_class)->build(object, doc, repr);
106 static void
107 sp_string_release(SPObject *object)
109     SPString *string = SP_STRING(object);
111     string->string.~ustring();
113     if (((SPObjectClass *) string_parent_class)->release)
114         ((SPObjectClass *) string_parent_class)->release(object);
117 static void
118 sp_string_read_content(SPObject *object)
120     SPString *string = SP_STRING(object);
122     string->string.clear();
123     gchar const *xml_string = string->repr->content();
124     // see algorithms described in svg 1.1 section 10.15
125     if (object->xml_space.value == SP_XML_SPACE_PRESERVE) {
126         for ( ; *xml_string ; xml_string = g_utf8_next_char(xml_string) ) {
127             gunichar c = g_utf8_get_char(xml_string);
128             if (c == '\n' || c == '\t') c = ' ';
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 == '\n') continue;
137             if (c == ' ' || c == '\t') whitespace = true;
138             else {
139                 if (whitespace && (!string->string.empty() || SP_OBJECT_PREV(object) != NULL))
140                     string->string += ' ';
141                 string->string += c;
142                 whitespace = false;
143             }
144         }
145         if (whitespace && SP_OBJECT_REPR(object)->next() != NULL)   // can't use SP_OBJECT_NEXT() when the SPObject tree is still being built
146             string->string += ' ';
147     }
148     object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
151 static void
152 sp_string_update(SPObject *object, SPCtx *ctx, unsigned flags)
154     if (((SPObjectClass *) string_parent_class)->update)
155         ((SPObjectClass *) string_parent_class)->update(object, ctx, flags);
157     if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG)) {
158         /* Parent style or we ourselves changed, so recalculate */
159         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // won't be "just a transformation" anymore, we're going to recompute "x" and "y" attributes
160         sp_string_calculate_dimensions(SP_STRING(object));
161     }
164 static void
165 sp_string_calculate_dimensions(SPString *)
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:encoding=utf-8:textwidth=99 :