1 /** \file
2 * Interface between Inkscape code (SPItem) and remove-overlaps function.
3 */
4 /*
5 * Authors:
6 * Tim Dwyer <tgdwyer@gmail.com>
7 *
8 * Copyright (C) 2005 Authors
9 *
10 * Released under GNU LGPL. Read the file 'COPYING' for more information.
11 */
12 #include "util/glib-list-iterators.h"
13 #include "sp-item.h"
14 #include "sp-item-transform.h"
15 #include "libvpsc/generate-constraints.h"
16 #include "libvpsc/remove_rectangle_overlap.h"
17 #include <utility>
19 using vpsc::Rectangle;
21 namespace {
22 struct Record {
23 SPItem *item;
24 Geom::Point midpoint;
25 Rectangle *vspc_rect;
27 Record() {}
28 Record(SPItem *i, Geom::Point m, Rectangle *r)
29 : item(i), midpoint(m), vspc_rect(r) {}
30 };
31 }
33 /**
34 * Takes a list of inkscape items and moves them as little as possible
35 * such that rectangular bounding boxes are separated by at least xGap
36 * horizontally and yGap vertically
37 */
38 void removeoverlap(GSList const *const items, double const xGap, double const yGap) {
39 using Inkscape::Util::GSListConstIterator;
40 std::list<SPItem *> selected;
41 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
42 std::vector<Record> records;
43 std::vector<Rectangle *> rs;
45 Geom::Point const gap(xGap, yGap);
46 for (std::list<SPItem *>::iterator it(selected.begin());
47 it != selected.end();
48 ++it)
49 {
50 using Geom::X; using Geom::Y;
51 Geom::OptRect item_box(sp_item_bbox_desktop(*it));
52 if (item_box) {
53 Geom::Point min(item_box->min() - .5*gap);
54 Geom::Point max(item_box->max() + .5*gap);
55 Rectangle *vspc_rect = new Rectangle(min[X], max[X], min[Y], max[Y]);
56 records.push_back(Record(*it, item_box->midpoint(), vspc_rect));
57 rs.push_back(vspc_rect);
58 }
59 }
60 if (!rs.empty()) {
61 removeRectangleOverlap(rs.size(), &rs[0], 0.0, 0.0);
62 }
63 for ( std::vector<Record>::iterator it = records.begin();
64 it != records.end();
65 ++it )
66 {
67 Geom::Point const curr = it->midpoint;
68 Geom::Point const dest(it->vspc_rect->getCentreX(),
69 it->vspc_rect->getCentreY());
70 sp_item_move_rel(it->item, Geom::Translate(dest - curr));
71 delete it->vspc_rect;
72 }
73 }