1 | /* |
2 | * Copyright (C) 2012 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
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. |
12 | * |
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 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. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "LayerFragment.h" |
29 | #include "RenderFragmentContainerSet.h" |
30 | #include "RenderMultiColumnFlow.h" |
31 | #include <wtf/Vector.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | // RenderMultiColumnSet represents a set of columns that all have the same width and height. By combining runs of same-size columns into a single |
36 | // object, we significantly reduce the number of unique RenderObjects required to represent columns. |
37 | // |
38 | // A simple multi-column block will have exactly one RenderMultiColumnSet child. A simple paginated multi-column block will have three |
39 | // RenderMultiColumnSet children: one for the content at the bottom of the first page (whose columns will have a shorter height), one |
40 | // for the 2nd to n-1 pages, and then one last column set that will hold the shorter columns on the final page (that may have to be balanced |
41 | // as well). |
42 | // |
43 | // Column spans result in the creation of new column sets as well, since a spanning fragment has to be placed in between the column sets that |
44 | // come before and after the span. |
45 | class RenderMultiColumnSet final : public RenderFragmentContainerSet { |
46 | WTF_MAKE_ISO_ALLOCATED(RenderMultiColumnSet); |
47 | public: |
48 | RenderMultiColumnSet(RenderFragmentedFlow&, RenderStyle&&); |
49 | |
50 | RenderBlockFlow* multiColumnBlockFlow() const { return downcast<RenderBlockFlow>(parent()); } |
51 | RenderMultiColumnFlow* multiColumnFlow() const { return static_cast<RenderMultiColumnFlow*>(fragmentedFlow()); } |
52 | |
53 | RenderMultiColumnSet* nextSiblingMultiColumnSet() const; |
54 | RenderMultiColumnSet* previousSiblingMultiColumnSet() const; |
55 | |
56 | // Return the first object in the flow thread that's rendered inside this set. |
57 | RenderObject* firstRendererInFragmentedFlow() const; |
58 | // Return the last object in the flow thread that's rendered inside this set. |
59 | RenderObject* lastRendererInFragmentedFlow() const; |
60 | |
61 | // Return true if the specified renderer (descendant of the flow thread) is inside this column set. |
62 | bool containsRendererInFragmentedFlow(const RenderObject&) const; |
63 | |
64 | void setLogicalTopInFragmentedFlow(LayoutUnit); |
65 | LayoutUnit logicalTopInFragmentedFlow() const { return isHorizontalWritingMode() ? fragmentedFlowPortionRect().y() : fragmentedFlowPortionRect().x(); } |
66 | void setLogicalBottomInFragmentedFlow(LayoutUnit); |
67 | LayoutUnit logicalBottomInFragmentedFlow() const { return isHorizontalWritingMode() ? fragmentedFlowPortionRect().maxY() : fragmentedFlowPortionRect().maxX(); } |
68 | LayoutUnit logicalHeightInFragmentedFlow() const { return isHorizontalWritingMode() ? fragmentedFlowPortionRect().height() : fragmentedFlowPortionRect().width(); } |
69 | |
70 | unsigned computedColumnCount() const { return m_computedColumnCount; } |
71 | LayoutUnit computedColumnWidth() const { return m_computedColumnWidth; } |
72 | LayoutUnit computedColumnHeight() const { return m_computedColumnHeight; } |
73 | bool columnHeightComputed() const { return m_columnHeightComputed; } |
74 | |
75 | void setComputedColumnWidthAndCount(LayoutUnit width, unsigned count) |
76 | { |
77 | m_computedColumnWidth = width; |
78 | m_computedColumnCount = count; |
79 | } |
80 | |
81 | LayoutUnit heightAdjustedForSetOffset(LayoutUnit height) const; |
82 | |
83 | void updateMinimumColumnHeight(LayoutUnit height) { m_minimumColumnHeight = std::max(height, m_minimumColumnHeight); } |
84 | LayoutUnit minimumColumnHeight() const { return m_minimumColumnHeight; } |
85 | |
86 | unsigned forcedBreaksCount() const { return m_contentRuns.size(); } |
87 | void clearForcedBreaks(); |
88 | void addForcedBreak(LayoutUnit offsetFromFirstPage); |
89 | |
90 | // (Re-)calculate the column height. This is first and foremost needed by sets that are to |
91 | // balance the column height, but even when it isn't to be balanced, this is necessary if the |
92 | // multicol container's height is constrained. If |initial| is set, and we are to balance, guess |
93 | // an initial column height; otherwise, stretch the column height a tad. Return true if column |
94 | // height changed and another layout pass is required. |
95 | bool recalculateColumnHeight(bool initial); |
96 | |
97 | // Record space shortage (the amount of space that would have been enough to prevent some |
98 | // element from being moved to the next column) at a column break. The smallest amount of space |
99 | // shortage we find is the amount with which we will stretch the column height, if it turns out |
100 | // after layout that the columns weren't tall enough. |
101 | void recordSpaceShortage(LayoutUnit spaceShortage); |
102 | |
103 | void updateLogicalWidth() override; |
104 | |
105 | void prepareForLayout(bool initial); |
106 | // Begin laying out content for this column set. This happens at the beginning of flow thread |
107 | // layout, and when advancing from a previous column set or spanner to this one. |
108 | void beginFlow(RenderBlock* container); |
109 | // Finish laying out content for this column set. This happens at end of flow thread layout, and |
110 | // when advancing to the next column set or spanner. |
111 | void endFlow(RenderBlock* container, LayoutUnit bottomInContainer); |
112 | // Has this set been flowed in this layout pass? |
113 | bool hasBeenFlowed() const { return logicalBottomInFragmentedFlow() != RenderFragmentedFlow::maxLogicalHeight(); } |
114 | |
115 | bool requiresBalancing() const; |
116 | |
117 | LayoutPoint columnTranslationForOffset(const LayoutUnit&) const; |
118 | |
119 | void paintColumnRules(PaintInfo&, const LayoutPoint& paintOffset) override; |
120 | |
121 | enum ColumnHitTestTranslationMode { |
122 | ClampHitTestTranslationToColumns, |
123 | DoNotClampHitTestTranslationToColumns |
124 | }; |
125 | LayoutPoint translateFragmentPointToFragmentedFlow(const LayoutPoint & logicalPoint, ColumnHitTestTranslationMode = DoNotClampHitTestTranslationToColumns) const; |
126 | |
127 | void updateHitTestResult(HitTestResult&, const LayoutPoint&) override; |
128 | |
129 | LayoutRect columnRectAt(unsigned index) const; |
130 | unsigned columnCount() const; |
131 | |
132 | protected: |
133 | void addOverflowFromChildren() override; |
134 | |
135 | private: |
136 | bool isRenderMultiColumnSet() const override { return true; } |
137 | void layout() override; |
138 | |
139 | LogicalExtentComputedValues computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop) const override; |
140 | |
141 | void paintObject(PaintInfo&, const LayoutPoint&) override { } |
142 | |
143 | LayoutUnit pageLogicalWidth() const override { return m_computedColumnWidth; } |
144 | LayoutUnit pageLogicalHeight() const override { return m_computedColumnHeight; } |
145 | |
146 | LayoutUnit pageLogicalTopForOffset(LayoutUnit offset) const override; |
147 | |
148 | LayoutUnit logicalHeightOfAllFragmentedFlowContent() const override { return logicalHeightInFragmentedFlow(); } |
149 | |
150 | void repaintFragmentedFlowContent(const LayoutRect& repaintRect) override; |
151 | |
152 | void collectLayerFragments(LayerFragments&, const LayoutRect& layerBoundingBox, const LayoutRect& dirtyRect) override; |
153 | |
154 | void adjustFragmentBoundsFromFragmentedFlowPortionRect(LayoutRect& fragmentBounds) const override; |
155 | |
156 | VisiblePosition positionForPoint(const LayoutPoint&, const RenderFragmentContainer*) override; |
157 | |
158 | const char* renderName() const override; |
159 | |
160 | LayoutUnit calculateMaxColumnHeight() const; |
161 | LayoutUnit columnGap() const; |
162 | |
163 | LayoutUnit columnLogicalLeft(unsigned) const; |
164 | LayoutUnit columnLogicalTop(unsigned) const; |
165 | |
166 | LayoutRect fragmentedFlowPortionRectAt(unsigned index) const; |
167 | LayoutRect fragmentedFlowPortionOverflowRect(const LayoutRect& fragmentedFlowPortion, unsigned index, unsigned colCount, LayoutUnit colGap); |
168 | |
169 | LayoutUnit initialBlockOffsetForPainting() const; |
170 | |
171 | enum ColumnIndexCalculationMode { |
172 | ClampToExistingColumns, // Stay within the range of already existing columns. |
173 | AssumeNewColumns // Allow column indices outside the range of already existing columns. |
174 | }; |
175 | unsigned columnIndexAtOffset(LayoutUnit, ColumnIndexCalculationMode = ClampToExistingColumns) const; |
176 | |
177 | void setAndConstrainColumnHeight(LayoutUnit); |
178 | |
179 | // Return the index of the content run with the currently tallest columns, taking all implicit |
180 | // breaks assumed so far into account. |
181 | unsigned findRunWithTallestColumns() const; |
182 | |
183 | // Given the current list of content runs, make assumptions about where we need to insert |
184 | // implicit breaks (if there's room for any at all; depending on the number of explicit breaks), |
185 | // and store the results. This is needed in order to balance the columns. |
186 | void distributeImplicitBreaks(); |
187 | |
188 | LayoutUnit calculateBalancedHeight(bool initial) const; |
189 | |
190 | unsigned m_computedColumnCount; // Used column count (the resulting 'N' from the pseudo-algorithm in the multicol spec) |
191 | LayoutUnit m_computedColumnWidth; // Used column width (the resulting 'W' from the pseudo-algorithm in the multicol spec) |
192 | LayoutUnit m_computedColumnHeight; |
193 | LayoutUnit m_availableColumnHeight; |
194 | bool m_columnHeightComputed; |
195 | |
196 | // The following variables are used when balancing the column set. |
197 | LayoutUnit m_maxColumnHeight; // Maximum column height allowed. |
198 | LayoutUnit m_minSpaceShortage; // The smallest amout of space shortage that caused a column break. |
199 | LayoutUnit m_minimumColumnHeight; |
200 | |
201 | // A run of content without explicit (forced) breaks; i.e. a flow thread portion between two |
202 | // explicit breaks, between flow thread start and an explicit break, between an explicit break |
203 | // and flow thread end, or, in cases when there are no explicit breaks at all: between flow flow |
204 | // thread start and flow thread end. We need to know where the explicit breaks are, in order to |
205 | // figure out where the implicit breaks will end up, so that we get the columns properly |
206 | // balanced. A content run starts out as representing one single column, and will represent one |
207 | // additional column for each implicit break "inserted" there. |
208 | class ContentRun { |
209 | public: |
210 | ContentRun(LayoutUnit breakOffset) |
211 | : m_breakOffset(breakOffset) |
212 | , m_assumedImplicitBreaks(0) { } |
213 | |
214 | unsigned assumedImplicitBreaks() const { return m_assumedImplicitBreaks; } |
215 | void assumeAnotherImplicitBreak() { m_assumedImplicitBreaks++; } |
216 | LayoutUnit breakOffset() const { return m_breakOffset; } |
217 | |
218 | // Return the column height that this content run would require, considering the implicit |
219 | // breaks assumed so far. |
220 | LayoutUnit columnLogicalHeight(LayoutUnit startOffset) const { return ceilf(float(m_breakOffset - startOffset) / float(m_assumedImplicitBreaks + 1)); } |
221 | |
222 | private: |
223 | LayoutUnit m_breakOffset; // Flow thread offset where this run ends. |
224 | unsigned m_assumedImplicitBreaks; // Number of implicit breaks in this run assumed so far. |
225 | }; |
226 | Vector<ContentRun, 1> m_contentRuns; |
227 | }; |
228 | |
229 | } // namespace WebCore |
230 | |
231 | SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderMultiColumnSet, isRenderMultiColumnSet()) |
232 | |