2 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 * (C) 1997 Torben Weis (weis@kde.org)
4 * (C) 1998 Waldo Bastian (bastian@kde.org)
5 * (C) 1999 Lars Knoll (knoll@kde.org)
6 * (C) 1999 Antti Koivisto (koivisto@kde.org)
7 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "RenderTableCell.h"
28 #include "FloatQuad.h"
29 #include "GraphicsContext.h"
30 #include "HTMLNames.h"
31 #include "HTMLTableCellElement.h"
32 #include "RenderTableCol.h"
33 #include "RenderView.h"
34 #include "TransformState.h"
45 using namespace HTMLNames;
47 RenderTableCell::RenderTableCell(Node* node)
53 , m_intrinsicPaddingTop(0)
54 , m_intrinsicPaddingBottom(0)
55 , m_percentageHeight(0)
60 void RenderTableCell::destroy()
62 RenderTableSection* recalcSection = parent() ? section() : 0;
64 RenderBlock::destroy();
67 recalcSection->setNeedsCellRecalc();
70 void RenderTableCell::updateFromElement()
73 if (n && (n->hasTagName(tdTag) || n->hasTagName(thTag))) {
74 HTMLTableCellElement* tc = static_cast<HTMLTableCellElement*>(n);
75 int oldRSpan = m_rowSpan;
76 int oldCSpan = m_columnSpan;
78 m_columnSpan = tc->colSpan();
79 m_rowSpan = tc->rowSpan();
80 if ((oldRSpan != m_rowSpan || oldCSpan != m_columnSpan) && style() && parent()) {
81 setNeedsLayoutAndPrefWidthsRecalc();
83 section()->setNeedsCellRecalc();
88 Length RenderTableCell::styleOrColWidth() const
90 Length w = style()->width();
94 RenderTableCol* tableCol = table()->colElement(col());
97 int colSpanCount = colSpan();
99 Length colWidthSum = Length(0, Fixed);
100 for (int i = 1; i <= colSpanCount; i++) {
101 Length colWidth = tableCol->style()->width();
103 // Percentage value should be returned only for colSpan == 1.
104 // Otherwise we return original width for the cell.
105 if (!colWidth.isFixed()) {
106 if (colSpanCount > 1)
111 colWidthSum = Length(colWidthSum.value() + colWidth.value(), Fixed);
113 tableCol = table()->nextColElement(tableCol);
114 // If no next <col> tag found for the span we just return what we have for now.
119 // Column widths specified on <col> apply to the border box of the cell.
120 // Percentages don't need to be handled since they're always treated this way (even when specified on the cells).
121 // See Bugzilla bug 8126 for details.
122 if (colWidthSum.isFixed() && colWidthSum.value() > 0)
123 colWidthSum = Length(max(0, colWidthSum.value() - borderAndPaddingWidth()), Fixed);
130 void RenderTableCell::calcPrefWidths()
132 // The child cells rely on the grids up in the sections to do their calcPrefWidths work. Normally the sections are set up early, as table
133 // cells are added, but relayout can cause the cells to be freed, leaving stale pointers in the sections'
134 // grids. We must refresh those grids before the child cells try to use them.
135 table()->recalcSectionsIfNeeded();
137 RenderBlock::calcPrefWidths();
138 if (node() && style()->autoWrap()) {
139 // See if nowrap was set.
140 Length w = styleOrColWidth();
141 String nowrap = static_cast<Element*>(node())->getAttribute(nowrapAttr);
142 if (!nowrap.isNull() && w.isFixed())
143 // Nowrap is set, but we didn't actually use it because of the
144 // fixed width set on the cell. Even so, it is a WinIE/Moz trait
145 // to make the minwidth of the cell into the fixed width. They do this
146 // even in strict mode, so do not make this a quirk. Affected the top
148 m_minPrefWidth = max(w.value(), m_minPrefWidth);
152 void RenderTableCell::calcWidth()
154 #ifdef ANDROID_LAYOUT
155 if (view()->frameView())
156 setVisibleWidth(view()->frameView()->textWrapWidth());
160 void RenderTableCell::updateWidth(int w)
164 setCellWidthChanged(true);
168 void RenderTableCell::layout()
170 layoutBlock(cellWidthChanged());
171 setCellWidthChanged(false);
174 int RenderTableCell::paddingTop(bool includeIntrinsicPadding) const
176 return RenderBlock::paddingTop() + (includeIntrinsicPadding ? intrinsicPaddingTop() : 0);
179 int RenderTableCell::paddingBottom(bool includeIntrinsicPadding) const
181 return RenderBlock::paddingBottom() + (includeIntrinsicPadding ? intrinsicPaddingBottom() : 0);
184 void RenderTableCell::setOverrideSize(int size)
186 clearIntrinsicPadding();
187 RenderBlock::setOverrideSize(size);
190 IntSize RenderTableCell::offsetFromContainer(RenderObject* o, const IntPoint& point) const
192 ASSERT(o == container());
194 IntSize offset = RenderBlock::offsetFromContainer(o, point);
196 offset.expand(-parentBox()->x(), -parentBox()->y());
201 IntRect RenderTableCell::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
203 // If the table grid is dirty, we cannot get reliable information about adjoining cells,
204 // so we ignore outside borders. This should not be a problem because it means that
205 // the table is going to recalculate the grid, relayout and repaint its current rect, which
206 // includes any outside borders of this cell.
207 if (!table()->collapseBorders() || table()->needsSectionRecalc())
208 return RenderBlock::clippedOverflowRectForRepaint(repaintContainer);
210 bool rtl = table()->style()->direction() == RTL;
211 int outlineSize = style()->outlineSize();
212 int left = max(borderHalfLeft(true), outlineSize);
213 int right = max(borderHalfRight(true), outlineSize);
214 int top = max(borderHalfTop(true), outlineSize);
215 int bottom = max(borderHalfBottom(true), outlineSize);
216 if ((left && !rtl) || (right && rtl)) {
217 if (RenderTableCell* before = table()->cellBefore(this)) {
218 top = max(top, before->borderHalfTop(true));
219 bottom = max(bottom, before->borderHalfBottom(true));
222 if ((left && rtl) || (right && !rtl)) {
223 if (RenderTableCell* after = table()->cellAfter(this)) {
224 top = max(top, after->borderHalfTop(true));
225 bottom = max(bottom, after->borderHalfBottom(true));
229 if (RenderTableCell* above = table()->cellAbove(this)) {
230 left = max(left, above->borderHalfLeft(true));
231 right = max(right, above->borderHalfRight(true));
235 if (RenderTableCell* below = table()->cellBelow(this)) {
236 left = max(left, below->borderHalfLeft(true));
237 right = max(right, below->borderHalfRight(true));
240 left = max(left, -leftVisibleOverflow());
241 top = max(top, -topVisibleOverflow());
242 IntRect r(-left, - top, left + max(width() + right, rightVisibleOverflow()), top + max(height() + bottom, bottomVisibleOverflow()));
244 if (RenderView* v = view()) {
245 // FIXME: layoutDelta needs to be applied in parts before/after transforms and
246 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
247 r.move(v->layoutDelta());
249 computeRectForRepaint(repaintContainer, r);
253 void RenderTableCell::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& r, bool fixed)
255 if (repaintContainer == this)
258 RenderView* v = view();
259 if ((!v || !v->layoutStateEnabled() || repaintContainer) && parent())
260 r.move(-parentBox()->x(), -parentBox()->y()); // Rows are in the same coordinate space, so don't add their offset in.
261 RenderBlock::computeRectForRepaint(repaintContainer, r, fixed);
264 int RenderTableCell::baselinePosition(bool firstLine, bool isRootLineBox) const
267 return RenderBox::baselinePosition(firstLine, isRootLineBox);
269 // <http://www.w3.org/TR/2007/CR-CSS21-20070719/tables.html#height-layout>: The baseline of a cell is the baseline of
270 // the first in-flow line box in the cell, or the first in-flow table-row in the cell, whichever comes first. If there
271 // is no such line box or table-row, the baseline is the bottom of content edge of the cell box.
272 int firstLineBaseline = firstLineBoxBaseline();
273 if (firstLineBaseline != -1)
274 return firstLineBaseline;
275 return paddingTop() + borderTop() + contentHeight();
278 void RenderTableCell::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
280 if (parent() && section() && style() && style()->height() != newStyle->height())
281 section()->setNeedsCellRecalc();
283 ASSERT(newStyle->display() == TABLE_CELL);
285 RenderBlock::styleWillChange(diff, newStyle);
288 void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
290 RenderBlock::styleDidChange(diff, oldStyle);
291 setHasBoxDecorations(true);
294 // The following rules apply for resolving conflicts and figuring out which border
296 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
297 // borders. Any border with this value suppresses all borders at this location.
298 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
299 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
300 // the default value for the border style.)
301 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
302 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
303 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
304 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
305 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
306 // is used when two elements of the same type disagree.
307 static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
309 // Sanity check the values passed in. If either is null, return the other.
310 if (!border2.exists())
312 if (!border1.exists())
316 if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
317 return CollapsedBorderValue(); // No border should exist at this location.
319 // Rule #2 above. A style of 'none' has lowest priority and always loses to any other border.
320 if (border2.style() == BNONE)
322 if (border1.style() == BNONE)
325 // The first part of rule #3 above. Wider borders win.
326 if (border1.width() != border2.width())
327 return border1.width() > border2.width() ? border1 : border2;
329 // The borders have equal width. Sort by border style.
330 if (border1.style() != border2.style())
331 return border1.style() > border2.style() ? border1 : border2;
333 // The border have the same width and style. Rely on precedence (cell over row over row group, etc.)
334 return border1.precedence() >= border2.precedence() ? border1 : border2;
337 CollapsedBorderValue RenderTableCell::collapsedLeftBorder(bool rtl) const
339 RenderTable* tableElt = table();
342 leftmostColumn = col() == 0;
344 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
345 leftmostColumn = effCol == tableElt->numEffCols() - 1;
348 // For border left, we need to check, in order of precedence:
349 // (1) Our left border.
350 int left = CSSPropertyBorderLeftColor;
351 int right = CSSPropertyBorderRightColor;
352 CollapsedBorderValue result(&style()->borderLeft(), style()->visitedDependentColor(left), BCELL);
354 // (2) The right border of the cell to the left.
355 RenderTableCell* prevCell = rtl ? tableElt->cellAfter(this) : tableElt->cellBefore(this);
357 CollapsedBorderValue prevCellBorder = CollapsedBorderValue(&prevCell->style()->borderRight(), prevCell->style()->visitedDependentColor(right), BCELL);
358 result = rtl ? compareBorders(result, prevCellBorder) : compareBorders(prevCellBorder, result);
359 if (!result.exists())
361 } else if (leftmostColumn) {
362 // (3) Our row's left border.
363 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), parent()->style()->visitedDependentColor(left), BROW));
364 if (!result.exists())
367 // (4) Our row group's left border.
368 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderLeft(), section()->style()->visitedDependentColor(left), BROWGROUP));
369 if (!result.exists())
373 // (5) Our column and column group's left borders.
376 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? colSpan() - 1 : 0), &startColEdge, &endColEdge);
377 if (colElt && (!rtl ? startColEdge : endColEdge)) {
378 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), colElt->style()->visitedDependentColor(left), BCOL));
379 if (!result.exists())
381 if (colElt->parent()->isTableCol() && (!rtl ? !colElt->previousSibling() : !colElt->nextSibling())) {
382 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderLeft(), colElt->parent()->style()->visitedDependentColor(left), BCOLGROUP));
383 if (!result.exists())
388 // (6) The right border of the column to the left.
389 if (!leftmostColumn) {
390 colElt = tableElt->colElement(col() + (rtl ? colSpan() : -1), &startColEdge, &endColEdge);
391 if (colElt && (!rtl ? endColEdge : startColEdge)) {
392 CollapsedBorderValue rightBorder = CollapsedBorderValue(&colElt->style()->borderRight(), colElt->style()->visitedDependentColor(right), BCOL);
393 result = rtl ? compareBorders(result, rightBorder) : compareBorders(rightBorder, result);
394 if (!result.exists())
398 // (7) The table's left border.
399 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderLeft(), tableElt->style()->visitedDependentColor(left), BTABLE));
400 if (!result.exists())
407 CollapsedBorderValue RenderTableCell::collapsedRightBorder(bool rtl) const
409 RenderTable* tableElt = table();
410 bool rightmostColumn;
412 rightmostColumn = col() == 0;
414 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
415 rightmostColumn = effCol == tableElt->numEffCols() - 1;
418 // For border right, we need to check, in order of precedence:
419 // (1) Our right border.
420 int left = CSSPropertyBorderLeftColor;
421 int right = CSSPropertyBorderRightColor;
422 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), style()->visitedDependentColor(right), BCELL);
424 // (2) The left border of the cell to the right.
425 if (!rightmostColumn) {
426 RenderTableCell* nextCell = rtl ? tableElt->cellBefore(this) : tableElt->cellAfter(this);
427 if (nextCell && nextCell->style()) {
428 CollapsedBorderValue leftBorder = CollapsedBorderValue(&nextCell->style()->borderLeft(), nextCell->style()->visitedDependentColor(left), BCELL);
429 result = rtl ? compareBorders(leftBorder, result) : compareBorders(result, leftBorder);
430 if (!result.exists())
434 // (3) Our row's right border.
435 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), parent()->style()->visitedDependentColor(right), BROW));
436 if (!result.exists())
439 // (4) Our row group's right border.
440 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderRight(), section()->style()->visitedDependentColor(right), BROWGROUP));
441 if (!result.exists())
445 // (5) Our column and column group's right borders.
448 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? 0 : colSpan() - 1), &startColEdge, &endColEdge);
449 if (colElt && (!rtl ? endColEdge : startColEdge)) {
450 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), colElt->style()->visitedDependentColor(right), BCOL));
451 if (!result.exists())
453 if (colElt->parent()->isTableCol() && (!rtl ? !colElt->nextSibling() : !colElt->previousSibling())) {
454 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderRight(), colElt->parent()->style()->visitedDependentColor(right), BCOLGROUP));
455 if (!result.exists())
460 // (6) The left border of the column to the right.
461 if (!rightmostColumn) {
462 colElt = tableElt->colElement(col() + (rtl ? -1 : colSpan()), &startColEdge, &endColEdge);
463 if (colElt && (!rtl ? startColEdge : endColEdge)) {
464 CollapsedBorderValue leftBorder = CollapsedBorderValue(&colElt->style()->borderLeft(), colElt->style()->visitedDependentColor(left), BCOL);
465 result = rtl ? compareBorders(leftBorder, result) : compareBorders(result, leftBorder);
466 if (!result.exists())
470 // (7) The table's right border.
471 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), tableElt->style()->visitedDependentColor(right), BTABLE));
472 if (!result.exists())
479 CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
481 // For border top, we need to check, in order of precedence:
482 // (1) Our top border.
483 int top = CSSPropertyBorderTopColor;
484 int bottom = CSSPropertyBorderBottomColor;
485 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), style()->visitedDependentColor(top), BCELL);
487 RenderTableCell* prevCell = table()->cellAbove(this);
489 // (2) A previous cell's bottom border.
490 result = compareBorders(CollapsedBorderValue(&prevCell->style()->borderBottom(), prevCell->style()->visitedDependentColor(bottom), BCELL), result);
491 if (!result.exists())
495 // (3) Our row's top border.
496 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), parent()->style()->visitedDependentColor(top), BROW));
497 if (!result.exists())
500 // (4) The previous row's bottom border.
502 RenderObject* prevRow = 0;
503 if (prevCell->section() == section())
504 prevRow = parent()->previousSibling();
506 prevRow = prevCell->section()->lastChild();
509 result = compareBorders(CollapsedBorderValue(&prevRow->style()->borderBottom(), prevRow->style()->visitedDependentColor(bottom), BROW), result);
510 if (!result.exists())
515 // Now check row groups.
516 RenderTableSection* currSection = section();
518 // (5) Our row group's top border.
519 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), currSection->style()->visitedDependentColor(top), BROWGROUP));
520 if (!result.exists())
523 // (6) Previous row group's bottom border.
524 currSection = table()->sectionAbove(currSection);
526 result = compareBorders(CollapsedBorderValue(&currSection->style()->borderBottom(), currSection->style()->visitedDependentColor(bottom), BROWGROUP), result);
527 if (!result.exists())
533 // (8) Our column and column group's top borders.
534 RenderTableCol* colElt = table()->colElement(col());
536 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), colElt->style()->visitedDependentColor(top), BCOL));
537 if (!result.exists())
539 if (colElt->parent()->isTableCol()) {
540 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderTop(), colElt->parent()->style()->visitedDependentColor(top), BCOLGROUP));
541 if (!result.exists())
546 // (9) The table's top border.
547 RenderTable* enclosingTable = table();
548 result = compareBorders(result, CollapsedBorderValue(&enclosingTable->style()->borderTop(), enclosingTable->style()->visitedDependentColor(top), BTABLE));
549 if (!result.exists())
556 CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
558 // For border top, we need to check, in order of precedence:
559 // (1) Our bottom border.
560 int top = CSSPropertyBorderTopColor;
561 int bottom = CSSPropertyBorderBottomColor;
562 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), style()->visitedDependentColor(bottom), BCELL);
564 RenderTableCell* nextCell = table()->cellBelow(this);
566 // (2) A following cell's top border.
567 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), nextCell->style()->visitedDependentColor(top), BCELL));
568 if (!result.exists())
572 // (3) Our row's bottom border. (FIXME: Deal with rowspan!)
573 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), parent()->style()->visitedDependentColor(bottom), BROW));
574 if (!result.exists())
577 // (4) The next row's top border.
579 result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), nextCell->parent()->style()->visitedDependentColor(top), BROW));
580 if (!result.exists())
584 // Now check row groups.
585 RenderTableSection* currSection = section();
586 if (row() + rowSpan() >= currSection->numRows()) {
587 // (5) Our row group's bottom border.
588 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), currSection->style()->visitedDependentColor(bottom), BROWGROUP));
589 if (!result.exists())
592 // (6) Following row group's top border.
593 currSection = table()->sectionBelow(currSection);
595 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), currSection->style()->visitedDependentColor(top), BROWGROUP));
596 if (!result.exists())
602 // (8) Our column and column group's bottom borders.
603 RenderTableCol* colElt = table()->colElement(col());
605 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), colElt->style()->visitedDependentColor(bottom), BCOL));
606 if (!result.exists()) return result;
607 if (colElt->parent()->isTableCol()) {
608 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderBottom(), colElt->parent()->style()->visitedDependentColor(bottom), BCOLGROUP));
609 if (!result.exists())
614 // (9) The table's bottom border.
615 RenderTable* enclosingTable = table();
616 result = compareBorders(result, CollapsedBorderValue(&enclosingTable->style()->borderBottom(), enclosingTable->style()->visitedDependentColor(bottom), BTABLE));
617 if (!result.exists())
624 int RenderTableCell::borderLeft() const
626 return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlock::borderLeft();
629 int RenderTableCell::borderRight() const
631 return table()->collapseBorders() ? borderHalfRight(false) : RenderBlock::borderRight();
634 int RenderTableCell::borderTop() const
636 return table()->collapseBorders() ? borderHalfTop(false) : RenderBlock::borderTop();
639 int RenderTableCell::borderBottom() const
641 return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlock::borderBottom();
644 int RenderTableCell::borderHalfLeft(bool outer) const
646 CollapsedBorderValue border = collapsedLeftBorder(table()->style()->direction() == RTL);
648 return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
652 int RenderTableCell::borderHalfRight(bool outer) const
654 CollapsedBorderValue border = collapsedRightBorder(table()->style()->direction() == RTL);
656 return (border.width() + (outer ? 1 : 0)) / 2;
660 int RenderTableCell::borderHalfTop(bool outer) const
662 CollapsedBorderValue border = collapsedTopBorder();
664 return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
668 int RenderTableCell::borderHalfBottom(bool outer) const
670 CollapsedBorderValue border = collapsedBottomBorder();
672 return (border.width() + (outer ? 1 : 0)) / 2;
676 void RenderTableCell::paint(PaintInfo& paintInfo, int tx, int ty)
678 if (paintInfo.phase == PaintPhaseCollapsedTableBorders && style()->visibility() == VISIBLE) {
679 if (!paintInfo.shouldPaintWithinRoot(this))
684 int os = 2 * maximalOutlineSize(paintInfo.phase);
685 if (ty - table()->outerBorderTop() < paintInfo.rect.bottom() + os &&
686 ty + height() + table()->outerBorderBottom() > paintInfo.rect.y() - os)
687 paintCollapsedBorder(paintInfo.context, tx, ty, width(), height());
691 RenderBlock::paint(paintInfo, tx, ty);
694 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
703 struct CollapsedBorder {
704 CollapsedBorderValue borderValue;
714 class CollapsedBorders {
721 void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
722 int x1, int y1, int x2, int y2, EBorderStyle borderStyle)
724 if (borderValue.exists() && shouldPaint) {
725 m_borders[m_count].borderValue = borderValue;
726 m_borders[m_count].side = borderSide;
727 m_borders[m_count].shouldPaint = shouldPaint;
728 m_borders[m_count].x1 = x1;
729 m_borders[m_count].x2 = x2;
730 m_borders[m_count].y1 = y1;
731 m_borders[m_count].y2 = y2;
732 m_borders[m_count].style = borderStyle;
737 CollapsedBorder* nextBorder()
739 for (int i = 0; i < m_count; i++) {
740 if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
741 m_borders[i].shouldPaint = false;
742 return &m_borders[i];
749 CollapsedBorder m_borders[4];
753 static void addBorderStyle(RenderTableCell::CollapsedBorderStyles& borderStyles, CollapsedBorderValue borderValue)
755 if (!borderValue.exists())
757 size_t count = borderStyles.size();
758 for (size_t i = 0; i < count; ++i)
759 if (borderStyles[i] == borderValue)
761 borderStyles.append(borderValue);
764 void RenderTableCell::collectBorderStyles(CollapsedBorderStyles& borderStyles) const
766 bool rtl = table()->style()->direction() == RTL;
767 addBorderStyle(borderStyles, collapsedLeftBorder(rtl));
768 addBorderStyle(borderStyles, collapsedRightBorder(rtl));
769 addBorderStyle(borderStyles, collapsedTopBorder());
770 addBorderStyle(borderStyles, collapsedBottomBorder());
773 static int compareBorderStylesForQSort(const void* pa, const void* pb)
775 const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
776 const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
779 CollapsedBorderValue borderWithHigherPrecedence = compareBorders(*a, *b);
781 if (*a == borderWithHigherPrecedence) {
782 // klibc uses a combsort for quicksort and requires that two values always give the same answer
783 // regardless of comparison order. Unfortunately, compareBorders does not honor that requirement.
784 // Call compareBorders again with reversed parameters. If it returns the first value again then
785 // we can assume the values are equal. http://bugs.webkit.org/show_bug.cgi?id=13147
786 CollapsedBorderValue qSortHack = compareBorders(*b, *a);
792 if (*a == borderWithHigherPrecedence)
798 void RenderTableCell::sortBorderStyles(CollapsedBorderStyles& borderStyles)
800 qsort(borderStyles.data(), borderStyles.size(), sizeof(CollapsedBorderValue),
801 compareBorderStylesForQSort);
804 void RenderTableCell::paintCollapsedBorder(GraphicsContext* graphicsContext, int tx, int ty, int w, int h)
806 if (!table()->currentBorderStyle())
809 bool rtl = table()->style()->direction() == RTL;
810 CollapsedBorderValue leftVal = collapsedLeftBorder(rtl);
811 CollapsedBorderValue rightVal = collapsedRightBorder(rtl);
812 CollapsedBorderValue topVal = collapsedTopBorder();
813 CollapsedBorderValue bottomVal = collapsedBottomBorder();
815 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
816 int topWidth = topVal.width();
817 int bottomWidth = bottomVal.width();
818 int leftWidth = leftVal.width();
819 int rightWidth = rightVal.width();
823 w += leftWidth / 2 + (rightWidth + 1) / 2;
824 h += topWidth / 2 + (bottomWidth + 1) / 2;
826 EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
827 EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
828 EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
829 EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
831 bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent();
832 bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent();
833 bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent();
834 bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent();
836 // We never paint diagonals at the joins. We simply let the border with the highest
837 // precedence paint on top of borders with lower precedence.
838 CollapsedBorders borders;
839 borders.addBorder(topVal, BSTop, renderTop, tx, ty, tx + w, ty + topWidth, topStyle);
840 borders.addBorder(bottomVal, BSBottom, renderBottom, tx, ty + h - bottomWidth, tx + w, ty + h, bottomStyle);
841 borders.addBorder(leftVal, BSLeft, renderLeft, tx, ty, tx + leftWidth, ty + h, leftStyle);
842 borders.addBorder(rightVal, BSRight, renderRight, tx + w - rightWidth, ty, tx + w, ty + h, rightStyle);
844 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
845 if (border->borderValue == *table()->currentBorderStyle())
846 drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
847 border->borderValue.color(), border->style, 0, 0);
851 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, int tx, int ty, RenderObject* backgroundObject)
853 if (!paintInfo.shouldPaintWithinRoot(this))
856 if (!backgroundObject)
859 if (style()->visibility() != VISIBLE)
862 RenderTable* tableElt = table();
863 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
866 if (backgroundObject != this) {
874 Color c = backgroundObject->style()->visitedDependentColor(CSSPropertyBackgroundColor);
875 const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers();
877 if (bgLayer->hasImage() || c.isValid()) {
878 // We have to clip here because the background would paint
879 // on top of the borders otherwise. This only matters for cells and rows.
880 bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
882 IntRect clipRect(tx + borderLeft(), ty + borderTop(),
883 w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
884 paintInfo.context->save();
885 paintInfo.context->clip(clipRect);
887 paintFillLayers(paintInfo, c, bgLayer, tx, ty, w, h, CompositeSourceOver, backgroundObject);
889 paintInfo.context->restore();
893 void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
895 if (!paintInfo.shouldPaintWithinRoot(this))
898 RenderTable* tableElt = table();
899 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
905 if (style()->boxShadow())
906 paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Normal);
908 // Paint our cell background.
909 paintBackgroundsBehindCell(paintInfo, tx, ty, this);
910 if (style()->boxShadow())
911 paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Inset);
913 if (!style()->hasBorder() || tableElt->collapseBorders())
916 paintBorder(paintInfo.context, tx, ty, w, h, style());
919 void RenderTableCell::paintMask(PaintInfo& paintInfo, int tx, int ty)
921 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
924 RenderTable* tableElt = table();
925 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
931 paintMaskImages(paintInfo, tx, ty, w, h);
934 } // namespace WebCore