1 #define __SP_FILTER_CHEMISTRY_C__
3 /*
4 * Various utility methods for filters
5 *
6 * Authors:
7 * Hugo Rodrigues
8 * bulia byak
9 *
10 * Copyright (C) 2006 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
16 #include "style.h"
17 #include "document-private.h"
18 #include "desktop-style.h"
20 #include "sp-filter.h"
21 #include "sp-gaussian-blur.h"
22 #include "svg/css-ostringstream.h"
24 #include "xml/repr.h"
26 /**
27 * Creates a filter with blur primitive of specified radius for an item with the given matrix expansion, width and height
28 */
29 SPFilter *
30 new_filter_gaussian_blur (SPDocument *document, gdouble radius, double expansion, double expansionX, double expansionY, double width, double height)
31 {
32 g_return_val_if_fail(document != NULL, NULL);
34 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
36 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
38 // create a new filter
39 Inkscape::XML::Node *repr;
40 repr = xml_doc->createElement("svg:filter");
41 repr->setAttribute("inkscape:collect", "always");
43 double rx = radius * (expansionY != 0? (expansion / expansionY) : 1);
44 double ry = radius * (expansionX != 0? (expansion / expansionX) : 1);
46 if (width != 0 && height != 0 && (2 * rx > width * 0.1 || 2 * ry > height * 0.1)) {
47 // If not within the default 10% margin (see
48 // http://www.w3.org/TR/SVG11/filters.html#FilterEffectsRegion), specify margins
49 double xmargin = 2 * (rx) / width;
50 double ymargin = 2 * (ry) / height;
52 // TODO: set it in UserSpaceOnUse instead?
53 sp_repr_set_svg_double(repr, "x", -xmargin);
54 sp_repr_set_svg_double(repr, "width", 1 + 2 * xmargin);
55 sp_repr_set_svg_double(repr, "y", -ymargin);
56 sp_repr_set_svg_double(repr, "height", 1 + 2 * ymargin);
57 }
59 //create feGaussianBlur node
60 Inkscape::XML::Node *b_repr;
61 b_repr = xml_doc->createElement("svg:feGaussianBlur");
62 b_repr->setAttribute("inkscape:collect", "always");
64 double stdDeviation = radius;
65 if (expansion != 0)
66 stdDeviation /= expansion;
68 //set stdDeviation attribute
69 sp_repr_set_svg_double(b_repr, "stdDeviation", stdDeviation);
71 //set feGaussianBlur as child of filter node
72 repr->appendChild(b_repr);
73 Inkscape::GC::release(b_repr);
75 // Append the new filter node to defs
76 SP_OBJECT_REPR(defs)->appendChild(repr);
77 Inkscape::GC::release(repr);
79 // get corresponding object
80 SPFilter *f = SP_FILTER( document->getObjectByRepr(repr) );
81 SPGaussianBlur *b = SP_GAUSSIANBLUR( document->getObjectByRepr(b_repr) );
83 g_assert(f != NULL);
84 g_assert(SP_IS_FILTER(f));
85 g_assert(b != NULL);
86 g_assert(SP_IS_GAUSSIANBLUR(b));
88 return f;
89 }
91 /**
92 * Creates a filter with blur primitive of specified radius for the given item
93 */
94 SPFilter *
95 new_filter_gaussian_blur_from_item (SPDocument *document, SPItem *item, gdouble radius)
96 {
97 NR::Rect const r = sp_item_bbox_desktop(item);
98 double width = r.extent(NR::X);
99 double height = r.extent(NR::Y);
101 NR::Matrix i2d = sp_item_i2d_affine (item);
103 return (new_filter_gaussian_blur (document, radius, i2d.expansion(), i2d.expansionX(), i2d.expansionY(), width, height));
104 }
106 void remove_filter (SPObject *item, bool recursive)
107 {
108 SPCSSAttr *css = sp_repr_css_attr_new ();
109 sp_repr_css_unset_property (css, "filter");
110 if (recursive)
111 sp_repr_css_change_recursive(SP_OBJECT_REPR(item), css, "style");
112 else
113 sp_repr_css_change (SP_OBJECT_REPR(item), css, "style");
114 sp_repr_css_attr_unref (css);
115 }
117 /*
118 Local Variables:
119 mode:c++
120 c-file-style:"stroustrup"
121 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
122 indent-tabs-mode:nil
123 fill-column:99
124 End:
125 */
126 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :