Code

cleanup unused methods in spcurve
[inkscape.git] / src / sp-fediffuselighting.cpp
index 543c16e8b03834536dabee69118a449baaefad58..96dee569b80c3dc716100109ad21b4f67e1c2a1c 100644 (file)
@@ -45,7 +45,7 @@ static void sp_feDiffuseLighting_child_added(SPObject *object,
                                     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 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);
 
@@ -99,6 +99,10 @@ sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting)
     feDiffuseLighting->lighting_color = 0xffffffff;
     //TODO kernelUnit
     feDiffuseLighting->renderer = NULL;
+
+    feDiffuseLighting->surfaceScale_set = FALSE;
+    feDiffuseLighting->diffuseConstant_set = FALSE;
+    feDiffuseLighting->lighting_color_set = FALSE;
 }
 
 /**
@@ -138,22 +142,47 @@ static void
 sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
 {
     SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object);
+    gchar const *cend_ptr = NULL;
+    gchar *end_ptr = NULL;
     
     switch(key) {
        /*DEAL WITH SETTING ATTRIBUTES HERE*/
 //TODO test forbidden values
         case SP_ATTR_SURFACESCALE:
-            feDiffuseLighting->surfaceScale = g_ascii_strtod(value, NULL);
+            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:
-            feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, NULL);
+            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:
@@ -166,7 +195,15 @@ sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_PROP_LIGHTING_COLOR:
-            feDiffuseLighting->lighting_color = sp_svg_read_color(value, 0xffffffff);
+            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;
             }
@@ -202,7 +239,7 @@ 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)
 {
     SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object);
     
@@ -212,18 +249,28 @@ sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint fl
             // is this sane?
             //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
         } else {
-            repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
+            repr = SP_OBJECT_REPR(object)->duplicate(doc);
         }
     }
-
-    sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale);
-    sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant);
+    
+    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 */ 
-    gchar c[64];
-    sp_svg_write_color(c, 64, fediffuselighting->lighting_color);
-    repr->setAttribute("lighting-color", c);
+    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;