2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #ifndef __CANVAS_TYPES_H__
21 #define __CANVAS_TYPES_H__
27 #include <boost/optional.hpp>
29 #include <cairomm/refptr.h>
31 #include "canvas/visibility.h"
37 namespace ArdourCanvas
41 typedef double Distance;
42 typedef uint32_t Color;
44 extern LIBCANVAS_API Coord const COORD_MAX;
47 canvas_safe_add (Coord a, Coord b)
49 if (((COORD_MAX - a) <= b) || ((COORD_MAX - b) <= a)) {
57 struct LIBCANVAS_API Duple
64 Duple (Coord x_, Coord y_)
72 Duple translate (const Duple& t) const throw() {
73 return Duple (canvas_safe_add (x, t.x), canvas_safe_add (y, t.y));
76 Duple operator- () const throw () {
77 return Duple (-x, -y);
79 Duple operator+ (Duple const & o) const throw () {
80 return Duple (canvas_safe_add (x, o.x), canvas_safe_add (y, o.y));
82 bool operator== (Duple const & o) const throw () {
83 return x == o.x && y == o.y;
85 bool operator!= (Duple const & o) const throw () {
86 return x != o.x || y != o.y;
88 Duple operator- (Duple const & o) const throw () {
89 return Duple (x - o.x, y - o.y);
91 Duple operator/ (double b) const throw () {
92 return Duple (x / b, y / b);
97 extern LIBCANVAS_API std::ostream & operator<< (std::ostream &, Duple const &);
99 struct LIBCANVAS_API Rect
108 Rect (Coord x0_, Coord y0_, Coord x1_, Coord y1_)
120 boost::optional<Rect> intersection (Rect const & o) const throw () {
121 Rect i (std::max (x0, o.x0), std::max (y0, o.y0),
122 std::min (x1, o.x1), std::min (y1, o.y1));
124 if (i.x0 > i.x1 || i.y0 > i.y1) {
125 return boost::optional<Rect> ();
128 return boost::optional<Rect> (i);
131 Rect extend (Rect const & o) const throw () {
132 return Rect (std::min (x0, o.x0), std::min (y0, o.y0),
133 std::max (x1, o.x1), std::max (y1, o.y1));
135 Rect translate (Duple const& t) const throw () {
136 return Rect (canvas_safe_add (x0, t.x), canvas_safe_add (y0, t.y),
137 canvas_safe_add (x1, t.x),canvas_safe_add (y1, t.y));
139 Rect expand (Distance amount) const throw () {
140 return Rect (x0 - amount, y0 - amount,
141 canvas_safe_add (x1, amount),
142 canvas_safe_add (y1, amount));
144 Rect expand (Distance top, Distance right, Distance bottom, Distance left) const throw () {
145 return Rect (x0 - left, y0 - top,
146 canvas_safe_add (x1, right),
147 canvas_safe_add (y1, bottom));
150 Rect shrink (Distance amount) const throw () {
151 /* This isn't the equivalent of expand (-distance) because
152 of the peculiarities of canvas_safe_add() with negative values.
155 return Rect (canvas_safe_add (x0, amount), canvas_safe_add (y0, amount),
156 x1 - amount, y1 - amount);
159 bool contains (Duple const & point) const throw () {
160 return point.x >= x0 && point.x < x1 && point.y >= y0 && point.y < y1;
162 Rect fix () const throw () {
163 return Rect (std::min (x0, x1), std::min (y0, y1),
164 std::max (x0, x1), std::max (y0, y1));
167 bool empty() const throw () { return (x0 == x1 && y0 == y1); }
169 Distance width () const throw () {
173 Distance height () const throw () {
176 bool operator!= (Rect const & o) const throw () {
184 extern LIBCANVAS_API std::ostream & operator<< (std::ostream &, Rect const &);
186 typedef std::vector<Duple> Points;