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 "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
13 #include "ui/gfx/gfx_export.h" | 13 #include "ui/gfx/gfx_export.h" |
14 | 14 |
15 namespace gfx { | 15 namespace gfx { |
16 | 16 |
17 // A floating version of gfx::Size. | 17 // A floating version of gfx::Size. |
18 class GFX_EXPORT SizeF { | 18 class GFX_EXPORT SizeF { |
19 public: | 19 public: |
20 constexpr SizeF() : width_(0.f), height_(0.f) {} | 20 constexpr SizeF() : width_(0.f), height_(0.f) {} |
21 constexpr SizeF(float width, float height) | 21 constexpr SizeF(float width, float height) |
22 : width_(width >= 0 ? width : 0), height_(height >= 0 ? height : 0) {} | 22 : width_(clamp(width)), height_(clamp(height)) {} |
23 | 23 |
24 constexpr explicit SizeF(const Size& size) | 24 constexpr explicit SizeF(const Size& size) |
25 : SizeF(static_cast<float>(size.width()), | 25 : SizeF(static_cast<float>(size.width()), |
26 static_cast<float>(size.height())) {} | 26 static_cast<float>(size.height())) {} |
27 | 27 |
28 constexpr float width() const { return width_; } | 28 constexpr float width() const { return width_; } |
29 constexpr float height() const { return height_; } | 29 constexpr float height() const { return height_; } |
30 | 30 |
31 void set_width(float width) { width_ = fmaxf(0, width); } | 31 void set_width(float width) { width_ = clamp(width); } |
32 void set_height(float height) { height_ = fmaxf(0, height); } | 32 void set_height(float height) { height_ = clamp(height); } |
33 | 33 |
34 float GetArea() const; | 34 float GetArea() const; |
35 | 35 |
36 void SetSize(float width, float height) { | 36 void SetSize(float width, float height) { |
37 set_width(width); | 37 set_width(width); |
38 set_height(height); | 38 set_height(height); |
39 } | 39 } |
40 | 40 |
41 void Enlarge(float grow_width, float grow_height); | 41 void Enlarge(float grow_width, float grow_height); |
42 | 42 |
43 void SetToMin(const SizeF& other); | 43 void SetToMin(const SizeF& other); |
44 void SetToMax(const SizeF& other); | 44 void SetToMax(const SizeF& other); |
45 | 45 |
46 bool IsEmpty() const { return !width() || !height(); } | 46 bool IsEmpty() const { return !height_ || !width_; } |
47 | 47 |
48 void Scale(float scale) { | 48 void Scale(float scale) { |
49 Scale(scale, scale); | 49 Scale(scale, scale); |
50 } | 50 } |
51 | 51 |
52 void Scale(float x_scale, float y_scale) { | 52 void Scale(float x_scale, float y_scale) { |
53 SetSize(width() * x_scale, height() * y_scale); | 53 SetSize(width() * x_scale, height() * y_scale); |
54 } | 54 } |
55 | 55 |
56 std::string ToString() const; | 56 std::string ToString() const; |
57 | 57 |
58 private: | 58 private: |
59 float width_; | 59 float width_; |
60 float height_; | 60 float height_; |
61 static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon(); | |
62 static constexpr float clamp(float f) { return f > kTrivial ? f : 0.f; } | |
63 friend float SizeFPrivateMatch(); // Test access to the kTrivial constant. | |
danakj
2017/03/28 18:00:20
make friends the first thing in the private block
Peter Mayo
2017/03/28 20:11:21
Done.
| |
61 }; | 64 }; |
62 | 65 |
63 inline bool operator==(const SizeF& lhs, const SizeF& rhs) { | 66 inline bool operator==(const SizeF& lhs, const SizeF& rhs) { |
64 return lhs.width() == rhs.width() && lhs.height() == rhs.height(); | 67 return lhs.width() == rhs.width() && lhs.height() == rhs.height(); |
65 } | 68 } |
66 | 69 |
67 inline bool operator!=(const SizeF& lhs, const SizeF& rhs) { | 70 inline bool operator!=(const SizeF& lhs, const SizeF& rhs) { |
68 return !(lhs == rhs); | 71 return !(lhs == rhs); |
69 } | 72 } |
70 | 73 |
71 GFX_EXPORT SizeF ScaleSize(const SizeF& p, float x_scale, float y_scale); | 74 GFX_EXPORT SizeF ScaleSize(const SizeF& p, float x_scale, float y_scale); |
72 | 75 |
73 inline SizeF ScaleSize(const SizeF& p, float scale) { | 76 inline SizeF ScaleSize(const SizeF& p, float scale) { |
74 return ScaleSize(p, scale, scale); | 77 return ScaleSize(p, scale, scale); |
75 } | 78 } |
76 | 79 |
77 // This is declared here for use in gtest-based unit tests but is defined in | 80 // 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 | 81 // 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. | 82 // test. This should not be used in production code - call ToString() instead. |
80 void PrintTo(const SizeF& size, ::std::ostream* os); | 83 void PrintTo(const SizeF& size, ::std::ostream* os); |
81 | 84 |
82 } // namespace gfx | 85 } // namespace gfx |
83 | 86 |
84 #endif // UI_GFX_GEOMETRY_SIZE_F_H_ | 87 #endif // UI_GFX_GEOMETRY_SIZE_F_H_ |
OLD | NEW |