1/*
2 * Copyright (C) 2006, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
4 *
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.
9 *
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.
14 *
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.
19 *
20*/
21
22#include "config.h"
23#include "HitTestLocation.h"
24
25#include "CachedImage.h"
26#include "DocumentMarkerController.h"
27#include "Frame.h"
28#include "FrameSelection.h"
29#include "FrameTree.h"
30#include "HTMLAnchorElement.h"
31#include "HTMLImageElement.h"
32#include "HTMLInputElement.h"
33#include "HTMLMediaElement.h"
34#include "HTMLNames.h"
35#include "HTMLParserIdioms.h"
36#include "HTMLPlugInImageElement.h"
37#include "HTMLVideoElement.h"
38#include "RenderBlock.h"
39#include "RenderImage.h"
40#include "RenderInline.h"
41#include "Scrollbar.h"
42#include "SVGNames.h"
43#include "XLinkNames.h"
44
45namespace WebCore {
46
47using namespace HTMLNames;
48
49HitTestLocation::HitTestLocation()
50    : m_isRectBased(false)
51    , m_isRectilinear(true)
52{
53}
54
55HitTestLocation::HitTestLocation(const LayoutPoint& point)
56    : m_point(point)
57    , m_boundingBox(rectForPoint(point, 0, 0, 0, 0))
58    , m_transformedPoint(point)
59    , m_transformedRect(m_boundingBox)
60    , m_isRectBased(false)
61    , m_isRectilinear(true)
62{
63}
64
65HitTestLocation::HitTestLocation(const FloatPoint& point)
66    : m_point(flooredLayoutPoint(point))
67    , m_boundingBox(rectForPoint(m_point, 0, 0, 0, 0))
68    , m_transformedPoint(point)
69    , m_transformedRect(m_boundingBox)
70    , m_isRectBased(false)
71    , m_isRectilinear(true)
72{
73}
74
75HitTestLocation::HitTestLocation(const FloatPoint& point, const FloatQuad& quad)
76    : m_transformedPoint(point)
77    , m_transformedRect(quad)
78    , m_isRectBased(true)
79{
80    m_point = flooredLayoutPoint(point);
81    m_boundingBox = enclosingIntRect(quad.boundingBox());
82    m_isRectilinear = quad.isRectilinear();
83}
84
85HitTestLocation::HitTestLocation(const LayoutPoint& centerPoint, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
86    : m_point(centerPoint)
87    , m_boundingBox(rectForPoint(centerPoint, topPadding, rightPadding, bottomPadding, leftPadding))
88    , m_transformedPoint(centerPoint)
89    , m_isRectBased(topPadding || rightPadding || bottomPadding || leftPadding)
90    , m_isRectilinear(true)
91{
92    m_transformedRect = FloatQuad(m_boundingBox);
93}
94
95HitTestLocation::HitTestLocation(const HitTestLocation& other, const LayoutSize& offset)
96    : m_point(other.m_point)
97    , m_boundingBox(other.m_boundingBox)
98    , m_transformedPoint(other.m_transformedPoint)
99    , m_transformedRect(other.m_transformedRect)
100    , m_isRectBased(other.m_isRectBased)
101    , m_isRectilinear(other.m_isRectilinear)
102{
103    move(offset);
104}
105
106HitTestLocation::HitTestLocation(const HitTestLocation& other)
107    : m_point(other.m_point)
108    , m_boundingBox(other.m_boundingBox)
109    , m_transformedPoint(other.m_transformedPoint)
110    , m_transformedRect(other.m_transformedRect)
111    , m_isRectBased(other.m_isRectBased)
112    , m_isRectilinear(other.m_isRectilinear)
113{
114}
115
116HitTestLocation::~HitTestLocation()
117{
118}
119
120HitTestLocation& HitTestLocation::operator=(const HitTestLocation& other)
121{
122    m_point = other.m_point;
123    m_boundingBox = other.m_boundingBox;
124    m_transformedPoint = other.m_transformedPoint;
125    m_transformedRect = other.m_transformedRect;
126    m_isRectBased = other.m_isRectBased;
127    m_isRectilinear = other.m_isRectilinear;
128
129    return *this;
130}
131
132void HitTestLocation::move(const LayoutSize& offset)
133{
134    m_point.move(offset);
135    m_transformedPoint.move(offset);
136    m_transformedRect.move(offset);
137    m_boundingBox = enclosingIntRect(m_transformedRect.boundingBox());
138}
139
140template<typename RectType>
141bool HitTestLocation::intersectsRect(const RectType& rect) const
142{
143    // FIXME: When the hit test is not rect based we should use rect.contains(m_point).
144    // That does change some corner case tests though.
145
146    // First check if rect even intersects our bounding box.
147    if (!rect.intersects(m_boundingBox))
148        return false;
149
150    // If the transformed rect is rectilinear the bounding box intersection was accurate.
151    if (m_isRectilinear)
152        return true;
153
154    // If rect fully contains our bounding box, we are also sure of an intersection.
155    if (rect.contains(m_boundingBox))
156        return true;
157
158    // Otherwise we need to do a slower quad based intersection test.
159    return m_transformedRect.intersectsRect(rect);
160}
161
162bool HitTestLocation::intersects(const LayoutRect& rect) const
163{
164    return intersectsRect(rect);
165}
166
167bool HitTestLocation::intersects(const FloatRect& rect) const
168{
169    return intersectsRect(rect);
170}
171
172bool HitTestLocation::intersects(const RoundedRect& rect) const
173{
174    return rect.intersectsQuad(m_transformedRect);
175}
176
177IntRect HitTestLocation::rectForPoint(const LayoutPoint& point, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
178{
179    IntPoint actualPoint(flooredIntPoint(point));
180    actualPoint -= IntSize(leftPadding, topPadding);
181
182    IntSize actualPadding(leftPadding + rightPadding, topPadding + bottomPadding);
183    // As IntRect is left inclusive and right exclusive (seeing IntRect::contains(x, y)), adding "1".
184    // FIXME: Remove this once non-rect based hit-detection stops using IntRect:intersects.
185    actualPadding += IntSize(1, 1);
186
187    return IntRect(actualPoint, actualPadding);
188}
189
190} // namespace WebCore
191