• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H_
6 #define UI_GFX_GEOMETRY_SIZE_H_
7 
8 #include <iosfwd>
9 #include <string>
10 
11 #include "base/compiler_specific.h"
12 #include "base/numerics/safe_math.h"
13 #include "build/build_config.h"
14 #include "ui/gfx/gfx_export.h"
15 
16 #if defined(OS_WIN)
17 typedef struct tagSIZE SIZE;
18 #elif defined(OS_MACOSX)
19 typedef struct CGSize CGSize;
20 #endif
21 
22 namespace gfx {
23 
24 // A size has width and height values.
25 class GFX_EXPORT Size {
26  public:
Size()27   constexpr Size() : width_(0), height_(0) {}
Size(int width,int height)28   constexpr Size(int width, int height)
29       : width_(width < 0 ? 0 : width), height_(height < 0 ? 0 : height) {}
30 #if defined(OS_MACOSX)
31   explicit Size(const CGSize& s);
32 #endif
33 
34 #if defined(OS_MACOSX)
35   Size& operator=(const CGSize& s);
36 #endif
37 
38 #if defined(OS_WIN)
39   SIZE ToSIZE() const;
40 #elif defined(OS_MACOSX)
41   CGSize ToCGSize() const;
42 #endif
43 
width()44   constexpr int width() const { return width_; }
height()45   constexpr int height() const { return height_; }
46 
set_width(int width)47   void set_width(int width) { width_ = width < 0 ? 0 : width; }
set_height(int height)48   void set_height(int height) { height_ = height < 0 ? 0 : height; }
49 
50   // This call will CHECK if the area of this size would overflow int.
51   int GetArea() const;
52   // Returns a checked numeric representation of the area.
53   base::CheckedNumeric<int> GetCheckedArea() const;
54 
SetSize(int width,int height)55   void SetSize(int width, int height) {
56     set_width(width);
57     set_height(height);
58   }
59 
60   void Enlarge(int grow_width, int grow_height);
61 
62   void SetToMin(const Size& other);
63   void SetToMax(const Size& other);
64 
IsEmpty()65   bool IsEmpty() const { return !width() || !height(); }
66 
67   std::string ToString() const;
68 
69  private:
70   int width_;
71   int height_;
72 };
73 
74 inline bool operator==(const Size& lhs, const Size& rhs) {
75   return lhs.width() == rhs.width() && lhs.height() == rhs.height();
76 }
77 
78 inline bool operator!=(const Size& lhs, const Size& rhs) {
79   return !(lhs == rhs);
80 }
81 
82 // This is declared here for use in gtest-based unit tests but is defined in
83 // the //ui/gfx:test_support target. Depend on that to use this in your unit
84 // test. This should not be used in production code - call ToString() instead.
85 void PrintTo(const Size& size, ::std::ostream* os);
86 
87 // Helper methods to scale a gfx::Size to a new gfx::Size.
88 GFX_EXPORT Size ScaleToCeiledSize(const Size& size,
89                                   float x_scale,
90                                   float y_scale);
91 GFX_EXPORT Size ScaleToCeiledSize(const Size& size, float scale);
92 GFX_EXPORT Size ScaleToFlooredSize(const Size& size,
93                                    float x_scale,
94                                    float y_scale);
95 GFX_EXPORT Size ScaleToFlooredSize(const Size& size, float scale);
96 GFX_EXPORT Size ScaleToRoundedSize(const Size& size,
97                                    float x_scale,
98                                    float y_scale);
99 GFX_EXPORT Size ScaleToRoundedSize(const Size& size, float scale);
100 
101 }  // namespace gfx
102 
103 #endif  // UI_GFX_GEOMETRY_SIZE_H_
104