1 /** \file
2 * SVG <feBlend> implementation.
3 *
4 */
5 /*
6 * Authors:
7 * Hugo Rodrigues <haa.rodrigues@gmail.com>
8 * Niko Kiirala <niko@kiirala.com>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2006,2007 authors
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 <string.h>
22 #include "attributes.h"
23 #include "svg/svg.h"
24 #include "blend.h"
25 #include "xml/repr.h"
27 #include "display/nr-filter.h"
28 #include "display/nr-filter-primitive.h"
29 #include "display/nr-filter-blend.h"
30 #include "display/nr-filter-types.h"
32 /* FeBlend base class */
34 static void sp_feBlend_class_init(SPFeBlendClass *klass);
35 static void sp_feBlend_init(SPFeBlend *feBlend);
37 static void sp_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
38 static void sp_feBlend_release(SPObject *object);
39 static void sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value);
40 static void sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags);
41 static Inkscape::XML::Node *sp_feBlend_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
42 static void sp_feBlend_build_renderer(SPFilterPrimitive *sp_prim, Inkscape::Filters::Filter *filter);
44 static SPFilterPrimitiveClass *feBlend_parent_class;
46 GType
47 sp_feBlend_get_type()
48 {
49 static GType feBlend_type = 0;
51 if (!feBlend_type) {
52 GTypeInfo feBlend_info = {
53 sizeof(SPFeBlendClass),
54 NULL, NULL,
55 (GClassInitFunc) sp_feBlend_class_init,
56 NULL, NULL,
57 sizeof(SPFeBlend),
58 16,
59 (GInstanceInitFunc) sp_feBlend_init,
60 NULL, /* value_table */
61 };
62 feBlend_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeBlend", &feBlend_info, (GTypeFlags)0);
63 }
64 return feBlend_type;
65 }
67 static void
68 sp_feBlend_class_init(SPFeBlendClass *klass)
69 {
70 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
71 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
73 feBlend_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
75 sp_object_class->build = sp_feBlend_build;
76 sp_object_class->release = sp_feBlend_release;
77 sp_object_class->write = sp_feBlend_write;
78 sp_object_class->set = sp_feBlend_set;
79 sp_object_class->update = sp_feBlend_update;
81 sp_primitive_class->build_renderer = sp_feBlend_build_renderer;
82 }
84 static void
85 sp_feBlend_init(SPFeBlend *feBlend)
86 {
87 feBlend->in2 = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
88 }
90 /**
91 * Reads the Inkscape::XML::Node, and initializes SPFeBlend 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_feBlend_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
97 {
98 SPFeBlend *blend = SP_FEBLEND(object);
100 if (((SPObjectClass *) feBlend_parent_class)->build) {
101 ((SPObjectClass *) feBlend_parent_class)->build(object, document, repr);
102 }
104 /*LOAD ATTRIBUTES FROM REPR HERE*/
105 object->readAttr( "mode" );
106 object->readAttr( "in2" );
108 /* Unlike normal in, in2 is required attribute. Make sure, we can call
109 * it by some name. */
110 if (blend->in2 == Inkscape::Filters::NR_FILTER_SLOT_NOT_SET ||
111 blend->in2 == Inkscape::Filters::NR_FILTER_UNNAMED_SLOT)
112 {
113 SPFilter *parent = SP_FILTER(object->parent);
114 blend->in2 = sp_filter_primitive_name_previous_out(blend);
115 repr->setAttribute("in2", sp_filter_name_for_image(parent, blend->in2));
116 }
117 }
119 /**
120 * Drops any allocated memory.
121 */
122 static void
123 sp_feBlend_release(SPObject *object)
124 {
125 if (((SPObjectClass *) feBlend_parent_class)->release)
126 ((SPObjectClass *) feBlend_parent_class)->release(object);
127 }
129 static Inkscape::Filters::FilterBlendMode sp_feBlend_readmode(gchar const *value)
130 {
131 if (!value) return Inkscape::Filters::BLEND_NORMAL;
132 switch (value[0]) {
133 case 'n':
134 if (strncmp(value, "normal", 6) == 0)
135 return Inkscape::Filters::BLEND_NORMAL;
136 break;
137 case 'm':
138 if (strncmp(value, "multiply", 8) == 0)
139 return Inkscape::Filters::BLEND_MULTIPLY;
140 break;
141 case 's':
142 if (strncmp(value, "screen", 6) == 0)
143 return Inkscape::Filters::BLEND_SCREEN;
144 break;
145 case 'd':
146 if (strncmp(value, "darken", 6) == 0)
147 return Inkscape::Filters::BLEND_DARKEN;
148 break;
149 case 'l':
150 if (strncmp(value, "lighten", 7) == 0)
151 return Inkscape::Filters::BLEND_LIGHTEN;
152 break;
153 default:
154 // do nothing by default
155 break;
156 }
157 return Inkscape::Filters::BLEND_NORMAL;
158 }
160 /**
161 * Sets a specific value in the SPFeBlend.
162 */
163 static void
164 sp_feBlend_set(SPObject *object, unsigned int key, gchar const *value)
165 {
166 SPFeBlend *feBlend = SP_FEBLEND(object);
167 (void)feBlend;
169 Inkscape::Filters::FilterBlendMode mode;
170 int input;
171 switch(key) {
172 /*DEAL WITH SETTING ATTRIBUTES HERE*/
173 case SP_ATTR_MODE:
174 mode = sp_feBlend_readmode(value);
175 if (mode != feBlend->blend_mode) {
176 feBlend->blend_mode = mode;
177 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
178 }
179 break;
180 case SP_ATTR_IN2:
181 input = sp_filter_primitive_read_in(feBlend, value);
182 if (input != feBlend->in2) {
183 feBlend->in2 = input;
184 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
185 }
186 break;
187 default:
188 if (((SPObjectClass *) feBlend_parent_class)->set)
189 ((SPObjectClass *) feBlend_parent_class)->set(object, key, value);
190 break;
191 }
193 }
195 /**
196 * Receives update notifications.
197 */
198 static void
199 sp_feBlend_update(SPObject *object, SPCtx *ctx, guint flags)
200 {
201 SPFeBlend *blend = SP_FEBLEND(object);
203 if (flags & SP_OBJECT_MODIFIED_FLAG) {
204 object->readAttr( "mode" );
205 object->readAttr( "in2" );
206 }
208 /* Unlike normal in, in2 is required attribute. Make sure, we can call
209 * it by some name. */
210 if (blend->in2 == Inkscape::Filters::NR_FILTER_SLOT_NOT_SET ||
211 blend->in2 == Inkscape::Filters::NR_FILTER_UNNAMED_SLOT)
212 {
213 SPFilter *parent = SP_FILTER(object->parent);
214 blend->in2 = sp_filter_primitive_name_previous_out(blend);
216 //XML Tree being used directly here while it shouldn't be.
217 object->getRepr()->setAttribute("in2", sp_filter_name_for_image(parent, blend->in2));
218 }
220 if (((SPObjectClass *) feBlend_parent_class)->update) {
221 ((SPObjectClass *) feBlend_parent_class)->update(object, ctx, flags);
222 }
223 }
225 /**
226 * Writes its settings to an incoming repr object, if any.
227 */
228 static Inkscape::XML::Node *
229 sp_feBlend_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
230 {
231 SPFeBlend *blend = SP_FEBLEND(object);
232 SPFilter *parent = SP_FILTER(object->parent);
234 if (!repr) {
235 repr = doc->createElement("svg:feBlend");
236 }
238 gchar const *out_name = sp_filter_name_for_image(parent, blend->in2);
239 if (out_name) {
240 repr->setAttribute("in2", out_name);
241 } else {
242 SPObject *i = parent->children;
243 while (i && i->next != object) i = i->next;
244 SPFilterPrimitive *i_prim = SP_FILTER_PRIMITIVE(i);
245 out_name = sp_filter_name_for_image(parent, i_prim->image_out);
246 repr->setAttribute("in2", out_name);
247 if (!out_name) {
248 g_warning("Unable to set in2 for feBlend");
249 }
250 }
252 char const *mode;
253 switch(blend->blend_mode) {
254 case Inkscape::Filters::BLEND_NORMAL:
255 mode = "normal"; break;
256 case Inkscape::Filters::BLEND_MULTIPLY:
257 mode = "multiply"; break;
258 case Inkscape::Filters::BLEND_SCREEN:
259 mode = "screen"; break;
260 case Inkscape::Filters::BLEND_DARKEN:
261 mode = "darken"; break;
262 case Inkscape::Filters::BLEND_LIGHTEN:
263 mode = "lighten"; break;
264 default:
265 mode = 0;
266 }
267 repr->setAttribute("mode", mode);
269 if (((SPObjectClass *) feBlend_parent_class)->write) {
270 ((SPObjectClass *) feBlend_parent_class)->write(object, doc, repr, flags);
271 }
273 return repr;
274 }
276 static void sp_feBlend_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
277 g_assert(primitive != NULL);
278 g_assert(filter != NULL);
280 SPFeBlend *sp_blend = SP_FEBLEND(primitive);
282 int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_BLEND);
283 Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
284 Inkscape::Filters::FilterBlend *nr_blend = dynamic_cast<Inkscape::Filters::FilterBlend*>(nr_primitive);
285 g_assert(nr_blend != NULL);
287 sp_filter_primitive_renderer_common(primitive, nr_primitive);
289 nr_blend->set_mode(sp_blend->blend_mode);
290 nr_blend->set_input(1, sp_blend->in2);
291 }
293 /*
294 Local Variables:
295 mode:c++
296 c-file-style:"stroustrup"
297 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
298 indent-tabs-mode:nil
299 fill-column:99
300 End:
301 */
302 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :