2 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "platform/PlatformExport.h"
31 #include "public/platform/WebCommon.h"
34 typedef struct CGSize CGSize;
37 #import <Foundation/Foundation.h>
43 class PLATFORM_EXPORT IntSize {
45 IntSize() : m_width(0), m_height(0) { }
46 IntSize(int width, int height) : m_width(width), m_height(height) { }
48 int width() const { return m_width; }
49 int height() const { return m_height; }
51 void setWidth(int width) { m_width = width; }
52 void setHeight(int height) { m_height = height; }
54 bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
55 bool isZero() const { return !m_width && !m_height; }
57 float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
59 void expand(int width, int height)
65 void scale(float widthScale, float heightScale)
67 m_width = static_cast<int>(static_cast<float>(m_width) * widthScale);
68 m_height = static_cast<int>(static_cast<float>(m_height) * heightScale);
71 void scale(float scale)
73 this->scale(scale, scale);
76 IntSize expandedTo(const IntSize& other) const
78 return IntSize(m_width > other.m_width ? m_width : other.m_width,
79 m_height > other.m_height ? m_height : other.m_height);
82 IntSize shrunkTo(const IntSize& other) const
84 return IntSize(m_width < other.m_width ? m_width : other.m_width,
85 m_height < other.m_height ? m_height : other.m_height);
88 void clampNegativeToZero()
90 *this = expandedTo(IntSize());
93 void clampToMinimumSize(const IntSize& minimumSize)
95 if (m_width < minimumSize.width())
96 m_width = minimumSize.width();
97 if (m_height < minimumSize.height())
98 m_height = minimumSize.height();
101 // Return area in a uint64_t to avoid overflow.
102 uint64_t area() const
104 return static_cast<uint64_t>(width()) * height();
107 int diagonalLengthSquared() const
109 return m_width * m_width + m_height * m_height;
112 IntSize transposedSize() const
114 return IntSize(m_height, m_width);
118 explicit IntSize(const CGSize&); // don't do this implicitly since it's lossy
119 operator CGSize() const;
121 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
122 explicit IntSize(const NSSize &); // don't do this implicitly since it's lossy
123 operator NSSize() const;
128 int m_width, m_height;
131 inline IntSize& operator+=(IntSize& a, const IntSize& b)
133 a.setWidth(a.width() + b.width());
134 a.setHeight(a.height() + b.height());
138 inline IntSize& operator-=(IntSize& a, const IntSize& b)
140 a.setWidth(a.width() - b.width());
141 a.setHeight(a.height() - b.height());
145 inline IntSize operator+(const IntSize& a, const IntSize& b)
147 return IntSize(a.width() + b.width(), a.height() + b.height());
150 inline IntSize operator-(const IntSize& a, const IntSize& b)
152 return IntSize(a.width() - b.width(), a.height() - b.height());
155 inline IntSize operator-(const IntSize& size)
157 return IntSize(-size.width(), -size.height());
160 inline bool operator==(const IntSize& a, const IntSize& b)
162 return a.width() == b.width() && a.height() == b.height();
165 inline bool operator!=(const IntSize& a, const IntSize& b)
167 return a.width() != b.width() || a.height() != b.height();