Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
insets.h
Go to the documentation of this file.
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_GEOMETRY_INSETS_H_
6#define UI_GFX_GEOMETRY_INSETS_H_
7
8#include <memory>
9#include <string>
10
11#include "gfx/gfx_export.h"
12#include "insets_f.h"
13#include "size.h"
14
15namespace gfx {
16
17class Vector2d;
18
19// Represents the widths of the four borders or margins of an unspecified
20// rectangle. An Insets stores the thickness of the top, left, bottom and right
21// edges, without storing the actual size and position of the rectangle itself.
22//
23// This can be used to represent a space within a rectangle, by "shrinking" the
24// rectangle by the inset amount on all four sides. Alternatively, it can
25// represent a border that has a different thickness on each side.
27 public:
28 constexpr Insets() : top_(0), left_(0), bottom_(0), right_(0) {}
29 constexpr explicit Insets(int all)
30 : top_(all),
31 left_(all),
32 bottom_(GetClampedValue(all, all)),
33 right_(GetClampedValue(all, all)) {}
34 constexpr explicit Insets(int vertical, int horizontal)
35 : top_(vertical),
36 left_(horizontal),
37 bottom_(GetClampedValue(vertical, vertical)),
38 right_(GetClampedValue(horizontal, horizontal)) {}
39 constexpr Insets(int top, int left, int bottom, int right)
40 : top_(top),
41 left_(left),
42 bottom_(GetClampedValue(top, bottom)),
43 right_(GetClampedValue(left, right)) {}
44
45 constexpr int top() const { return top_; }
46 constexpr int left() const { return left_; }
47 constexpr int bottom() const { return bottom_; }
48 constexpr int right() const { return right_; }
49
50 // Returns the total width taken up by the insets, which is the sum of the
51 // left and right insets.
52 constexpr int width() const { return left_ + right_; }
53
54 // Returns the total height taken up by the insets, which is the sum of the
55 // top and bottom insets.
56 constexpr int height() const { return top_ + bottom_; }
57
58 // Returns the sum of the left and right insets as the width, the sum of the
59 // top and bottom insets as the height.
60 constexpr Size size() const { return Size(width(), height()); }
61
62 // Returns true if the insets are empty.
63 bool IsEmpty() const { return width() == 0 && height() == 0; }
64
65 void set_top(int top) {
66 top_ = top;
67 bottom_ = GetClampedValue(top_, bottom_);
68 }
69 void set_left(int left) {
70 left_ = left;
71 right_ = GetClampedValue(left_, right_);
72 }
73 void set_bottom(int bottom) { bottom_ = GetClampedValue(top_, bottom); }
74 void set_right(int right) { right_ = GetClampedValue(left_, right); }
75
76 void Set(int top, int left, int bottom, int right) {
77 top_ = top;
78 left_ = left;
79 bottom_ = GetClampedValue(top_, bottom);
80 right_ = GetClampedValue(left_, right);
81 }
82
83 bool operator==(const Insets& insets) const {
84 return top_ == insets.top_ && left_ == insets.left_ &&
85 bottom_ == insets.bottom_ && right_ == insets.right_;
86 }
87
88 bool operator!=(const Insets& insets) const { return !(*this == insets); }
89
90 void operator+=(const Insets& insets) {
91 top_ = base::ClampAdd(top_, insets.top_);
92 left_ = base::ClampAdd(left_, insets.left_);
93 bottom_ = GetClampedValue(top_, base::ClampAdd(bottom_, insets.bottom_));
94 right_ = GetClampedValue(left_, base::ClampAdd(right_, insets.right_));
95 }
96
97 void operator-=(const Insets& insets) {
98 top_ = base::ClampSub(top_, insets.top_);
99 left_ = base::ClampSub(left_, insets.left_);
100 bottom_ = GetClampedValue(top_, base::ClampSub(bottom_, insets.bottom_));
101 right_ = GetClampedValue(left_, base::ClampSub(right_, insets.right_));
102 }
103
105 return Insets(-base::MakeClampedNum(top_), -base::MakeClampedNum(left_),
106 -base::MakeClampedNum(bottom_),
107 -base::MakeClampedNum(right_));
108 }
109
110 Insets Scale(float scale) const { return Scale(scale, scale); }
111
112 Insets Scale(float x_scale, float y_scale) const {
113 return Insets(static_cast<int>(base::ClampMul(top(), y_scale)),
114 static_cast<int>(base::ClampMul(left(), x_scale)),
115 static_cast<int>(base::ClampMul(bottom(), y_scale)),
116 static_cast<int>(base::ClampMul(right(), x_scale)));
117 }
118
119 // Adjusts the vertical and horizontal dimensions by the values described in
120 // |vector|. Offsetting insets before applying to a rectangle would be
121 // equivalent to offseting the rectangle then applying the insets.
122 Insets Offset(const gfx::Vector2d& vector) const;
123
124 operator InsetsF() const {
125 return InsetsF(static_cast<float>(top()), static_cast<float>(left()),
126 static_cast<float>(bottom()), static_cast<float>(right()));
127 }
128
129 // Returns a string representation of the insets.
130 std::string ToString() const;
131
132 private:
133 int top_;
134 int left_;
135 int bottom_;
136 int right_;
137
138 // See rect.h
139 // Returns true iff a+b would overflow max int.
140 static constexpr bool AddWouldOverflow(int a, int b) {
141 // In this function, GCC tries to make optimizations that would only work if
142 // max - a wouldn't overflow but it isn't smart enough to notice that a > 0.
143 // So cast everything to unsigned to avoid this. As it is guaranteed that
144 // max - a and b are both already positive, the cast is a noop.
145 //
146 // This is intended to be: a > 0 && max - a < b
147 return a > 0 && b > 0 &&
148 static_cast<unsigned>(std::numeric_limits<int>::max() - a) <
149 static_cast<unsigned>(b);
150 }
151
152 // Returns true iff a+b would underflow min int.
153 static constexpr bool AddWouldUnderflow(int a, int b) {
154 return a < 0 && b < 0 && std::numeric_limits<int>::min() - a > b;
155 }
156
157 // Clamp the right/bottom to avoid integer over/underflow in width() and
158 // height(). This returns the right/bottom given a top_or_left and a
159 // bottom_or_right.
160 // TODO(enne): this should probably use base::ClampAdd, but that
161 // function is not a constexpr.
162 static constexpr int GetClampedValue(int top_or_left, int bottom_or_right) {
163 if (AddWouldOverflow(top_or_left, bottom_or_right)) {
164 return std::numeric_limits<int>::max() - top_or_left;
165 } else if (AddWouldUnderflow(top_or_left, bottom_or_right)) {
166 // If |top_or_left| and |bottom_or_right| are both negative,
167 // adds |top_or_left| to prevent underflow by subtracting it.
168 return std::numeric_limits<int>::min() - top_or_left;
169 } else {
170 return bottom_or_right;
171 }
172 }
173};
174
175inline Insets operator+(Insets lhs, const Insets& rhs) {
176 lhs += rhs;
177 return lhs;
178}
179
180inline Insets operator-(Insets lhs, const Insets& rhs) {
181 lhs -= rhs;
182 return lhs;
183}
184
185} // namespace gfx
186
187#endif // UI_GFX_GEOMETRY_INSETS_H_
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
void Set(int top, int left, int bottom, int right)
Definition insets.h:76
constexpr int width() const
Definition insets.h:52
constexpr int right() const
Definition insets.h:48
void set_right(int right)
Definition insets.h:74
constexpr int bottom() const
Definition insets.h:47
Insets Scale(float scale) const
Definition insets.h:110
void set_left(int left)
Definition insets.h:69
constexpr Insets(int vertical, int horizontal)
Definition insets.h:34
constexpr Insets(int top, int left, int bottom, int right)
Definition insets.h:39
Insets Scale(float x_scale, float y_scale) const
Definition insets.h:112
void set_top(int top)
Definition insets.h:65
constexpr Size size() const
Definition insets.h:60
bool operator!=(const Insets &insets) const
Definition insets.h:88
constexpr Insets()
Definition insets.h:28
void set_bottom(int bottom)
Definition insets.h:73
constexpr int top() const
Definition insets.h:45
constexpr Insets(int all)
Definition insets.h:29
constexpr int height() const
Definition insets.h:56
constexpr int left() const
Definition insets.h:46
bool IsEmpty() const
Definition insets.h:63
void operator+=(const Insets &insets)
Definition insets.h:90
Insets operator-() const
Definition insets.h:104
void operator-=(const Insets &insets)
Definition insets.h:97
bool operator==(const Insets &insets) const
Definition insets.h:83
static bool b
struct MyStruct a[10]
#define GFX_EXPORT
Definition gfx_export.h:26
Definition insets.cc:10
Insets operator+(Insets lhs, const Insets &rhs)
Definition insets.h:175
Insets operator-(Insets lhs, const Insets &rhs)
Definition insets.h:180
int32_t height
int32_t width
const Scalar scale