summaryrefslogtreecommitdiffstats
path: root/src/object/filters/image.cpp
blob: 8eecbd9f99179c5d2abcc4b93cc55aacf3f5a43d (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// SPDX-License-Identifier: GPL-2.0-or-later
/** \file
 * SVG <feImage> implementation.
 *
 */
/*
 * Authors:
 *   Felipe Corrêa da Silva Sanches <juca@members.fsf.org>
 *   hugo Rodrigues <haa.rodrigues@gmail.com>
 *   Abhishek Sharma
 *
 * Copyright (C) 2007 Felipe Sanches
 * Copyright (C) 2006 Hugo Rodrigues
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "image.h"

#include <sigc++/bind.h>

#include "attributes.h"
#include "enums.h"

#include "bad-uri-exception.h"

#include "object/sp-image.h"
#include "object/uri.h"
#include "object/uri-references.h"

#include "display/nr-filter-image.h"
#include "display/nr-filter.h"

#include "xml/repr.h"


SPFeImage::SPFeImage() : SPFilterPrimitive() {
	this->href = nullptr;
	this->from_element = false;
	this->SVGElemRef = nullptr;
	this->SVGElem = nullptr;

    this->aspect_align = SP_ASPECT_XMID_YMID; // Default
    this->aspect_clip = SP_ASPECT_MEET; // Default
}

SPFeImage::~SPFeImage() = default;

/**
 * Reads the Inkscape::XML::Node, and initializes SPFeImage variables.  For this to get called,
 * our name must be associated with a repr via "sp_object_type_register".  Best done through
 * sp-object-repr.cpp's repr_name_entries array.
 */
void SPFeImage::build(SPDocument *document, Inkscape::XML::Node *repr)
{
    SPFilterPrimitive::build(document, repr);

    /*LOAD ATTRIBUTES FROM REPR HERE*/

    this->readAttr(SPAttr::PRESERVEASPECTRATIO);
    this->readAttr(SPAttr::XLINK_HREF);
}

/**
 * Drops any allocated memory.
 */
void SPFeImage::release() {
    this->_image_modified_connection.disconnect();
    this->_href_modified_connection.disconnect();

    if (this->SVGElemRef) {
    	delete this->SVGElemRef;
    }

    SPFilterPrimitive::release();
}

static void sp_feImage_elem_modified(SPObject* /*href*/, guint /*flags*/, SPObject* obj)
{
    obj->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
}

static void sp_feImage_href_modified(SPObject* /*old_elem*/, SPObject* new_elem, SPObject* obj)
{
    SPFeImage *feImage = SP_FEIMAGE(obj);
    feImage->_image_modified_connection.disconnect();
    if (new_elem) {
        feImage->SVGElem = SP_ITEM(new_elem);
        feImage->_image_modified_connection = ((SPObject*) feImage->SVGElem)->connectModified(sigc::bind(sigc::ptr_fun(&sp_feImage_elem_modified), obj));
    } else {
        feImage->SVGElem = nullptr;
    }

    obj->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
}

/**
 * Sets a specific value in the SPFeImage.
 */
void SPFeImage::set(SPAttr key, gchar const *value) {
    switch(key) {
    /*DEAL WITH SETTING ATTRIBUTES HERE*/
        case SPAttr::XLINK_HREF:
            if (this->href) {
                g_free(this->href);
            }
            this->href = (value) ? g_strdup (value) : nullptr;
            if (!this->href) return;
            delete this->SVGElemRef;
            this->SVGElemRef = nullptr;
            this->SVGElem = nullptr;
            this->_image_modified_connection.disconnect();
            this->_href_modified_connection.disconnect();
            try{
                Inkscape::URI SVGElem_uri(this->href);
                this->SVGElemRef = new Inkscape::URIReference(this->document);
                this->SVGElemRef->attach(SVGElem_uri);
                this->from_element = true;
                this->_href_modified_connection = this->SVGElemRef->changedSignal().connect(sigc::bind(sigc::ptr_fun(&sp_feImage_href_modified), this));
                if (SPObject *elemref = this->SVGElemRef->getObject()) {
                    this->SVGElem = SP_ITEM(elemref);
                    this->_image_modified_connection = ((SPObject*) this->SVGElem)->connectModified(sigc::bind(sigc::ptr_fun(&sp_feImage_elem_modified), this));
                    this->requestModified(SP_OBJECT_MODIFIED_FLAG);
                    break;
                } else {
                    g_warning("SVG element URI was not found in the document while loading this: %s", value);
                }
            }
            // catches either MalformedURIException or UnsupportedURIException
            catch(const Inkscape::BadURIException & e)
            {
                this->from_element = false;
                /* This occurs when using external image as the source */
                //g_warning("caught Inkscape::BadURIException in sp_feImage_set");
                break;
            }
            break;

        case SPAttr::PRESERVEASPECTRATIO:
            /* Copied from sp-image.cpp */
            /* Do setup before, so we can use break to escape */
            this->aspect_align = SP_ASPECT_XMID_YMID; // Default
            this->aspect_clip = SP_ASPECT_MEET; // Default
            this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
            if (value) {
                int len;
                gchar c[256];
                const gchar *p, *e;
                unsigned int align, clip;
                p = value;
                while (*p && *p == 32) p += 1;
                if (!*p) break;
                e = p;
                while (*e && *e != 32) e += 1;
                len = e - p;
                if (len > 8) break;
                memcpy (c, value, len);
                c[len] = 0;
                /* Now the actual part */
                if (!strcmp (c, "none")) {
                    align = SP_ASPECT_NONE;
                } else if (!strcmp (c, "xMinYMin")) {
                    align = SP_ASPECT_XMIN_YMIN;
                } else if (!strcmp (c, "xMidYMin")) {
                    align = SP_ASPECT_XMID_YMIN;
                } else if (!strcmp (c, "xMaxYMin")) {
                    align = SP_ASPECT_XMAX_YMIN;
                } else if (!strcmp (c, "xMinYMid")) {
                    align = SP_ASPECT_XMIN_YMID;
                } else if (!strcmp (c, "xMidYMid")) {
                    align = SP_ASPECT_XMID_YMID;
                } else if (!strcmp (c, "xMaxYMid")) {
                    align = SP_ASPECT_XMAX_YMID;
                } else if (!strcmp (c, "xMinYMax")) {
                    align = SP_ASPECT_XMIN_YMAX;
                } else if (!strcmp (c, "xMidYMax")) {
                    align = SP_ASPECT_XMID_YMAX;
                } else if (!strcmp (c, "xMaxYMax")) {
                    align = SP_ASPECT_XMAX_YMAX;
                } else {
                    g_warning("Illegal preserveAspectRatio: %s", c);
                    break;
                }
                clip = SP_ASPECT_MEET;
                while (*e && *e == 32) e += 1;
                if (*e) {
                    if (!strcmp (e, "meet")) {
                        clip = SP_ASPECT_MEET;
                    } else if (!strcmp (e, "slice")) {
                        clip = SP_ASPECT_SLICE;
                    } else {
                        break;
                    }
                }
                this->aspect_align = align;
                this->aspect_clip = clip;
            }
            break;

        default:
        	SPFilterPrimitive::set(key, value);
            break;
    }
}

/*
 * Check if the object is being used in the filter's definition
 * and returns true if it is being used (to avoid infinate loops)
 */
bool SPFeImage::valid_for(SPObject const *obj) const
{
    // SVGElem could be nullptr, but this should still work.
    return obj && SP_ITEM(obj) != SVGElem;
}

/**
 * Receives update notifications.
 */
void SPFeImage::update(SPCtx *ctx, guint flags) {
    if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
                 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {

        /* do something to trigger redisplay, updates? */
    }

    SPFilterPrimitive::update(ctx, flags);
}

/**
 * Writes its settings to an incoming repr object, if any.
 */
Inkscape::XML::Node* SPFeImage::write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) {
    /* TODO: Don't just clone, but create a new repr node and write all
     * relevant values into it */
    if (!repr) {
        repr = this->getRepr()->duplicate(doc);
    }

    SPFilterPrimitive::write(doc, repr, flags);

    return repr;
}

void SPFeImage::build_renderer(Inkscape::Filters::Filter* filter) {
    g_assert(filter != nullptr);

    int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_IMAGE);
    Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
    Inkscape::Filters::FilterImage *nr_image = dynamic_cast<Inkscape::Filters::FilterImage*>(nr_primitive);
    g_assert(nr_image != nullptr);

    this->renderer_common(nr_primitive);

    nr_image->from_element = this->from_element;
    nr_image->SVGElem = this->SVGElem;
    nr_image->set_align( this->aspect_align );
    nr_image->set_clip( this->aspect_clip );
    nr_image->set_href(this->href);
    nr_image->set_document(this->document);
}

/*
  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 :