2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef RenderMultiColumnSet_h
28 #define RenderMultiColumnSet_h
30 #include "core/rendering/RenderMultiColumnFlowThread.h"
31 #include "core/rendering/RenderRegion.h"
32 #include "wtf/Vector.h"
36 // RenderMultiColumnSet represents a set of columns that all have the same width and height. By
37 // combining runs of same-size columns into a single object, we significantly reduce the number of
38 // unique RenderObjects required to represent columns.
40 // Column sets are inserted as anonymous children of the actual multicol container (i.e. the
41 // renderer whose style computes to non-auto column-count and/or column-width).
43 // Being a "region", a column set has no children on its own, but is merely used to slice a portion
44 // of the tall "single-column" flow thread into actual columns visually, to convert from flow thread
45 // coordinates to visual ones. It is in charge of both positioning columns correctly relatively to
46 // the parent multicol container, and to calculate the correct translation for each column's
47 // contents, and to paint any rules between them. RenderMultiColumnSet objects are used for
48 // painting, hit testing, and any other type of operation that requires mapping from flow thread
49 // coordinates to visual coordinates.
51 // Column spans result in the creation of new column sets, since a spanning renderer has to be
52 // placed in between the column sets that come before and after the span.
53 class RenderMultiColumnSet FINAL : public RenderRegion {
55 static RenderMultiColumnSet* createAnonymous(RenderFlowThread*, RenderStyle* parentStyle);
57 virtual bool isRenderMultiColumnSet() const OVERRIDE { return true; }
59 RenderBlockFlow* multiColumnBlockFlow() const { return toRenderBlockFlow(parent()); }
60 RenderMultiColumnFlowThread* multiColumnFlowThread() const
62 ASSERT_WITH_SECURITY_IMPLICATION(!flowThread() || flowThread()->isRenderMultiColumnFlowThread());
63 return static_cast<RenderMultiColumnFlowThread*>(flowThread());
66 RenderMultiColumnSet* nextSiblingMultiColumnSet() const;
68 LayoutUnit logicalBottomInFlowThread() const { return isHorizontalWritingMode() ? flowThreadPortionRect().maxY() : flowThreadPortionRect().maxX(); }
70 unsigned computedColumnCount() const { return m_computedColumnCount; }
71 LayoutUnit computedColumnWidth() const { return m_computedColumnWidth; }
72 LayoutUnit computedColumnHeight() const { return m_computedColumnHeight; }
74 void setComputedColumnWidthAndCount(LayoutUnit width, unsigned count)
76 m_computedColumnWidth = width;
77 m_computedColumnCount = count;
80 // Find the column that contains the given block offset, and return the translation needed to
81 // get from flow thread coordinates to visual coordinates.
82 LayoutSize flowThreadTranslationAtOffset(LayoutUnit) const;
84 LayoutUnit heightAdjustedForSetOffset(LayoutUnit height) const;
86 void updateMinimumColumnHeight(LayoutUnit height) { m_minimumColumnHeight = std::max(height, m_minimumColumnHeight); }
87 LayoutUnit minimumColumnHeight() const { return m_minimumColumnHeight; }
89 unsigned forcedBreaksCount() const { return m_contentRuns.size(); }
90 void clearForcedBreaks();
91 void addForcedBreak(LayoutUnit offsetFromFirstPage);
93 // (Re-)calculate the column height if it's auto. If 'initial' is set, guess an initial column
94 // height; otherwise, stretch the column height a tad. Return true if column height changed and
95 // another layout pass is required.
96 bool recalculateColumnHeight(bool initial);
98 // Record space shortage (the amount of space that would have been enough to prevent some
99 // element from being moved to the next column) at a column break. The smallest amount of space
100 // shortage we find is the amount with which we will stretch the column height, if it turns out
101 // after layout that the columns weren't tall enough.
102 void recordSpaceShortage(LayoutUnit spaceShortage);
104 virtual void updateLogicalWidth() OVERRIDE;
106 void prepareForLayout();
108 // Expand this set's flow thread portion rectangle to contain all trailing flow thread
109 // overflow. Only to be called on the last set.
110 void expandToEncompassFlowThreadContentsIfNeeded();
113 RenderMultiColumnSet(RenderFlowThread*);
115 virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const OVERRIDE;
117 virtual void paintObject(PaintInfo&, const LayoutPoint& paintOffset) OVERRIDE;
119 virtual LayoutUnit pageLogicalWidth() const OVERRIDE { return m_computedColumnWidth; }
120 virtual LayoutUnit pageLogicalHeight() const OVERRIDE { return m_computedColumnHeight; }
122 virtual LayoutUnit pageLogicalTopForOffset(LayoutUnit offset) const OVERRIDE;
124 // FIXME: This will change once we have column sets constrained by enclosing pages, etc.
125 virtual LayoutUnit logicalHeightOfAllFlowThreadContent() const OVERRIDE { return m_computedColumnHeight; }
127 virtual void repaintFlowThreadContent(const LayoutRect& repaintRect) const OVERRIDE;
129 virtual void collectLayerFragments(LayerFragments&, const LayoutRect& layerBoundingBox, const LayoutRect& dirtyRect) OVERRIDE;
131 virtual const char* renderName() const OVERRIDE;
133 void paintColumnRules(PaintInfo&, const LayoutPoint& paintOffset);
135 LayoutUnit columnGap() const;
136 LayoutRect columnRectAt(unsigned index) const;
137 unsigned columnCount() const;
139 LayoutRect flowThreadPortionRectAt(unsigned index) const;
140 LayoutRect flowThreadPortionOverflowRect(const LayoutRect& flowThreadPortion, unsigned index, unsigned colCount, LayoutUnit colGap) const;
142 enum ColumnIndexCalculationMode {
143 ClampToExistingColumns, // Stay within the range of already existing columns.
144 AssumeNewColumns // Allow column indices outside the range of already existing columns.
146 unsigned columnIndexAtOffset(LayoutUnit, ColumnIndexCalculationMode = ClampToExistingColumns) const;
148 void setAndConstrainColumnHeight(LayoutUnit);
150 // Return the index of the content run with the currently tallest columns, taking all implicit
151 // breaks assumed so far into account.
152 unsigned findRunWithTallestColumns() const;
154 // Given the current list of content runs, make assumptions about where we need to insert
155 // implicit breaks (if there's room for any at all; depending on the number of explicit breaks),
156 // and store the results. This is needed in order to balance the columns.
157 void distributeImplicitBreaks();
159 LayoutUnit calculateColumnHeight(bool initial) const;
161 unsigned m_computedColumnCount; // Used column count (the resulting 'N' from the pseudo-algorithm in the multicol spec)
162 LayoutUnit m_computedColumnWidth; // Used column width (the resulting 'W' from the pseudo-algorithm in the multicol spec)
163 LayoutUnit m_computedColumnHeight;
165 // The following variables are used when balancing the column set.
166 LayoutUnit m_maxColumnHeight; // Maximum column height allowed.
167 LayoutUnit m_minSpaceShortage; // The smallest amout of space shortage that caused a column break.
168 LayoutUnit m_minimumColumnHeight;
170 // A run of content without explicit (forced) breaks; i.e. a flow thread portion between two
171 // explicit breaks, between flow thread start and an explicit break, between an explicit break
172 // and flow thread end, or, in cases when there are no explicit breaks at all: between flow flow
173 // thread start and flow thread end. We need to know where the explicit breaks are, in order to
174 // figure out where the implicit breaks will end up, so that we get the columns properly
175 // balanced. A content run starts out as representing one single column, and will represent one
176 // additional column for each implicit break "inserted" there.
179 ContentRun(LayoutUnit breakOffset)
180 : m_breakOffset(breakOffset)
181 , m_assumedImplicitBreaks(0) { }
183 unsigned assumedImplicitBreaks() const { return m_assumedImplicitBreaks; }
184 void assumeAnotherImplicitBreak() { m_assumedImplicitBreaks++; }
185 LayoutUnit breakOffset() const { return m_breakOffset; }
187 // Return the column height that this content run would require, considering the implicit
188 // breaks assumed so far.
189 LayoutUnit columnLogicalHeight(LayoutUnit startOffset) const { return ceilf((m_breakOffset - startOffset).toFloat() / float(m_assumedImplicitBreaks + 1)); }
192 LayoutUnit m_breakOffset; // Flow thread offset where this run ends.
193 unsigned m_assumedImplicitBreaks; // Number of implicit breaks in this run assumed so far.
195 Vector<ContentRun, 1> m_contentRuns;
198 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderMultiColumnSet, isRenderMultiColumnSet());
200 } // namespace WebCore
202 #endif // RenderMultiColumnSet_h