68312aa1b9ffd95a046ad120d1e34683f23a263e
1 #define __SP_FECOMPOSITE_CPP__
3 /** \file
4 * SVG <feComposite> implementation.
5 *
6 */
7 /*
8 * Authors:
9 * hugo Rodrigues <haa.rodrigues@gmail.com>
10 *
11 * Copyright (C) 2006 Hugo Rodrigues
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include "attributes.h"
21 #include "svg/svg.h"
22 #include "sp-fecomposite.h"
23 #include "xml/repr.h"
24 #include "display/nr-filter-composite.h"
26 /* FeComposite base class */
28 static void sp_feComposite_class_init(SPFeCompositeClass *klass);
29 static void sp_feComposite_init(SPFeComposite *feComposite);
31 static void sp_feComposite_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static void sp_feComposite_release(SPObject *object);
33 static void sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value);
34 static void sp_feComposite_update(SPObject *object, SPCtx *ctx, guint flags);
35 static Inkscape::XML::Node *sp_feComposite_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
36 static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
38 static SPFilterPrimitiveClass *feComposite_parent_class;
40 GType
41 sp_feComposite_get_type()
42 {
43 static GType feComposite_type = 0;
45 if (!feComposite_type) {
46 GTypeInfo feComposite_info = {
47 sizeof(SPFeCompositeClass),
48 NULL, NULL,
49 (GClassInitFunc) sp_feComposite_class_init,
50 NULL, NULL,
51 sizeof(SPFeComposite),
52 16,
53 (GInstanceInitFunc) sp_feComposite_init,
54 NULL, /* value_table */
55 };
56 feComposite_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeComposite", &feComposite_info, (GTypeFlags)0);
57 }
58 return feComposite_type;
59 }
61 static void
62 sp_feComposite_class_init(SPFeCompositeClass *klass)
63 {
64 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
65 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
67 feComposite_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
69 sp_object_class->build = sp_feComposite_build;
70 sp_object_class->release = sp_feComposite_release;
71 sp_object_class->write = sp_feComposite_write;
72 sp_object_class->set = sp_feComposite_set;
73 sp_object_class->update = sp_feComposite_update;
75 sp_primitive_class->build_renderer = sp_feComposite_build_renderer;
76 }
78 static void
79 sp_feComposite_init(SPFeComposite *feComposite)
80 {
81 feComposite->composite_operator = COMPOSITE_DEFAULT;
82 feComposite->k1 = 0;
83 feComposite->k2 = 0;
84 feComposite->k3 = 0;
85 feComposite->k4 = 0;
86 feComposite->in2 = NR::NR_FILTER_SLOT_NOT_SET;
87 }
89 /**
90 * Reads the Inkscape::XML::Node, and initializes SPFeComposite variables. For this to get called,
91 * our name must be associated with a repr via "sp_object_type_register". Best done through
92 * sp-object-repr.cpp's repr_name_entries array.
93 */
94 static void
95 sp_feComposite_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
96 {
97 if (((SPObjectClass *) feComposite_parent_class)->build) {
98 ((SPObjectClass *) feComposite_parent_class)->build(object, document, repr);
99 }
101 SPFeComposite *composite = SP_FECOMPOSITE(object);
103 sp_object_read_attr(object, "operator");
104 if (composite->composite_operator == COMPOSITE_ARITHMETIC) {
105 sp_object_read_attr(object, "k1");
106 sp_object_read_attr(object, "k2");
107 sp_object_read_attr(object, "k3");
108 sp_object_read_attr(object, "k4");
109 }
110 sp_object_read_attr(object, "in2");
111 }
113 /**
114 * Drops any allocated memory.
115 */
116 static void
117 sp_feComposite_release(SPObject *object)
118 {
119 if (((SPObjectClass *) feComposite_parent_class)->release)
120 ((SPObjectClass *) feComposite_parent_class)->release(object);
121 }
123 static double
124 sp_feComposite_read_number(gchar const *value) {
125 char *end;
126 double ret = g_ascii_strtod(value, &end);
127 if (*end) {
128 g_warning("Unable to convert \"%s\" to number", value);
129 // We could leave this out, too. If strtod can't convert
130 // anything, it will return zero.
131 ret = 0;
132 }
133 return ret;
134 }
136 static FeCompositeOperator
137 sp_feComposite_read_operator(gchar const *value) {
138 if (!value) return COMPOSITE_DEFAULT;
140 if (strcmp(value, "over") == 0) return COMPOSITE_OVER;
141 else if (strcmp(value, "in") == 0) return COMPOSITE_IN;
142 else if (strcmp(value, "out") == 0) return COMPOSITE_OUT;
143 else if (strcmp(value, "atop") == 0) return COMPOSITE_ATOP;
144 else if (strcmp(value, "xor") == 0) return COMPOSITE_XOR;
145 else if (strcmp(value, "arithmetic") == 0) return COMPOSITE_ARITHMETIC;
146 return COMPOSITE_DEFAULT;
147 }
149 /**
150 * Sets a specific value in the SPFeComposite.
151 */
152 static void
153 sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
154 {
155 SPFeComposite *feComposite = SP_FECOMPOSITE(object);
156 (void)feComposite;
158 int input;
159 FeCompositeOperator op;
160 double k_n;
161 switch(key) {
162 /*DEAL WITH SETTING ATTRIBUTES HERE*/
163 case SP_ATTR_OPERATOR:
164 op = sp_feComposite_read_operator(value);
165 if (op != feComposite->composite_operator) {
166 feComposite->composite_operator = op;
167 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
168 }
169 break;
171 case SP_ATTR_K1:
172 k_n = sp_feComposite_read_number(value);
173 if (k_n != feComposite->k1) {
174 feComposite->k1 = k_n;
175 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
176 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
177 }
178 break;
180 case SP_ATTR_K2:
181 k_n = sp_feComposite_read_number(value);
182 if (k_n != feComposite->k2) {
183 feComposite->k2 = k_n;
184 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
185 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
186 }
187 break;
189 case SP_ATTR_K3:
190 k_n = sp_feComposite_read_number(value);
191 if (k_n != feComposite->k3) {
192 feComposite->k3 = k_n;
193 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
194 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
195 }
196 break;
198 case SP_ATTR_K4:
199 k_n = sp_feComposite_read_number(value);
200 if (k_n != feComposite->k4) {
201 feComposite->k4 = k_n;
202 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
203 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
204 }
205 break;
207 case SP_ATTR_IN2:
208 input = sp_filter_primitive_read_in(feComposite, value);
209 if (input != feComposite->in2) {
210 feComposite->in2 = input;
211 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
212 }
213 break;
215 default:
216 if (((SPObjectClass *) feComposite_parent_class)->set)
217 ((SPObjectClass *) feComposite_parent_class)->set(object, key, value);
218 break;
219 }
221 }
223 /**
224 * Receives update notifications.
225 */
226 static void
227 sp_feComposite_update(SPObject *object, SPCtx *ctx, guint flags)
228 {
229 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
230 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
232 /* do something to trigger redisplay, updates? */
234 }
236 if (((SPObjectClass *) feComposite_parent_class)->update) {
237 ((SPObjectClass *) feComposite_parent_class)->update(object, ctx, flags);
238 }
239 }
241 /**
242 * Writes its settings to an incoming repr object, if any.
243 */
244 static Inkscape::XML::Node *
245 sp_feComposite_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
246 {
247 // Inkscape-only object, not copied during an "plain SVG" dump:
248 if (flags & SP_OBJECT_WRITE_EXT) {
249 if (repr) {
250 // is this sane?
251 //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
252 } else {
253 repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
254 }
255 }
257 if (((SPObjectClass *) feComposite_parent_class)->write) {
258 ((SPObjectClass *) feComposite_parent_class)->write(object, repr, flags);
259 }
261 return repr;
262 }
264 static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
265 g_assert(primitive != NULL);
266 g_assert(filter != NULL);
268 SPFeComposite *sp_composite = SP_FECOMPOSITE(primitive);
270 int primitive_n = filter->add_primitive(NR::NR_FILTER_COMPOSITE);
271 NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
272 NR::FilterComposite *nr_composite = dynamic_cast<NR::FilterComposite*>(nr_primitive);
273 g_assert(nr_composite != NULL);
275 sp_filter_primitive_renderer_common(primitive, nr_primitive);
277 nr_composite->set_operator(sp_composite->composite_operator);
278 nr_composite->set_input(1, sp_composite->in2);
279 if (sp_composite->composite_operator == COMPOSITE_ARITHMETIC) {
280 nr_composite->set_arithmetic(sp_composite->k1, sp_composite->k2,
281 sp_composite->k3, sp_composite->k4);
282 }
283 }
286 /*
287 Local Variables:
288 mode:c++
289 c-file-style:"stroustrup"
290 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
291 indent-tabs-mode:nil
292 fill-column:99
293 End:
294 */
295 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :