Move render_view_context_menu.* and related files out of tab_contents.
[chromium-blink-merge.git] / ppapi / cpp / rect.cc
blob1e9ff05236b3d41dec29efb0b73b90b77cfd3e80
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.
5 #include "ppapi/cpp/rect.h"
7 #include <algorithm>
9 namespace {
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;
22 } // namespace
24 namespace pp {
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));
32 void Rect::Offset(int32_t horizontal, int32_t vertical) {
33 rect_.point.x += horizontal;
34 rect_.point.y += vertical;
37 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());
42 bool Rect::Contains(const Rect& rect) const {
43 return (rect.x() >= x() && rect.right() <= right() &&
44 rect.y() >= y() && rect.bottom() <= bottom());
47 bool Rect::Intersects(const Rect& rect) const {
48 return !(rect.x() >= right() || rect.right() <= x() ||
49 rect.y() >= bottom() || rect.bottom() <= y());
52 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());
58 if (rx >= rr || ry >= rb)
59 rx = ry = rr = rb = 0; // non-intersecting
61 return Rect(rx, ry, rr - rx, rb - ry);
64 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;
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());
76 return Rect(rx, ry, rr - rx, rb - ry);
79 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();
86 int32_t rx = x();
87 int32_t ry = y();
88 int32_t rr = right();
89 int32_t rb = bottom();
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();
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();
106 return Rect(rx, ry, rr - rx, rb - ry);
109 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);
119 Point Rect::CenterPoint() const {
120 return Point(x() + (width() + 1) / 2, y() + (height() + 1) / 2);
123 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()));
130 } // namespace pp