Code

safety check
[inkscape.git] / src / filter-chemistry.cpp
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     // create a new filter
37     Inkscape::XML::Node *repr;
38     repr = sp_repr_new("svg:filter");
39     repr->setAttribute("inkscape:collect", "always");
41     double rx = radius * (expansionY != 0? (expansion / expansionY) : 1);
42     double ry = radius * (expansionX != 0? (expansion / expansionX) : 1);
44     if (width != 0 && height != 0 && (2 * rx > width * 0.1 || 2 * ry > height * 0.1)) {
45         // If not within the default 10% margin (see
46         // http://www.w3.org/TR/SVG11/filters.html#FilterEffectsRegion), specify margins
47         double xmargin = 2 * (rx) / width;
48         double ymargin = 2 * (ry) / height;
50         // TODO: set it in UserSpaceOnUse instead?
51         sp_repr_set_svg_double(repr, "x", -xmargin);
52         sp_repr_set_svg_double(repr, "width", 1 + 2 * xmargin);
53         sp_repr_set_svg_double(repr, "y", -ymargin);
54         sp_repr_set_svg_double(repr, "height", 1 + 2 * ymargin);
55     }
57     //create feGaussianBlur node
58     Inkscape::XML::Node *b_repr;
59     b_repr = sp_repr_new("svg:feGaussianBlur");
60     b_repr->setAttribute("inkscape:collect", "always");
61     
62     double stdDeviation = radius;
63     if (expansion != 0)
64         stdDeviation /= expansion;
66     //set stdDeviation attribute
67     sp_repr_set_svg_double(b_repr, "stdDeviation", stdDeviation);
68     
69     //set feGaussianBlur as child of filter node
70     repr->appendChild(b_repr);
71     Inkscape::GC::release(b_repr);
72     
73     // Append the new filter node to defs
74     SP_OBJECT_REPR(defs)->appendChild(repr);
75     Inkscape::GC::release(repr);
77     // get corresponding object
78     SPFilter *f = SP_FILTER( document->getObjectByRepr(repr) );
79     SPGaussianBlur *b = SP_GAUSSIANBLUR( document->getObjectByRepr(b_repr) );
80     
81     g_assert(f != NULL);
82     g_assert(SP_IS_FILTER(f));
83     g_assert(b != NULL);
84     g_assert(SP_IS_GAUSSIANBLUR(b));
86     return f;
87 }
89 /**
90  * Creates a filter with blur primitive of specified radius for the given item
91  */
92 SPFilter *
93 new_filter_gaussian_blur_from_item (SPDocument *document, SPItem *item, gdouble radius)
94 {
95     NR::Rect const r = sp_item_bbox_desktop(item);
96     double width = r.extent(NR::X);
97     double height = r.extent(NR::Y);
99     NR::Matrix i2d = sp_item_i2d_affine (item);
101     return (new_filter_gaussian_blur (document, radius, i2d.expansion(), i2d.expansionX(), i2d.expansionY(), width, height));
104 void remove_filter (SPObject *item, bool recursive)
106         SPCSSAttr *css = sp_repr_css_attr_new ();
107         sp_repr_css_unset_property (css, "filter");
108         if (recursive)
109                 sp_repr_css_change_recursive(SP_OBJECT_REPR(item), css, "style");
110         else
111                 sp_repr_css_change (SP_OBJECT_REPR(item), css, "style");
112       sp_repr_css_attr_unref (css);
115 /*
116   Local Variables:
117   mode:c++
118   c-file-style:"stroustrup"
119   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
120   indent-tabs-mode:nil
121   fill-column:99
122   End:
123 */
124 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :