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 IntRect RenderTableCell::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
173 // If the table grid is dirty, we cannot get reliable information about adjoining cells,
174 // so we ignore outside borders. This should not be a problem because it means that
175 // the table is going to recalculate the grid, relayout and repaint its current rect, which
176 // includes any outside borders of this cell.
177 if (!table()->collapseBorders() || table()->needsSectionRecalc())
178 return RenderBlock::clippedOverflowRectForRepaint(repaintContainer);
180 bool rtl = table()->style()->direction() == RTL;
181 int outlineSize = style()->outlineSize();
182 int left = max(borderHalfLeft(true), outlineSize);
183 int right = max(borderHalfRight(true), outlineSize);
184 int top = max(borderHalfTop(true), outlineSize);
185 int bottom = max(borderHalfBottom(true), outlineSize);
186 if ((left && !rtl) || (right && rtl)) {
187 if (RenderTableCell* before = table()->cellBefore(this)) {
188 top = max(top, before->borderHalfTop(true));
189 bottom = max(bottom, before->borderHalfBottom(true));
192 if ((left && rtl) || (right && !rtl)) {
193 if (RenderTableCell* after = table()->cellAfter(this)) {
194 top = max(top, after->borderHalfTop(true));
195 bottom = max(bottom, after->borderHalfBottom(true));
199 if (RenderTableCell* above = table()->cellAbove(this)) {
200 left = max(left, above->borderHalfLeft(true));
201 right = max(right, above->borderHalfRight(true));
205 if (RenderTableCell* below = table()->cellBelow(this)) {
206 left = max(left, below->borderHalfLeft(true));
207 right = max(right, below->borderHalfRight(true));
210 left = max(left, -overflowLeft(false));
211 top = max(top, -overflowTop(false));
212 IntRect r(-left, - top, left + max(width() + right, overflowWidth(false)), top + max(height() + bottom, overflowHeight(false)));
214 if (RenderView* v = view()) {
215 // FIXME: layoutDelta needs to be applied in parts before/after transforms and
216 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
217 r.move(v->layoutDelta());
219 computeRectForRepaint(repaintContainer, r);
223 void RenderTableCell::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& r, bool fixed)
225 if (repaintContainer == this)
228 RenderView* v = view();
229 if ((!v || !v->layoutStateEnabled()) && parent())
230 r.move(-parentBox()->x(), -parentBox()->y()); // Rows are in the same coordinate space, so don't add their offset in.
231 RenderBlock::computeRectForRepaint(repaintContainer, r, fixed);
234 void RenderTableCell::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState) const
236 if (repaintContainer == this)
239 RenderView* v = view();
240 if ((!v || !v->layoutStateEnabled()) && parent()) {
241 // Rows are in the same coordinate space, so don't add their offset in.
242 // FIXME: this is wrong with transforms
243 transformState.move(-parentBox()->x(), -parentBox()->y());
245 RenderBlock::mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
248 void RenderTableCell::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
250 RenderBlock::mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
252 // Rows are in the same coordinate space, so add their offset back in.
253 // FIXME: this is wrong with transforms
254 transformState.move(parentBox()->x(), parentBox()->y());
258 int RenderTableCell::baselinePosition(bool firstLine, bool isRootLineBox) const
261 return RenderBox::baselinePosition(firstLine, isRootLineBox);
263 // <http://www.w3.org/TR/2007/CR-CSS21-20070719/tables.html#height-layout>: The baseline of a cell is the baseline of
264 // the first in-flow line box in the cell, or the first in-flow table-row in the cell, whichever comes first. If there
265 // is no such line box or table-row, the baseline is the bottom of content edge of the cell box.
266 int firstLineBaseline = firstLineBoxBaseline();
267 if (firstLineBaseline != -1)
268 return firstLineBaseline;
269 return paddingTop() + borderTop() + contentHeight();
272 void RenderTableCell::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
274 if (parent() && section() && style() && style()->height() != newStyle->height())
275 section()->setNeedsCellRecalc();
277 ASSERT(newStyle->display() == TABLE_CELL);
279 RenderBlock::styleWillChange(diff, newStyle);
282 void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
284 RenderBlock::styleDidChange(diff, oldStyle);
285 setHasBoxDecorations(true);
288 // The following rules apply for resolving conflicts and figuring out which border
290 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
291 // borders. Any border with this value suppresses all borders at this location.
292 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
293 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
294 // the default value for the border style.)
295 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
296 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
297 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
298 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
299 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
300 // is used when two elements of the same type disagree.
301 static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
303 // Sanity check the values passed in. If either is null, return the other.
304 if (!border2.exists())
306 if (!border1.exists())
310 if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
311 return CollapsedBorderValue(); // No border should exist at this location.
313 // Rule #2 above. A style of 'none' has lowest priority and always loses to any other border.
314 if (border2.style() == BNONE)
316 if (border1.style() == BNONE)
319 // The first part of rule #3 above. Wider borders win.
320 if (border1.width() != border2.width())
321 return border1.width() > border2.width() ? border1 : border2;
323 // The borders have equal width. Sort by border style.
324 if (border1.style() != border2.style())
325 return border1.style() > border2.style() ? border1 : border2;
327 // The border have the same width and style. Rely on precedence (cell over row over row group, etc.)
328 return border1.precedence >= border2.precedence ? border1 : border2;
331 CollapsedBorderValue RenderTableCell::collapsedLeftBorder(bool rtl) const
333 RenderTable* tableElt = table();
336 leftmostColumn = col() == 0;
338 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
339 leftmostColumn = effCol == tableElt->numEffCols() - 1;
342 // For border left, we need to check, in order of precedence:
343 // (1) Our left border.
344 CollapsedBorderValue result(&style()->borderLeft(), BCELL);
346 // (2) The right border of the cell to the left.
347 RenderTableCell* prevCell = rtl ? tableElt->cellAfter(this) : tableElt->cellBefore(this);
349 result = rtl ? compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL)) : compareBorders(CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL), result);
350 if (!result.exists())
352 } else if (leftmostColumn) {
353 // (3) Our row's left border.
354 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), BROW));
355 if (!result.exists())
358 // (4) Our row group's left border.
359 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderLeft(), BROWGROUP));
360 if (!result.exists())
364 // (5) Our column and column group's left borders.
367 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? colSpan() - 1 : 0), &startColEdge, &endColEdge);
368 if (colElt && (!rtl ? startColEdge : endColEdge)) {
369 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
370 if (!result.exists())
372 if (colElt->parent()->isTableCol() && (!rtl ? !colElt->previousSibling() : !colElt->nextSibling())) {
373 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderLeft(), BCOLGROUP));
374 if (!result.exists())
379 // (6) The right border of the column to the left.
380 if (!leftmostColumn) {
381 colElt = tableElt->colElement(col() + (rtl ? colSpan() : -1), &startColEdge, &endColEdge);
382 if (colElt && (!rtl ? endColEdge : startColEdge)) {
383 result = rtl ? compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL)) : compareBorders(CollapsedBorderValue(&colElt->style()->borderRight(), BCOL), result);
384 if (!result.exists())
388 // (7) The table's left border.
389 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderLeft(), BTABLE));
390 if (!result.exists())
397 CollapsedBorderValue RenderTableCell::collapsedRightBorder(bool rtl) const
399 RenderTable* tableElt = table();
400 bool rightmostColumn;
402 rightmostColumn = col() == 0;
404 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
405 rightmostColumn = effCol == tableElt->numEffCols() - 1;
408 // For border right, we need to check, in order of precedence:
409 // (1) Our right border.
410 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), BCELL);
412 // (2) The left border of the cell to the right.
413 if (!rightmostColumn) {
414 RenderTableCell* nextCell = rtl ? tableElt->cellBefore(this) : tableElt->cellAfter(this);
415 if (nextCell && nextCell->style()) {
416 result = rtl ? compareBorders(CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL), result) : compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL));
417 if (!result.exists())
421 // (3) Our row's right border.
422 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), BROW));
423 if (!result.exists())
426 // (4) Our row group's right border.
427 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderRight(), BROWGROUP));
428 if (!result.exists())
432 // (5) Our column and column group's right borders.
435 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? 0 : colSpan() - 1), &startColEdge, &endColEdge);
436 if (colElt && (!rtl ? endColEdge : startColEdge)) {
437 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
438 if (!result.exists())
440 if (colElt->parent()->isTableCol() && (!rtl ? !colElt->nextSibling() : !colElt->previousSibling())) {
441 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderRight(), BCOLGROUP));
442 if (!result.exists())
447 // (6) The left border of the column to the right.
448 if (!rightmostColumn) {
449 colElt = tableElt->colElement(col() + (rtl ? -1 : colSpan()), &startColEdge, &endColEdge);
450 if (colElt && (!rtl ? startColEdge : endColEdge)) {
451 result = rtl ? compareBorders(CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL), result) : compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
452 if (!result.exists())
456 // (7) The table's right border.
457 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), BTABLE));
458 if (!result.exists())
465 CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
467 // For border top, we need to check, in order of precedence:
468 // (1) Our top border.
469 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), BCELL);
471 RenderTableCell* prevCell = table()->cellAbove(this);
473 // (2) A previous cell's bottom border.
474 result = compareBorders(CollapsedBorderValue(&prevCell->style()->borderBottom(), BCELL), result);
475 if (!result.exists())
479 // (3) Our row's top border.
480 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), BROW));
481 if (!result.exists())
484 // (4) The previous row's bottom border.
486 RenderObject* prevRow = 0;
487 if (prevCell->section() == section())
488 prevRow = parent()->previousSibling();
490 prevRow = prevCell->section()->lastChild();
493 result = compareBorders(CollapsedBorderValue(&prevRow->style()->borderBottom(), BROW), result);
494 if (!result.exists())
499 // Now check row groups.
500 RenderTableSection* currSection = section();
502 // (5) Our row group's top border.
503 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
504 if (!result.exists())
507 // (6) Previous row group's bottom border.
508 currSection = table()->sectionAbove(currSection);
510 result = compareBorders(CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP), result);
511 if (!result.exists())
517 // (8) Our column and column group's top borders.
518 RenderTableCol* colElt = table()->colElement(col());
520 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), BCOL));
521 if (!result.exists())
523 if (colElt->parent()->isTableCol()) {
524 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderTop(), BCOLGROUP));
525 if (!result.exists())
530 // (9) The table's top border.
531 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderTop(), BTABLE));
532 if (!result.exists())
539 CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
541 // For border top, we need to check, in order of precedence:
542 // (1) Our bottom border.
543 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), BCELL);
545 RenderTableCell* nextCell = table()->cellBelow(this);
547 // (2) A following cell's top border.
548 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), BCELL));
549 if (!result.exists())
553 // (3) Our row's bottom border. (FIXME: Deal with rowspan!)
554 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), BROW));
555 if (!result.exists())
558 // (4) The next row's top border.
560 result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), BROW));
561 if (!result.exists())
565 // Now check row groups.
566 RenderTableSection* currSection = section();
567 if (row() + rowSpan() >= currSection->numRows()) {
568 // (5) Our row group's bottom border.
569 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
570 if (!result.exists())
573 // (6) Following row group's top border.
574 currSection = table()->sectionBelow(currSection);
576 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
577 if (!result.exists())
583 // (8) Our column and column group's bottom borders.
584 RenderTableCol* colElt = table()->colElement(col());
586 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), BCOL));
587 if (!result.exists()) return result;
588 if (colElt->parent()->isTableCol()) {
589 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderBottom(), BCOLGROUP));
590 if (!result.exists())
595 // (9) The table's bottom border.
596 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderBottom(), BTABLE));
597 if (!result.exists())
604 int RenderTableCell::borderLeft() const
606 return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlock::borderLeft();
609 int RenderTableCell::borderRight() const
611 return table()->collapseBorders() ? borderHalfRight(false) : RenderBlock::borderRight();
614 int RenderTableCell::borderTop() const
616 return table()->collapseBorders() ? borderHalfTop(false) : RenderBlock::borderTop();
619 int RenderTableCell::borderBottom() const
621 return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlock::borderBottom();
624 int RenderTableCell::borderHalfLeft(bool outer) const
626 CollapsedBorderValue border = collapsedLeftBorder(table()->style()->direction() == RTL);
628 return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
632 int RenderTableCell::borderHalfRight(bool outer) const
634 CollapsedBorderValue border = collapsedRightBorder(table()->style()->direction() == RTL);
636 return (border.width() + (outer ? 1 : 0)) / 2;
640 int RenderTableCell::borderHalfTop(bool outer) const
642 CollapsedBorderValue border = collapsedTopBorder();
644 return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
648 int RenderTableCell::borderHalfBottom(bool outer) const
650 CollapsedBorderValue border = collapsedBottomBorder();
652 return (border.width() + (outer ? 1 : 0)) / 2;
656 void RenderTableCell::paint(PaintInfo& paintInfo, int tx, int ty)
658 if (paintInfo.phase == PaintPhaseCollapsedTableBorders && style()->visibility() == VISIBLE) {
661 int os = 2 * maximalOutlineSize(paintInfo.phase);
662 if (ty - table()->outerBorderTop() < paintInfo.rect.bottom() + os &&
663 ty + height() + table()->outerBorderBottom() > paintInfo.rect.y() - os)
664 paintCollapsedBorder(paintInfo.context, tx, ty, width(), height());
668 RenderBlock::paint(paintInfo, tx, ty);
671 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
680 struct CollapsedBorder {
681 CollapsedBorderValue borderValue;
691 class CollapsedBorders {
698 void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
699 int x1, int y1, int x2, int y2, EBorderStyle borderStyle)
701 if (borderValue.exists() && shouldPaint) {
702 m_borders[m_count].borderValue = borderValue;
703 m_borders[m_count].side = borderSide;
704 m_borders[m_count].shouldPaint = shouldPaint;
705 m_borders[m_count].x1 = x1;
706 m_borders[m_count].x2 = x2;
707 m_borders[m_count].y1 = y1;
708 m_borders[m_count].y2 = y2;
709 m_borders[m_count].style = borderStyle;
714 CollapsedBorder* nextBorder()
716 for (int i = 0; i < m_count; i++) {
717 if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
718 m_borders[i].shouldPaint = false;
719 return &m_borders[i];
726 CollapsedBorder m_borders[4];
730 static void addBorderStyle(RenderTableCell::CollapsedBorderStyles& borderStyles, CollapsedBorderValue borderValue)
732 if (!borderValue.exists())
734 size_t count = borderStyles.size();
735 for (size_t i = 0; i < count; ++i)
736 if (borderStyles[i] == borderValue)
738 borderStyles.append(borderValue);
741 void RenderTableCell::collectBorderStyles(CollapsedBorderStyles& borderStyles) const
743 bool rtl = table()->style()->direction() == RTL;
744 addBorderStyle(borderStyles, collapsedLeftBorder(rtl));
745 addBorderStyle(borderStyles, collapsedRightBorder(rtl));
746 addBorderStyle(borderStyles, collapsedTopBorder());
747 addBorderStyle(borderStyles, collapsedBottomBorder());
750 static int compareBorderStylesForQSort(const void* pa, const void* pb)
752 const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
753 const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
756 CollapsedBorderValue borderWithHigherPrecedence = compareBorders(*a, *b);
758 if (*a == borderWithHigherPrecedence) {
759 // klibc uses a combsort for quicksort and requires that two values always give the same answer
760 // regardless of comparison order. Unfortunately, compareBorders does not honor that requirement.
761 // Call compareBorders again with reversed parameters. If it returns the first value again then
762 // we can assume the values are equal. http://bugs.webkit.org/show_bug.cgi?id=13147
763 CollapsedBorderValue qSortHack = compareBorders(*b, *a);
769 if (*a == borderWithHigherPrecedence)
775 void RenderTableCell::sortBorderStyles(CollapsedBorderStyles& borderStyles)
777 qsort(borderStyles.data(), borderStyles.size(), sizeof(CollapsedBorderValue),
778 compareBorderStylesForQSort);
781 void RenderTableCell::paintCollapsedBorder(GraphicsContext* graphicsContext, int tx, int ty, int w, int h)
783 if (!table()->currentBorderStyle())
786 bool rtl = table()->style()->direction() == RTL;
787 CollapsedBorderValue leftVal = collapsedLeftBorder(rtl);
788 CollapsedBorderValue rightVal = collapsedRightBorder(rtl);
789 CollapsedBorderValue topVal = collapsedTopBorder();
790 CollapsedBorderValue bottomVal = collapsedBottomBorder();
792 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
793 int topWidth = topVal.width();
794 int bottomWidth = bottomVal.width();
795 int leftWidth = leftVal.width();
796 int rightWidth = rightVal.width();
800 w += leftWidth / 2 + (rightWidth + 1) / 2;
801 h += topWidth / 2 + (bottomWidth + 1) / 2;
803 EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
804 EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
805 EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
806 EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
808 bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent();
809 bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent();
810 bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent();
811 bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent();
813 // We never paint diagonals at the joins. We simply let the border with the highest
814 // precedence paint on top of borders with lower precedence.
815 CollapsedBorders borders;
816 borders.addBorder(topVal, BSTop, renderTop, tx, ty, tx + w, ty + topWidth, topStyle);
817 borders.addBorder(bottomVal, BSBottom, renderBottom, tx, ty + h - bottomWidth, tx + w, ty + h, bottomStyle);
818 borders.addBorder(leftVal, BSLeft, renderLeft, tx, ty, tx + leftWidth, ty + h, leftStyle);
819 borders.addBorder(rightVal, BSRight, renderRight, tx + w - rightWidth, ty, tx + w, ty + h, rightStyle);
821 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
822 if (border->borderValue == *table()->currentBorderStyle())
823 drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
824 border->borderValue.color(), style()->color(), border->style, 0, 0);
828 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, int tx, int ty, RenderObject* backgroundObject)
830 if (!backgroundObject)
833 if (style()->visibility() != VISIBLE)
836 RenderTable* tableElt = table();
837 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
840 if (backgroundObject != this) {
848 Color c = backgroundObject->style()->backgroundColor();
849 const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers();
851 if (bgLayer->hasImage() || c.isValid()) {
852 // We have to clip here because the background would paint
853 // on top of the borders otherwise. This only matters for cells and rows.
854 bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
856 IntRect clipRect(tx + borderLeft(), ty + borderTop(),
857 w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
858 paintInfo.context->save();
859 paintInfo.context->clip(clipRect);
861 paintFillLayers(paintInfo, c, bgLayer, tx, ty, w, h);
863 paintInfo.context->restore();
867 void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
869 RenderTable* tableElt = table();
870 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
876 if (style()->boxShadow())
877 paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Normal);
879 // Paint our cell background.
880 paintBackgroundsBehindCell(paintInfo, tx, ty, this);
881 if (style()->boxShadow())
882 paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Inset);
884 if (!style()->hasBorder() || tableElt->collapseBorders())
887 paintBorder(paintInfo.context, tx, ty, w, h, style());
890 void RenderTableCell::paintMask(PaintInfo& paintInfo, int tx, int ty)
892 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
895 RenderTable* tableElt = table();
896 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
902 paintMaskImages(paintInfo, tx, ty, w, h);
905 } // namespace WebCore