Code

49ab18a25fcf7c25343d01f8e90d9e797a6c085a
[inkscape.git] / src / sp-feblend.cpp
1 #define __SP_FEBLEND_CPP__
3 /** \file
4  * SVG <feBlend> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   Hugo Rodrigues <haa.rodrigues@gmail.com>
10  *   Niko Kiirala <niko@kiirala.com>
11  *
12  * Copyright (C) 2006,2007 authors
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <string.h>
23 #include "attributes.h"
24 #include "svg/svg.h"
25 #include "sp-feblend.h"
26 #include "xml/repr.h"
28 #include "display/nr-filter.h"
29 #include "display/nr-filter-primitive.h"
30 #include "display/nr-filter-blend.h"
31 #include "display/nr-filter-types.h"
33 /* FeBlend base class */
35 static void sp_feBlend_class_init(SPFeBlendClass *klass);
36 static void sp_feBlend_init(SPFeBlend *feBlend);
38 static void sp_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
39 static void sp_feBlend_release(SPObject *object);
40 static void sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value);
41 static void sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags);
42 static Inkscape::XML::Node *sp_feBlend_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
43 static void sp_feBlend_build_renderer(SPFilterPrimitive *sp_prim, NR::Filter *filter);
45 static SPFilterPrimitiveClass *feBlend_parent_class;
46 static int renderer;
48 GType
49 sp_feBlend_get_type()
50 {
51     static GType feBlend_type = 0;
53     if (!feBlend_type) {
54         GTypeInfo feBlend_info = {
55             sizeof(SPFeBlendClass),
56             NULL, NULL,
57             (GClassInitFunc) sp_feBlend_class_init,
58             NULL, NULL,
59             sizeof(SPFeBlend),
60             16,
61             (GInstanceInitFunc) sp_feBlend_init,
62             NULL,    /* value_table */
63         };
64         feBlend_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeBlend", &feBlend_info, (GTypeFlags)0);
65     }
66     return feBlend_type;
67 }
69 static void
70 sp_feBlend_class_init(SPFeBlendClass *klass)
71 {
72     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
73     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
75     feBlend_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
77     sp_object_class->build = sp_feBlend_build;
78     sp_object_class->release = sp_feBlend_release;
79     sp_object_class->write = sp_feBlend_write;
80     sp_object_class->set = sp_feBlend_set;
81     sp_object_class->update = sp_feBlend_update;
83     sp_primitive_class->build_renderer = sp_feBlend_build_renderer;
84 }
86 static void
87 sp_feBlend_init(SPFeBlend *feBlend)
88 {
89     renderer = -1;
90 }
92 /**
93  * Reads the Inkscape::XML::Node, and initializes SPFeBlend variables.  For this to get called,
94  * our name must be associated with a repr via "sp_object_type_register".  Best done through
95  * sp-object-repr.cpp's repr_name_entries array.
96  */
97 static void
98 sp_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
99 {
100     if (((SPObjectClass *) feBlend_parent_class)->build) {
101         ((SPObjectClass *) feBlend_parent_class)->build(object, document, repr);
102     }
104     /*LOAD ATTRIBUTES FROM REPR HERE*/
105     sp_object_read_attr(object, "mode");
108 /**
109  * Drops any allocated memory.
110  */
111 static void
112 sp_feBlend_release(SPObject *object)
114     if (((SPObjectClass *) feBlend_parent_class)->release)
115         ((SPObjectClass *) feBlend_parent_class)->release(object);
118 static NR::FilterBlendMode sp_feBlend_readmode(gchar const *value)
120     if (!value) return NR::BLEND_NORMAL;
121     switch (value[0]) {
122         case 'n':
123             if (strncmp(value, "normal", 6) == 0)
124                 return NR::BLEND_NORMAL;
125             break;
126         case 'm':
127             if (strncmp(value, "multiply", 8) == 0)
128                 return NR::BLEND_MULTIPLY;
129             break;
130         case 's':
131             if (strncmp(value, "screen", 6) == 0)
132                 return NR::BLEND_SCREEN;
133             break;
134         case 'd':
135             if (strncmp(value, "darken", 6) == 0)
136                 return NR::BLEND_DARKEN;
137             break;
138         case 'l':
139             if (strncmp(value, "lighten", 7) == 0)
140                 return NR::BLEND_LIGHTEN;
141             break;
142         default:
143             // do nothing by default
144             break;
145     }
146     return NR::BLEND_NORMAL;
149 /**
150  * Sets a specific value in the SPFeBlend.
151  */
152 static void
153 sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value)
155     SPFeBlend *feBlend = SP_FEBLEND(object);
156     (void)feBlend;
158     switch(key) {
159         /*DEAL WITH SETTING ATTRIBUTES HERE*/
160         case SP_ATTR_MODE:
161             feBlend->blend_mode = sp_feBlend_readmode(value);
162 /*
163             if (renderer >= 0) {
164                 NR::Filter *filter = SP_FILTER(object->parent)->_renderer;
165                 NR::FilterBlend *blend = dynamic_cast<NR::FilterBlend*>(filter->get_primitive(renderer));
166                 blend->set_mode(feBlend->blend_mode);
167             }
168 */
169             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
170             break;
171         default:
172             if (((SPObjectClass *) feBlend_parent_class)->set)
173                 ((SPObjectClass *) feBlend_parent_class)->set(object, key, value);
174             break;
175     }
179 /**
180  * Receives update notifications.
181  */
182 static void
183 sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags)
185     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
186         sp_object_read_attr(object, "mode");
187     }
189     if (((SPObjectClass *) feBlend_parent_class)->update) {
190         ((SPObjectClass *) feBlend_parent_class)->update(object, ctx, flags);
191     }
194 /**
195  * Writes its settings to an incoming repr object, if any.
196  */
197 static Inkscape::XML::Node *
198 sp_feBlend_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
200     // Inkscape-only object, not copied during an "plain SVG" dump:
201     if (flags & SP_OBJECT_WRITE_EXT) {
202         if (repr) {
203             // is this sane?
204             // repr->mergeFrom(SP_OBJECT_REPR(object), "id");
205         } else {
206             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
207         }
208     }
210     if (((SPObjectClass *) feBlend_parent_class)->write) {
211         ((SPObjectClass *) feBlend_parent_class)->write(object, repr, flags);
212     }
214     return repr;
217 static void sp_feBlend_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
218     g_assert(primitive != NULL);
219     g_assert(filter != NULL);
221     SPFeBlend *sp_blend = SP_FEBLEND(primitive);
223     int primitive_n = filter->add_primitive(NR::NR_FILTER_BLEND);
224     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
225     NR::FilterBlend *nr_blend = dynamic_cast<NR::FilterBlend*>(nr_primitive);
226     g_assert(nr_blend != NULL);
228     sp_filter_primitive_renderer_common(primitive, nr_primitive);
230     nr_blend->set_mode(sp_blend->blend_mode);
232     renderer = primitive_n;
235 /*
236   Local Variables:
237   mode:c++
238   c-file-style:"stroustrup"
239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240   indent-tabs-mode:nil
241   fill-column:99
242   End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :