1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! CSS table formatting contexts.
use layout::box_::Box;
use layout::block::{BlockFlow, MarginsMayNotCollapse, WidthAndMarginsComputer};
use layout::block::{WidthConstraintInput, WidthConstraintSolution};
use layout::construct::FlowConstructor;
use layout::context::LayoutContext;
use layout::display_list_builder::{DisplayListBuilder, DisplayListBuildingInfo};
use layout::floats::{FloatKind};
use layout::flow::{TableFlowClass, FlowClass, Flow, ImmutableFlowUtils};
use layout::flow;
use layout::table_wrapper::{TableLayout, FixedLayout, AutoLayout};
use layout::wrapper::ThreadSafeLayoutNode;
use gfx::display_list::StackingContext;
use servo_util::geometry::Au;
use servo_util::geometry;
use style::computed_values::table_layout;
/// A table flow corresponded to the table's internal table box under a table wrapper flow.
/// The properties `position`, `float`, and `margin-*` are used on the table wrapper box,
/// not table box per CSS 2.1 § 10.5.
pub struct TableFlow {
block_flow: BlockFlow,
/// Column widths
col_widths: ~[Au],
/// Column min widths.
col_min_widths: ~[Au],
/// Column pref widths.
col_pref_widths: ~[Au],
/// Table-layout property
table_layout: TableLayout,
}
impl TableFlow {
pub fn from_node_and_box(node: &ThreadSafeLayoutNode,
box_: Box)
-> TableFlow {
let mut block_flow = BlockFlow::from_node_and_box(node, box_);
let table_layout = if block_flow.box_().style().Table.get().table_layout ==
table_layout::fixed {
FixedLayout
} else {
AutoLayout
};
TableFlow {
block_flow: block_flow,
col_widths: ~[],
col_min_widths: ~[],
col_pref_widths: ~[],
table_layout: table_layout
}
}
pub fn from_node(constructor: &mut FlowConstructor,
node: &ThreadSafeLayoutNode)
-> TableFlow {
let mut block_flow = BlockFlow::from_node(constructor, node);
let table_layout = if block_flow.box_().style().Table.get().table_layout ==
table_layout::fixed {
FixedLayout
} else {
AutoLayout
};
TableFlow {
block_flow: block_flow,
col_widths: ~[],
col_min_widths: ~[],
col_pref_widths: ~[],
table_layout: table_layout
}
}
pub fn float_from_node(constructor: &mut FlowConstructor,
node: &ThreadSafeLayoutNode,
float_kind: FloatKind)
-> TableFlow {
let mut block_flow = BlockFlow::float_from_node(constructor, node, float_kind);
let table_layout = if block_flow.box_().style().Table.get().table_layout ==
table_layout::fixed {
FixedLayout
} else {
AutoLayout
};
TableFlow {
block_flow: block_flow,
col_widths: ~[],
col_min_widths: ~[],
col_pref_widths: ~[],
table_layout: table_layout
}
}
pub fn teardown(&mut self) {
self.block_flow.teardown();
self.col_widths = ~[];
self.col_min_widths = ~[];
self.col_pref_widths = ~[];
}
/// Update the corresponding value of self_widths if a value of kid_widths has larger value
/// than one of self_widths.
pub fn update_col_widths(self_widths: &mut ~[Au], kid_widths: &~[Au]) -> Au {
let mut sum_widths = Au(0);
let mut kid_widths_it = kid_widths.iter();
for self_width in self_widths.mut_iter() {
match kid_widths_it.next() {
Some(kid_width) => {
if *self_width < *kid_width {
*self_width = *kid_width;
}
},
None => {}
}
sum_widths = sum_widths + *self_width;
}
sum_widths
}
/// Assign height for table flow.
///
/// TODO(pcwalton): This probably doesn't handle margin collapse right.
///
/// inline(always) because this is only ever called by in-order or non-in-order top-level
/// methods
#[inline(always)]
fn assign_height_table_base(&mut self, layout_context: &mut LayoutContext, inorder: bool) {
self.block_flow.assign_height_block_base(layout_context, inorder, MarginsMayNotCollapse);
}
pub fn build_display_list_table(&mut self,
stacking_context: &mut StackingContext,
builder: &mut DisplayListBuilder,
info: &DisplayListBuildingInfo) {
debug!("build_display_list_table: same process as block flow");
self.block_flow.build_display_list_block(stacking_context, builder, info);
}
}
impl Flow for TableFlow {
fn class(&self) -> FlowClass {
TableFlowClass
}
fn as_table<'a>(&'a mut self) -> &'a mut TableFlow {
self
}
fn as_block<'a>(&'a mut self) -> &'a mut BlockFlow {
&mut self.block_flow
}
fn col_widths<'a>(&'a mut self) -> &'a mut ~[Au] {
&mut self.col_widths
}
fn col_min_widths<'a>(&'a self) -> &'a ~[Au] {
&self.col_min_widths
}
fn col_pref_widths<'a>(&'a self) -> &'a ~[Au] {
&self.col_pref_widths
}
/// The specified column widths are set from column group and the first row for the fixed
/// table layout calculation.
/// The maximum min/pref widths of each column are set from the rows for the automatic
/// table layout calculation.
fn bubble_widths(&mut self, _: &mut LayoutContext) {
let mut min_width = Au(0);
let mut pref_width = Au(0);
let mut did_first_row = false;
let mut num_floats = 0;
for kid in self.block_flow.base.child_iter() {
assert!(kid.is_proper_table_child());
if kid.is_table_colgroup() {
self.col_widths.push_all(kid.as_table_colgroup().widths);
self.col_min_widths = self.col_widths.clone();
self.col_pref_widths = self.col_widths.clone();
} else if kid.is_table_rowgroup() || kid.is_table_row() {
// read column widths from table-row-group/table-row, and assign
// width=0 for the columns not defined in column-group
// FIXME: need to read widths from either table-header-group OR
// first table-row
match self.table_layout {
FixedLayout => {
let kid_col_widths = kid.col_widths();
if !did_first_row {
did_first_row = true;
let mut child_widths = kid_col_widths.iter();
for col_width in self.col_widths.mut_iter() {
match child_widths.next() {
Some(child_width) => {
if *col_width == Au::new(0) {
*col_width = *child_width;
}
},
None => break
}
}
}
let num_child_cols = kid_col_widths.len();
let num_cols = self.col_widths.len();
debug!("table until the previous row has {} column(s) and this row has {} column(s)",
num_cols, num_child_cols);
for i in range(num_cols, num_child_cols) {
self.col_widths.push( kid_col_widths[i] );
}
},
AutoLayout => {
min_width = TableFlow::update_col_widths(&mut self.col_min_widths, kid.col_min_widths());
pref_width = TableFlow::update_col_widths(&mut self.col_pref_widths, kid.col_pref_widths());
// update the number of column widths from table-rows.
let num_cols = self.col_min_widths.len();
let num_child_cols = kid.col_min_widths().len();
debug!("table until the previous row has {} column(s) and this row has {} column(s)",
num_cols, num_child_cols);
for i in range(num_cols, num_child_cols) {
self.col_widths.push(Au::new(0));
let new_kid_min = kid.col_min_widths()[i];
self.col_min_widths.push( new_kid_min );
let new_kid_pref = kid.col_pref_widths()[i];
self.col_pref_widths.push( new_kid_pref );
min_width = min_width + new_kid_min;
pref_width = pref_width + new_kid_pref;
}
}
}
}
let child_base = flow::mut_base(kid);
num_floats = num_floats + child_base.num_floats;
}
for box_ in self.block_flow.box_.iter() {
box_.compute_borders(box_.style());
}
self.block_flow.base.num_floats = num_floats;
self.block_flow.base.intrinsic_widths.minimum_width = min_width;
self.block_flow.base.intrinsic_widths.preferred_width = geometry::max(min_width, pref_width);
}
/// Recursively (top-down) determines the actual width of child contexts and boxes. When called
/// on this context, the context has had its width set by the parent context.
fn assign_widths(&mut self, ctx: &mut LayoutContext) {
debug!("assign_widths({}): assigning width for flow", "table");
// The position was set to the containing block by the flow's parent.
let containing_block_width = self.block_flow.base.position.size.width;
let mut left_content_edge = Au::new(0);
let mut content_width = containing_block_width;
let mut num_unspecified_widths = 0;
let mut total_column_width = Au::new(0);
for col_width in self.col_widths.iter() {
if *col_width == Au::new(0) {
num_unspecified_widths += 1;
} else {
total_column_width = total_column_width.add(col_width);
}
}
let width_computer = InternalTable;
width_computer.compute_used_width(&mut self.block_flow, ctx, containing_block_width);
for box_ in self.block_flow.box_.iter() {
left_content_edge = box_.padding.get().left + box_.border.get().left;
let padding_and_borders = box_.padding.get().left + box_.padding.get().right +
box_.border.get().left + box_.border.get().right;
content_width = box_.border_box.get().size.width - padding_and_borders;
}
match self.table_layout {
FixedLayout => {
// In fixed table layout, we distribute extra space among the unspecified columns if there are
// any, or among all the columns if all are specified.
if (total_column_width < content_width) && (num_unspecified_widths == 0) {
let ratio = content_width.to_f64().unwrap() / total_column_width.to_f64().unwrap();
for col_width in self.col_widths.mut_iter() {
*col_width = (*col_width).scale_by(ratio);
}
} else if num_unspecified_widths != 0 {
let extra_column_width = (content_width - total_column_width) / Au::new(num_unspecified_widths);
for col_width in self.col_widths.mut_iter() {
if *col_width == Au(0) {
*col_width = extra_column_width;
}
}
}
}
_ => {}
}
self.block_flow.propagate_assigned_width_to_children(left_content_edge, content_width, Some(self.col_widths.clone()));
}
/// This is called on kid flows by a parent.
///
/// Hence, we can assume that assign_height has already been called on the
/// kid (because of the bottom-up traversal).
fn assign_height_inorder(&mut self, ctx: &mut LayoutContext) {
debug!("assign_height_inorder: assigning height for table");
self.assign_height_table_base(ctx, true);
}
fn assign_height(&mut self, ctx: &mut LayoutContext) {
debug!("assign_height: assigning height for table");
self.assign_height_table_base(ctx, false);
}
fn debug_str(&self) -> ~str {
let txt = ~"TableFlow: ";
txt.append(match self.block_flow.box_ {
Some(ref rb) => rb.debug_str(),
None => ~"",
})
}
}
/// Table, TableRowGroup, TableRow, TableCell types.
/// Their widths are calculated in the same way and do not have margins.
pub struct InternalTable;
impl WidthAndMarginsComputer for InternalTable {
/// Compute the used value of width, taking care of min-width and max-width.
///
/// CSS Section 10.4: Minimum and Maximum widths
fn compute_used_width(&self,
block: &mut BlockFlow,
ctx: &mut LayoutContext,
parent_flow_width: Au) {
let input = self.compute_width_constraint_inputs(block, parent_flow_width, ctx);
let solution = self.solve_width_constraints(block, input);
self.set_width_constraint_solutions(block, solution);
}
/// Solve the width and margins constraints for this block flow.
fn solve_width_constraints(&self,
_: &mut BlockFlow,
input: WidthConstraintInput)
-> WidthConstraintSolution {
WidthConstraintSolution::new(input.available_width, Au::new(0), Au::new(0))
}
}
|