2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "web/ViewportAnchor.h"
34 #include "core/dom/ContainerNode.h"
35 #include "core/dom/Node.h"
36 #include "core/page/EventHandler.h"
37 #include "core/rendering/HitTestResult.h"
39 using namespace WebCore;
45 static const float viewportAnchorRelativeEpsilon = 0.1f;
46 static const int viewportToNodeMaxRelativeArea = 2;
48 template <typename RectType>
49 int area(const RectType& rect) {
50 return rect.width() * rect.height();
53 Node* findNonEmptyAnchorNode(const IntPoint& point, const IntRect& viewRect, EventHandler* eventHandler)
55 Node* node = eventHandler->hitTestResultAtPoint(point, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent).innerNode();
57 // If the node bounding box is sufficiently large, make a single attempt to
58 // find a smaller node; the larger the node bounds, the greater the
59 // variability under resize.
60 const int maxNodeArea = area(viewRect) * viewportToNodeMaxRelativeArea;
61 if (node && area(node->boundingBox()) > maxNodeArea) {
62 IntSize pointOffset = viewRect.size();
63 pointOffset.scale(viewportAnchorRelativeEpsilon);
64 node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent).innerNode();
67 while (node && node->boundingBox().isEmpty())
68 node = node->parentNode();
75 ViewportAnchor::ViewportAnchor(EventHandler* eventHandler)
76 : m_eventHandler(eventHandler) { }
78 void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorInViewCoords)
80 m_viewRect = viewRect;
82 m_anchorNodeBounds = LayoutRect();
83 m_anchorInNodeCoords = FloatSize();
84 m_anchorInViewCoords = anchorInViewCoords;
86 if (viewRect.isEmpty())
89 // Preserve origins at the absolute screen origin
90 if (viewRect.location() == IntPoint::zero())
93 FloatSize anchorOffset = viewRect.size();
94 anchorOffset.scale(anchorInViewCoords.width(), anchorInViewCoords.height());
95 const FloatPoint anchorPoint = FloatPoint(viewRect.location()) + anchorOffset;
97 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), viewRect, m_eventHandler);
102 m_anchorNodeBounds = node->boundingBox();
103 m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location();
104 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorNodeBounds.height());
107 IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const
109 if (!m_anchorNode || !m_anchorNode->inDocument())
110 return m_viewRect.location();
112 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox();
113 if (m_anchorNodeBounds == currentNodeBounds)
114 return m_viewRect.location();
116 // Compute the new anchor point relative to the node position
117 FloatSize anchorOffsetFromNode = currentNodeBounds.size();
118 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoords.height());
119 FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode;
121 // Compute the new origin point relative to the new anchor point
122 FloatSize anchorOffsetFromOrigin = currentViewSize;
123 anchorOffsetFromOrigin.scale(m_anchorInViewCoords.width(), m_anchorInViewCoords.height());
124 return flooredIntPoint(anchorPoint - anchorOffsetFromOrigin);