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 #ifndef UI_GFX_RECT_F_H_ 6 #define UI_GFX_RECT_F_H_ 7 8 #include <string> 9 10 #include "ui/gfx/point_f.h" 11 #include "ui/gfx/rect_base.h" 12 #include "ui/gfx/size_f.h" 13 #include "ui/gfx/vector2d_f.h" 14 15 namespace gfx { 16 17 class InsetsF; 18 19 // A floating version of gfx::Rect. 20 class UI_EXPORT RectF 21 : public RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> { 22 public: 23 RectF() 24 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> 25 (SizeF()) {} 26 27 RectF(float width, float height) 28 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> 29 (SizeF(width, height)) {} 30 31 RectF(float x, float y, float width, float height) 32 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> 33 (PointF(x, y), SizeF(width, height)) {} 34 35 explicit RectF(const SizeF& size) 36 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> 37 (size) {} 38 39 RectF(const PointF& origin, const SizeF& size) 40 : RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float> 41 (origin, size) {} 42 43 ~RectF() {} 44 45 // Scales the rectangle by |scale|. 46 void Scale(float scale) { 47 Scale(scale, scale); 48 } 49 50 void Scale(float x_scale, float y_scale) { 51 set_origin(ScalePoint(origin(), x_scale, y_scale)); 52 set_size(ScaleSize(size(), x_scale, y_scale)); 53 } 54 55 // This method reports if the RectF can be safely converted to an integer 56 // Rect. When it is false, some dimension of the RectF is outside the bounds 57 // of what an integer can represent, and converting it to a Rect will require 58 // clamping. 59 bool IsExpressibleAsRect() const; 60 61 std::string ToString() const; 62 }; 63 64 inline bool operator==(const RectF& lhs, const RectF& rhs) { 65 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); 66 } 67 68 inline bool operator!=(const RectF& lhs, const RectF& rhs) { 69 return !(lhs == rhs); 70 } 71 72 inline RectF operator+(const RectF& lhs, const Vector2dF& rhs) { 73 return RectF(lhs.x() + rhs.x(), lhs.y() + rhs.y(), 74 lhs.width(), lhs.height()); 75 } 76 77 inline RectF operator-(const RectF& lhs, const Vector2dF& rhs) { 78 return RectF(lhs.x() - rhs.x(), lhs.y() - rhs.y(), 79 lhs.width(), lhs.height()); 80 } 81 82 inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) { 83 return rhs + lhs; 84 } 85 86 UI_EXPORT RectF IntersectRects(const RectF& a, const RectF& b); 87 UI_EXPORT RectF UnionRects(const RectF& a, const RectF& b); 88 UI_EXPORT RectF SubtractRects(const RectF& a, const RectF& b); 89 90 inline RectF ScaleRect(const RectF& r, float x_scale, float y_scale) { 91 return RectF(r.x() * x_scale, r.y() * y_scale, 92 r.width() * x_scale, r.height() * y_scale); 93 } 94 95 inline RectF ScaleRect(const RectF& r, float scale) { 96 return ScaleRect(r, scale, scale); 97 } 98 99 // Constructs a rectangle with |p1| and |p2| as opposite corners. 100 // 101 // This could also be thought of as "the smallest rect that contains both 102 // points", except that we consider points on the right/bottom edges of the 103 // rect to be outside the rect. So technically one or both points will not be 104 // contained within the rect, because they will appear on one of these edges. 105 UI_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2); 106 107 #if !defined(COMPILER_MSVC) 108 extern template class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float>; 109 #endif 110 111 } // namespace gfx 112 113 #endif // UI_GFX_RECT_F_H_ 114