1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "core/paint/TableCellPainter.h"
#include "core/paint/BlockPainter.h"
#include "core/paint/BoxPainter.h"
#include "core/paint/RenderDrawingRecorder.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/RenderTableCell.h"
namespace blink {
inline CollapsedBorderValue TableCellPainter::cachedCollapsedLeftBorder(const RenderStyle* styleForCellFlow) const
{
if (styleForCellFlow->isHorizontalWritingMode()) {
return styleForCellFlow->isLeftToRightDirection() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSStart)
: m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSEnd);
}
return styleForCellFlow->isFlippedBlocksWritingMode() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSAfter)
: m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSBefore);
}
inline CollapsedBorderValue TableCellPainter::cachedCollapsedRightBorder(const RenderStyle* styleForCellFlow) const
{
if (styleForCellFlow->isHorizontalWritingMode()) {
return styleForCellFlow->isLeftToRightDirection() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSEnd)
: m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSStart);
}
return styleForCellFlow->isFlippedBlocksWritingMode() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSBefore)
: m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSAfter);
}
inline CollapsedBorderValue TableCellPainter::cachedCollapsedTopBorder(const RenderStyle* styleForCellFlow) const
{
if (styleForCellFlow->isHorizontalWritingMode())
return styleForCellFlow->isFlippedBlocksWritingMode() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSAfter) : m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSBefore);
return styleForCellFlow->isLeftToRightDirection() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSStart) : m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSEnd);
}
inline CollapsedBorderValue TableCellPainter::cachedCollapsedBottomBorder(const RenderStyle* styleForCellFlow) const
{
if (styleForCellFlow->isHorizontalWritingMode()) {
return styleForCellFlow->isFlippedBlocksWritingMode() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSBefore)
: m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSAfter);
}
return styleForCellFlow->isLeftToRightDirection() ? m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSEnd)
: m_renderTableCell.section()->cachedCollapsedBorder(&m_renderTableCell, CBSStart);
}
void TableCellPainter::paint(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
ASSERT(paintInfo.phase != PaintPhaseCollapsedTableBorders);
BlockPainter(m_renderTableCell).paint(paintInfo, paintOffset);
}
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 (unsigned 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];
unsigned m_count;
};
static EBorderStyle collapsedBorderStyle(EBorderStyle style)
{
if (style == OUTSET)
return GROOVE;
if (style == INSET)
return RIDGE;
return style;
}
void TableCellPainter::paintCollapsedBorders(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
ASSERT(paintInfo.phase == PaintPhaseCollapsedTableBorders);
if (!paintInfo.shouldPaintWithinRoot(&m_renderTableCell) || m_renderTableCell.style()->visibility() != VISIBLE)
return;
LayoutRect paintRect = paintBounds(paintOffset, AddOffsetFromParent);
if (paintRect.y() - m_renderTableCell.table()->outerBorderTop() >= paintInfo.rect.maxY())
return;
if (paintRect.maxY() + m_renderTableCell.table()->outerBorderBottom() <= paintInfo.rect.y())
return;
if (!m_renderTableCell.table()->currentBorderValue())
return;
RenderDrawingRecorder recorder(paintInfo.context, m_renderTableCell, paintInfo.phase, paintRect);
if (recorder.canUseCachedDrawing())
return;
const RenderStyle* styleForCellFlow = m_renderTableCell.styleForCellFlow();
CollapsedBorderValue leftVal = cachedCollapsedLeftBorder(styleForCellFlow);
CollapsedBorderValue rightVal = cachedCollapsedRightBorder(styleForCellFlow);
CollapsedBorderValue topVal = cachedCollapsedTopBorder(styleForCellFlow);
CollapsedBorderValue bottomVal = cachedCollapsedBottomBorder(styleForCellFlow);
// Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
int topWidth = topVal.width();
int bottomWidth = bottomVal.width();
int leftWidth = leftVal.width();
int rightWidth = rightVal.width();
IntRect borderRect = pixelSnappedIntRect(paintRect.x() - leftWidth / 2,
paintRect.y() - topWidth / 2,
paintRect.width() + leftWidth / 2 + (rightWidth + 1) / 2,
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();
// We never paint diagonals at the joins. We simply let the border with the highest
// precedence paint on top of borders with lower precedence.
CollapsedBorders borders;
borders.addBorder(topVal, BSTop, renderTop, borderRect.x(), borderRect.y(), borderRect.maxX(), borderRect.y() + topWidth, topStyle);
borders.addBorder(bottomVal, BSBottom, renderBottom, borderRect.x(), borderRect.maxY() - bottomWidth, borderRect.maxX(), borderRect.maxY(), bottomStyle);
borders.addBorder(leftVal, BSLeft, renderLeft, borderRect.x(), borderRect.y(), borderRect.x() + leftWidth, borderRect.maxY(), leftStyle);
borders.addBorder(rightVal, BSRight, renderRight, borderRect.maxX() - rightWidth, borderRect.y(), borderRect.maxX(), borderRect.maxY(), rightStyle);
GraphicsContext* graphicsContext = paintInfo.context;
bool antialias = BoxPainter::shouldAntialiasLines(graphicsContext);
for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
if (border->borderValue.isSameIgnoringColor(*m_renderTableCell.table()->currentBorderValue())) {
ObjectPainter::drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
border->borderValue.color().resolve(m_renderTableCell.resolveColor(CSSPropertyColor)), border->style, 0, 0, antialias);
}
}
}
void TableCellPainter::paintBackgroundsBehindCell(const PaintInfo& paintInfo, const LayoutPoint& paintOffset, RenderObject* backgroundObject)
{
if (!paintInfo.shouldPaintWithinRoot(&m_renderTableCell))
return;
if (!backgroundObject)
return;
if (m_renderTableCell.style()->visibility() != VISIBLE)
return;
RenderTable* tableElt = m_renderTableCell.table();
if (!tableElt->collapseBorders() && m_renderTableCell.style()->emptyCells() == HIDE && !m_renderTableCell.firstChild())
return;
Color c = backgroundObject->resolveColor(CSSPropertyBackgroundColor);
const FillLayer& bgLayer = backgroundObject->style()->backgroundLayers();
LayoutRect paintRect = paintBounds(paintOffset, backgroundObject != &m_renderTableCell ? AddOffsetFromParent : DoNotAddOffsetFromParent);
if (bgLayer.hasImage() || c.alpha()) {
// We have to clip here because the background would paint
// on top of the borders otherwise. This only matters for cells and rows.
bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == &m_renderTableCell || backgroundObject == m_renderTableCell.parent()) && tableElt->collapseBorders();
GraphicsContextStateSaver stateSaver(*paintInfo.context, shouldClip);
if (shouldClip) {
LayoutRect clipRect(paintRect.location(), m_renderTableCell.size());
clipRect.expand(m_renderTableCell.borderInsets());
paintInfo.context->clip(clipRect);
}
BoxPainter(m_renderTableCell).paintFillLayers(paintInfo, c, bgLayer, paintRect, BackgroundBleedNone, CompositeSourceOver, backgroundObject);
}
}
void TableCellPainter::paintBoxDecorationBackground(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!paintInfo.shouldPaintWithinRoot(&m_renderTableCell))
return;
RenderTable* tableElt = m_renderTableCell.table();
if (!tableElt->collapseBorders() && m_renderTableCell.style()->emptyCells() == HIDE && !m_renderTableCell.firstChild())
return;
LayoutRect paintRect = paintBounds(paintOffset, DoNotAddOffsetFromParent);
RenderDrawingRecorder recorder(paintInfo.context, m_renderTableCell, paintInfo.phase, pixelSnappedIntRect(paintRect));
if (recorder.canUseCachedDrawing())
return;
BoxPainter::paintBoxShadow(paintInfo, paintRect, m_renderTableCell.style(), Normal);
// Paint our cell background.
paintBackgroundsBehindCell(paintInfo, paintOffset, &m_renderTableCell);
BoxPainter::paintBoxShadow(paintInfo, paintRect, m_renderTableCell.style(), Inset);
if (!m_renderTableCell.style()->hasBorder() || tableElt->collapseBorders())
return;
BoxPainter::paintBorder(m_renderTableCell, paintInfo, paintRect, m_renderTableCell.style());
}
void TableCellPainter::paintMask(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (m_renderTableCell.style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
return;
RenderTable* tableElt = m_renderTableCell.table();
if (!tableElt->collapseBorders() && m_renderTableCell.style()->emptyCells() == HIDE && !m_renderTableCell.firstChild())
return;
BoxPainter(m_renderTableCell).paintMaskImages(paintInfo, paintBounds(paintOffset, DoNotAddOffsetFromParent));
}
LayoutRect TableCellPainter::paintBounds(const LayoutPoint& paintOffset, PaintBoundOffsetBehavior paintBoundOffsetBehavior)
{
LayoutPoint adjustedPaintOffset = paintOffset;
if (paintBoundOffsetBehavior == AddOffsetFromParent)
adjustedPaintOffset.moveBy(m_renderTableCell.location());
return LayoutRect(adjustedPaintOffset, LayoutSize(m_renderTableCell.pixelSnappedSize()));
}
} // namespace blink
|