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 if (!value) return 0;
127 char *end;
128 double ret = g_ascii_strtod(value, &end);
129 if (*end) {
130 g_warning("Unable to convert \"%s\" to number", value);
131 // We could leave this out, too. If strtod can't convert
132 // anything, it will return zero.
133 ret = 0;
134 }
135 return ret;
136 }
138 static FeCompositeOperator
139 sp_feComposite_read_operator(gchar const *value) {
140 if (!value) return COMPOSITE_DEFAULT;
142 if (strcmp(value, "over") == 0) return COMPOSITE_OVER;
143 else if (strcmp(value, "in") == 0) return COMPOSITE_IN;
144 else if (strcmp(value, "out") == 0) return COMPOSITE_OUT;
145 else if (strcmp(value, "atop") == 0) return COMPOSITE_ATOP;
146 else if (strcmp(value, "xor") == 0) return COMPOSITE_XOR;
147 else if (strcmp(value, "arithmetic") == 0) return COMPOSITE_ARITHMETIC;
148 return COMPOSITE_DEFAULT;
149 }
151 /**
152 * Sets a specific value in the SPFeComposite.
153 */
154 static void
155 sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
156 {
157 SPFeComposite *feComposite = SP_FECOMPOSITE(object);
158 (void)feComposite;
160 int input;
161 FeCompositeOperator op;
162 double k_n;
163 switch(key) {
164 /*DEAL WITH SETTING ATTRIBUTES HERE*/
165 case SP_ATTR_OPERATOR:
166 op = sp_feComposite_read_operator(value);
167 if (op != feComposite->composite_operator) {
168 feComposite->composite_operator = op;
169 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
170 }
171 break;
173 case SP_ATTR_K1:
174 k_n = sp_feComposite_read_number(value);
175 if (k_n != feComposite->k1) {
176 feComposite->k1 = k_n;
177 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
178 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
179 }
180 break;
182 case SP_ATTR_K2:
183 k_n = sp_feComposite_read_number(value);
184 if (k_n != feComposite->k2) {
185 feComposite->k2 = k_n;
186 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
187 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
188 }
189 break;
191 case SP_ATTR_K3:
192 k_n = sp_feComposite_read_number(value);
193 if (k_n != feComposite->k3) {
194 feComposite->k3 = k_n;
195 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
196 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
197 }
198 break;
200 case SP_ATTR_K4:
201 k_n = sp_feComposite_read_number(value);
202 if (k_n != feComposite->k4) {
203 feComposite->k4 = k_n;
204 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
205 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
206 }
207 break;
209 case SP_ATTR_IN2:
210 input = sp_filter_primitive_read_in(feComposite, value);
211 if (input != feComposite->in2) {
212 feComposite->in2 = input;
213 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
214 }
215 break;
217 default:
218 if (((SPObjectClass *) feComposite_parent_class)->set)
219 ((SPObjectClass *) feComposite_parent_class)->set(object, key, value);
220 break;
221 }
223 }
225 /**
226 * Receives update notifications.
227 */
228 static void
229 sp_feComposite_update(SPObject *object, SPCtx *ctx, guint flags)
230 {
231 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
232 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
234 /* do something to trigger redisplay, updates? */
236 }
238 if (((SPObjectClass *) feComposite_parent_class)->update) {
239 ((SPObjectClass *) feComposite_parent_class)->update(object, ctx, flags);
240 }
241 }
243 /**
244 * Writes its settings to an incoming repr object, if any.
245 */
246 static Inkscape::XML::Node *
247 sp_feComposite_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
248 {
249 // Inkscape-only object, not copied during an "plain SVG" dump:
250 if (flags & SP_OBJECT_WRITE_EXT) {
251 if (repr) {
252 // is this sane?
253 //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
254 } else {
255 repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
256 }
257 }
259 if (((SPObjectClass *) feComposite_parent_class)->write) {
260 ((SPObjectClass *) feComposite_parent_class)->write(object, repr, flags);
261 }
263 return repr;
264 }
266 static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
267 g_assert(primitive != NULL);
268 g_assert(filter != NULL);
270 SPFeComposite *sp_composite = SP_FECOMPOSITE(primitive);
272 int primitive_n = filter->add_primitive(NR::NR_FILTER_COMPOSITE);
273 NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
274 NR::FilterComposite *nr_composite = dynamic_cast<NR::FilterComposite*>(nr_primitive);
275 g_assert(nr_composite != NULL);
277 sp_filter_primitive_renderer_common(primitive, nr_primitive);
279 nr_composite->set_operator(sp_composite->composite_operator);
280 nr_composite->set_input(1, sp_composite->in2);
281 if (sp_composite->composite_operator == COMPOSITE_ARITHMETIC) {
282 nr_composite->set_arithmetic(sp_composite->k1, sp_composite->k2,
283 sp_composite->k3, sp_composite->k4);
284 }
285 }
288 /*
289 Local Variables:
290 mode:c++
291 c-file-style:"stroustrup"
292 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
293 indent-tabs-mode:nil
294 fill-column:99
295 End:
296 */
297 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :