Code

Fix ef spam when adjusting pattern on text - patch from Adonis Papaderos
[inkscape.git] / src / style.cpp
index f2686287184228017fadde3950814b4bd8024321..551dd658516b594ea794405040c0aa6427b0d73b 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_STYLE_C__
-
 /** @file
  * @brief SVG stylesheets implementation.
  */
@@ -28,7 +26,6 @@
 #include "svg/svg.h"
 #include "svg/svg-color.h"
 #include "svg/svg-icc-color.h"
-#include "svg/svg-device-color.h"
 
 #include "display/canvas-bpath.h"
 #include "attributes.h"
@@ -86,7 +83,6 @@ static void sp_style_read_ilength(SPILength *val, gchar const *str);
 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
-static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
 static void sp_style_read_ibaselineshift(SPIBaselineShift *val, gchar const *str);
 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
@@ -489,11 +485,11 @@ sp_style_new_from_object(SPObject *object)
     g_return_val_if_fail(object != NULL, NULL);
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
 
-    SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
+    SPStyle *style = sp_style_new( object->document );
     style->object = object;
     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
 
-    if (object && SP_OBJECT_IS_CLONED(object)) {
+    if (object && object->cloned) {
         style->cloned = true;
     }
 
@@ -575,7 +571,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
 
     sp_style_clear(style);
 
-    if (object && SP_OBJECT_IS_CLONED(object)) {
+    if (object && object->cloned) {
         style->cloned = true;
     }
 
@@ -663,7 +659,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
     if (!style->fill.set) {
         val = repr->attribute("fill");
         if (val) {
-            sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+            style->fill.read( val, *style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL );
         }
     }
     /* fill-opacity */
@@ -679,7 +675,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
     if (!style->stroke.set) {
         val = repr->attribute("stroke");
         if (val) {
-            sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+            style->stroke.read( val, *style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL );
         }
     }
     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
@@ -1084,7 +1080,7 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
         }
         case SP_PROP_FILL:
             if (!style->fill.set) {
-                sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
+                style->fill.read( val, *style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL );
             }
             break;
         case SP_PROP_FILL_OPACITY:
@@ -1151,7 +1147,7 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
 
         case SP_PROP_STROKE:
             if (!style->stroke.set) {
-                sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
+                style->stroke.read( val, *style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL );
             }
             break;
         case SP_PROP_STROKE_WIDTH:
@@ -1370,12 +1366,12 @@ sp_style_merge_baseline_shift_from_parent(SPIBaselineShift &child, SPIBaselineSh
             child.computed = 0; // No change
         } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUB ) {
             // Should use subscript position from font relative to alphabetic baseline
-            // In mean time use values from OpenOffice and Adobe
-            child.computed = -0.33 * pfont_size.computed; 
+            // OpenOffice, Adobe: -0.33, Word -0.14, LaTex about -0.2.
+            child.computed = -0.2 * pfont_size.computed; 
         } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUPER ) {
             // Should use superscript position from font relative to alphabetic baseline
-            // In mean time use values from OpenOffice and Adobe
-            child.computed =  0.33 * pfont_size.computed; 
+            // OpenOffice, Adobe: 0.33, Word 0.35, LaTex about 0.45.
+            child.computed =  0.4 * pfont_size.computed; 
         } else {
             /* Illegal value */
         }
@@ -1396,6 +1392,8 @@ sp_style_merge_baseline_shift_from_parent(SPIBaselineShift &child, SPIBaselineSh
                 break;
         }
     }
+    // baseline-shifts are relative to parent baseline
+    child.computed += parent.computed;
 }
 
 /**
@@ -3199,18 +3197,17 @@ sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
  *
  * \pre paint == \&style.fill || paint == \&style.stroke.
  */
-static void
-sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
+void SPIPaint::read( gchar const *str, SPStyle &style, SPDocument *document )
 {
     while (g_ascii_isspace(*str)) {
         ++str;
     }
 
-    paint->clear();
+    clear();
 
     if (streq(str, "inherit")) {
-        paint->set = TRUE;
-        paint->inherit = TRUE;
+        set = TRUE;
+        inherit = TRUE;
     } else {
         if ( strneq(str, "url", 3) ) {
             gchar *uri = extract_uri( str, &str );
@@ -3218,33 +3215,33 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
                 ++str;
             }
             // TODO check on and comment the comparrison "paint != &style->color".
-            if ( uri && *uri && (paint != &style->color) ) {
-                paint->set = TRUE;
+            if ( uri && *uri && (this != &style.color) ) {
+                set = TRUE;
 
                 // it may be that this style's SPIPaint has not yet created its URIReference;
                 // now that we have a document, we can create it here
-                if (!paint->value.href && document) {
-                    paint->value.href = new SPPaintServerReference(document);
-                    paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
+                if (!value.href && document) {
+                    value.href = new SPPaintServerReference(document);
+                    value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((this == &style.fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), &style));
                 }
 
                 // TODO check what this does in light of move away from union
-                sp_style_set_ipaint_to_uri_string (style, paint, uri);
+                sp_style_set_ipaint_to_uri_string (&style, this, uri);
             }
             g_free( uri );
         }
 
-        if (streq(str, "currentColor") && paint != &style->color) {
-            paint->set = TRUE;
-            paint->currentcolor = TRUE;
-        } else if (streq(str, "none") && paint != &style->color) {
-            paint->set = TRUE;
-            paint->noneSet = TRUE;
+        if (streq(str, "currentColor") && (this != &style.color)) {
+            set = TRUE;
+            currentcolor = TRUE;
+        } else if (streq(str, "none") && (this != &style.color)) {
+            set = TRUE;
+            noneSet = TRUE;
         } else {
             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
             if (rgb0 != 0xff) {
-                paint->setColor( rgb0 );
-                paint->set = TRUE;
+                setColor( rgb0 );
+                set = TRUE;
 
                 while (g_ascii_isspace(*str)) {
                     ++str;
@@ -3255,18 +3252,7 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
                         delete tmp;
                         tmp = 0;
                     }
-                    paint->value.color.icc = tmp;
-                }
-                if (strneq(str, "device-gray(", 12) ||
-                    strneq(str, "device-rgb(", 11) ||
-                    strneq(str, "device-cmyk(", 12) ||
-                    strneq(str, "device-nchannel(", 16)) {
-                    SVGDeviceColor* tmp = new SVGDeviceColor();
-                    if ( ! sp_svg_read_device_color( str, &str, tmp ) ) {
-                        delete tmp;
-                        tmp = 0;
-                    }
-                    paint->value.color.device = tmp;
+                    value.color.icc = tmp;
                 }
             }
         }
@@ -3950,35 +3936,25 @@ sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
 }
 
 
-/**
- *
+/*
+ * baseline-shift is relative to parent. The only time it should
+ * not be written out is if it is zero (or not set).
  */
 static bool
-sp_baseline_shift_differ(SPIBaselineShift const *const a, SPIBaselineShift const *const b)
+sp_baseline_shift_notzero(SPIBaselineShift const *const a )
 {
-    if (a->type != b->type)
-        return true;
-    if (a->type == SP_BASELINE_SHIFT_LITERAL ) {
-        if (a->literal != b->literal)
-            return true;
-    }
-    if (a->type == SP_BASELINE_SHIFT_LENGTH) {
-        if (a->unit == SP_CSS_UNIT_EM || a->unit == SP_CSS_UNIT_EX ) {
-            if( a->value != b->value )
-                return true;
-        } else {
-            if (a->computed != b->computed)
-            return true;
+    if( a->type == SP_BASELINE_SHIFT_LITERAL ) {
+        if( a->literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
+            return false;
+        }
+    } else {
+        if( a->value == 0.0 ) {
+            return false;
         }
     }
-    if (a->type == SP_BASELINE_SHIFT_PERCENTAGE) {
-        if (a->value != b->value)
-            return true;
-    }
-    return false;
+    return true;
 }
 
-
 /**
  * Write SPIBaselineShift object into string.
  */
@@ -3990,13 +3966,13 @@ sp_style_write_ibaselineshift(gchar *p, gint const len, gchar const *key,
     if ((flags & SP_STYLE_FLAG_ALWAYS)
         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
-            && (!base->set || sp_baseline_shift_differ(val, base))))
+            && (!base->set || sp_baseline_shift_notzero(val) )))
     {
         if (val->inherit) {
             return g_snprintf(p, len, "%s:inherit;", key);
         } else if (val->type == SP_BASELINE_SHIFT_LITERAL) {
             for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
-                if (enum_baseline_shift[i].value == static_cast< gint > (val->value) ) {
+                if (enum_baseline_shift[i].value == static_cast< gint > (val->literal) ) {
                     return g_snprintf(p, len, "%s:%s;", key, enum_baseline_shift[i].key);
                 }
             }
@@ -4045,6 +4021,17 @@ sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
     return 0;
 }
 
+SPIPaint::SPIPaint() :
+    set(0),
+    inherit(0),
+    currentcolor(0),
+    colorSet(0),
+    noneSet(0),
+    value()
+{
+    value.color.set( 0 );
+    value.href = 0;
+}
 
 void SPIPaint::clear()
 {
@@ -4449,4 +4436,4 @@ css2_escape_quote(gchar const *val) {
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :