fix clamping of line and rect coordinates to avoid issues with cairo when drawing...
[ardour.git] / libs / canvas / poly_item.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <algorithm>
21
22 #include "pbd/compose.h"
23
24 #include "canvas/poly_item.h"
25 #include "canvas/canvas.h"
26
27 using namespace std;
28 using namespace ArdourCanvas;
29
30 PolyItem::PolyItem (Group* parent)
31         : Item (parent)
32         , Outline (parent)
33 {
34
35 }
36
37 void
38 PolyItem::compute_bounding_box () const
39 {
40         bool have_one = false;
41
42         Rect bbox;
43
44         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
45                 if (have_one) {
46                         bbox.x0 = min (bbox.x0, i->x);
47                         bbox.y0 = min (bbox.y0, i->y);
48                         bbox.x1 = max (bbox.x1, i->x);
49                         bbox.y1 = max (bbox.y1, i->y);
50                 } else {
51                         bbox.x0 = bbox.x1 = i->x;
52                         bbox.y0 = bbox.y1 = i->y;
53                         have_one = true;
54                 }
55         }
56
57
58         if (!have_one) {
59                 _bounding_box = boost::optional<Rect> ();
60         } else {
61                 _bounding_box = bbox.expand (_outline_width / 2);
62         }
63         
64         _bounding_box_dirty = false;
65 }
66
67 void
68 PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
69 {
70         bool done_first = false;
71         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
72                 if (done_first) {
73                         context->line_to (i->x, i->y);
74                 } else {
75                         context->move_to (i->x, i->y);
76                         done_first = true;
77                 }
78         }
79 }
80
81 void
82 PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
83 {
84         bool done_first = false;
85
86         if (_points.size() <= 2) {
87                 render_path (area, context);
88                 return;
89         }
90
91         Points::const_iterator cp1 = first_control_points.begin();
92         Points::const_iterator cp2 = second_control_points.begin();
93
94         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
95
96                 if (done_first) {
97
98                         context->curve_to (cp1->x, cp1->y,
99                                            cp2->x, cp2->y,
100                                            i->x, i->y);
101
102                         cp1++;
103                         cp2++;
104                         
105                 } else {
106
107                         context->move_to (i->x, i->y);
108                         done_first = true;
109                 }
110         }
111 }
112
113 void
114 PolyItem::set (Points const & points)
115 {
116         begin_change ();
117         
118         _points = points;
119         
120         _bounding_box_dirty = true;
121         end_change ();
122 }
123
124 Points const &
125 PolyItem::get () const
126 {
127         return _points;
128 }
129
130 void
131 PolyItem::dump (ostream& o) const
132 {
133         Item::dump (o);
134
135         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
136         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
137                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
138         }
139 }