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 "diffuselighting.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::Document *doc, Inkscape::XML::Node *repr, guint flags);
49 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::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;
106 }
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)
115 {
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");
126 }
128 /**
129 * Drops any allocated memory.
130 */
131 static void
132 sp_feDiffuseLighting_release(SPObject *object)
133 {
134 if (((SPObjectClass *) feDiffuseLighting_parent_class)->release)
135 ((SPObjectClass *) feDiffuseLighting_parent_class)->release(object);
136 }
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)
143 {
144 SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object);
145 gchar const *cend_ptr = NULL;
146 gchar *end_ptr = NULL;
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 }
218 }
220 /**
221 * Receives update notifications.
222 */
223 static void
224 sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags)
225 {
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 }
236 }
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::Document *doc, Inkscape::XML::Node *repr, guint flags)
243 {
244 SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object);
246 /* TODO: Don't just clone, but create a new repr node and write all
247 * relevant values _and children_ into it */
248 if (!repr) {
249 repr = SP_OBJECT_REPR(object)->duplicate(doc);
250 //repr = doc->createElement("svg:feDiffuseLighting");
251 }
253 if (fediffuselighting->surfaceScale_set)
254 sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale);
255 else
256 repr->setAttribute("surfaceScale", NULL);
257 if (fediffuselighting->diffuseConstant_set)
258 sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant);
259 else
260 repr->setAttribute("diffuseConstant", NULL);
261 /*TODO kernelUnits */
262 if (fediffuselighting->lighting_color_set) {
263 gchar c[64];
264 sp_svg_write_color(c, sizeof(c), fediffuselighting->lighting_color);
265 repr->setAttribute("lighting-color", c);
266 } else
267 repr->setAttribute("lighting-color", NULL);
269 if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) {
270 ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, doc, repr, flags);
271 }
273 return repr;
274 }
276 /**
277 * Callback for child_added event.
278 */
279 static void
280 sp_feDiffuseLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
281 {
282 SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
284 if (((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)
285 (* ((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)(object, child, ref);
287 sp_feDiffuseLighting_children_modified(f);
288 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
289 }
292 /**
293 * Callback for remove_child event.
294 */
295 static void
296 sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child)
297 {
298 SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
300 if (((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)
301 (* ((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)(object, child);
303 sp_feDiffuseLighting_children_modified(f);
304 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
305 }
307 static void
308 sp_feDiffuseLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
309 {
310 SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
311 if (((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed)
312 (* ((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) (object, child, old_ref, new_ref);
314 sp_feDiffuseLighting_children_modified(f);
315 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
316 }
318 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting)
319 {
320 if (sp_diffuselighting->renderer) {
321 sp_diffuselighting->renderer->light_type = Inkscape::Filters::NO_LIGHT;
322 if (SP_IS_FEDISTANTLIGHT(sp_diffuselighting->children)) {
323 sp_diffuselighting->renderer->light_type = Inkscape::Filters::DISTANT_LIGHT;
324 sp_diffuselighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_diffuselighting->children);
325 }
326 if (SP_IS_FEPOINTLIGHT(sp_diffuselighting->children)) {
327 sp_diffuselighting->renderer->light_type = Inkscape::Filters::POINT_LIGHT;
328 sp_diffuselighting->renderer->light.point = SP_FEPOINTLIGHT(sp_diffuselighting->children);
329 }
330 if (SP_IS_FESPOTLIGHT(sp_diffuselighting->children)) {
331 sp_diffuselighting->renderer->light_type = Inkscape::Filters::SPOT_LIGHT;
332 sp_diffuselighting->renderer->light.spot = SP_FESPOTLIGHT(sp_diffuselighting->children);
333 }
334 }
335 }
337 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
338 g_assert(primitive != NULL);
339 g_assert(filter != NULL);
341 SPFeDiffuseLighting *sp_diffuselighting = SP_FEDIFFUSELIGHTING(primitive);
343 int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_DIFFUSELIGHTING);
344 Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
345 Inkscape::Filters::FilterDiffuseLighting *nr_diffuselighting = dynamic_cast<Inkscape::Filters::FilterDiffuseLighting*>(nr_primitive);
346 g_assert(nr_diffuselighting != NULL);
348 sp_diffuselighting->renderer = nr_diffuselighting;
349 sp_filter_primitive_renderer_common(primitive, nr_primitive);
351 nr_diffuselighting->diffuseConstant = sp_diffuselighting->diffuseConstant;
352 nr_diffuselighting->surfaceScale = sp_diffuselighting->surfaceScale;
353 nr_diffuselighting->lighting_color = sp_diffuselighting->lighting_color;
354 //We assume there is at most one child
355 nr_diffuselighting->light_type = Inkscape::Filters::NO_LIGHT;
356 if (SP_IS_FEDISTANTLIGHT(primitive->children)) {
357 nr_diffuselighting->light_type = Inkscape::Filters::DISTANT_LIGHT;
358 nr_diffuselighting->light.distant = SP_FEDISTANTLIGHT(primitive->children);
359 }
360 if (SP_IS_FEPOINTLIGHT(primitive->children)) {
361 nr_diffuselighting->light_type = Inkscape::Filters::POINT_LIGHT;
362 nr_diffuselighting->light.point = SP_FEPOINTLIGHT(primitive->children);
363 }
364 if (SP_IS_FESPOTLIGHT(primitive->children)) {
365 nr_diffuselighting->light_type = Inkscape::Filters::SPOT_LIGHT;
366 nr_diffuselighting->light.spot = SP_FESPOTLIGHT(primitive->children);
367 }
369 //nr_offset->set_dx(sp_offset->dx);
370 //nr_offset->set_dy(sp_offset->dy);
371 }
374 /*
375 Local Variables:
376 mode:c++
377 c-file-style:"stroustrup"
378 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
379 indent-tabs-mode:nil
380 fill-column:99
381 End:
382 */
383 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :