Code

refactoring: gathering some commonly copy'n'pasted functions on a common
[inkscape.git] / src / sp-fecomposite.cpp
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 "helper-fns.h"
24 #include "xml/repr.h"
25 #include "display/nr-filter-composite.h"
27 /* FeComposite base class */
29 static void sp_feComposite_class_init(SPFeCompositeClass *klass);
30 static void sp_feComposite_init(SPFeComposite *feComposite);
32 static void sp_feComposite_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
33 static void sp_feComposite_release(SPObject *object);
34 static void sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value);
35 static void sp_feComposite_update(SPObject *object, SPCtx *ctx, guint flags);
36 static Inkscape::XML::Node *sp_feComposite_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
37 static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
39 static SPFilterPrimitiveClass *feComposite_parent_class;
41 GType
42 sp_feComposite_get_type()
43 {
44     static GType feComposite_type = 0;
46     if (!feComposite_type) {
47         GTypeInfo feComposite_info = {
48             sizeof(SPFeCompositeClass),
49             NULL, NULL,
50             (GClassInitFunc) sp_feComposite_class_init,
51             NULL, NULL,
52             sizeof(SPFeComposite),
53             16,
54             (GInstanceInitFunc) sp_feComposite_init,
55             NULL,    /* value_table */
56         };
57         feComposite_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeComposite", &feComposite_info, (GTypeFlags)0);
58     }
59     return feComposite_type;
60 }
62 static void
63 sp_feComposite_class_init(SPFeCompositeClass *klass)
64 {
65     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
66     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
68     feComposite_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
70     sp_object_class->build = sp_feComposite_build;
71     sp_object_class->release = sp_feComposite_release;
72     sp_object_class->write = sp_feComposite_write;
73     sp_object_class->set = sp_feComposite_set;
74     sp_object_class->update = sp_feComposite_update;
76     sp_primitive_class->build_renderer = sp_feComposite_build_renderer;
77 }
79 static void
80 sp_feComposite_init(SPFeComposite *feComposite)
81 {
82     feComposite->composite_operator = COMPOSITE_DEFAULT;
83     feComposite->k1 = 0;
84     feComposite->k2 = 0;
85     feComposite->k3 = 0;
86     feComposite->k4 = 0;
87     feComposite->in2 = NR::NR_FILTER_SLOT_NOT_SET;
88 }
90 /**
91  * Reads the Inkscape::XML::Node, and initializes SPFeComposite variables.  For this to get called,
92  * our name must be associated with a repr via "sp_object_type_register".  Best done through
93  * sp-object-repr.cpp's repr_name_entries array.
94  */
95 static void
96 sp_feComposite_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
97 {
98     if (((SPObjectClass *) feComposite_parent_class)->build) {
99         ((SPObjectClass *) feComposite_parent_class)->build(object, document, repr);
100     }
102     SPFeComposite *composite = SP_FECOMPOSITE(object);
104     sp_object_read_attr(object, "operator");
105     if (composite->composite_operator == COMPOSITE_ARITHMETIC) {
106         sp_object_read_attr(object, "k1");
107         sp_object_read_attr(object, "k2");
108         sp_object_read_attr(object, "k3");
109         sp_object_read_attr(object, "k4");
110     }
111     sp_object_read_attr(object, "in2");
114 /**
115  * Drops any allocated memory.
116  */
117 static void
118 sp_feComposite_release(SPObject *object)
120     if (((SPObjectClass *) feComposite_parent_class)->release)
121         ((SPObjectClass *) feComposite_parent_class)->release(object);
124 static FeCompositeOperator
125 sp_feComposite_read_operator(gchar const *value) {
126     if (!value) return COMPOSITE_DEFAULT;
128     if (strcmp(value, "over") == 0) return COMPOSITE_OVER;
129     else if (strcmp(value, "in") == 0) return COMPOSITE_IN;
130     else if (strcmp(value, "out") == 0) return COMPOSITE_OUT;
131     else if (strcmp(value, "atop") == 0) return COMPOSITE_ATOP;
132     else if (strcmp(value, "xor") == 0) return COMPOSITE_XOR;
133     else if (strcmp(value, "arithmetic") == 0) return COMPOSITE_ARITHMETIC;
134     return COMPOSITE_DEFAULT;
137 /**
138  * Sets a specific value in the SPFeComposite.
139  */
140 static void
141 sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
143     SPFeComposite *feComposite = SP_FECOMPOSITE(object);
144     (void)feComposite;
146     int input;
147     FeCompositeOperator op;
148     double k_n;
149     switch(key) {
150         /*DEAL WITH SETTING ATTRIBUTES HERE*/
151         case SP_ATTR_OPERATOR:
152             op = sp_feComposite_read_operator(value);
153             if (op != feComposite->composite_operator) {
154                 feComposite->composite_operator = op;
155                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
156             }
157             break;
159         case SP_ATTR_K1:
160             k_n = helperfns_read_number(value);
161             if (k_n != feComposite->k1) {
162                 feComposite->k1 = k_n;
163                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
164                     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
165             }
166             break;
168         case SP_ATTR_K2:
169             k_n = helperfns_read_number(value);
170             if (k_n != feComposite->k2) {
171                 feComposite->k2 = k_n;
172                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
173                     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
174             }
175             break;
177         case SP_ATTR_K3:
178             k_n = helperfns_read_number(value);
179             if (k_n != feComposite->k3) {
180                 feComposite->k3 = k_n;
181                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
182                     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
183             }
184             break;
186         case SP_ATTR_K4:
187             k_n = helperfns_read_number(value);
188             if (k_n != feComposite->k4) {
189                 feComposite->k4 = k_n;
190                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
191                     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
192             }
193             break;
195         case SP_ATTR_IN2:
196             input = sp_filter_primitive_read_in(feComposite, value);
197             if (input != feComposite->in2) {
198                 feComposite->in2 = input;
199                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
200             }
201             break;
203         default:
204             if (((SPObjectClass *) feComposite_parent_class)->set)
205                 ((SPObjectClass *) feComposite_parent_class)->set(object, key, value);
206             break;
207     }
211 /**
212  * Receives update notifications.
213  */
214 static void
215 sp_feComposite_update(SPObject *object, SPCtx *ctx, guint flags)
217     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
218                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
220         /* do something to trigger redisplay, updates? */
222     }
224     if (((SPObjectClass *) feComposite_parent_class)->update) {
225         ((SPObjectClass *) feComposite_parent_class)->update(object, ctx, flags);
226     }
229 /**
230  * Writes its settings to an incoming repr object, if any.
231  */
232 static Inkscape::XML::Node *
233 sp_feComposite_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
235     // Inkscape-only object, not copied during an "plain SVG" dump:
236     if (flags & SP_OBJECT_WRITE_EXT) {
237         if (repr) {
238             // is this sane?
239             //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
240         } else {
241             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
242         }
243     }
245     if (((SPObjectClass *) feComposite_parent_class)->write) {
246         ((SPObjectClass *) feComposite_parent_class)->write(object, repr, flags);
247     }
249     return repr;
252 static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
253     g_assert(primitive != NULL);
254     g_assert(filter != NULL);
256     SPFeComposite *sp_composite = SP_FECOMPOSITE(primitive);
258     int primitive_n = filter->add_primitive(NR::NR_FILTER_COMPOSITE);
259     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
260     NR::FilterComposite *nr_composite = dynamic_cast<NR::FilterComposite*>(nr_primitive);
261     g_assert(nr_composite != NULL);
263     sp_filter_primitive_renderer_common(primitive, nr_primitive);
265     nr_composite->set_operator(sp_composite->composite_operator);
266     nr_composite->set_input(1, sp_composite->in2);
267     if (sp_composite->composite_operator == COMPOSITE_ARITHMETIC) {
268         nr_composite->set_arithmetic(sp_composite->k1, sp_composite->k2,
269                                      sp_composite->k3, sp_composite->k4);
270     }
274 /*
275   Local Variables:
276   mode:c++
277   c-file-style:"stroustrup"
278   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
279   indent-tabs-mode:nil
280   fill-column:99
281   End:
282 */
283 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :