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 LayoutPoint_h 32#define LayoutPoint_h 33 34#include "FloatPoint.h" 35#include "LayoutSize.h" 36#include <wtf/MathExtras.h> 37 38#if PLATFORM(QT) 39#include <qglobal.h> 40QT_BEGIN_NAMESPACE 41class QPoint; 42class QPointF; 43QT_END_NAMESPACE 44#endif 45 46namespace WebCore { 47 48class LayoutPoint { 49public: 50 LayoutPoint() : m_x(0), m_y(0) { } 51 LayoutPoint(LayoutUnit x, LayoutUnit y) : m_x(x), m_y(y) { } 52 LayoutPoint(const IntPoint& point) : m_x(point.x()), m_y(point.y()) { } 53 explicit LayoutPoint(const FloatPoint& size) : m_x(size.x()), m_y(size.y()) { } 54 explicit LayoutPoint(const LayoutSize& size) : m_x(size.width()), m_y(size.height()) { } 55 56 static LayoutPoint zero() { return LayoutPoint(); } 57 58 LayoutUnit x() const { return m_x; } 59 LayoutUnit y() const { return m_y; } 60 61 void setX(LayoutUnit x) { m_x = x; } 62 void setY(LayoutUnit y) { m_y = y; } 63 64 void move(const LayoutSize& s) { move(s.width(), s.height()); } 65 void moveBy(const LayoutPoint& offset) { move(offset.x(), offset.y()); } 66 void move(LayoutUnit dx, LayoutUnit dy) { m_x += dx; m_y += dy; } 67 void scale(float sx, float sy) 68 { 69 m_x *= sx; 70 m_y *= sy; 71 } 72 73 LayoutPoint expandedTo(const LayoutPoint& other) const 74 { 75 return LayoutPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y)); 76 } 77 78 LayoutPoint shrunkTo(const LayoutPoint& other) const 79 { 80 return LayoutPoint(std::min(m_x, other.m_x), std::min(m_y, other.m_y)); 81 } 82 83 void clampNegativeToZero() 84 { 85 *this = expandedTo(zero()); 86 } 87 88 LayoutPoint transposedPoint() const 89 { 90 return LayoutPoint(m_y, m_x); 91 } 92 93#if PLATFORM(QT) 94 explicit LayoutPoint(const QPoint&); 95 explicit LayoutPoint(const QPointF&); 96 operator QPointF() const; 97#endif 98 99private: 100 LayoutUnit m_x, m_y; 101}; 102 103inline LayoutPoint& operator+=(LayoutPoint& a, const LayoutSize& b) 104{ 105 a.move(b.width(), b.height()); 106 return a; 107} 108 109inline LayoutPoint& operator-=(LayoutPoint& a, const LayoutSize& b) 110{ 111 a.move(-b.width(), -b.height()); 112 return a; 113} 114 115inline LayoutPoint operator+(const LayoutPoint& a, const LayoutSize& b) 116{ 117 return LayoutPoint(a.x() + b.width(), a.y() + b.height()); 118} 119 120inline LayoutPoint operator+(const LayoutPoint& a, const LayoutPoint& b) 121{ 122 return LayoutPoint(a.x() + b.x(), a.y() + b.y()); 123} 124 125inline LayoutSize operator-(const LayoutPoint& a, const LayoutPoint& b) 126{ 127 return LayoutSize(a.x() - b.x(), a.y() - b.y()); 128} 129 130inline LayoutPoint operator-(const LayoutPoint& a, const LayoutSize& b) 131{ 132 return LayoutPoint(a.x() - b.width(), a.y() - b.height()); 133} 134 135inline LayoutPoint operator-(const LayoutPoint& point) 136{ 137 return LayoutPoint(-point.x(), -point.y()); 138} 139 140inline bool operator==(const LayoutPoint& a, const LayoutPoint& b) 141{ 142 return a.x() == b.x() && a.y() == b.y(); 143} 144 145inline bool operator!=(const LayoutPoint& a, const LayoutPoint& b) 146{ 147 return a.x() != b.x() || a.y() != b.y(); 148} 149 150inline LayoutPoint toPoint(const LayoutSize& size) 151{ 152 return LayoutPoint(size.width(), size.height()); 153} 154 155inline LayoutPoint toLayoutPoint(const LayoutSize& p) 156{ 157 return LayoutPoint(p.width(), p.height()); 158} 159 160inline LayoutSize toSize(const LayoutPoint& a) 161{ 162 return LayoutSize(a.x(), a.y()); 163} 164 165inline IntPoint flooredIntPoint(const LayoutPoint& point) 166{ 167 return IntPoint(point.x().floor(), point.y().floor()); 168} 169 170inline IntPoint roundedIntPoint(const LayoutPoint& point) 171{ 172 return IntPoint(point.x().round(), point.y().round()); 173} 174 175inline IntPoint roundedIntPoint(const LayoutSize& size) 176{ 177 return IntPoint(size.width().round(), size.height().round()); 178} 179 180inline IntPoint ceiledIntPoint(const LayoutPoint& point) 181{ 182 return IntPoint(point.x().ceil(), point.y().ceil()); 183} 184 185inline LayoutPoint flooredLayoutPoint(const FloatPoint& p) 186{ 187 return LayoutPoint(LayoutUnit::fromFloatFloor(p.x()), LayoutUnit::fromFloatFloor(p.y())); 188} 189 190inline LayoutPoint ceiledLayoutPoint(const FloatPoint& p) 191{ 192 return LayoutPoint(LayoutUnit::fromFloatCeil(p.x()), LayoutUnit::fromFloatCeil(p.y())); 193} 194 195inline IntSize pixelSnappedIntSize(const LayoutSize& s, const LayoutPoint& p) 196{ 197 return IntSize(snapSizeToPixel(s.width(), p.x()), snapSizeToPixel(s.height(), p.y())); 198} 199 200inline LayoutPoint roundedLayoutPoint(const FloatPoint& p) 201{ 202#if ENABLE(SUBPIXEL_LAYOUT) 203 return LayoutPoint(p); 204#else 205 return roundedIntPoint(p); 206#endif 207} 208 209inline LayoutSize toLayoutSize(const LayoutPoint& p) 210{ 211 return LayoutSize(p.x(), p.y()); 212} 213 214inline LayoutPoint flooredLayoutPoint(const FloatSize& s) 215{ 216 return flooredLayoutPoint(FloatPoint(s)); 217} 218 219 220} // namespace WebCore 221 222#endif // LayoutPoint_h 223