X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fcanvas%2Ftypes.h;h=dbd9683b75585a2dd6c8d69f3d32001627a8ac30;hb=573ec69a9aa29b08d99cba56284660fcd080fb55;hp=33bb92ca58e77eb43ccb907b71da957ad3afd4fd;hpb=5e0e41e068a04603198a4e50464d794156f42c47;p=ardour.git diff --git a/libs/canvas/canvas/types.h b/libs/canvas/canvas/types.h index 33bb92ca58..dbd9683b75 100644 --- a/libs/canvas/canvas/types.h +++ b/libs/canvas/canvas/types.h @@ -23,12 +23,15 @@ #include #include #include +#include #include #include +#include "canvas/visibility.h" + namespace Cairo { - struct Context; + class Context; } namespace ArdourCanvas @@ -38,9 +41,20 @@ typedef double Coord; typedef double Distance; typedef uint32_t Color; -extern Coord const COORD_MAX; +extern LIBCANVAS_API Coord const COORD_MAX; + +inline Coord +canvas_safe_add (Coord a, Coord b) +{ + if (((COORD_MAX - a) <= b) || ((COORD_MAX - b) <= a)) { + return COORD_MAX; + } + + return a + b; +} + -struct Duple +struct LIBCANVAS_API Duple { Duple () : x (0) @@ -55,18 +69,34 @@ struct Duple Coord x; Coord y; - Duple translate (Duple) const; + Duple translate (const Duple& t) const throw() { + return Duple (canvas_safe_add (x, t.x), canvas_safe_add (y, t.y)); + } + + Duple operator- () const throw () { + return Duple (-x, -y); + } + Duple operator+ (Duple const & o) const throw () { + return Duple (canvas_safe_add (x, o.x), canvas_safe_add (y, o.y)); + } + bool operator== (Duple const & o) const throw () { + return x == o.x && y == o.y; + } + bool operator!= (Duple const & o) const throw () { + return x != o.x || y != o.y; + } + Duple operator- (Duple const & o) const throw () { + return Duple (x - o.x, y - o.y); + } + Duple operator/ (double b) const throw () { + return Duple (x / b, y / b); + } }; -extern Duple operator- (Duple const &); -extern Duple operator+ (Duple const &, Duple const &); -extern bool operator== (Duple const &, Duple const &); -extern Duple operator- (Duple const &, Duple const &); -extern Duple operator/ (Duple const &, double); -extern std::ostream & operator<< (std::ostream &, Duple const &); +extern LIBCANVAS_API std::ostream & operator<< (std::ostream &, Duple const &); -struct Rect +struct LIBCANVAS_API Rect { Rect () : x0 (0) @@ -87,26 +117,66 @@ struct Rect Coord x1; Coord y1; - boost::optional intersection (Rect const &) const; - Rect extend (Rect const &) const; - Rect translate (Duple) const; - Rect expand (Distance) const; - bool contains (Duple) const; - Rect fix () const; + boost::optional intersection (Rect const & o) const throw () { + Rect i (std::max (x0, o.x0), std::max (y0, o.y0), + std::min (x1, o.x1), std::min (y1, o.y1)); + + if (i.x0 > i.x1 || i.y0 > i.y1) { + return boost::optional (); + } + + return boost::optional (i); + } + + Rect extend (Rect const & o) const throw () { + return Rect (std::min (x0, o.x0), std::min (y0, o.y0), + std::max (x1, o.x1), std::max (y1, o.y1)); + } + Rect translate (Duple const& t) const throw () { + return Rect (canvas_safe_add (x0, t.x), canvas_safe_add (y0, t.y), + canvas_safe_add (x1, t.x),canvas_safe_add (y1, t.y)); + } + Rect expand (Distance amount) const throw () { + return Rect (x0 - amount, y0 - amount, + canvas_safe_add (x1, amount), + canvas_safe_add (y1, amount)); + } - Rect convert_to_device (Cairo::RefPtr) const; - Rect convert_to_user (Cairo::RefPtr) const; + Rect shrink (Distance amount) const throw () { + /* This isn't the equivalent of expand (-distance) because + of the peculiarities of canvas_safe_add() with negative values. + Maybe. + */ + return Rect (canvas_safe_add (x0, amount), canvas_safe_add (y0, amount), + x1 - amount, y1 - amount); + } + + bool contains (Duple const & point) const throw () { + return point.x >= x0 && point.x < x1 && point.y >= y0 && point.y < y1; + } + Rect fix () const throw () { + return Rect (std::min (x0, x1), std::min (y0, y1), + std::max (x0, x1), std::max (y0, y1)); + } + + bool empty() const throw () { return (x0 == x1 && y0 == y1); } - Distance width () const { + Distance width () const throw () { return x1 - x0; } - Distance height () const { + Distance height () const throw () { return y1 - y0; } + bool operator!= (Rect const & o) const throw () { + return x0 != o.x0 || + x1 != o.x1 || + y0 != o.y0 || + y1 != o.y1; + } }; -extern std::ostream & operator<< (std::ostream &, Rect const &); +extern LIBCANVAS_API std::ostream & operator<< (std::ostream &, Rect const &); typedef std::vector Points;