2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "RenderRegion.h"
33 #include "FlowThreadController.h"
34 #include "GraphicsContext.h"
35 #include "HitTestResult.h"
37 #include "PaintInfo.h"
38 #include "RenderBoxRegionInfo.h"
39 #include "RenderNamedFlowThread.h"
40 #include "RenderView.h"
41 #include "StyleResolver.h"
45 RenderRegion::RenderRegion(Node* node, RenderFlowThread* flowThread)
46 : RenderReplaced(node, IntSize())
47 , m_flowThread(flowThread)
48 , m_parentNamedFlowThread(0)
50 , m_hasCustomRegionStyle(false)
51 , m_regionState(RegionUndefined)
52 , m_dispatchRegionLayoutUpdateEvent(false)
56 LayoutUnit RenderRegion::logicalWidthForFlowThreadContent() const
58 return m_flowThread->isHorizontalWritingMode() ? contentWidth() : contentHeight();
61 LayoutUnit RenderRegion::logicalHeightForFlowThreadContent() const
63 return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
66 LayoutRect RenderRegion::regionOverflowRect() const
68 // FIXME: Would like to just use hasOverflowClip() but we aren't a block yet. When RenderRegion is eliminated and
69 // folded into RenderBlock, switch to hasOverflowClip().
70 bool clipX = style()->overflowX() != OVISIBLE;
71 bool clipY = style()->overflowY() != OVISIBLE;
72 if ((clipX && clipY) || !isValid() || !m_flowThread)
75 LayoutRect flowThreadOverflow = m_flowThread->visualOverflowRect();
77 // Only clip along the flow thread axis.
78 LayoutUnit outlineSize = maximalOutlineSize(PaintPhaseOutline);
80 if (m_flowThread->isHorizontalWritingMode()) {
81 LayoutUnit minY = isFirstRegion() ? (flowThreadOverflow.y() - outlineSize) : regionRect().y();
82 LayoutUnit maxY = isLastRegion() ? max(regionRect().maxY(), flowThreadOverflow.maxY()) + outlineSize : regionRect().maxY();
83 LayoutUnit minX = clipX ? regionRect().x() : (flowThreadOverflow.x() - outlineSize);
84 LayoutUnit maxX = clipX ? regionRect().maxX() : (flowThreadOverflow.maxX() + outlineSize);
85 clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
87 LayoutUnit minX = isFirstRegion() ? (flowThreadOverflow.x() - outlineSize) : regionRect().x();
88 LayoutUnit maxX = isLastRegion() ? max(regionRect().maxX(), flowThreadOverflow.maxX()) + outlineSize : regionRect().maxX();
89 LayoutUnit minY = clipY ? regionRect().y() : (flowThreadOverflow.y() - outlineSize);
90 LayoutUnit maxY = clipY ? regionRect().maxY() : (flowThreadOverflow.maxY() + outlineSize);
91 clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
97 bool RenderRegion::isFirstRegion() const
99 ASSERT(isValid() && m_flowThread);
100 return m_flowThread->firstRegion() == this;
103 bool RenderRegion::isLastRegion() const
105 ASSERT(isValid() && m_flowThread);
106 return m_flowThread->lastRegion() == this;
109 void RenderRegion::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
111 // Delegate painting of content in region to RenderFlowThread.
112 if (!m_flowThread || !isValid())
115 setRegionObjectsRegionStyle();
116 m_flowThread->paintIntoRegion(paintInfo, this, LayoutPoint(paintOffset.x() + borderLeft() + paddingLeft(), paintOffset.y() + borderTop() + paddingTop()));
117 restoreRegionObjectsOriginalStyle();
121 bool RenderRegion::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
126 LayoutPoint adjustedLocation = accumulatedOffset + location();
128 // Check our bounds next. For this purpose always assume that we can only be hit in the
129 // foreground phase (which is true for replaced elements like images).
130 LayoutRect boundsRect = borderBoxRectInRegion(pointInContainer.region());
131 boundsRect.moveBy(adjustedLocation);
132 if (visibleToHitTesting() && action == HitTestForeground && pointInContainer.intersects(boundsRect)) {
133 // Check the contents of the RenderFlowThread.
134 if (m_flowThread && m_flowThread->hitTestRegion(this, request, result, pointInContainer, LayoutPoint(adjustedLocation.x() + borderLeft() + paddingLeft(), adjustedLocation.y() + borderTop() + paddingTop())))
136 updateHitTestResult(result, pointInContainer.point() - toLayoutSize(adjustedLocation));
137 if (!result.addNodeToRectBasedTestResult(node(), pointInContainer, boundsRect))
144 void RenderRegion::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
146 RenderReplaced::styleDidChange(diff, oldStyle);
148 // If the region is not attached to any thread, there is no need to check
149 // whether the region has region styling since no content will be displayed
152 setHasCustomRegionStyle(false);
156 bool customRegionStyle = false;
158 Element* regionElement = static_cast<Element*>(node());
159 customRegionStyle = view()->document()->styleResolver()->checkRegionStyle(regionElement);
161 setHasCustomRegionStyle(customRegionStyle);
162 m_flowThread->checkRegionsWithStyling();
165 void RenderRegion::layout()
167 RenderReplaced::layout();
168 if (m_flowThread && isValid()) {
169 LayoutRect oldRegionRect(regionRect());
170 if (!isHorizontalWritingMode())
171 oldRegionRect = oldRegionRect.transposedRect();
172 if (oldRegionRect.width() != logicalWidthForFlowThreadContent() || oldRegionRect.height() != logicalHeightForFlowThreadContent())
173 m_flowThread->invalidateRegions();
176 // FIXME: We need to find a way to set up overflow properly. Our flow thread hasn't gotten a layout
177 // yet, so we can't look to it for correct information. It's possible we could wait until after the RenderFlowThread
178 // gets a layout, and then try to propagate overflow information back to the region, and then mark for a second layout.
179 // That second layout would then be able to use the information from the RenderFlowThread to set up overflow.
181 // The big problem though is that overflow needs to be region-specific. We can't simply use the RenderFlowThread's global
182 // overflow values, since then we'd always think any narrow region had huge overflow (all the way to the width of the
183 // RenderFlowThread itself).
185 // We'll need to expand RenderBoxRegionInfo to also hold left and right overflow values.
188 void RenderRegion::attachRegion()
193 // By now the flow thread should already be added to the rendering tree,
194 // so we go up the rendering parents and check that this region is not part of the same
195 // flow that it actually needs to display. It would create a circular reference.
196 RenderObject* parentObject = parent();
197 m_parentNamedFlowThread = 0;
198 for ( ; parentObject; parentObject = parentObject->parent()) {
199 if (parentObject->isRenderNamedFlowThread()) {
200 m_parentNamedFlowThread = toRenderNamedFlowThread(parentObject);
201 // Do not take into account a region that links a flow with itself. The dependency
202 // cannot change, so it is not worth adding it to the list.
203 if (m_flowThread == m_parentNamedFlowThread) {
211 m_flowThread->addRegionToThread(this);
214 void RenderRegion::detachRegion()
217 m_flowThread->removeRegionFromThread(this);
220 RenderBoxRegionInfo* RenderRegion::renderBoxRegionInfo(const RenderBox* box) const
222 if (!m_isValid || !m_flowThread)
224 return m_renderBoxRegionInfo.get(box);
227 RenderBoxRegionInfo* RenderRegion::setRenderBoxRegionInfo(const RenderBox* box, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
228 bool containingBlockChainIsInset)
230 ASSERT(m_isValid && m_flowThread);
231 if (!m_isValid || !m_flowThread)
234 OwnPtr<RenderBoxRegionInfo>& boxInfo = m_renderBoxRegionInfo.add(box, nullptr).iterator->second;
236 *boxInfo = RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
238 boxInfo = adoptPtr(new RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset));
240 return boxInfo.get();
243 PassOwnPtr<RenderBoxRegionInfo> RenderRegion::takeRenderBoxRegionInfo(const RenderBox* box)
245 return m_renderBoxRegionInfo.take(box);
248 void RenderRegion::removeRenderBoxRegionInfo(const RenderBox* box)
250 m_renderBoxRegionInfo.remove(box);
253 void RenderRegion::deleteAllRenderBoxRegionInfo()
255 m_renderBoxRegionInfo.clear();
258 LayoutUnit RenderRegion::offsetFromLogicalTopOfFirstPage() const
260 if (!m_isValid || !m_flowThread)
262 if (m_flowThread->isHorizontalWritingMode())
263 return regionRect().y();
264 return regionRect().x();
267 void RenderRegion::setRegionObjectsRegionStyle()
269 if (!hasCustomRegionStyle())
272 // Start from content nodes and recursively compute the style in region for the render objects below.
273 // If the style in region was already computed, used that style instead of computing a new one.
274 RenderNamedFlowThread* namedFlow = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
275 const NamedFlowContentNodes& contentNodes = namedFlow->contentNodes();
277 for (NamedFlowContentNodes::const_iterator iter = contentNodes.begin(), end = contentNodes.end(); iter != end; ++iter) {
278 const Node* node = *iter;
279 // The list of content nodes contains also the nodes with display:none.
280 if (!node->renderer())
283 RenderObject* object = node->renderer();
284 // If the content node does not flow any of its children in this region,
285 // we do not compute any style for them in this region.
286 if (!flowThread()->objectInFlowRegion(object, this))
289 // If the object has style in region, use that instead of computing a new one.
290 RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(object);
291 RefPtr<RenderStyle> objectStyleInRegion;
292 bool objectRegionStyleCached = false;
293 if (it != m_renderObjectRegionStyle.end()) {
294 objectStyleInRegion = it->second.style;
295 ASSERT(it->second.cached);
296 objectRegionStyleCached = true;
298 objectStyleInRegion = computeStyleInRegion(object);
300 setObjectStyleInRegion(object, objectStyleInRegion, objectRegionStyleCached);
302 computeChildrenStyleInRegion(object);
306 void RenderRegion::restoreRegionObjectsOriginalStyle()
308 if (!hasCustomRegionStyle())
311 RenderObjectRegionStyleMap temp;
312 for (RenderObjectRegionStyleMap::iterator iter = m_renderObjectRegionStyle.begin(), end = m_renderObjectRegionStyle.end(); iter != end; ++iter) {
313 RenderObject* object = const_cast<RenderObject*>(iter->first);
314 RefPtr<RenderStyle> objectRegionStyle = object->style();
315 RefPtr<RenderStyle> objectOriginalStyle = iter->second.style;
316 object->setStyleInternal(objectOriginalStyle);
318 bool shouldCacheRegionStyle = iter->second.cached;
319 if (!shouldCacheRegionStyle) {
320 // Check whether we should cache the computed style in region.
321 unsigned changedContextSensitiveProperties = ContextSensitivePropertyNone;
322 StyleDifference styleDiff = objectOriginalStyle->diff(objectRegionStyle.get(), changedContextSensitiveProperties);
323 if (styleDiff < StyleDifferenceLayoutPositionedMovementOnly)
324 shouldCacheRegionStyle = true;
326 if (shouldCacheRegionStyle) {
327 ObjectRegionStyleInfo styleInfo;
328 styleInfo.style = objectRegionStyle;
329 styleInfo.cached = true;
330 temp.set(object, styleInfo);
334 m_renderObjectRegionStyle.swap(temp);
337 PassRefPtr<RenderStyle> RenderRegion::computeStyleInRegion(const RenderObject* object)
340 ASSERT(object->view());
341 ASSERT(object->view()->document());
342 ASSERT(!object->isAnonymous());
343 ASSERT(object->node() && object->node()->isElementNode());
345 Element* element = toElement(object->node());
346 RefPtr<RenderStyle> renderObjectRegionStyle = object->view()->document()->styleResolver()->styleForElement(element, 0, DisallowStyleSharing, MatchAllRules, this);
348 return renderObjectRegionStyle.release();
351 void RenderRegion::computeChildrenStyleInRegion(const RenderObject* object)
353 for (RenderObject* child = object->firstChild(); child; child = child->nextSibling()) {
355 RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(child);
357 RefPtr<RenderStyle> childStyleInRegion;
358 bool objectRegionStyleCached = false;
359 if (it != m_renderObjectRegionStyle.end()) {
360 childStyleInRegion = it->second.style;
361 objectRegionStyleCached = true;
363 if (child->isAnonymous())
364 childStyleInRegion = RenderStyle::createAnonymousStyleWithDisplay(object->style(), child->style()->display());
365 else if (child->isText())
366 childStyleInRegion = RenderStyle::clone(object->style());
368 childStyleInRegion = computeStyleInRegion(child);
371 setObjectStyleInRegion(child, childStyleInRegion, objectRegionStyleCached);
373 computeChildrenStyleInRegion(child);
377 void RenderRegion::setObjectStyleInRegion(RenderObject* object, PassRefPtr<RenderStyle> styleInRegion, bool objectRegionStyleCached)
379 RefPtr<RenderStyle> objectOriginalStyle = object->style();
380 object->setStyleInternal(styleInRegion);
382 if (object->isBoxModelObject() && !object->hasBoxDecorations()) {
383 bool hasBoxDecorations = object->isTableCell()
384 || object->style()->hasBackground()
385 || object->style()->hasBorder()
386 || object->style()->hasAppearance()
387 || object->style()->boxShadow();
388 object->setHasBoxDecorations(hasBoxDecorations);
391 ObjectRegionStyleInfo styleInfo;
392 styleInfo.style = objectOriginalStyle;
393 styleInfo.cached = objectRegionStyleCached;
394 m_renderObjectRegionStyle.set(object, styleInfo);
397 void RenderRegion::clearObjectStyleInRegion(const RenderObject* object)
400 m_renderObjectRegionStyle.remove(object);
402 // Clear the style for the children of this object.
403 for (RenderObject* child = object->firstChild(); child; child = child->nextSibling())
404 clearObjectStyleInRegion(child);
407 } // namespace WebCore