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, 2009, 2013 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.
25 #ifndef RenderTableSection_h
26 #define RenderTableSection_h
28 #include "core/rendering/RenderTable.h"
29 #include "wtf/Vector.h"
33 // This variable is used to balance the memory consumption vs the paint invalidation time on big tables.
34 const float gMaxAllowedOverflowingCellRatioForFastPaintPath = 0.1f;
36 enum CollapsedBorderSide {
43 // Helper class for paintObject.
46 CellSpan(unsigned start, unsigned end)
52 unsigned start() const { return m_start; }
53 unsigned end() const { return m_end; }
55 void decreaseStart() { --m_start; }
56 void increaseEnd() { ++m_end; }
63 class RenderTableCell;
66 class RenderTableSection final : public RenderBox {
68 RenderTableSection(Element*);
69 virtual ~RenderTableSection();
70 virtual void trace(Visitor*) override;
72 RenderTableRow* firstRow() const;
73 RenderTableRow* lastRow() const;
75 const RenderObjectChildList* children() const { return &m_children; }
76 RenderObjectChildList* children() { return &m_children; }
78 virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0) override;
80 virtual int firstLineBoxBaseline() const override;
82 void addCell(RenderTableCell*, RenderTableRow* row);
84 int calcRowLogicalHeight();
86 void computeOverflowFromCells();
88 RenderTable* table() const { return toRenderTable(parent()); }
90 typedef WillBeHeapVector<RawPtrWillBeMember<RenderTableCell>, 2> SpanningRenderTableCells;
93 ALLOW_ONLY_INLINE_ALLOCATION();
95 WillBeHeapVector<RawPtrWillBeMember<RenderTableCell>, 1> cells;
96 bool inColSpan; // true for columns after the first in a colspan
102 void trace(Visitor*);
104 RenderTableCell* primaryCell()
106 return hasCells() ? cells[cells.size() - 1].get() : 0;
109 const RenderTableCell* primaryCell() const
111 return hasCells() ? cells[cells.size() - 1].get() : 0;
114 bool hasCells() const { return cells.size() > 0; }
117 typedef WillBeHeapVector<CellStruct> Row;
120 ALLOW_ONLY_INLINE_ALLOCATION();
123 : rowRenderer(nullptr)
127 void trace(Visitor*);
130 RawPtrWillBeMember<RenderTableRow> rowRenderer;
132 Length logicalHeight;
135 struct SpanningRowsHeight {
136 WTF_MAKE_NONCOPYABLE(SpanningRowsHeight);
141 , spanningCellHeightIgnoringBorderSpacing(0)
142 , isAnyRowWithOnlySpanningCells(false)
146 Vector<int> rowHeight;
148 int spanningCellHeightIgnoringBorderSpacing;
149 bool isAnyRowWithOnlySpanningCells;
152 const BorderValue& borderAdjoiningTableStart() const
154 if (hasSameDirectionAs(table()))
155 return style()->borderStart();
157 return style()->borderEnd();
160 const BorderValue& borderAdjoiningTableEnd() const
162 if (hasSameDirectionAs(table()))
163 return style()->borderEnd();
165 return style()->borderStart();
168 const BorderValue& borderAdjoiningStartCell(const RenderTableCell*) const;
169 const BorderValue& borderAdjoiningEndCell(const RenderTableCell*) const;
171 const RenderTableCell* firstRowCellAdjoiningTableStart() const;
172 const RenderTableCell* firstRowCellAdjoiningTableEnd() const;
174 CellStruct& cellAt(unsigned row, unsigned col) { return m_grid[row].row[col]; }
175 const CellStruct& cellAt(unsigned row, unsigned col) const { return m_grid[row].row[col]; }
176 RenderTableCell* primaryCellAt(unsigned row, unsigned col)
178 CellStruct& c = m_grid[row].row[col];
179 return c.primaryCell();
182 RenderTableRow* rowRendererAt(unsigned row) const { return m_grid[row].rowRenderer; }
184 void appendColumn(unsigned pos);
185 void splitColumn(unsigned pos, unsigned first);
187 enum BlockBorderSide { BorderBefore, BorderAfter };
188 int calcBlockDirectionOuterBorder(BlockBorderSide) const;
189 enum InlineBorderSide { BorderStart, BorderEnd };
190 int calcInlineDirectionOuterBorder(InlineBorderSide) const;
191 void recalcOuterBorder();
193 int outerBorderBefore() const { return m_outerBorderBefore; }
194 int outerBorderAfter() const { return m_outerBorderAfter; }
195 int outerBorderStart() const { return m_outerBorderStart; }
196 int outerBorderEnd() const { return m_outerBorderEnd; }
198 unsigned numRows() const { return m_grid.size(); }
199 unsigned numColumns() const;
201 void recalcCellsIfNeeded()
203 if (m_needsCellRecalc)
207 bool needsCellRecalc() const { return m_needsCellRecalc; }
208 void setNeedsCellRecalc();
210 LayoutUnit rowBaseline(unsigned row) { return m_grid[row].baseline; }
212 void rowLogicalHeightChanged(RenderTableRow*);
214 void removeCachedCollapsedBorders(const RenderTableCell*);
215 void setCachedCollapsedBorder(const RenderTableCell*, CollapsedBorderSide, CollapsedBorderValue);
216 CollapsedBorderValue& cachedCollapsedBorder(const RenderTableCell*, CollapsedBorderSide);
218 // distributeExtraLogicalHeightToRows methods return the *consumed* extra logical height.
219 // FIXME: We may want to introduce a structure holding the in-flux layout information.
220 int distributeExtraLogicalHeightToRows(int extraLogicalHeight);
222 static RenderTableSection* createAnonymousWithParentRenderer(const RenderObject*);
223 virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const override
225 return createAnonymousWithParentRenderer(parent);
228 virtual void paint(PaintInfo&, const LayoutPoint&) override;
230 // Flip the rect so it aligns with the coordinates used by the rowPos and columnPos vectors.
231 LayoutRect logicalRectForWritingModeAndDirection(const LayoutRect&) const;
233 CellSpan dirtiedRows(const LayoutRect& paintInvalidationRect) const;
234 CellSpan dirtiedColumns(const LayoutRect& paintInvalidationRect) const;
235 WillBeHeapHashSet<RawPtrWillBeMember<RenderTableCell> >& overflowingCells() { return m_overflowingCells; }
236 bool hasMultipleCellLevels() { return m_hasMultipleCellLevels; }
239 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
240 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
243 virtual RenderObjectChildList* virtualChildren() override { return children(); }
244 virtual const RenderObjectChildList* virtualChildren() const override { return children(); }
246 virtual const char* renderName() const override { return (isAnonymous() || isPseudoElement()) ? "RenderTableSection (anonymous)" : "RenderTableSection"; }
248 virtual bool isOfType(RenderObjectType type) const override { return type == RenderObjectTableSection || RenderBox::isOfType(type); }
250 virtual void willBeRemovedFromTree() override;
252 virtual void layout() override;
254 virtual void paintObject(PaintInfo&, const LayoutPoint&) override;
256 virtual void imageChanged(WrappedImagePtr, const IntRect* = 0) override;
258 int borderSpacingForRow(unsigned row) const { return m_grid[row].rowRenderer ? table()->vBorderSpacing() : 0; }
260 void ensureRows(unsigned);
262 bool rowHasOnlySpanningCells(unsigned);
263 unsigned calcRowHeightHavingOnlySpanningCells(unsigned);
264 void updateRowsHeightHavingOnlySpanningCells(RenderTableCell*, struct SpanningRowsHeight&);
265 bool isHeightNeededForRowHavingOnlySpanningCells(unsigned);
267 void populateSpanningRowsHeightFromCell(RenderTableCell*, struct SpanningRowsHeight&);
268 void distributeExtraRowSpanHeightToPercentRows(RenderTableCell*, int, int&, Vector<int>&);
269 void distributeWholeExtraRowSpanHeightToPercentRows(RenderTableCell*, int, int&, Vector<int>&);
270 void distributeExtraRowSpanHeightToAutoRows(RenderTableCell*, int, int&, Vector<int>&);
271 void distributeExtraRowSpanHeightToRemainingRows(RenderTableCell*, int, int&, Vector<int>&);
272 void distributeRowSpanHeightToRows(SpanningRenderTableCells& rowSpanCells);
274 void distributeExtraLogicalHeightToPercentRows(int& extraLogicalHeight, int totalPercent);
275 void distributeExtraLogicalHeightToAutoRows(int& extraLogicalHeight, unsigned autoRowsCount);
276 void distributeRemainingExtraLogicalHeight(int& extraLogicalHeight);
278 void updateBaselineForCell(RenderTableCell*, unsigned row, LayoutUnit& baselineDescent);
280 bool hasOverflowingCell() const { return m_overflowingCells.size() || m_forceSlowPaintPathWithOverflowingCell; }
282 void computeOverflowFromCells(unsigned totalRows, unsigned nEffCols);
284 CellSpan fullTableRowSpan() const { return CellSpan(0, m_grid.size()); }
285 CellSpan fullTableColumnSpan() const { return CellSpan(0, table()->columns().size()); }
287 // These two functions take a rectangle as input that has been flipped by logicalRectForWritingModeAndDirection.
288 // The returned span of rows or columns is end-exclusive, and empty if start==end.
289 CellSpan spannedRows(const LayoutRect& flippedRect) const;
290 CellSpan spannedColumns(const LayoutRect& flippedRect) const;
292 void setLogicalPositionForCell(RenderTableCell*, unsigned effectiveColumn) const;
294 RenderObjectChildList m_children;
296 WillBeHeapVector<RowStruct> m_grid;
297 Vector<int> m_rowPos;
299 // the current insertion position
303 int m_outerBorderStart;
304 int m_outerBorderEnd;
305 int m_outerBorderBefore;
306 int m_outerBorderAfter;
308 bool m_needsCellRecalc;
310 // This HashSet holds the overflowing cells for faster painting.
311 // If we have more than gMaxAllowedOverflowingCellRatio * total cells, it will be empty
312 // and m_forceSlowPaintPathWithOverflowingCell will be set to save memory.
313 WillBeHeapHashSet<RawPtrWillBeMember<RenderTableCell> > m_overflowingCells;
314 bool m_forceSlowPaintPathWithOverflowingCell;
316 bool m_hasMultipleCellLevels;
318 // This map holds the collapsed border values for cells with collapsed borders.
319 // It is held at RenderTableSection level to spare memory consumption by table cells.
320 WillBeHeapHashMap<pair<RawPtrWillBeMember<const RenderTableCell>, int>, CollapsedBorderValue > m_cellsCollapsedBorders;
323 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderTableSection, isTableSection());
330 template<> struct VectorTraits<blink::RenderTableSection::RowStruct> : VectorTraitsBase<blink::RenderTableSection::RowStruct> {
331 static const bool needsDestruction = false;
337 #endif // RenderTableSection_h