1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** \file
3  * Superclass for all the filter primitives
4  *
5  */
6 /*
7  * Authors:
8  *   Kees Cook <kees@outflux.net>
9  *   Niko Kiirala <niko@kiirala.com>
10  *   Abhishek Sharma
11  *
12  * Copyright (C) 2004-2007 Authors
13  *
14  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15  */
16 
17 #include <cstring>
18 
19 #include "sp-filter-primitive.h"
20 
21 #include "attributes.h"
22 
23 #include "display/nr-filter-primitive.h"
24 
25 #include "style.h"
26 
27 
28 // CPPIFY: Make pure virtual.
29 //void SPFilterPrimitive::build_renderer(Inkscape::Filters::Filter* filter) {
30 // throw;
31 //}
32 
SPFilterPrimitive()33 SPFilterPrimitive::SPFilterPrimitive() : SPObject() {
34     this->image_in = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
35     this->image_out = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
36 
37     // We must keep track if a value is set or not, if not set then the region defaults to 0%, 0%,
38     // 100%, 100% ("x", "y", "width", "height") of the -> filter <- region.  If set then
39     // percentages are in terms of bounding box or viewbox, depending on value of "primitiveUnits"
40 
41     // NB: SVGLength.set takes prescaled percent values: 1 means 100%
42     this->x.unset(SVGLength::PERCENT, 0, 0);
43     this->y.unset(SVGLength::PERCENT, 0, 0);
44     this->width.unset(SVGLength::PERCENT, 1, 0);
45     this->height.unset(SVGLength::PERCENT, 1, 0);
46 }
47 
48 SPFilterPrimitive::~SPFilterPrimitive() = default;
49 
50 /**
51  * Reads the Inkscape::XML::Node, and initializes SPFilterPrimitive variables.  For this to get called,
52  * our name must be associated with a repr via "sp_object_type_register".  Best done through
53  * sp-object-repr.cpp's repr_name_entries array.
54  */
build(SPDocument * document,Inkscape::XML::Node * repr)55 void SPFilterPrimitive::build(SPDocument *document, Inkscape::XML::Node *repr) {
56     SPFilterPrimitive* object = this;
57 
58     object->readAttr(SPAttr::STYLE); // struct not derived from SPItem, we need to do this ourselves.
59     object->readAttr(SPAttr::IN_);
60     object->readAttr(SPAttr::RESULT);
61     object->readAttr(SPAttr::X);
62     object->readAttr(SPAttr::Y);
63     object->readAttr(SPAttr::WIDTH);
64     object->readAttr(SPAttr::HEIGHT);
65 
66     SPObject::build(document, repr);
67 }
68 
69 /**
70  * Drops any allocated memory.
71  */
release()72 void SPFilterPrimitive::release() {
73     SPObject::release();
74 }
75 
76 /**
77  * Sets a specific value in the SPFilterPrimitive.
78  */
set(SPAttr key,gchar const * value)79 void SPFilterPrimitive::set(SPAttr key, gchar const *value) {
80 
81     int image_nr;
82     switch (key) {
83         case SPAttr::IN_:
84             if (value) {
85                 image_nr = this->read_in(value);
86             } else {
87                 image_nr = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
88             }
89             if (image_nr != this->image_in) {
90                 this->image_in = image_nr;
91                 this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
92             }
93             break;
94         case SPAttr::RESULT:
95             if (value) {
96                 image_nr = this->read_result(value);
97             } else {
98                 image_nr = Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
99             }
100             if (image_nr != this->image_out) {
101                 this->image_out = image_nr;
102                 this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
103             }
104             break;
105 
106         /* Filter primitive sub-region */
107         case SPAttr::X:
108             this->x.readOrUnset(value);
109             this->requestModified(SP_OBJECT_MODIFIED_FLAG);
110             break;
111         case SPAttr::Y:
112             this->y.readOrUnset(value);
113             this->requestModified(SP_OBJECT_MODIFIED_FLAG);
114             break;
115         case SPAttr::WIDTH:
116             this->width.readOrUnset(value);
117             this->requestModified(SP_OBJECT_MODIFIED_FLAG);
118             break;
119         case SPAttr::HEIGHT:
120             this->height.readOrUnset(value);
121             this->requestModified(SP_OBJECT_MODIFIED_FLAG);
122             break;
123     }
124 
125     /* See if any parents need this value. */
126     SPObject::set(key, value);
127 }
128 
129 /**
130  * Receives update notifications.
131  */
update(SPCtx * ctx,guint flags)132 void SPFilterPrimitive::update(SPCtx *ctx, guint flags) {
133 
134     SPItemCtx *ictx = (SPItemCtx *) ctx;
135 
136     // Do here since we know viewport (Bounding box case handled during rendering)
137     SPFilter *parent = SP_FILTER(this->parent);
138 
139     if( parent->primitiveUnits == SP_FILTER_UNITS_USERSPACEONUSE ) {
140         this->calcDimsFromParentViewport(ictx, true);
141     }
142 
143     SPObject::update(ctx, flags);
144 }
145 
146 /**
147  * Writes its settings to an incoming repr object, if any.
148  */
write(Inkscape::XML::Document * doc,Inkscape::XML::Node * repr,guint flags)149 Inkscape::XML::Node* SPFilterPrimitive::write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) {
150     SPFilterPrimitive* object = this;
151 
152     SPFilterPrimitive *prim = SP_FILTER_PRIMITIVE(object);
153     SPFilter *parent = SP_FILTER(object->parent);
154 
155     if (!repr) {
156         repr = object->getRepr()->duplicate(doc);
157     }
158 
159     gchar const *in_name = parent->name_for_image(prim->image_in);
160     repr->setAttribute("in", in_name);
161 
162     gchar const *out_name = parent->name_for_image(prim->image_out);
163     repr->setAttribute("result", out_name);
164 
165     /* Do we need to add x,y,width,height? */
166     SPObject::write(doc, repr, flags);
167 
168     return repr;
169 }
170 
read_in(gchar const * name)171 int SPFilterPrimitive::read_in(gchar const *name)
172 {
173     if (!name){
174         return Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
175     }
176     // TODO: are these case sensitive or not? (assumed yes)
177     switch (name[0]) {
178         case 'S':
179             if (strcmp(name, "SourceGraphic") == 0)
180                 return Inkscape::Filters::NR_FILTER_SOURCEGRAPHIC;
181             if (strcmp(name, "SourceAlpha") == 0)
182                 return Inkscape::Filters::NR_FILTER_SOURCEALPHA;
183             if (strcmp(name, "StrokePaint") == 0)
184                 return Inkscape::Filters::NR_FILTER_STROKEPAINT;
185             break;
186         case 'B':
187             if (strcmp(name, "BackgroundImage") == 0)
188                 return Inkscape::Filters::NR_FILTER_BACKGROUNDIMAGE;
189             if (strcmp(name, "BackgroundAlpha") == 0)
190                 return Inkscape::Filters::NR_FILTER_BACKGROUNDALPHA;
191             break;
192         case 'F':
193             if (strcmp(name, "FillPaint") == 0)
194                 return Inkscape::Filters::NR_FILTER_FILLPAINT;
195             break;
196     }
197 
198     SPFilter *parent = SP_FILTER(this->parent);
199     int ret = parent->get_image_name(name);
200     if (ret >= 0) return ret;
201 
202     return Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
203 }
204 
read_result(gchar const * name)205 int SPFilterPrimitive::read_result(gchar const *name)
206 {
207     SPFilter *parent = SP_FILTER(this->parent);
208     int ret = parent->get_image_name(name);
209     if (ret >= 0) return ret;
210 
211     ret = parent->set_image_name(name);
212     if (ret >= 0) return ret;
213 
214     return Inkscape::Filters::NR_FILTER_SLOT_NOT_SET;
215 }
216 
217 /**
218  * Gives name for output of previous filter. Makes things clearer when 'this'
219  * is a filter with two or more inputs. Returns the slot number of result
220  * of previous primitive, or NR_FILTER_SOURCEGRAPHIC if this is the first
221  * primitive.
222  */
name_previous_out()223 int SPFilterPrimitive::name_previous_out() {
224     SPFilter *parent = SP_FILTER(this->parent);
225     SPObject *i = parent->firstChild();
226     while (i && i->getNext() != this) {
227         i = i->getNext();
228     }
229     if (i) {
230         SPFilterPrimitive *i_prim = SP_FILTER_PRIMITIVE(i);
231         if (i_prim->image_out < 0) {
232             Glib::ustring name = parent->get_new_result_name();
233             int slot = parent->set_image_name(name.c_str());
234             i_prim->image_out = slot;
235             //XML Tree is being directly used while it shouldn't be.
236             i_prim->setAttributeOrRemoveIfEmpty("result", name);
237             return slot;
238         } else {
239             return i_prim->image_out;
240         }
241     }
242     return Inkscape::Filters::NR_FILTER_SOURCEGRAPHIC;
243 }
244 
245 /* Common initialization for filter primitives */
renderer_common(Inkscape::Filters::FilterPrimitive * nr_prim)246 void SPFilterPrimitive::renderer_common(Inkscape::Filters::FilterPrimitive *nr_prim)
247 {
248     g_assert(nr_prim != nullptr);
249 
250 
251     nr_prim->set_input(this->image_in);
252     nr_prim->set_output(this->image_out);
253 
254     /* TODO: place here code to handle input images, filter area etc. */
255     // We don't know current viewport or bounding box, this is wrong approach.
256     nr_prim->set_subregion( this->x, this->y, this->width, this->height );
257 
258     // Give renderer access to filter properties
259     nr_prim->setStyle( this->style );
260 }
261 
262 /* Calculate the region taken up by this filter, given the previous region.
263  *
264  * @param current_region The original shape's region or previous primitive's
265  *                       calcualte_region output.
266  */
calculate_region(Geom::Rect region)267 Geom::Rect SPFilterPrimitive::calculate_region(Geom::Rect region)
268 {
269     return region; // No change.
270 }
271 
272 
273 /*
274   Local Variables:
275   mode:c++
276   c-file-style:"stroustrup"
277   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
278   indent-tabs-mode:nil
279   fill-column:99
280   End:
281 */
282 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
283