2 * Copyright (C) 2006, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 #include "core/rendering/HitTestLocation.h"
27 HitTestLocation::HitTestLocation()
28 : m_isRectBased(false)
29 , m_isRectilinear(true)
33 HitTestLocation::HitTestLocation(const LayoutPoint& point)
35 , m_boundingBox(rectForPoint(point, 0, 0, 0, 0))
36 , m_transformedPoint(point)
37 , m_transformedRect(m_boundingBox)
38 , m_isRectBased(false)
39 , m_isRectilinear(true)
43 HitTestLocation::HitTestLocation(const FloatPoint& point)
44 : m_point(flooredLayoutPoint(point))
45 , m_boundingBox(rectForPoint(m_point, 0, 0, 0, 0))
46 , m_transformedPoint(point)
47 , m_transformedRect(m_boundingBox)
48 , m_isRectBased(false)
49 , m_isRectilinear(true)
53 HitTestLocation::HitTestLocation(const FloatPoint& point, const FloatQuad& quad)
54 : m_transformedPoint(point)
55 , m_transformedRect(quad)
58 m_point = flooredLayoutPoint(point);
59 m_boundingBox = enclosingIntRect(quad.boundingBox());
60 m_isRectilinear = quad.isRectilinear();
63 HitTestLocation::HitTestLocation(const LayoutPoint& centerPoint, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
64 : m_point(centerPoint)
65 , m_boundingBox(rectForPoint(centerPoint, topPadding, rightPadding, bottomPadding, leftPadding))
66 , m_transformedPoint(centerPoint)
67 , m_isRectBased(topPadding || rightPadding || bottomPadding || leftPadding)
68 , m_isRectilinear(true)
70 m_transformedRect = FloatQuad(m_boundingBox);
73 HitTestLocation::HitTestLocation(const HitTestLocation& other, const LayoutSize& offset)
74 : m_point(other.m_point)
75 , m_boundingBox(other.m_boundingBox)
76 , m_transformedPoint(other.m_transformedPoint)
77 , m_transformedRect(other.m_transformedRect)
78 , m_isRectBased(other.m_isRectBased)
79 , m_isRectilinear(other.m_isRectilinear)
84 HitTestLocation::HitTestLocation(const HitTestLocation& other)
85 : m_point(other.m_point)
86 , m_boundingBox(other.m_boundingBox)
87 , m_transformedPoint(other.m_transformedPoint)
88 , m_transformedRect(other.m_transformedRect)
89 , m_isRectBased(other.m_isRectBased)
90 , m_isRectilinear(other.m_isRectilinear)
94 HitTestLocation::~HitTestLocation()
98 HitTestLocation& HitTestLocation::operator=(const HitTestLocation& other)
100 m_point = other.m_point;
101 m_boundingBox = other.m_boundingBox;
102 m_transformedPoint = other.m_transformedPoint;
103 m_transformedRect = other.m_transformedRect;
104 m_isRectBased = other.m_isRectBased;
105 m_isRectilinear = other.m_isRectilinear;
110 void HitTestLocation::move(const LayoutSize& offset)
112 m_point.move(offset);
113 m_transformedPoint.move(offset);
114 m_transformedRect.move(offset);
115 m_boundingBox = enclosingIntRect(m_transformedRect.boundingBox());
118 template<typename RectType>
119 bool HitTestLocation::intersectsRect(const RectType& rect) const
121 // FIXME: When the hit test is not rect based we should use rect.contains(m_point).
122 // That does change some corner case tests though.
124 // First check if rect even intersects our bounding box.
125 if (!rect.intersects(m_boundingBox))
128 // If the transformed rect is rectilinear the bounding box intersection was accurate.
132 // If rect fully contains our bounding box, we are also sure of an intersection.
133 if (rect.contains(m_boundingBox))
136 // Otherwise we need to do a slower quad based intersection test.
137 return m_transformedRect.intersectsRect(rect);
140 bool HitTestLocation::intersects(const LayoutRect& rect) const
142 return intersectsRect(rect);
145 bool HitTestLocation::intersects(const FloatRect& rect) const
147 return intersectsRect(rect);
150 bool HitTestLocation::intersects(const RoundedRect& rect) const
152 return rect.intersectsQuad(m_transformedRect);
155 IntRect HitTestLocation::rectForPoint(const LayoutPoint& point, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
157 IntPoint actualPoint(flooredIntPoint(point));
158 actualPoint -= IntSize(leftPadding, topPadding);
160 IntSize actualPadding(leftPadding + rightPadding, topPadding + bottomPadding);
161 // As IntRect is left inclusive and right exclusive (seeing IntRect::contains(x, y)), adding "1".
162 // FIXME: Remove this once non-rect based hit-detection stops using IntRect:intersects.
163 actualPadding += IntSize(1, 1);
165 return IntRect(actualPoint, actualPadding);
168 } // namespace WebCore