summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/filter/filter.cpp
blob: c9779c78a326d30da5eecd87a0f5452dadc7dc48 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Ted Gould <ted@gould.cx>
 *
 * Copyright (C) 2008 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "desktop.h"
#include "selection.h"
#include "extension/extension.h"
#include "extension/effect.h"
#include "extension/system.h"
#include "xml/repr.h"
#include "xml/simple-node.h"
#include "xml/attribute-record.h"
#include "object/sp-defs.h"

#include "filter.h"

namespace Inkscape {
namespace Extension {
namespace Internal {
namespace Filter {

Filter::Filter() :
    Inkscape::Extension::Implementation::Implementation(),
    _filter(nullptr) {
    return;
}

Filter::Filter(gchar const * filter) :
    Inkscape::Extension::Implementation::Implementation(),
    _filter(filter) {
    return;
}

Filter::~Filter () {
    if (_filter != nullptr) {
        _filter = nullptr;
    }

    return;
}

bool Filter::load(Inkscape::Extension::Extension * /*module*/)
{
    return true;
}

Inkscape::Extension::Implementation::ImplementationDocumentCache *Filter::newDocCache(Inkscape::Extension::Extension * /*ext*/,
										      Inkscape::UI::View::View * /*doc*/)
{
    return nullptr;
}

gchar const *Filter::get_filter_text(Inkscape::Extension::Extension * /*ext*/)
{
    return _filter;
}

Inkscape::XML::Document *
Filter::get_filter (Inkscape::Extension::Extension * ext) {
    gchar const * filter = get_filter_text(ext);
    return sp_repr_read_mem(filter, strlen(filter), nullptr);
}

void
Filter::merge_filters( Inkscape::XML::Node * to, Inkscape::XML::Node * from,
		       Inkscape::XML::Document * doc,
		       gchar const * srcGraphic, gchar const * srcGraphicAlpha)
{
    if (from == nullptr) return;

    // copy attributes
    for ( const auto & iter : from->attributeList()) {
        gchar const * attr = g_quark_to_string(iter.key);
        //printf("Attribute List: %s\n", attr);
        if (!strcmp(attr, "id")) continue; // nope, don't copy that one!
        to->setAttribute(attr, from->attribute(attr));

        if (!strcmp(attr, "in") || !strcmp(attr, "in2") || !strcmp(attr, "in3")) {
            if (srcGraphic != nullptr && !strcmp(from->attribute(attr), "SourceGraphic")) {
                to->setAttribute(attr, srcGraphic);
            }

            if (srcGraphicAlpha != nullptr && !strcmp(from->attribute(attr), "SourceAlpha")) {
                to->setAttribute(attr, srcGraphicAlpha);
            }
        }
    }

    // for each child call recursively
    for (Inkscape::XML::Node * from_child = from->firstChild();
         from_child != nullptr ; from_child = from_child->next()) {
        Glib::ustring name = "svg:";
        name += from_child->name();

        Inkscape::XML::Node * to_child = doc->createElement(name.c_str());
        to->appendChild(to_child);
        merge_filters(to_child, from_child, doc, srcGraphic, srcGraphicAlpha);

        if (from_child == from->firstChild() && !strcmp("filter", from->name()) && srcGraphic != nullptr && to_child->attribute("in") == nullptr) {
            to_child->setAttribute("in", srcGraphic);
        }
        Inkscape::GC::release(to_child);
    }
}

#define FILTER_SRC_GRAPHIC       "fbSourceGraphic"
#define FILTER_SRC_GRAPHIC_ALPHA "fbSourceGraphicAlpha"

void Filter::effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document,
                    Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
{
    Inkscape::XML::Document *filterdoc = get_filter(module);
    if (filterdoc == nullptr) {
        return; // could not parse the XML source of the filter; typically parser will stderr a warning
    }

    //printf("Calling filter effect\n");
    Inkscape::Selection * selection = ((SPDesktop *)document)->selection;

    // TODO need to properly refcount the items, at least
    std::vector<SPItem*> items(selection->items().begin(), selection->items().end());

    Inkscape::XML::Document * xmldoc = document->doc()->getReprDoc();
    Inkscape::XML::Node * defsrepr = document->doc()->getDefs()->getRepr();

    for(auto spitem : items) {
        Inkscape::XML::Node *node = spitem->getRepr();

        SPCSSAttr * css = sp_repr_css_attr(node, "style");
        gchar const * filter = sp_repr_css_property(css, "filter", nullptr);

        if (filter == nullptr) {

            Inkscape::XML::Node * newfilterroot = xmldoc->createElement("svg:filter");
            merge_filters(newfilterroot, filterdoc->root(), xmldoc);
            defsrepr->appendChild(newfilterroot);
            document->doc()->resources_changed_signals[g_quark_from_string("filter")].emit();

            Glib::ustring url = "url(#"; url += newfilterroot->attribute("id"); url += ")";


            Inkscape::GC::release(newfilterroot);

            sp_repr_css_set_property(css, "filter", url.c_str());
            sp_repr_css_set(node, css, "style");
        } else {
            if (strncmp(filter, "url(#", strlen("url(#")) || filter[strlen(filter) - 1] != ')') {
                // This is not url(#id) -- we can't handle it
                continue;
            }

            gchar * lfilter = g_strndup(filter + 5, strlen(filter) - 6);
            Inkscape::XML::Node * filternode = nullptr;
            for (Inkscape::XML::Node * child = defsrepr->firstChild(); child != nullptr; child = child->next()) {
                if (!strcmp(lfilter, child->attribute("id"))) {
                    filternode = child;
                    break;
                }
            }
            g_free(lfilter);

            // no filter
            if (filternode == nullptr) {
                g_warning("no assigned filter found!");
                continue;
            }

            if (filternode->lastChild() == nullptr) {
                // empty filter, we insert
                merge_filters(filternode, filterdoc->root(), xmldoc);
            } else {
                // existing filter, we merge
                filternode->lastChild()->setAttribute("result", FILTER_SRC_GRAPHIC);
                Inkscape::XML::Node * alpha = xmldoc->createElement("svg:feColorMatrix");
                alpha->setAttribute("result", FILTER_SRC_GRAPHIC_ALPHA);
                alpha->setAttribute("in", FILTER_SRC_GRAPHIC); // not required, but we're being explicit
                alpha->setAttribute("values", "0 0 0 -1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1 0");

                filternode->appendChild(alpha);

                merge_filters(filternode, filterdoc->root(), xmldoc, FILTER_SRC_GRAPHIC, FILTER_SRC_GRAPHIC_ALPHA);

                Inkscape::GC::release(alpha);
            }
        }
    }

    return;
}

#include "extension/internal/clear-n_.h"

void
Filter::filter_init (gchar const * id, gchar const * name, gchar const * submenu, gchar const * tip, gchar const * filter)
{
    // clang-format off
    gchar * xml_str = g_strdup_printf(
        "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
        "<name>%s</name>\n"
        "<id>org.inkscape.effect.filter.%s</id>\n"
        "<effect>\n"
        "<object-type>all</object-type>\n"
        "<effects-menu>\n"
        "<submenu name=\"" N_("Filters") "\" />\n"
        "<submenu name=\"%s\"/>\n"
        "</effects-menu>\n"
        "<menu-tip>%s</menu-tip>\n"
        "</effect>\n"
        "</inkscape-extension>\n", name, id, submenu, tip);
    // clang-format on
    Inkscape::Extension::build_from_mem(xml_str, new Filter(filter));
    g_free(xml_str);
    return;
}

}; /* namespace Filter */
}; /* 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:fileencoding=utf-8:textwidth=99 :