summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/bluredge.cpp
blob: 3f230439a4e868352fb21412d30b082ccf6be663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
    \file bluredge.cpp
 
    A plug-in to add an effect to blur the edges of an object. 
*/
/*
 * Authors:
 *   Ted Gould <ted@gould.cx>
 *
 * Copyright (C) 2005 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "desktop.h"
#include "selection.h"
#include "helper/action.h"
#include "prefs-utils.h"
#include "path-chemistry.h"
#include "sp-item.h"

#include "util/glib-list-iterators.h"

#include "extension/effect.h"
#include "extension/system.h"


#include "bluredge.h"

namespace Inkscape {
namespace Extension {
namespace Internal {


/**
    \brief  A function to allocated anything -- just an example here
    \param  module  Unused
    \return Whether the load was sucessful
*/
bool
BlurEdge::load (Inkscape::Extension::Extension *module)
{
    // std::cout << "Hey, I'm Blur Edge, I'm loading!" << std::endl;
    return TRUE;
}

/**
    \brief  This actually blurs the edge.
    \param  module   The effect that was called (unused)
    \param  document What should be edited.
*/
void
BlurEdge::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document)
{
    Inkscape::Selection * selection     = ((SPDesktop *)document)->selection;

    float width = module->get_param_float("blur-width");
    int   steps = module->get_param_int("num-steps");

    double old_offset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);

    using Inkscape::Util::GSListConstIterator;
    // TODO need to properly refcount the items, at least
    std::list<SPItem *> items;
    items.insert<GSListConstIterator<SPItem *> >(items.end(), selection->itemList(), NULL);
    selection->clear();

    std::list<SPItem *> new_items;
    for(std::list<SPItem *>::iterator item = items.begin();
            item != items.end(); item++) {
        SPItem * spitem = *item;

        Inkscape::XML::Node * new_items[steps];
        Inkscape::XML::Node * new_group = sp_repr_new("svg:g");
        (SP_OBJECT_REPR(spitem)->parent())->appendChild(new_group);
        /** \todo  Need to figure out how to get from XML::Node to SPItem */
        /* new_items.push_back(); */

        double orig_opacity = sp_repr_css_double_property(sp_repr_css_attr(SP_OBJECT_REPR(spitem), "style"), "opacity", 1.0);
        char opacity_string[64];
        g_ascii_formatd(opacity_string, sizeof(opacity_string), "%f",
                        orig_opacity / (steps));

        for (int i = 0; i < steps; i++) {
            double offset = (width / (float)(steps - 1) * (float)i) - (width / 2.0);

            new_items[i] = (SP_OBJECT_REPR(spitem))->duplicate();

            SPCSSAttr * css = sp_repr_css_attr(new_items[i], "style");
            sp_repr_css_set_property(css, "opacity", opacity_string);
            sp_repr_css_change(new_items[i], css, "style");

            new_group->appendChild(new_items[i]);
            selection->add(new_items[i]);
            sp_selected_path_to_curves();

            if (offset < 0.0) {
                /* Doing an inset here folks */
                offset *= -1.0;
                prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
                sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_INSET)->get_action(document), NULL);
            } else if (offset == 0.0) {
            } else {
                prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
                sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_OFFSET)->get_action(document), NULL);
            }

            selection->clear();
        }

    }

    prefs_set_double_attribute("options.defaultoffsetwidth", "value", old_offset);

    selection->clear();
    selection->add(items.begin(), items.end());
    selection->add(new_items.begin(), new_items.end());

    return;
}

Gtk::Widget *
BlurEdge::prefs_effect(Inkscape::Extension::Effect * module, Inkscape::UI::View::View * view)
{
    return module->autogui();
}

void
BlurEdge::init (void)
{
    Inkscape::Extension::build_from_mem(
        "<inkscape-extension>\n"
            "<name>Blur Edge</name>\n"
            "<id>org.inkscape.effect.bluredge</id>\n"
            "<param name=\"blur-width\" gui-text=\"Blur Width\" type=\"float\" min=\"1.0\" max=\"50.0\">1.0</param>\n"
            "<param name=\"num-steps\" gui-text=\"Number of Steps\" type=\"int\" min=\"5\" max=\"100\">11</param>\n"
            "<effect>\n"
                "<object-type>all</object-type>\n"
            "</effect>\n"
        "</inkscape-extension>\n" , new BlurEdge());
    return;
}

}; /* namespace Internal */
}; /* namespace Extension */
}; /* namespace Inkscape */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :