| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 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 #include "config.h" | |
| 32 #include "web/ViewportAnchor.h" | |
| 33 | |
| 34 #include "core/dom/ContainerNode.h" | |
| 35 #include "core/dom/Node.h" | |
| 36 #include "core/page/EventHandler.h" | |
| 37 #include "core/rendering/HitTestResult.h" | |
| 38 | |
| 39 namespace blink { | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 static const float viewportAnchorRelativeEpsilon = 0.1f; | |
| 44 static const int viewportToNodeMaxRelativeArea = 2; | |
| 45 | |
| 46 template <typename RectType> | |
| 47 int area(const RectType& rect) { | |
| 48 return rect.width() * rect.height(); | |
| 49 } | |
| 50 | |
| 51 Node* findNonEmptyAnchorNode(const IntPoint& point, const IntRect& viewRect, Eve
ntHandler* eventHandler) | |
| 52 { | |
| 53 Node* node = eventHandler->hitTestResultAtPoint(point, HitTestRequest::ReadO
nly | HitTestRequest::Active).innerNode(); | |
| 54 | |
| 55 // If the node bounding box is sufficiently large, make a single attempt to | |
| 56 // find a smaller node; the larger the node bounds, the greater the | |
| 57 // variability under resize. | |
| 58 const int maxNodeArea = area(viewRect) * viewportToNodeMaxRelativeArea; | |
| 59 if (node && area(node->boundingBox()) > maxNodeArea) { | |
| 60 IntSize pointOffset = viewRect.size(); | |
| 61 pointOffset.scale(viewportAnchorRelativeEpsilon); | |
| 62 node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRe
quest::ReadOnly | HitTestRequest::Active).innerNode(); | |
| 63 } | |
| 64 | |
| 65 while (node && node->boundingBox().isEmpty()) | |
| 66 node = node->parentNode(); | |
| 67 | |
| 68 return node; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 ViewportAnchor::ViewportAnchor(EventHandler* eventHandler) | |
| 74 : m_eventHandler(eventHandler) { } | |
| 75 | |
| 76 void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorI
nViewCoords) | |
| 77 { | |
| 78 m_viewRect = viewRect; | |
| 79 m_anchorNode.clear(); | |
| 80 m_anchorNodeBounds = LayoutRect(); | |
| 81 m_anchorInNodeCoords = FloatSize(); | |
| 82 m_anchorInViewCoords = anchorInViewCoords; | |
| 83 | |
| 84 if (viewRect.isEmpty()) | |
| 85 return; | |
| 86 | |
| 87 // Preserve origins at the absolute screen origin | |
| 88 if (viewRect.location() == IntPoint::zero()) | |
| 89 return; | |
| 90 | |
| 91 FloatSize anchorOffset = viewRect.size(); | |
| 92 anchorOffset.scale(anchorInViewCoords.width(), anchorInViewCoords.height()); | |
| 93 const FloatPoint anchorPoint = FloatPoint(viewRect.location()) + anchorOffse
t; | |
| 94 | |
| 95 Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), viewRect,
m_eventHandler); | |
| 96 if (!node) | |
| 97 return; | |
| 98 | |
| 99 m_anchorNode = node; | |
| 100 m_anchorNodeBounds = node->boundingBox(); | |
| 101 m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location(); | |
| 102 m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorN
odeBounds.height()); | |
| 103 } | |
| 104 | |
| 105 IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const | |
| 106 { | |
| 107 if (!m_anchorNode || !m_anchorNode->inDocument()) | |
| 108 return m_viewRect.location(); | |
| 109 | |
| 110 const LayoutRect currentNodeBounds = m_anchorNode->boundingBox(); | |
| 111 if (m_anchorNodeBounds == currentNodeBounds) | |
| 112 return m_viewRect.location(); | |
| 113 | |
| 114 // Compute the new anchor point relative to the node position | |
| 115 FloatSize anchorOffsetFromNode = currentNodeBounds.size(); | |
| 116 anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoord
s.height()); | |
| 117 FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode
; | |
| 118 | |
| 119 // Compute the new origin point relative to the new anchor point | |
| 120 FloatSize anchorOffsetFromOrigin = currentViewSize; | |
| 121 anchorOffsetFromOrigin.scale(m_anchorInViewCoords.width(), m_anchorInViewCoo
rds.height()); | |
| 122 return flooredIntPoint(anchorPoint - anchorOffsetFromOrigin); | |
| 123 } | |
| 124 | |
| 125 } // namespace blink | |
| OLD | NEW |