• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ppapi/cpp/rect.h"
6 
7 #include <algorithm>
8 
9 namespace {
10 
AdjustAlongAxis(int32_t dst_origin,int32_t dst_size,int32_t * origin,int32_t * size)11 void AdjustAlongAxis(int32_t dst_origin, int32_t dst_size,
12                      int32_t* origin, int32_t* size) {
13   if (*origin < dst_origin) {
14     *origin = dst_origin;
15     *size = std::min(dst_size, *size);
16   } else {
17     *size = std::min(dst_size, *size);
18     *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
19   }
20 }
21 
22 }  // namespace
23 
24 namespace pp {
25 
Inset(int32_t left,int32_t top,int32_t right,int32_t bottom)26 void Rect::Inset(int32_t left, int32_t top, int32_t right, int32_t bottom) {
27   Offset(left, top);
28   set_width(std::max<int32_t>(width() - left - right, 0));
29   set_height(std::max<int32_t>(height() - top - bottom, 0));
30 }
31 
Offset(int32_t horizontal,int32_t vertical)32 void Rect::Offset(int32_t horizontal, int32_t vertical) {
33   rect_.point.x += horizontal;
34   rect_.point.y += vertical;
35 }
36 
Contains(int32_t point_x,int32_t point_y) const37 bool Rect::Contains(int32_t point_x, int32_t point_y) const {
38   return (point_x >= x()) && (point_x < right()) &&
39          (point_y >= y()) && (point_y < bottom());
40 }
41 
Contains(const Rect & rect) const42 bool Rect::Contains(const Rect& rect) const {
43   return (rect.x() >= x() && rect.right() <= right() &&
44           rect.y() >= y() && rect.bottom() <= bottom());
45 }
46 
Intersects(const Rect & rect) const47 bool Rect::Intersects(const Rect& rect) const {
48   return !(rect.x() >= right() || rect.right() <= x() ||
49            rect.y() >= bottom() || rect.bottom() <= y());
50 }
51 
Intersect(const Rect & rect) const52 Rect Rect::Intersect(const Rect& rect) const {
53   int32_t rx = std::max(x(), rect.x());
54   int32_t ry = std::max(y(), rect.y());
55   int32_t rr = std::min(right(), rect.right());
56   int32_t rb = std::min(bottom(), rect.bottom());
57 
58   if (rx >= rr || ry >= rb)
59     rx = ry = rr = rb = 0;  // non-intersecting
60 
61   return Rect(rx, ry, rr - rx, rb - ry);
62 }
63 
Union(const Rect & rect) const64 Rect Rect::Union(const Rect& rect) const {
65   // special case empty rects...
66   if (IsEmpty())
67     return rect;
68   if (rect.IsEmpty())
69     return *this;
70 
71   int32_t rx = std::min(x(), rect.x());
72   int32_t ry = std::min(y(), rect.y());
73   int32_t rr = std::max(right(), rect.right());
74   int32_t rb = std::max(bottom(), rect.bottom());
75 
76   return Rect(rx, ry, rr - rx, rb - ry);
77 }
78 
Subtract(const Rect & rect) const79 Rect Rect::Subtract(const Rect& rect) const {
80   // boundary cases:
81   if (!Intersects(rect))
82     return *this;
83   if (rect.Contains(*this))
84     return Rect();
85 
86   int32_t rx = x();
87   int32_t ry = y();
88   int32_t rr = right();
89   int32_t rb = bottom();
90 
91   if (rect.y() <= y() && rect.bottom() >= bottom()) {
92     // complete intersection in the y-direction
93     if (rect.x() <= x()) {
94       rx = rect.right();
95     } else {
96       rr = rect.x();
97     }
98   } else if (rect.x() <= x() && rect.right() >= right()) {
99     // complete intersection in the x-direction
100     if (rect.y() <= y()) {
101       ry = rect.bottom();
102     } else {
103       rb = rect.y();
104     }
105   }
106   return Rect(rx, ry, rr - rx, rb - ry);
107 }
108 
AdjustToFit(const Rect & rect) const109 Rect Rect::AdjustToFit(const Rect& rect) const {
110   int32_t new_x = x();
111   int32_t new_y = y();
112   int32_t new_width = width();
113   int32_t new_height = height();
114   AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
115   AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
116   return Rect(new_x, new_y, new_width, new_height);
117 }
118 
CenterPoint() const119 Point Rect::CenterPoint() const {
120   return Point(x() + (width() + 1) / 2, y() + (height() + 1) / 2);
121 }
122 
SharesEdgeWith(const Rect & rect) const123 bool Rect::SharesEdgeWith(const Rect& rect) const {
124   return (y() == rect.y() && height() == rect.height() &&
125              (x() == rect.right() || right() == rect.x())) ||
126          (x() == rect.x() && width() == rect.width() &&
127              (y() == rect.bottom() || bottom() == rect.y()));
128 }
129 
130 }  // namespace pp
131