From 30c3c8cf483401fd287eef2cb5e526d814379df4 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 12 Jan 2020 17:40:58 -0500 Subject: [PATCH 1/2] Fix #581, seperate shall_write logic and style attr loop when writing xml node. --- src/object/sp-object.cpp | 9 +++++++++ src/style-internal.cpp | 9 +++++++-- src/style-internal.h | 3 +++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/object/sp-object.cpp b/src/object/sp-object.cpp index 419027228c7..3a8a6c41077 100644 --- a/src/object/sp-object.cpp +++ b/src/object/sp-object.cpp @@ -1094,6 +1094,15 @@ Inkscape::XML::Node* SPObject::write(Inkscape::XML::Document *doc, Inkscape::XML Glib::ustring s = style->write(SP_STYLE_FLAG_IFSET | SP_STYLE_FLAG_IFSRC, SP_STYLE_SRC_STYLE_PROP); + // Write style attributes (SP_STYLE_SRC_ATTRIBUTE) back to xml object + auto properties = style->properties(); + for (auto * prop : properties) { + if(prop->shall_write(SP_STYLE_FLAG_IFSET | SP_STYLE_FLAG_IFSRC, SP_STYLE_SRC_ATTRIBUTE)) { + // WARNING: We don't know for sure if the css names are the same as the attribute names + repr->setAttributeOrRemoveIfEmpty(prop->name(), prop->get_value()); + } + } + // Check for valid attributes. This may be time consuming. // It is useful, though, for debugging Inkscape code. Inkscape::Preferences *prefs = Inkscape::Preferences::get(); diff --git a/src/style-internal.cpp b/src/style-internal.cpp index f290904c65c..c46ae334bdc 100644 --- a/src/style-internal.cpp +++ b/src/style-internal.cpp @@ -78,13 +78,18 @@ inline bool should_write( guint const flags, bool set, bool dfp, bool src) { return should_write; } -const Glib::ustring SPIBase::write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const +bool SPIBase::shall_write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const { // Is this class different from the SPIBase given, this is used in Object-to-Path SPIBase const *const my_base = dynamic_cast(base); bool dfp = (!inherits || !my_base || (my_base != this)); // Different from parent bool src = (style_src_req == style_src || !(flags & SP_STYLE_FLAG_IFSRC)); - if (should_write(flags, set, dfp, src)) { + return should_write(flags, set, dfp, src); +} + +const Glib::ustring SPIBase::write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const +{ + if (shall_write(flags, style_src_req, base)) { auto value = this->get_value(); if ( !value.empty() ) { return (name() + ":" + value + important_str() + ";"); diff --git a/src/style-internal.h b/src/style-internal.h index bf1eb6d1b31..ab9eb28729e 100644 --- a/src/style-internal.h +++ b/src/style-internal.h @@ -150,6 +150,9 @@ public: } virtual const Glib::ustring get_value() const = 0; + virtual bool shall_write( guint const flags = SP_STYLE_FLAG_IFSET, + SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, + SPIBase const *const base = nullptr ) const; virtual const Glib::ustring write( guint const flags = SP_STYLE_FLAG_IFSET, SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, SPIBase const *const base = nullptr ) const; -- GitLab From 1bb3efc84635bbc7506f193118999914572a0740 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Mon, 13 Jan 2020 09:52:01 -0500 Subject: [PATCH 2/2] Add doxygen comments and shall_write is a real --- src/style-internal.cpp | 20 ++++++++++++++++++++ src/style-internal.h | 6 +++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/style-internal.cpp b/src/style-internal.cpp index c46ae334bdc..e9606ca047c 100644 --- a/src/style-internal.cpp +++ b/src/style-internal.cpp @@ -78,6 +78,16 @@ inline bool should_write( guint const flags, bool set, bool dfp, bool src) { return should_write; } +/** + * Check if this property should be written. This function won't do any writing so can + * be used as a quick way to check if specific kinds of style attrs have been changed. + * + * @param flags Conditions bitmask of `SP_STYLE_FLAG_*` bits + * @param style_src_req For `SP_STYLE_FLAG_IFSRC` + * @param base Parent node style for `SP_STYLE_FLAG_IFDIFF`, can be NULL + * + * @return True if property should be written, false otherwise + */ bool SPIBase::shall_write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const { // Is this class different from the SPIBase given, this is used in Object-to-Path @@ -87,6 +97,16 @@ bool SPIBase::shall_write(guint const flags, SPStyleSrc const &style_src_req, SP return should_write(flags, set, dfp, src); } +/** + * Compile this style conditionally into a 'name: value' string suitable for css attrs. + * see shall_write for details on conditional flags. + * + * @param flags Conditions bitmask of `SP_STYLE_FLAG_*` bits + * @param style_src_req For `SP_STYLE_FLAG_IFSRC` + * @param base Parent node style for `SP_STYLE_FLAG_IFDIFF`, can be NULL + * + * @return completed css string or empty string if conditions not met. + */ const Glib::ustring SPIBase::write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const { if (shall_write(flags, style_src_req, base)) { diff --git a/src/style-internal.h b/src/style-internal.h index ab9eb28729e..2f6a582db79 100644 --- a/src/style-internal.h +++ b/src/style-internal.h @@ -150,9 +150,9 @@ public: } virtual const Glib::ustring get_value() const = 0; - virtual bool shall_write( guint const flags = SP_STYLE_FLAG_IFSET, - SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, - SPIBase const *const base = nullptr ) const; + bool shall_write( guint const flags = SP_STYLE_FLAG_IFSET, + SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, + SPIBase const *const base = nullptr ) const; virtual const Glib::ustring write( guint const flags = SP_STYLE_FLAG_IFSET, SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, SPIBase const *const base = nullptr ) const; -- GitLab