RenderMultiColumnSet.cpp [plain text]
#include "config.h"
#include "RenderMultiColumnSet.h"
#include "PaintInfo.h"
#include "RenderLayer.h"
#include "RenderMultiColumnBlock.h"
#include "RenderMultiColumnFlowThread.h"
using namespace std;
namespace WebCore {
RenderMultiColumnSet::RenderMultiColumnSet(RenderFlowThread* flowThread)
: RenderRegionSet(0, flowThread)
, m_computedColumnCount(1)
, m_computedColumnWidth(0)
, m_computedColumnHeight(0)
, m_maxColumnHeight(LayoutUnit::max())
, m_minSpaceShortage(LayoutUnit::max())
, m_minimumColumnHeight(0)
, m_forcedBreaksCount(0)
, m_maximumDistanceBetweenForcedBreaks(0)
, m_forcedBreakOffset(0)
{
}
RenderMultiColumnSet* RenderMultiColumnSet::createAnonymous(RenderFlowThread* flowThread)
{
Document* document = flowThread->document();
RenderMultiColumnSet* renderer = new (document->renderArena()) RenderMultiColumnSet(flowThread);
renderer->setDocumentForAnonymous(document);
return renderer;
}
LayoutUnit RenderMultiColumnSet::heightAdjustedForSetOffset(LayoutUnit height) const
{
RenderMultiColumnBlock* multicolBlock = toRenderMultiColumnBlock(parent());
LayoutUnit contentLogicalTop = logicalTop() - multicolBlock->borderAndPaddingBefore();
height -= contentLogicalTop;
return max(height, LayoutUnit(1)); }
LayoutUnit RenderMultiColumnSet::pageLogicalTopForOffset(LayoutUnit offset) const
{
LayoutUnit portionLogicalTop = (isHorizontalWritingMode() ? flowThreadPortionRect().y() : flowThreadPortionRect().x());
unsigned columnIndex = columnIndexAtOffset(offset, AssumeNewColumns);
return portionLogicalTop + columnIndex * computedColumnHeight();
}
void RenderMultiColumnSet::setAndConstrainColumnHeight(LayoutUnit newHeight)
{
m_computedColumnHeight = newHeight;
if (m_computedColumnHeight > m_maxColumnHeight)
m_computedColumnHeight = m_maxColumnHeight;
}
bool RenderMultiColumnSet::calculateBalancedHeight(bool initial)
{
ASSERT(toRenderMultiColumnBlock(parent())->requiresBalancing());
LayoutUnit oldColumnHeight = m_computedColumnHeight;
LayoutUnit currentMinSpaceShortage = m_minSpaceShortage;
m_minSpaceShortage = LayoutUnit::max();
if (initial) {
LayoutUnit logicalHeightGuess = ceilf(float(flowThread()->logicalHeight()) / float(m_computedColumnCount));
logicalHeightGuess = max(logicalHeightGuess, m_minimumColumnHeight);
setAndConstrainColumnHeight(logicalHeightGuess);
return m_computedColumnHeight != oldColumnHeight;
}
if (columnCount() <= computedColumnCount())
return false;
ASSERT(currentMinSpaceShortage != LayoutUnit::max()); if (currentMinSpaceShortage == LayoutUnit::max())
return false;
setAndConstrainColumnHeight(m_computedColumnHeight + currentMinSpaceShortage);
ASSERT(m_computedColumnHeight >= oldColumnHeight); return m_computedColumnHeight > oldColumnHeight;
}
void RenderMultiColumnSet::recordSpaceShortage(LayoutUnit spaceShortage)
{
if (spaceShortage >= m_minSpaceShortage)
return;
ASSERT(spaceShortage > 0);
m_minSpaceShortage = spaceShortage;
}
void RenderMultiColumnSet::updateLogicalWidth()
{
RenderMultiColumnBlock* parentBlock = toRenderMultiColumnBlock(parent());
setComputedColumnWidthAndCount(parentBlock->columnWidth(), parentBlock->columnCount());
setLogicalWidth(parentBox()->contentLogicalWidth());
unsigned colCount = columnCount();
LayoutUnit colGap = columnGap();
LayoutUnit minimumContentLogicalWidth = colCount * computedColumnWidth() + (colCount - 1) * colGap;
LayoutUnit currentContentLogicalWidth = contentLogicalWidth();
LayoutUnit delta = max(LayoutUnit(), minimumContentLogicalWidth - currentContentLogicalWidth);
if (!delta)
return;
setLogicalWidth(logicalWidth() + delta);
}
void RenderMultiColumnSet::prepareForLayout()
{
RenderMultiColumnBlock* multicolBlock = toRenderMultiColumnBlock(parent());
RenderStyle* multicolStyle = multicolBlock->style();
ASSERT(!previousSiblingBox() || !previousSiblingBox()->isRenderMultiColumnSet()); setLogicalTop(multicolBlock->borderAndPaddingBefore());
updateLogicalWidth();
if (multicolBlock->requiresBalancing()) {
m_maxColumnHeight = LayoutUnit::max();
if (!multicolStyle->logicalHeight().isAuto())
m_maxColumnHeight = multicolBlock->computeContentLogicalHeight(multicolStyle->logicalHeight());
if (!multicolStyle->logicalMaxHeight().isUndefined()) {
LayoutUnit logicalMaxHeight = multicolBlock->computeContentLogicalHeight(multicolStyle->logicalMaxHeight());
if (m_maxColumnHeight > logicalMaxHeight)
m_maxColumnHeight = logicalMaxHeight;
}
m_maxColumnHeight = heightAdjustedForSetOffset(m_maxColumnHeight);
m_computedColumnHeight = 0; } else
setAndConstrainColumnHeight(heightAdjustedForSetOffset(multicolBlock->columnHeightAvailable()));
m_minimumColumnHeight = 0;
}
void RenderMultiColumnSet::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
{
computedValues.m_extent = m_computedColumnHeight;
computedValues.m_position = logicalTop;
}
LayoutUnit RenderMultiColumnSet::columnGap() const
{
RenderMultiColumnBlock* parentBlock = toRenderMultiColumnBlock(parent());
if (parentBlock->style()->hasNormalColumnGap())
return parentBlock->style()->fontDescription().computedPixelSize(); return parentBlock->style()->columnGap();
}
unsigned RenderMultiColumnSet::columnCount() const
{
if (!computedColumnHeight())
return 1;
LayoutUnit logicalHeightInColumns = flowThread()->isHorizontalWritingMode() ? flowThreadPortionRect().height() : flowThreadPortionRect().width();
unsigned count = ceil(static_cast<float>(logicalHeightInColumns) / computedColumnHeight());
ASSERT(count >= 1);
return count;
}
LayoutRect RenderMultiColumnSet::columnRectAt(unsigned index) const
{
LayoutUnit colLogicalWidth = computedColumnWidth();
LayoutUnit colLogicalHeight = computedColumnHeight();
LayoutUnit colLogicalTop = borderAndPaddingBefore();
LayoutUnit colLogicalLeft = borderAndPaddingLogicalLeft();
LayoutUnit colGap = columnGap();
if (style()->isLeftToRightDirection())
colLogicalLeft += index * (colLogicalWidth + colGap);
else
colLogicalLeft += contentLogicalWidth() - colLogicalWidth - index * (colLogicalWidth + colGap);
if (isHorizontalWritingMode())
return LayoutRect(colLogicalLeft, colLogicalTop, colLogicalWidth, colLogicalHeight);
return LayoutRect(colLogicalTop, colLogicalLeft, colLogicalHeight, colLogicalWidth);
}
unsigned RenderMultiColumnSet::columnIndexAtOffset(LayoutUnit offset, ColumnIndexCalculationMode mode) const
{
LayoutRect portionRect(flowThreadPortionRect());
LayoutUnit flowThreadLogicalTop = isHorizontalWritingMode() ? portionRect.y() : portionRect.x();
if (offset < flowThreadLogicalTop)
return 0;
if (mode == ClampToExistingColumns) {
LayoutUnit flowThreadLogicalBottom = isHorizontalWritingMode() ? portionRect.maxY() : portionRect.maxX();
if (offset >= flowThreadLogicalBottom)
return columnCount() - 1;
}
return static_cast<float>(offset - flowThreadLogicalTop) / computedColumnHeight();
}
LayoutRect RenderMultiColumnSet::flowThreadPortionRectAt(unsigned index) const
{
LayoutRect portionRect = flowThreadPortionRect();
if (isHorizontalWritingMode())
portionRect = LayoutRect(portionRect.x(), portionRect.y() + index * computedColumnHeight(), portionRect.width(), computedColumnHeight());
else
portionRect = LayoutRect(portionRect.x() + index * computedColumnHeight(), portionRect.y(), computedColumnHeight(), portionRect.height());
return portionRect;
}
LayoutRect RenderMultiColumnSet::flowThreadPortionOverflowRect(const LayoutRect& portionRect, unsigned index, unsigned colCount, LayoutUnit colGap) const
{
bool isFirstColumn = !index;
bool isLastColumn = index == colCount - 1;
bool isLeftmostColumn = style()->isLeftToRightDirection() ? isFirstColumn : isLastColumn;
bool isRightmostColumn = style()->isLeftToRightDirection() ? isLastColumn : isFirstColumn;
LayoutRect overflowRect(portionRect);
if (isHorizontalWritingMode()) {
if (isLeftmostColumn) {
overflowRect.shiftXEdgeTo(min(flowThread()->visualOverflowRect().x(), portionRect.x()));
} else {
overflowRect.shiftXEdgeTo(portionRect.x() - colGap / 2);
}
if (isRightmostColumn) {
overflowRect.shiftMaxXEdgeTo(max(flowThread()->visualOverflowRect().maxX(), portionRect.maxX()));
} else {
overflowRect.shiftMaxXEdgeTo(portionRect.maxX() + colGap / 2);
}
} else {
if (isLeftmostColumn) {
overflowRect.shiftYEdgeTo(min(flowThread()->visualOverflowRect().y(), portionRect.y()));
} else {
overflowRect.shiftYEdgeTo(portionRect.y() - colGap / 2);
}
if (isRightmostColumn) {
overflowRect.shiftMaxYEdgeTo(max(flowThread()->visualOverflowRect().maxY(), portionRect.maxY()));
} else {
overflowRect.shiftMaxYEdgeTo(portionRect.maxY() + colGap / 2);
}
}
return overflowRectForFlowThreadPortion(overflowRect, isFirstRegion() && isFirstColumn, isLastRegion() && isLastColumn);
}
void RenderMultiColumnSet::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (style()->visibility() != VISIBLE)
return;
RenderBlock::paintObject(paintInfo, paintOffset);
if (!m_flowThread || !isValid() || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
return;
paintColumnRules(paintInfo, paintOffset);
}
void RenderMultiColumnSet::paintColumnRules(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (paintInfo.context->paintingDisabled())
return;
RenderStyle* blockStyle = toRenderMultiColumnBlock(parent())->style();
const Color& ruleColor = blockStyle->visitedDependentColor(CSSPropertyWebkitColumnRuleColor);
bool ruleTransparent = blockStyle->columnRuleIsTransparent();
EBorderStyle ruleStyle = blockStyle->columnRuleStyle();
LayoutUnit ruleThickness = blockStyle->columnRuleWidth();
LayoutUnit colGap = columnGap();
bool renderRule = ruleStyle > BHIDDEN && !ruleTransparent;
if (!renderRule)
return;
unsigned colCount = columnCount();
if (colCount <= 1)
return;
bool antialias = shouldAntialiasLines(paintInfo.context);
bool leftToRight = style()->isLeftToRightDirection();
LayoutUnit currLogicalLeftOffset = leftToRight ? LayoutUnit() : contentLogicalWidth();
LayoutUnit ruleAdd = borderAndPaddingLogicalLeft();
LayoutUnit ruleLogicalLeft = leftToRight ? LayoutUnit() : contentLogicalWidth();
LayoutUnit inlineDirectionSize = computedColumnWidth();
BoxSide boxSide = isHorizontalWritingMode()
? leftToRight ? BSLeft : BSRight
: leftToRight ? BSTop : BSBottom;
for (unsigned i = 0; i < colCount; i++) {
if (leftToRight) {
ruleLogicalLeft += inlineDirectionSize + colGap / 2;
currLogicalLeftOffset += inlineDirectionSize + colGap;
} else {
ruleLogicalLeft -= (inlineDirectionSize + colGap / 2);
currLogicalLeftOffset -= (inlineDirectionSize + colGap);
}
if (i < colCount - 1) {
LayoutUnit ruleLeft = isHorizontalWritingMode() ? paintOffset.x() + ruleLogicalLeft - ruleThickness / 2 + ruleAdd : paintOffset.x() + borderLeft() + paddingLeft();
LayoutUnit ruleRight = isHorizontalWritingMode() ? ruleLeft + ruleThickness : ruleLeft + contentWidth();
LayoutUnit ruleTop = isHorizontalWritingMode() ? paintOffset.y() + borderTop() + paddingTop() : paintOffset.y() + ruleLogicalLeft - ruleThickness / 2 + ruleAdd;
LayoutUnit ruleBottom = isHorizontalWritingMode() ? ruleTop + contentHeight() : ruleTop + ruleThickness;
IntRect pixelSnappedRuleRect = pixelSnappedIntRectFromEdges(ruleLeft, ruleTop, ruleRight, ruleBottom);
drawLineForBoxSide(paintInfo.context, pixelSnappedRuleRect.x(), pixelSnappedRuleRect.y(), pixelSnappedRuleRect.maxX(), pixelSnappedRuleRect.maxY(), boxSide, ruleColor, ruleStyle, 0, 0, antialias);
}
ruleLogicalLeft = currLogicalLeftOffset;
}
}
void RenderMultiColumnSet::repaintFlowThreadContent(const LayoutRect& repaintRect, bool immediate) const
{
LayoutRect flowThreadRepaintRect(repaintRect);
flowThread()->flipForWritingMode(flowThreadRepaintRect);
LayoutRect clippedRect(flowThreadRepaintRect);
clippedRect.intersect(RenderRegion::flowThreadPortionOverflowRect());
if (clippedRect.isEmpty())
return;
LayoutUnit repaintLogicalTop = isHorizontalWritingMode() ? flowThreadRepaintRect.y() : flowThreadRepaintRect.x();
LayoutUnit repaintLogicalBottom = (isHorizontalWritingMode() ? flowThreadRepaintRect.maxY() : flowThreadRepaintRect.maxX()) - 1;
unsigned startColumn = columnIndexAtOffset(repaintLogicalTop);
unsigned endColumn = columnIndexAtOffset(repaintLogicalBottom);
LayoutUnit colGap = columnGap();
unsigned colCount = columnCount();
for (unsigned i = startColumn; i <= endColumn; i++) {
LayoutRect colRect = columnRectAt(i);
LayoutRect flowThreadPortion = flowThreadPortionRectAt(i);
LayoutRect flowThreadOverflowPortion = flowThreadPortionOverflowRect(flowThreadPortion, i, colCount, colGap);
repaintFlowThreadContentRectangle(repaintRect, immediate, flowThreadPortion, flowThreadOverflowPortion, colRect.location());
}
}
void RenderMultiColumnSet::collectLayerFragments(LayerFragments& fragments, const LayoutRect& layerBoundingBox, const LayoutRect& dirtyRect)
{
LayoutRect layerBoundsInFlowThread(layerBoundingBox);
flowThread()->flipForWritingMode(layerBoundsInFlowThread);
LayoutRect dirtyRectInFlowThread(dirtyRect);
flowThread()->flipForWritingMode(dirtyRectInFlowThread);
LayoutRect clippedRect(layerBoundsInFlowThread);
clippedRect.intersect(RenderRegion::flowThreadPortionOverflowRect());
if (clippedRect.isEmpty())
return;
LayoutUnit layerLogicalTop = isHorizontalWritingMode() ? layerBoundsInFlowThread.y() : layerBoundsInFlowThread.x();
LayoutUnit layerLogicalBottom = (isHorizontalWritingMode() ? layerBoundsInFlowThread.maxY() : layerBoundsInFlowThread.maxX()) - 1;
unsigned startColumn = columnIndexAtOffset(layerLogicalTop);
unsigned endColumn = columnIndexAtOffset(layerLogicalBottom);
LayoutUnit colLogicalWidth = computedColumnWidth();
LayoutUnit colGap = columnGap();
unsigned colCount = columnCount();
for (unsigned i = startColumn; i <= endColumn; i++) {
LayoutRect flowThreadPortion = flowThreadPortionRectAt(i);
LayoutRect flowThreadOverflowPortion = flowThreadPortionOverflowRect(flowThreadPortion, i, colCount, colGap);
LayoutRect clippedRect(layerBoundsInFlowThread);
clippedRect.intersect(flowThreadOverflowPortion);
if (clippedRect.isEmpty())
continue;
LayoutPoint translationOffset;
LayoutUnit inlineOffset = i * (colLogicalWidth + colGap);
if (!style()->isLeftToRightDirection())
inlineOffset = -inlineOffset;
translationOffset.setX(inlineOffset);
LayoutUnit blockOffset = isHorizontalWritingMode() ? -flowThreadPortion.y() : -flowThreadPortion.x();
if (isFlippedBlocksWritingMode(style()->writingMode()))
blockOffset = -blockOffset;
translationOffset.setY(blockOffset);
if (!isHorizontalWritingMode())
translationOffset = translationOffset.transposedPoint();
LayoutRect translatedDirtyRect(dirtyRectInFlowThread);
translatedDirtyRect.moveBy(-translationOffset);
clippedRect = layerBoundsInFlowThread;
clippedRect.intersect(translatedDirtyRect);
if (clippedRect.isEmpty())
continue;
LayerFragment fragment;
fragment.paginationOffset = translationOffset;
LayoutRect flippedFlowThreadOverflowPortion(flowThreadOverflowPortion);
flipForWritingMode(flippedFlowThreadOverflowPortion);
fragment.paginationClip = flippedFlowThreadOverflowPortion;
fragments.append(fragment);
}
}
const char* RenderMultiColumnSet::renderName() const
{
return "RenderMultiColumnSet";
}
}