Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / geometry / size_f.h
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 <cmath>
9 #include <iosfwd>
10 #include <string>
11
12 #include "base/compiler_specific.h"
13 #include "ui/gfx/gfx_export.h"
14
15 namespace gfx {
16
17 // A floating version of gfx::Size.
18 class GFX_EXPORT SizeF {
19  public:
20   SizeF() : width_(0.f), height_(0.f) {}
21   SizeF(float width, float height)
22       : width_(fmaxf(0, width)), height_(fmaxf(0, height)) {}
23   ~SizeF() {}
24
25   float width() const { return width_; }
26   float height() const { return height_; }
27
28   void set_width(float width) { width_ = fmaxf(0, width); }
29   void set_height(float height) { height_ = fmaxf(0, height); }
30
31   float GetArea() const;
32
33   void SetSize(float width, float height) {
34     set_width(width);
35     set_height(height);
36   }
37
38   void Enlarge(float grow_width, float grow_height);
39
40   void SetToMin(const SizeF& other);
41   void SetToMax(const SizeF& other);
42
43   bool IsEmpty() const { return !width() || !height(); }
44
45   void Scale(float scale) {
46     Scale(scale, scale);
47   }
48
49   void Scale(float x_scale, float y_scale) {
50     SetSize(width() * x_scale, height() * y_scale);
51   }
52
53   std::string ToString() const;
54
55  private:
56   float width_;
57   float height_;
58 };
59
60 inline bool operator==(const SizeF& lhs, const SizeF& rhs) {
61   return lhs.width() == rhs.width() && lhs.height() == rhs.height();
62 }
63
64 inline bool operator!=(const SizeF& lhs, const SizeF& rhs) {
65   return !(lhs == rhs);
66 }
67
68 GFX_EXPORT SizeF ScaleSize(const SizeF& p, float x_scale, float y_scale);
69
70 inline SizeF ScaleSize(const SizeF& p, float scale) {
71   return ScaleSize(p, scale, scale);
72 }
73
74 // This is declared here for use in gtest-based unit tests but is defined in
75 // the gfx_test_support target. Depend on that to use this in your unit test.
76 // This should not be used in production code - call ToString() instead.
77 void PrintTo(const SizeF& size, ::std::ostream* os);
78
79 }  // namespace gfx
80
81 #endif  // UI_GFX_GEOMETRY_SIZE_F_H_