index f55694fcf05a29e5539209bcb9eb292331dd5fd3..afbf1600944cbaef69ff4e1ab7461e8a9d860cfd 100644 (file)
#include "sp-text.h"
#include "sp-tspan.h"
#include <libnr/nr-matrix-fns.h>
+#include <libnr/nr-point-fns.h>
#include "xml/repr.h"
#include "svg/svg.h"
#include "svg/svg-color.h"
+#include "svg/css-ostringstream.h"
+#include "prefs-utils.h"
// Terminology:
/* If gr hrefs some other gradient, remove the href */
if (gr->ref->getObject()) {
/* We are hrefing someone, so require flattening */
- SP_OBJECT(gr)->updateRepr(((SPObject *) gr)->repr, SP_OBJECT_WRITE_EXT | SP_OBJECT_WRITE_ALL);
+ SP_OBJECT(gr)->updateRepr(SP_OBJECT_WRITE_EXT | SP_OBJECT_WRITE_ALL);
sp_gradient_repr_set_link(SP_OBJECT_REPR(gr), NULL);
}
SPStyle *style = SP_OBJECT_STYLE(o);
if (style
- && style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+ && style->fill.isPaintserver()
&& SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style))
&& SP_GRADIENT(SP_STYLE_FILL_SERVER(style)) == gr)
{
i ++;
}
if (style
- && style->stroke.type == SP_PAINT_TYPE_PAINTSERVER
+ && style->stroke.isPaintserver()
&& SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style))
&& SP_GRADIENT(SP_STYLE_STROKE_SERVER(style)) == gr)
{
SPGradient *
sp_gradient_fork_vector_if_necessary (SPGradient *gr)
{
+ // Some people actually prefer their gradient vectors to be shared...
+ if (prefs_get_int_attribute("options.forkgradientvectors", "value", 1) == 0)
+ return gr;
+
if (SP_OBJECT_HREFCOUNT(gr) > 1) {
- SPDocument *document = SP_OBJECT_DOCUMENT(gr);
+ SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
+ Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
- Inkscape::XML::Node *repr = SP_OBJECT_REPR (gr)->duplicate();
- SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document))->addChild(repr, NULL);
- SPGradient *gr_new = (SPGradient *) document->getObjectByRepr(repr);
+ Inkscape::XML::Node *repr = SP_OBJECT_REPR (gr)->duplicate(xml_doc);
+ SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
+ SPGradient *gr_new = (SPGradient *) doc->getObjectByRepr(repr);
gr_new = sp_gradient_ensure_vector_normalized (gr_new);
Inkscape::GC::release(repr);
return gr_new;
return gr;
}
+/**
+ * Obtain the vector from the gradient. A forked vector will be created and linked to this gradient if another gradient uses it.
+ */
+SPGradient *
+sp_gradient_get_forked_vector_if_necessary(SPGradient *gradient, bool force_vector)
+{
+ SPGradient *vector = sp_gradient_get_vector (gradient, force_vector);
+ vector = sp_gradient_fork_vector_if_necessary (vector);
+ if ( gradient != vector && gradient->ref->getObject() != vector ) {
+ sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
+ }
+ return vector;
+}
+
+
/**
* Convert an item's gradient to userspace _without_ preserving coords, setting them to defaults
* instead. No forking or reapplying is done because this is only called for newly created privates.
// calculate the bbox of the item
sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
- NR::Rect const bbox = item->invokeBbox(NR::identity()); // we need "true" bbox without item_i2d_affine
+ boost::optional<NR::Rect> bbox = item->getBounds(NR::identity()); // we need "true" bbox without item_i2d_affine
- NR::Coord const width = bbox.dimensions()[NR::X];
- NR::Coord const height = bbox.dimensions()[NR::Y];
- g_assert(width > 0 && height > 0);
+ if ( !bbox || bbox->isEmpty() )
+ return gr;
- NR::Point const center = bbox.midpoint();
+ NR::Coord const width = bbox->dimensions()[NR::X];
+ NR::Coord const height = bbox->dimensions()[NR::Y];
+
+ NR::Point const center = bbox->midpoint();
if (SP_IS_RADIALGRADIENT(gr)) {
sp_repr_set_svg_double(repr, "cx", center[NR::X]);
@@ -316,10 +341,16 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
// calculate the bbox of the item
sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
- NR::Rect const bbox = item->invokeBbox(NR::identity()); // we need "true" bbox without item_i2d_affine
- NR::Matrix bbox2user(bbox.dimensions()[NR::X], 0,
- 0, bbox.dimensions()[NR::Y],
- bbox.min()[NR::X], bbox.min()[NR::Y]);
+ NR::Matrix bbox2user;
+ boost::optional<NR::Rect> bbox = item->getBounds(NR::identity()); // we need "true" bbox without item_i2d_affine
+ if ( bbox && !bbox->isEmpty() ) {
+ bbox2user = NR::Matrix(bbox->dimensions()[NR::X], 0,
+ 0, bbox->dimensions()[NR::Y],
+ bbox->min()[NR::X], bbox->min()[NR::Y]);
+ } else {
+ // would be degenerate otherwise
+ bbox2user = NR::identity();
+ }
/* skew is the additional transform, defined by the proportions of the item, that we need
* to apply to the gradient in order to work around this weird bit from SVG 1.1
@@ -336,7 +367,7 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
* transformation from bounding box space to user space.
*/
NR::Matrix skew = bbox2user;
- double exp = skew.expansion();
+ double exp = NR::expansion(skew);
skew[0] /= exp;
skew[1] /= exp;
skew[2] /= exp;
@@ -367,7 +398,7 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
// converted points in userspace coords
NR::Point c_u = c_b * point_convert;
NR::Point f_u = f_b * point_convert;
- double r_u = r_b * point_convert.expansion();
+ double r_u = r_b * NR::expansion(point_convert);
sp_repr_set_svg_double(repr, "cx", c_u[NR::X]);
sp_repr_set_svg_double(repr, "cy", c_u[NR::Y]);
SPGradient *gradient = NULL;
if (fill_or_stroke) {
- if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
+ if (style && (style->fill.isPaintserver())) {
SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
if (SP_IS_GRADIENT (server)) {
gradient = SP_GRADIENT (server);
}
}
} else {
- if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
+ if (style && (style->stroke.isPaintserver())) {
SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
if (SP_IS_GRADIENT (server)) {
gradient = SP_GRADIENT (server);
SPStop*
sp_next_stop(SPStop *stop)
{
- for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
- if (SP_IS_STOP (ochild))
- return SP_STOP(ochild);
- }
- return NULL;
+ for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
+ if (SP_IS_STOP (ochild))
+ return SP_STOP(ochild);
+ }
+ return NULL;
}
SPStop*
{
for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
if (sp_next_stop (stop) == NULL)
- return stop;
- }
- return NULL;
+ return stop;
+ }
+ return NULL;
+}
+
+guint
+sp_number_of_stops(SPGradient *gradient)
+{
+ guint n = 0;
+ for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
+ if (sp_next_stop (stop) == NULL)
+ return n;
+ n ++;
+ }
+ return n;
+}
+
+guint
+sp_number_of_stops_before_stop(SPGradient *gradient, SPStop *target)
+{
+ guint n = 0;
+ for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
+ if (stop == target)
+ return n;
+ n ++;
+ }
+ return n;
}
+
SPStop*
sp_get_stop_i(SPGradient *gradient, guint stop_i)
{
- SPStop *stop = sp_first_stop (gradient);
+ SPStop *stop = sp_first_stop (gradient);
+
+ // if this is valid but weird gradient without an offset-zero stop element,
+ // inkscape has created a handle for the start of gradient anyway,
+ // so when it asks for stop N that corresponds to stop element N-1
+ if (stop->offset != 0)
+ stop_i --;
- for (guint i=0; i < stop_i; i++) {
- if (!stop) return NULL;
- stop = sp_next_stop (stop);
- }
+ for (guint i=0; i < stop_i; i++) {
+ if (!stop) return NULL;
+ stop = sp_next_stop (stop);
+ }
- return stop;
+ return stop;
}
+guint32
+average_color (guint32 c1, guint32 c2, gdouble p)
+{
+ guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
+ guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
+ guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
+ guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
+
+ return SP_RGBA32_U_COMPOSE (r, g, b, a);
+}
+
+SPStop *
+sp_vector_add_stop (SPGradient *vector, SPStop* prev_stop, SPStop* next_stop, gfloat offset)
+{
+ Inkscape::XML::Node *new_stop_repr = NULL;
+ new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
+ SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
+
+ SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
+ newstop->offset = offset;
+ sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
+ guint32 const c1 = sp_stop_get_rgba32(prev_stop);
+ guint32 const c2 = sp_stop_get_rgba32(next_stop);
+ guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
+ Inkscape::CSSOStringStream os;
+ gchar c[64];
+ sp_svg_write_color (c, sizeof(c), cnew);
+ gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
+ os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
+ SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
+ Inkscape::GC::release(new_stop_repr);
+
+ return newstop;
+}
void
sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
GSList *child_copies = NULL;
for (GSList *i = child_reprs; i != NULL; i = i->next) {
Inkscape::XML::Node *repr = (Inkscape::XML::Node *) i->data;
- child_copies = g_slist_append (child_copies, repr->duplicate());
+ Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(vector)->document();
+ child_copies = g_slist_append (child_copies, repr->duplicate(xml_doc));
}
}
-// FIXME: make general global function
-static double
-get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
-{
- double length = NR::L2(end - begin);
- NR::Point be = (end - begin) / length;
- double r = NR::dot(p - begin, be);
-
- if (r < 0.0) return 0.0;
- if (r > length) return 1.0;
-
- return (r / length);
-}
-
-
/**
Set the position of point point_type of the gradient applied to item (either fill_or_stroke) to
p_w (in desktop coordinates). Write_repr if you want the change to become permanent.
@@ -742,7 +824,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
gradient = sp_gradient_convert_to_userspace (gradient, item, fill_or_stroke? "fill" : "stroke");
- NR::Matrix i2d = sp_item_i2d_affine (item);
+ NR::Matrix i2d (sp_item_i2d_affine (item));
NR::Point p = p_w * i2d.inverse();
p *= (gradient->gradientTransform).inverse();
// now p is in gradient's original coordinates
@@ -792,14 +874,15 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
{
// using X-coordinates only to determine the offset, assuming p has been snapped to the vector from begin to end.
double offset = get_offset_between_points (p, NR::Point(lg->x1.computed, lg->y1.computed), NR::Point(lg->x2.computed, lg->y2.computed));
+ SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (lg, false);
+ sp_gradient_ensure_vector(lg);
lg->vector.stops.at(point_i).offset = offset;
- SPGradient *vector = sp_gradient_get_vector (lg, false);
SPStop* stopi = sp_get_stop_i(vector, point_i);
stopi->offset = offset;
if (write_repr) {
sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
} else {
- SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
}
}
break;
@@ -807,16 +890,15 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
break;
}
} else if (SP_IS_RADIALGRADIENT(gradient)) {
-
- SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
- NR::Point c (rg->cx.computed, rg->cy.computed);
- NR::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
- if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && NR::L2 (p_w - c_w) < 1e-3) {
- // prevent setting a radius too close to the center
- return;
- }
- NR::Matrix new_transform;
- bool transform_set = false;
+ SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
+ NR::Point c (rg->cx.computed, rg->cy.computed);
+ NR::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
+ if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && NR::L2 (p_w - c_w) < 1e-3) {
+ // prevent setting a radius too close to the center
+ return;
+ }
+ NR::Matrix new_transform;
+ bool transform_set = false;
switch (point_type) {
case POINT_RG_CENTER:
@@ -845,7 +927,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
break;
case POINT_RG_R1:
{
- NR::Point r1_w = (c + NR::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
+ NR::Point r1_w = (c + NR::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
double r1_angle = NR::atan2(r1_w - c_w);
double move_angle = NR::atan2(p_w - c_w) - r1_angle;
double move_stretch = NR::L2(p_w - c_w) / NR::L2(r1_w - c_w);
@@ -879,38 +961,40 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
transform_set = true;
- break;
- }
+ break;
+ }
case POINT_RG_MID1:
- {
+ {
NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed);
- NR::Point end = NR::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
+ NR::Point end = NR::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
double offset = get_offset_between_points (p, start, end);
- rg->vector.stops.at(point_i).offset = offset;
- SPGradient *vector = sp_gradient_get_vector (rg, false);
+ SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (rg, false);
+ sp_gradient_ensure_vector(rg);
+ rg->vector.stops.at(point_i).offset = offset;
SPStop* stopi = sp_get_stop_i(vector, point_i);
stopi->offset = offset;
if (write_repr) {
sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
} else {
- SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
}
- break;
- }
+ break;
+ }
case POINT_RG_MID2:
NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed);
NR::Point end = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
double offset = get_offset_between_points (p, start, end);
+ SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(rg, false);
+ sp_gradient_ensure_vector(rg);
rg->vector.stops.at(point_i).offset = offset;
- SPGradient *vector = sp_gradient_get_vector (rg, false);
SPStop* stopi = sp_get_stop_i(vector, point_i);
stopi->offset = offset;
if (write_repr) {
sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
} else {
- SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
}
- break;
+ break;
}
if (transform_set) {
gradient->gradientTransform = new_transform;
@@ -1010,12 +1094,14 @@ sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool
if (SP_GRADIENT(gradient)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
- NR::Rect const bbox = item->invokeBbox(NR::identity()); // we need "true" bbox without item_i2d_affine
- p *= NR::Matrix(bbox.dimensions()[NR::X], 0,
- 0, bbox.dimensions()[NR::Y],
- bbox.min()[NR::X], bbox.min()[NR::Y]);
+ boost::optional<NR::Rect> bbox = item->getBounds(NR::identity()); // we need "true" bbox without item_i2d_affine
+ if (bbox) {
+ p *= NR::Matrix(bbox->dimensions()[NR::X], 0,
+ 0, bbox->dimensions()[NR::Y],
+ bbox->min()[NR::X], bbox->min()[NR::Y]);
+ }
}
- p *= NR::Matrix(gradient->gradientTransform) * sp_item_i2d_affine(item);
+ p *= NR::Matrix(gradient->gradientTransform) * (NR::Matrix)sp_item_i2d_affine(item);
return p;
}
@@ -1038,9 +1124,8 @@ sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_
SPStyle *style = SP_OBJECT_STYLE(item);
g_assert(style != NULL);
- guint style_type = is_fill? style->fill.type : style->stroke.type;
SPPaintServer *ps = NULL;
- if (style_type == SP_PAINT_TYPE_PAINTSERVER)
+ if (is_fill? style->fill.isPaintserver() : style->stroke.isPaintserver())
ps = is_fill? SP_STYLE_FILL_SERVER(style) : SP_STYLE_STROKE_SERVER(style);
if (ps
Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
gchar b[64];
- sp_svg_write_color(b, 64, color);
+ sp_svg_write_color(b, sizeof(b), color);
{
gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:1;", b);
@@ -1194,15 +1279,15 @@ sp_gradient_vector_for_object(SPDocument *const doc, SPDesktop *const desktop,
SPIPaint const &paint = ( is_fill
? style.fill
: style.stroke );
- if (paint.type == SP_PAINT_TYPE_COLOR) {
- rgba = sp_color_get_rgba32_ualpha(&paint.value.color, 0xff);
- } else if (paint.type == SP_PAINT_TYPE_PAINTSERVER) {
+ if (paint.isPaintserver()) {
SPObject *server = is_fill? SP_OBJECT_STYLE_FILL_SERVER(o) : SP_OBJECT_STYLE_STROKE_SERVER(o);
if (SP_IS_GRADIENT (server)) {
return sp_gradient_get_vector(SP_GRADIENT (server), TRUE);
} else {
rgba = sp_desktop_get_color(desktop, is_fill);
}
+ } else if (paint.isColor()) {
+ rgba = paint.value.color.toRGBA32( 0xff );
} else {
// if o doesn't use flat color, then take current color of the desktop.
rgba = sp_desktop_get_color(desktop, is_fill);
@@ -1213,17 +1298,6 @@ sp_gradient_vector_for_object(SPDocument *const doc, SPDesktop *const desktop,
}
-SPGradient *
-sp_gradient_get_forked_vector_if_necessary(SPGradient *gradient, bool force_vector)
-{
- SPGradient *vector = sp_gradient_get_vector (gradient, force_vector);
- vector = sp_gradient_fork_vector_if_necessary (vector);
- if ( gradient != vector && gradient->ref->getObject() != vector ) {
- sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
- }
- return vector;
-}
-
/*
Local Variables:
mode:c++