size.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef UI_GFX_GEOMETRY_SIZE_H_
  5. #define UI_GFX_GEOMETRY_SIZE_H_
  6. #include <algorithm>
  7. #include <iosfwd>
  8. #include <string>
  9. #include "base/numerics/safe_math.h"
  10. #include "build/build_config.h"
  11. #include "ui/gfx/geometry/geometry_export.h"
  12. #if BUILDFLAG(IS_WIN)
  13. typedef struct tagSIZE SIZE;
  14. #elif BUILDFLAG(IS_APPLE)
  15. typedef struct CGSize CGSize;
  16. #endif
  17. namespace gfx {
  18. // A size has width and height values.
  19. class GEOMETRY_EXPORT Size {
  20. public:
  21. constexpr Size() : width_(0), height_(0) {}
  22. constexpr Size(int width, int height)
  23. : width_(std::max(0, width)), height_(std::max(0, height)) {}
  24. #if BUILDFLAG(IS_APPLE)
  25. explicit Size(const CGSize& s);
  26. #endif
  27. void operator+=(const Size& size);
  28. void operator-=(const Size& size);
  29. #if BUILDFLAG(IS_WIN)
  30. SIZE ToSIZE() const;
  31. #elif BUILDFLAG(IS_APPLE)
  32. CGSize ToCGSize() const;
  33. #endif
  34. constexpr int width() const { return width_; }
  35. constexpr int height() const { return height_; }
  36. void set_width(int width) { width_ = std::max(0, width); }
  37. void set_height(int height) { height_ = std::max(0, height); }
  38. // This call will CHECK if the area of this size would overflow int.
  39. int GetArea() const;
  40. // Returns a checked numeric representation of the area.
  41. base::CheckedNumeric<int> GetCheckedArea() const;
  42. uint64_t Area64() const {
  43. return static_cast<uint64_t>(width_) * static_cast<uint64_t>(height_);
  44. }
  45. void SetSize(int width, int height) {
  46. set_width(width);
  47. set_height(height);
  48. }
  49. void Enlarge(int grow_width, int grow_height);
  50. void SetToMin(const Size& other);
  51. void SetToMax(const Size& other);
  52. bool IsEmpty() const { return !width() || !height(); }
  53. bool IsZero() const { return !width() && !height(); }
  54. void Transpose() {
  55. using std::swap;
  56. swap(width_, height_);
  57. }
  58. std::string ToString() const;
  59. private:
  60. int width_;
  61. int height_;
  62. };
  63. inline bool operator==(const Size& lhs, const Size& rhs) {
  64. return lhs.width() == rhs.width() && lhs.height() == rhs.height();
  65. }
  66. inline bool operator!=(const Size& lhs, const Size& rhs) {
  67. return !(lhs == rhs);
  68. }
  69. inline Size operator+(Size lhs, const Size& rhs) {
  70. lhs += rhs;
  71. return lhs;
  72. }
  73. inline Size operator-(Size lhs, const Size& rhs) {
  74. lhs -= rhs;
  75. return lhs;
  76. }
  77. // 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
  79. // test. This should not be used in production code - call ToString() instead.
  80. void PrintTo(const Size& size, ::std::ostream* os);
  81. // Helper methods to scale a gfx::Size to a new gfx::Size.
  82. GEOMETRY_EXPORT Size ScaleToCeiledSize(const Size& size,
  83. float x_scale,
  84. float y_scale);
  85. GEOMETRY_EXPORT Size ScaleToCeiledSize(const Size& size, float scale);
  86. GEOMETRY_EXPORT Size ScaleToFlooredSize(const Size& size,
  87. float x_scale,
  88. float y_scale);
  89. GEOMETRY_EXPORT Size ScaleToFlooredSize(const Size& size, float scale);
  90. GEOMETRY_EXPORT Size ScaleToRoundedSize(const Size& size,
  91. float x_scale,
  92. float y_scale);
  93. GEOMETRY_EXPORT Size ScaleToRoundedSize(const Size& size, float scale);
  94. inline Size TransposeSize(const Size& s) {
  95. return Size(s.height(), s.width());
  96. }
  97. } // namespace gfx
  98. #endif // UI_GFX_GEOMETRY_SIZE_H_