// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Defines a simple integer rectangle class. The containment semantics // are array-like; that is, the coordinate (x, y) is considered to be // contained by the rectangle, but the coordinate (x + width, y) is not. // The class will happily let you create malformed rectangles (that is, // rectangles with negative width and/or height), but there will be assertions // in the operations (such as Contains()) to complain in this case. #ifndef UI_GFX_GEOMETRY_RECT_H_ #define UI_GFX_GEOMETRY_RECT_H_ #include <cmath> #include <iosfwd> #include <string> #include "base/check.h" #include "base/numerics/clamped_math.h" #include "base/numerics/safe_conversions.h" #include "build/build_config.h" #include "ui/gfx/geometry/insets.h" #include "ui/gfx/geometry/outsets.h" #include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/vector2d.h" #if BUILDFLAG(IS_WIN) typedef struct tagRECT RECT; #elif BUILDFLAG(IS_APPLE) typedef struct CGRect CGRect; #endif namespace gfx { class GEOMETRY_EXPORT Rect { … }; inline bool operator==(const Rect& lhs, const Rect& rhs) { … } inline bool operator!=(const Rect& lhs, const Rect& rhs) { … } GEOMETRY_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs); GEOMETRY_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs); inline Rect operator+(const Vector2d& lhs, const Rect& rhs) { … } GEOMETRY_EXPORT Rect IntersectRects(const Rect& a, const Rect& b); GEOMETRY_EXPORT Rect UnionRects(const Rect& a, const Rect& b); GEOMETRY_EXPORT Rect UnionRectsEvenIfEmpty(const Rect& a, const Rect& b); GEOMETRY_EXPORT Rect SubtractRects(const Rect& a, const Rect& b); // Constructs a rectangle with |p1| and |p2| as opposite corners. // // This could also be thought of as "the smallest rect that contains both // points", except that we consider points on the right/bottom edges of the // rect to be outside the rect. So technically one or both points will not be // contained within the rect, because they will appear on one of these edges. GEOMETRY_EXPORT Rect BoundingRect(const Point& p1, const Point& p2); // Scales the rect and returns the enclosing rect. The components are clamped // if they would overflow. inline Rect ScaleToEnclosingRect(const Rect& rect, float x_scale, float y_scale) { … } inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { … } inline Rect ScaleToEnclosedRect(const Rect& rect, float x_scale, float y_scale) { … } inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { … } // Scales |rect| by scaling its four corner points. If the corner points lie on // non-integral coordinate after scaling, their values are rounded to the // nearest integer. The components are clamped if they would overflow. // This is helpful during layout when relative positions of multiple gfx::Rect // in a given coordinate space needs to be same after scaling as it was before // scaling. ie. this gives a lossless relative positioning of rects. inline Rect ScaleToRoundedRect(const Rect& rect, float x_scale, float y_scale) { … } inline Rect ScaleToRoundedRect(const Rect& rect, float scale) { … } // Scales `rect` by `scale` and rounds to enclosing rect, but for each edge, if // the distance between the edge and the nearest integer grid is smaller than // `error`, the edge is snapped to the integer grid. The default error is 0.001 // , which is used by cc/viz. Use this when scaling the window/layer size. GEOMETRY_EXPORT Rect ScaleToEnclosingRectIgnoringError(const Rect& rect, float scale, float error = 0.001f); // Return a maximum rectangle that is covered by the a or b. GEOMETRY_EXPORT Rect MaximumCoveredRect(const Rect& a, const Rect& b); // This is declared here for use in gtest-based unit tests but is defined in // the //ui/gfx:test_support target. Depend on that to use this in your unit // test. This should not be used in production code - call ToString() instead. void PrintTo(const Rect& rect, ::std::ostream* os); } // namespace gfx #endif // UI_GFX_GEOMETRY_RECT_H_