Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / filters / diffuselighting.cpp
1 /** \file
2  * SVG <feDiffuseLighting> 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 "diffuselighting.h"
26 #include "xml/repr.h"
27 #include "display/nr-filter-diffuselighting.h"
29 /* FeDiffuseLighting base class */
31 static void sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass);
32 static void sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting);
34 static void sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
35 static void sp_feDiffuseLighting_release(SPObject *object);
36 static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value);
37 static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags);
38 //we assume that svg:feDiffuseLighting 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_feDiffuseLighting_child_added(SPObject *object,
43                                     Inkscape::XML::Node *child,
44                                     Inkscape::XML::Node *ref);
45 static void sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child);
46 static void sp_feDiffuseLighting_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
47 static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
48 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
49 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting);
51 static SPFilterPrimitiveClass *feDiffuseLighting_parent_class;
53 GType
54 sp_feDiffuseLighting_get_type()
55 {
56     static GType feDiffuseLighting_type = 0;
58     if (!feDiffuseLighting_type) {
59         GTypeInfo feDiffuseLighting_info = {
60             sizeof(SPFeDiffuseLightingClass),
61             NULL, NULL,
62             (GClassInitFunc) sp_feDiffuseLighting_class_init,
63             NULL, NULL,
64             sizeof(SPFeDiffuseLighting),
65             16,
66             (GInstanceInitFunc) sp_feDiffuseLighting_init,
67             NULL,    /* value_table */
68         };
69         feDiffuseLighting_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeDiffuseLighting", &feDiffuseLighting_info, (GTypeFlags)0);
70     }
71     return feDiffuseLighting_type;
72 }
74 static void
75 sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass)
76 {
77     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
78     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
79     feDiffuseLighting_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
81     sp_object_class->build = sp_feDiffuseLighting_build;
82     sp_object_class->release = sp_feDiffuseLighting_release;
83     sp_object_class->write = sp_feDiffuseLighting_write;
84     sp_object_class->set = sp_feDiffuseLighting_set;
85     sp_object_class->update = sp_feDiffuseLighting_update;
86     sp_object_class->child_added = sp_feDiffuseLighting_child_added;
87     sp_object_class->remove_child = sp_feDiffuseLighting_remove_child;
88     sp_object_class->order_changed = sp_feDiffuseLighting_order_changed;
90     sp_primitive_class->build_renderer = sp_feDiffuseLighting_build_renderer;
91 }
93 static void
94 sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting)
95 {
96     feDiffuseLighting->surfaceScale = 1;
97     feDiffuseLighting->diffuseConstant = 1;
98     feDiffuseLighting->lighting_color = 0xffffffff;
99     //TODO kernelUnit
100     feDiffuseLighting->renderer = NULL;
102     feDiffuseLighting->surfaceScale_set = FALSE;
103     feDiffuseLighting->diffuseConstant_set = FALSE;
104     feDiffuseLighting->lighting_color_set = FALSE;
107 /**
108  * Reads the Inkscape::XML::Node, and initializes SPFeDiffuseLighting variables.  For this to get called,
109  * our name must be associated with a repr via "sp_object_type_register".  Best done through
110  * sp-object-repr.cpp's repr_name_entries array.
111  */
112 static void
113 sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
115     if (((SPObjectClass *) feDiffuseLighting_parent_class)->build) {
116         ((SPObjectClass *) feDiffuseLighting_parent_class)->build(object, document, repr);
117     }
119     /*LOAD ATTRIBUTES FROM REPR HERE*/
120     object->readAttr( "surfaceScale" );
121     object->readAttr( "diffuseConstant" );
122     object->readAttr( "kernelUnitLength" );
123     object->readAttr( "lighting-color" );
124     
127 /**
128  * Drops any allocated memory.
129  */
130 static void
131 sp_feDiffuseLighting_release(SPObject *object)
133     if (((SPObjectClass *) feDiffuseLighting_parent_class)->release)
134         ((SPObjectClass *) feDiffuseLighting_parent_class)->release(object);
137 /**
138  * Sets a specific value in the SPFeDiffuseLighting.
139  */
140 static void
141 sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
143     SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object);
144     gchar const *cend_ptr = NULL;
145     gchar *end_ptr = NULL;
146     
147     switch(key) {
148         /*DEAL WITH SETTING ATTRIBUTES HERE*/
149 //TODO test forbidden values
150         case SP_ATTR_SURFACESCALE:
151             end_ptr = NULL;
152             if (value) {
153                 feDiffuseLighting->surfaceScale = g_ascii_strtod(value, &end_ptr);
154                 if (end_ptr) {
155                     feDiffuseLighting->surfaceScale_set = TRUE;
156                 }
157             } 
158             if (!value || !end_ptr) {
159                 feDiffuseLighting->surfaceScale = 1;
160                 feDiffuseLighting->surfaceScale_set = FALSE;
161             }
162             if (feDiffuseLighting->renderer) {
163                 feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->surfaceScale;
164             }
165             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
166             break;
167         case SP_ATTR_DIFFUSECONSTANT:
168             end_ptr = NULL;
169             if (value) {
170                 feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, &end_ptr);
171                 if (end_ptr && feDiffuseLighting->diffuseConstant >= 0) {
172                     feDiffuseLighting->diffuseConstant_set = TRUE;
173                 } else {
174                     end_ptr = NULL;
175                     g_warning("feDiffuseLighting: diffuseConstant should be a positive number ... defaulting to 1");
176                 }
177             } 
178             if (!value || !end_ptr) {
179                 feDiffuseLighting->diffuseConstant = 1;
180                 feDiffuseLighting->diffuseConstant_set = FALSE;
181             }
182             if (feDiffuseLighting->renderer) {
183                 feDiffuseLighting->renderer->diffuseConstant = feDiffuseLighting->diffuseConstant;
184     }
185             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
186             break;
187         case SP_ATTR_KERNELUNITLENGTH:
188             //TODO kernelUnit
189             //feDiffuseLighting->kernelUnitLength.set(value);
190             /*TODOif (feDiffuseLighting->renderer) {
191                 feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->renderer;
192             }
193             */
194             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
195             break;
196         case SP_PROP_LIGHTING_COLOR:
197             cend_ptr = NULL;
198             feDiffuseLighting->lighting_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
199             //if a value was read
200             if (cend_ptr) {
201                 feDiffuseLighting->lighting_color_set = TRUE; 
202             } else {
203                 //lighting_color already contains the default value
204                 feDiffuseLighting->lighting_color_set = FALSE; 
205             }
206             if (feDiffuseLighting->renderer) {
207                 feDiffuseLighting->renderer->lighting_color = feDiffuseLighting->lighting_color;
208             }
209             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
210             break;
211         default:
212             if (((SPObjectClass *) feDiffuseLighting_parent_class)->set)
213                 ((SPObjectClass *) feDiffuseLighting_parent_class)->set(object, key, value);
214             break;
215     }
219 /**
220  * Receives update notifications.
221  */
222 static void
223 sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags)
225     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
226         object->readAttr( "surfaceScale" );
227         object->readAttr( "diffuseConstant" );
228         object->readAttr( "kernelUnit" );
229         object->readAttr( "lighting-color" );
230     }
232     if (((SPObjectClass *) feDiffuseLighting_parent_class)->update) {
233         ((SPObjectClass *) feDiffuseLighting_parent_class)->update(object, ctx, flags);
234     }
237 /**
238  * Writes its settings to an incoming repr object, if any.
239  */
240 static Inkscape::XML::Node *
241 sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
243     SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object);
244     
245     /* TODO: Don't just clone, but create a new repr node and write all
246      * relevant values _and children_ into it */
247     if (!repr) {
248         repr = SP_OBJECT_REPR(object)->duplicate(doc);
249         //repr = doc->createElement("svg:feDiffuseLighting");
250     }
251     
252     if (fediffuselighting->surfaceScale_set)
253         sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale);
254     else
255         repr->setAttribute("surfaceScale", NULL);
256     if (fediffuselighting->diffuseConstant_set)
257         sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant);
258     else
259         repr->setAttribute("diffuseConstant", NULL);
260    /*TODO kernelUnits */ 
261     if (fediffuselighting->lighting_color_set) {
262         gchar c[64];
263         sp_svg_write_color(c, sizeof(c), fediffuselighting->lighting_color);
264         repr->setAttribute("lighting-color", c);
265     } else
266         repr->setAttribute("lighting-color", NULL);
267         
268     if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) {
269         ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, doc, repr, flags);
270     }
272     return repr;
275 /**
276  * Callback for child_added event.
277  */
278 static void
279 sp_feDiffuseLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
281     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
283     if (((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)
284         (* ((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)(object, child, ref);
286     sp_feDiffuseLighting_children_modified(f);
287     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
289             
291 /**
292  * Callback for remove_child event.
293  */
294 static void
295 sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child)
296 {   
297     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
299     if (((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)
300         (* ((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)(object, child);   
302     sp_feDiffuseLighting_children_modified(f);
303     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
306 static void
307 sp_feDiffuseLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
309     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
310     if (((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed)
311         (* ((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) (object, child, old_ref, new_ref);
313     sp_feDiffuseLighting_children_modified(f);
314     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
317 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting)
319    if (sp_diffuselighting->renderer) {
320         sp_diffuselighting->renderer->light_type = Inkscape::Filters::NO_LIGHT;
321         if (SP_IS_FEDISTANTLIGHT(sp_diffuselighting->children)) {
322             sp_diffuselighting->renderer->light_type = Inkscape::Filters::DISTANT_LIGHT;
323             sp_diffuselighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_diffuselighting->children);
324         }
325         if (SP_IS_FEPOINTLIGHT(sp_diffuselighting->children)) {
326             sp_diffuselighting->renderer->light_type = Inkscape::Filters::POINT_LIGHT;
327             sp_diffuselighting->renderer->light.point = SP_FEPOINTLIGHT(sp_diffuselighting->children);
328         }
329         if (SP_IS_FESPOTLIGHT(sp_diffuselighting->children)) {
330             sp_diffuselighting->renderer->light_type = Inkscape::Filters::SPOT_LIGHT;
331             sp_diffuselighting->renderer->light.spot = SP_FESPOTLIGHT(sp_diffuselighting->children);
332         }
333    }
336 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
337     g_assert(primitive != NULL);
338     g_assert(filter != NULL);
340     SPFeDiffuseLighting *sp_diffuselighting = SP_FEDIFFUSELIGHTING(primitive);
342     int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_DIFFUSELIGHTING);
343     Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
344     Inkscape::Filters::FilterDiffuseLighting *nr_diffuselighting = dynamic_cast<Inkscape::Filters::FilterDiffuseLighting*>(nr_primitive);
345     g_assert(nr_diffuselighting != NULL);
347     sp_diffuselighting->renderer = nr_diffuselighting;
348     sp_filter_primitive_renderer_common(primitive, nr_primitive);
350     nr_diffuselighting->diffuseConstant = sp_diffuselighting->diffuseConstant;
351     nr_diffuselighting->surfaceScale = sp_diffuselighting->surfaceScale;
352     nr_diffuselighting->lighting_color = sp_diffuselighting->lighting_color;
353     //We assume there is at most one child
354     nr_diffuselighting->light_type = Inkscape::Filters::NO_LIGHT;
355     if (SP_IS_FEDISTANTLIGHT(primitive->children)) {
356         nr_diffuselighting->light_type = Inkscape::Filters::DISTANT_LIGHT;
357         nr_diffuselighting->light.distant = SP_FEDISTANTLIGHT(primitive->children);
358     }
359     if (SP_IS_FEPOINTLIGHT(primitive->children)) {
360         nr_diffuselighting->light_type = Inkscape::Filters::POINT_LIGHT;
361         nr_diffuselighting->light.point = SP_FEPOINTLIGHT(primitive->children);
362     }
363     if (SP_IS_FESPOTLIGHT(primitive->children)) {
364         nr_diffuselighting->light_type = Inkscape::Filters::SPOT_LIGHT;
365         nr_diffuselighting->light.spot = SP_FESPOTLIGHT(primitive->children);
366     }
367         
368     //nr_offset->set_dx(sp_offset->dx);
369     //nr_offset->set_dy(sp_offset->dy);
373 /*
374   Local Variables:
375   mode:c++
376   c-file-style:"stroustrup"
377   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
378   indent-tabs-mode:nil
379   fill-column:99
380   End:
381 */
382 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :