Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / filters / specularlighting.cpp
1 /** \file
2  * SVG <feSpecularLighting> implementation.
3  *
4  */
5 /*
6  * Authors:
7  *   hugo Rodrigues <haa.rodrigues@gmail.com>
8  *   Jean-Rene Reinhard <jr@komite.net>
9  *   Abhishek Sharma
10  *
11  * Copyright (C) 2006 Hugo Rodrigues
12  *               2007 authors
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include "attributes.h"
22 #include "svg/svg.h"
23 #include "sp-object.h"
24 #include "svg/svg-color.h"
25 #include "specularlighting.h"
26 #include "xml/repr.h"
27 #include "display/nr-filter-specularlighting.h"
29 /* FeSpecularLighting base class */
31 static void sp_feSpecularLighting_class_init(SPFeSpecularLightingClass *klass);
32 static void sp_feSpecularLighting_init(SPFeSpecularLighting *feSpecularLighting);
34 static void sp_feSpecularLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
35 static void sp_feSpecularLighting_release(SPObject *object);
36 static void sp_feSpecularLighting_set(SPObject *object, unsigned int key, gchar const *value);
37 static void sp_feSpecularLighting_update(SPObject *object, SPCtx *ctx, guint flags);
38 //we assume that svg:feSpecularLighting can have any number of children
39 //only the first one is considered as the light source of the filter
40 //TODO is that right?
41 //if not modify child_added and remove_child to raise errors
42 static void sp_feSpecularLighting_child_added(SPObject *object,
43                                     Inkscape::XML::Node *child,
44                                     Inkscape::XML::Node *ref);
45 static void sp_feSpecularLighting_remove_child(SPObject *object, Inkscape::XML::Node *child);
46 static void sp_feSpecularLighting_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
47 static Inkscape::XML::Node *sp_feSpecularLighting_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
48 static void sp_feSpecularLighting_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
49 static void sp_feSpecularLighting_children_modified(SPFeSpecularLighting *sp_specularlighting);
51 static SPFilterPrimitiveClass *feSpecularLighting_parent_class;
53 GType
54 sp_feSpecularLighting_get_type()
55 {
56     static GType feSpecularLighting_type = 0;
58     if (!feSpecularLighting_type) {
59         GTypeInfo feSpecularLighting_info = {
60             sizeof(SPFeSpecularLightingClass),
61             NULL, NULL,
62             (GClassInitFunc) sp_feSpecularLighting_class_init,
63             NULL, NULL,
64             sizeof(SPFeSpecularLighting),
65             16,
66             (GInstanceInitFunc) sp_feSpecularLighting_init,
67             NULL,    /* value_table */
68         };
69         feSpecularLighting_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeSpecularLighting", &feSpecularLighting_info, (GTypeFlags)0);
70     }
71     return feSpecularLighting_type;
72 }
74 static void
75 sp_feSpecularLighting_class_init(SPFeSpecularLightingClass *klass)
76 {
77     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
78     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
79     feSpecularLighting_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
81     sp_object_class->build = sp_feSpecularLighting_build;
82     sp_object_class->release = sp_feSpecularLighting_release;
83     sp_object_class->write = sp_feSpecularLighting_write;
84     sp_object_class->set = sp_feSpecularLighting_set;
85     sp_object_class->update = sp_feSpecularLighting_update;
86     sp_object_class->child_added = sp_feSpecularLighting_child_added;
87     sp_object_class->remove_child = sp_feSpecularLighting_remove_child;
88     sp_object_class->order_changed = sp_feSpecularLighting_order_changed;
90     sp_primitive_class->build_renderer = sp_feSpecularLighting_build_renderer;
91 }
93 static void
94 sp_feSpecularLighting_init(SPFeSpecularLighting *feSpecularLighting)
95 {
96     feSpecularLighting->surfaceScale = 1;
97     feSpecularLighting->specularConstant = 1;
98     feSpecularLighting->specularExponent = 1;
99     feSpecularLighting->lighting_color = 0xffffffff;
100     //TODO kernelUnit
101     feSpecularLighting->renderer = NULL;
102     
103     feSpecularLighting->surfaceScale_set = FALSE;
104     feSpecularLighting->specularConstant_set = FALSE;
105     feSpecularLighting->specularExponent_set = FALSE;
106     feSpecularLighting->lighting_color_set = FALSE;
109 /**
110  * Reads the Inkscape::XML::Node, and initializes SPFeSpecularLighting variables.  For this to get called,
111  * our name must be associated with a repr via "sp_object_type_register".  Best done through
112  * sp-object-repr.cpp's repr_name_entries array.
113  */
114 static void
115 sp_feSpecularLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
117     if (((SPObjectClass *) feSpecularLighting_parent_class)->build) {
118         ((SPObjectClass *) feSpecularLighting_parent_class)->build(object, document, repr);
119     }
121     /*LOAD ATTRIBUTES FROM REPR HERE*/
122     object->readAttr( "surfaceScale" );
123     object->readAttr( "specularConstant" );
124     object->readAttr( "specularExponent" );
125     object->readAttr( "kernelUnitLength" );
126     object->readAttr( "lighting-color" );
127     
130 /**
131  * Drops any allocated memory.
132  */
133 static void
134 sp_feSpecularLighting_release(SPObject *object)
136     if (((SPObjectClass *) feSpecularLighting_parent_class)->release)
137         ((SPObjectClass *) feSpecularLighting_parent_class)->release(object);
140 /**
141  * Sets a specific value in the SPFeSpecularLighting.
142  */
143 static void
144 sp_feSpecularLighting_set(SPObject *object, unsigned int key, gchar const *value)
146     SPFeSpecularLighting *feSpecularLighting = SP_FESPECULARLIGHTING(object);
147     gchar const *cend_ptr = NULL;
148     gchar *end_ptr = NULL;
149     switch(key) {
150         /*DEAL WITH SETTING ATTRIBUTES HERE*/
151 //TODO test forbidden values
152         case SP_ATTR_SURFACESCALE:
153             end_ptr = NULL;
154             if (value) {
155                 feSpecularLighting->surfaceScale = g_ascii_strtod(value, &end_ptr);
156                 if (end_ptr) {
157                     feSpecularLighting->surfaceScale_set = TRUE;
158                 } else {
159                     g_warning("feSpecularLighting: surfaceScale should be a number ... defaulting to 1");
160                 }
162             }
163             //if the attribute is not set or has an unreadable value
164             if (!value || !end_ptr) {
165                 feSpecularLighting->surfaceScale = 1;
166                 feSpecularLighting->surfaceScale_set = FALSE;
167             }
168             if (feSpecularLighting->renderer) {
169                 feSpecularLighting->renderer->surfaceScale = feSpecularLighting->surfaceScale;
170             }
171             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
172             break;
173         case SP_ATTR_SPECULARCONSTANT:
174             end_ptr = NULL;
175             if (value) {
176                 feSpecularLighting->specularConstant = g_ascii_strtod(value, &end_ptr);
177                 if (end_ptr && feSpecularLighting->specularConstant >= 0) {
178                     feSpecularLighting->specularConstant_set = TRUE;
179                 } else {
180                     end_ptr = NULL;
181                     g_warning("feSpecularLighting: specularConstant should be a positive number ... defaulting to 1");
182                 }
183             }
184             if (!value || !end_ptr) {
185                 feSpecularLighting->specularConstant = 1;
186                 feSpecularLighting->specularConstant_set = FALSE;
187             }
188             if (feSpecularLighting->renderer) {
189                 feSpecularLighting->renderer->specularConstant = feSpecularLighting->specularConstant;
190             }
191             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
192             break;
193         case SP_ATTR_SPECULAREXPONENT:
194             end_ptr = NULL;
195             if (value) {
196                 feSpecularLighting->specularExponent = g_ascii_strtod(value, &end_ptr);
197                 if (feSpecularLighting->specularExponent >= 1 && feSpecularLighting->specularExponent <= 128) {
198                     feSpecularLighting->specularExponent_set = TRUE;
199                 } else {
200                     end_ptr = NULL;
201                     g_warning("feSpecularLighting: specularExponent should be a number in range [1, 128] ... defaulting to 1");
202                 }
203             } 
204             if (!value || !end_ptr) {
205                 feSpecularLighting->specularExponent = 1;
206                 feSpecularLighting->specularExponent_set = FALSE;
207             }
208             if (feSpecularLighting->renderer) {
209                 feSpecularLighting->renderer->specularExponent = feSpecularLighting->specularExponent;
210             }
211             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
212             break;
213         case SP_ATTR_KERNELUNITLENGTH:
214             //TODO kernelUnit
215             //feSpecularLighting->kernelUnitLength.set(value);
216             /*TODOif (feSpecularLighting->renderer) {
217                 feSpecularLighting->renderer->surfaceScale = feSpecularLighting->renderer;
218             }
219             */
220             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
221             break;
222         case SP_PROP_LIGHTING_COLOR:
223             cend_ptr = NULL;
224             feSpecularLighting->lighting_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
225             //if a value was read
226             if (cend_ptr) {
227                 feSpecularLighting->lighting_color_set = TRUE;
228             } else {
229                 //lighting_color already contains the default value
230                 feSpecularLighting->lighting_color_set = FALSE;
231             }
232             if (feSpecularLighting->renderer) {
233                 feSpecularLighting->renderer->lighting_color = feSpecularLighting->lighting_color;
234             }
235             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
236             break;
237         default:
238             if (((SPObjectClass *) feSpecularLighting_parent_class)->set)
239                 ((SPObjectClass *) feSpecularLighting_parent_class)->set(object, key, value);
240             break;
241     }
245 /**
246  * Receives update notifications.
247  */
248 static void
249 sp_feSpecularLighting_update(SPObject *object, SPCtx *ctx, guint flags)
251     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
252         object->readAttr( "surfaceScale" );
253         object->readAttr( "specularConstant" );
254         object->readAttr( "specularExponent" );
255         object->readAttr( "kernelUnitLength" );
256         object->readAttr( "lighting-color" );
257     }
259     if (((SPObjectClass *) feSpecularLighting_parent_class)->update) {
260         ((SPObjectClass *) feSpecularLighting_parent_class)->update(object, ctx, flags);
261     }
264 /**
265  * Writes its settings to an incoming repr object, if any.
266  */
267 static Inkscape::XML::Node *
268 sp_feSpecularLighting_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
270     SPFeSpecularLighting *fespecularlighting = SP_FESPECULARLIGHTING(object);
271     
272     /* TODO: Don't just clone, but create a new repr node and write all
273      * relevant values _and children_ into it */
274     if (!repr) {
275         repr = SP_OBJECT_REPR(object)->duplicate(doc);
276         //repr = doc->createElement("svg:feSpecularLighting");
277     }
279     if (fespecularlighting->surfaceScale_set)
280         sp_repr_set_css_double(repr, "surfaceScale", fespecularlighting->surfaceScale);
281     if (fespecularlighting->specularConstant_set)
282         sp_repr_set_css_double(repr, "specularConstant", fespecularlighting->specularConstant);
283     if (fespecularlighting->specularExponent_set)
284         sp_repr_set_css_double(repr, "specularExponent", fespecularlighting->specularExponent);
285    /*TODO kernelUnits */ 
286     if (fespecularlighting->lighting_color_set) {
287         gchar c[64];
288         sp_svg_write_color(c, sizeof(c), fespecularlighting->lighting_color);
289         repr->setAttribute("lighting-color", c);
290     }
291     if (((SPObjectClass *) feSpecularLighting_parent_class)->write) {
292         ((SPObjectClass *) feSpecularLighting_parent_class)->write(object, doc, repr, flags);
293     }
295     return repr;
298 /**
299  * Callback for child_added event.
300  */
301 static void
302 sp_feSpecularLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
304     SPFeSpecularLighting *f = SP_FESPECULARLIGHTING(object);
306     if (((SPObjectClass *) feSpecularLighting_parent_class)->child_added)
307         (* ((SPObjectClass *) feSpecularLighting_parent_class)->child_added)(object, child, ref);
309     sp_feSpecularLighting_children_modified(f);
310     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
312             
314 /**
315  * Callback for remove_child event.
316  */
317 static void
318 sp_feSpecularLighting_remove_child(SPObject *object, Inkscape::XML::Node *child)
319 {   
320     SPFeSpecularLighting *f = SP_FESPECULARLIGHTING(object);
322     if (((SPObjectClass *) feSpecularLighting_parent_class)->remove_child)
323         (* ((SPObjectClass *) feSpecularLighting_parent_class)->remove_child)(object, child);   
325     sp_feSpecularLighting_children_modified(f);
326     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
329 static void
330 sp_feSpecularLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
332     SPFeSpecularLighting *f = SP_FESPECULARLIGHTING(object);
333     if (((SPObjectClass *) (feSpecularLighting_parent_class))->order_changed)
334         (* ((SPObjectClass *) (feSpecularLighting_parent_class))->order_changed) (object, child, old_ref, new_ref);
336     sp_feSpecularLighting_children_modified(f);
337     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
340 static void sp_feSpecularLighting_children_modified(SPFeSpecularLighting *sp_specularlighting)
342    if (sp_specularlighting->renderer) {
343         sp_specularlighting->renderer->light_type = Inkscape::Filters::NO_LIGHT;
344         if (SP_IS_FEDISTANTLIGHT(sp_specularlighting->children)) {
345             sp_specularlighting->renderer->light_type = Inkscape::Filters::DISTANT_LIGHT;
346             sp_specularlighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_specularlighting->children);
347         }
348         if (SP_IS_FEPOINTLIGHT(sp_specularlighting->children)) {
349             sp_specularlighting->renderer->light_type = Inkscape::Filters::POINT_LIGHT;
350             sp_specularlighting->renderer->light.point = SP_FEPOINTLIGHT(sp_specularlighting->children);
351         }
352         if (SP_IS_FESPOTLIGHT(sp_specularlighting->children)) {
353             sp_specularlighting->renderer->light_type = Inkscape::Filters::SPOT_LIGHT;
354             sp_specularlighting->renderer->light.spot = SP_FESPOTLIGHT(sp_specularlighting->children);
355         }
356    }
359 static void sp_feSpecularLighting_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
360     g_assert(primitive != NULL);
361     g_assert(filter != NULL);
363     SPFeSpecularLighting *sp_specularlighting = SP_FESPECULARLIGHTING(primitive);
365     int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_SPECULARLIGHTING);
366     Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
367     Inkscape::Filters::FilterSpecularLighting *nr_specularlighting = dynamic_cast<Inkscape::Filters::FilterSpecularLighting*>(nr_primitive);
368     g_assert(nr_specularlighting != NULL);
370     sp_specularlighting->renderer = nr_specularlighting;
371     sp_filter_primitive_renderer_common(primitive, nr_primitive);
373     nr_specularlighting->specularConstant = sp_specularlighting->specularConstant;
374     nr_specularlighting->specularExponent = sp_specularlighting->specularExponent;
375     nr_specularlighting->surfaceScale = sp_specularlighting->surfaceScale;
376     nr_specularlighting->lighting_color = sp_specularlighting->lighting_color;
377     //We assume there is at most one child
378     nr_specularlighting->light_type = Inkscape::Filters::NO_LIGHT;
379     if (SP_IS_FEDISTANTLIGHT(primitive->children)) {
380         nr_specularlighting->light_type = Inkscape::Filters::DISTANT_LIGHT;
381         nr_specularlighting->light.distant = SP_FEDISTANTLIGHT(primitive->children);
382     }
383     if (SP_IS_FEPOINTLIGHT(primitive->children)) {
384         nr_specularlighting->light_type = Inkscape::Filters::POINT_LIGHT;
385         nr_specularlighting->light.point = SP_FEPOINTLIGHT(primitive->children);
386     }
387     if (SP_IS_FESPOTLIGHT(primitive->children)) {
388         nr_specularlighting->light_type = Inkscape::Filters::SPOT_LIGHT;
389         nr_specularlighting->light.spot = SP_FESPOTLIGHT(primitive->children);
390     }
391         
392     //nr_offset->set_dx(sp_offset->dx);
393     //nr_offset->set_dy(sp_offset->dy);
397 /*
398   Local Variables:
399   mode:c++
400   c-file-style:"stroustrup"
401   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
402   indent-tabs-mode:nil
403   fill-column:99
404   End:
405 */
406 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :