| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc. All rights reserve
d. | |
| 3 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 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. | |
| 13 * | |
| 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. | |
| 25 */ | |
| 26 | |
| 27 #ifndef IntSize_h | |
| 28 #define IntSize_h | |
| 29 | |
| 30 #if OS(MACOSX) | |
| 31 typedef struct CGSize CGSize; | |
| 32 | |
| 33 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES | |
| 34 typedef struct CGSize NSSize; | |
| 35 #else | |
| 36 typedef struct _NSSize NSSize; | |
| 37 #endif | |
| 38 #endif | |
| 39 | |
| 40 namespace WebCore { | |
| 41 | |
| 42 class IntSize { | |
| 43 public: | |
| 44 IntSize() : m_width(0), m_height(0) { } | |
| 45 IntSize(int width, int height) : m_width(width), m_height(height) { } | |
| 46 | |
| 47 int width() const { return m_width; } | |
| 48 int height() const { return m_height; } | |
| 49 | |
| 50 void setWidth(int width) { m_width = width; } | |
| 51 void setHeight(int height) { m_height = height; } | |
| 52 | |
| 53 bool isEmpty() const { return m_width <= 0 || m_height <= 0; } | |
| 54 bool isZero() const { return !m_width && !m_height; } | |
| 55 | |
| 56 float aspectRatio() const { return static_cast<float>(m_width) / static_cast
<float>(m_height); } | |
| 57 | |
| 58 void expand(int width, int height) | |
| 59 { | |
| 60 m_width += width; | |
| 61 m_height += height; | |
| 62 } | |
| 63 | |
| 64 void scale(float widthScale, float heightScale) | |
| 65 { | |
| 66 m_width = static_cast<int>(static_cast<float>(m_width) * widthScale); | |
| 67 m_height = static_cast<int>(static_cast<float>(m_height) * heightScale); | |
| 68 } | |
| 69 | |
| 70 void scale(float scale) | |
| 71 { | |
| 72 this->scale(scale, scale); | |
| 73 } | |
| 74 | |
| 75 IntSize expandedTo(const IntSize& other) const | |
| 76 { | |
| 77 return IntSize(m_width > other.m_width ? m_width : other.m_width, | |
| 78 m_height > other.m_height ? m_height : other.m_height); | |
| 79 } | |
| 80 | |
| 81 IntSize shrunkTo(const IntSize& other) const | |
| 82 { | |
| 83 return IntSize(m_width < other.m_width ? m_width : other.m_width, | |
| 84 m_height < other.m_height ? m_height : other.m_height); | |
| 85 } | |
| 86 | |
| 87 void clampNegativeToZero() | |
| 88 { | |
| 89 *this = expandedTo(IntSize()); | |
| 90 } | |
| 91 | |
| 92 void clampToMinimumSize(const IntSize& minimumSize) | |
| 93 { | |
| 94 if (m_width < minimumSize.width()) | |
| 95 m_width = minimumSize.width(); | |
| 96 if (m_height < minimumSize.height()) | |
| 97 m_height = minimumSize.height(); | |
| 98 } | |
| 99 | |
| 100 int area() const | |
| 101 { | |
| 102 return m_width * m_height; | |
| 103 } | |
| 104 | |
| 105 int diagonalLengthSquared() const | |
| 106 { | |
| 107 return m_width * m_width + m_height * m_height; | |
| 108 } | |
| 109 | |
| 110 IntSize transposedSize() const | |
| 111 { | |
| 112 return IntSize(m_height, m_width); | |
| 113 } | |
| 114 | |
| 115 #if OS(MACOSX) | |
| 116 explicit IntSize(const CGSize&); // don't do this implicitly since it's loss
y | |
| 117 operator CGSize() const; | |
| 118 | |
| 119 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) | |
| 120 explicit IntSize(const NSSize &); // don't do this implicitly since it's los
sy | |
| 121 operator NSSize() const; | |
| 122 #endif | |
| 123 #endif | |
| 124 | |
| 125 private: | |
| 126 int m_width, m_height; | |
| 127 }; | |
| 128 | |
| 129 inline IntSize& operator+=(IntSize& a, const IntSize& b) | |
| 130 { | |
| 131 a.setWidth(a.width() + b.width()); | |
| 132 a.setHeight(a.height() + b.height()); | |
| 133 return a; | |
| 134 } | |
| 135 | |
| 136 inline IntSize& operator-=(IntSize& a, const IntSize& b) | |
| 137 { | |
| 138 a.setWidth(a.width() - b.width()); | |
| 139 a.setHeight(a.height() - b.height()); | |
| 140 return a; | |
| 141 } | |
| 142 | |
| 143 inline IntSize operator+(const IntSize& a, const IntSize& b) | |
| 144 { | |
| 145 return IntSize(a.width() + b.width(), a.height() + b.height()); | |
| 146 } | |
| 147 | |
| 148 inline IntSize operator-(const IntSize& a, const IntSize& b) | |
| 149 { | |
| 150 return IntSize(a.width() - b.width(), a.height() - b.height()); | |
| 151 } | |
| 152 | |
| 153 inline IntSize operator-(const IntSize& size) | |
| 154 { | |
| 155 return IntSize(-size.width(), -size.height()); | |
| 156 } | |
| 157 | |
| 158 inline bool operator==(const IntSize& a, const IntSize& b) | |
| 159 { | |
| 160 return a.width() == b.width() && a.height() == b.height(); | |
| 161 } | |
| 162 | |
| 163 inline bool operator!=(const IntSize& a, const IntSize& b) | |
| 164 { | |
| 165 return a.width() != b.width() || a.height() != b.height(); | |
| 166 } | |
| 167 | |
| 168 } // namespace WebCore | |
| 169 | |
| 170 #endif // IntSize_h | |
| OLD | NEW |