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 "blend.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::Document *doc, Inkscape::XML::Node *repr, guint flags);
43 static void sp_feBlend_build_renderer(SPFilterPrimitive *sp_prim, Inkscape::Filters::Filter *filter);
45 static SPFilterPrimitiveClass *feBlend_parent_class;
47 GType
48 sp_feBlend_get_type()
49 {
50 static GType feBlend_type = 0;
52 if (!feBlend_type) {
53 GTypeInfo feBlend_info = {
54 sizeof(SPFeBlendClass),
55 NULL, NULL,
56 (GClassInitFunc) sp_feBlend_class_init,
57 NULL, NULL,
58 sizeof(SPFeBlend),
59 16,
60 (GInstanceInitFunc) sp_feBlend_init,
61 NULL, /* value_table */
62 };
63 feBlend_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeBlend", &feBlend_info, (GTypeFlags)0);
64 }
65 return feBlend_type;
66 }
68 static void
69 sp_feBlend_class_init(SPFeBlendClass *klass)
70 {
71 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
72 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
74 feBlend_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
76 sp_object_class->build = sp_feBlend_build;
77 sp_object_class->release = sp_feBlend_release;
78 sp_object_class->write = sp_feBlend_write;
79 sp_object_class->set = sp_feBlend_set;
80 sp_object_class->update = sp_feBlend_update;
82 sp_primitive_class->build_renderer = sp_feBlend_build_renderer;
83 }
85 static void
86 sp_feBlend_init(SPFeBlend *feBlend)
87 {
88 feBlend->in2 = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
89 }
91 /**
92 * Reads the Inkscape::XML::Node, and initializes SPFeBlend variables. For this to get called,
93 * our name must be associated with a repr via "sp_object_type_register". Best done through
94 * sp-object-repr.cpp's repr_name_entries array.
95 */
96 static void
97 sp_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
98 {
99 SPFeBlend *blend = SP_FEBLEND(object);
101 if (((SPObjectClass *) feBlend_parent_class)->build) {
102 ((SPObjectClass *) feBlend_parent_class)->build(object, document, repr);
103 }
105 /*LOAD ATTRIBUTES FROM REPR HERE*/
106 object->readAttr( "mode");
107 object->readAttr( "in2");
109 /* Unlike normal in, in2 is required attribute. Make sure, we can call
110 * it by some name. */
111 if (blend->in2 == Inkscape::Filters::NR_FILTER_SLOT_NOT_SET ||
112 blend->in2 == Inkscape::Filters::NR_FILTER_UNNAMED_SLOT)
113 {
114 SPFilter *parent = SP_FILTER(object->parent);
115 blend->in2 = sp_filter_primitive_name_previous_out(blend);
116 repr->setAttribute("in2", sp_filter_name_for_image(parent, blend->in2));
117 }
118 }
120 /**
121 * Drops any allocated memory.
122 */
123 static void
124 sp_feBlend_release(SPObject *object)
125 {
126 if (((SPObjectClass *) feBlend_parent_class)->release)
127 ((SPObjectClass *) feBlend_parent_class)->release(object);
128 }
130 static Inkscape::Filters::FilterBlendMode sp_feBlend_readmode(gchar const *value)
131 {
132 if (!value) return Inkscape::Filters::BLEND_NORMAL;
133 switch (value[0]) {
134 case 'n':
135 if (strncmp(value, "normal", 6) == 0)
136 return Inkscape::Filters::BLEND_NORMAL;
137 break;
138 case 'm':
139 if (strncmp(value, "multiply", 8) == 0)
140 return Inkscape::Filters::BLEND_MULTIPLY;
141 break;
142 case 's':
143 if (strncmp(value, "screen", 6) == 0)
144 return Inkscape::Filters::BLEND_SCREEN;
145 break;
146 case 'd':
147 if (strncmp(value, "darken", 6) == 0)
148 return Inkscape::Filters::BLEND_DARKEN;
149 break;
150 case 'l':
151 if (strncmp(value, "lighten", 7) == 0)
152 return Inkscape::Filters::BLEND_LIGHTEN;
153 break;
154 default:
155 // do nothing by default
156 break;
157 }
158 return Inkscape::Filters::BLEND_NORMAL;
159 }
161 /**
162 * Sets a specific value in the SPFeBlend.
163 */
164 static void
165 sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value)
166 {
167 SPFeBlend *feBlend = SP_FEBLEND(object);
168 (void)feBlend;
170 Inkscape::Filters::FilterBlendMode mode;
171 int input;
172 switch(key) {
173 /*DEAL WITH SETTING ATTRIBUTES HERE*/
174 case SP_ATTR_MODE:
175 mode = sp_feBlend_readmode(value);
176 if (mode != feBlend->blend_mode) {
177 feBlend->blend_mode = mode;
178 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
179 }
180 break;
181 case SP_ATTR_IN2:
182 input = sp_filter_primitive_read_in(feBlend, value);
183 if (input != feBlend->in2) {
184 feBlend->in2 = input;
185 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
186 }
187 break;
188 default:
189 if (((SPObjectClass *) feBlend_parent_class)->set)
190 ((SPObjectClass *) feBlend_parent_class)->set(object, key, value);
191 break;
192 }
194 }
196 /**
197 * Receives update notifications.
198 */
199 static void
200 sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags)
201 {
202 SPFeBlend *blend = SP_FEBLEND(object);
204 if (flags & SP_OBJECT_MODIFIED_FLAG) {
205 object->readAttr( "mode");
206 object->readAttr( "in2");
207 }
209 /* Unlike normal in, in2 is required attribute. Make sure, we can call
210 * it by some name. */
211 if (blend->in2 == Inkscape::Filters::NR_FILTER_SLOT_NOT_SET ||
212 blend->in2 == Inkscape::Filters::NR_FILTER_UNNAMED_SLOT)
213 {
214 SPFilter *parent = SP_FILTER(object->parent);
215 blend->in2 = sp_filter_primitive_name_previous_out(blend);
217 //XML Tree being used directly here while it shouldn't be.
218 object->getRepr()->setAttribute("in2", sp_filter_name_for_image(parent, blend->in2));
219 }
221 if (((SPObjectClass *) feBlend_parent_class)->update) {
222 ((SPObjectClass *) feBlend_parent_class)->update(object, ctx, flags);
223 }
224 }
226 /**
227 * Writes its settings to an incoming repr object, if any.
228 */
229 static Inkscape::XML::Node *
230 sp_feBlend_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
231 {
232 SPFeBlend *blend = SP_FEBLEND(object);
233 SPFilter *parent = SP_FILTER(object->parent);
235 if (!repr) {
236 repr = doc->createElement("svg:feBlend");
237 }
239 gchar const *out_name = sp_filter_name_for_image(parent, blend->in2);
240 if (out_name) {
241 repr->setAttribute("in2", out_name);
242 } else {
243 SPObject *i = parent->children;
244 while (i && i->next != object) i = i->next;
245 SPFilterPrimitive *i_prim = SP_FILTER_PRIMITIVE(i);
246 out_name = sp_filter_name_for_image(parent, i_prim->image_out);
247 repr->setAttribute("in2", out_name);
248 if (!out_name) {
249 g_warning("Unable to set in2 for feBlend");
250 }
251 }
253 char const *mode;
254 switch(blend->blend_mode) {
255 case Inkscape::Filters::BLEND_NORMAL:
256 mode = "normal"; break;
257 case Inkscape::Filters::BLEND_MULTIPLY:
258 mode = "multiply"; break;
259 case Inkscape::Filters::BLEND_SCREEN:
260 mode = "screen"; break;
261 case Inkscape::Filters::BLEND_DARKEN:
262 mode = "darken"; break;
263 case Inkscape::Filters::BLEND_LIGHTEN:
264 mode = "lighten"; break;
265 default:
266 mode = 0;
267 }
268 repr->setAttribute("mode", mode);
270 if (((SPObjectClass *) feBlend_parent_class)->write) {
271 ((SPObjectClass *) feBlend_parent_class)->write(object, doc, repr, flags);
272 }
274 return repr;
275 }
277 static void sp_feBlend_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
278 g_assert(primitive != NULL);
279 g_assert(filter != NULL);
281 SPFeBlend *sp_blend = SP_FEBLEND(primitive);
283 int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_BLEND);
284 Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
285 Inkscape::Filters::FilterBlend *nr_blend = dynamic_cast<Inkscape::Filters::FilterBlend*>(nr_primitive);
286 g_assert(nr_blend != NULL);
288 sp_filter_primitive_renderer_common(primitive, nr_primitive);
290 nr_blend->set_mode(sp_blend->blend_mode);
291 nr_blend->set_input(1, sp_blend->in2);
292 }
294 /*
295 Local Variables:
296 mode:c++
297 c-file-style:"stroustrup"
298 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
299 indent-tabs-mode:nil
300 fill-column:99
301 End:
302 */
303 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :