1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/geometry/size.h"
#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_math.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/size_conversions.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#elif BUILDFLAG(IS_IOS)
#include <CoreGraphics/CoreGraphics.h>
#elif BUILDFLAG(IS_MAC)
#include <ApplicationServices/ApplicationServices.h>
#endif
namespace gfx {
#if BUILDFLAG(IS_APPLE)
Size::Size(const CGSize& s) : Size(s.width, s.height) {}
#endif
void Size::operator+=(const Size& size) {
Enlarge(size.width(), size.height());
}
void Size::operator-=(const Size& size) {
Enlarge(-size.width(), -size.height());
}
#if BUILDFLAG(IS_WIN)
SIZE Size::ToSIZE() const {
SIZE s;
s.cx = width();
s.cy = height();
return s;
}
#elif BUILDFLAG(IS_APPLE)
CGSize Size::ToCGSize() const {
return CGSizeMake(width(), height());
}
#endif
int Size::GetArea() const {
return GetCheckedArea().ValueOrDie();
}
base::CheckedNumeric<int> Size::GetCheckedArea() const {
base::CheckedNumeric<int> checked_area = width();
checked_area *= height();
return checked_area;
}
void Size::Enlarge(int grow_width, int grow_height) {
SetSize(base::ClampAdd(width(), grow_width),
base::ClampAdd(height(), grow_height));
}
void Size::SetToMin(const Size& other) {
width_ = std::min(width_, other.width_);
height_ = std::min(height_, other.height_);
}
void Size::SetToMax(const Size& other) {
width_ = std::max(width_, other.width_);
height_ = std::max(height_, other.height_);
}
std::string Size::ToString() const {
return base::StringPrintf("%dx%d", width(), height());
}
Size ScaleToCeiledSize(const Size& size, float x_scale, float y_scale) {
if (x_scale == 1.f && y_scale == 1.f)
return size;
return ToCeiledSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
}
Size ScaleToCeiledSize(const Size& size, float scale) {
if (scale == 1.f)
return size;
return ToCeiledSize(ScaleSize(gfx::SizeF(size), scale, scale));
}
Size ScaleToFlooredSize(const Size& size, float x_scale, float y_scale) {
if (x_scale == 1.f && y_scale == 1.f)
return size;
return ToFlooredSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
}
Size ScaleToFlooredSize(const Size& size, float scale) {
if (scale == 1.f)
return size;
return ToFlooredSize(ScaleSize(gfx::SizeF(size), scale, scale));
}
Size ScaleToRoundedSize(const Size& size, float x_scale, float y_scale) {
if (x_scale == 1.f && y_scale == 1.f)
return size;
return ToRoundedSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
}
Size ScaleToRoundedSize(const Size& size, float scale) {
if (scale == 1.f)
return size;
return ToRoundedSize(ScaleSize(gfx::SizeF(size), scale, scale));
}
} // namespace gfx
|