Home | History | Annotate | Download | only in graphics
      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 #ifndef LayoutSize_h
     32 #define LayoutSize_h
     33 
     34 #include "core/platform/LayoutUnit.h"
     35 #include "core/platform/graphics/FloatSize.h"
     36 #include "core/platform/graphics/IntSize.h"
     37 
     38 namespace WebCore {
     39 
     40 class LayoutPoint;
     41 
     42 class LayoutSize {
     43 public:
     44     LayoutSize() : m_width(0), m_height(0) { }
     45     LayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
     46     LayoutSize(LayoutUnit width, LayoutUnit height) : m_width(width), m_height(height) { }
     47 
     48     explicit LayoutSize(const FloatSize& size) : m_width(size.width()), m_height(size.height()) { }
     49 
     50     LayoutUnit width() const { return m_width; }
     51     LayoutUnit height() const { return m_height; }
     52 
     53     void setWidth(LayoutUnit width) { m_width = width; }
     54     void setHeight(LayoutUnit height) { m_height = height; }
     55 
     56     bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
     57     bool isZero() const { return !m_width && !m_height; }
     58 
     59     float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
     60 
     61     void expand(LayoutUnit width, LayoutUnit height)
     62     {
     63         m_width += width;
     64         m_height += height;
     65     }
     66 
     67     void shrink(LayoutUnit width, LayoutUnit height)
     68     {
     69         m_width -= width;
     70         m_height -= height;
     71     }
     72 
     73     void scale(float scale)
     74     {
     75         m_width *= scale;
     76         m_height *= scale;
     77     }
     78 
     79     void scale(float widthScale, float heightScale)
     80     {
     81         m_width *= widthScale;
     82         m_height *= heightScale;
     83     }
     84 
     85     LayoutSize expandedTo(const LayoutSize& other) const
     86     {
     87         return LayoutSize(m_width > other.m_width ? m_width : other.m_width,
     88             m_height > other.m_height ? m_height : other.m_height);
     89     }
     90 
     91     LayoutSize shrunkTo(const LayoutSize& other) const
     92     {
     93         return LayoutSize(m_width < other.m_width ? m_width : other.m_width,
     94             m_height < other.m_height ? m_height : other.m_height);
     95     }
     96 
     97     void clampNegativeToZero()
     98     {
     99         *this = expandedTo(LayoutSize());
    100     }
    101 
    102     void clampToMinimumSize(const LayoutSize& minimumSize)
    103     {
    104         if (m_width < minimumSize.width())
    105             m_width = minimumSize.width();
    106         if (m_height < minimumSize.height())
    107             m_height = minimumSize.height();
    108     }
    109 
    110     LayoutSize transposedSize() const
    111     {
    112         return LayoutSize(m_height, m_width);
    113     }
    114 
    115 private:
    116     LayoutUnit m_width, m_height;
    117 };
    118 
    119 inline LayoutSize& operator+=(LayoutSize& a, const LayoutSize& b)
    120 {
    121     a.setWidth(a.width() + b.width());
    122     a.setHeight(a.height() + b.height());
    123     return a;
    124 }
    125 
    126 inline LayoutSize& operator-=(LayoutSize& a, const LayoutSize& b)
    127 {
    128     a.setWidth(a.width() - b.width());
    129     a.setHeight(a.height() - b.height());
    130     return a;
    131 }
    132 
    133 inline LayoutSize operator+(const LayoutSize& a, const LayoutSize& b)
    134 {
    135     return LayoutSize(a.width() + b.width(), a.height() + b.height());
    136 }
    137 
    138 inline LayoutSize operator-(const LayoutSize& a, const LayoutSize& b)
    139 {
    140     return LayoutSize(a.width() - b.width(), a.height() - b.height());
    141 }
    142 
    143 inline LayoutSize operator-(const LayoutSize& size)
    144 {
    145     return LayoutSize(-size.width(), -size.height());
    146 }
    147 
    148 inline bool operator==(const LayoutSize& a, const LayoutSize& b)
    149 {
    150     return a.width() == b.width() && a.height() == b.height();
    151 }
    152 
    153 inline bool operator!=(const LayoutSize& a, const LayoutSize& b)
    154 {
    155     return a.width() != b.width() || a.height() != b.height();
    156 }
    157 
    158 inline IntSize flooredIntSize(const LayoutSize& s)
    159 {
    160     return IntSize(s.width().floor(), s.height().floor());
    161 }
    162 
    163 inline IntSize roundedIntSize(const LayoutSize& s)
    164 {
    165     return IntSize(s.width().round(), s.height().round());
    166 }
    167 
    168 inline LayoutSize roundedLayoutSize(const FloatSize& s)
    169 {
    170     return LayoutSize(s);
    171 }
    172 
    173 } // namespace WebCore
    174 
    175 #endif // LayoutSize_h
    176