Code

Fixed blur not rotating with the object in some cases (bug 1762289)
[inkscape.git] / src / sp-filter.cpp
1 #define __SP_FILTER_CPP__
3 /** \file
4  * SVG <filter> implementation.
5  */
6 /*
7  * Authors:
8  *   Hugo Rodrigues <haa.rodrigues@gmail.com>
9  *   Niko Kiirala <niko@kiirala.com>
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 <map>
21 #include <string.h>
22 using std::map;
23 using std::pair;
25 #include "attributes.h"
26 #include "document.h"
27 #include "sp-filter.h"
28 #include "sp-filter-reference.h"
29 #include "uri.h"
30 #include "xml/repr.h"
32 #define SP_MACROS_SILENT
33 #include "macros.h"
35 #include "display/nr-filter.cpp"
37 /* Filter base class */
39 static void sp_filter_class_init(SPFilterClass *klass);
40 static void sp_filter_init(SPFilter *filter);
42 static void sp_filter_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
43 static void sp_filter_release(SPObject *object);
44 static void sp_filter_set(SPObject *object, unsigned int key, gchar const *value);
45 static void sp_filter_update(SPObject *object, SPCtx *ctx, guint flags);
46 static void sp_filter_child_added(SPObject *object,
47                                     Inkscape::XML::Node *child,
48                                     Inkscape::XML::Node *ref);
49 static void sp_filter_remove_child(SPObject *object, Inkscape::XML::Node *child);
50 static Inkscape::XML::Node *sp_filter_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
52 static void filter_ref_changed(SPObject *old_ref, SPObject *ref, SPFilter *filter);
53 static void filter_ref_modified(SPObject *href, guint flags, SPFilter *filter);
55 static SPObjectClass *filter_parent_class;
57 GType
58 sp_filter_get_type()
59 {
60     static GType filter_type = 0;
62     if (!filter_type) {
63         GTypeInfo filter_info = {
64             sizeof(SPFilterClass),
65             NULL, NULL,
66             (GClassInitFunc) sp_filter_class_init,
67             NULL, NULL,
68             sizeof(SPFilter),
69             16,
70             (GInstanceInitFunc) sp_filter_init,
71             NULL,    /* value_table */
72         };
73         filter_type = g_type_register_static(SP_TYPE_OBJECT, "SPFilter", &filter_info, (GTypeFlags)0);
74     }
75     return filter_type;
76 }
78 static void
79 sp_filter_class_init(SPFilterClass *klass)
80 {
82     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
84     filter_parent_class = (SPObjectClass*)g_type_class_peek_parent(klass);
86     sp_object_class->build = sp_filter_build;
87     sp_object_class->release = sp_filter_release;
88     sp_object_class->write = sp_filter_write;
89     sp_object_class->set = sp_filter_set;
90     sp_object_class->update = sp_filter_update;
91     sp_object_class->child_added = sp_filter_child_added;
92     sp_object_class->remove_child = sp_filter_remove_child;
93 }
95 static void
96 sp_filter_init(SPFilter *filter)
97 {
98     filter->href = new SPFilterReference(SP_OBJECT(filter));
99     filter->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(filter_ref_changed), filter));
101     filter->x = 0;
102     filter->y = 0;
103     filter->width = 0;
104     filter->height = 0;
106     filter->filterUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
107     filter->primitiveUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
108     filter->filterUnits_set = FALSE;
109     filter->primitiveUnits_set = FALSE;
111     filter->_renderer = NULL;
112     
113     filter->_image_name = map<gchar *, int, ltstr>();
115     filter->filterRes = NumberOptNumber();
117     new (&filter->modified_connection) sigc::connection();
120 /**
121  * Reads the Inkscape::XML::Node, and initializes SPFilter variables.  For this to get called,
122  * our name must be associated with a repr via "sp_object_type_register".  Best done through
123  * sp-object-repr.cpp's repr_name_entries array.
124  */
125 static void
126 sp_filter_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
128     if (((SPObjectClass *) filter_parent_class)->build) {
129         ((SPObjectClass *) filter_parent_class)->build(object, document, repr);
130     }
132     //Read values of key attributes from XML nodes into object.
133     sp_object_read_attr(object, "filterUnits");
134     sp_object_read_attr(object, "primitiveUnits");
135     sp_object_read_attr(object, "x");
136     sp_object_read_attr(object, "y");
137     sp_object_read_attr(object, "width");
138     sp_object_read_attr(object, "height");
139     sp_object_read_attr(object, "filterRes");
140     sp_object_read_attr(object, "xlink:href");
142 //is this necessary?
143     sp_document_add_resource(document, "filter", object);
146 /**
147  * Drops any allocated memory.
148  */
149 static void
150 sp_filter_release(SPObject *object)
152     SPFilter *filter = SP_FILTER(object);
154     if (SP_OBJECT_DOCUMENT(object)) {
155         /* Unregister ourselves */
156         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "filter", SP_OBJECT(object));
157     }
159 //TODO: release resources here
161     //release href
162     if (filter->href) {
163         filter->modified_connection.disconnect();
164         filter->href->detach();
165         delete filter->href;
166         filter->href = NULL;
167     }
169     filter->modified_connection.~connection();
171     if (((SPObjectClass *) filter_parent_class)->release)
172         ((SPObjectClass *) filter_parent_class)->release(object);
175 /**
176  * Sets a specific value in the SPFilter.
177  */
178 static void
179 sp_filter_set(SPObject *object, unsigned int key, gchar const *value)
181     SPFilter *filter = SP_FILTER(object);
183     switch (key) {
184         case SP_ATTR_FILTERUNITS:
185             if (value) {
186                 if (!strcmp(value, "userSpaceOnUse")) {
187                     filter->filterUnits = SP_FILTER_UNITS_USERSPACEONUSE;
188                 } else {
189                     filter->filterUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
190                 }
191                 filter->filterUnits_set = TRUE;
192             } else {
193                 filter->filterUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
194                 filter->filterUnits_set = FALSE;
195             }
196             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
197             break;
198         case SP_ATTR_PRIMITIVEUNITS:
199             if (value) {
200                 if (!strcmp(value, "userSpaceOnUse")) {
201                     filter->primitiveUnits = SP_FILTER_UNITS_USERSPACEONUSE;
202                 } else {
203                     filter->primitiveUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
204                 }
205                 filter->primitiveUnits_set = TRUE;
206             } else {
207                 filter->primitiveUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
208                 filter->primitiveUnits_set = FALSE;
209             }
210             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
211             break;
212         case SP_ATTR_X:
213             filter->x.readOrUnset(value);
214             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
215             break;
216         case SP_ATTR_Y:
217             filter->y.readOrUnset(value);
218             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
219             break;
220         case SP_ATTR_WIDTH:
221             filter->width.readOrUnset(value);
222             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
223             break;
224         case SP_ATTR_HEIGHT:
225             filter->height.readOrUnset(value);
226             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
227             break;
228         case SP_ATTR_FILTERRES:
229             filter->filterRes.set(value);
230             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
231             break;
232         case SP_ATTR_XLINK_HREF:
233             if (value) {
234                 try {
235                     filter->href->attach(Inkscape::URI(value));
236                 } catch (Inkscape::BadURIException &e) {
237                     g_warning("%s", e.what());
238                     filter->href->detach();
239                 }
240             } else {
241                 filter->href->detach();
242             }
243             break;
244         default:
245             // See if any parents need this value. 
246             if (((SPObjectClass *) filter_parent_class)->set) {
247                 ((SPObjectClass *) filter_parent_class)->set(object, key, value);
248             }
249             break;
250     }
253 /**
254  * Receives update notifications.
255  */
256 static void
257 sp_filter_update(SPObject *object, SPCtx *ctx, guint flags)
259     //SPFilter *filter = SP_FILTER(object);
261     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
262                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
264         /* do something to trigger redisplay, updates? */
266     }
268     if (((SPObjectClass *) filter_parent_class)->update) {
269         ((SPObjectClass *) filter_parent_class)->update(object, ctx, flags);
270     }
273 /**
274  * Writes its settings to an incoming repr object, if any.
275  */
276 static Inkscape::XML::Node *
277 sp_filter_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
279     SPFilter *filter = SP_FILTER(object);
281     if (!repr) {
282         repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
283     }
285     if ((flags & SP_OBJECT_WRITE_ALL) || filter->filterUnits_set) {
286         switch (filter->filterUnits) {
287             case SP_FILTER_UNITS_USERSPACEONUSE:
288                 repr->setAttribute("filterUnits", "userSpaceOnUse");
289                 break;
290             default:
291                 repr->setAttribute("filterUnits", "objectBoundingBox");
292                 break;
293         }
294     }
296     if ((flags & SP_OBJECT_WRITE_ALL) || filter->primitiveUnits_set) {
297         switch (filter->primitiveUnits) {
298             case SP_FILTER_UNITS_USERSPACEONUSE:
299                 repr->setAttribute("primitiveUnits", "userSpaceOnUse");
300                 break;
301             default:
302                 repr->setAttribute("primitiveUnits", "objectBoundingBox");
303                 break;
304         }
305     }
307     if (filter->x._set) {
308         sp_repr_set_svg_double(repr, "x", filter->x.computed);
309     } else {
310         repr->setAttribute("x", NULL);
311     }
313     if (filter->y._set) {
314         sp_repr_set_svg_double(repr, "y", filter->y.computed);
315     } else {
316         repr->setAttribute("y", NULL);
317     }
319     if (filter->width._set) {
320         sp_repr_set_svg_double(repr, "width", filter->width.computed);
321     } else {
322         repr->setAttribute("width", NULL);
323     }
325     if (filter->height._set) {
326         sp_repr_set_svg_double(repr, "height", filter->height.computed);
327     } else {
328         repr->setAttribute("height", NULL);
329     }
331     if (filter->filterRes.getNumber()>=0) {
332         gchar *tmp = filter->filterRes.getValueString();
333         repr->setAttribute("filterRes", tmp);
334         g_free(tmp);
335     } else {
336         repr->setAttribute("filterRes", NULL);
337     }
339     if (filter->href->getURI()) {
340         gchar *uri_string = filter->href->getURI()->toString();
341         repr->setAttribute("xlink:href", uri_string);
342         g_free(uri_string);
343     }
345     if (((SPObjectClass *) filter_parent_class)->write) {
346         ((SPObjectClass *) filter_parent_class)->write(object, repr, flags);
347     }
349     return repr;
353 /**
354  * Gets called when the filter is (re)attached to another filter.
355  */
356 static void
357 filter_ref_changed(SPObject *old_ref, SPObject *ref, SPFilter *filter)
359     if (old_ref) {
360         filter->modified_connection.disconnect();
361     }
362     if ( SP_IS_FILTER(ref)
363          && ref != filter )
364     {
365         filter->modified_connection = 
366             ref->connectModified(sigc::bind(sigc::ptr_fun(&filter_ref_modified), filter));
367     }
369     filter_ref_modified(ref, 0, filter);
372 static void
373 filter_ref_modified(SPObject *href, guint flags, SPFilter *filter)
375     SP_OBJECT(filter)->requestModified(SP_OBJECT_MODIFIED_FLAG);
378 /**
379  * Callback for child_added event.
380  */
381 static void
382 sp_filter_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
384     //SPFilter *f = SP_FILTER(object);
386     if (((SPObjectClass *) filter_parent_class)->child_added)
387         (* ((SPObjectClass *) filter_parent_class)->child_added)(object, child, ref);
389     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
392 /**
393  * Callback for remove_child event.
394  */
395 static void
396 sp_filter_remove_child(SPObject *object, Inkscape::XML::Node *child)
398 //    SPFilter *f = SP_FILTER(object);
400     if (((SPObjectClass *) filter_parent_class)->remove_child)
401         (* ((SPObjectClass *) filter_parent_class)->remove_child)(object, child);
403     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
406 void sp_filter_build_renderer(SPFilter *sp_filter, NR::Filter *nr_filter)
408     g_assert(sp_filter != NULL);
409     g_assert(nr_filter != NULL);
411     sp_filter->_renderer = nr_filter;
413     nr_filter->set_x(sp_filter->x);
414     nr_filter->set_y(sp_filter->y);
415     nr_filter->set_width(sp_filter->width);
416     nr_filter->set_height(sp_filter->height);
418     if (sp_filter->filterRes.getNumber() >= 0) {
419         if (sp_filter->filterRes.getOptNumber() >= 0) {
420             nr_filter->set_resolution(sp_filter->filterRes.getNumber(),
421                                       sp_filter->filterRes.getOptNumber());
422         } else {
423             nr_filter->set_resolution(sp_filter->filterRes.getNumber());
424         }
425     }
427     nr_filter->clear_primitives();
428     SPObject *primitive_obj = sp_filter->children;
429     while (primitive_obj) {
430         if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
431             SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
432             g_assert(primitive != NULL);
433             if (((SPFilterPrimitiveClass*) G_OBJECT_GET_CLASS(primitive))->build_renderer) {
434                 ((SPFilterPrimitiveClass *) G_OBJECT_GET_CLASS(primitive))->build_renderer(primitive, nr_filter);
435             } else {
436                 g_warning("Cannot build filter renderer: missing builder");
437             }
438         }
439         primitive_obj = primitive_obj->next;
440     }
443 int sp_filter_primitive_count(SPFilter *filter) {
444     g_assert(filter != NULL);
445     int count = 0;
447     SPObject *primitive_obj = filter->children;
448     while (primitive_obj) {
449         if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) count++;
450         primitive_obj = primitive_obj->next;
451     }
452     return count;
455 int sp_filter_get_image_name(SPFilter *filter, gchar const *name) {
456     gchar *name_copy = strdup(name);
457     map<gchar *, int, ltstr>::iterator result = filter->_image_name.find(name_copy);
458     free(name_copy);
459     if (result == filter->_image_name.end()) return -1;
460     else return (*result).second;
463 int sp_filter_set_image_name(SPFilter *filter, gchar const *name) {
464     int value = filter->_image_number_next;
465     filter->_image_number_next++;
466     gchar *name_copy = strdup(name);
467     pair<map<gchar*,int,ltstr>::iterator,bool> ret = filter->_image_name.insert(pair<gchar*,int>(name_copy, value));
468     if (ret.second == false) {
469         return (*ret.first).second;
470     }
471     return value;
474 bool ltstr::operator()(const char* s1, const char* s2) const
476     return strcmp(s1, s2) < 0;
480 /*
481   Local Variables:
482   mode:c++
483   c-file-style:"stroustrup"
484   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
485   indent-tabs-mode:nil
486   fill-column:99
487   End:
488 */
489 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :