X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsp-fediffuselighting.cpp;h=96dee569b80c3dc716100109ad21b4f67e1c2a1c;hb=8d358698ecbf192ba7c6dc05d4f7de7592753d9f;hp=5683b330d173e9490b7779ce46dd3007ba3e84f5;hpb=761a541468cc0b8a7fcfb8c65784c0896f0f80f4;p=inkscape.git diff --git a/src/sp-fediffuselighting.cpp b/src/sp-fediffuselighting.cpp index 5683b330d..96dee569b 100644 --- a/src/sp-fediffuselighting.cpp +++ b/src/sp-fediffuselighting.cpp @@ -7,8 +7,10 @@ /* * Authors: * hugo Rodrigues + * Jean-Rene Reinhard * * Copyright (C) 2006 Hugo Rodrigues + * 2007 authors * * Released under GNU GPL, read the file 'COPYING' for more information */ @@ -19,22 +21,11 @@ #include "attributes.h" #include "svg/svg.h" +#include "sp-object.h" +#include "svg/svg-color.h" #include "sp-fediffuselighting.h" #include "xml/repr.h" - -//#define SP_MACROS_SILENT -//#include "macros.h" - -#define DEBUG_FEDIFFUSELIGHTING -#ifdef DEBUG_FEDIFFUSELIGHTING -# define debug(f, a...) { g_print("%s(%d) %s:", \ - __FILE__,__LINE__,__FUNCTION__); \ - g_print(f, ## a); \ - g_print("\n"); \ - } -#else -# define debug(f, a...) /**/ -#endif +#include "display/nr-filter-diffuselighting.h" /* FeDiffuseLighting base class */ @@ -45,9 +36,20 @@ static void sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, I 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); -static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags); - -static SPObjectClass *feDiffuseLighting_parent_class; +//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::Document *doc, 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; GType sp_feDiffuseLighting_get_type() @@ -65,7 +67,7 @@ sp_feDiffuseLighting_get_type() (GInstanceInitFunc) sp_feDiffuseLighting_init, NULL, /* value_table */ }; - feDiffuseLighting_type = g_type_register_static(SP_TYPE_OBJECT, "SPFeDiffuseLighting", &feDiffuseLighting_info, (GTypeFlags)0); + feDiffuseLighting_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeDiffuseLighting", &feDiffuseLighting_info, (GTypeFlags)0); } return feDiffuseLighting_type; } @@ -74,20 +76,33 @@ static void sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass) { SPObjectClass *sp_object_class = (SPObjectClass *)klass; - - feDiffuseLighting_parent_class = (SPObjectClass*)g_type_class_peek_parent(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->release = sp_feDiffuseLighting_release; 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) { - debug("0x%p",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; } /** @@ -98,12 +113,16 @@ sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting) static void sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr) { - debug("0x%p",object); if (((SPObjectClass *) feDiffuseLighting_parent_class)->build) { ((SPObjectClass *) feDiffuseLighting_parent_class)->build(object, document, repr); } /*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"); + } /** @@ -112,8 +131,6 @@ sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML static void sp_feDiffuseLighting_release(SPObject *object) { - debug("0x%p",object); - if (((SPObjectClass *) feDiffuseLighting_parent_class)->release) ((SPObjectClass *) feDiffuseLighting_parent_class)->release(object); } @@ -124,12 +141,74 @@ sp_feDiffuseLighting_release(SPObject *object) static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value) { - debug("0x%p %s(%u): '%s'",object, - sp_attribute_name(key),key,value); SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object); - - switch(key) { + 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); @@ -144,13 +223,11 @@ sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value) static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags) { - debug("0x%p",object); - - 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) { @@ -162,27 +239,140 @@ sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags) * Writes its settings to an incoming repr object, if any. */ static Inkscape::XML::Node * -sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags) +sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) { - debug("0x%p",object); - + 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 = SP_OBJECT_REPR(object)->duplicate(doc); } } - + + 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, sizeof(c), 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); + ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, doc, repr, flags); } 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_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: