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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // 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.
4
5 #include "core/layout/IntersectionGeometry.h"
6
7 #include "core/frame/FrameView.h"
8 #include "core/frame/LocalFrame.h"
9 #include "core/html/HTMLFrameOwnerElement.h"
10 #include "core/layout/LayoutBox.h"
11 #include "core/layout/LayoutView.h"
12 #include "core/layout/api/LayoutAPIShim.h"
13 #include "core/layout/api/LayoutViewItem.h"
14 #include "core/paint/PaintLayer.h"
15
16 namespace blink {
17
18 namespace {
19
20 bool isContainingBlockChainDescendant(LayoutObject* descendant,
21 LayoutObject* ancestor) {
22 LocalFrame* ancestorFrame = ancestor->document().frame();
23 LocalFrame* descendantFrame = descendant->document().frame();
24 if (!ancestorFrame)
25 return false;
26
27 while (descendantFrame && descendantFrame != ancestorFrame) {
28 HTMLFrameOwnerElement* owner = descendant->document().localOwner();
29 if (!owner)
30 return false;
31 descendant = owner->layoutObject();
32 if (!descendant)
33 return false;
34 descendantFrame = descendant->document().frame();
35 }
36
37 if (!descendantFrame)
38 return false;
39
40 if (ancestor->isLayoutView())
41 return true;
42
43 while (descendant && descendant != ancestor)
44 descendant = descendant->containingBlock();
45 return descendant;
46 }
47
48 void mapRectUpToDocument(LayoutRect& rect,
49 const LayoutObject& descendant,
50 const Document& document) {
51 FloatQuad mappedQuad = descendant.localToAncestorQuad(
52 FloatQuad(FloatRect(rect)), document.layoutView(),
53 UseTransforms | ApplyContainerFlip);
54 rect = LayoutRect(mappedQuad.boundingBox());
55 }
56
57 void mapRectDownToDocument(LayoutRect& rect,
58 LayoutBoxModelObject* ancestor,
59 const Document& document) {
60 FloatQuad mappedQuad = document.layoutView()->ancestorToLocalQuad(
61 ancestor, FloatQuad(FloatRect(rect)),
62 UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries);
63 rect = LayoutRect(mappedQuad.boundingBox());
64 }
65
66 LayoutUnit computeMargin(const Length& length, LayoutUnit referenceLength) {
67 if (length.type() == Percent) {
68 return LayoutUnit(
69 static_cast<int>(referenceLength.toFloat() * length.percent() / 100.0));
70 }
71 DCHECK_EQ(length.type(), Fixed);
72 return LayoutUnit(length.intValue());
73 }
74
75 LayoutView* localRootView(Element& element) {
76 LocalFrame* frame = element.document().frame();
77 frame = frame ? frame->localFrameRoot() : nullptr;
78 return frame ? frame->contentLayoutObject() : nullptr;
79 }
80
81 } // namespace
82
83 IntersectionGeometry::IntersectionGeometry(Element* root,
84 Element& target,
85 const Vector<Length>& rootMargin,
86 bool shouldReportRootBounds)
87 : m_root(root ? root->layoutObject() : localRootView(target)),
88 m_target(target.layoutObject()),
89 m_rootMargin(rootMargin),
90 m_shouldReportRootBounds(shouldReportRootBounds),
91 m_rootIsImplicit(root ? 0 : 1),
92 m_doesIntersect(0),
93 m_isValid(initializeGeometry(root, target)) {}
94
95 IntersectionGeometry::~IntersectionGeometry() {}
96
97 bool IntersectionGeometry::initializeGeometry(Element* root, Element& target) {
98 DCHECK(m_rootMargin.isEmpty() || m_rootMargin.size() == 4);
99 if (root && !root->isConnected())
100 return false;
101 if (!m_root || !m_root->isBox())
102 return false;
103 if (!target.isConnected())
104 return false;
105 if (!m_target || (!m_target->isBoxModelObject() && !m_target->isText()))
106 return false;
107 if (root && !isContainingBlockChainDescendant(m_target, m_root))
108 return false;
109 initializeTargetRect();
110 m_intersectionRect = m_targetRect;
111 initializeRootRect();
112 return true;
113 }
114
115 void IntersectionGeometry::initializeTargetRect() {
116 m_targetRect =
117 LayoutRect(toLayoutBoxModelObject(target())->borderBoundingBox());
118 }
119
120 void IntersectionGeometry::initializeRootRect() {
121 // TODO(szager): In OOPIF, m_root will be the LayoutView of the
122 // localFrameRoot(). Once viewport intersection support lands,
123 // add a call to mapToVisualRectInAncestorSpace to map the rect up to
124 // top-level frame coordinates.
125 if (m_root->isLayoutView()) {
126 m_rootRect =
127 LayoutRect(toLayoutView(m_root)->frameView()->visibleContentRect());
128 } else if (m_root->isBox() && m_root->hasOverflowClip()) {
129 m_rootRect = LayoutRect(toLayoutBox(m_root)->contentBoxRect());
130 } else {
131 m_rootRect =
132 LayoutRect(toLayoutBoxModelObject(m_root)->borderBoundingBox());
133 }
134 applyRootMargin();
135 }
136
137 void IntersectionGeometry::applyRootMargin() {
138 if (m_rootMargin.isEmpty())
139 return;
140
141 // TODO(szager): Make sure the spec is clear that left/right margins are
142 // resolved against width and not height.
143 LayoutUnit topMargin = computeMargin(m_rootMargin[0], m_rootRect.height());
144 LayoutUnit rightMargin = computeMargin(m_rootMargin[1], m_rootRect.width());
145 LayoutUnit bottomMargin = computeMargin(m_rootMargin[2], m_rootRect.height());
146 LayoutUnit leftMargin = computeMargin(m_rootMargin[3], m_rootRect.width());
147
148 m_rootRect.setX(m_rootRect.x() - leftMargin);
149 m_rootRect.setWidth(m_rootRect.width() + leftMargin + rightMargin);
150 m_rootRect.setY(m_rootRect.y() - topMargin);
151 m_rootRect.setHeight(m_rootRect.height() + topMargin + bottomMargin);
152 }
153
154 void IntersectionGeometry::clipToRoot() {
155 // Map and clip rect into root element coordinates.
156 // TODO(szager): the writing mode flipping needs a test.
157 // TODO(szager): Once the OOPIF viewport intersection code lands,
158 // use nullptr for ancestor to map to the top frame.
159 LayoutBox* ancestor = toLayoutBox(m_root);
160 m_doesIntersect = m_target->mapToVisualRectInAncestorSpace(
161 ancestor, m_intersectionRect, EdgeInclusive);
162 if (ancestor && ancestor->hasOverflowClip())
163 m_intersectionRect.move(-ancestor->scrolledContentOffset());
164 if (!m_doesIntersect)
165 return;
166 LayoutRect rootClipRect(m_rootRect);
167 if (ancestor)
168 ancestor->flipForWritingMode(rootClipRect);
169 m_doesIntersect &= m_intersectionRect.inclusiveIntersect(rootClipRect);
170 }
171
172 void IntersectionGeometry::mapTargetRectToTargetFrameCoordinates() {
173 Document& targetDocument = m_target->document();
174 LayoutSize scrollPosition =
175 LayoutSize(targetDocument.view()->getScrollOffset());
176 mapRectUpToDocument(m_targetRect, *m_target, targetDocument);
177 m_targetRect.move(-scrollPosition);
178 }
179
180 void IntersectionGeometry::mapRootRectToRootFrameCoordinates() {
181 Document& rootDocument = m_root->document();
182 if (!rootIsImplicit())
183 mapRectUpToDocument(m_rootRect, *m_root, rootDocument);
184 // TODO(szager): When OOPIF support lands, this scroll offset adjustment
185 // will probably be wrong.
186 LayoutSize scrollPosition =
187 LayoutSize(rootDocument.view()->getScrollOffset());
188 m_rootRect.move(-scrollPosition);
189 }
190
191 void IntersectionGeometry::mapIntersectionRectToTargetFrameCoordinates() {
192 Document& targetDocument = m_target->document();
193 if (rootIsImplicit()) {
194 LocalFrame* targetFrame = targetDocument.frame();
195 Frame* rootFrame = targetFrame->tree().top();
196 LayoutSize scrollPosition =
197 LayoutSize(targetDocument.view()->getScrollOffset());
198 if (targetFrame != rootFrame)
199 mapRectDownToDocument(m_intersectionRect, nullptr, targetDocument);
200 m_intersectionRect.move(-scrollPosition);
201 } else {
202 LayoutSize scrollPosition =
203 LayoutSize(targetDocument.view()->getScrollOffset());
204 mapRectUpToDocument(m_intersectionRect, *m_root, m_root->document());
205 m_intersectionRect.move(-scrollPosition);
206 }
207 }
208
209 void IntersectionGeometry::computeGeometry() {
210 if (!isValid())
211 return;
212 clipToRoot();
213 mapTargetRectToTargetFrameCoordinates();
214 if (m_doesIntersect)
215 mapIntersectionRectToTargetFrameCoordinates();
216 else
217 m_intersectionRect = LayoutRect();
218 // Small optimization: if we're not going to report root bounds, don't bother
219 // transforming them to the frame.
220 if (m_shouldReportRootBounds)
221 mapRootRectToRootFrameCoordinates();
222 }
223
224 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698