OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_GEOMETRY_SIZE_F_H_ | 5 #ifndef UI_GFX_GEOMETRY_SIZE_F_H_ |
6 #define UI_GFX_GEOMETRY_SIZE_F_H_ | 6 #define UI_GFX_GEOMETRY_SIZE_F_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/gtest_prod_util.h" |
12 #include "ui/gfx/geometry/size.h" | 13 #include "ui/gfx/geometry/size.h" |
13 #include "ui/gfx/gfx_export.h" | 14 #include "ui/gfx/gfx_export.h" |
14 | 15 |
15 namespace gfx { | 16 namespace gfx { |
16 | 17 |
| 18 FORWARD_DECLARE_TEST(SizeTest, TrivialDimensionTests); |
| 19 FORWARD_DECLARE_TEST(SizeTest, ClampsToZero); |
| 20 FORWARD_DECLARE_TEST(SizeTest, ConsistentClamping); |
| 21 |
17 // A floating version of gfx::Size. | 22 // A floating version of gfx::Size. |
18 class GFX_EXPORT SizeF { | 23 class GFX_EXPORT SizeF { |
19 public: | 24 public: |
20 constexpr SizeF() : width_(0.f), height_(0.f) {} | 25 constexpr SizeF() : width_(0.f), height_(0.f) {} |
21 constexpr SizeF(float width, float height) | 26 constexpr SizeF(float width, float height) |
22 : width_(width >= 0 ? width : 0), height_(height >= 0 ? height : 0) {} | 27 : width_(clamp(width)), height_(clamp(height)) {} |
23 | 28 |
24 constexpr explicit SizeF(const Size& size) | 29 constexpr explicit SizeF(const Size& size) |
25 : SizeF(static_cast<float>(size.width()), | 30 : SizeF(static_cast<float>(size.width()), |
26 static_cast<float>(size.height())) {} | 31 static_cast<float>(size.height())) {} |
27 | 32 |
28 constexpr float width() const { return width_; } | 33 constexpr float width() const { return width_; } |
29 constexpr float height() const { return height_; } | 34 constexpr float height() const { return height_; } |
30 | 35 |
31 void set_width(float width) { width_ = fmaxf(0, width); } | 36 void set_width(float width) { width_ = clamp(width); } |
32 void set_height(float height) { height_ = fmaxf(0, height); } | 37 void set_height(float height) { height_ = clamp(height); } |
33 | 38 |
34 float GetArea() const; | 39 float GetArea() const; |
35 | 40 |
36 void SetSize(float width, float height) { | 41 void SetSize(float width, float height) { |
37 set_width(width); | 42 set_width(width); |
38 set_height(height); | 43 set_height(height); |
39 } | 44 } |
40 | 45 |
41 void Enlarge(float grow_width, float grow_height); | 46 void Enlarge(float grow_width, float grow_height); |
42 | 47 |
43 void SetToMin(const SizeF& other); | 48 void SetToMin(const SizeF& other); |
44 void SetToMax(const SizeF& other); | 49 void SetToMax(const SizeF& other); |
45 | 50 |
46 bool IsEmpty() const { return !width() || !height(); } | 51 bool IsEmpty() const { return !width() || !height(); } |
47 | 52 |
48 void Scale(float scale) { | 53 void Scale(float scale) { |
49 Scale(scale, scale); | 54 Scale(scale, scale); |
50 } | 55 } |
51 | 56 |
52 void Scale(float x_scale, float y_scale) { | 57 void Scale(float x_scale, float y_scale) { |
53 SetSize(width() * x_scale, height() * y_scale); | 58 SetSize(width() * x_scale, height() * y_scale); |
54 } | 59 } |
55 | 60 |
56 std::string ToString() const; | 61 std::string ToString() const; |
57 | 62 |
58 private: | 63 private: |
| 64 FRIEND_TEST_ALL_PREFIXES(SizeTest, TrivialDimensionTests); |
| 65 FRIEND_TEST_ALL_PREFIXES(SizeTest, ClampsToZero); |
| 66 FRIEND_TEST_ALL_PREFIXES(SizeTest, ConsistentClamping); |
| 67 |
| 68 static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon(); |
| 69 |
| 70 static constexpr float clamp(float f) { return f > kTrivial ? f : 0.f; } |
| 71 |
59 float width_; | 72 float width_; |
60 float height_; | 73 float height_; |
61 }; | 74 }; |
62 | 75 |
63 inline bool operator==(const SizeF& lhs, const SizeF& rhs) { | 76 inline bool operator==(const SizeF& lhs, const SizeF& rhs) { |
64 return lhs.width() == rhs.width() && lhs.height() == rhs.height(); | 77 return lhs.width() == rhs.width() && lhs.height() == rhs.height(); |
65 } | 78 } |
66 | 79 |
67 inline bool operator!=(const SizeF& lhs, const SizeF& rhs) { | 80 inline bool operator!=(const SizeF& lhs, const SizeF& rhs) { |
68 return !(lhs == rhs); | 81 return !(lhs == rhs); |
69 } | 82 } |
70 | 83 |
71 GFX_EXPORT SizeF ScaleSize(const SizeF& p, float x_scale, float y_scale); | 84 GFX_EXPORT SizeF ScaleSize(const SizeF& p, float x_scale, float y_scale); |
72 | 85 |
73 inline SizeF ScaleSize(const SizeF& p, float scale) { | 86 inline SizeF ScaleSize(const SizeF& p, float scale) { |
74 return ScaleSize(p, scale, scale); | 87 return ScaleSize(p, scale, scale); |
75 } | 88 } |
76 | 89 |
77 // This is declared here for use in gtest-based unit tests but is defined in | 90 // This is declared here for use in gtest-based unit tests but is defined in |
78 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 91 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
79 // test. This should not be used in production code - call ToString() instead. | 92 // test. This should not be used in production code - call ToString() instead. |
80 void PrintTo(const SizeF& size, ::std::ostream* os); | 93 void PrintTo(const SizeF& size, ::std::ostream* os); |
81 | 94 |
82 } // namespace gfx | 95 } // namespace gfx |
83 | 96 |
84 #endif // UI_GFX_GEOMETRY_SIZE_F_H_ | 97 #endif // UI_GFX_GEOMETRY_SIZE_F_H_ |
OLD | NEW |