1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** \file
3  * SVG <feSpecularLighting> implementation.
4  *
5  */
6 /*
7  * Authors:
8  *   hugo Rodrigues <haa.rodrigues@gmail.com>
9  *   Jean-Rene Reinhard <jr@komite.net>
10  *   Abhishek Sharma
11  *
12  * Copyright (C) 2006 Hugo Rodrigues
13  *               2007 authors
14  *
15  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
16  */
17 
18 // Same directory
19 #include "specularlighting.h"
20 #include "distantlight.h"
21 #include "pointlight.h"
22 #include "spotlight.h"
23 
24 #include "attributes.h"
25 #include "strneq.h"
26 
27 #include "display/nr-filter.h"
28 #include "display/nr-filter-specularlighting.h"
29 
30 #include "object/sp-object.h"
31 
32 #include "svg/svg.h"
33 #include "svg/svg-color.h"
34 #include "svg/svg-icc-color.h"
35 
36 #include "xml/repr.h"
37 
38 /* FeSpecularLighting base class */
39 static void sp_feSpecularLighting_children_modified(SPFeSpecularLighting *sp_specularlighting);
40 
SPFeSpecularLighting()41 SPFeSpecularLighting::SPFeSpecularLighting() : SPFilterPrimitive() {
42     this->surfaceScale = 1;
43     this->specularConstant = 1;
44     this->specularExponent = 1;
45     this->lighting_color = 0xffffffff;
46     this->icc = nullptr;
47 
48     //TODO kernelUnit
49     this->renderer = nullptr;
50 
51     this->surfaceScale_set = FALSE;
52     this->specularConstant_set = FALSE;
53     this->specularExponent_set = FALSE;
54     this->lighting_color_set = FALSE;
55 }
56 
57 SPFeSpecularLighting::~SPFeSpecularLighting() = default;
58 
59 /**
60  * Reads the Inkscape::XML::Node, and initializes SPFeSpecularLighting variables.  For this to get called,
61  * our name must be associated with a repr via "sp_object_type_register".  Best done through
62  * sp-object-repr.cpp's repr_name_entries array.
63  */
build(SPDocument * document,Inkscape::XML::Node * repr)64 void SPFeSpecularLighting::build(SPDocument *document, Inkscape::XML::Node *repr) {
65 	SPFilterPrimitive::build(document, repr);
66 
67 	/*LOAD ATTRIBUTES FROM REPR HERE*/
68 	this->readAttr(SPAttr::SURFACESCALE);
69 	this->readAttr(SPAttr::SPECULARCONSTANT);
70 	this->readAttr(SPAttr::SPECULAREXPONENT);
71 	this->readAttr(SPAttr::KERNELUNITLENGTH);
72 	this->readAttr(SPAttr::LIGHTING_COLOR);
73 }
74 
75 /**
76  * Drops any allocated memory.
77  */
release()78 void SPFeSpecularLighting::release() {
79 	SPFilterPrimitive::release();
80 }
81 
82 /**
83  * Sets a specific value in the SPFeSpecularLighting.
84  */
set(SPAttr key,gchar const * value)85 void SPFeSpecularLighting::set(SPAttr key, gchar const *value) {
86     gchar const *cend_ptr = nullptr;
87     gchar *end_ptr = nullptr;
88 
89     switch(key) {
90 	/*DEAL WITH SETTING ATTRIBUTES HERE*/
91 //TODO test forbidden values
92         case SPAttr::SURFACESCALE:
93             end_ptr = nullptr;
94             if (value) {
95                 this->surfaceScale = g_ascii_strtod(value, &end_ptr);
96                 if (end_ptr) {
97                     this->surfaceScale_set = TRUE;
98                 } else {
99                     g_warning("this: surfaceScale should be a number ... defaulting to 1");
100                 }
101 
102             }
103             //if the attribute is not set or has an unreadable value
104             if (!value || !end_ptr) {
105                 this->surfaceScale = 1;
106                 this->surfaceScale_set = FALSE;
107             }
108             if (this->renderer) {
109                 this->renderer->surfaceScale = this->surfaceScale;
110             }
111             this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
112             break;
113         case SPAttr::SPECULARCONSTANT:
114             end_ptr = nullptr;
115             if (value) {
116                 this->specularConstant = g_ascii_strtod(value, &end_ptr);
117                 if (end_ptr && this->specularConstant >= 0) {
118                     this->specularConstant_set = TRUE;
119                 } else {
120                     end_ptr = nullptr;
121                     g_warning("this: specularConstant should be a positive number ... defaulting to 1");
122                 }
123             }
124             if (!value || !end_ptr) {
125                 this->specularConstant = 1;
126                 this->specularConstant_set = FALSE;
127             }
128             if (this->renderer) {
129                 this->renderer->specularConstant = this->specularConstant;
130             }
131             this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
132             break;
133         case SPAttr::SPECULAREXPONENT:
134             end_ptr = nullptr;
135             if (value) {
136                 this->specularExponent = g_ascii_strtod(value, &end_ptr);
137                 if (this->specularExponent >= 1 && this->specularExponent <= 128) {
138                     this->specularExponent_set = TRUE;
139                 } else {
140                     end_ptr = nullptr;
141                     g_warning("this: specularExponent should be a number in range [1, 128] ... defaulting to 1");
142                 }
143             }
144             if (!value || !end_ptr) {
145                 this->specularExponent = 1;
146                 this->specularExponent_set = FALSE;
147             }
148             if (this->renderer) {
149                 this->renderer->specularExponent = this->specularExponent;
150             }
151             this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
152             break;
153         case SPAttr::KERNELUNITLENGTH:
154             //TODO kernelUnit
155             //this->kernelUnitLength.set(value);
156             /*TODOif (feSpecularLighting->renderer) {
157                 feSpecularLighting->renderer->surfaceScale = feSpecularLighting->renderer;
158             }
159             */
160             this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
161             break;
162         case SPAttr::LIGHTING_COLOR:
163             cend_ptr = nullptr;
164             this->lighting_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
165             //if a value was read
166             if (cend_ptr) {
167                 while (g_ascii_isspace(*cend_ptr)) {
168                     ++cend_ptr;
169                 }
170                 if (strneq(cend_ptr, "icc-color(", 10)) {
171                     if (!this->icc) this->icc = new SVGICCColor();
172                     if ( ! sp_svg_read_icc_color( cend_ptr, this->icc ) ) {
173                         delete this->icc;
174                         this->icc = nullptr;
175                     }
176                 }
177                 this->lighting_color_set = TRUE;
178             } else {
179                 //lighting_color already contains the default value
180                 this->lighting_color_set = FALSE;
181             }
182             if (this->renderer) {
183                 this->renderer->lighting_color = this->lighting_color;
184             }
185             this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
186             break;
187         default:
188         	SPFilterPrimitive::set(key, value);
189             break;
190     }
191 }
192 
193 /**
194  * Receives update notifications.
195  */
update(SPCtx * ctx,guint flags)196 void SPFeSpecularLighting::update(SPCtx *ctx, guint flags) {
197     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
198         this->readAttr(SPAttr::SURFACESCALE);
199         this->readAttr(SPAttr::SPECULARCONSTANT);
200         this->readAttr(SPAttr::SPECULAREXPONENT);
201         this->readAttr(SPAttr::KERNELUNITLENGTH);
202         this->readAttr(SPAttr::LIGHTING_COLOR);
203     }
204 
205     SPFilterPrimitive::update(ctx, flags);
206 }
207 
208 /**
209  * Writes its settings to an incoming repr object, if any.
210  */
write(Inkscape::XML::Document * doc,Inkscape::XML::Node * repr,guint flags)211 Inkscape::XML::Node* SPFeSpecularLighting::write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) {
212     /* TODO: Don't just clone, but create a new repr node and write all
213      * relevant values _and children_ into it */
214     if (!repr) {
215         repr = this->getRepr()->duplicate(doc);
216         //repr = doc->createElement("svg:feSpecularLighting");
217     }
218 
219     if (this->surfaceScale_set) {
220         sp_repr_set_css_double(repr, "surfaceScale", this->surfaceScale);
221     }
222 
223     if (this->specularConstant_set) {
224         sp_repr_set_css_double(repr, "specularConstant", this->specularConstant);
225     }
226 
227     if (this->specularExponent_set) {
228         sp_repr_set_css_double(repr, "specularExponent", this->specularExponent);
229     }
230 
231     /*TODO kernelUnits */
232     if (this->lighting_color_set) {
233         gchar c[64];
234         sp_svg_write_color(c, sizeof(c), this->lighting_color);
235         repr->setAttribute("lighting-color", c);
236     }
237 
238     SPFilterPrimitive::write(doc, repr, flags);
239 
240     return repr;
241 }
242 
243 /**
244  * Callback for child_added event.
245  */
child_added(Inkscape::XML::Node * child,Inkscape::XML::Node * ref)246 void SPFeSpecularLighting::child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) {
247     SPFilterPrimitive::child_added(child, ref);
248 
249     sp_feSpecularLighting_children_modified(this);
250     this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
251 }
252 
253 /**
254  * Callback for remove_child event.
255  */
remove_child(Inkscape::XML::Node * child)256 void SPFeSpecularLighting::remove_child(Inkscape::XML::Node *child) {
257     SPFilterPrimitive::remove_child(child);
258 
259     sp_feSpecularLighting_children_modified(this);
260     this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
261 }
262 
order_changed(Inkscape::XML::Node * child,Inkscape::XML::Node * old_ref,Inkscape::XML::Node * new_ref)263 void SPFeSpecularLighting::order_changed(Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref) {
264     SPFilterPrimitive::order_changed(child, old_ref, new_ref);
265 
266     sp_feSpecularLighting_children_modified(this);
267     this->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
268 }
269 
sp_feSpecularLighting_children_modified(SPFeSpecularLighting * sp_specularlighting)270 static void sp_feSpecularLighting_children_modified(SPFeSpecularLighting *sp_specularlighting) {
271 	if (sp_specularlighting->renderer) {
272         sp_specularlighting->renderer->light_type = Inkscape::Filters::NO_LIGHT;
273 
274         if (SP_IS_FEDISTANTLIGHT(sp_specularlighting->firstChild())) {
275             sp_specularlighting->renderer->light_type = Inkscape::Filters::DISTANT_LIGHT;
276             sp_specularlighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_specularlighting->firstChild());
277         }
278 
279         if (SP_IS_FEPOINTLIGHT(sp_specularlighting->firstChild())) {
280             sp_specularlighting->renderer->light_type = Inkscape::Filters::POINT_LIGHT;
281             sp_specularlighting->renderer->light.point = SP_FEPOINTLIGHT(sp_specularlighting->firstChild());
282         }
283 
284         if (SP_IS_FESPOTLIGHT(sp_specularlighting->firstChild())) {
285             sp_specularlighting->renderer->light_type = Inkscape::Filters::SPOT_LIGHT;
286             sp_specularlighting->renderer->light.spot = SP_FESPOTLIGHT(sp_specularlighting->firstChild());
287         }
288 	}
289 }
290 
build_renderer(Inkscape::Filters::Filter * filter)291 void SPFeSpecularLighting::build_renderer(Inkscape::Filters::Filter* filter) {
292     g_assert(filter != nullptr);
293 
294     int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_SPECULARLIGHTING);
295     Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
296     Inkscape::Filters::FilterSpecularLighting *nr_specularlighting = dynamic_cast<Inkscape::Filters::FilterSpecularLighting*>(nr_primitive);
297     g_assert(nr_specularlighting != nullptr);
298 
299     this->renderer = nr_specularlighting;
300     this->renderer_common(nr_primitive);
301 
302     nr_specularlighting->specularConstant = this->specularConstant;
303     nr_specularlighting->specularExponent = this->specularExponent;
304     nr_specularlighting->surfaceScale = this->surfaceScale;
305     nr_specularlighting->lighting_color = this->lighting_color;
306     nr_specularlighting->set_icc(this->icc);
307 
308     //We assume there is at most one child
309     nr_specularlighting->light_type = Inkscape::Filters::NO_LIGHT;
310 
311     if (SP_IS_FEDISTANTLIGHT(this->firstChild())) {
312         nr_specularlighting->light_type = Inkscape::Filters::DISTANT_LIGHT;
313         nr_specularlighting->light.distant = SP_FEDISTANTLIGHT(this->firstChild());
314     }
315 
316     if (SP_IS_FEPOINTLIGHT(this->firstChild())) {
317         nr_specularlighting->light_type = Inkscape::Filters::POINT_LIGHT;
318         nr_specularlighting->light.point = SP_FEPOINTLIGHT(this->firstChild());
319     }
320 
321     if (SP_IS_FESPOTLIGHT(this->firstChild())) {
322         nr_specularlighting->light_type = Inkscape::Filters::SPOT_LIGHT;
323         nr_specularlighting->light.spot = SP_FESPOTLIGHT(this->firstChild());
324     }
325 
326     //nr_offset->set_dx(sp_offset->dx);
327     //nr_offset->set_dy(sp_offset->dy);
328 }
329 
330 
331 /*
332   Local Variables:
333   mode:c++
334   c-file-style:"stroustrup"
335   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
336   indent-tabs-mode:nil
337   fill-column:99
338   End:
339 */
340 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
341