index e9720ab1648a10805dd54c3833e22b0c6a810810..cdb771cd021375fd158e54dc11ef34637e74ab97 100644 (file)
/*
* Authors:
* hugo Rodrigues <haa.rodrigues@gmail.com>
+ * Jean-Rene Reinhard <jr@komite.net>
*
* Copyright (C) 2006 Hugo Rodrigues
+ * 2007 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "attributes.h"
#include "svg/svg.h"
+#include "sp-object.h"
+#include "svg/svg-color.h"
#include "sp-fediffuselighting.h"
#include "xml/repr.h"
-
+#include "display/nr-filter-diffuselighting.h"
/* FeDiffuseLighting base class */
static void sp_feDiffuseLighting_release(SPObject *object);
static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value);
static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags);
+//we assume that svg:feDiffuseLighting can have any number of children
+//only the first one is considered as the light source of the filter
+//TODO is that right?
+//if not modify child_added and remove_child to raise errors
+static void sp_feDiffuseLighting_child_added(SPObject *object,
+ Inkscape::XML::Node *child,
+ Inkscape::XML::Node *ref);
+static void sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child);
+static void sp_feDiffuseLighting_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
+static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting);
static SPFilterPrimitiveClass *feDiffuseLighting_parent_class;
sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass)
{
SPObjectClass *sp_object_class = (SPObjectClass *)klass;
-
+ SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
feDiffuseLighting_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
sp_object_class->build = sp_feDiffuseLighting_build;
sp_object_class->write = sp_feDiffuseLighting_write;
sp_object_class->set = sp_feDiffuseLighting_set;
sp_object_class->update = sp_feDiffuseLighting_update;
+ sp_object_class->child_added = sp_feDiffuseLighting_child_added;
+ sp_object_class->remove_child = sp_feDiffuseLighting_remove_child;
+ sp_object_class->order_changed = sp_feDiffuseLighting_order_changed;
+
+ sp_primitive_class->build_renderer = sp_feDiffuseLighting_build_renderer;
}
static void
sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting)
{
+ feDiffuseLighting->surfaceScale = 1;
+ feDiffuseLighting->diffuseConstant = 1;
+ feDiffuseLighting->lighting_color = 0xffffffff;
+ //TODO kernelUnit
+ feDiffuseLighting->renderer = NULL;
+
+ feDiffuseLighting->surfaceScale_set = FALSE;
+ feDiffuseLighting->diffuseConstant_set = FALSE;
+ feDiffuseLighting->lighting_color_set = FALSE;
}
/**
@@ -89,6 +118,11 @@ sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML
}
/*LOAD ATTRIBUTES FROM REPR HERE*/
+ sp_object_read_attr(object, "surfaceScale");
+ sp_object_read_attr(object, "diffuseConstant");
+ sp_object_read_attr(object, "kernelUnitLength");
+ sp_object_read_attr(object, "lighting-color");
+
}
/**
sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
{
SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object);
- (void)feDiffuseLighting;
-
+ gchar const *cend_ptr = NULL;
+ gchar *end_ptr = NULL;
+
switch(key) {
/*DEAL WITH SETTING ATTRIBUTES HERE*/
+//TODO test forbidden values
+ case SP_ATTR_SURFACESCALE:
+ end_ptr = NULL;
+ if (value) {
+ feDiffuseLighting->surfaceScale = g_ascii_strtod(value, &end_ptr);
+ if (end_ptr) {
+ feDiffuseLighting->surfaceScale_set = TRUE;
+ }
+ }
+ if (!value || !end_ptr) {
+ feDiffuseLighting->surfaceScale = 1;
+ feDiffuseLighting->surfaceScale_set = FALSE;
+ }
+ if (feDiffuseLighting->renderer) {
+ feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->surfaceScale;
+ }
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+ break;
+ case SP_ATTR_DIFFUSECONSTANT:
+ end_ptr = NULL;
+ if (value) {
+ feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, &end_ptr);
+ if (end_ptr && feDiffuseLighting->diffuseConstant >= 0) {
+ feDiffuseLighting->diffuseConstant_set = TRUE;
+ } else {
+ end_ptr = NULL;
+ g_warning("feDiffuseLighting: diffuseConstant should be a positive number ... defaulting to 1");
+ }
+ }
+ if (!value || !end_ptr) {
+ feDiffuseLighting->diffuseConstant = 1;
+ feDiffuseLighting->diffuseConstant_set = FALSE;
+ }
+ if (feDiffuseLighting->renderer) {
+ feDiffuseLighting->renderer->diffuseConstant = feDiffuseLighting->diffuseConstant;
+ }
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+ break;
+ case SP_ATTR_KERNELUNITLENGTH:
+ //TODO kernelUnit
+ //feDiffuseLighting->kernelUnitLength.set(value);
+ /*TODOif (feDiffuseLighting->renderer) {
+ feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->renderer;
+ }
+ */
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+ break;
+ case SP_PROP_LIGHTING_COLOR:
+ cend_ptr = NULL;
+ feDiffuseLighting->lighting_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
+ //if a value was read
+ if (cend_ptr) {
+ feDiffuseLighting->lighting_color_set = TRUE;
+ } else {
+ //lighting_color already contains the default value
+ feDiffuseLighting->lighting_color_set = FALSE;
+ }
+ if (feDiffuseLighting->renderer) {
+ feDiffuseLighting->renderer->lighting_color = feDiffuseLighting->lighting_color;
+ }
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+ break;
default:
if (((SPObjectClass *) feDiffuseLighting_parent_class)->set)
((SPObjectClass *) feDiffuseLighting_parent_class)->set(object, key, value);
@@ -126,11 +223,11 @@ sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
static void
sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags)
{
- if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
- SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
-
- /* do something to trigger redisplay, updates? */
-
+ if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
+ sp_object_read_attr(object, "surfaceScale");
+ sp_object_read_attr(object, "diffuseConstant");
+ sp_object_read_attr(object, "kernelUnit");
+ sp_object_read_attr(object, "lighting-color");
}
if (((SPObjectClass *) feDiffuseLighting_parent_class)->update) {
static Inkscape::XML::Node *
sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
{
+ SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object);
+
// Inkscape-only object, not copied during an "plain SVG" dump:
if (flags & SP_OBJECT_WRITE_EXT) {
if (repr) {
// is this sane?
- repr->mergeFrom(SP_OBJECT_REPR(object), "id");
+ //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
} else {
- repr = SP_OBJECT_REPR(object)->duplicate(repr->document());
+ repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
}
}
-
+
+ if (fediffuselighting->surfaceScale_set)
+ sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale);
+ else
+ repr->setAttribute("surfaceScale", NULL);
+ if (fediffuselighting->diffuseConstant_set)
+ sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant);
+ else
+ repr->setAttribute("diffuseConstant", NULL);
+ /*TODO kernelUnits */
+ if (fediffuselighting->lighting_color_set) {
+ gchar c[64];
+ sp_svg_write_color(c, 64, fediffuselighting->lighting_color);
+ repr->setAttribute("lighting-color", c);
+ } else
+ repr->setAttribute("lighting-color", NULL);
+
if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) {
((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, repr, flags);
}
@@ -161,6 +276,103 @@ sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint fl
return repr;
}
+/**
+ * Callback for child_added event.
+ */
+static void
+sp_feDiffuseLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
+{
+ SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
+
+ if (((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)
+ (* ((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)(object, child, ref);
+
+ sp_feDiffuseLighting_children_modified(f);
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+}
+
+
+/**
+ * Callback for remove_child event.
+ */
+static void
+sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child)
+{
+ SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
+
+ if (((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)
+ (* ((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)(object, child);
+
+ sp_feDiffuseLighting_children_modified(f);
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+}
+
+static void
+sp_feDiffuseLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
+{
+ SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
+ if (((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed)
+ (* ((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) (object, child, old_ref, new_ref);
+
+ sp_feDiffuseLighting_children_modified(f);
+ object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+}
+
+static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting)
+{
+ if (sp_diffuselighting->renderer) {
+ sp_diffuselighting->renderer->light_type = NR::NO_LIGHT;
+ if (SP_IS_FEDISTANTLIGHT(sp_diffuselighting->children)) {
+ sp_diffuselighting->renderer->light_type = NR::DISTANT_LIGHT;
+ sp_diffuselighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_diffuselighting->children);
+ }
+ if (SP_IS_FEPOINTLIGHT(sp_diffuselighting->children)) {
+ sp_diffuselighting->renderer->light_type = NR::POINT_LIGHT;
+ sp_diffuselighting->renderer->light.point = SP_FEPOINTLIGHT(sp_diffuselighting->children);
+ }
+ if (SP_IS_FESPOTLIGHT(sp_diffuselighting->children)) {
+ sp_diffuselighting->renderer->light_type = NR::SPOT_LIGHT;
+ sp_diffuselighting->renderer->light.spot = SP_FESPOTLIGHT(sp_diffuselighting->children);
+ }
+ }
+}
+
+static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
+ g_assert(primitive != NULL);
+ g_assert(filter != NULL);
+
+ SPFeDiffuseLighting *sp_diffuselighting = SP_FEDIFFUSELIGHTING(primitive);
+
+ int primitive_n = filter->add_primitive(NR::NR_FILTER_DIFFUSELIGHTING);
+ NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
+ NR::FilterDiffuseLighting *nr_diffuselighting = dynamic_cast<NR::FilterDiffuseLighting*>(nr_primitive);
+ g_assert(nr_diffuselighting != NULL);
+
+ sp_diffuselighting->renderer = nr_diffuselighting;
+ sp_filter_primitive_renderer_common(primitive, nr_primitive);
+
+ nr_diffuselighting->diffuseConstant = sp_diffuselighting->diffuseConstant;
+ nr_diffuselighting->surfaceScale = sp_diffuselighting->surfaceScale;
+ nr_diffuselighting->lighting_color = sp_diffuselighting->lighting_color;
+ //We assume there is at most one child
+ nr_diffuselighting->light_type = NR::NO_LIGHT;
+ if (SP_IS_FEDISTANTLIGHT(primitive->children)) {
+ nr_diffuselighting->light_type = NR::DISTANT_LIGHT;
+ nr_diffuselighting->light.distant = SP_FEDISTANTLIGHT(primitive->children);
+ }
+ if (SP_IS_FEPOINTLIGHT(primitive->children)) {
+ nr_diffuselighting->light_type = NR::POINT_LIGHT;
+ nr_diffuselighting->light.point = SP_FEPOINTLIGHT(primitive->children);
+ }
+ if (SP_IS_FESPOTLIGHT(primitive->children)) {
+ nr_diffuselighting->light_type = NR::SPOT_LIGHT;
+ nr_diffuselighting->light.spot = SP_FESPOTLIGHT(primitive->children);
+ }
+
+ //nr_offset->set_dx(sp_offset->dx);
+ //nr_offset->set_dy(sp_offset->dy);
+}
+
/*
Local Variables: