X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fgradient-chemistry.cpp;h=70bc835bc6420f993b45fdf93b28626361f24495;hb=4e33f020dd832c0fa662afd97a40d6eba7f1fade;hp=b4a1eca92571c2d78bd765c7f0f98924140dae70;hpb=f951374eef04129c6a0d213e7ab4d9ed2095ca69;p=inkscape.git diff --git a/src/gradient-chemistry.cpp b/src/gradient-chemistry.cpp index b4a1eca92..70bc835bc 100644 --- a/src/gradient-chemistry.cpp +++ b/src/gradient-chemistry.cpp @@ -6,7 +6,9 @@ * Authors: * Lauris Kaplinski * bulia byak + * Johan Engelen * + * Copyright (C) 2007 Johan Engelen * Copyright (C) 2001-2005 authors * Copyright (C) 2001 Ximian, Inc. * @@ -19,6 +21,7 @@ #include "desktop-style.h" #include "sp-gradient-reference.h" +#include "sp-gradient-vector.h" #include "sp-linear-gradient.h" #include "sp-radial-gradient.h" #include "sp-stop.h" @@ -27,8 +30,12 @@ #include "sp-text.h" #include "sp-tspan.h" #include +#include #include "xml/repr.h" #include "svg/svg.h" +#include "svg/svg-color.h" +#include "svg/css-ostringstream.h" +#include "prefs-utils.h" // Terminology: @@ -41,8 +48,6 @@ // object. It is either linear or radial. static void sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *gr); -static void sp_item_repr_set_style_gradient(Inkscape::XML::Node *repr, gchar const *property, - SPGradient *gr, bool recursive); SPGradient * sp_gradient_ensure_vector_normalized(SPGradient *gr) @@ -93,12 +98,13 @@ sp_gradient_get_private_normalized(SPDocument *document, SPGradient *vector, SPG SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document); // create a new private gradient of the requested type Inkscape::XML::Node *repr; if (type == SP_GRADIENT_TYPE_LINEAR) { - repr = sp_repr_new("svg:linearGradient"); + repr = xml_doc->createElement("svg:linearGradient"); } else { - repr = sp_repr_new("svg:radialGradient"); + repr = xml_doc->createElement("svg:radialGradient"); } // privates are garbage-collectable @@ -132,14 +138,14 @@ count_gradient_hrefs(SPObject *o, SPGradient *gr) 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) { @@ -230,12 +236,17 @@ sp_gradient_fork_private_if_necessary(SPGradient *gr, SPGradient *vector, 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; @@ -243,6 +254,21 @@ sp_gradient_fork_vector_if_necessary (SPGradient *gr) 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. @@ -255,13 +281,15 @@ sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item) // 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::Maybe 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]); @@ -277,12 +305,9 @@ sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item) gr->gradientTransform = squeeze; { - gchar c[256]; - if (sp_svg_transform_write(c, 256, gr->gradientTransform)) { - SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c); - } else { - SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", NULL); - } + gchar *c=sp_svg_transform_write(gr->gradientTransform); + SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c); + g_free(c); } } else { sp_repr_set_svg_double(repr, "x1", (center - NR::Point(width/2, 0))[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; + NR::Maybe 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 @@ -347,12 +378,9 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop // apply skew to the gradient gr->gradientTransform = skew; { - gchar c[256]; - if (sp_svg_transform_write(c, 256, gr->gradientTransform)) { - SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c); - } else { - SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", NULL); - } + gchar *c=sp_svg_transform_write(gr->gradientTransform); + SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c); + g_free(c); } // Matrix to convert points to userspace coords; postmultiply by inverse of skew so @@ -403,9 +431,9 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop // refer to one gradient, hence the recursive call for text (because we can't/don't // want to access tspans and set gradients on them separately) if (SP_IS_TEXT(item)) - sp_item_repr_set_style_gradient(SP_OBJECT_REPR(item), property, gr, true); + sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), true); else - sp_item_repr_set_style_gradient(SP_OBJECT_REPR(item), property, gr, false); + sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), false); return gr; } @@ -420,12 +448,9 @@ sp_gradient_transform_multiply(SPGradient *gradient, NR::Matrix postmul, bool se } gradient->gradientTransform_set = TRUE; - gchar c[256]; - if (sp_svg_transform_write(c, 256, gradient->gradientTransform)) { - SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", c); - } else { - SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", NULL); - } + gchar *c=sp_svg_transform_write(gradient->gradientTransform); + SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", c); + g_free(c); } SPGradient * @@ -435,14 +460,14 @@ sp_item_gradient (SPItem *item, bool fill_or_stroke) 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); @@ -484,11 +509,11 @@ sp_prev_stop(SPStop *stop, SPGradient *gradient) 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* @@ -496,14 +521,86 @@ sp_last_stop(SPGradient *gradient) { 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); + + for (guint i=0; i < stop_i; i++) { + if (!stop) return NULL; + stop = sp_next_stop (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_num, bool fill_or_stroke) +sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -511,8 +608,8 @@ sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) return; SPGradient *vector = sp_gradient_get_vector (gradient, false); - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: case POINT_RG_CENTER: case POINT_RG_FOCUS: { @@ -521,7 +618,7 @@ sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) } break; - case POINT_LG_P2: + case POINT_LG_END: case POINT_RG_R1: case POINT_RG_R2: { @@ -529,13 +626,22 @@ sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) gtk_widget_show (dialog); } break; + + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + { + GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_get_stop_i (vector, point_i)); + gtk_widget_show (dialog); + } + break; default: break; } } guint32 -sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_stroke) +sp_item_gradient_stop_query_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -547,8 +653,8 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s if (!vector) // orphan! return 0; // what else to do? - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: case POINT_RG_CENTER: case POINT_RG_FOCUS: { @@ -559,7 +665,7 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s } break; - case POINT_LG_P2: + case POINT_LG_END: case POINT_RG_R1: case POINT_RG_R2: { @@ -569,6 +675,18 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s } } break; + + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + { + SPStop *stopi = sp_get_stop_i (vector, point_i); + if (stopi) { + return sp_stop_get_rgba32(stopi); + } + } + break; + default: break; } @@ -576,7 +694,7 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s } void -sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_stroke, SPCSSAttr *stop) +sp_item_gradient_stop_set_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, SPCSSAttr *stop) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -593,8 +711,8 @@ sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_str sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector); } - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: case POINT_RG_CENTER: case POINT_RG_FOCUS: { @@ -605,7 +723,7 @@ sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_str } break; - case POINT_LG_P2: + case POINT_LG_END: case POINT_RG_R1: case POINT_RG_R2: { @@ -615,6 +733,18 @@ sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_str } } break; + + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + { + SPStop *stopi = sp_get_stop_i (vector, point_i); + if (stopi) { + sp_repr_css_change (SP_OBJECT_REPR (stopi), stop, "style"); + } + } + break; + default: break; } @@ -649,7 +779,8 @@ sp_item_gradient_reverse_vector (SPItem *item, 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)); } @@ -673,13 +804,12 @@ sp_item_gradient_reverse_vector (SPItem *item, bool fill_or_stroke) } - /** -Set the position of point point_num of the gradient applied to item (either fill_or_stroke) to +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. */ void -sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool fill_or_stroke, bool write_repr, bool scale) +sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::Point p_w, bool fill_or_stroke, bool write_repr, bool scale) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -697,8 +827,8 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool if (SP_IS_LINEARGRADIENT(gradient)) { SPLinearGradient *lg = SP_LINEARGRADIENT(gradient); - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: if (scale) { lg->x2.computed += (lg->x1.computed - p[NR::X]); lg->y2.computed += (lg->y1.computed - p[NR::Y]); @@ -716,7 +846,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG); } break; - case POINT_LG_P2: + case POINT_LG_END: if (scale) { lg->x1.computed += (lg->x2.computed - p[NR::X]); lg->y1.computed += (lg->y2.computed - p[NR::Y]); @@ -734,22 +864,37 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG); } break; + case POINT_LG_MID: + { + // 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; + 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 (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + } + } + break; default: 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_num == POINT_RG_R1 || point_num == 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_num) { + switch (point_type) { case POINT_RG_CENTER: rg->fx.computed = p[NR::X] + (rg->fx.computed - rg->cx.computed); rg->fy.computed = p[NR::Y] + (rg->fy.computed - rg->cy.computed); @@ -776,7 +921,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool 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); @@ -810,20 +955,48 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool 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); + 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; + 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 (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + } + 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; + 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 (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + } + break; + } if (transform_set) { gradient->gradientTransform = new_transform; gradient->gradientTransform_set = TRUE; if (write_repr) { - gchar s[256]; - if (sp_svg_transform_write(s, 256, gradient->gradientTransform)) { - SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", s); - } else { - SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", NULL); - } + gchar *s=sp_svg_transform_write(gradient->gradientTransform); + SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", s); + g_free(s); } else { SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG); } @@ -853,12 +1026,12 @@ sp_item_gradient_get_spread (SPItem *item, bool fill_or_stroke) /** -Returns the position of point point_num of the gradient applied to item (either fill_or_stroke), +Returns the position of point point_type of the gradient applied to item (either fill_or_stroke), in desktop coordinates. */ NR::Point -sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke) +sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -869,17 +1042,23 @@ sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke) if (SP_IS_LINEARGRADIENT(gradient)) { SPLinearGradient *lg = SP_LINEARGRADIENT(gradient); - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: p = NR::Point (lg->x1.computed, lg->y1.computed); break; - case POINT_LG_P2: + case POINT_LG_END: p = NR::Point (lg->x2.computed, lg->y2.computed); break; + case POINT_LG_MID: + { + gdouble offset = lg->vector.stops.at(point_i).offset; + p = (1-offset) * NR::Point(lg->x1.computed, lg->y1.computed) + offset * NR::Point(lg->x2.computed, lg->y2.computed); + } + break; } } else if (SP_IS_RADIALGRADIENT(gradient)) { SPRadialGradient *rg = SP_RADIALGRADIENT(gradient); - switch (point_num) { + switch (point_type) { case POINT_RG_CENTER: p = NR::Point (rg->cx.computed, rg->cy.computed); break; @@ -892,15 +1071,29 @@ sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke) case POINT_RG_R2: p = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed); break; + case POINT_RG_MID1: + { + gdouble offset = rg->vector.stops.at(point_i).offset; + p = (1-offset) * NR::Point (rg->cx.computed, rg->cy.computed) + offset * NR::Point(rg->cx.computed + rg->r.computed, rg->cy.computed); + } + break; + case POINT_RG_MID2: + { + gdouble offset = rg->vector.stops.at(point_i).offset; + p = (1-offset) * NR::Point (rg->cx.computed, rg->cy.computed) + offset * NR::Point(rg->cx.computed, rg->cy.computed - rg->r.computed); + } + break; } } 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]); + NR::Maybe 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); return p; @@ -925,9 +1118,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 @@ -965,7 +1157,7 @@ sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_ /* We have to change object style here; recursive because this is used from * fill&stroke and must work for groups etc. */ - sp_item_repr_set_style_gradient(SP_OBJECT_REPR(item), is_fill? "fill" : "stroke", normalized, true); + sp_style_set_property_url(SP_OBJECT(item), is_fill? "fill" : "stroke", SP_OBJECT(normalized), true); } SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); return normalized; @@ -975,9 +1167,7 @@ sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_ /* Current fill style is not a gradient or wrong type, so construct everything */ SPGradient *constructed = sp_gradient_get_private_normalized(SP_OBJECT_DOCUMENT(item), gr, type); constructed = sp_gradient_reset_to_userspace(constructed, item); - sp_item_repr_set_style_gradient(SP_OBJECT_REPR(item), - ( is_fill ? "fill" : "stroke" ), - constructed, true); + sp_style_set_property_url(SP_OBJECT(item), ( is_fill ? "fill" : "stroke" ), SP_OBJECT(constructed), true); SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG )); return constructed; @@ -1005,27 +1195,6 @@ sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *link) repr->setAttribute("xlink:href", ref); } -static void -sp_item_repr_set_style_gradient(Inkscape::XML::Node *repr, gchar const *property, - SPGradient *gr, bool recursive) -{ - g_return_if_fail(repr != NULL); - g_return_if_fail(gr != NULL); - g_return_if_fail(SP_IS_GRADIENT(gr)); - - gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(gr)); - - SPCSSAttr *css = sp_repr_css_attr_new(); - sp_repr_css_set_property(css, property, val); - g_free(val); - if (recursive) { - sp_repr_css_change_recursive(repr, css, "style"); - } else { - sp_repr_css_change(repr, css, "style"); - } - sp_repr_css_attr_unref(css); -} - /* * Get default normalized gradient vector of document, create if there is none */ @@ -1034,8 +1203,9 @@ SPGradient * sp_document_default_gradient_vector(SPDocument *document, guint32 color) { SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document); - Inkscape::XML::Node *repr = sp_repr_new("svg:linearGradient"); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient"); repr->setAttribute("inkscape:collect", "always"); // set here, but removed when it's edited in the gradient editor @@ -1043,10 +1213,10 @@ sp_document_default_gradient_vector(SPDocument *document, guint32 color) // (1) here, search gradients by color and return what is found without duplication // (2) in fill & stroke, show only one copy of each gradient in list - Inkscape::XML::Node *stop = sp_repr_new("svg:stop"); + 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); @@ -1059,7 +1229,7 @@ sp_document_default_gradient_vector(SPDocument *document, guint32 color) repr->appendChild(stop); Inkscape::GC::release(stop); - stop = sp_repr_new("svg:stop"); + stop = xml_doc->createElement("svg:stop"); { gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:0;", b); @@ -1103,15 +1273,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); @@ -1121,6 +1291,7 @@ sp_gradient_vector_for_object(SPDocument *const doc, SPDesktop *const desktop, return sp_document_default_gradient_vector(doc, rgba); } + /* Local Variables: mode:c++