678e492ee414257cf36f5582e42b990323da6143
1 /**
2 \file bluredge.cpp
4 A plug-in to add an effect to blur the edges of an object.
5 */
6 /*
7 * Authors:
8 * Ted Gould <ted@gould.cx>
9 *
10 * Copyright (C) 2005 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <vector>
16 #include "desktop.h"
17 #include "selection.h"
18 #include "helper/action.h"
19 #include "prefs-utils.h"
20 #include "path-chemistry.h"
21 #include "sp-item.h"
23 #include "util/glib-list-iterators.h"
25 #include "extension/effect.h"
26 #include "extension/system.h"
29 #include "bluredge.h"
31 namespace Inkscape {
32 namespace Extension {
33 namespace Internal {
36 /**
37 \brief A function to allocated anything -- just an example here
38 \param module Unused
39 \return Whether the load was sucessful
40 */
41 bool
42 BlurEdge::load (Inkscape::Extension::Extension *module)
43 {
44 // std::cout << "Hey, I'm Blur Edge, I'm loading!" << std::endl;
45 return TRUE;
46 }
48 /**
49 \brief This actually blurs the edge.
50 \param module The effect that was called (unused)
51 \param document What should be edited.
52 */
53 void
54 BlurEdge::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document)
55 {
56 Inkscape::Selection * selection = ((SPDesktop *)document)->selection;
58 float width = module->get_param_float("blur-width");
59 int steps = module->get_param_int("num-steps");
61 double old_offset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
63 using Inkscape::Util::GSListConstIterator;
64 // TODO need to properly refcount the items, at least
65 std::list<SPItem *> items;
66 items.insert<GSListConstIterator<SPItem *> >(items.end(), selection->itemList(), NULL);
67 selection->clear();
69 for(std::list<SPItem *>::iterator item = items.begin();
70 item != items.end(); item++) {
71 SPItem * spitem = *item;
73 std::vector<Inkscape::XML::Node *> new_items(steps);
74 Inkscape::XML::Node * new_group = sp_repr_new("svg:g");
75 (SP_OBJECT_REPR(spitem)->parent())->appendChild(new_group);
77 double orig_opacity = sp_repr_css_double_property(sp_repr_css_attr(SP_OBJECT_REPR(spitem), "style"), "opacity", 1.0);
78 char opacity_string[64];
79 g_ascii_formatd(opacity_string, sizeof(opacity_string), "%f",
80 orig_opacity / (steps));
82 for (int i = 0; i < steps; i++) {
83 double offset = (width / (float)(steps - 1) * (float)i) - (width / 2.0);
85 new_items[i] = (SP_OBJECT_REPR(spitem))->duplicate();
87 SPCSSAttr * css = sp_repr_css_attr(new_items[i], "style");
88 sp_repr_css_set_property(css, "opacity", opacity_string);
89 sp_repr_css_change(new_items[i], css, "style");
91 new_group->appendChild(new_items[i]);
92 selection->add(new_items[i]);
93 sp_selected_path_to_curves();
95 if (offset < 0.0) {
96 /* Doing an inset here folks */
97 offset *= -1.0;
98 prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
99 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_INSET)->get_action(document), NULL);
100 } else if (offset > 0.0) {
101 prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
102 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_OFFSET)->get_action(document), NULL);
103 }
105 selection->clear();
106 }
108 }
110 prefs_set_double_attribute("options.defaultoffsetwidth", "value", old_offset);
112 selection->clear();
113 selection->add(items.begin(), items.end());
115 return;
116 }
118 Gtk::Widget *
119 BlurEdge::prefs_effect(Inkscape::Extension::Effect * module, Inkscape::UI::View::View * view)
120 {
121 return module->autogui(NULL, NULL);
122 }
124 #include "clear-n_.h"
126 void
127 BlurEdge::init (void)
128 {
129 Inkscape::Extension::build_from_mem(
130 "<inkscape-extension>\n"
131 "<name>" N_("Blur Edge") "</name>\n"
132 "<id>org.inkscape.effect.bluredge</id>\n"
133 "<param name=\"blur-width\" gui-text=\"" N_("Blur Width") "\" gui-description=\"" N_("Width in pixels of the blurred area") "\" scope=\"document\" type=\"float\" min=\"1.0\" max=\"50.0\">1.0</param>\n"
134 "<param name=\"num-steps\" gui-text=\"" N_("Number of Steps") "\" gui-description=\"" N_("Number of copies of the object to make to simulate the blur") "\" scope=\"document\" type=\"int\" min=\"5\" max=\"100\">11</param>\n"
135 "<effect>\n"
136 "<object-type>all</object-type>\n"
137 "<effects-menu>\n"
138 "<submenu name=\"" N_("Generate from Path") "\" />\n"
139 "</effects-menu>\n"
140 "</effect>\n"
141 "</inkscape-extension>\n" , new BlurEdge());
142 return;
143 }
145 }; /* namespace Internal */
146 }; /* namespace Extension */
147 }; /* namespace Inkscape */
149 /*
150 Local Variables:
151 mode:c++
152 c-file-style:"stroustrup"
153 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
154 indent-tabs-mode:nil
155 fill-column:99
156 End:
157 */
158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :