| 1 | /* |
| 2 | * Copyright (c) 2012, Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "FloatSize.h" |
| 34 | #include "IntSize.h" |
| 35 | #include "LayoutUnit.h" |
| 36 | |
| 37 | namespace WTF { |
| 38 | class TextStream; |
| 39 | } |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | class LayoutPoint; |
| 44 | |
| 45 | enum AspectRatioFit { |
| 46 | AspectRatioFitShrink, |
| 47 | AspectRatioFitGrow |
| 48 | }; |
| 49 | |
| 50 | class LayoutSize { |
| 51 | public: |
| 52 | LayoutSize() : m_width(0), m_height(0) { } |
| 53 | LayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { } |
| 54 | LayoutSize(LayoutUnit width, LayoutUnit height) : m_width(width), m_height(height) { } |
| 55 | |
| 56 | explicit LayoutSize(const FloatSize& size) : m_width(size.width()), m_height(size.height()) { } |
| 57 | |
| 58 | LayoutUnit width() const { return m_width; } |
| 59 | LayoutUnit height() const { return m_height; } |
| 60 | |
| 61 | void setWidth(LayoutUnit width) { m_width = width; } |
| 62 | void setHeight(LayoutUnit height) { m_height = height; } |
| 63 | |
| 64 | bool isEmpty() const { return m_width <= 0 || m_height <= 0; } |
| 65 | bool isZero() const { return !m_width && !m_height; } |
| 66 | |
| 67 | float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); } |
| 68 | |
| 69 | void expand(LayoutUnit width, LayoutUnit height) |
| 70 | { |
| 71 | m_width += width; |
| 72 | m_height += height; |
| 73 | } |
| 74 | |
| 75 | void shrink(LayoutUnit width, LayoutUnit height) |
| 76 | { |
| 77 | m_width -= width; |
| 78 | m_height -= height; |
| 79 | } |
| 80 | |
| 81 | void scale(float scale) |
| 82 | { |
| 83 | m_width *= scale; |
| 84 | m_height *= scale; |
| 85 | } |
| 86 | |
| 87 | void scale(float widthScale, float heightScale) |
| 88 | { |
| 89 | m_width *= widthScale; |
| 90 | m_height *= heightScale; |
| 91 | } |
| 92 | |
| 93 | LayoutSize constrainedBetween(const LayoutSize& min, const LayoutSize& max) const; |
| 94 | |
| 95 | LayoutSize expandedTo(const LayoutSize& other) const |
| 96 | { |
| 97 | return LayoutSize(m_width > other.m_width ? m_width : other.m_width, |
| 98 | m_height > other.m_height ? m_height : other.m_height); |
| 99 | } |
| 100 | |
| 101 | LayoutSize shrunkTo(const LayoutSize& other) const |
| 102 | { |
| 103 | return LayoutSize(m_width < other.m_width ? m_width : other.m_width, |
| 104 | m_height < other.m_height ? m_height : other.m_height); |
| 105 | } |
| 106 | |
| 107 | void clampNegativeToZero() |
| 108 | { |
| 109 | *this = expandedTo(LayoutSize()); |
| 110 | } |
| 111 | |
| 112 | void clampToMinimumSize(const LayoutSize& minimumSize) |
| 113 | { |
| 114 | if (m_width < minimumSize.width()) |
| 115 | m_width = minimumSize.width(); |
| 116 | if (m_height < minimumSize.height()) |
| 117 | m_height = minimumSize.height(); |
| 118 | } |
| 119 | |
| 120 | LayoutSize transposedSize() const |
| 121 | { |
| 122 | return LayoutSize(m_height, m_width); |
| 123 | } |
| 124 | |
| 125 | operator FloatSize() const { return FloatSize(m_width, m_height); } |
| 126 | |
| 127 | LayoutSize fitToAspectRatio(const LayoutSize& aspectRatio, AspectRatioFit fit) const |
| 128 | { |
| 129 | float heightScale = height().toFloat() / aspectRatio.height().toFloat(); |
| 130 | float widthScale = width().toFloat() / aspectRatio.width().toFloat(); |
| 131 | |
| 132 | if ((widthScale > heightScale) != (fit == AspectRatioFitGrow)) |
| 133 | return LayoutSize(height() * aspectRatio.width() / aspectRatio.height(), height()); |
| 134 | |
| 135 | return LayoutSize(width(), width() * aspectRatio.height() / aspectRatio.width()); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | LayoutUnit m_width; |
| 140 | LayoutUnit m_height; |
| 141 | }; |
| 142 | |
| 143 | inline LayoutSize& operator+=(LayoutSize& a, const LayoutSize& b) |
| 144 | { |
| 145 | a.setWidth(a.width() + b.width()); |
| 146 | a.setHeight(a.height() + b.height()); |
| 147 | return a; |
| 148 | } |
| 149 | |
| 150 | inline LayoutSize& operator-=(LayoutSize& a, const LayoutSize& b) |
| 151 | { |
| 152 | a.setWidth(a.width() - b.width()); |
| 153 | a.setHeight(a.height() - b.height()); |
| 154 | return a; |
| 155 | } |
| 156 | |
| 157 | inline LayoutSize operator+(const LayoutSize& a, const LayoutSize& b) |
| 158 | { |
| 159 | return LayoutSize(a.width() + b.width(), a.height() + b.height()); |
| 160 | } |
| 161 | |
| 162 | inline LayoutSize operator-(const LayoutSize& a, const LayoutSize& b) |
| 163 | { |
| 164 | return LayoutSize(a.width() - b.width(), a.height() - b.height()); |
| 165 | } |
| 166 | |
| 167 | inline LayoutSize operator-(const LayoutSize& size) |
| 168 | { |
| 169 | return LayoutSize(-size.width(), -size.height()); |
| 170 | } |
| 171 | |
| 172 | inline bool operator==(const LayoutSize& a, const LayoutSize& b) |
| 173 | { |
| 174 | return a.width() == b.width() && a.height() == b.height(); |
| 175 | } |
| 176 | |
| 177 | inline bool operator!=(const LayoutSize& a, const LayoutSize& b) |
| 178 | { |
| 179 | return a.width() != b.width() || a.height() != b.height(); |
| 180 | } |
| 181 | |
| 182 | inline IntSize flooredIntSize(const LayoutSize& s) |
| 183 | { |
| 184 | return IntSize(s.width().floor(), s.height().floor()); |
| 185 | } |
| 186 | |
| 187 | inline IntSize roundedIntSize(const LayoutSize& s) |
| 188 | { |
| 189 | return IntSize(s.width().round(), s.height().round()); |
| 190 | } |
| 191 | |
| 192 | inline FloatSize floorSizeToDevicePixels(const LayoutSize& size, float pixelSnappingFactor) |
| 193 | { |
| 194 | return FloatSize(floorToDevicePixel(size.width(), pixelSnappingFactor), floorToDevicePixel(size.height(), pixelSnappingFactor)); |
| 195 | } |
| 196 | |
| 197 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const LayoutSize&); |
| 198 | |
| 199 | } // namespace WebCore |
| 200 | |
| 201 | |