OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/layout/LayoutMultiColumnSet.h" | 5 #include "core/layout/LayoutMultiColumnSet.h" |
6 | 6 |
7 namespace blink { | 7 namespace blink { |
8 | 8 |
9 // A column balancer traverses a portion of the subtree of a flow thread that be
longs to one or | 9 // A column balancer traverses a portion of the subtree of a flow thread that |
10 // more fragmentainer groups within one column set, in order to collect certain
data to be used for | 10 // belongs to one or more fragmentainer groups within one column set, in order |
11 // column balancing. This is an abstract class that just walks the subtree and l
eaves it to | 11 // to collect certain data to be used for column balancing. This is an abstract |
12 // subclasses to actually collect data. | 12 // class that just walks the subtree and leaves it to subclasses to actually |
| 13 // collect data. |
13 class ColumnBalancer { | 14 class ColumnBalancer { |
14 protected: | 15 protected: |
15 ColumnBalancer(const LayoutMultiColumnSet&, | 16 ColumnBalancer(const LayoutMultiColumnSet&, LayoutUnit logicalTopInFlowThread, |
16 LayoutUnit logicalTopInFlowThread, | |
17 LayoutUnit logicalBottomInFlowThread); | 17 LayoutUnit logicalBottomInFlowThread); |
18 | 18 |
19 const LayoutMultiColumnSet& columnSet() const { return m_columnSet; } | 19 const LayoutMultiColumnSet& columnSet() const { return m_columnSet; } |
20 | 20 |
21 // The flow thread portion we're examining. It may be that of the entire colum
n set, or just of | 21 // The flow thread portion we're examining. It may be that of the entire |
22 // a fragmentainer group. | 22 // column set, or just of a fragmentainer group. |
23 const LayoutUnit logicalTopInFlowThread() const { | 23 const LayoutUnit logicalTopInFlowThread() const { |
24 return m_logicalTopInFlowThread; | 24 return m_logicalTopInFlowThread; |
25 } | 25 } |
26 const LayoutUnit logicalBottomInFlowThread() const { | 26 const LayoutUnit logicalBottomInFlowThread() const { |
27 return m_logicalBottomInFlowThread; | 27 return m_logicalBottomInFlowThread; |
28 } | 28 } |
29 | 29 |
30 const MultiColumnFragmentainerGroup& groupAtOffset( | 30 const MultiColumnFragmentainerGroup& groupAtOffset( |
31 LayoutUnit offsetInFlowThread) const { | 31 LayoutUnit offsetInFlowThread) const { |
32 return m_columnSet.fragmentainerGroupAtFlowThreadOffset( | 32 return m_columnSet.fragmentainerGroupAtFlowThreadOffset( |
33 offsetInFlowThread, LayoutBox::AssociateWithLatterPage); | 33 offsetInFlowThread, LayoutBox::AssociateWithLatterPage); |
34 } | 34 } |
35 | 35 |
36 LayoutUnit offsetFromColumnLogicalTop(LayoutUnit offsetInFlowThread) const { | 36 LayoutUnit offsetFromColumnLogicalTop(LayoutUnit offsetInFlowThread) const { |
37 return offsetInFlowThread - | 37 return offsetInFlowThread - |
38 groupAtOffset(offsetInFlowThread) | 38 groupAtOffset(offsetInFlowThread) |
39 .columnLogicalTopForOffset(offsetInFlowThread); | 39 .columnLogicalTopForOffset(offsetInFlowThread); |
40 } | 40 } |
41 | 41 |
42 // Flow thread offset for the layout object that we're currently examining. | 42 // Flow thread offset for the layout object that we're currently examining. |
43 LayoutUnit flowThreadOffset() const { return m_flowThreadOffset; } | 43 LayoutUnit flowThreadOffset() const { return m_flowThreadOffset; } |
44 | 44 |
45 // Return true if the specified offset is at the top of a column, as long as i
t's not the first | 45 // Return true if the specified offset is at the top of a column, as long as |
46 // column in the flow thread portion. | 46 // it's not the first column in the flow thread portion. |
47 bool isFirstAfterBreak(LayoutUnit flowThreadOffset) const { | 47 bool isFirstAfterBreak(LayoutUnit flowThreadOffset) const { |
48 if (flowThreadOffset <= m_logicalTopInFlowThread) { | 48 if (flowThreadOffset <= m_logicalTopInFlowThread) { |
49 // The first column is either not after any break at all, or after a break
in a | 49 // The first column is either not after any break at all, or after a break |
50 // previous fragmentainer group. | 50 // in a previous fragmentainer group. |
51 return false; | 51 return false; |
52 } | 52 } |
53 return flowThreadOffset == | 53 return flowThreadOffset == |
54 groupAtOffset(flowThreadOffset) | 54 groupAtOffset(flowThreadOffset) |
55 .columnLogicalTopForOffset(flowThreadOffset); | 55 .columnLogicalTopForOffset(flowThreadOffset); |
56 } | 56 } |
57 | 57 |
58 bool isLogicalTopWithinBounds(LayoutUnit logicalTopInFlowThread) const { | 58 bool isLogicalTopWithinBounds(LayoutUnit logicalTopInFlowThread) const { |
59 return logicalTopInFlowThread >= m_logicalTopInFlowThread && | 59 return logicalTopInFlowThread >= m_logicalTopInFlowThread && |
60 logicalTopInFlowThread < m_logicalBottomInFlowThread; | 60 logicalTopInFlowThread < m_logicalBottomInFlowThread; |
61 } | 61 } |
62 | 62 |
63 bool isLogicalBottomWithinBounds(LayoutUnit logicalBottomInFlowThread) const { | 63 bool isLogicalBottomWithinBounds(LayoutUnit logicalBottomInFlowThread) const { |
64 return logicalBottomInFlowThread > m_logicalTopInFlowThread && | 64 return logicalBottomInFlowThread > m_logicalTopInFlowThread && |
65 logicalBottomInFlowThread <= m_logicalBottomInFlowThread; | 65 logicalBottomInFlowThread <= m_logicalBottomInFlowThread; |
66 } | 66 } |
67 | 67 |
68 // Examine and collect column balancing data from a layout box that has been f
ound to intersect | 68 // Examine and collect column balancing data from a layout box that has been |
69 // with the flow thread portion we're examining. Does not recurse into | 69 // found to intersect with the flow thread portion we're examining. Does not |
70 // children. flowThreadOffset() will return the offset from |box| to the flow
thread. Two hooks | 70 // recurse into children. flowThreadOffset() will return the offset from |box| |
71 // are provided here. The first one is called right after entering and before
traversing the | 71 // to the flow thread. Two hooks are provided here. The first one is called |
72 // subtree of the box, and the second one right after having traversed the sub
tree. | 72 // right after entering and before traversing the subtree of the box, and the |
| 73 // second one right after having traversed the subtree. |
73 virtual void examineBoxAfterEntering(const LayoutBox&, | 74 virtual void examineBoxAfterEntering(const LayoutBox&, |
74 EBreak previousBreakAfterValue) = 0; | 75 EBreak previousBreakAfterValue) = 0; |
75 virtual void examineBoxBeforeLeaving(const LayoutBox&) = 0; | 76 virtual void examineBoxBeforeLeaving(const LayoutBox&) = 0; |
76 | 77 |
77 // Examine and collect column balancing data from a line that has been found t
o intersect with | 78 // Examine and collect column balancing data from a line that has been found |
78 // the flow thread portion. Does not recurse into layout objects on that line. | 79 // to intersect with the flow thread portion. Does not recurse into layout |
| 80 // objects on that line. |
79 virtual void examineLine(const RootInlineBox&) = 0; | 81 virtual void examineLine(const RootInlineBox&) = 0; |
80 | 82 |
81 // Examine and collect column balancing data for everything in the flow thread
portion. Will | 83 // Examine and collect column balancing data for everything in the flow thread |
82 // trigger calls to examineBoxAfterEntering(), examineBoxBeforeLeaving() and e
xamineLine() for | 84 // portion. Will trigger calls to examineBoxAfterEntering(), |
83 // interesting boxes and lines. | 85 // examineBoxBeforeLeaving() and examineLine() for interesting boxes and |
| 86 // lines. |
84 void traverse(); | 87 void traverse(); |
85 | 88 |
86 private: | 89 private: |
87 void traverseSubtree(const LayoutBox&); | 90 void traverseSubtree(const LayoutBox&); |
88 | 91 |
89 const LayoutMultiColumnSet& m_columnSet; | 92 const LayoutMultiColumnSet& m_columnSet; |
90 const LayoutUnit m_logicalTopInFlowThread; | 93 const LayoutUnit m_logicalTopInFlowThread; |
91 const LayoutUnit m_logicalBottomInFlowThread; | 94 const LayoutUnit m_logicalBottomInFlowThread; |
92 | 95 |
93 LayoutUnit m_flowThreadOffset; | 96 LayoutUnit m_flowThreadOffset; |
94 }; | 97 }; |
95 | 98 |
96 // After an initial layout pass, we know the height of the contents of a flow th
read. Based on | 99 // After an initial layout pass, we know the height of the contents of a flow |
97 // this, we can estimate an initial minimal column height. This class will colle
ct the necessary | 100 // thread. Based on this, we can estimate an initial minimal column height. This |
98 // information from the layout objects to make this estimate. This estimate may
be used to perform | 101 // class will collect the necessary information from the layout objects to make |
99 // another layout iteration. If we after such a layout iteration cannot fit the
contents with the | 102 // this estimate. This estimate may be used to perform another layout iteration. |
100 // given column height without creating overflowing columns, we will have to str
etch the columns by | 103 // If we after such a layout iteration cannot fit the contents with the given |
101 // some amount and lay out again. We may need to do this several times (but typi
cally not more | 104 // column height without creating overflowing columns, we will have to stretch |
102 // times than the number of columns that we have). The amount to stretch is prov
ided by the sister | 105 // the columns by some amount and lay out again. We may need to do this several |
103 // of this class, named MinimumSpaceShortageFinder. | 106 // times (but typically not more times than the number of columns that we have). |
| 107 // The amount to stretch is provided by the sister of this class, named |
| 108 // MinimumSpaceShortageFinder. |
104 class InitialColumnHeightFinder final : public ColumnBalancer { | 109 class InitialColumnHeightFinder final : public ColumnBalancer { |
105 public: | 110 public: |
106 InitialColumnHeightFinder(const LayoutMultiColumnSet&, | 111 InitialColumnHeightFinder(const LayoutMultiColumnSet&, |
107 LayoutUnit logicalTopInFlowThread, | 112 LayoutUnit logicalTopInFlowThread, |
108 LayoutUnit logicalBottomInFlowThread); | 113 LayoutUnit logicalBottomInFlowThread); |
109 | 114 |
110 LayoutUnit initialMinimalBalancedHeight() const; | 115 LayoutUnit initialMinimalBalancedHeight() const; |
111 | 116 |
112 // Height of the tallest piece of unbreakable content. This is the minimum col
umn logical height | 117 // Height of the tallest piece of unbreakable content. This is the minimum |
113 // required to avoid fragmentation where it shouldn't occur (inside unbreakabl
e content, between | 118 // column logical height required to avoid fragmentation where it shouldn't |
114 // orphans and widows, etc.). This will be used as a hint to the column balanc
er to help set a | 119 // occur (inside unbreakable content, between orphans and widows, etc.). This |
115 // good initial column height. | 120 // will be used as a hint to the column balancer to help set a good initial |
| 121 // column height. |
116 LayoutUnit tallestUnbreakableLogicalHeight() const { | 122 LayoutUnit tallestUnbreakableLogicalHeight() const { |
117 return m_tallestUnbreakableLogicalHeight; | 123 return m_tallestUnbreakableLogicalHeight; |
118 } | 124 } |
119 | 125 |
120 private: | 126 private: |
121 void examineBoxAfterEntering(const LayoutBox&, | 127 void examineBoxAfterEntering(const LayoutBox&, |
122 EBreak previousBreakAfterValue); | 128 EBreak previousBreakAfterValue); |
123 void examineBoxBeforeLeaving(const LayoutBox&); | 129 void examineBoxBeforeLeaving(const LayoutBox&); |
124 void examineLine(const RootInlineBox&); | 130 void examineLine(const RootInlineBox&); |
125 | 131 |
126 // Record that there's a pagination strut that ends at the specified |offsetIn
FlowThread|, which | 132 // Record that there's a pagination strut that ends at the specified |
127 // is an offset exactly at the top of some column. | 133 // |offsetInFlowThread|, which is an offset exactly at the top of some column. |
128 void recordStrutBeforeOffset(LayoutUnit offsetInFlowThread, LayoutUnit strut); | 134 void recordStrutBeforeOffset(LayoutUnit offsetInFlowThread, LayoutUnit strut); |
129 | 135 |
130 // Return the accumulated space used by struts at all column boundaries preced
ing the specified | 136 // Return the accumulated space used by struts at all column boundaries |
131 // flowthread offset. | 137 // preceding the specified flowthread offset. |
132 LayoutUnit spaceUsedByStrutsAt(LayoutUnit offsetInFlowThread) const; | 138 LayoutUnit spaceUsedByStrutsAt(LayoutUnit offsetInFlowThread) const; |
133 | 139 |
134 // Add a content run, specified by its end position. A content run is appended
at every | 140 // Add a content run, specified by its end position. A content run is appended |
135 // forced/explicit break and at the end of the column set. The content runs ar
e used to | 141 // at every forced/explicit break and at the end of the column set. The |
136 // determine where implicit/soft breaks will occur, in order to calculate an i
nitial column | 142 // content runs are used to determine where implicit/soft breaks will occur, |
137 // height. | 143 // in order to calculate an initial column height. |
138 void addContentRun(LayoutUnit endOffsetInFlowThread); | 144 void addContentRun(LayoutUnit endOffsetInFlowThread); |
139 | 145 |
140 // Return the index of the content run with the currently tallest columns, tak
ing all implicit | 146 // Return the index of the content run with the currently tallest columns, |
141 // breaks assumed so far into account. | 147 // taking all implicit breaks assumed so far into account. |
142 unsigned contentRunIndexWithTallestColumns() const; | 148 unsigned contentRunIndexWithTallestColumns() const; |
143 | 149 |
144 // Given the current list of content runs, make assumptions about where we nee
d to insert | 150 // Given the current list of content runs, make assumptions about where we |
145 // implicit breaks (if there's room for any at all; depending on the number of
explicit breaks), | 151 // need to insert implicit breaks (if there's room for any at all; depending |
146 // and store the results. This is needed in order to balance the columns. | 152 // on the number of explicit breaks), and store the results. This is needed in |
| 153 // order to balance the columns. |
147 void distributeImplicitBreaks(); | 154 void distributeImplicitBreaks(); |
148 | 155 |
149 // A run of content without explicit (forced) breaks; i.e. a flow thread porti
on between two | 156 // A run of content without explicit (forced) breaks; i.e. a flow thread |
150 // explicit breaks, between flow thread start and an explicit break, between a
n explicit break | 157 // portion between two explicit breaks, between flow thread start and an |
151 // and flow thread end, or, in cases when there are no explicit breaks at all:
between flow | 158 // explicit break, between an explicit break and flow thread end, or, in cases |
152 // thread portion start and flow thread portion end. We need to know where the
explicit breaks | 159 // when there are no explicit breaks at all: between flow thread portion start |
153 // are, in order to figure out where the implicit breaks will end up, so that
we get the columns | 160 // and flow thread portion end. We need to know where the explicit breaks are, |
154 // properly balanced. A content run starts out as representing one single colu
mn, and will | 161 // in order to figure out where the implicit breaks will end up, so that we |
155 // represent one additional column for each implicit break "inserted" there. | 162 // get the columns properly balanced. A content run starts out as representing |
| 163 // one single column, and will represent one additional column for each |
| 164 // implicit break "inserted" there. |
156 class ContentRun { | 165 class ContentRun { |
157 public: | 166 public: |
158 ContentRun(LayoutUnit breakOffset) | 167 ContentRun(LayoutUnit breakOffset) |
159 : m_breakOffset(breakOffset), m_assumedImplicitBreaks(0) {} | 168 : m_breakOffset(breakOffset), m_assumedImplicitBreaks(0) {} |
160 | 169 |
161 unsigned assumedImplicitBreaks() const { return m_assumedImplicitBreaks; } | 170 unsigned assumedImplicitBreaks() const { return m_assumedImplicitBreaks; } |
162 void assumeAnotherImplicitBreak() { m_assumedImplicitBreaks++; } | 171 void assumeAnotherImplicitBreak() { m_assumedImplicitBreaks++; } |
163 LayoutUnit breakOffset() const { return m_breakOffset; } | 172 LayoutUnit breakOffset() const { return m_breakOffset; } |
164 | 173 |
165 // Return the column height that this content run would require, considering
the implicit | 174 // Return the column height that this content run would require, considering |
166 // breaks assumed so far. | 175 // the implicit breaks assumed so far. |
167 LayoutUnit columnLogicalHeight(LayoutUnit startOffset) const { | 176 LayoutUnit columnLogicalHeight(LayoutUnit startOffset) const { |
168 return LayoutUnit::fromFloatCeil(float(m_breakOffset - startOffset) / | 177 return LayoutUnit::fromFloatCeil(float(m_breakOffset - startOffset) / |
169 float(m_assumedImplicitBreaks + 1)); | 178 float(m_assumedImplicitBreaks + 1)); |
170 } | 179 } |
171 | 180 |
172 private: | 181 private: |
173 LayoutUnit m_breakOffset; // Flow thread offset where this run ends. | 182 LayoutUnit m_breakOffset; // Flow thread offset where this run ends. |
174 unsigned | 183 unsigned m_assumedImplicitBreaks; // Number of implicit breaks in this run |
175 m_assumedImplicitBreaks; // Number of implicit breaks in this run assum
ed so far. | 184 // assumed so far. |
176 }; | 185 }; |
177 Vector<ContentRun, 32> m_contentRuns; | 186 Vector<ContentRun, 32> m_contentRuns; |
178 | 187 |
179 // Shortest strut found at each column boundary (index 0 being the boundary be
tween the first | 188 // Shortest strut found at each column boundary (index 0 being the boundary |
180 // and the second column, index 1 being the one between the second and the thi
rd boundary, and | 189 // between the first and the second column, index 1 being the one between the |
181 // so on). There may be several objects that cross the same column boundary, a
nd we're only | 190 // second and the third boundary, and so on). There may be several objects |
182 // interested in the shortest one. For example, when having a float beside reg
ular in-flow | 191 // that cross the same column boundary, and we're only interested in the |
183 // content, we end up with two parallel fragmentation flows [1]. The shortest
strut found at a | 192 // shortest one. For example, when having a float beside regular in-flow |
184 // column boundary is the amount of space that we wasted at said column bounda
ry, and it needs | 193 // content, we end up with two parallel fragmentation flows [1]. The shortest |
185 // to be deducted when estimating the initial balanced column height, or we ri
sk making the | 194 // strut found at a column boundary is the amount of space that we wasted at |
186 // column row too tall. An entry set to LayoutUnit::max() means that we didn't
detect any object | 195 // said column boundary, and it needs to be deducted when estimating the |
| 196 // initial balanced column height, or we risk making the column row too tall. |
| 197 // An entry set to LayoutUnit::max() means that we didn't detect any object |
187 // crossing that boundary. | 198 // crossing that boundary. |
188 // | 199 // |
189 // [1] http://www.w3.org/TR/css3-break/#parallel-flows | 200 // [1] http://www.w3.org/TR/css3-break/#parallel-flows |
190 Vector<LayoutUnit, 32> m_shortestStruts; | 201 Vector<LayoutUnit, 32> m_shortestStruts; |
191 | 202 |
192 LayoutUnit m_tallestUnbreakableLogicalHeight; | 203 LayoutUnit m_tallestUnbreakableLogicalHeight; |
193 }; | 204 }; |
194 | 205 |
195 // If we have previously used InitialColumnHeightFinder to estimate an initial c
olumn height, and | 206 // If we have previously used InitialColumnHeightFinder to estimate an initial |
196 // that didn't result in tall enough columns, we need subsequent layout passes w
here we increase | 207 // column height, and that didn't result in tall enough columns, we need |
197 // the column height by the minimum space shortage at column breaks. This class
finds the minimum | 208 // subsequent layout passes where we increase the column height by the minimum |
198 // space shortage after having laid out with the current column height. | 209 // space shortage at column breaks. This class finds the minimum space shortage |
| 210 // after having laid out with the current column height. |
199 class MinimumSpaceShortageFinder final : public ColumnBalancer { | 211 class MinimumSpaceShortageFinder final : public ColumnBalancer { |
200 public: | 212 public: |
201 MinimumSpaceShortageFinder(const LayoutMultiColumnSet&, | 213 MinimumSpaceShortageFinder(const LayoutMultiColumnSet&, |
202 LayoutUnit logicalTopInFlowThread, | 214 LayoutUnit logicalTopInFlowThread, |
203 LayoutUnit logicalBottomInFlowThread); | 215 LayoutUnit logicalBottomInFlowThread); |
204 | 216 |
205 LayoutUnit minimumSpaceShortage() const { return m_minimumSpaceShortage; } | 217 LayoutUnit minimumSpaceShortage() const { return m_minimumSpaceShortage; } |
206 unsigned forcedBreaksCount() const { return m_forcedBreaksCount; } | 218 unsigned forcedBreaksCount() const { return m_forcedBreaksCount; } |
207 | 219 |
208 private: | 220 private: |
209 void examineBoxAfterEntering(const LayoutBox&, | 221 void examineBoxAfterEntering(const LayoutBox&, |
210 EBreak previousBreakAfterValue); | 222 EBreak previousBreakAfterValue); |
211 void examineBoxBeforeLeaving(const LayoutBox&); | 223 void examineBoxBeforeLeaving(const LayoutBox&); |
212 void examineLine(const RootInlineBox&); | 224 void examineLine(const RootInlineBox&); |
213 | 225 |
214 void recordSpaceShortage(LayoutUnit shortage) { | 226 void recordSpaceShortage(LayoutUnit shortage) { |
215 // Only positive values are interesting (and allowed) here. Zero space short
age may | 227 // Only positive values are interesting (and allowed) here. Zero space |
216 // be reported when we're at the top of a column and the element has zero | 228 // shortage may be reported when we're at the top of a column and the |
217 // height. | 229 // element has zero height. |
218 if (shortage > 0) | 230 if (shortage > 0) |
219 m_minimumSpaceShortage = std::min(m_minimumSpaceShortage, shortage); | 231 m_minimumSpaceShortage = std::min(m_minimumSpaceShortage, shortage); |
220 } | 232 } |
221 | 233 |
222 // The smallest amout of space shortage that caused a column break. | 234 // The smallest amout of space shortage that caused a column break. |
223 LayoutUnit m_minimumSpaceShortage; | 235 LayoutUnit m_minimumSpaceShortage; |
224 | 236 |
225 // Set when breaking before a block, and we're looking for the first unbreakab
le descendant, in | 237 // Set when breaking before a block, and we're looking for the first |
226 // order to report correct space shortage for that one. | 238 // unbreakable descendant, in order to report correct space shortage for that |
| 239 // one. |
227 LayoutUnit m_pendingStrut; | 240 LayoutUnit m_pendingStrut; |
228 | 241 |
229 unsigned m_forcedBreaksCount; | 242 unsigned m_forcedBreaksCount; |
230 }; | 243 }; |
231 | 244 |
232 } // namespace blink | 245 } // namespace blink |
OLD | NEW |