RenderFlowThread.cpp [plain text]
#include "config.h"
#include "RenderFlowThread.h"
#include "FlowThreadController.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "Node.h"
#include "PODIntervalTree.h"
#include "PaintInfo.h"
#include "RenderBoxRegionInfo.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderRegion.h"
#include "RenderView.h"
#include "TransformState.h"
#include "WebKitNamedFlow.h"
#include <wtf/StackStats.h>
namespace WebCore {
RenderFlowThread::RenderFlowThread()
: RenderBlock(0)
, m_autoLogicalHeightRegionsCount(0)
, m_regionsInvalidated(false)
, m_regionsHaveUniformLogicalWidth(true)
, m_regionsHaveUniformLogicalHeight(true)
, m_overset(true)
, m_hasRegionsWithStyling(false)
, m_dispatchRegionLayoutUpdateEvent(false)
, m_pageLogicalSizeChanged(false)
, m_inConstrainedLayoutPhase(false)
, m_needsTwoPhasesLayout(false)
{
setFlowThreadState(InsideOutOfFlowThread);
}
PassRefPtr<RenderStyle> RenderFlowThread::createFlowThreadStyle(RenderStyle* parentStyle)
{
RefPtr<RenderStyle> newStyle(RenderStyle::create());
newStyle->inheritFrom(parentStyle);
newStyle->setDisplay(BLOCK);
newStyle->setPosition(AbsolutePosition);
newStyle->setZIndex(0);
newStyle->setLeft(Length(0, Fixed));
newStyle->setTop(Length(0, Fixed));
newStyle->setWidth(Length(100, Percent));
newStyle->setHeight(Length(100, Percent));
newStyle->font().update(0);
return newStyle.release();
}
void RenderFlowThread::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
if (oldStyle && oldStyle->writingMode() != style()->writingMode())
m_regionsInvalidated = true;
}
void RenderFlowThread::removeFlowChildInfo(RenderObject* child)
{
if (child->isBox())
removeRenderBoxRegionInfo(toRenderBox(child));
clearRenderObjectCustomStyle(child);
}
void RenderFlowThread::addRegionToThread(RenderRegion* renderRegion)
{
ASSERT(renderRegion);
m_regionList.add(renderRegion);
renderRegion->setIsValid(true);
}
void RenderFlowThread::removeRegionFromThread(RenderRegion* renderRegion)
{
ASSERT(renderRegion);
m_regionList.remove(renderRegion);
}
void RenderFlowThread::invalidateRegions()
{
if (m_regionsInvalidated) {
ASSERT(selfNeedsLayout());
return;
}
m_regionRangeMap.clear();
m_breakBeforeToRegionMap.clear();
m_breakAfterToRegionMap.clear();
setNeedsLayout(true);
m_regionsInvalidated = true;
}
class CurrentRenderFlowThreadDisabler {
WTF_MAKE_NONCOPYABLE(CurrentRenderFlowThreadDisabler);
public:
CurrentRenderFlowThreadDisabler(RenderView* view)
: m_view(view)
, m_renderFlowThread(0)
{
m_renderFlowThread = m_view->flowThreadController()->currentRenderFlowThread();
if (m_renderFlowThread)
view->flowThreadController()->setCurrentRenderFlowThread(0);
}
~CurrentRenderFlowThreadDisabler()
{
if (m_renderFlowThread)
m_view->flowThreadController()->setCurrentRenderFlowThread(m_renderFlowThread);
}
private:
RenderView* m_view;
RenderFlowThread* m_renderFlowThread;
};
void RenderFlowThread::validateRegions()
{
if (m_regionsInvalidated) {
m_regionsInvalidated = false;
m_regionsHaveUniformLogicalWidth = true;
m_regionsHaveUniformLogicalHeight = true;
if (hasRegions()) {
LayoutUnit previousRegionLogicalWidth = 0;
LayoutUnit previousRegionLogicalHeight = 0;
bool firstRegionVisited = false;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
region->deleteAllRenderBoxRegionInfo();
if (!inConstrainedLayoutPhase() && region->hasAutoLogicalHeight()) {
region->setOverrideLogicalContentHeight(region->maxPageLogicalHeight());
m_regionsHaveUniformLogicalHeight = false;
}
LayoutUnit regionLogicalWidth = region->pageLogicalWidth();
LayoutUnit regionLogicalHeight = region->pageLogicalHeight();
if (!firstRegionVisited)
firstRegionVisited = true;
else {
if (m_regionsHaveUniformLogicalWidth && previousRegionLogicalWidth != regionLogicalWidth)
m_regionsHaveUniformLogicalWidth = false;
if (m_regionsHaveUniformLogicalHeight && previousRegionLogicalHeight != regionLogicalHeight)
m_regionsHaveUniformLogicalHeight = false;
}
previousRegionLogicalWidth = regionLogicalWidth;
}
}
}
updateLogicalWidth(); updateRegionsFlowThreadPortionRect();
}
void RenderFlowThread::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
m_pageLogicalSizeChanged = m_regionsInvalidated && everHadLayout();
if (!m_regionsInvalidated && !inConstrainedLayoutPhase())
initializeRegionsOverrideLogicalContentHeight();
validateRegions();
m_needsTwoPhasesLayout = !inConstrainedLayoutPhase() && hasAutoLogicalHeightRegions();
CurrentRenderFlowThreadMaintainer currentFlowThreadSetter(this);
RenderBlock::layout();
m_pageLogicalSizeChanged = false;
if (lastRegion())
lastRegion()->expandToEncompassFlowThreadContentsIfNeeded();
if (shouldDispatchRegionLayoutUpdateEvent())
dispatchRegionLayoutUpdateEvent();
}
void RenderFlowThread::updateLogicalWidth()
{
LayoutUnit logicalWidth = initialLogicalWidth();
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
logicalWidth = max(region->pageLogicalWidth(), logicalWidth);
}
setLogicalWidth(logicalWidth);
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
LayoutUnit regionLogicalWidth = region->pageLogicalWidth();
if (regionLogicalWidth != logicalWidth) {
LayoutUnit logicalLeft = style()->direction() == LTR ? LayoutUnit() : logicalWidth - regionLogicalWidth;
region->setRenderBoxRegionInfo(this, logicalLeft, regionLogicalWidth, false);
}
}
}
void RenderFlowThread::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
{
computedValues.m_position = logicalTop;
computedValues.m_extent = 0;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
computedValues.m_extent += region->logicalHeightOfAllFlowThreadContent();
}
}
LayoutRect RenderFlowThread::computeRegionClippingRect(const LayoutPoint& offset, const LayoutRect& flowThreadPortionRect, const LayoutRect& flowThreadPortionOverflowRect) const
{
LayoutRect regionClippingRect(offset + (flowThreadPortionOverflowRect.location() - flowThreadPortionRect.location()), flowThreadPortionOverflowRect.size());
if (style()->isFlippedBlocksWritingMode())
regionClippingRect.move(flowThreadPortionRect.size() - flowThreadPortionOverflowRect.size());
return regionClippingRect;
}
void RenderFlowThread::paintFlowThreadPortionInRegion(PaintInfo& paintInfo, RenderRegion* region, const LayoutRect& flowThreadPortionRect, const LayoutRect& flowThreadPortionOverflowRect, const LayoutPoint& paintOffset) const
{
GraphicsContext* context = paintInfo.context;
if (!context)
return;
IntPoint adjustedPaintOffset;
LayoutPoint portionLocation;
if (style()->isFlippedBlocksWritingMode()) {
LayoutRect flippedFlowThreadPortionRect(flowThreadPortionRect);
flipForWritingMode(flippedFlowThreadPortionRect);
portionLocation = flippedFlowThreadPortionRect.location();
} else
portionLocation = flowThreadPortionRect.location();
adjustedPaintOffset = roundedIntPoint(paintOffset - portionLocation);
IntRect regionClippingRect = pixelSnappedIntRect(computeRegionClippingRect(adjustedPaintOffset + portionLocation, flowThreadPortionRect, flowThreadPortionOverflowRect));
PaintInfo info(paintInfo);
info.rect.intersect(regionClippingRect);
if (!info.rect.isEmpty()) {
context->save();
context->clip(regionClippingRect);
context->translate(adjustedPaintOffset.x(), adjustedPaintOffset.y());
info.rect.moveBy(-adjustedPaintOffset);
PaintBehavior paintBehavior = 0;
if (info.phase == PaintPhaseTextClip)
paintBehavior |= PaintBehaviorForceBlackText;
else if (info.phase == PaintPhaseSelection)
paintBehavior |= PaintBehaviorSelectionOnly;
layer()->paint(context, info.rect, paintBehavior, 0, region, RenderLayer::PaintLayerTemporaryClipRects);
context->restore();
}
}
bool RenderFlowThread::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
{
if (hitTestAction == HitTestBlockBackground)
return false;
return RenderBlock::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction);
}
bool RenderFlowThread::hitTestFlowThreadPortionInRegion(RenderRegion* region, const LayoutRect& flowThreadPortionRect, const LayoutRect& flowThreadPortionOverflowRect, const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset) const
{
LayoutRect regionClippingRect = computeRegionClippingRect(accumulatedOffset, flowThreadPortionRect, flowThreadPortionOverflowRect);
if (!regionClippingRect.contains(locationInContainer.point()))
return false;
LayoutSize renderFlowThreadOffset;
if (style()->isFlippedBlocksWritingMode()) {
LayoutRect flippedFlowThreadPortionRect(flowThreadPortionRect);
flipForWritingMode(flippedFlowThreadPortionRect);
renderFlowThreadOffset = accumulatedOffset - flippedFlowThreadPortionRect.location();
} else
renderFlowThreadOffset = accumulatedOffset - flowThreadPortionRect.location();
HitTestRequest newRequest(request.type() | HitTestRequest::IgnoreClipping | HitTestRequest::DisallowShadowContent);
HitTestLocation newHitTestLocation(locationInContainer, -renderFlowThreadOffset, region);
bool isPointInsideFlowThread = layer()->hitTest(newRequest, newHitTestLocation, result);
return isPointInsideFlowThread;
}
bool RenderFlowThread::shouldRepaint(const LayoutRect& r) const
{
if (view()->printing() || r.isEmpty())
return false;
return true;
}
void RenderFlowThread::repaintRectangleInRegions(const LayoutRect& repaintRect, bool immediate) const
{
if (!shouldRepaint(repaintRect) || !hasValidRegionInfo())
return;
LayoutStateDisabler layoutStateDisabler(view());
CurrentRenderFlowThreadDisabler disabler(view());
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
region->repaintFlowThreadContent(repaintRect, immediate);
}
}
RenderRegion* RenderFlowThread::regionAtBlockOffset(LayoutUnit offset, bool extendLastRegion, RegionAutoGenerationPolicy autoGenerationPolicy)
{
ASSERT(!m_regionsInvalidated);
if (autoGenerationPolicy == AllowRegionAutoGeneration)
autoGenerateRegionsToBlockOffset(offset);
if (offset <= 0)
return m_regionList.isEmpty() ? 0 : m_regionList.first();
RegionSearchAdapter adapter(offset);
m_regionIntervalTree.allOverlapsWithAdapter<RegionSearchAdapter>(adapter);
if (!adapter.result() && !m_regionList.isEmpty() && (extendLastRegion || m_regionList.last()->isRenderRegionSet()))
return m_regionList.last();
return adapter.result();
}
LayoutPoint RenderFlowThread::adjustedPositionRelativeToOffsetParent(const RenderBoxModelObject& boxModelObject, const LayoutPoint& startPoint)
{
LayoutPoint referencePoint = startPoint;
RenderRegion* startRegion = regionAtBlockOffset(referencePoint.y());
if (startRegion) {
RenderBoxModelObject* currObject = startRegion;
RenderBoxModelObject* currOffsetParent;
while ((currOffsetParent = currObject->offsetParent())) {
referencePoint.move(currObject->offsetLeft(), currObject->offsetTop());
if (currOffsetParent->isBox() && !currOffsetParent->isBody())
referencePoint.move(toRenderBox(currOffsetParent)->borderLeft(), toRenderBox(currOffsetParent)->borderTop());
currObject = currOffsetParent;
}
bool wasComputedRelativeToOtherRegion = false;
const RenderBlock* objContainingBlock = boxModelObject.containingBlock();
while (objContainingBlock && !objContainingBlock->isRenderNamedFlowThread()) {
RenderRegion* parentStartRegion = 0;
RenderRegion* parentEndRegion = 0;
getRegionRangeForBox(objContainingBlock, parentStartRegion, parentEndRegion);
if (parentStartRegion && parentStartRegion != startRegion) {
wasComputedRelativeToOtherRegion = true;
break;
}
objContainingBlock = objContainingBlock->containingBlock();
}
if (wasComputedRelativeToOtherRegion) {
if (boxModelObject.isBox()) {
LayoutRect borderBoxRect = toRenderBox(&boxModelObject)->borderBoxRectInRegion(startRegion, RenderBox::DoNotCacheRenderBoxRegionInfo);
referencePoint.move(borderBoxRect.location().x(), 0);
}
LayoutUnit top = 0;
if (boxModelObject.isRenderBlock())
top = toRenderBlock(&boxModelObject)->offsetFromLogicalTopOfFirstPage();
else {
if (boxModelObject.containingBlock())
top = boxModelObject.containingBlock()->offsetFromLogicalTopOfFirstPage();
if (boxModelObject.isBox())
top += toRenderBox(&boxModelObject)->topLeftLocation().y();
else if (boxModelObject.isRenderInline())
top -= toRenderInline(&boxModelObject)->borderTop();
}
LayoutUnit regionLogicalTop = startRegion->pageLogicalTopForOffset(top);
LayoutUnit topRelativeToRegion = top - regionLogicalTop;
referencePoint.setY(startRegion->offsetTop() + topRelativeToRegion);
if (boxModelObject.isRelPositioned())
referencePoint.move(0, boxModelObject.relativePositionOffset().height());
else if (boxModelObject.isStickyPositioned())
referencePoint.move(0, boxModelObject.stickyPositionOffset().height());
}
referencePoint.move(startRegion->borderLeft(), startRegion->borderTop());
}
return referencePoint;
}
LayoutUnit RenderFlowThread::pageLogicalTopForOffset(LayoutUnit offset)
{
RenderRegion* region = regionAtBlockOffset(offset);
return region ? region->pageLogicalTopForOffset(offset) : LayoutUnit();
}
LayoutUnit RenderFlowThread::pageLogicalWidthForOffset(LayoutUnit offset)
{
RenderRegion* region = regionAtBlockOffset(offset, true);
return region ? region->pageLogicalWidth() : contentLogicalWidth();
}
LayoutUnit RenderFlowThread::pageLogicalHeightForOffset(LayoutUnit offset)
{
RenderRegion* region = regionAtBlockOffset(offset);
if (!region)
return 0;
return region->pageLogicalHeight();
}
LayoutUnit RenderFlowThread::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule)
{
RenderRegion* region = regionAtBlockOffset(offset);
if (!region)
return 0;
LayoutUnit pageLogicalTop = region->pageLogicalTopForOffset(offset);
LayoutUnit pageLogicalHeight = region->pageLogicalHeight();
LayoutUnit pageLogicalBottom = pageLogicalTop + pageLogicalHeight;
LayoutUnit remainingHeight = pageLogicalBottom - offset;
if (pageBoundaryRule == IncludePageBoundary) {
remainingHeight = intMod(remainingHeight, pageLogicalHeight);
}
return remainingHeight;
}
RenderRegion* RenderFlowThread::mapFromFlowToRegion(TransformState& transformState) const
{
if (!hasValidRegionInfo())
return 0;
LayoutRect boxRect = transformState.mappedQuad().enclosingBoundingBox();
flipForWritingMode(boxRect);
LayoutPoint center = boxRect.center();
RenderRegion* renderRegion = const_cast<RenderFlowThread*>(this)->regionAtBlockOffset(isHorizontalWritingMode() ? center.y() : center.x(), true, DisallowRegionAutoGeneration);
if (!renderRegion)
return 0;
LayoutRect flippedRegionRect(renderRegion->flowThreadPortionRect());
flipForWritingMode(flippedRegionRect);
transformState.move(renderRegion->contentBoxRect().location() - flippedRegionRect.location());
return renderRegion;
}
void RenderFlowThread::removeRenderBoxRegionInfo(RenderBox* box)
{
if (!hasRegions())
return;
if (m_regionsInvalidated) {
ASSERT(selfNeedsLayout());
return;
}
RenderRegion* startRegion;
RenderRegion* endRegion;
getRegionRangeForBox(box, startRegion, endRegion);
for (RenderRegionList::iterator iter = m_regionList.find(startRegion); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
region->removeRenderBoxRegionInfo(box);
if (region == endRegion)
break;
}
#ifndef NDEBUG
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
ASSERT(!region->renderBoxRegionInfo(box));
}
#endif
m_regionRangeMap.remove(box);
}
bool RenderFlowThread::logicalWidthChangedInRegionsForBlock(const RenderBlock* block)
{
if (!hasRegions())
return false;
RenderRegion* startRegion;
RenderRegion* endRegion;
getRegionRangeForBox(block, startRegion, endRegion);
if (m_pageLogicalSizeChanged && !startRegion)
return true;
if (block == this)
return false;
for (RenderRegionList::iterator iter = m_regionList.find(startRegion); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
OwnPtr<RenderBoxRegionInfo> oldInfo = region->takeRenderBoxRegionInfo(block);
if (!oldInfo)
continue;
LayoutUnit oldLogicalWidth = oldInfo->logicalWidth();
RenderBoxRegionInfo* newInfo = block->renderBoxRegionInfo(region);
if (!newInfo || newInfo->logicalWidth() != oldLogicalWidth)
return true;
if (region == endRegion)
break;
}
return false;
}
LayoutUnit RenderFlowThread::contentLogicalWidthOfFirstRegion() const
{
RenderRegion* firstValidRegionInFlow = firstRegion();
if (!firstValidRegionInFlow)
return 0;
return isHorizontalWritingMode() ? firstValidRegionInFlow->contentWidth() : firstValidRegionInFlow->contentHeight();
}
LayoutUnit RenderFlowThread::contentLogicalHeightOfFirstRegion() const
{
RenderRegion* firstValidRegionInFlow = firstRegion();
if (!firstValidRegionInFlow)
return 0;
return isHorizontalWritingMode() ? firstValidRegionInFlow->contentHeight() : firstValidRegionInFlow->contentWidth();
}
LayoutUnit RenderFlowThread::contentLogicalLeftOfFirstRegion() const
{
RenderRegion* firstValidRegionInFlow = firstRegion();
if (!firstValidRegionInFlow)
return 0;
return isHorizontalWritingMode() ? firstValidRegionInFlow->flowThreadPortionRect().x() : firstValidRegionInFlow->flowThreadPortionRect().y();
}
RenderRegion* RenderFlowThread::firstRegion() const
{
if (!hasValidRegionInfo())
return 0;
return m_regionList.first();
}
RenderRegion* RenderFlowThread::lastRegion() const
{
if (!hasValidRegionInfo())
return 0;
return m_regionList.last();
}
void RenderFlowThread::clearRenderObjectCustomStyle(const RenderObject* object,
const RenderRegion* oldStartRegion, const RenderRegion* oldEndRegion,
const RenderRegion* newStartRegion, const RenderRegion* newEndRegion)
{
bool insideOldRegionRange = false;
bool insideNewRegionRange = false;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (oldStartRegion == region)
insideOldRegionRange = true;
if (newStartRegion == region)
insideNewRegionRange = true;
if (!(insideOldRegionRange && insideNewRegionRange))
region->clearObjectStyleInRegion(object);
if (oldEndRegion == region)
insideOldRegionRange = false;
if (newEndRegion == region)
insideNewRegionRange = false;
}
}
void RenderFlowThread::setRegionRangeForBox(const RenderBox* box, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
if (!hasRegions())
return;
RenderRegion* startRegion = regionAtBlockOffset(offsetFromLogicalTopOfFirstPage, true);
RenderRegion* endRegion = regionAtBlockOffset(offsetFromLogicalTopOfFirstPage + box->logicalHeight(), true);
RenderRegionRangeMap::iterator it = m_regionRangeMap.find(box);
if (it == m_regionRangeMap.end()) {
m_regionRangeMap.set(box, RenderRegionRange(startRegion, endRegion));
clearRenderObjectCustomStyle(box);
return;
}
RenderRegionRange& range = it->value;
if (range.startRegion() == startRegion && range.endRegion() == endRegion)
return;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (region == startRegion) {
iter = m_regionList.find(endRegion);
continue;
}
region->removeRenderBoxRegionInfo(box);
if (region == range.endRegion())
break;
}
clearRenderObjectCustomStyle(box, range.startRegion(), range.endRegion(), startRegion, endRegion);
range.setRange(startRegion, endRegion);
}
void RenderFlowThread::getRegionRangeForBox(const RenderBox* box, RenderRegion*& startRegion, RenderRegion*& endRegion) const
{
startRegion = 0;
endRegion = 0;
RenderRegionRangeMap::const_iterator it = m_regionRangeMap.find(box);
if (it == m_regionRangeMap.end())
return;
const RenderRegionRange& range = it->value;
startRegion = range.startRegion();
endRegion = range.endRegion();
ASSERT(m_regionList.contains(startRegion) && m_regionList.contains(endRegion));
}
void RenderFlowThread::applyBreakAfterContent(LayoutUnit clientHeight)
{
addForcedRegionBreak(clientHeight, this, false);
}
void RenderFlowThread::computeOverflowStateForRegions(LayoutUnit oldClientAfterEdge)
{
LayoutUnit height = oldClientAfterEdge;
if (hasRenderOverflow()
&& ( (isHorizontalWritingMode() && visualOverflowRect().maxY() > clientBoxRect().maxY())
|| (!isHorizontalWritingMode() && visualOverflowRect().maxX() > clientBoxRect().maxX())))
height = isHorizontalWritingMode() ? visualOverflowRect().maxY() : visualOverflowRect().maxX();
RenderRegion* lastReg = lastRegion();
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
LayoutUnit flowMin = height - (isHorizontalWritingMode() ? region->flowThreadPortionRect().y() : region->flowThreadPortionRect().x());
LayoutUnit flowMax = height - (isHorizontalWritingMode() ? region->flowThreadPortionRect().maxY() : region->flowThreadPortionRect().maxX());
RenderRegion::RegionState previousState = region->regionState();
RenderRegion::RegionState state = RenderRegion::RegionFit;
if (flowMin <= 0)
state = RenderRegion::RegionEmpty;
if (flowMax > 0 && region == lastReg)
state = RenderRegion::RegionOverset;
region->setRegionState(state);
if (previousState != state
|| state == RenderRegion::RegionFit
|| state == RenderRegion::RegionOverset)
setDispatchRegionLayoutUpdateEvent(true);
}
m_overset = lastReg ? lastReg->regionState() == RenderRegion::RegionOverset : true;
}
bool RenderFlowThread::regionInRange(const RenderRegion* targetRegion, const RenderRegion* startRegion, const RenderRegion* endRegion) const
{
ASSERT(targetRegion);
for (RenderRegionList::const_iterator it = m_regionList.find(const_cast<RenderRegion*>(startRegion)); it != m_regionList.end(); ++it) {
const RenderRegion* currRegion = *it;
if (targetRegion == currRegion)
return true;
if (currRegion == endRegion)
break;
}
return false;
}
void RenderFlowThread::checkRegionsWithStyling()
{
bool hasRegionsWithStyling = false;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (region->hasCustomRegionStyle()) {
hasRegionsWithStyling = true;
break;
}
}
m_hasRegionsWithStyling = hasRegionsWithStyling;
}
bool RenderFlowThread::objectInFlowRegion(const RenderObject* object, const RenderRegion* region) const
{
ASSERT(object);
ASSERT(region);
RenderFlowThread* flowThread = object->flowThreadContainingBlock();
if (flowThread != this)
return false;
if (!m_regionList.contains(const_cast<RenderRegion*>(region)))
return false;
RenderBox* enclosingBox = object->enclosingBox();
RenderRegion* enclosingBoxStartRegion = 0;
RenderRegion* enclosingBoxEndRegion = 0;
getRegionRangeForBox(enclosingBox, enclosingBoxStartRegion, enclosingBoxEndRegion);
if (!regionInRange(region, enclosingBoxStartRegion, enclosingBoxEndRegion))
return false;
if (object->isBox())
return true;
LayoutRect objectABBRect = object->absoluteBoundingBoxRect(true);
if (!objectABBRect.width())
objectABBRect.setWidth(1);
if (!objectABBRect.height())
objectABBRect.setHeight(1);
if (objectABBRect.intersects(region->absoluteBoundingBoxRect(true)))
return true;
if (region == lastRegion()) {
for (RenderRegionList::const_iterator it = m_regionList.find(enclosingBoxStartRegion); it != m_regionList.end(); ++it) {
const RenderRegion* currRegion = *it;
if (currRegion == region)
break;
if (objectABBRect.intersects(currRegion->absoluteBoundingBoxRect(true)))
return false;
}
return true;
}
return false;
}
#ifndef NDEBUG
bool RenderFlowThread::isAutoLogicalHeightRegionsCountConsistent() const
{
unsigned autoLogicalHeightRegions = 0;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
const RenderRegion* region = *iter;
if (region->hasAutoLogicalHeight())
autoLogicalHeightRegions++;
}
return autoLogicalHeightRegions == m_autoLogicalHeightRegionsCount;
}
#endif
void RenderFlowThread::initializeRegionsOverrideLogicalContentHeight(RenderRegion* startRegion)
{
ASSERT(!inConstrainedLayoutPhase());
if (!hasAutoLogicalHeightRegions())
return;
RenderRegionList::iterator regionIter = startRegion ? m_regionList.find(startRegion) : m_regionList.begin();
for (; regionIter != m_regionList.end(); ++regionIter) {
RenderRegion* region = *regionIter;
if (region->hasAutoLogicalHeight())
region->setOverrideLogicalContentHeight(region->maxPageLogicalHeight());
}
}
void RenderFlowThread::markAutoLogicalHeightRegionsForLayout()
{
ASSERT(hasAutoLogicalHeightRegions());
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->hasAutoLogicalHeight())
continue;
region->setNeedsLayout(true);
}
}
void RenderFlowThread::updateRegionsFlowThreadPortionRect(const RenderRegion* lastRegionWithContent)
{
ASSERT(!lastRegionWithContent || (!inConstrainedLayoutPhase() && hasAutoLogicalHeightRegions()));
LayoutUnit logicalHeight = 0;
bool emptyRegionsSegment = false;
m_regionIntervalTree.clear();
m_regionIntervalTree.initIfNeeded();
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (emptyRegionsSegment && region->hasAutoLogicalHeight())
region->clearOverrideLogicalContentHeight();
LayoutUnit regionLogicalWidth = region->pageLogicalWidth();
LayoutUnit regionLogicalHeight = std::min<LayoutUnit>(LayoutUnit::max() / 2 - logicalHeight, region->logicalHeightOfAllFlowThreadContent());
LayoutRect regionRect(style()->direction() == LTR ? LayoutUnit() : logicalWidth() - regionLogicalWidth, logicalHeight, regionLogicalWidth, regionLogicalHeight);
region->setFlowThreadPortionRect(isHorizontalWritingMode() ? regionRect : regionRect.transposedRect());
m_regionIntervalTree.add(RegionIntervalTree::createInterval(logicalHeight, logicalHeight + regionLogicalHeight, region));
logicalHeight += regionLogicalHeight;
if (lastRegionWithContent == region)
emptyRegionsSegment = true;
}
ASSERT(!lastRegionWithContent || emptyRegionsSegment);
}
bool RenderFlowThread::addForcedRegionBreak(LayoutUnit offsetBreakInFlowThread, RenderObject* breakChild, bool isBefore, LayoutUnit* offsetBreakAdjustment)
{
if (inConstrainedLayoutPhase())
return false;
RenderObjectToRegionMap& mapToUse = isBefore ? m_breakBeforeToRegionMap : m_breakAfterToRegionMap;
RenderObjectToRegionMap::iterator iter = mapToUse.find(breakChild);
if (iter != mapToUse.end()) {
RenderRegionList::iterator regionIter = m_regionList.find(iter->value);
ASSERT(regionIter != m_regionList.end());
ASSERT((*regionIter)->hasAutoLogicalHeight());
initializeRegionsOverrideLogicalContentHeight(*regionIter);
updateRegionsFlowThreadPortionRect();
}
RenderRegion* region = regionAtBlockOffset(offsetBreakInFlowThread);
if (!region)
return false;
bool lastBreakAfterContent = breakChild == this;
bool overrideLogicalContentHeightComputed = false;
LayoutUnit currentRegionOffsetInFlowThread = isHorizontalWritingMode() ? region->flowThreadPortionRect().y() : region->flowThreadPortionRect().x();
LayoutUnit offsetBreakInCurrentRegion = offsetBreakInFlowThread - currentRegionOffsetInFlowThread;
if (region->hasAutoLogicalHeight()) {
ASSERT(region->maxPageLogicalHeight() == region->overrideLogicalContentHeight());
mapToUse.set(breakChild, region);
overrideLogicalContentHeightComputed = true;
LayoutUnit regionOverrideLogicalContentHeight = region->constrainContentBoxLogicalHeightByMinMax(offsetBreakInCurrentRegion);
ASSERT(regionOverrideLogicalContentHeight <= region->maxPageLogicalHeight());
region->setOverrideLogicalContentHeight(regionOverrideLogicalContentHeight);
currentRegionOffsetInFlowThread += regionOverrideLogicalContentHeight;
} else
currentRegionOffsetInFlowThread += isHorizontalWritingMode() ? region->flowThreadPortionRect().height() : region->flowThreadPortionRect().width();
if (hasAutoLogicalHeightRegions() && lastBreakAfterContent)
updateRegionsFlowThreadPortionRect(region);
else if (overrideLogicalContentHeightComputed)
updateRegionsFlowThreadPortionRect();
if (offsetBreakAdjustment)
*offsetBreakAdjustment = max<LayoutUnit>(0, currentRegionOffsetInFlowThread - offsetBreakInFlowThread);
return overrideLogicalContentHeightComputed;
}
void RenderFlowThread::incrementAutoLogicalHeightRegions()
{
if (!m_autoLogicalHeightRegionsCount)
view()->flowThreadController()->incrementFlowThreadsWithAutoLogicalHeightRegions();
++m_autoLogicalHeightRegionsCount;
}
void RenderFlowThread::decrementAutoLogicalHeightRegions()
{
ASSERT(m_autoLogicalHeightRegionsCount > 0);
--m_autoLogicalHeightRegionsCount;
if (!m_autoLogicalHeightRegionsCount)
view()->flowThreadController()->decrementFlowThreadsWithAutoLogicalHeightRegions();
}
void RenderFlowThread::collectLayerFragments(LayerFragments& layerFragments, const LayoutRect& layerBoundingBox, const LayoutRect& dirtyRect)
{
ASSERT(!m_regionsInvalidated);
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
region->collectLayerFragments(layerFragments, layerBoundingBox, dirtyRect);
}
}
LayoutRect RenderFlowThread::fragmentsBoundingBox(const LayoutRect& layerBoundingBox)
{
ASSERT(!m_regionsInvalidated);
LayoutRect result;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
LayerFragments fragments;
region->collectLayerFragments(fragments, layerBoundingBox, PaintInfo::infiniteRect());
for (size_t i = 0; i < fragments.size(); ++i) {
const LayerFragment& fragment = fragments.at(i);
LayoutRect fragmentRect(layerBoundingBox);
fragmentRect.intersect(fragment.paginationClip);
fragmentRect.moveBy(fragment.paginationOffset);
result.unite(fragmentRect);
}
}
return result;
}
bool RenderFlowThread::hasCachedOffsetFromLogicalTopOfFirstRegion(const RenderBox* box) const
{
return m_boxesToOffsetMap.contains(box);
}
LayoutUnit RenderFlowThread::cachedOffsetFromLogicalTopOfFirstRegion(const RenderBox* box) const
{
return m_boxesToOffsetMap.get(box);
}
void RenderFlowThread::setOffsetFromLogicalTopOfFirstRegion(const RenderBox* box, LayoutUnit offset)
{
m_boxesToOffsetMap.set(box, offset);
}
void RenderFlowThread::clearOffsetFromLogicalTopOfFirstRegion(const RenderBox* box)
{
ASSERT(m_boxesToOffsetMap.contains(box));
m_boxesToOffsetMap.remove(box);
}
const RenderBox* RenderFlowThread::currentActiveRenderBox() const
{
const RenderObject* currentObject = m_activeObjectsStack.isEmpty() ? 0 : m_activeObjectsStack.last();
if (currentObject && currentObject->isBox())
return toRenderBox(currentObject);
return 0;
}
void RenderFlowThread::pushFlowThreadLayoutState(const RenderObject* object)
{
const RenderBox* currentBoxDescendant = currentActiveRenderBox();
LayoutState* layoutState = view()->layoutState();
if (currentBoxDescendant && layoutState && layoutState->isPaginated()) {
ASSERT(layoutState->m_renderer == currentBoxDescendant);
LayoutSize offsetDelta = layoutState->m_layoutOffset - layoutState->m_pageOffset;
setOffsetFromLogicalTopOfFirstRegion(currentBoxDescendant, currentBoxDescendant->isHorizontalWritingMode() ? offsetDelta.height() : offsetDelta.width());
}
m_activeObjectsStack.add(object);
}
void RenderFlowThread::popFlowThreadLayoutState()
{
m_activeObjectsStack.removeLast();
const RenderBox* currentBoxDescendant = currentActiveRenderBox();
LayoutState* layoutState = view()->layoutState();
if (currentBoxDescendant && layoutState && layoutState->isPaginated())
clearOffsetFromLogicalTopOfFirstRegion(currentBoxDescendant);
}
LayoutUnit RenderFlowThread::offsetFromLogicalTopOfFirstRegion(const RenderBlock* currentBlock) const
{
if (hasCachedOffsetFromLogicalTopOfFirstRegion(currentBlock))
return cachedOffsetFromLogicalTopOfFirstRegion(currentBlock);
const RenderBox* currentBoxDescendant = currentActiveRenderBox();
if (currentBlock == currentBoxDescendant) {
LayoutState* layoutState = view()->layoutState();
ASSERT(layoutState->m_renderer == currentBlock);
ASSERT(layoutState && layoutState->isPaginated());
LayoutSize offsetDelta = layoutState->m_layoutOffset - layoutState->m_pageOffset;
return currentBoxDescendant->isHorizontalWritingMode() ? offsetDelta.height() : offsetDelta.width();
}
LayoutRect blockRect(0, 0, currentBlock->width(), currentBlock->height());
while (currentBlock && !currentBlock->isRenderFlowThread()) {
RenderBlock* containerBlock = currentBlock->containingBlock();
ASSERT(containerBlock);
if (!containerBlock)
return 0;
LayoutPoint currentBlockLocation = currentBlock->location();
if (containerBlock->style()->writingMode() != currentBlock->style()->writingMode()) {
if (containerBlock->style()->isFlippedBlocksWritingMode()) {
if (containerBlock->isHorizontalWritingMode())
blockRect.setY(currentBlock->height() - blockRect.maxY());
else
blockRect.setX(currentBlock->width() - blockRect.maxX());
}
currentBlock->flipForWritingMode(blockRect);
}
blockRect.moveBy(currentBlockLocation);
currentBlock = containerBlock;
}
return currentBlock->isHorizontalWritingMode() ? blockRect.y() : blockRect.x();
}
void RenderFlowThread::RegionSearchAdapter::collectIfNeeded(const RegionInterval& interval)
{
if (m_result)
return;
if (interval.low() <= m_offset && interval.high() > m_offset)
m_result = interval.data();
}
void RenderFlowThread::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
{
if (this == repaintContainer)
return;
if (RenderRegion* region = mapFromFlowToRegion(transformState))
static_cast<const RenderObject*>(region)->mapLocalToContainer(region->containerForRepaint(), transformState, mode, wasFixed);
}
CurrentRenderFlowThreadMaintainer::CurrentRenderFlowThreadMaintainer(RenderFlowThread* renderFlowThread)
: m_renderFlowThread(renderFlowThread)
, m_previousRenderFlowThread(0)
{
if (!m_renderFlowThread)
return;
RenderView* view = m_renderFlowThread->view();
m_previousRenderFlowThread = view->flowThreadController()->currentRenderFlowThread();
ASSERT(!m_previousRenderFlowThread || !renderFlowThread->isRenderNamedFlowThread());
view->flowThreadController()->setCurrentRenderFlowThread(m_renderFlowThread);
}
CurrentRenderFlowThreadMaintainer::~CurrentRenderFlowThreadMaintainer()
{
if (!m_renderFlowThread)
return;
RenderView* view = m_renderFlowThread->view();
ASSERT(view->flowThreadController()->currentRenderFlowThread() == m_renderFlowThread);
view->flowThreadController()->setCurrentRenderFlowThread(m_previousRenderFlowThread);
}
}