RenderTableCell.cpp [plain text]
#include "config.h"
#include "RenderTableCell.h"
#include "CollapsedBorderValue.h"
#include "FloatQuad.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "HTMLTableCellElement.h"
#include "PaintInfo.h"
#include "RenderTableCol.h"
#include "RenderView.h"
#include "TransformState.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderTableCell::RenderTableCell(Node* node)
: RenderBlock(node)
, m_row(-1)
, m_column(-1)
, m_rowSpan(1)
, m_columnSpan(1)
, m_cellWidthChanged(false)
, m_intrinsicPaddingBefore(0)
, m_intrinsicPaddingAfter(0)
{
updateFromElement();
}
void RenderTableCell::destroy()
{
RenderTableSection* recalcSection = parent() ? section() : 0;
RenderBlock::destroy();
if (recalcSection)
recalcSection->setNeedsCellRecalc();
}
void RenderTableCell::updateFromElement()
{
Node* n = node();
if (n && (n->hasTagName(tdTag) || n->hasTagName(thTag))) {
HTMLTableCellElement* tc = static_cast<HTMLTableCellElement*>(n);
int oldRSpan = m_rowSpan;
int oldCSpan = m_columnSpan;
m_columnSpan = tc->colSpan();
m_rowSpan = tc->rowSpan();
if ((oldRSpan != m_rowSpan || oldCSpan != m_columnSpan) && style() && parent()) {
setNeedsLayoutAndPrefWidthsRecalc();
if (section())
section()->setNeedsCellRecalc();
}
}
}
Length RenderTableCell::styleOrColLogicalWidth() const
{
Length w = style()->logicalWidth();
if (!w.isAuto())
return w;
if (RenderTableCol* tableCol = table()->colElement(col())) {
int colSpanCount = colSpan();
Length colWidthSum = Length(0, Fixed);
for (int i = 1; i <= colSpanCount; i++) {
Length colWidth = tableCol->style()->logicalWidth();
if (!colWidth.isFixed()) {
if (colSpanCount > 1)
return w;
return colWidth;
}
colWidthSum = Length(colWidthSum.value() + colWidth.value(), Fixed);
tableCol = table()->nextColElement(tableCol);
if (!tableCol)
break;
}
if (colWidthSum.isFixed() && colWidthSum.value() > 0)
colWidthSum = Length(max(0, colWidthSum.value() - borderAndPaddingLogicalWidth()), Fixed);
return colWidthSum;
}
return w;
}
void RenderTableCell::computePreferredLogicalWidths()
{
table()->recalcSectionsIfNeeded();
RenderBlock::computePreferredLogicalWidths();
if (node() && style()->autoWrap()) {
Length w = styleOrColLogicalWidth();
String nowrap = static_cast<Element*>(node())->getAttribute(nowrapAttr);
if (!nowrap.isNull() && w.isFixed())
m_minPreferredLogicalWidth = max(w.value(), m_minPreferredLogicalWidth);
}
}
void RenderTableCell::computeLogicalWidth()
{
}
void RenderTableCell::updateLogicalWidth(int w)
{
if (w == logicalWidth())
return;
setLogicalWidth(w);
setCellWidthChanged(true);
}
void RenderTableCell::layout()
{
layoutBlock(cellWidthChanged());
setCellWidthChanged(false);
}
int RenderTableCell::paddingTop(bool includeIntrinsicPadding) const
{
int result = RenderBlock::paddingTop();
if (!includeIntrinsicPadding || !isHorizontalWritingMode())
return result;
return result + (style()->writingMode() == TopToBottomWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter());
}
int RenderTableCell::paddingBottom(bool includeIntrinsicPadding) const
{
int result = RenderBlock::paddingBottom();
if (!includeIntrinsicPadding || !isHorizontalWritingMode())
return result;
return result + (style()->writingMode() == TopToBottomWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore());
}
int RenderTableCell::paddingLeft(bool includeIntrinsicPadding) const
{
int result = RenderBlock::paddingLeft();
if (!includeIntrinsicPadding || isHorizontalWritingMode())
return result;
return result + (style()->writingMode() == LeftToRightWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter());
}
int RenderTableCell::paddingRight(bool includeIntrinsicPadding) const
{
int result = RenderBlock::paddingRight();
if (!includeIntrinsicPadding || isHorizontalWritingMode())
return result;
return result + (style()->writingMode() == LeftToRightWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore());
}
int RenderTableCell::paddingBefore(bool includeIntrinsicPadding) const
{
int result = RenderBlock::paddingBefore();
if (!includeIntrinsicPadding)
return result;
return result + intrinsicPaddingBefore();
}
int RenderTableCell::paddingAfter(bool includeIntrinsicPadding) const
{
int result = RenderBlock::paddingAfter();
if (!includeIntrinsicPadding)
return result;
return result + intrinsicPaddingAfter();
}
void RenderTableCell::setOverrideSize(int size)
{
clearIntrinsicPadding();
RenderBlock::setOverrideSize(size);
}
void RenderTableCell::setOverrideSizeFromRowHeight(int rowHeight)
{
clearIntrinsicPadding();
RenderBlock::setOverrideSize(max(0, rowHeight - borderBefore() - paddingBefore() - borderAfter() - paddingAfter()));
}
IntSize RenderTableCell::offsetFromContainer(RenderObject* o, const IntPoint& point) const
{
ASSERT(o == container());
IntSize offset = RenderBlock::offsetFromContainer(o, point);
if (parent())
offset.expand(-parentBox()->x(), -parentBox()->y());
return offset;
}
IntRect RenderTableCell::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
{
if (!table()->collapseBorders() || table()->needsSectionRecalc())
return RenderBlock::clippedOverflowRectForRepaint(repaintContainer);
bool rtl = !table()->style()->isLeftToRightDirection();
int outlineSize = style()->outlineSize();
int left = max(borderHalfLeft(true), outlineSize);
int right = max(borderHalfRight(true), outlineSize);
int top = max(borderHalfTop(true), outlineSize);
int bottom = max(borderHalfBottom(true), outlineSize);
if ((left && !rtl) || (right && rtl)) {
if (RenderTableCell* before = table()->cellBefore(this)) {
top = max(top, before->borderHalfTop(true));
bottom = max(bottom, before->borderHalfBottom(true));
}
}
if ((left && rtl) || (right && !rtl)) {
if (RenderTableCell* after = table()->cellAfter(this)) {
top = max(top, after->borderHalfTop(true));
bottom = max(bottom, after->borderHalfBottom(true));
}
}
if (top) {
if (RenderTableCell* above = table()->cellAbove(this)) {
left = max(left, above->borderHalfLeft(true));
right = max(right, above->borderHalfRight(true));
}
}
if (bottom) {
if (RenderTableCell* below = table()->cellBelow(this)) {
left = max(left, below->borderHalfLeft(true));
right = max(right, below->borderHalfRight(true));
}
}
left = max(left, -minXVisualOverflow());
top = max(top, -minYVisualOverflow());
IntRect r(-left, - top, left + max(width() + right, maxXVisualOverflow()), top + max(height() + bottom, maxYVisualOverflow()));
if (RenderView* v = view()) {
r.move(v->layoutDelta());
}
computeRectForRepaint(repaintContainer, r);
return r;
}
void RenderTableCell::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& r, bool fixed)
{
if (repaintContainer == this)
return;
r.setY(r.y());
RenderView* v = view();
if ((!v || !v->layoutStateEnabled() || repaintContainer) && parent())
r.move(-parentBox()->x(), -parentBox()->y()); RenderBlock::computeRectForRepaint(repaintContainer, r, fixed);
}
int RenderTableCell::cellBaselinePosition() const
{
int firstLineBaseline = firstLineBoxBaseline();
if (firstLineBaseline != -1)
return firstLineBaseline;
return paddingBefore() + borderBefore() + contentLogicalHeight();
}
void RenderTableCell::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
if (parent() && section() && style() && style()->height() != newStyle->height())
section()->setNeedsCellRecalc();
ASSERT(newStyle->display() == TABLE_CELL);
RenderBlock::styleWillChange(diff, newStyle);
}
void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
setHasBoxDecorations(true);
}
static int compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
{
if (!border2.exists()) {
if (!border1.exists())
return 0;
return 1;
}
if (!border1.exists())
return -1;
if (border2.style() == BHIDDEN) {
if (border1.style() == BHIDDEN)
return 0;
return -1;
}
if (border1.style() == BHIDDEN)
return 1;
if (border2.style() == BNONE) {
if (border1.style() == BNONE)
return 0;
return 1;
}
if (border1.style() == BNONE)
return -1;
if (border1.width() != border2.width())
return border1.width() < border2.width() ? -1 : 1;
if (border1.style() != border2.style())
return border1.style() < border2.style() ? -1 : 1;
if (border1.precedence() == border2.precedence())
return 0;
return border1.precedence() < border2.precedence() ? -1 : 1;
}
static CollapsedBorderValue chooseBorder(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
{
const CollapsedBorderValue& border = compareBorders(border1, border2) < 0 ? border2 : border1;
return border.style() == BHIDDEN ? CollapsedBorderValue() : border;
}
CollapsedBorderValue RenderTableCell::collapsedStartBorder() const
{
RenderTable* table = this->table();
bool isStartColumn = col() == 0;
int start = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderStartColor, table->style()->direction(), table->style()->writingMode());
int end = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderEndColor, table->style()->direction(), table->style()->writingMode());
CollapsedBorderValue result(&style()->borderStart(), style()->visitedDependentColor(start), BCELL);
if (RenderTableCell* prevCell = table->cellBefore(this)) {
CollapsedBorderValue prevCellBorder = CollapsedBorderValue(&prevCell->style()->borderEnd(), prevCell->style()->visitedDependentColor(end), BCELL);
result = chooseBorder(prevCellBorder, result);
if (!result.exists())
return result;
} else if (isStartColumn) {
result = chooseBorder(result, CollapsedBorderValue(&parent()->style()->borderStart(), parent()->style()->visitedDependentColor(start), BROW));
if (!result.exists())
return result;
result = chooseBorder(result, CollapsedBorderValue(§ion()->style()->borderStart(), section()->style()->visitedDependentColor(start), BROWGROUP));
if (!result.exists())
return result;
}
bool startColEdge;
bool endColEdge;
RenderTableCol* colElt = table->colElement(col(), &startColEdge, &endColEdge);
if (colElt && startColEdge) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->style()->borderStart(), colElt->style()->visitedDependentColor(start), BCOL));
if (!result.exists())
return result;
if (colElt->parent()->isTableCol() && !colElt->previousSibling()) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->parent()->style()->borderStart(), colElt->parent()->style()->visitedDependentColor(start), BCOLGROUP));
if (!result.exists())
return result;
}
}
if (!isStartColumn) {
colElt = table->colElement(col() -1, &startColEdge, &endColEdge);
if (colElt && endColEdge) {
CollapsedBorderValue endBorder = CollapsedBorderValue(&colElt->style()->borderEnd(), colElt->style()->visitedDependentColor(end), BCOL);
result = chooseBorder(endBorder, result);
if (!result.exists())
return result;
}
} else {
result = chooseBorder(result, CollapsedBorderValue(&table->style()->borderStart(), table->style()->visitedDependentColor(start), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedEndBorder() const
{
RenderTable* table = this->table();
bool isEndColumn = table->colToEffCol(col() + colSpan() - 1) == table->numEffCols() - 1;
int start = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderStartColor, table->style()->direction(), table->style()->writingMode());
int end = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderEndColor, table->style()->direction(), table->style()->writingMode());
CollapsedBorderValue result = CollapsedBorderValue(&style()->borderEnd(), style()->visitedDependentColor(end), BCELL);
if (!isEndColumn) {
RenderTableCell* nextCell = table->cellAfter(this);
if (nextCell && nextCell->style()) {
CollapsedBorderValue startBorder = CollapsedBorderValue(&nextCell->style()->borderStart(), nextCell->style()->visitedDependentColor(start), BCELL);
result = chooseBorder(result, startBorder);
if (!result.exists())
return result;
}
} else {
result = chooseBorder(result, CollapsedBorderValue(&parent()->style()->borderEnd(), parent()->style()->visitedDependentColor(end), BROW));
if (!result.exists())
return result;
result = chooseBorder(result, CollapsedBorderValue(§ion()->style()->borderEnd(), section()->style()->visitedDependentColor(end), BROWGROUP));
if (!result.exists())
return result;
}
bool startColEdge;
bool endColEdge;
RenderTableCol* colElt = table->colElement(col() + colSpan() - 1, &startColEdge, &endColEdge);
if (colElt && endColEdge) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->style()->borderEnd(), colElt->style()->visitedDependentColor(end), BCOL));
if (!result.exists())
return result;
if (colElt->parent()->isTableCol() && !colElt->nextSibling()) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->parent()->style()->borderEnd(), colElt->parent()->style()->visitedDependentColor(end), BCOLGROUP));
if (!result.exists())
return result;
}
}
if (!isEndColumn) {
colElt = table->colElement(col() + colSpan(), &startColEdge, &endColEdge);
if (colElt && startColEdge) {
CollapsedBorderValue startBorder = CollapsedBorderValue(&colElt->style()->borderStart(), colElt->style()->visitedDependentColor(start), BCOL);
result = chooseBorder(result, startBorder);
if (!result.exists())
return result;
}
} else {
result = chooseBorder(result, CollapsedBorderValue(&table->style()->borderEnd(), table->style()->visitedDependentColor(end), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedBeforeBorder() const
{
RenderTable* table = this->table();
int before = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderBeforeColor, table->style()->direction(), table->style()->writingMode());
int after = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderAfterColor, table->style()->direction(), table->style()->writingMode());
CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBefore(), style()->visitedDependentColor(before), BCELL);
RenderTableCell* prevCell = table->cellAbove(this);
if (prevCell) {
result = chooseBorder(CollapsedBorderValue(&prevCell->style()->borderAfter(), prevCell->style()->visitedDependentColor(after), BCELL), result);
if (!result.exists())
return result;
}
result = chooseBorder(result, CollapsedBorderValue(&parent()->style()->borderBefore(), parent()->style()->visitedDependentColor(before), BROW));
if (!result.exists())
return result;
if (prevCell) {
RenderObject* prevRow = 0;
if (prevCell->section() == section())
prevRow = parent()->previousSibling();
else
prevRow = prevCell->section()->lastChild();
if (prevRow) {
result = chooseBorder(CollapsedBorderValue(&prevRow->style()->borderAfter(), prevRow->style()->visitedDependentColor(after), BROW), result);
if (!result.exists())
return result;
}
}
RenderTableSection* currSection = section();
if (!row()) {
result = chooseBorder(result, CollapsedBorderValue(&currSection->style()->borderBefore(), currSection->style()->visitedDependentColor(before), BROWGROUP));
if (!result.exists())
return result;
currSection = table->sectionAbove(currSection);
if (currSection) {
result = chooseBorder(CollapsedBorderValue(&currSection->style()->borderAfter(), currSection->style()->visitedDependentColor(after), BROWGROUP), result);
if (!result.exists())
return result;
}
}
if (!currSection) {
RenderTableCol* colElt = table->colElement(col());
if (colElt) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->style()->borderBefore(), colElt->style()->visitedDependentColor(before), BCOL));
if (!result.exists())
return result;
if (colElt->parent()->isTableCol()) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->parent()->style()->borderBefore(), colElt->parent()->style()->visitedDependentColor(before), BCOLGROUP));
if (!result.exists())
return result;
}
}
result = chooseBorder(result, CollapsedBorderValue(&table->style()->borderBefore(), table->style()->visitedDependentColor(before), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedAfterBorder() const
{
RenderTable* table = this->table();
int before = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderBeforeColor, table->style()->direction(), table->style()->writingMode());
int after = CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderAfterColor, table->style()->direction(), table->style()->writingMode());
CollapsedBorderValue result = CollapsedBorderValue(&style()->borderAfter(), style()->visitedDependentColor(after), BCELL);
RenderTableCell* nextCell = table->cellBelow(this);
if (nextCell) {
result = chooseBorder(result, CollapsedBorderValue(&nextCell->style()->borderBefore(), nextCell->style()->visitedDependentColor(before), BCELL));
if (!result.exists())
return result;
}
result = chooseBorder(result, CollapsedBorderValue(&parent()->style()->borderAfter(), parent()->style()->visitedDependentColor(after), BROW));
if (!result.exists())
return result;
if (nextCell) {
result = chooseBorder(result, CollapsedBorderValue(&nextCell->parent()->style()->borderBefore(), nextCell->parent()->style()->visitedDependentColor(before), BROW));
if (!result.exists())
return result;
}
RenderTableSection* currSection = section();
if (row() + rowSpan() >= currSection->numRows()) {
result = chooseBorder(result, CollapsedBorderValue(&currSection->style()->borderAfter(), currSection->style()->visitedDependentColor(after), BROWGROUP));
if (!result.exists())
return result;
currSection = table->sectionBelow(currSection);
if (currSection) {
result = chooseBorder(result, CollapsedBorderValue(&currSection->style()->borderBefore(), currSection->style()->visitedDependentColor(before), BROWGROUP));
if (!result.exists())
return result;
}
}
if (!currSection) {
RenderTableCol* colElt = table->colElement(col());
if (colElt) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->style()->borderAfter(), colElt->style()->visitedDependentColor(after), BCOL));
if (!result.exists()) return result;
if (colElt->parent()->isTableCol()) {
result = chooseBorder(result, CollapsedBorderValue(&colElt->parent()->style()->borderAfter(), colElt->parent()->style()->visitedDependentColor(after), BCOLGROUP));
if (!result.exists())
return result;
}
}
result = chooseBorder(result, CollapsedBorderValue(&table->style()->borderAfter(), table->style()->visitedDependentColor(after), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedLeftBorder() const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isLeftToRightDirection() ? collapsedStartBorder() : collapsedEndBorder();
return tableStyle->isFlippedBlocksWritingMode() ? collapsedAfterBorder() : collapsedBeforeBorder();
}
CollapsedBorderValue RenderTableCell::collapsedRightBorder() const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isLeftToRightDirection() ? collapsedEndBorder() : collapsedStartBorder();
return tableStyle->isFlippedBlocksWritingMode() ? collapsedBeforeBorder() : collapsedAfterBorder();
}
CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isFlippedBlocksWritingMode() ? collapsedAfterBorder() : collapsedBeforeBorder();
return tableStyle->isLeftToRightDirection() ? collapsedStartBorder() : collapsedEndBorder();
}
CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isFlippedBlocksWritingMode() ? collapsedBeforeBorder() : collapsedAfterBorder();
return tableStyle->isLeftToRightDirection() ? collapsedEndBorder() : collapsedStartBorder();
}
int RenderTableCell::borderLeft() const
{
return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlock::borderLeft();
}
int RenderTableCell::borderRight() const
{
return table()->collapseBorders() ? borderHalfRight(false) : RenderBlock::borderRight();
}
int RenderTableCell::borderTop() const
{
return table()->collapseBorders() ? borderHalfTop(false) : RenderBlock::borderTop();
}
int RenderTableCell::borderBottom() const
{
return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlock::borderBottom();
}
int RenderTableCell::borderStart() const
{
return table()->collapseBorders() ? borderHalfStart(false) : RenderBlock::borderStart();
}
int RenderTableCell::borderEnd() const
{
return table()->collapseBorders() ? borderHalfEnd(false) : RenderBlock::borderEnd();
}
int RenderTableCell::borderBefore() const
{
return table()->collapseBorders() ? borderHalfBefore(false) : RenderBlock::borderBefore();
}
int RenderTableCell::borderAfter() const
{
return table()->collapseBorders() ? borderHalfAfter(false) : RenderBlock::borderAfter();
}
int RenderTableCell::borderHalfLeft(bool outer) const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isLeftToRightDirection() ? borderHalfStart(outer) : borderHalfEnd(outer);
return tableStyle->isFlippedBlocksWritingMode() ? borderHalfAfter(outer) : borderHalfBefore(outer);
}
int RenderTableCell::borderHalfRight(bool outer) const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isLeftToRightDirection() ? borderHalfEnd(outer) : borderHalfStart(outer);
return tableStyle->isFlippedBlocksWritingMode() ? borderHalfBefore(outer) : borderHalfAfter(outer);
}
int RenderTableCell::borderHalfTop(bool outer) const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isFlippedBlocksWritingMode() ? borderHalfAfter(outer) : borderHalfBefore(outer);
return tableStyle->isLeftToRightDirection() ? borderHalfStart(outer) : borderHalfEnd(outer);
}
int RenderTableCell::borderHalfBottom(bool outer) const
{
RenderStyle* tableStyle = table()->style();
if (tableStyle->isHorizontalWritingMode())
return tableStyle->isFlippedBlocksWritingMode() ? borderHalfBefore(outer) : borderHalfAfter(outer);
return tableStyle->isLeftToRightDirection() ? borderHalfEnd(outer) : borderHalfStart(outer);
}
int RenderTableCell::borderHalfStart(bool outer) const
{
CollapsedBorderValue border = collapsedStartBorder();
if (border.exists())
return (border.width() + ((table()->style()->isLeftToRightDirection() ^ outer) ? 1 : 0)) / 2; return 0;
}
int RenderTableCell::borderHalfEnd(bool outer) const
{
CollapsedBorderValue border = collapsedEndBorder();
if (border.exists())
return (border.width() + ((table()->style()->isLeftToRightDirection() ^ outer) ? 0 : 1)) / 2;
return 0;
}
int RenderTableCell::borderHalfBefore(bool outer) const
{
CollapsedBorderValue border = collapsedBeforeBorder();
if (border.exists())
return (border.width() + ((table()->style()->isFlippedBlocksWritingMode() ^ outer) ? 0 : 1)) / 2; return 0;
}
int RenderTableCell::borderHalfAfter(bool outer) const
{
CollapsedBorderValue border = collapsedAfterBorder();
if (border.exists())
return (border.width() + ((table()->style()->isFlippedBlocksWritingMode() ^ outer) ? 1 : 0)) / 2;
return 0;
}
void RenderTableCell::paint(PaintInfo& paintInfo, int tx, int ty)
{
if (paintInfo.phase == PaintPhaseCollapsedTableBorders && style()->visibility() == VISIBLE) {
if (!paintInfo.shouldPaintWithinRoot(this))
return;
tx += x();
ty += y();
int os = 2 * maximalOutlineSize(paintInfo.phase);
if (ty - table()->outerBorderTop() < paintInfo.rect.maxY() + os
&& ty + height() + table()->outerBorderBottom() > paintInfo.rect.y() - os)
paintCollapsedBorder(paintInfo.context, IntRect(tx, ty, width(), height()));
return;
}
RenderBlock::paint(paintInfo, tx, ty);
}
static EBorderStyle collapsedBorderStyle(EBorderStyle style)
{
if (style == OUTSET)
return GROOVE;
if (style == INSET)
return RIDGE;
return style;
}
struct CollapsedBorder {
CollapsedBorderValue borderValue;
BoxSide side;
bool shouldPaint;
int x1;
int y1;
int x2;
int y2;
EBorderStyle style;
};
class CollapsedBorders {
public:
CollapsedBorders()
: m_count(0)
{
}
void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
int x1, int y1, int x2, int y2, EBorderStyle borderStyle)
{
if (borderValue.exists() && shouldPaint) {
m_borders[m_count].borderValue = borderValue;
m_borders[m_count].side = borderSide;
m_borders[m_count].shouldPaint = shouldPaint;
m_borders[m_count].x1 = x1;
m_borders[m_count].x2 = x2;
m_borders[m_count].y1 = y1;
m_borders[m_count].y2 = y2;
m_borders[m_count].style = borderStyle;
m_count++;
}
}
CollapsedBorder* nextBorder()
{
for (int i = 0; i < m_count; i++) {
if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
m_borders[i].shouldPaint = false;
return &m_borders[i];
}
}
return 0;
}
CollapsedBorder m_borders[4];
int m_count;
};
static void addBorderStyle(RenderTableCell::CollapsedBorderStyles& borderStyles, CollapsedBorderValue borderValue)
{
if (!borderValue.exists())
return;
size_t count = borderStyles.size();
for (size_t i = 0; i < count; ++i)
if (borderStyles[i] == borderValue)
return;
borderStyles.append(borderValue);
}
void RenderTableCell::collectBorderStyles(CollapsedBorderStyles& borderStyles) const
{
addBorderStyle(borderStyles, collapsedStartBorder());
addBorderStyle(borderStyles, collapsedEndBorder());
addBorderStyle(borderStyles, collapsedBeforeBorder());
addBorderStyle(borderStyles, collapsedAfterBorder());
}
static int compareBorderStylesForQSort(const void* pa, const void* pb)
{
const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
if (*a == *b)
return 0;
return compareBorders(*a, *b);
}
void RenderTableCell::sortBorderStyles(CollapsedBorderStyles& borderStyles)
{
qsort(borderStyles.data(), borderStyles.size(), sizeof(CollapsedBorderValue),
compareBorderStylesForQSort);
}
void RenderTableCell::paintCollapsedBorder(GraphicsContext* graphicsContext, const IntRect& paintRect)
{
if (!table()->currentBorderStyle() || graphicsContext->paintingDisabled())
return;
CollapsedBorderValue leftVal = collapsedLeftBorder();
CollapsedBorderValue rightVal = collapsedRightBorder();
CollapsedBorderValue topVal = collapsedTopBorder();
CollapsedBorderValue bottomVal = collapsedBottomBorder();
int topWidth = topVal.width();
int bottomWidth = bottomVal.width();
int leftWidth = leftVal.width();
int rightWidth = rightVal.width();
int x = paintRect.x() - leftWidth / 2;
int y = paintRect.y() - topWidth / 2;
int w = paintRect.width() + leftWidth / 2 + (rightWidth + 1) / 2;
int h = paintRect.height() + topWidth / 2 + (bottomWidth + 1) / 2;
EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent();
bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent();
bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent();
bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent();
CollapsedBorders borders;
borders.addBorder(topVal, BSTop, renderTop, x, y, x + w, y + topWidth, topStyle);
borders.addBorder(bottomVal, BSBottom, renderBottom, x, y + h - bottomWidth, x + w, y + h, bottomStyle);
borders.addBorder(leftVal, BSLeft, renderLeft, x, y, x + leftWidth, y + h, leftStyle);
borders.addBorder(rightVal, BSRight, renderRight, x + w - rightWidth, y, x + w, y + h, rightStyle);
const AffineTransform& currentCTM = graphicsContext->getCTM();
bool antialias = !currentCTM.isIdentityOrTranslationOrFlipped();
for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
if (border->borderValue == *table()->currentBorderStyle())
drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
border->borderValue.color(), border->style, 0, 0, antialias);
}
}
void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, int tx, int ty, RenderObject* backgroundObject)
{
if (!paintInfo.shouldPaintWithinRoot(this))
return;
if (!backgroundObject)
return;
if (style()->visibility() != VISIBLE)
return;
RenderTable* tableElt = table();
if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
return;
if (backgroundObject != this) {
tx += x();
ty += y();
}
int w = width();
int h = height();
Color c = backgroundObject->style()->visitedDependentColor(CSSPropertyBackgroundColor);
const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers();
if (bgLayer->hasImage() || c.isValid()) {
bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
GraphicsContextStateSaver stateSaver(*paintInfo.context, shouldClip);
if (shouldClip) {
IntRect clipRect(tx + borderLeft(), ty + borderTop(),
w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
paintInfo.context->clip(clipRect);
}
paintFillLayers(paintInfo, c, bgLayer, IntRect(tx, ty, w, h), BackgroundBleedNone, CompositeSourceOver, backgroundObject);
}
}
void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
{
if (!paintInfo.shouldPaintWithinRoot(this))
return;
RenderTable* tableElt = table();
if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
return;
IntRect paintRect = IntRect(IntPoint(tx, ty), size());
paintBoxShadow(paintInfo.context, paintRect, style(), Normal);
paintBackgroundsBehindCell(paintInfo, tx, ty, this);
paintBoxShadow(paintInfo.context, paintRect, style(), Inset);
if (!style()->hasBorder() || tableElt->collapseBorders())
return;
paintBorder(paintInfo.context, paintRect, style());
}
void RenderTableCell::paintMask(PaintInfo& paintInfo, IntSize paintOffset)
{
if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
return;
RenderTable* tableElt = table();
if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
return;
paintMaskImages(paintInfo, IntRect(toPoint(paintOffset), size()));
}
void RenderTableCell::scrollbarsChanged(bool horizontalScrollbarChanged, bool verticalScrollbarChanged)
{
int scrollbarHeight = scrollbarLogicalHeight();
if (!scrollbarHeight)
return;
if ((isHorizontalWritingMode() && !horizontalScrollbarChanged) ||
(!isHorizontalWritingMode() && !verticalScrollbarChanged))
return;
if (style()->verticalAlign() == MIDDLE) {
int totalHeight = logicalHeight();
int heightWithoutIntrinsicPadding = totalHeight - intrinsicPaddingBefore() - intrinsicPaddingAfter();
totalHeight -= scrollbarHeight;
int newBeforePadding = (totalHeight - heightWithoutIntrinsicPadding) / 2;
int newAfterPadding = totalHeight - heightWithoutIntrinsicPadding - newBeforePadding;
setIntrinsicPaddingBefore(newBeforePadding);
setIntrinsicPaddingAfter(newAfterPadding);
} else
setIntrinsicPaddingAfter(intrinsicPaddingAfter() - scrollbarHeight);
}
}