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