Code

a44ac5894204a7c3cdd3bbe6434d970b6ea57c8c
[inkscape.git] / src / sp-fediffuselighting.cpp
1 #define __SP_FEDIFFUSELIGHTING_CPP__
3 /** \file
4  * SVG <feDiffuseLighting> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   hugo Rodrigues <haa.rodrigues@gmail.com>
10  *   Jean-Rene Reinhard <jr@komite.net>
11  *
12  * Copyright (C) 2006 Hugo Rodrigues
13  *               2007 authors
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include "attributes.h"
23 #include "svg/svg.h"
24 #include "sp-object.h"
25 #include "svg/svg-color.h"
26 #include "sp-fediffuselighting.h"
27 #include "xml/repr.h"
28 #include "display/nr-filter-diffuselighting.h"
30 /* FeDiffuseLighting base class */
32 static void sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass);
33 static void sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting);
35 static void sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
36 static void sp_feDiffuseLighting_release(SPObject *object);
37 static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value);
38 static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags);
39 //we assume that svg:feDiffuseLighting can have any number of children
40 //only the first one is considered as the light source of the filter
41 //TODO is that right?
42 //if not modify child_added and remove_child to raise errors
43 static void sp_feDiffuseLighting_child_added(SPObject *object,
44                                     Inkscape::XML::Node *child,
45                                     Inkscape::XML::Node *ref);
46 static void sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child);
47 static void sp_feDiffuseLighting_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
48 static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
49 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
50 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting);
52 static SPFilterPrimitiveClass *feDiffuseLighting_parent_class;
54 GType
55 sp_feDiffuseLighting_get_type()
56 {
57     static GType feDiffuseLighting_type = 0;
59     if (!feDiffuseLighting_type) {
60         GTypeInfo feDiffuseLighting_info = {
61             sizeof(SPFeDiffuseLightingClass),
62             NULL, NULL,
63             (GClassInitFunc) sp_feDiffuseLighting_class_init,
64             NULL, NULL,
65             sizeof(SPFeDiffuseLighting),
66             16,
67             (GInstanceInitFunc) sp_feDiffuseLighting_init,
68             NULL,    /* value_table */
69         };
70         feDiffuseLighting_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeDiffuseLighting", &feDiffuseLighting_info, (GTypeFlags)0);
71     }
72     return feDiffuseLighting_type;
73 }
75 static void
76 sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass)
77 {
78     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
79     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
80     feDiffuseLighting_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
82     sp_object_class->build = sp_feDiffuseLighting_build;
83     sp_object_class->release = sp_feDiffuseLighting_release;
84     sp_object_class->write = sp_feDiffuseLighting_write;
85     sp_object_class->set = sp_feDiffuseLighting_set;
86     sp_object_class->update = sp_feDiffuseLighting_update;
87     sp_object_class->child_added = sp_feDiffuseLighting_child_added;
88     sp_object_class->remove_child = sp_feDiffuseLighting_remove_child;
89     sp_object_class->order_changed = sp_feDiffuseLighting_order_changed;
91     sp_primitive_class->build_renderer = sp_feDiffuseLighting_build_renderer;
92 }
94 static void
95 sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting)
96 {
97     feDiffuseLighting->surfaceScale = 1;
98     feDiffuseLighting->diffuseConstant = 1;
99     feDiffuseLighting->lighting_color = 0xffffffff;
100     //TODO kernelUnit
101     feDiffuseLighting->renderer = NULL;
103     feDiffuseLighting->surfaceScale_set = FALSE;
104     feDiffuseLighting->diffuseConstant_set = FALSE;
105     feDiffuseLighting->lighting_color_set = FALSE;
108 /**
109  * Reads the Inkscape::XML::Node, and initializes SPFeDiffuseLighting variables.  For this to get called,
110  * our name must be associated with a repr via "sp_object_type_register".  Best done through
111  * sp-object-repr.cpp's repr_name_entries array.
112  */
113 static void
114 sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
116     if (((SPObjectClass *) feDiffuseLighting_parent_class)->build) {
117         ((SPObjectClass *) feDiffuseLighting_parent_class)->build(object, document, repr);
118     }
120     /*LOAD ATTRIBUTES FROM REPR HERE*/
121     sp_object_read_attr(object, "surfaceScale");
122     sp_object_read_attr(object, "diffuseConstant");
123     sp_object_read_attr(object, "kernelUnitLength");
124     sp_object_read_attr(object, "lighting-color");
125     
128 /**
129  * Drops any allocated memory.
130  */
131 static void
132 sp_feDiffuseLighting_release(SPObject *object)
134     if (((SPObjectClass *) feDiffuseLighting_parent_class)->release)
135         ((SPObjectClass *) feDiffuseLighting_parent_class)->release(object);
138 /**
139  * Sets a specific value in the SPFeDiffuseLighting.
140  */
141 static void
142 sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
144     SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object);
145     gchar const *cend_ptr = NULL;
146     gchar *end_ptr = NULL;
147     
148     switch(key) {
149         /*DEAL WITH SETTING ATTRIBUTES HERE*/
150 //TODO test forbidden values
151         case SP_ATTR_SURFACESCALE:
152             end_ptr = NULL;
153             if (value) {
154                 feDiffuseLighting->surfaceScale = g_ascii_strtod(value, &end_ptr);
155                 if (end_ptr) {
156                     feDiffuseLighting->surfaceScale_set = TRUE;
157                 }
158             } 
159             if (!value || !end_ptr) {
160                 feDiffuseLighting->surfaceScale = 1;
161                 feDiffuseLighting->surfaceScale_set = FALSE;
162             }
163             if (feDiffuseLighting->renderer) {
164                 feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->surfaceScale;
165             }
166             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
167             break;
168         case SP_ATTR_DIFFUSECONSTANT:
169             end_ptr = NULL;
170             if (value) {
171                 feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, &end_ptr);
172                 if (end_ptr && feDiffuseLighting->diffuseConstant >= 0) {
173                     feDiffuseLighting->diffuseConstant_set = TRUE;
174                 } else {
175                     end_ptr = NULL;
176                     g_warning("feDiffuseLighting: diffuseConstant should be a positive number ... defaulting to 1");
177                 }
178             } 
179             if (!value || !end_ptr) {
180                 feDiffuseLighting->diffuseConstant = 1;
181                 feDiffuseLighting->diffuseConstant_set = FALSE;
182             }
183             if (feDiffuseLighting->renderer) {
184                 feDiffuseLighting->renderer->diffuseConstant = feDiffuseLighting->diffuseConstant;
185     }
186             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
187             break;
188         case SP_ATTR_KERNELUNITLENGTH:
189             //TODO kernelUnit
190             //feDiffuseLighting->kernelUnitLength.set(value);
191             /*TODOif (feDiffuseLighting->renderer) {
192                 feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->renderer;
193             }
194             */
195             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
196             break;
197         case SP_PROP_LIGHTING_COLOR:
198             cend_ptr = NULL;
199             feDiffuseLighting->lighting_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
200             //if a value was read
201             if (cend_ptr) {
202                 feDiffuseLighting->lighting_color_set = TRUE; 
203             } else {
204                 //lighting_color already contains the default value
205                 feDiffuseLighting->lighting_color_set = FALSE; 
206             }
207             if (feDiffuseLighting->renderer) {
208                 feDiffuseLighting->renderer->lighting_color = feDiffuseLighting->lighting_color;
209             }
210             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
211             break;
212         default:
213             if (((SPObjectClass *) feDiffuseLighting_parent_class)->set)
214                 ((SPObjectClass *) feDiffuseLighting_parent_class)->set(object, key, value);
215             break;
216     }
220 /**
221  * Receives update notifications.
222  */
223 static void
224 sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags)
226     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
227         sp_object_read_attr(object, "surfaceScale");
228         sp_object_read_attr(object, "diffuseConstant");
229         sp_object_read_attr(object, "kernelUnit");
230         sp_object_read_attr(object, "lighting-color");
231     }
233     if (((SPObjectClass *) feDiffuseLighting_parent_class)->update) {
234         ((SPObjectClass *) feDiffuseLighting_parent_class)->update(object, ctx, flags);
235     }
238 /**
239  * Writes its settings to an incoming repr object, if any.
240  */
241 static Inkscape::XML::Node *
242 sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
244     SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object);
245     
246     // Inkscape-only object, not copied during an "plain SVG" dump:
247     if (flags & SP_OBJECT_WRITE_EXT) {
248         if (repr) {
249             // is this sane?
250             //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
251         } else {
252             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
253         }
254     }
255     
256     if (fediffuselighting->surfaceScale_set)
257         sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale);
258     else
259         repr->setAttribute("surfaceScale", NULL);
260     if (fediffuselighting->diffuseConstant_set)
261         sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant);
262     else
263         repr->setAttribute("diffuseConstant", NULL);
264    /*TODO kernelUnits */ 
265     if (fediffuselighting->lighting_color_set) {
266         gchar c[64];
267         sp_svg_write_color(c, sizeof(c), fediffuselighting->lighting_color);
268         repr->setAttribute("lighting-color", c);
269     } else
270         repr->setAttribute("lighting-color", NULL);
271         
272     if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) {
273         ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, repr, flags);
274     }
276     return repr;
279 /**
280  * Callback for child_added event.
281  */
282 static void
283 sp_feDiffuseLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
285     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
287     if (((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)
288         (* ((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)(object, child, ref);
290     sp_feDiffuseLighting_children_modified(f);
291     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
293             
295 /**
296  * Callback for remove_child event.
297  */
298 static void
299 sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child)
300 {   
301     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
303     if (((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)
304         (* ((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)(object, child);   
306     sp_feDiffuseLighting_children_modified(f);
307     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
310 static void
311 sp_feDiffuseLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
313     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
314     if (((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed)
315         (* ((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) (object, child, old_ref, new_ref);
317     sp_feDiffuseLighting_children_modified(f);
318     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
321 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting)
323    if (sp_diffuselighting->renderer) {
324         sp_diffuselighting->renderer->light_type = NR::NO_LIGHT;
325         if (SP_IS_FEDISTANTLIGHT(sp_diffuselighting->children)) {
326             sp_diffuselighting->renderer->light_type = NR::DISTANT_LIGHT;
327             sp_diffuselighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_diffuselighting->children);
328         }
329         if (SP_IS_FEPOINTLIGHT(sp_diffuselighting->children)) {
330             sp_diffuselighting->renderer->light_type = NR::POINT_LIGHT;
331             sp_diffuselighting->renderer->light.point = SP_FEPOINTLIGHT(sp_diffuselighting->children);
332         }
333         if (SP_IS_FESPOTLIGHT(sp_diffuselighting->children)) {
334             sp_diffuselighting->renderer->light_type = NR::SPOT_LIGHT;
335             sp_diffuselighting->renderer->light.spot = SP_FESPOTLIGHT(sp_diffuselighting->children);
336         }
337    }
340 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
341     g_assert(primitive != NULL);
342     g_assert(filter != NULL);
344     SPFeDiffuseLighting *sp_diffuselighting = SP_FEDIFFUSELIGHTING(primitive);
346     int primitive_n = filter->add_primitive(NR::NR_FILTER_DIFFUSELIGHTING);
347     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
348     NR::FilterDiffuseLighting *nr_diffuselighting = dynamic_cast<NR::FilterDiffuseLighting*>(nr_primitive);
349     g_assert(nr_diffuselighting != NULL);
351     sp_diffuselighting->renderer = nr_diffuselighting;
352     sp_filter_primitive_renderer_common(primitive, nr_primitive);
354     nr_diffuselighting->diffuseConstant = sp_diffuselighting->diffuseConstant;
355     nr_diffuselighting->surfaceScale = sp_diffuselighting->surfaceScale;
356     nr_diffuselighting->lighting_color = sp_diffuselighting->lighting_color;
357     //We assume there is at most one child
358     nr_diffuselighting->light_type = NR::NO_LIGHT;
359     if (SP_IS_FEDISTANTLIGHT(primitive->children)) {
360         nr_diffuselighting->light_type = NR::DISTANT_LIGHT;
361         nr_diffuselighting->light.distant = SP_FEDISTANTLIGHT(primitive->children);
362     }
363     if (SP_IS_FEPOINTLIGHT(primitive->children)) {
364         nr_diffuselighting->light_type = NR::POINT_LIGHT;
365         nr_diffuselighting->light.point = SP_FEPOINTLIGHT(primitive->children);
366     }
367     if (SP_IS_FESPOTLIGHT(primitive->children)) {
368         nr_diffuselighting->light_type = NR::SPOT_LIGHT;
369         nr_diffuselighting->light.spot = SP_FESPOTLIGHT(primitive->children);
370     }
371         
372     //nr_offset->set_dx(sp_offset->dx);
373     //nr_offset->set_dy(sp_offset->dy);
377 /*
378   Local Variables:
379   mode:c++
380   c-file-style:"stroustrup"
381   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
382   indent-tabs-mode:nil
383   fill-column:99
384   End:
385 */
386 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :