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