From b0cc47554b385fb68643d07efe6e42366c7121ad Mon Sep 17 00:00:00 2001 From: Markus Engel Date: Sat, 6 Apr 2013 01:36:16 +0200 Subject: Merged PaintServer and subclasses; moved Gradient classes to own files. (bzr r11608.1.82) --- src/sp-stop.cpp | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 194 insertions(+), 1 deletion(-) (limited to 'src/sp-stop.cpp') diff --git a/src/sp-stop.cpp b/src/sp-stop.cpp index 0c0a3b03a..71d06ced9 100644 --- a/src/sp-stop.cpp +++ b/src/sp-stop.cpp @@ -17,6 +17,177 @@ #include "sp-stop.h" #include "style.h" +#include "attributes.h" +#include "streq.h" +#include "svg/svg.h" +#include "svg/svg-color.h" +#include "svg/css-ostringstream.h" +#include "xml/repr.h" + +#include "sp-factory.h" + +namespace { + SPObject* createStop() { + return new SPStop(); + } + + bool stopRegistered = SPFactory::instance().registerObject("svg:stop", createStop); +} + +SPStop::SPStop() : SPObject(), CObject(this) { + delete this->cobject; + this->cobject = this; + + this->path_string = NULL; + + this->offset = 0.0; + this->currentColor = false; + this->specified_color.set( 0x000000ff ); + this->opacity = 1.0; +} + +SPStop::~SPStop() { +} + +void SPStop::build(SPDocument* doc, Inkscape::XML::Node* repr) { + SPStop* object = this; + + CObject::build(doc, repr); + + object->readAttr( "offset" ); + object->readAttr( "stop-color" ); + object->readAttr( "stop-opacity" ); + object->readAttr( "style" ); + object->readAttr( "path" ); // For mesh +} + +/** + * Virtual build: set stop attributes from its associated XML node. + */ + +void SPStop::set(unsigned int key, const gchar* value) { + SPStop* object = this; + + SPStop *stop = SP_STOP(object); + + switch (key) { + case SP_ATTR_STYLE: { + /** \todo + * fixme: We are reading simple values 3 times during build (Lauris). + * \par + * We need presentation attributes etc. + * \par + * remove the hackish "style reading" from here: see comments in + * sp_object_get_style_property about the bugs in our current + * approach. However, note that SPStyle doesn't currently have + * stop-color and stop-opacity properties. + */ + { + gchar const *p = object->getStyleProperty( "stop-color", "black"); + if (streq(p, "currentColor")) { + stop->currentColor = true; + } else { + stop->specified_color = SPStop::readStopColor( p ); + } + } + { + gchar const *p = object->getStyleProperty( "stop-opacity", "1"); + gdouble opacity = sp_svg_read_percentage(p, stop->opacity); + stop->opacity = opacity; + } + object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + break; + } + case SP_PROP_STOP_COLOR: { + { + gchar const *p = object->getStyleProperty( "stop-color", "black"); + if (streq(p, "currentColor")) { + stop->currentColor = true; + } else { + stop->currentColor = false; + stop->specified_color = SPStop::readStopColor( p ); + } + } + object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + break; + } + case SP_PROP_STOP_OPACITY: { + { + gchar const *p = object->getStyleProperty( "stop-opacity", "1"); + gdouble opacity = sp_svg_read_percentage(p, stop->opacity); + stop->opacity = opacity; + } + object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + break; + } + case SP_ATTR_OFFSET: { + stop->offset = sp_svg_read_percentage(value, 0.0); + object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + break; + } + case SP_PROP_STOP_PATH: { + if (value) { + stop->path_string = new Glib::ustring( value ); + //Geom::PathVector pv = sp_svg_read_pathv(value); + //SPCurve *curve = new SPCurve(pv); + //if( curve ) { + // std::cout << "Got Curve" << std::endl; + //curve->unref(); + //} + } + break; + } + default: { + CObject::set(key, value); + break; + } + } +} + +/** + * Virtual set: set attribute to value. + */ + +Inkscape::XML::Node* SPStop::write(Inkscape::XML::Document* xml_doc, Inkscape::XML::Node* repr, guint flags) { + SPStop* object = this; + + SPStop *stop = SP_STOP(object); + + if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { + repr = xml_doc->createElement("svg:stop"); + } + + Glib::ustring colorStr = stop->specified_color.toString(); + gfloat opacity = stop->opacity; + + CObject::write(xml_doc, repr, flags); + + // Since we do a hackish style setting here (because SPStyle does not support stop-color and + // stop-opacity), we must do it AFTER calling the parent write method; otherwise + // sp_object_write would clear our style= attribute (bug 1695287) + + Inkscape::CSSOStringStream os; + os << "stop-color:"; + if (stop->currentColor) { + os << "currentColor"; + } else { + os << colorStr; + } + os << ";stop-opacity:" << opacity; + repr->setAttribute("style", os.str().c_str()); + repr->setAttribute("stop-color", NULL); + repr->setAttribute("stop-opacity", NULL); + sp_repr_set_css_double(repr, "offset", stop->offset); + /* strictly speaking, offset an SVG rather than a CSS one, but exponents make no sense + * for offset proportions. */ + + return repr; +} + +/** + * Virtual write: write object attributes to repr. + */ + // A stop might have some non-stop siblings SPStop* SPStop::getNextStop() { @@ -80,7 +251,29 @@ SPColor SPStop::getEffectiveColor() const return ret; } - +/** + * Return stop's color as 32bit value. + */ +guint32 +sp_stop_get_rgba32(SPStop const *const stop) +{ + guint32 rgb0 = 0; + /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial + * value depends on user agent, and don't give any further restrictions that I can + * see.) */ + if (stop->currentColor) { + char const *str = stop->getStyleProperty( "color", NULL); + if (str) { + rgb0 = sp_svg_read_color(str, rgb0); + } + unsigned const alpha = static_cast(stop->opacity * 0xff + 0.5); + g_return_val_if_fail((alpha & ~0xff) == 0, + rgb0 | 0xff); + return rgb0 | alpha; + } else { + return stop->specified_color.toRGBA32( stop->opacity ); + } +} /* Local Variables: -- cgit v1.2.3 From 27e2102f96a5554bcd5310ec11435d155773b279 Mon Sep 17 00:00:00 2001 From: Markus Engel Date: Sun, 7 Apr 2013 18:28:22 +0200 Subject: Merge Object and subclasses. Merging of SP- and C-classes complete. (bzr r11608.1.86) --- src/sp-stop.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/sp-stop.cpp') diff --git a/src/sp-stop.cpp b/src/sp-stop.cpp index 71d06ced9..a8d81818b 100644 --- a/src/sp-stop.cpp +++ b/src/sp-stop.cpp @@ -34,10 +34,7 @@ namespace { bool stopRegistered = SPFactory::instance().registerObject("svg:stop", createStop); } -SPStop::SPStop() : SPObject(), CObject(this) { - delete this->cobject; - this->cobject = this; - +SPStop::SPStop() : SPObject() { this->path_string = NULL; this->offset = 0.0; @@ -52,7 +49,7 @@ SPStop::~SPStop() { void SPStop::build(SPDocument* doc, Inkscape::XML::Node* repr) { SPStop* object = this; - CObject::build(doc, repr); + SPObject::build(doc, repr); object->readAttr( "offset" ); object->readAttr( "stop-color" ); @@ -138,7 +135,7 @@ void SPStop::set(unsigned int key, const gchar* value) { break; } default: { - CObject::set(key, value); + SPObject::set(key, value); break; } } @@ -160,7 +157,7 @@ Inkscape::XML::Node* SPStop::write(Inkscape::XML::Document* xml_doc, Inkscape::X Glib::ustring colorStr = stop->specified_color.toString(); gfloat opacity = stop->opacity; - CObject::write(xml_doc, repr, flags); + SPObject::write(xml_doc, repr, flags); // Since we do a hackish style setting here (because SPStyle does not support stop-color and // stop-opacity), we must do it AFTER calling the parent write method; otherwise -- cgit v1.2.3 From bf4a1d2d49850170b936c30cfe2b30e798716406 Mon Sep 17 00:00:00 2001 From: Markus Engel Date: Sat, 3 Aug 2013 03:03:43 +0200 Subject: Cleaned up. (bzr r11608.1.117) --- src/sp-stop.cpp | 103 ++++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 55 deletions(-) (limited to 'src/sp-stop.cpp') diff --git a/src/sp-stop.cpp b/src/sp-stop.cpp index a8d81818b..d644a9b4b 100644 --- a/src/sp-stop.cpp +++ b/src/sp-stop.cpp @@ -47,15 +47,13 @@ SPStop::~SPStop() { } void SPStop::build(SPDocument* doc, Inkscape::XML::Node* repr) { - SPStop* object = this; - SPObject::build(doc, repr); - object->readAttr( "offset" ); - object->readAttr( "stop-color" ); - object->readAttr( "stop-opacity" ); - object->readAttr( "style" ); - object->readAttr( "path" ); // For mesh + this->readAttr( "offset" ); + this->readAttr( "stop-color" ); + this->readAttr( "stop-opacity" ); + this->readAttr( "style" ); + this->readAttr( "path" ); // For mesh } /** @@ -63,10 +61,6 @@ void SPStop::build(SPDocument* doc, Inkscape::XML::Node* repr) { */ void SPStop::set(unsigned int key, const gchar* value) { - SPStop* object = this; - - SPStop *stop = SP_STOP(object); - switch (key) { case SP_ATTR_STYLE: { /** \todo @@ -80,51 +74,51 @@ void SPStop::set(unsigned int key, const gchar* value) { * stop-color and stop-opacity properties. */ { - gchar const *p = object->getStyleProperty( "stop-color", "black"); + gchar const *p = this->getStyleProperty( "stop-color", "black"); if (streq(p, "currentColor")) { - stop->currentColor = true; + this->currentColor = true; } else { - stop->specified_color = SPStop::readStopColor( p ); + this->specified_color = SPStop::readStopColor( p ); } } { - gchar const *p = object->getStyleProperty( "stop-opacity", "1"); - gdouble opacity = sp_svg_read_percentage(p, stop->opacity); - stop->opacity = opacity; + gchar const *p = this->getStyleProperty( "stop-opacity", "1"); + gdouble opacity = sp_svg_read_percentage(p, this->opacity); + this->opacity = opacity; } - object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + this->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); break; } case SP_PROP_STOP_COLOR: { { - gchar const *p = object->getStyleProperty( "stop-color", "black"); + gchar const *p = this->getStyleProperty( "stop-color", "black"); if (streq(p, "currentColor")) { - stop->currentColor = true; + this->currentColor = true; } else { - stop->currentColor = false; - stop->specified_color = SPStop::readStopColor( p ); + this->currentColor = false; + this->specified_color = SPStop::readStopColor( p ); } } - object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + this->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); break; } case SP_PROP_STOP_OPACITY: { { - gchar const *p = object->getStyleProperty( "stop-opacity", "1"); - gdouble opacity = sp_svg_read_percentage(p, stop->opacity); - stop->opacity = opacity; + gchar const *p = this->getStyleProperty( "stop-opacity", "1"); + gdouble opacity = sp_svg_read_percentage(p, this->opacity); + this->opacity = opacity; } - object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + this->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); break; } case SP_ATTR_OFFSET: { - stop->offset = sp_svg_read_percentage(value, 0.0); - object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + this->offset = sp_svg_read_percentage(value, 0.0); + this->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); break; } case SP_PROP_STOP_PATH: { if (value) { - stop->path_string = new Glib::ustring( value ); + this->path_string = new Glib::ustring( value ); //Geom::PathVector pv = sp_svg_read_pathv(value); //SPCurve *curve = new SPCurve(pv); //if( curve ) { @@ -146,16 +140,12 @@ void SPStop::set(unsigned int key, const gchar* value) { */ Inkscape::XML::Node* SPStop::write(Inkscape::XML::Document* xml_doc, Inkscape::XML::Node* repr, guint flags) { - SPStop* object = this; - - SPStop *stop = SP_STOP(object); - if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { repr = xml_doc->createElement("svg:stop"); } - Glib::ustring colorStr = stop->specified_color.toString(); - gfloat opacity = stop->opacity; + Glib::ustring colorStr = this->specified_color.toString(); + gfloat opacity = this->opacity; SPObject::write(xml_doc, repr, flags); @@ -165,7 +155,7 @@ Inkscape::XML::Node* SPStop::write(Inkscape::XML::Document* xml_doc, Inkscape::X Inkscape::CSSOStringStream os; os << "stop-color:"; - if (stop->currentColor) { + if (this->currentColor) { os << "currentColor"; } else { os << colorStr; @@ -174,7 +164,7 @@ Inkscape::XML::Node* SPStop::write(Inkscape::XML::Document* xml_doc, Inkscape::X repr->setAttribute("style", os.str().c_str()); repr->setAttribute("stop-color", NULL); repr->setAttribute("stop-opacity", NULL); - sp_repr_set_css_double(repr, "offset", stop->offset); + sp_repr_set_css_double(repr, "offset", this->offset); /* strictly speaking, offset an SVG rather than a CSS one, but exponents make no sense * for offset proportions. */ @@ -186,8 +176,7 @@ Inkscape::XML::Node* SPStop::write(Inkscape::XML::Document* xml_doc, Inkscape::X */ // A stop might have some non-stop siblings -SPStop* SPStop::getNextStop() -{ +SPStop* SPStop::getNextStop() { SPStop *result = 0; for (SPObject* obj = getNext(); obj && !result; obj = obj->getNext()) { @@ -199,8 +188,7 @@ SPStop* SPStop::getNextStop() return result; } -SPStop* SPStop::getPrevStop() -{ +SPStop* SPStop::getPrevStop() { SPStop *result = 0; for (SPObject* obj = getPrev(); obj; obj = obj->getPrev()) { @@ -220,22 +208,24 @@ SPStop* SPStop::getPrevStop() return result; } -SPColor SPStop::readStopColor( Glib::ustring const &styleStr, guint32 dfl ) -{ +SPColor SPStop::readStopColor(Glib::ustring const &styleStr, guint32 dfl) { SPColor color(dfl); SPStyle* style = sp_style_new(0); SPIPaint paint; paint.read( styleStr.c_str(), *style ); + if ( paint.isColor() ) { color = paint.value.color; } + sp_style_unref(style); + return color; } -SPColor SPStop::getEffectiveColor() const -{ +SPColor SPStop::getEffectiveColor() const { SPColor ret; + if (currentColor) { char const *str = getStyleProperty("color", NULL); /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial @@ -245,30 +235,33 @@ SPColor SPStop::getEffectiveColor() const } else { ret = specified_color; } + return ret; } /** * Return stop's color as 32bit value. */ -guint32 -sp_stop_get_rgba32(SPStop const *const stop) -{ +guint32 SPStop::get_rgba32() const { guint32 rgb0 = 0; + /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial * value depends on user agent, and don't give any further restrictions that I can * see.) */ - if (stop->currentColor) { - char const *str = stop->getStyleProperty( "color", NULL); + if (this->currentColor) { + char const *str = this->getStyleProperty("color", NULL); + if (str) { rgb0 = sp_svg_read_color(str, rgb0); } - unsigned const alpha = static_cast(stop->opacity * 0xff + 0.5); - g_return_val_if_fail((alpha & ~0xff) == 0, - rgb0 | 0xff); + + unsigned const alpha = static_cast(this->opacity * 0xff + 0.5); + + g_return_val_if_fail((alpha & ~0xff) == 0, rgb0 | 0xff); + return rgb0 | alpha; } else { - return stop->specified_color.toRGBA32( stop->opacity ); + return this->specified_color.toRGBA32(this->opacity); } } -- cgit v1.2.3