size.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "ui/gfx/geometry/size.h"
  5. #include "base/numerics/clamped_math.h"
  6. #include "base/numerics/safe_math.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "build/build_config.h"
  9. #include "ui/gfx/geometry/size_conversions.h"
  10. #if BUILDFLAG(IS_WIN)
  11. #include <windows.h>
  12. #elif BUILDFLAG(IS_IOS)
  13. #include <CoreGraphics/CoreGraphics.h>
  14. #elif BUILDFLAG(IS_MAC)
  15. #include <ApplicationServices/ApplicationServices.h>
  16. #endif
  17. namespace gfx {
  18. #if BUILDFLAG(IS_APPLE)
  19. Size::Size(const CGSize& s) : Size(s.width, s.height) {}
  20. #endif
  21. void Size::operator+=(const Size& size) {
  22. Enlarge(size.width(), size.height());
  23. }
  24. void Size::operator-=(const Size& size) {
  25. Enlarge(-size.width(), -size.height());
  26. }
  27. #if BUILDFLAG(IS_WIN)
  28. SIZE Size::ToSIZE() const {
  29. SIZE s;
  30. s.cx = width();
  31. s.cy = height();
  32. return s;
  33. }
  34. #elif BUILDFLAG(IS_APPLE)
  35. CGSize Size::ToCGSize() const {
  36. return CGSizeMake(width(), height());
  37. }
  38. #endif
  39. int Size::GetArea() const {
  40. return GetCheckedArea().ValueOrDie();
  41. }
  42. base::CheckedNumeric<int> Size::GetCheckedArea() const {
  43. base::CheckedNumeric<int> checked_area = width();
  44. checked_area *= height();
  45. return checked_area;
  46. }
  47. void Size::Enlarge(int grow_width, int grow_height) {
  48. SetSize(base::ClampAdd(width(), grow_width),
  49. base::ClampAdd(height(), grow_height));
  50. }
  51. void Size::SetToMin(const Size& other) {
  52. width_ = std::min(width_, other.width_);
  53. height_ = std::min(height_, other.height_);
  54. }
  55. void Size::SetToMax(const Size& other) {
  56. width_ = std::max(width_, other.width_);
  57. height_ = std::max(height_, other.height_);
  58. }
  59. std::string Size::ToString() const {
  60. return base::StringPrintf("%dx%d", width(), height());
  61. }
  62. Size ScaleToCeiledSize(const Size& size, float x_scale, float y_scale) {
  63. if (x_scale == 1.f && y_scale == 1.f)
  64. return size;
  65. return ToCeiledSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
  66. }
  67. Size ScaleToCeiledSize(const Size& size, float scale) {
  68. if (scale == 1.f)
  69. return size;
  70. return ToCeiledSize(ScaleSize(gfx::SizeF(size), scale, scale));
  71. }
  72. Size ScaleToFlooredSize(const Size& size, float x_scale, float y_scale) {
  73. if (x_scale == 1.f && y_scale == 1.f)
  74. return size;
  75. return ToFlooredSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
  76. }
  77. Size ScaleToFlooredSize(const Size& size, float scale) {
  78. if (scale == 1.f)
  79. return size;
  80. return ToFlooredSize(ScaleSize(gfx::SizeF(size), scale, scale));
  81. }
  82. Size ScaleToRoundedSize(const Size& size, float x_scale, float y_scale) {
  83. if (x_scale == 1.f && y_scale == 1.f)
  84. return size;
  85. return ToRoundedSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
  86. }
  87. Size ScaleToRoundedSize(const Size& size, float scale) {
  88. if (scale == 1.f)
  89. return size;
  90. return ToRoundedSize(ScaleSize(gfx::SizeF(size), scale, scale));
  91. }
  92. } // namespace gfx