1
#define __SP_FECOMPOSITE_CPP__
4
* SVG <feComposite> implementation.
9
* hugo Rodrigues <haa.rodrigues@gmail.com>
11
* Copyright (C) 2006 Hugo Rodrigues
13
* Released under GNU GPL, read the file 'COPYING' for more information
20
#include "attributes.h"
22
#include "composite.h"
23
#include "helper-fns.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::Document *doc, Inkscape::XML::Node *repr, guint flags);
37
static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
39
static SPFilterPrimitiveClass *feComposite_parent_class;
42
sp_feComposite_get_type()
44
static GType feComposite_type = 0;
46
if (!feComposite_type) {
47
GTypeInfo feComposite_info = {
48
sizeof(SPFeCompositeClass),
50
(GClassInitFunc) sp_feComposite_class_init,
52
sizeof(SPFeComposite),
54
(GInstanceInitFunc) sp_feComposite_init,
55
NULL, /* value_table */
57
feComposite_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeComposite", &feComposite_info, (GTypeFlags)0);
59
return feComposite_type;
63
sp_feComposite_class_init(SPFeCompositeClass *klass)
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;
80
sp_feComposite_init(SPFeComposite *feComposite)
82
feComposite->composite_operator = COMPOSITE_DEFAULT;
87
feComposite->in2 = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
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.
96
sp_feComposite_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
98
if (((SPObjectClass *) feComposite_parent_class)->build) {
99
((SPObjectClass *) feComposite_parent_class)->build(object, document, repr);
102
SPFeComposite *comp = SP_FECOMPOSITE(object);
104
sp_object_read_attr(object, "operator");
105
if (comp->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");
111
sp_object_read_attr(object, "in2");
113
/* Unlike normal in, in2 is required attribute. Make sure, we can call
114
* it by some name. */
115
if (comp->in2 == Inkscape::Filters::NR_FILTER_SLOT_NOT_SET ||
116
comp->in2 == Inkscape::Filters::NR_FILTER_UNNAMED_SLOT)
118
SPFilter *parent = SP_FILTER(object->parent);
119
comp->in2 = sp_filter_primitive_name_previous_out(comp);
120
repr->setAttribute("in2", sp_filter_name_for_image(parent, comp->in2));
125
* Drops any allocated memory.
128
sp_feComposite_release(SPObject *object)
130
if (((SPObjectClass *) feComposite_parent_class)->release)
131
((SPObjectClass *) feComposite_parent_class)->release(object);
134
static FeCompositeOperator
135
sp_feComposite_read_operator(gchar const *value) {
136
if (!value) return COMPOSITE_DEFAULT;
138
if (strcmp(value, "over") == 0) return COMPOSITE_OVER;
139
else if (strcmp(value, "in") == 0) return COMPOSITE_IN;
140
else if (strcmp(value, "out") == 0) return COMPOSITE_OUT;
141
else if (strcmp(value, "atop") == 0) return COMPOSITE_ATOP;
142
else if (strcmp(value, "xor") == 0) return COMPOSITE_XOR;
143
else if (strcmp(value, "arithmetic") == 0) return COMPOSITE_ARITHMETIC;
144
return COMPOSITE_DEFAULT;
148
* Sets a specific value in the SPFeComposite.
151
sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
153
SPFeComposite *feComposite = SP_FECOMPOSITE(object);
157
FeCompositeOperator op;
160
/*DEAL WITH SETTING ATTRIBUTES HERE*/
161
case SP_ATTR_OPERATOR:
162
op = sp_feComposite_read_operator(value);
163
if (op != feComposite->composite_operator) {
164
feComposite->composite_operator = op;
165
object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
170
k_n = value ? helperfns_read_number(value) : 0;
171
if (k_n != feComposite->k1) {
172
feComposite->k1 = k_n;
173
if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
174
object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
179
k_n = value ? helperfns_read_number(value) : 0;
180
if (k_n != feComposite->k2) {
181
feComposite->k2 = k_n;
182
if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
183
object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
188
k_n = value ? helperfns_read_number(value) : 0;
189
if (k_n != feComposite->k3) {
190
feComposite->k3 = k_n;
191
if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
192
object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
197
k_n = value ? helperfns_read_number(value) : 0;
198
if (k_n != feComposite->k4) {
199
feComposite->k4 = k_n;
200
if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
201
object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
206
input = sp_filter_primitive_read_in(feComposite, value);
207
if (input != feComposite->in2) {
208
feComposite->in2 = input;
209
object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
214
if (((SPObjectClass *) feComposite_parent_class)->set)
215
((SPObjectClass *) feComposite_parent_class)->set(object, key, value);
222
* Receives update notifications.
225
sp_feComposite_update(SPObject *object, SPCtx *ctx, guint flags)
227
SPFeComposite *comp = SP_FECOMPOSITE(object);
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? */
236
/* Unlike normal in, in2 is required attribute. Make sure, we can call
237
* it by some name. */
238
if (comp->in2 == Inkscape::Filters::NR_FILTER_SLOT_NOT_SET ||
239
comp->in2 == Inkscape::Filters::NR_FILTER_UNNAMED_SLOT)
241
SPFilter *parent = SP_FILTER(object->parent);
242
comp->in2 = sp_filter_primitive_name_previous_out(comp);
243
object->repr->setAttribute("in2", sp_filter_name_for_image(parent, comp->in2));
246
if (((SPObjectClass *) feComposite_parent_class)->update) {
247
((SPObjectClass *) feComposite_parent_class)->update(object, ctx, flags);
252
* Writes its settings to an incoming repr object, if any.
254
static Inkscape::XML::Node *
255
sp_feComposite_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
257
SPFeComposite *comp = SP_FECOMPOSITE(object);
258
SPFilter *parent = SP_FILTER(object->parent);
261
repr = doc->createElement("svg:feComposite");
264
gchar const *out_name = sp_filter_name_for_image(parent, comp->in2);
266
repr->setAttribute("in2", out_name);
268
SPObject *i = parent->children;
269
while (i && i->next != object) i = i->next;
270
SPFilterPrimitive *i_prim = SP_FILTER_PRIMITIVE(i);
271
out_name = sp_filter_name_for_image(parent, i_prim->image_out);
272
repr->setAttribute("in2", out_name);
274
g_warning("Unable to set in2 for feComposite");
279
switch (comp->composite_operator) {
281
comp_op = "over"; break;
283
comp_op = "in"; break;
285
comp_op = "out"; break;
287
comp_op = "atop"; break;
289
comp_op = "xor"; break;
290
case COMPOSITE_ARITHMETIC:
291
comp_op = "arithmetic"; break;
295
repr->setAttribute("operator", comp_op);
297
if (comp->composite_operator == COMPOSITE_ARITHMETIC) {
298
sp_repr_set_svg_double(repr, "k1", comp->k1);
299
sp_repr_set_svg_double(repr, "k2", comp->k2);
300
sp_repr_set_svg_double(repr, "k3", comp->k3);
301
sp_repr_set_svg_double(repr, "k4", comp->k4);
303
repr->setAttribute("k1", 0);
304
repr->setAttribute("k2", 0);
305
repr->setAttribute("k3", 0);
306
repr->setAttribute("k4", 0);
309
if (((SPObjectClass *) feComposite_parent_class)->write) {
310
((SPObjectClass *) feComposite_parent_class)->write(object, doc, repr, flags);
316
static void sp_feComposite_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
317
g_assert(primitive != NULL);
318
g_assert(filter != NULL);
320
SPFeComposite *sp_composite = SP_FECOMPOSITE(primitive);
322
int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_COMPOSITE);
323
Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
324
Inkscape::Filters::FilterComposite *nr_composite = dynamic_cast<Inkscape::Filters::FilterComposite*>(nr_primitive);
325
g_assert(nr_composite != NULL);
327
sp_filter_primitive_renderer_common(primitive, nr_primitive);
329
nr_composite->set_operator(sp_composite->composite_operator);
330
nr_composite->set_input(1, sp_composite->in2);
331
if (sp_composite->composite_operator == COMPOSITE_ARITHMETIC) {
332
nr_composite->set_arithmetic(sp_composite->k1, sp_composite->k2,
333
sp_composite->k3, sp_composite->k4);
341
c-file-style:"stroustrup"
342
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
347
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :