Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(728)

Unified Diff: third_party/WebKit/Source/core/layout/IntersectionGeometry.cpp

Issue 2553103004: Refactor IntersectionGeometry class and move it to core/layout. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/layout/IntersectionGeometry.cpp
diff --git a/third_party/WebKit/Source/core/layout/IntersectionGeometry.cpp b/third_party/WebKit/Source/core/layout/IntersectionGeometry.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f217e015dd9516ea18720f2af24bad2a83653996
--- /dev/null
+++ b/third_party/WebKit/Source/core/layout/IntersectionGeometry.cpp
@@ -0,0 +1,224 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
xjz 2016/12/07 01:01:14 Can you adjust the git similarity setting when upl
szager1 2016/12/07 01:30:49 Will do.
+
+#include "core/layout/IntersectionGeometry.h"
+
+#include "core/frame/FrameView.h"
+#include "core/frame/LocalFrame.h"
+#include "core/html/HTMLFrameOwnerElement.h"
+#include "core/layout/LayoutBox.h"
+#include "core/layout/LayoutView.h"
+#include "core/layout/api/LayoutAPIShim.h"
+#include "core/layout/api/LayoutViewItem.h"
+#include "core/paint/PaintLayer.h"
+
+namespace blink {
+
+namespace {
+
+bool isContainingBlockChainDescendant(LayoutObject* descendant,
+ LayoutObject* ancestor) {
+ LocalFrame* ancestorFrame = ancestor->document().frame();
+ LocalFrame* descendantFrame = descendant->document().frame();
+ if (!ancestorFrame)
+ return false;
+
+ while (descendantFrame && descendantFrame != ancestorFrame) {
+ HTMLFrameOwnerElement* owner = descendant->document().localOwner();
+ if (!owner)
+ return false;
+ descendant = owner->layoutObject();
+ if (!descendant)
+ return false;
+ descendantFrame = descendant->document().frame();
+ }
+
+ if (!descendantFrame)
+ return false;
+
+ if (ancestor->isLayoutView())
+ return true;
+
+ while (descendant && descendant != ancestor)
+ descendant = descendant->containingBlock();
+ return descendant;
+}
+
+void mapRectUpToDocument(LayoutRect& rect,
+ const LayoutObject& descendant,
+ const Document& document) {
+ FloatQuad mappedQuad = descendant.localToAncestorQuad(
+ FloatQuad(FloatRect(rect)), document.layoutView(),
+ UseTransforms | ApplyContainerFlip);
+ rect = LayoutRect(mappedQuad.boundingBox());
+}
+
+void mapRectDownToDocument(LayoutRect& rect,
+ LayoutBoxModelObject* ancestor,
+ const Document& document) {
+ FloatQuad mappedQuad = document.layoutView()->ancestorToLocalQuad(
+ ancestor, FloatQuad(FloatRect(rect)),
+ UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries);
+ rect = LayoutRect(mappedQuad.boundingBox());
+}
+
+LayoutUnit computeMargin(const Length& length, LayoutUnit referenceLength) {
+ if (length.type() == Percent) {
+ return LayoutUnit(
+ static_cast<int>(referenceLength.toFloat() * length.percent() / 100.0));
+ }
+ DCHECK_EQ(length.type(), Fixed);
+ return LayoutUnit(length.intValue());
+}
+
+LayoutView* localRootView(Element& element) {
+ LocalFrame* frame = element.document().frame();
+ frame = frame ? frame->localFrameRoot() : nullptr;
+ return frame ? frame->contentLayoutObject() : nullptr;
+}
+
+} // namespace
+
+IntersectionGeometry::IntersectionGeometry(Element* root,
+ Element& target,
+ const Vector<Length>& rootMargin,
+ bool shouldReportRootBounds)
+ : m_root(root ? root->layoutObject() : localRootView(target)),
+ m_target(target.layoutObject()),
+ m_rootMargin(rootMargin),
+ m_shouldReportRootBounds(shouldReportRootBounds),
+ m_rootIsImplicit(root ? 0 : 1),
+ m_doesIntersect(0),
+ m_isValid(initializeGeometry(root, target)) {}
+
+IntersectionGeometry::~IntersectionGeometry() {}
+
+bool IntersectionGeometry::initializeGeometry(Element* root, Element& target) {
+ DCHECK(m_rootMargin.isEmpty() || m_rootMargin.size() == 4);
+ if (root && !root->isConnected())
+ return false;
+ if (!m_root || !m_root->isBox())
+ return false;
+ if (!target.isConnected())
+ return false;
+ if (!m_target || (!m_target->isBoxModelObject() && !m_target->isText()))
+ return false;
+ if (root && !isContainingBlockChainDescendant(m_target, m_root))
+ return false;
+ initializeTargetRect();
+ m_intersectionRect = m_targetRect;
+ initializeRootRect();
+ return true;
+}
+
+void IntersectionGeometry::initializeTargetRect() {
+ m_targetRect =
+ LayoutRect(toLayoutBoxModelObject(target())->borderBoundingBox());
+}
+
+void IntersectionGeometry::initializeRootRect() {
+ // TODO(szager): In OOPIF, m_root will be the LayoutView of the
+ // localFrameRoot(). Once viewport intersection support lands,
+ // add a call to mapToVisualRectInAncestorSpace to map the rect up to
+ // top-level frame coordinates.
+ if (m_root->isLayoutView()) {
+ m_rootRect =
+ LayoutRect(toLayoutView(m_root)->frameView()->visibleContentRect());
+ } else if (m_root->isBox() && m_root->hasOverflowClip()) {
+ m_rootRect = LayoutRect(toLayoutBox(m_root)->contentBoxRect());
+ } else {
+ m_rootRect =
+ LayoutRect(toLayoutBoxModelObject(m_root)->borderBoundingBox());
+ }
+ applyRootMargin();
+}
+
+void IntersectionGeometry::applyRootMargin() {
+ if (m_rootMargin.isEmpty())
+ return;
+
+ // TODO(szager): Make sure the spec is clear that left/right margins are
+ // resolved against width and not height.
+ LayoutUnit topMargin = computeMargin(m_rootMargin[0], m_rootRect.height());
+ LayoutUnit rightMargin = computeMargin(m_rootMargin[1], m_rootRect.width());
+ LayoutUnit bottomMargin = computeMargin(m_rootMargin[2], m_rootRect.height());
+ LayoutUnit leftMargin = computeMargin(m_rootMargin[3], m_rootRect.width());
+
+ m_rootRect.setX(m_rootRect.x() - leftMargin);
+ m_rootRect.setWidth(m_rootRect.width() + leftMargin + rightMargin);
+ m_rootRect.setY(m_rootRect.y() - topMargin);
+ m_rootRect.setHeight(m_rootRect.height() + topMargin + bottomMargin);
+}
+
+void IntersectionGeometry::clipToRoot() {
+ // Map and clip rect into root element coordinates.
+ // TODO(szager): the writing mode flipping needs a test.
+ // TODO(szager): Once the OOPIF viewport intersection code lands,
+ // use nullptr for ancestor to map to the top frame.
+ LayoutBox* ancestor = toLayoutBox(m_root);
+ m_doesIntersect = m_target->mapToVisualRectInAncestorSpace(
+ ancestor, m_intersectionRect, EdgeInclusive);
+ if (ancestor && ancestor->hasOverflowClip())
+ m_intersectionRect.move(-ancestor->scrolledContentOffset());
+ if (!m_doesIntersect)
+ return;
+ LayoutRect rootClipRect(m_rootRect);
+ if (ancestor)
+ ancestor->flipForWritingMode(rootClipRect);
+ m_doesIntersect &= m_intersectionRect.inclusiveIntersect(rootClipRect);
+}
+
+void IntersectionGeometry::mapTargetRectToTargetFrameCoordinates() {
+ Document& targetDocument = m_target->document();
+ LayoutSize scrollPosition =
+ LayoutSize(targetDocument.view()->getScrollOffset());
+ mapRectUpToDocument(m_targetRect, *m_target, targetDocument);
+ m_targetRect.move(-scrollPosition);
+}
+
+void IntersectionGeometry::mapRootRectToRootFrameCoordinates() {
+ Document& rootDocument = m_root->document();
+ if (!rootIsImplicit())
+ mapRectUpToDocument(m_rootRect, *m_root, rootDocument);
+ // TODO(szager): When OOPIF support lands, this scroll offset adjustment
+ // will probably be wrong.
+ LayoutSize scrollPosition =
+ LayoutSize(rootDocument.view()->getScrollOffset());
+ m_rootRect.move(-scrollPosition);
+}
+
+void IntersectionGeometry::mapIntersectionRectToTargetFrameCoordinates() {
+ Document& targetDocument = m_target->document();
+ if (rootIsImplicit()) {
+ LocalFrame* targetFrame = targetDocument.frame();
+ Frame* rootFrame = targetFrame->tree().top();
+ LayoutSize scrollPosition =
+ LayoutSize(targetDocument.view()->getScrollOffset());
+ if (targetFrame != rootFrame)
+ mapRectDownToDocument(m_intersectionRect, nullptr, targetDocument);
+ m_intersectionRect.move(-scrollPosition);
+ } else {
+ LayoutSize scrollPosition =
+ LayoutSize(targetDocument.view()->getScrollOffset());
+ mapRectUpToDocument(m_intersectionRect, *m_root, m_root->document());
+ m_intersectionRect.move(-scrollPosition);
+ }
+}
+
+void IntersectionGeometry::computeGeometry() {
+ if (!isValid())
+ return;
+ clipToRoot();
+ mapTargetRectToTargetFrameCoordinates();
+ if (m_doesIntersect)
+ mapIntersectionRectToTargetFrameCoordinates();
+ else
+ m_intersectionRect = LayoutRect();
+ // Small optimization: if we're not going to report root bounds, don't bother
+ // transforming them to the frame.
+ if (m_shouldReportRootBounds)
+ mapRootRectToRootFrameCoordinates();
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698