Code

- Fix bug #171150: Connectors should always touch the shape boundary
[inkscape.git] / src / sp-conn-end.cpp
2 #include <cstring>
3 #include <string>
4 #include <limits>
6 #include "display/curve.h"
7 #include "libnr/nr-matrix-fns.h"
8 #include "xml/repr.h"
9 #include "sp-conn-end.h"
10 #include "sp-path.h"
11 #include "uri.h"
12 #include "document.h"
13 #include "sp-item-group.h"
14 #include "2geom/path.h"
15 #include "2geom/pathvector.h"
16 #include "2geom/path-intersection.h"
19 static void change_endpts(SPCurve *const curve, Geom::Point const h2endPt[2]);
21 SPConnEnd::SPConnEnd(SPObject *const owner) :
22     ref(owner),
23     href(NULL),
24     _changed_connection(),
25     _delete_connection(),
26     _transformed_connection()
27 {
28 }
30 static SPObject const *
31 get_nearest_common_ancestor(SPObject const *const obj, SPItem const *const objs[2]) {
32     SPObject const *anc_sofar = obj;
33     for (unsigned i = 0; i < 2; ++i) {
34         if ( objs[i] != NULL ) {
35             anc_sofar = anc_sofar->nearestCommonAncestor(objs[i]);
36         }
37     }
38     return anc_sofar;
39 }
42 static bool try_get_intersect_point_with_item_recursive(SPCurve *conn_curve, SPItem& item, 
43         const Geom::Matrix& item_transform, const bool at_start, double* intersect_pos, 
44         unsigned *intersect_index) {
46     double initial_pos = (at_start) ? 0.0 : std::numeric_limits<double>::max();
48     // if this is a group...
49     if (SP_IS_GROUP(&item)) {
50         SPGroup* group = SP_GROUP(&item);
51         
52         // consider all first-order children
53         double child_pos = initial_pos;
54         unsigned child_index;
55         for (GSList const* i = sp_item_group_item_list(group); i != NULL; i = i->next) {
56             SPItem* child_item = SP_ITEM(i->data);
57             try_get_intersect_point_with_item_recursive(conn_curve, *child_item, 
58                     item_transform * child_item->transform, at_start, &child_pos, &child_index);
59             if (fabs(initial_pos - child_pos) > fabs(initial_pos - *intersect_pos)) {
60                 // It is further away from the initial point than the current intersection
61                 // point (i.e. the "outermost" intersection), so use this one.
62                 *intersect_pos = child_pos;
63                 *intersect_index = child_index;
64             }
65         }
66         return *intersect_pos != initial_pos;
67     }
69     // if this is a shape...
70     if (!SP_IS_SHAPE(&item)) return false;
72     // make sure it has an associated curve
73     SPCurve* item_curve = sp_shape_get_curve(SP_SHAPE(&item));
74     if (!item_curve) return false;
76     // apply transformations (up to common ancestor)
77     item_curve->transform(item_transform);
79     const Geom::PathVector& curve_pv = item_curve->get_pathvector();
80     const Geom::PathVector& conn_pv = conn_curve->get_pathvector();
81     Geom::CrossingSet cross = crossings(conn_pv, curve_pv);
82     // iterate over all Crossings
83     for (Geom::CrossingSet::const_iterator i = cross.begin(); i != cross.end(); i++) {
84         const Geom::Crossings& cr = *i;
86         for (Geom::Crossings::const_iterator i = cr.begin(); i != cr.end(); i++) {
87             const Geom::Crossing& cr_pt = *i;
88             if (fabs(initial_pos - cr_pt.ta) > fabs(initial_pos - *intersect_pos)) {
89                 // It is further away from the initial point than the current intersection
90                 // point (i.e. the "outermost" intersection), so use this one.
91                 *intersect_pos = cr_pt.ta;
92                 *intersect_index = cr_pt.a;
93             }
94         }
95     }
97     item_curve->unref();
99     return *intersect_pos != initial_pos;
103 // This function returns the outermost intersection point between the path (a connector)
104 // and the item given.  If the item is a group, then the component items are considered.
105 // The transforms given should be to a common ancestor of both the path and item.
106 //
107 static bool try_get_intersect_point_with_item(SPPath& conn, SPItem& item, 
108         const Geom::Matrix& item_transform, const Geom::Matrix& conn_transform, 
109         const bool at_start, double* intersect_pos, unsigned *intersect_index) {
110  
111     // We start with the intersection point either at the beginning or end of the 
112     // path, depending on whether we are considering the source or target endpoint.
113     *intersect_pos = (at_start) ? 0.0 : std::numeric_limits<double>::max();
115     // Copy the curve and apply transformations up to common ancestor.
116     SPCurve* conn_curve = conn.curve->copy();
117     conn_curve->transform(conn_transform);
119     // Find the intersection.
120     bool result = try_get_intersect_point_with_item_recursive(conn_curve, item, item_transform, 
121             at_start, intersect_pos, intersect_index);
122     
123     // Free the curve copy.
124     conn_curve->unref();
126     return result;
130 static void
131 sp_conn_end_move_compensate(Geom::Matrix const */*mp*/, SPItem */*moved_item*/,
132                             SPPath *const path,
133                             bool const updatePathRepr = true)
135     // TODO: SPItem::getBounds gives the wrong result for some objects
136     //       that have internal representations that are updated later
137     //       by the sp_*_update functions, e.g., text.
138     sp_document_ensure_up_to_date(path->document);
140     // Get the new route around obstacles.
141     path->connEndPair.reroutePath();
143     SPItem *h2attItem[2];
144     path->connEndPair.getAttachedItems(h2attItem);
146     SPItem const *const path_item = SP_ITEM(path);
147     SPObject const *const ancestor = get_nearest_common_ancestor(path_item, h2attItem);
148     Geom::Matrix const path2anc(i2anc_affine(path_item, ancestor));
150     Geom::Point endPts[2] = { *(path->curve->first_point()), *(path->curve->last_point()) };
151  
152     for (unsigned h = 0; h < 2; ++h) {
153         if (h2attItem[h]) {
154             // For each attached object, change the corresponding point to be
155             // at the outermost intersection with the object's path.
156             double intersect_pos;
157             unsigned intersect_index;
158             Geom::Matrix h2i2anc = i2anc_affine(h2attItem[h], ancestor);
159             if ( try_get_intersect_point_with_item(*path, *h2attItem[h], h2i2anc, path2anc, 
160                         (h == 0), &intersect_pos, &intersect_index) ) {
161                 const Geom::PathVector& curve = path->curve->get_pathvector();
162                 endPts[h] = curve[intersect_index].pointAt(intersect_pos);
163             }
164         }
165     }
166     change_endpts(path->curve, endPts);
167     if (updatePathRepr) {
168         path->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
169         path->updateRepr();
170     }
173 // TODO: This triggering of makeInvalidPath could be cleaned up to be
174 //       another option passed to move_compensate.
175 static void
176 sp_conn_end_shape_move_compensate(Geom::Matrix const *mp, SPItem *moved_item,
177                             SPPath *const path)
179     if (path->connEndPair.isAutoRoutingConn()) {
180         path->connEndPair.makePathInvalid();
181     }
182     sp_conn_end_move_compensate(mp, moved_item, path);
186 void
187 sp_conn_adjust_invalid_path(SPPath *const path)
189     sp_conn_end_move_compensate(NULL, NULL, path);
192 void
193 sp_conn_adjust_path(SPPath *const path)
195     if (path->connEndPair.isAutoRoutingConn()) {
196         path->connEndPair.makePathInvalid();
197     }
198     // Don't update the path repr or else connector dragging is slowed by
199     // constant update of values to the xml editor, and each step is also
200     // needlessly remembered by undo/redo.
201     bool const updatePathRepr = false;
202     sp_conn_end_move_compensate(NULL, NULL, path, updatePathRepr);
206 static void
207 change_endpts(SPCurve *const curve, Geom::Point const h2endPt[2])
209 #if 0
210     curve->reset();
211     curve->moveto(h2endPt[0]);
212     curve->lineto(h2endPt[1]);
213 #else
214     curve->move_endpoints(h2endPt[0], h2endPt[1]);
215 #endif
218 static void
219 sp_conn_end_deleted(SPObject *, SPObject *const owner, unsigned const handle_ix)
221     // todo: The first argument is the deleted object, or just NULL if
222     //       called by sp_conn_end_detach.
223     g_return_if_fail(handle_ix < 2);
224     char const *const attr_str[] = {"inkscape:connection-start",
225                                     "inkscape:connection-end"};
226     SP_OBJECT_REPR(owner)->setAttribute(attr_str[handle_ix], NULL);
227     /* I believe this will trigger sp_conn_end_href_changed. */
230 void
231 sp_conn_end_detach(SPObject *const owner, unsigned const handle_ix)
233     sp_conn_end_deleted(NULL, owner, handle_ix);
236 void
237 SPConnEnd::setAttacherHref(gchar const *value)
239     if ( value && href && ( strcmp(value, href) == 0 ) ) {
240         /* No change, do nothing. */
241     } else {
242         g_free(href);
243         href = NULL;
244         if (value) {
245             // First, set the href field, because sp_conn_end_href_changed will need it.
246             href = g_strdup(value);
248             // Now do the attaching, which emits the changed signal.
249             try {
250                 ref.attach(Inkscape::URI(value));
251             } catch (Inkscape::BadURIException &e) {
252                 /* TODO: Proper error handling as per
253                  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.  (Also needed for
254                  * sp-use.) */
255                 g_warning("%s", e.what());
256                 ref.detach();
257             }
258         } else {
259             ref.detach();
260         }
261     }
264 void
265 sp_conn_end_href_changed(SPObject */*old_ref*/, SPObject */*ref*/,
266                          SPConnEnd *connEndPtr, SPPath *const path, unsigned const handle_ix)
268     g_return_if_fail(connEndPtr != NULL);
269     SPConnEnd &connEnd = *connEndPtr;
270     connEnd._delete_connection.disconnect();
271     connEnd._transformed_connection.disconnect();
273     if (connEnd.href) {
274         SPObject *refobj = connEnd.ref.getObject();
275         if (refobj) {
276             connEnd._delete_connection
277                 = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_conn_end_deleted),
278                                                               SP_OBJECT(path), handle_ix));
279             connEnd._transformed_connection
280                 = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_conn_end_shape_move_compensate),
281                                                                  path));
282         }
283     }
288 /*
289   Local Variables:
290   mode:c++
291   c-file-style:"stroustrup"
292   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
293   indent-tabs-mode:nil
294   fill-column:99
295   End:
296 */
297 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :