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