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();
91 if (colSpan() > 1 || !w.isAuto())
93 RenderTableCol* tableCol = table()->colElement(col());
95 w = tableCol->style()->width();
97 // Column widths specified on <col> apply to the border box of the cell.
98 // Percentages don't need to be handled since they're always treated this way (even when specified on the cells).
99 // See Bugzilla bug 8126 for details.
100 if (w.isFixed() && w.value() > 0)
101 w = Length(max(0, w.value() - borderLeft() - borderRight() - paddingLeft() - paddingRight()), Fixed);
106 void RenderTableCell::calcPrefWidths()
108 // 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
109 // cells are added, but relayout can cause the cells to be freed, leaving stale pointers in the sections'
110 // grids. We must refresh those grids before the child cells try to use them.
111 table()->recalcSectionsIfNeeded();
113 RenderBlock::calcPrefWidths();
114 if (node() && style()->autoWrap()) {
115 // See if nowrap was set.
116 Length w = styleOrColWidth();
117 String nowrap = static_cast<Element*>(node())->getAttribute(nowrapAttr);
118 if (!nowrap.isNull() && w.isFixed())
119 // Nowrap is set, but we didn't actually use it because of the
120 // fixed width set on the cell. Even so, it is a WinIE/Moz trait
121 // to make the minwidth of the cell into the fixed width. They do this
122 // even in strict mode, so do not make this a quirk. Affected the top
124 m_minPrefWidth = max(w.value(), m_minPrefWidth);
128 void RenderTableCell::calcWidth()
130 #ifdef ANDROID_LAYOUT
131 if (view()->frameView()) {
132 const Settings* settings = document()->settings();
134 if (settings->layoutAlgorithm() == Settings::kLayoutFitColumnToScreen) {
135 m_visibleWidth = view()->frameView()->screenWidth();
141 void RenderTableCell::updateWidth(int w)
145 setCellWidthChanged(true);
149 void RenderTableCell::layout()
151 layoutBlock(cellWidthChanged());
152 setCellWidthChanged(false);
155 int RenderTableCell::paddingTop(bool includeIntrinsicPadding) const
157 return RenderBlock::paddingTop() + (includeIntrinsicPadding ? intrinsicPaddingTop() : 0);
160 int RenderTableCell::paddingBottom(bool includeIntrinsicPadding) const
162 return RenderBlock::paddingBottom() + (includeIntrinsicPadding ? intrinsicPaddingBottom() : 0);
165 void RenderTableCell::setOverrideSize(int size)
167 clearIntrinsicPadding();
168 RenderBlock::setOverrideSize(size);
171 IntSize RenderTableCell::offsetFromContainer(RenderObject* o, const IntPoint& point) const
173 ASSERT(o == container());
175 IntSize offset = RenderBlock::offsetFromContainer(o, point);
177 offset.expand(-parentBox()->x(), -parentBox()->y());
182 IntRect RenderTableCell::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
184 // If the table grid is dirty, we cannot get reliable information about adjoining cells,
185 // so we ignore outside borders. This should not be a problem because it means that
186 // the table is going to recalculate the grid, relayout and repaint its current rect, which
187 // includes any outside borders of this cell.
188 if (!table()->collapseBorders() || table()->needsSectionRecalc())
189 return RenderBlock::clippedOverflowRectForRepaint(repaintContainer);
191 bool rtl = table()->style()->direction() == RTL;
192 int outlineSize = style()->outlineSize();
193 int left = max(borderHalfLeft(true), outlineSize);
194 int right = max(borderHalfRight(true), outlineSize);
195 int top = max(borderHalfTop(true), outlineSize);
196 int bottom = max(borderHalfBottom(true), outlineSize);
197 if ((left && !rtl) || (right && rtl)) {
198 if (RenderTableCell* before = table()->cellBefore(this)) {
199 top = max(top, before->borderHalfTop(true));
200 bottom = max(bottom, before->borderHalfBottom(true));
203 if ((left && rtl) || (right && !rtl)) {
204 if (RenderTableCell* after = table()->cellAfter(this)) {
205 top = max(top, after->borderHalfTop(true));
206 bottom = max(bottom, after->borderHalfBottom(true));
210 if (RenderTableCell* above = table()->cellAbove(this)) {
211 left = max(left, above->borderHalfLeft(true));
212 right = max(right, above->borderHalfRight(true));
216 if (RenderTableCell* below = table()->cellBelow(this)) {
217 left = max(left, below->borderHalfLeft(true));
218 right = max(right, below->borderHalfRight(true));
221 left = max(left, -leftVisibleOverflow());
222 top = max(top, -topVisibleOverflow());
223 IntRect r(-left, - top, left + max(width() + right, rightVisibleOverflow()), top + max(height() + bottom, bottomVisibleOverflow()));
225 if (RenderView* v = view()) {
226 // FIXME: layoutDelta needs to be applied in parts before/after transforms and
227 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
228 r.move(v->layoutDelta());
230 computeRectForRepaint(repaintContainer, r);
234 void RenderTableCell::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& r, bool fixed)
236 if (repaintContainer == this)
239 RenderView* v = view();
240 if ((!v || !v->layoutStateEnabled() || repaintContainer) && parent())
241 r.move(-parentBox()->x(), -parentBox()->y()); // Rows are in the same coordinate space, so don't add their offset in.
242 RenderBlock::computeRectForRepaint(repaintContainer, r, fixed);
245 int RenderTableCell::baselinePosition(bool firstLine, bool isRootLineBox) const
248 return RenderBox::baselinePosition(firstLine, isRootLineBox);
250 // <http://www.w3.org/TR/2007/CR-CSS21-20070719/tables.html#height-layout>: The baseline of a cell is the baseline of
251 // the first in-flow line box in the cell, or the first in-flow table-row in the cell, whichever comes first. If there
252 // is no such line box or table-row, the baseline is the bottom of content edge of the cell box.
253 int firstLineBaseline = firstLineBoxBaseline();
254 if (firstLineBaseline != -1)
255 return firstLineBaseline;
256 return paddingTop() + borderTop() + contentHeight();
259 void RenderTableCell::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
261 if (parent() && section() && style() && style()->height() != newStyle->height())
262 section()->setNeedsCellRecalc();
264 ASSERT(newStyle->display() == TABLE_CELL);
266 RenderBlock::styleWillChange(diff, newStyle);
269 void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
271 RenderBlock::styleDidChange(diff, oldStyle);
272 setHasBoxDecorations(true);
275 // The following rules apply for resolving conflicts and figuring out which border
277 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
278 // borders. Any border with this value suppresses all borders at this location.
279 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
280 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
281 // the default value for the border style.)
282 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
283 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
284 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
285 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
286 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
287 // is used when two elements of the same type disagree.
288 static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
290 // Sanity check the values passed in. If either is null, return the other.
291 if (!border2.exists())
293 if (!border1.exists())
297 if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
298 return CollapsedBorderValue(); // No border should exist at this location.
300 // Rule #2 above. A style of 'none' has lowest priority and always loses to any other border.
301 if (border2.style() == BNONE)
303 if (border1.style() == BNONE)
306 // The first part of rule #3 above. Wider borders win.
307 if (border1.width() != border2.width())
308 return border1.width() > border2.width() ? border1 : border2;
310 // The borders have equal width. Sort by border style.
311 if (border1.style() != border2.style())
312 return border1.style() > border2.style() ? border1 : border2;
314 // The border have the same width and style. Rely on precedence (cell over row over row group, etc.)
315 return border1.precedence >= border2.precedence ? border1 : border2;
318 CollapsedBorderValue RenderTableCell::collapsedLeftBorder(bool rtl) const
320 RenderTable* tableElt = table();
323 leftmostColumn = col() == 0;
325 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
326 leftmostColumn = effCol == tableElt->numEffCols() - 1;
329 // For border left, we need to check, in order of precedence:
330 // (1) Our left border.
331 CollapsedBorderValue result(&style()->borderLeft(), BCELL);
333 // (2) The right border of the cell to the left.
334 RenderTableCell* prevCell = rtl ? tableElt->cellAfter(this) : tableElt->cellBefore(this);
336 result = rtl ? compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL)) : compareBorders(CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL), result);
337 if (!result.exists())
339 } else if (leftmostColumn) {
340 // (3) Our row's left border.
341 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), BROW));
342 if (!result.exists())
345 // (4) Our row group's left border.
346 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderLeft(), BROWGROUP));
347 if (!result.exists())
351 // (5) Our column and column group's left borders.
354 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? colSpan() - 1 : 0), &startColEdge, &endColEdge);
355 if (colElt && (!rtl ? startColEdge : endColEdge)) {
356 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
357 if (!result.exists())
359 if (colElt->parent()->isTableCol() && (!rtl ? !colElt->previousSibling() : !colElt->nextSibling())) {
360 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderLeft(), BCOLGROUP));
361 if (!result.exists())
366 // (6) The right border of the column to the left.
367 if (!leftmostColumn) {
368 colElt = tableElt->colElement(col() + (rtl ? colSpan() : -1), &startColEdge, &endColEdge);
369 if (colElt && (!rtl ? endColEdge : startColEdge)) {
370 result = rtl ? compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL)) : compareBorders(CollapsedBorderValue(&colElt->style()->borderRight(), BCOL), result);
371 if (!result.exists())
375 // (7) The table's left border.
376 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderLeft(), BTABLE));
377 if (!result.exists())
384 CollapsedBorderValue RenderTableCell::collapsedRightBorder(bool rtl) const
386 RenderTable* tableElt = table();
387 bool rightmostColumn;
389 rightmostColumn = col() == 0;
391 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
392 rightmostColumn = effCol == tableElt->numEffCols() - 1;
395 // For border right, we need to check, in order of precedence:
396 // (1) Our right border.
397 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), BCELL);
399 // (2) The left border of the cell to the right.
400 if (!rightmostColumn) {
401 RenderTableCell* nextCell = rtl ? tableElt->cellBefore(this) : tableElt->cellAfter(this);
402 if (nextCell && nextCell->style()) {
403 result = rtl ? compareBorders(CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL), result) : compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL));
404 if (!result.exists())
408 // (3) Our row's right border.
409 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), BROW));
410 if (!result.exists())
413 // (4) Our row group's right border.
414 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderRight(), BROWGROUP));
415 if (!result.exists())
419 // (5) Our column and column group's right borders.
422 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? 0 : colSpan() - 1), &startColEdge, &endColEdge);
423 if (colElt && (!rtl ? endColEdge : startColEdge)) {
424 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
425 if (!result.exists())
427 if (colElt->parent()->isTableCol() && (!rtl ? !colElt->nextSibling() : !colElt->previousSibling())) {
428 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderRight(), BCOLGROUP));
429 if (!result.exists())
434 // (6) The left border of the column to the right.
435 if (!rightmostColumn) {
436 colElt = tableElt->colElement(col() + (rtl ? -1 : colSpan()), &startColEdge, &endColEdge);
437 if (colElt && (!rtl ? startColEdge : endColEdge)) {
438 result = rtl ? compareBorders(CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL), result) : compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
439 if (!result.exists())
443 // (7) The table's right border.
444 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), BTABLE));
445 if (!result.exists())
452 CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
454 // For border top, we need to check, in order of precedence:
455 // (1) Our top border.
456 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), BCELL);
458 RenderTableCell* prevCell = table()->cellAbove(this);
460 // (2) A previous cell's bottom border.
461 result = compareBorders(CollapsedBorderValue(&prevCell->style()->borderBottom(), BCELL), result);
462 if (!result.exists())
466 // (3) Our row's top border.
467 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), BROW));
468 if (!result.exists())
471 // (4) The previous row's bottom border.
473 RenderObject* prevRow = 0;
474 if (prevCell->section() == section())
475 prevRow = parent()->previousSibling();
477 prevRow = prevCell->section()->lastChild();
480 result = compareBorders(CollapsedBorderValue(&prevRow->style()->borderBottom(), BROW), result);
481 if (!result.exists())
486 // Now check row groups.
487 RenderTableSection* currSection = section();
489 // (5) Our row group's top border.
490 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
491 if (!result.exists())
494 // (6) Previous row group's bottom border.
495 currSection = table()->sectionAbove(currSection);
497 result = compareBorders(CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP), result);
498 if (!result.exists())
504 // (8) Our column and column group's top borders.
505 RenderTableCol* colElt = table()->colElement(col());
507 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), BCOL));
508 if (!result.exists())
510 if (colElt->parent()->isTableCol()) {
511 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderTop(), BCOLGROUP));
512 if (!result.exists())
517 // (9) The table's top border.
518 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderTop(), BTABLE));
519 if (!result.exists())
526 CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
528 // For border top, we need to check, in order of precedence:
529 // (1) Our bottom border.
530 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), BCELL);
532 RenderTableCell* nextCell = table()->cellBelow(this);
534 // (2) A following cell's top border.
535 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), BCELL));
536 if (!result.exists())
540 // (3) Our row's bottom border. (FIXME: Deal with rowspan!)
541 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), BROW));
542 if (!result.exists())
545 // (4) The next row's top border.
547 result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), BROW));
548 if (!result.exists())
552 // Now check row groups.
553 RenderTableSection* currSection = section();
554 if (row() + rowSpan() >= currSection->numRows()) {
555 // (5) Our row group's bottom border.
556 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
557 if (!result.exists())
560 // (6) Following row group's top border.
561 currSection = table()->sectionBelow(currSection);
563 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
564 if (!result.exists())
570 // (8) Our column and column group's bottom borders.
571 RenderTableCol* colElt = table()->colElement(col());
573 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), BCOL));
574 if (!result.exists()) return result;
575 if (colElt->parent()->isTableCol()) {
576 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderBottom(), BCOLGROUP));
577 if (!result.exists())
582 // (9) The table's bottom border.
583 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderBottom(), BTABLE));
584 if (!result.exists())
591 int RenderTableCell::borderLeft() const
593 return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlock::borderLeft();
596 int RenderTableCell::borderRight() const
598 return table()->collapseBorders() ? borderHalfRight(false) : RenderBlock::borderRight();
601 int RenderTableCell::borderTop() const
603 return table()->collapseBorders() ? borderHalfTop(false) : RenderBlock::borderTop();
606 int RenderTableCell::borderBottom() const
608 return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlock::borderBottom();
611 int RenderTableCell::borderHalfLeft(bool outer) const
613 CollapsedBorderValue border = collapsedLeftBorder(table()->style()->direction() == RTL);
615 return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
619 int RenderTableCell::borderHalfRight(bool outer) const
621 CollapsedBorderValue border = collapsedRightBorder(table()->style()->direction() == RTL);
623 return (border.width() + (outer ? 1 : 0)) / 2;
627 int RenderTableCell::borderHalfTop(bool outer) const
629 CollapsedBorderValue border = collapsedTopBorder();
631 return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
635 int RenderTableCell::borderHalfBottom(bool outer) const
637 CollapsedBorderValue border = collapsedBottomBorder();
639 return (border.width() + (outer ? 1 : 0)) / 2;
643 void RenderTableCell::paint(PaintInfo& paintInfo, int tx, int ty)
645 if (paintInfo.phase == PaintPhaseCollapsedTableBorders && style()->visibility() == VISIBLE) {
646 if (!shouldPaintWithinRoot(paintInfo))
651 int os = 2 * maximalOutlineSize(paintInfo.phase);
652 if (ty - table()->outerBorderTop() < paintInfo.rect.bottom() + os &&
653 ty + height() + table()->outerBorderBottom() > paintInfo.rect.y() - os)
654 paintCollapsedBorder(paintInfo.context, tx, ty, width(), height());
658 RenderBlock::paint(paintInfo, tx, ty);
661 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
670 struct CollapsedBorder {
671 CollapsedBorderValue borderValue;
681 class CollapsedBorders {
688 void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
689 int x1, int y1, int x2, int y2, EBorderStyle borderStyle)
691 if (borderValue.exists() && shouldPaint) {
692 m_borders[m_count].borderValue = borderValue;
693 m_borders[m_count].side = borderSide;
694 m_borders[m_count].shouldPaint = shouldPaint;
695 m_borders[m_count].x1 = x1;
696 m_borders[m_count].x2 = x2;
697 m_borders[m_count].y1 = y1;
698 m_borders[m_count].y2 = y2;
699 m_borders[m_count].style = borderStyle;
704 CollapsedBorder* nextBorder()
706 for (int i = 0; i < m_count; i++) {
707 if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
708 m_borders[i].shouldPaint = false;
709 return &m_borders[i];
716 CollapsedBorder m_borders[4];
720 static void addBorderStyle(RenderTableCell::CollapsedBorderStyles& borderStyles, CollapsedBorderValue borderValue)
722 if (!borderValue.exists())
724 size_t count = borderStyles.size();
725 for (size_t i = 0; i < count; ++i)
726 if (borderStyles[i] == borderValue)
728 borderStyles.append(borderValue);
731 void RenderTableCell::collectBorderStyles(CollapsedBorderStyles& borderStyles) const
733 bool rtl = table()->style()->direction() == RTL;
734 addBorderStyle(borderStyles, collapsedLeftBorder(rtl));
735 addBorderStyle(borderStyles, collapsedRightBorder(rtl));
736 addBorderStyle(borderStyles, collapsedTopBorder());
737 addBorderStyle(borderStyles, collapsedBottomBorder());
740 static int compareBorderStylesForQSort(const void* pa, const void* pb)
742 const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
743 const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
746 CollapsedBorderValue borderWithHigherPrecedence = compareBorders(*a, *b);
748 if (*a == borderWithHigherPrecedence) {
749 // klibc uses a combsort for quicksort and requires that two values always give the same answer
750 // regardless of comparison order. Unfortunately, compareBorders does not honor that requirement.
751 // Call compareBorders again with reversed parameters. If it returns the first value again then
752 // we can assume the values are equal. http://bugs.webkit.org/show_bug.cgi?id=13147
753 CollapsedBorderValue qSortHack = compareBorders(*b, *a);
759 if (*a == borderWithHigherPrecedence)
765 void RenderTableCell::sortBorderStyles(CollapsedBorderStyles& borderStyles)
767 qsort(borderStyles.data(), borderStyles.size(), sizeof(CollapsedBorderValue),
768 compareBorderStylesForQSort);
771 void RenderTableCell::paintCollapsedBorder(GraphicsContext* graphicsContext, int tx, int ty, int w, int h)
773 if (!table()->currentBorderStyle())
776 bool rtl = table()->style()->direction() == RTL;
777 CollapsedBorderValue leftVal = collapsedLeftBorder(rtl);
778 CollapsedBorderValue rightVal = collapsedRightBorder(rtl);
779 CollapsedBorderValue topVal = collapsedTopBorder();
780 CollapsedBorderValue bottomVal = collapsedBottomBorder();
782 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
783 int topWidth = topVal.width();
784 int bottomWidth = bottomVal.width();
785 int leftWidth = leftVal.width();
786 int rightWidth = rightVal.width();
790 w += leftWidth / 2 + (rightWidth + 1) / 2;
791 h += topWidth / 2 + (bottomWidth + 1) / 2;
793 EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
794 EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
795 EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
796 EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
798 bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent();
799 bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent();
800 bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent();
801 bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent();
803 // We never paint diagonals at the joins. We simply let the border with the highest
804 // precedence paint on top of borders with lower precedence.
805 CollapsedBorders borders;
806 borders.addBorder(topVal, BSTop, renderTop, tx, ty, tx + w, ty + topWidth, topStyle);
807 borders.addBorder(bottomVal, BSBottom, renderBottom, tx, ty + h - bottomWidth, tx + w, ty + h, bottomStyle);
808 borders.addBorder(leftVal, BSLeft, renderLeft, tx, ty, tx + leftWidth, ty + h, leftStyle);
809 borders.addBorder(rightVal, BSRight, renderRight, tx + w - rightWidth, ty, tx + w, ty + h, rightStyle);
811 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
812 if (border->borderValue == *table()->currentBorderStyle())
813 drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
814 border->borderValue.color(), style()->color(), border->style, 0, 0);
818 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, int tx, int ty, RenderObject* backgroundObject)
820 if (!shouldPaintWithinRoot(paintInfo))
823 if (!backgroundObject)
826 if (style()->visibility() != VISIBLE)
829 RenderTable* tableElt = table();
830 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
833 if (backgroundObject != this) {
841 Color c = backgroundObject->style()->backgroundColor();
842 const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers();
844 if (bgLayer->hasImage() || c.isValid()) {
845 // We have to clip here because the background would paint
846 // on top of the borders otherwise. This only matters for cells and rows.
847 bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
849 IntRect clipRect(tx + borderLeft(), ty + borderTop(),
850 w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
851 paintInfo.context->save();
852 paintInfo.context->clip(clipRect);
854 paintFillLayers(paintInfo, c, bgLayer, tx, ty, w, h, CompositeSourceOver, backgroundObject);
856 paintInfo.context->restore();
860 void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
862 if (!shouldPaintWithinRoot(paintInfo))
865 RenderTable* tableElt = table();
866 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
872 if (style()->boxShadow())
873 paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Normal);
875 // Paint our cell background.
876 paintBackgroundsBehindCell(paintInfo, tx, ty, this);
877 if (style()->boxShadow())
878 paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Inset);
880 if (!style()->hasBorder() || tableElt->collapseBorders())
883 paintBorder(paintInfo.context, tx, ty, w, h, style());
886 void RenderTableCell::paintMask(PaintInfo& paintInfo, int tx, int ty)
888 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
891 RenderTable* tableElt = table();
892 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
898 paintMaskImages(paintInfo, tx, ty, w, h);
901 } // namespace WebCore