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"
25 #include "HTMLNames.h"
27 #include "XLinkNames.h"
31 using namespace HTMLNames;
33 HitTestLocation::HitTestLocation()
34 : m_isRectBased(false)
35 , m_isRectilinear(true)
39 HitTestLocation::HitTestLocation(const LayoutPoint& point)
41 , m_boundingBox(rectForPoint(point, 0, 0, 0, 0))
42 , m_transformedPoint(point)
43 , m_transformedRect(m_boundingBox)
44 , m_isRectBased(false)
45 , m_isRectilinear(true)
49 HitTestLocation::HitTestLocation(const FloatPoint& point)
50 : m_point(flooredLayoutPoint(point))
51 , m_boundingBox(rectForPoint(m_point, 0, 0, 0, 0))
52 , m_transformedPoint(point)
53 , m_transformedRect(m_boundingBox)
54 , m_isRectBased(false)
55 , m_isRectilinear(true)
59 HitTestLocation::HitTestLocation(const FloatPoint& point, const FloatQuad& quad)
60 : m_transformedPoint(point)
61 , m_transformedRect(quad)
64 m_point = flooredLayoutPoint(point);
65 m_boundingBox = enclosingIntRect(quad.boundingBox());
66 m_isRectilinear = quad.isRectilinear();
69 HitTestLocation::HitTestLocation(const LayoutPoint& centerPoint, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
70 : m_point(centerPoint)
71 , m_boundingBox(rectForPoint(centerPoint, topPadding, rightPadding, bottomPadding, leftPadding))
72 , m_transformedPoint(centerPoint)
73 , m_isRectBased(topPadding || rightPadding || bottomPadding || leftPadding)
74 , m_isRectilinear(true)
76 m_transformedRect = FloatQuad(m_boundingBox);
79 HitTestLocation::HitTestLocation(const HitTestLocation& other, const LayoutSize& offset)
80 : m_point(other.m_point)
81 , m_boundingBox(other.m_boundingBox)
82 , m_transformedPoint(other.m_transformedPoint)
83 , m_transformedRect(other.m_transformedRect)
84 , m_isRectBased(other.m_isRectBased)
85 , m_isRectilinear(other.m_isRectilinear)
90 HitTestLocation::HitTestLocation(const HitTestLocation& other)
91 : m_point(other.m_point)
92 , m_boundingBox(other.m_boundingBox)
93 , m_transformedPoint(other.m_transformedPoint)
94 , m_transformedRect(other.m_transformedRect)
95 , m_isRectBased(other.m_isRectBased)
96 , m_isRectilinear(other.m_isRectilinear)
100 HitTestLocation::~HitTestLocation()
104 HitTestLocation& HitTestLocation::operator=(const HitTestLocation& other)
106 m_point = other.m_point;
107 m_boundingBox = other.m_boundingBox;
108 m_transformedPoint = other.m_transformedPoint;
109 m_transformedRect = other.m_transformedRect;
110 m_isRectBased = other.m_isRectBased;
111 m_isRectilinear = other.m_isRectilinear;
116 void HitTestLocation::move(const LayoutSize& offset)
118 m_point.move(offset);
119 m_transformedPoint.move(offset);
120 m_transformedRect.move(offset);
121 m_boundingBox = enclosingIntRect(m_transformedRect.boundingBox());
124 template<typename RectType>
125 bool HitTestLocation::intersectsRect(const RectType& rect) const
127 // FIXME: When the hit test is not rect based we should use rect.contains(m_point).
128 // That does change some corner case tests though.
130 // First check if rect even intersects our bounding box.
131 if (!rect.intersects(m_boundingBox))
134 // If the transformed rect is rectilinear the bounding box intersection was accurate.
138 // If rect fully contains our bounding box, we are also sure of an intersection.
139 if (rect.contains(m_boundingBox))
142 // Otherwise we need to do a slower quad based intersection test.
143 return m_transformedRect.intersectsRect(rect);
146 bool HitTestLocation::intersects(const LayoutRect& rect) const
148 return intersectsRect(rect);
151 bool HitTestLocation::intersects(const FloatRect& rect) const
153 return intersectsRect(rect);
156 bool HitTestLocation::intersects(const RoundedRect& rect) const
158 return rect.intersectsQuad(m_transformedRect);
161 IntRect HitTestLocation::rectForPoint(const LayoutPoint& point, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
163 IntPoint actualPoint(flooredIntPoint(point));
164 actualPoint -= IntSize(leftPadding, topPadding);
166 IntSize actualPadding(leftPadding + rightPadding, topPadding + bottomPadding);
167 // As IntRect is left inclusive and right exclusive (seeing IntRect::contains(x, y)), adding "1".
168 // FIXME: Remove this once non-rect based hit-detection stops using IntRect:intersects.
169 actualPadding += IntSize(1, 1);
171 return IntRect(actualPoint, actualPadding);
174 } // namespace WebCore