Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 1997 Martin Jones (mjones (at) kde.org)
      3  *           (C) 1997 Torben Weis (weis (at) kde.org)
      4  *           (C) 1998 Waldo Bastian (bastian (at) kde.org)
      5  *           (C) 1999 Lars Knoll (knoll (at) kde.org)
      6  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      7  * Copyright (C) 2003, 2004, 2005, 2006, 2009, 2013 Apple Inc. All rights reserved.
      8  *
      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.
     13  *
     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.
     18  *
     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.
     23  */
     24 
     25 #ifndef RenderTableSection_h
     26 #define RenderTableSection_h
     27 
     28 #include "core/rendering/RenderTable.h"
     29 #include "wtf/Vector.h"
     30 
     31 namespace blink {
     32 
     33 // This variable is used to balance the memory consumption vs the paint invalidation time on big tables.
     34 const float gMaxAllowedOverflowingCellRatioForFastPaintPath = 0.1f;
     35 
     36 enum CollapsedBorderSide {
     37     CBSBefore,
     38     CBSAfter,
     39     CBSStart,
     40     CBSEnd
     41 };
     42 
     43 // Helper class for paintObject.
     44 class CellSpan {
     45 public:
     46     CellSpan(unsigned start, unsigned end)
     47         : m_start(start)
     48         , m_end(end)
     49     {
     50     }
     51 
     52     unsigned start() const { return m_start; }
     53     unsigned end() const { return m_end; }
     54 
     55     unsigned& start() { return m_start; }
     56     unsigned& end() { return m_end; }
     57 
     58 private:
     59     unsigned m_start;
     60     unsigned m_end;
     61 };
     62 
     63 class RenderTableCell;
     64 class RenderTableRow;
     65 
     66 class RenderTableSection FINAL : public RenderBox {
     67 public:
     68     RenderTableSection(Element*);
     69     virtual ~RenderTableSection();
     70     virtual void trace(Visitor*) OVERRIDE;
     71 
     72     RenderTableRow* firstRow() const;
     73     RenderTableRow* lastRow() const;
     74 
     75     const RenderObjectChildList* children() const { return &m_children; }
     76     RenderObjectChildList* children() { return &m_children; }
     77 
     78     virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0) OVERRIDE;
     79 
     80     virtual int firstLineBoxBaseline() const OVERRIDE;
     81 
     82     void addCell(RenderTableCell*, RenderTableRow* row);
     83 
     84     int calcRowLogicalHeight();
     85     void layoutRows();
     86     void computeOverflowFromCells();
     87 
     88     RenderTable* table() const { return toRenderTable(parent()); }
     89 
     90     typedef WillBeHeapVector<RawPtrWillBeMember<RenderTableCell>, 2> SpanningRenderTableCells;
     91 
     92     struct CellStruct {
     93         ALLOW_ONLY_INLINE_ALLOCATION();
     94     public:
     95         WillBeHeapVector<RawPtrWillBeMember<RenderTableCell>, 1> cells;
     96         bool inColSpan; // true for columns after the first in a colspan
     97 
     98         CellStruct()
     99             : inColSpan(false)
    100         {
    101         }
    102         void trace(Visitor*);
    103 
    104         RenderTableCell* primaryCell()
    105         {
    106             return hasCells() ? cells[cells.size() - 1].get() : 0;
    107         }
    108 
    109         const RenderTableCell* primaryCell() const
    110         {
    111             return hasCells() ? cells[cells.size() - 1].get() : 0;
    112         }
    113 
    114         bool hasCells() const { return cells.size() > 0; }
    115     };
    116 
    117     typedef WillBeHeapVector<CellStruct> Row;
    118 
    119     struct RowStruct {
    120         ALLOW_ONLY_INLINE_ALLOCATION();
    121     public:
    122         RowStruct()
    123             : rowRenderer(nullptr)
    124             , baseline()
    125         {
    126         }
    127         void trace(Visitor*);
    128 
    129         Row row;
    130         RawPtrWillBeMember<RenderTableRow> rowRenderer;
    131         LayoutUnit baseline;
    132         Length logicalHeight;
    133     };
    134 
    135     struct SpanningRowsHeight {
    136         WTF_MAKE_NONCOPYABLE(SpanningRowsHeight);
    137 
    138     public:
    139         SpanningRowsHeight()
    140             : totalRowsHeight(0)
    141             , spanningCellHeightIgnoringBorderSpacing(0)
    142             , isAnyRowWithOnlySpanningCells(false)
    143         {
    144         }
    145 
    146         Vector<int> rowHeight;
    147         int totalRowsHeight;
    148         int spanningCellHeightIgnoringBorderSpacing;
    149         bool isAnyRowWithOnlySpanningCells;
    150     };
    151 
    152     const BorderValue& borderAdjoiningTableStart() const
    153     {
    154         if (hasSameDirectionAs(table()))
    155             return style()->borderStart();
    156 
    157         return style()->borderEnd();
    158     }
    159 
    160     const BorderValue& borderAdjoiningTableEnd() const
    161     {
    162         if (hasSameDirectionAs(table()))
    163             return style()->borderEnd();
    164 
    165         return style()->borderStart();
    166     }
    167 
    168     const BorderValue& borderAdjoiningStartCell(const RenderTableCell*) const;
    169     const BorderValue& borderAdjoiningEndCell(const RenderTableCell*) const;
    170 
    171     const RenderTableCell* firstRowCellAdjoiningTableStart() const;
    172     const RenderTableCell* firstRowCellAdjoiningTableEnd() const;
    173 
    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)
    177     {
    178         CellStruct& c = m_grid[row].row[col];
    179         return c.primaryCell();
    180     }
    181 
    182     RenderTableRow* rowRendererAt(unsigned row) const { return m_grid[row].rowRenderer; }
    183 
    184     void appendColumn(unsigned pos);
    185     void splitColumn(unsigned pos, unsigned first);
    186 
    187     enum BlockBorderSide { BorderBefore, BorderAfter };
    188     int calcBlockDirectionOuterBorder(BlockBorderSide) const;
    189     enum InlineBorderSide { BorderStart, BorderEnd };
    190     int calcInlineDirectionOuterBorder(InlineBorderSide) const;
    191     void recalcOuterBorder();
    192 
    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; }
    197 
    198     unsigned numRows() const { return m_grid.size(); }
    199     unsigned numColumns() const;
    200     void recalcCells();
    201     void recalcCellsIfNeeded()
    202     {
    203         if (m_needsCellRecalc)
    204             recalcCells();
    205     }
    206 
    207     bool needsCellRecalc() const { return m_needsCellRecalc; }
    208     void setNeedsCellRecalc();
    209 
    210     LayoutUnit rowBaseline(unsigned row) { return m_grid[row].baseline; }
    211 
    212     void rowLogicalHeightChanged(RenderTableRow*);
    213 
    214     void removeCachedCollapsedBorders(const RenderTableCell*);
    215     void setCachedCollapsedBorder(const RenderTableCell*, CollapsedBorderSide, CollapsedBorderValue);
    216     CollapsedBorderValue& cachedCollapsedBorder(const RenderTableCell*, CollapsedBorderSide);
    217 
    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);
    221 
    222     static RenderTableSection* createAnonymousWithParentRenderer(const RenderObject*);
    223     virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const OVERRIDE
    224     {
    225         return createAnonymousWithParentRenderer(parent);
    226     }
    227 
    228     virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE;
    229 
    230     // Flip the rect so it aligns with the coordinates used by the rowPos and columnPos vectors.
    231     LayoutRect logicalRectForWritingModeAndDirection(const LayoutRect&) const;
    232 
    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; }
    237 
    238 protected:
    239     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
    240     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
    241 
    242 private:
    243     virtual RenderObjectChildList* virtualChildren() OVERRIDE { return children(); }
    244     virtual const RenderObjectChildList* virtualChildren() const OVERRIDE { return children(); }
    245 
    246     virtual const char* renderName() const OVERRIDE { return (isAnonymous() || isPseudoElement()) ? "RenderTableSection (anonymous)" : "RenderTableSection"; }
    247 
    248     virtual bool isTableSection() const OVERRIDE { return true; }
    249 
    250     virtual void willBeRemovedFromTree() OVERRIDE;
    251 
    252     virtual void layout() OVERRIDE;
    253 
    254     virtual void paintObject(PaintInfo&, const LayoutPoint&) OVERRIDE;
    255 
    256     virtual void imageChanged(WrappedImagePtr, const IntRect* = 0) OVERRIDE;
    257 
    258     int borderSpacingForRow(unsigned row) const { return m_grid[row].rowRenderer ? table()->vBorderSpacing() : 0; }
    259 
    260     void ensureRows(unsigned);
    261 
    262     bool rowHasOnlySpanningCells(unsigned);
    263     unsigned calcRowHeightHavingOnlySpanningCells(unsigned);
    264     void updateRowsHeightHavingOnlySpanningCells(RenderTableCell*, struct SpanningRowsHeight&);
    265     bool isHeightNeededForRowHavingOnlySpanningCells(unsigned);
    266 
    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);
    273 
    274     void distributeExtraLogicalHeightToPercentRows(int& extraLogicalHeight, int totalPercent);
    275     void distributeExtraLogicalHeightToAutoRows(int& extraLogicalHeight, unsigned autoRowsCount);
    276     void distributeRemainingExtraLogicalHeight(int& extraLogicalHeight);
    277 
    278     void updateBaselineForCell(RenderTableCell*, unsigned row, LayoutUnit& baselineDescent);
    279 
    280     bool hasOverflowingCell() const { return m_overflowingCells.size() || m_forceSlowPaintPathWithOverflowingCell; }
    281 
    282     void computeOverflowFromCells(unsigned totalRows, unsigned nEffCols);
    283 
    284     CellSpan fullTableRowSpan() const { return CellSpan(0, m_grid.size()); }
    285     CellSpan fullTableColumnSpan() const { return CellSpan(0, table()->columns().size()); }
    286 
    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;
    291 
    292     void setLogicalPositionForCell(RenderTableCell*, unsigned effectiveColumn) const;
    293 
    294     RenderObjectChildList m_children;
    295 
    296     WillBeHeapVector<RowStruct> m_grid;
    297     Vector<int> m_rowPos;
    298 
    299     // the current insertion position
    300     unsigned m_cCol;
    301     unsigned m_cRow;
    302 
    303     int m_outerBorderStart;
    304     int m_outerBorderEnd;
    305     int m_outerBorderBefore;
    306     int m_outerBorderAfter;
    307 
    308     bool m_needsCellRecalc;
    309 
    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;
    315 
    316     bool m_hasMultipleCellLevels;
    317 
    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;
    321 };
    322 
    323 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderTableSection, isTableSection());
    324 
    325 } // namespace blink
    326 
    327 namespace WTF {
    328 
    329 #if ENABLE(OILPAN)
    330 template<> struct VectorTraits<blink::RenderTableSection::CellStruct> : VectorTraitsBase<blink::RenderTableSection::CellStruct> {
    331     static const bool needsDestruction = false;
    332 };
    333 template<> struct VectorTraits<blink::RenderTableSection::RowStruct> : VectorTraitsBase<blink::RenderTableSection::RowStruct> {
    334     static const bool needsDestruction = false;
    335 };
    336 #endif
    337 
    338 }
    339 
    340 #endif // RenderTableSection_h
    341