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