1 // Copyright 2017 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 // Note: ported from Chromium commit head: 0e161fe 5 // Note: only necessary functions are ported from gfx::Rect 6 7 // Defines a simple integer rectangle class. The containment semantics 8 // are array-like; that is, the coordinate (x, y) is considered to be 9 // contained by the rectangle, but the coordinate (x + width, y) is not. 10 // The class will happily let you create malformed rectangles (that is, 11 // rectangles with negative width and/or height), but there will be assertions 12 // in the operations (such as Contains()) to complain in this case. 13 14 #ifndef RECT_H_ 15 #define RECT_H_ 16 17 #include <string> 18 19 #include "base/strings/stringprintf.h" 20 #include "size.h" 21 22 namespace media { 23 24 // Helper struct for rect to replace gfx::Rect usage from original code. 25 // Only partial functions of gfx::Rect is implemented here. 26 class Rect { 27 public: 28 Rect() : x_(0), y_(0), size_(0, 0) {} 29 Rect(int width, int height) : x_(0), y_(0), size_(width, height) {} 30 Rect(int x, int y, int width, int height) 31 : x_(x), y_(y), size_(width, height) {} 32 explicit Rect(const Size& size) : x_(0), y_(0), size_(size) {} 33 34 int x() const { return x_; } 35 void set_x(int x) { x_ = x; } 36 37 int y() const { return y_; } 38 void set_y(int y) { y_ = y; } 39 40 int width() const { return size_.width(); } 41 void set_width(int width) { size_.set_width(width); } 42 43 int height() const { return size_.height(); } 44 void set_height(int height) { size_.set_height(height); } 45 46 const Size& size() const { return size_; } 47 void set_size(const Size& size) { 48 set_width(size.width()); 49 set_height(size.height()); 50 } 51 52 constexpr int right() const { return x() + width(); } 53 constexpr int bottom() const { return y() + height(); } 54 55 void SetRect(int x, int y, int width, int height) { 56 set_x(x); 57 set_y(y); 58 set_width(width); 59 set_height(height); 60 } 61 62 // Returns true if the area of the rectangle is zero. 63 bool IsEmpty() const { return size_.IsEmpty(); } 64 65 // Returns true if this rectangle contains the specified rectangle. 66 bool Contains(const Rect& rect) const { 67 return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() && 68 rect.bottom() <= bottom()); 69 } 70 71 std::string ToString() const { 72 return base::StringPrintf("(%d,%d) %s", 73 x_, y_, size().ToString().c_str()); 74 } 75 76 private: 77 int x_; 78 int y_; 79 Size size_; 80 }; 81 82 inline bool operator==(const Rect& lhs, const Rect& rhs) { 83 return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.size() == rhs.size(); 84 } 85 86 inline bool operator!=(const Rect& lhs, const Rect& rhs) { 87 return !(lhs == rhs); 88 } 89 90 } // namespace media 91 92 #endif // RECT_H_ 93