From 35301e418f34ce11cfed9c11a7f8a923faf48cf0 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Mon, 29 Aug 2011 20:30:39 +0200 Subject: Added comments. (bzr r10596) --- src/xml/repr-css.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 5 deletions(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index dc6494bcd..5c8c6bf4b 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -1,6 +1,21 @@ /* * bulia byak -*/ + * Tavmjong Bah (Documentation) + * + * Functions to manipulate SPCSSAttr which is a class derived from Inkscape::XML::Node See + * sp-css-attr.h and node.h + * + * SPCSSAttr is a special node type where the "attributes" are the properties in an element's style + * attribute. For example, style="fill:blue;stroke:none" is stored in a List (Inkscape::Util:List) + * where the key is the property (e.g. "fill" or "stroke") and the value is the property's value + * (e.g. "blue" or "none"). An element's properties are manipulated by adding, removing, or + * changing an item in the List. Utility functions are provided to go back and forth between the + * two ways of representing properties (by a string or by a list). + * + * Use sp_repr_write_string to go from a property list to a style string. + * + * Use sp_repr_css_add_component to parse a property string and add the properties to the List. + */ #define SP_REPR_CSS_C @@ -35,7 +50,9 @@ protected: static void sp_repr_css_add_components(SPCSSAttr *css, Node *repr, gchar const *attr); - +/** + * Creates an empty SPCSSAttr (a class for manipulating CSS style properties). + */ SPCSSAttr * sp_repr_css_attr_new() { @@ -46,6 +63,9 @@ sp_repr_css_attr_new() return new SPCSSAttrImpl(attr_doc); } +/** + * Unreferences an SPCSSAttr (will be garbage collected if no references remain). + */ void sp_repr_css_attr_unref(SPCSSAttr *css) { @@ -53,6 +73,12 @@ sp_repr_css_attr_unref(SPCSSAttr *css) Inkscape::GC::release((Node *) css); } +/** + * Creates a new SPCSSAttr with one attribute (i.e. style) copied from an existing repr (node). The + * repr attribute data is in the form of a char const * string (e.g. fill:#00ff00;stroke:none). The + * string is parsed by libcroco which returns a CRDeclaration list (a typical C linked list) of + * properties and values. This list is then used to fill the attributes of the new SPCSSAttr. + */ SPCSSAttr *sp_repr_css_attr(Node *repr, gchar const *attr) { g_assert(repr != NULL); @@ -63,6 +89,9 @@ SPCSSAttr *sp_repr_css_attr(Node *repr, gchar const *attr) return css; } +/** + * Adds an attribute to an existing SPCSAttr with the cascaded value including all parents. + */ static void sp_repr_css_attr_inherited_recursive(SPCSSAttr *css, Node *repr, gchar const *attr) { @@ -72,11 +101,12 @@ sp_repr_css_attr_inherited_recursive(SPCSSAttr *css, Node *repr, gchar const *at if (parent) { sp_repr_css_attr_inherited_recursive(css, parent, attr); } - sp_repr_css_add_components(css, repr, attr); } - +/** + * Creates a new SPCSSAttr with one attribute whose value is determined by cascading. + */ SPCSSAttr *sp_repr_css_attr_inherited(Node *repr, gchar const *attr) { g_assert(repr != NULL); @@ -89,6 +119,9 @@ SPCSSAttr *sp_repr_css_attr_inherited(Node *repr, gchar const *attr) return css; } +/** + * Adds components (style properties) to an existing SPCSAttr from a character string. + */ static void sp_repr_css_add_components(SPCSSAttr *css, Node *repr, gchar const *attr) { @@ -100,6 +133,10 @@ sp_repr_css_add_components(SPCSSAttr *css, Node *repr, gchar const *attr) sp_repr_css_attr_add_from_string(css, data); } +/** + * Returns a character string of the value of a given style property or a default value if the + * attribute is not found. + */ char const * sp_repr_css_property(SPCSSAttr *css, gchar const *name, gchar const *defval) { @@ -112,6 +149,9 @@ sp_repr_css_property(SPCSSAttr *css, gchar const *name, gchar const *defval) : attr ); } +/** + * Returns true if a style property is present and its value is unset. + */ bool sp_repr_css_property_is_unset(SPCSSAttr *css, gchar const *name) { @@ -123,6 +163,9 @@ sp_repr_css_property_is_unset(SPCSSAttr *css, gchar const *name) } +/** + * Set a style property to a new value (e.g. fill to #ffff00). + */ void sp_repr_css_set_property(SPCSSAttr *css, gchar const *name, gchar const *value) { @@ -132,6 +175,9 @@ sp_repr_css_set_property(SPCSSAttr *css, gchar const *name, gchar const *value) ((Node *) css)->setAttribute(name, value, false); } +/** + * Set a style property to "inkscape:unset". + */ void sp_repr_css_unset_property(SPCSSAttr *css, gchar const *name) { @@ -141,6 +187,9 @@ sp_repr_css_unset_property(SPCSSAttr *css, gchar const *name) ((Node *) css)->setAttribute(name, "inkscape:unset", false); } +/** + * Return the value of a style property if property define, or a default value if not. + */ double sp_repr_css_double_property(SPCSSAttr *css, gchar const *name, double defval) { @@ -150,6 +199,9 @@ sp_repr_css_double_property(SPCSSAttr *css, gchar const *name, double defval) return sp_repr_get_double_attribute((Node *) css, name, defval); } +/** + * Write a style attribute string from a list of properties stored in an SPCSAttr object. + */ gchar * sp_repr_css_write_string(SPCSSAttr *css) { @@ -186,6 +238,9 @@ sp_repr_css_write_string(SPCSSAttr *css) return (buffer.empty() ? NULL : g_strdup (buffer.c_str())); } +/** + * Sets an attribute (e.g. style) to a string created from a list of style properties. + */ void sp_repr_css_set(Node *repr, SPCSSAttr *css, gchar const *attr) { @@ -200,6 +255,9 @@ sp_repr_css_set(Node *repr, SPCSSAttr *css, gchar const *attr) if (value) g_free (value); } +/** + * Loops through a List of style properties, printing key/value pairs. + */ void sp_repr_css_print(SPCSSAttr *css) { @@ -212,6 +270,9 @@ sp_repr_css_print(SPCSSAttr *css) } } +/** + * Merges two SPCSSAttr's. Properties in src overwrite properties in dst if present in both. + */ void sp_repr_css_merge(SPCSSAttr *dst, SPCSSAttr *src) { @@ -219,9 +280,12 @@ sp_repr_css_merge(SPCSSAttr *dst, SPCSSAttr *src) g_assert(src != NULL); dst->mergeFrom(src, ""); + sp_repr_css_print( dst ); } - +/** + * Merges style properties as parsed by libcroco into an existing SPCSSAttr. + */ static void sp_repr_css_merge_from_decl(SPCSSAttr *css, CRDeclaration const *const decl) { @@ -234,6 +298,8 @@ sp_repr_css_merge_from_decl(SPCSSAttr *css, CRDeclaration const *const decl) } /** + * Merges style properties as parsed by libcroco into an existing SPCSSAttr. + * * \pre decl_list != NULL */ static void @@ -248,6 +314,10 @@ sp_repr_css_merge_from_decl_list(SPCSSAttr *css, CRDeclaration const *const decl } } +/** + * Use libcroco to parse a string for CSS properties and then merge + * them into an existing SPCSSAttr. + */ void sp_repr_css_attr_add_from_string(SPCSSAttr *css, gchar const *p) { @@ -261,6 +331,10 @@ sp_repr_css_attr_add_from_string(SPCSSAttr *css, gchar const *p) } } +/** + * Creates a new SPCSAttr with the values filled from a repr, merges in properties from the given + * SPCSAttr, and then replaces the that SPCSAttr with the new one. + */ void sp_repr_css_change(Node *repr, SPCSSAttr *css, gchar const *attr) { -- cgit v1.2.3 From f5e85edb29a2e69721ddd2121b649a2402bed994 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Mon, 29 Aug 2011 20:33:41 +0200 Subject: Remove forgotten call to print routine. (bzr r10597) --- src/xml/repr-css.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index 5c8c6bf4b..46a16715c 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -280,7 +280,6 @@ sp_repr_css_merge(SPCSSAttr *dst, SPCSSAttr *src) g_assert(src != NULL); dst->mergeFrom(src, ""); - sp_repr_css_print( dst ); } /** -- cgit v1.2.3 From 56c911746539f515dcf162c5d134abf76557a287 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Thu, 15 Sep 2011 21:08:22 +0200 Subject: Use CSSOStringStream in writing number strings parsed by libcroco as libcroco uses %.17f for formatting, resulting in trailing zeros or small rounding errors. (bzr r10629) --- src/xml/repr-css.cpp | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index 46a16715c..cb30e65ce 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -14,13 +14,15 @@ * * Use sp_repr_write_string to go from a property list to a style string. * - * Use sp_repr_css_add_component to parse a property string and add the properties to the List. */ #define SP_REPR_CSS_C #include +#include +#include #include +#include "svg/css-ostringstream.h" #include "xml/repr.h" #include "xml/simple-document.h" @@ -120,7 +122,9 @@ SPCSSAttr *sp_repr_css_attr_inherited(Node *repr, gchar const *attr) } /** - * Adds components (style properties) to an existing SPCSAttr from a character string. + * Adds components (style properties) to an existing SPCSAttr from the specified attribute's data + * (nominally a style attribute). + * */ static void sp_repr_css_add_components(SPCSSAttr *css, Node *repr, gchar const *attr) @@ -228,6 +232,7 @@ sp_repr_css_write_string(SPCSSAttr *css) } } else { buffer.append(iter->value); // unquoted + g_warning("sp_repr_css_write_string: %s %s", g_quark_to_string(iter->key), iter->value ); } if (rest(iter)) { @@ -250,6 +255,12 @@ sp_repr_css_set(Node *repr, SPCSSAttr *css, gchar const *attr) gchar *value = sp_repr_css_write_string(css); + /* + * If the new value is different from the old value, this will sometimes send a signal via + * CompositeNodeObserver::notiftyAttributeChanged() which results in calling + * SPObject::sp_object_repr_attr_changed and thus updates the object's SPStyle. This update + * results in another call to repr->setAttribute(). + */ repr->setAttribute(attr, value); if (value) g_free (value); @@ -291,7 +302,22 @@ sp_repr_css_merge_from_decl(SPCSSAttr *css, CRDeclaration const *const decl) guchar *const str_value_unsigned = cr_term_to_string(decl->value); gchar *const str_value = reinterpret_cast(str_value_unsigned); gchar *value_unquoted = attribute_unquote (str_value); // libcroco returns strings quoted in "" - ((Node *) css)->setAttribute(decl->property->stryng->str, value_unquoted, false); + + // libcroco uses %.17f for formatting... leading to trailing zeros or small rounding errors. + // CSSOStringStream is used here to write valid CSS (as in sp_style_write_string). This has + // the additional benefit of respecting the numerical precission set in the SVG Output + // preferences. We assume any numerical part comes first (if not, the whole string is copied). + std::stringstream ss( value_unquoted ); + double number; + std::string characters; + bool number_valid = !(ss >> number).fail(); + if( !number_valid ) ss.clear(); + bool character_valid = !(ss >> characters).fail(); + Inkscape::CSSOStringStream os; + if( number_valid ) os << number; + if( character_valid ) os << characters; + + ((Node *) css)->setAttribute(decl->property->stryng->str, os.str().c_str(), false); g_free(value_unquoted); g_free(str_value); } @@ -332,7 +358,8 @@ sp_repr_css_attr_add_from_string(SPCSSAttr *css, gchar const *p) /** * Creates a new SPCSAttr with the values filled from a repr, merges in properties from the given - * SPCSAttr, and then replaces the that SPCSAttr with the new one. + * SPCSAttr, and then replaces that SPCSAttr with the new one. This is called, for example, for + * each object in turn when a selection's style is updated via sp_desktop_set_style(). */ void sp_repr_css_change(Node *repr, SPCSSAttr *css, gchar const *attr) -- cgit v1.2.3 From f21461e44a12ad13d86c125b095a8793e24dffda Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Fri, 16 Sep 2011 01:47:40 +0200 Subject: Fix incorrect argument in call to varargs function in xml/repr-css.cpp (bzr r10630) --- src/xml/repr-css.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index cb30e65ce..8e8042dfd 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -232,7 +232,7 @@ sp_repr_css_write_string(SPCSSAttr *css) } } else { buffer.append(iter->value); // unquoted - g_warning("sp_repr_css_write_string: %s %s", g_quark_to_string(iter->key), iter->value ); + g_warning("sp_repr_css_write_string: %s %s", g_quark_to_string(iter->key), iter->value.pointer() ); } if (rest(iter)) { -- cgit v1.2.3 From 043e682872b382312e0dc58c197ce452b1cf6766 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Fri, 16 Sep 2011 09:12:03 +0200 Subject: Remove left over debug g_warning... and the cause of compilation problems. (bzr r10631) --- src/xml/repr-css.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index 8e8042dfd..7db1e8b86 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -232,7 +232,6 @@ sp_repr_css_write_string(SPCSSAttr *css) } } else { buffer.append(iter->value); // unquoted - g_warning("sp_repr_css_write_string: %s %s", g_quark_to_string(iter->key), iter->value.pointer() ); } if (rest(iter)) { @@ -316,7 +315,7 @@ sp_repr_css_merge_from_decl(SPCSSAttr *css, CRDeclaration const *const decl) Inkscape::CSSOStringStream os; if( number_valid ) os << number; if( character_valid ) os << characters; - + ((Node *) css)->setAttribute(decl->property->stryng->str, os.str().c_str(), false); g_free(value_unquoted); g_free(str_value); -- cgit v1.2.3 From 278bc7c50017df7bf9ae28e639c3aecc072fa3df Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 28 Sep 2011 15:18:38 +0200 Subject: Fixed problem with font names that contain spaces. (bzr r10649) --- src/xml/repr-css.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index 7db1e8b86..a0b45a42e 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -232,6 +232,7 @@ sp_repr_css_write_string(SPCSSAttr *css) } } else { buffer.append(iter->value); // unquoted + g_warning("sp_repr_css_write_string: %s %s", g_quark_to_string(iter->key), iter->value.pointer() ); } if (rest(iter)) { @@ -309,13 +310,17 @@ sp_repr_css_merge_from_decl(SPCSSAttr *css, CRDeclaration const *const decl) std::stringstream ss( value_unquoted ); double number; std::string characters; + std::string temp; bool number_valid = !(ss >> number).fail(); if( !number_valid ) ss.clear(); - bool character_valid = !(ss >> characters).fail(); + while( !(ss >> temp).eof() ) { + characters += temp; + characters += " "; + } + characters += temp; Inkscape::CSSOStringStream os; if( number_valid ) os << number; - if( character_valid ) os << characters; - + os << characters; ((Node *) css)->setAttribute(decl->property->stryng->str, os.str().c_str(), false); g_free(value_unquoted); g_free(str_value); -- cgit v1.2.3 From a87e24fa2d1c86e85444e177a3c3156f4d630c3f Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Sun, 2 Oct 2011 15:40:19 +0200 Subject: Removed forgotten debug statement. (bzr r10657) --- src/xml/repr-css.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index a0b45a42e..8de85c36d 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -232,7 +232,6 @@ sp_repr_css_write_string(SPCSSAttr *css) } } else { buffer.append(iter->value); // unquoted - g_warning("sp_repr_css_write_string: %s %s", g_quark_to_string(iter->key), iter->value.pointer() ); } if (rest(iter)) { -- cgit v1.2.3 From 57558641a9819e4da97bc014ac35f9323306ae1f Mon Sep 17 00:00:00 2001 From: Kris De Gussem Date: Thu, 10 Nov 2011 23:23:06 +0100 Subject: cppcheck: initialization / warning cleanup (bzr r10736) --- src/xml/repr-css.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/xml/repr-css.cpp') diff --git a/src/xml/repr-css.cpp b/src/xml/repr-css.cpp index 8de85c36d..ced4f5da4 100644 --- a/src/xml/repr-css.cpp +++ b/src/xml/repr-css.cpp @@ -307,7 +307,7 @@ sp_repr_css_merge_from_decl(SPCSSAttr *css, CRDeclaration const *const decl) // the additional benefit of respecting the numerical precission set in the SVG Output // preferences. We assume any numerical part comes first (if not, the whole string is copied). std::stringstream ss( value_unquoted ); - double number; + double number = 0; std::string characters; std::string temp; bool number_valid = !(ss >> number).fail(); -- cgit v1.2.3