1 /** \file
2 * SVG <feFlood> implementation.
3 *
4 */
5 /*
6 * Authors:
7 * hugo Rodrigues <haa.rodrigues@gmail.com>
8 * Abhishek Sharma
9 *
10 * Copyright (C) 2006 Hugo Rodrigues
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #include "strneq.h"
21 #include "attributes.h"
22 #include "svg/svg.h"
23 #include "flood.h"
24 #include "xml/repr.h"
25 #include "helper-fns.h"
27 /* FeFlood base class */
29 static void sp_feFlood_class_init(SPFeFloodClass *klass);
30 static void sp_feFlood_init(SPFeFlood *feFlood);
32 static void sp_feFlood_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
33 static void sp_feFlood_release(SPObject *object);
34 static void sp_feFlood_set(SPObject *object, unsigned int key, gchar const *value);
35 static void sp_feFlood_update(SPObject *object, SPCtx *ctx, guint flags);
36 static Inkscape::XML::Node *sp_feFlood_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
37 static void sp_feFlood_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
39 static SPFilterPrimitiveClass *feFlood_parent_class;
41 GType
42 sp_feFlood_get_type()
43 {
44 static GType feFlood_type = 0;
46 if (!feFlood_type) {
47 GTypeInfo feFlood_info = {
48 sizeof(SPFeFloodClass),
49 NULL, NULL,
50 (GClassInitFunc) sp_feFlood_class_init,
51 NULL, NULL,
52 sizeof(SPFeFlood),
53 16,
54 (GInstanceInitFunc) sp_feFlood_init,
55 NULL, /* value_table */
56 };
57 feFlood_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeFlood", &feFlood_info, (GTypeFlags)0);
58 }
59 return feFlood_type;
60 }
62 static void
63 sp_feFlood_class_init(SPFeFloodClass *klass)
64 {
65 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
66 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
68 feFlood_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
70 sp_object_class->build = sp_feFlood_build;
71 sp_object_class->release = sp_feFlood_release;
72 sp_object_class->write = sp_feFlood_write;
73 sp_object_class->set = sp_feFlood_set;
74 sp_object_class->update = sp_feFlood_update;
75 sp_primitive_class->build_renderer = sp_feFlood_build_renderer;
76 }
78 static void
79 sp_feFlood_init(SPFeFlood *feFlood)
80 {
81 feFlood->opacity = 1;
82 feFlood->icc = NULL;
83 }
85 /**
86 * Reads the Inkscape::XML::Node, and initializes SPFeFlood variables. For this to get called,
87 * our name must be associated with a repr via "sp_object_type_register". Best done through
88 * sp-object-repr.cpp's repr_name_entries array.
89 */
90 static void
91 sp_feFlood_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
92 {
93 if (((SPObjectClass *) feFlood_parent_class)->build) {
94 ((SPObjectClass *) feFlood_parent_class)->build(object, document, repr);
95 }
97 /*LOAD ATTRIBUTES FROM REPR HERE*/
98 object->readAttr( "flood-opacity" );
99 object->readAttr( "flood-color" );
100 }
102 /**
103 * Drops any allocated memory.
104 */
105 static void
106 sp_feFlood_release(SPObject *object)
107 {
108 if (((SPObjectClass *) feFlood_parent_class)->release)
109 ((SPObjectClass *) feFlood_parent_class)->release(object);
110 }
112 /**
113 * Sets a specific value in the SPFeFlood.
114 */
115 static void
116 sp_feFlood_set(SPObject *object, unsigned int key, gchar const *value)
117 {
118 SPFeFlood *feFlood = SP_FEFLOOD(object);
119 (void)feFlood;
120 gchar const *cend_ptr = NULL;
121 gchar *end_ptr = NULL;
122 guint32 read_color;
123 double read_num;
124 bool dirty = false;
126 switch(key) {
127 /*DEAL WITH SETTING ATTRIBUTES HERE*/
128 case SP_PROP_FLOOD_COLOR:
129 cend_ptr = NULL;
130 read_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
132 if (cend_ptr && read_color != feFlood->color){
133 feFlood->color = read_color;
134 dirty=true;
135 }
137 if (cend_ptr){
138 while (g_ascii_isspace(*cend_ptr)) {
139 ++cend_ptr;
140 }
141 if (strneq(cend_ptr, "icc-color(", 10)) {
142 if (!feFlood->icc) feFlood->icc = new SVGICCColor();
143 if ( ! sp_svg_read_icc_color( cend_ptr, feFlood->icc ) ) {
144 delete feFlood->icc;
145 feFlood->icc = NULL;
146 }
147 dirty = true;
148 }
149 }
150 if (dirty)
151 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
152 break;
153 case SP_PROP_FLOOD_OPACITY:
154 if (value) {
155 read_num = g_ascii_strtod(value, &end_ptr);
156 if (*end_ptr) {
157 g_warning("Unable to convert \"%s\" to number", value);
158 read_num = 1;
159 }
160 }
161 else {
162 read_num = 1;
163 }
164 if (read_num != feFlood->opacity){
165 feFlood->opacity = read_num;
166 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
167 }
168 break;
169 default:
170 if (((SPObjectClass *) feFlood_parent_class)->set)
171 ((SPObjectClass *) feFlood_parent_class)->set(object, key, value);
172 break;
173 }
175 }
177 /**
178 * Receives update notifications.
179 */
180 static void
181 sp_feFlood_update(SPObject *object, SPCtx *ctx, guint flags)
182 {
183 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
184 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
186 /* do something to trigger redisplay, updates? */
188 }
190 if (((SPObjectClass *) feFlood_parent_class)->update) {
191 ((SPObjectClass *) feFlood_parent_class)->update(object, ctx, flags);
192 }
193 }
195 /**
196 * Writes its settings to an incoming repr object, if any.
197 */
198 static Inkscape::XML::Node *
199 sp_feFlood_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
200 {
201 /* TODO: Don't just clone, but create a new repr node and write all
202 * relevant values into it */
203 if (!repr) {
204 repr = SP_OBJECT_REPR(object)->duplicate(doc);
205 }
207 if (((SPObjectClass *) feFlood_parent_class)->write) {
208 ((SPObjectClass *) feFlood_parent_class)->write(object, doc, repr, flags);
209 }
211 return repr;
212 }
214 static void sp_feFlood_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
215 g_assert(primitive != NULL);
216 g_assert(filter != NULL);
218 SPFeFlood *sp_flood = SP_FEFLOOD(primitive);
219 (void)sp_flood;
221 int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_FLOOD);
222 Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
223 Inkscape::Filters::FilterFlood *nr_flood = dynamic_cast<Inkscape::Filters::FilterFlood*>(nr_primitive);
224 g_assert(nr_flood != NULL);
226 sp_filter_primitive_renderer_common(primitive, nr_primitive);
228 nr_flood->set_opacity(sp_flood->opacity);
229 nr_flood->set_color(sp_flood->color);
230 nr_flood->set_icc(sp_flood->icc);
231 }
234 /*
235 Local Variables:
236 mode:c++
237 c-file-style:"stroustrup"
238 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
239 indent-tabs-mode:nil
240 fill-column:99
241 End:
242 */
243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :