/
float.rs
324 lines (259 loc) · 11.8 KB
/
float.rs
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
/* 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/. */
use layout::box::{RenderBox};
use layout::context::LayoutContext;
use layout::display_list_builder::{DisplayListBuilder, ExtraDisplayListData};
use layout::flow::{FloatFlow, FlowData};
use layout::model::{MaybeAuto};
use layout::float_context::{FloatContext, PlacementInfo, FloatType};
use std::cell::Cell;
use geom::point::Point2D;
use geom::rect::Rect;
use gfx::display_list::DisplayList;
use gfx::geometry::Au;
use gfx::geometry;
use servo_util::tree::TreeNodeRef;
pub struct FloatFlowData {
/// Data common to all flows.
common: FlowData,
/// The associated render box.
box: Option<RenderBox>,
containing_width: Au,
/// Offset relative to where the parent tried to position this flow
rel_pos: Point2D<Au>,
/// Left or right?
float_type: FloatType,
/// Index into the box list for inline floats
index: Option<uint>,
/// Number of floated children
floated_children: uint,
}
impl FloatFlowData {
pub fn new(common: FlowData, float_type: FloatType) -> FloatFlowData {
FloatFlowData {
common: common,
containing_width: Au(0),
box: None,
index: None,
float_type: float_type,
rel_pos: Point2D(Au(0), Au(0)),
floated_children: 0,
}
}
pub fn teardown(&mut self) {
self.common.teardown();
for box in self.box.iter() {
box.teardown();
}
self.box = None;
self.index = None;
}
}
impl FloatFlowData {
pub fn bubble_widths_float(@mut self, ctx: &LayoutContext) {
let mut min_width = Au(0);
let mut pref_width = Au(0);
let mut num_floats = 0;
for child_ctx in FloatFlow(self).children() {
//assert!(child_ctx.starts_block_flow() || child_ctx.starts_inline_flow());
do child_ctx.with_mut_base |child_node| {
min_width = geometry::max(min_width, child_node.min_width);
pref_width = geometry::max(pref_width, child_node.pref_width);
num_floats = num_floats + child_node.num_floats;
}
}
self.common.num_floats = 1;
self.floated_children = num_floats;
self.box.map(|&box| {
let style = box.style();
do box.with_model |model| {
model.compute_borders(style)
}
min_width = min_width.add(&box.get_min_width(ctx));
pref_width = pref_width.add(&box.get_pref_width(ctx));
});
self.common.min_width = min_width;
self.common.pref_width = pref_width;
}
pub fn assign_widths_float(@mut self) {
debug!("assign_widths_float: assigning width for flow %?", self.common.id);
// position.size.width is set by parent even though we don't know
// position.origin yet.
let mut remaining_width = self.common.position.size.width;
self.containing_width = remaining_width;
let mut x_offset = Au(0);
// Parent usually sets this, but floats are never inorder
self.common.is_inorder = false;
for &box in self.box.iter() {
let style = box.style();
do box.with_model |model| {
// Can compute padding here since we know containing block width.
model.compute_padding(style, remaining_width);
// Margins for floats are 0 if auto.
let margin_top = MaybeAuto::from_margin(style.margin_top(),
remaining_width,
style.font_size()).specified_or_zero();
let margin_bottom = MaybeAuto::from_margin(style.margin_bottom(),
remaining_width,
style.font_size()).specified_or_zero();
let margin_left = MaybeAuto::from_margin(style.margin_left(),
remaining_width,
style.font_size()).specified_or_zero();
let margin_right = MaybeAuto::from_margin(style.margin_right(),
remaining_width,
style.font_size()).specified_or_zero();
let shrink_to_fit = geometry::min(self.common.pref_width,
geometry::max(self.common.min_width,
remaining_width));
let width = MaybeAuto::from_width(style.width(),
remaining_width,
style.font_size()).specified_or_default(shrink_to_fit);
debug!("assign_widths_float -- width: %?", width);
model.margin.top = margin_top;
model.margin.right = margin_right;
model.margin.bottom = margin_bottom;
model.margin.left = margin_left;
x_offset = model.offset();
remaining_width = width;
}
do box.with_mut_base |base| {
//The associated box is the border box of this flow
base.position.origin.x = base.model.margin.left;
let pb = base.model.padding.left + base.model.padding.right +
base.model.border.left + base.model.border.right;
base.position.size.width = remaining_width + pb;
}
}
self.common.position.size.width = remaining_width;
let has_inorder_children = self.common.num_floats > 0;
for kid in FloatFlow(self).children() {
//assert!(kid.starts_block_flow() || kid.starts_inline_flow());
do kid.with_mut_base |child_node| {
child_node.position.origin.x = x_offset;
child_node.position.size.width = remaining_width;
child_node.is_inorder = has_inorder_children;
if !child_node.is_inorder {
child_node.floats_in = FloatContext::new(0);
}
}
}
}
pub fn assign_height_inorder_float(@mut self) {
debug!("assign_height_inorder_float: assigning height for float %?", self.common.id);
// assign_height_float was already called by the traversal function
// so this is well-defined
let mut height = Au(0);
let mut clearance = Au(0);
let mut full_noncontent_width = Au(0);
let mut margin_height = Au(0);
self.box.map(|&box| {
height = do box.with_base |base| {
base.position.size.height
};
clearance = match box.clear() {
None => Au(0),
Some(clear) => {
self.common.floats_in.clearance(clear)
}
};
do box.with_base |base| {
let noncontent_width = base.model.padding.left + base.model.padding.right +
base.model.border.left + base.model.border.right;
full_noncontent_width = noncontent_width + base.model.margin.left + base.model.margin.right;
margin_height = base.model.margin.top + base.model.margin.bottom;
}
});
let info = PlacementInfo {
width: self.common.position.size.width + full_noncontent_width,
height: height + margin_height,
ceiling: clearance,
max_width: self.containing_width,
f_type: self.float_type,
};
// Place the float and return the FloatContext back to the parent flow.
// After, grab the position and use that to set our position.
self.common.floats_out = self.common.floats_in.add_float(&info);
self.rel_pos = self.common.floats_out.last_float_pos();
}
pub fn assign_height_float(@mut self, ctx: &mut LayoutContext) {
debug!("assign_height_float: assigning height for float %?", self.common.id);
let has_inorder_children = self.common.num_floats > 0;
if has_inorder_children {
let mut float_ctx = FloatContext::new(self.floated_children);
for kid in FloatFlow(self).children() {
do kid.with_mut_base |child_node| {
child_node.floats_in = float_ctx.clone();
}
kid.assign_height_inorder(ctx);
do kid.with_mut_base |child_node| {
float_ctx = child_node.floats_out.clone();
}
}
}
let mut cur_y = Au(0);
let mut top_offset = Au(0);
for &box in self.box.iter() {
do box.with_model |model| {
top_offset = model.margin.top + model.border.top + model.padding.top;
cur_y = cur_y + top_offset;
}
}
for kid in FloatFlow(self).children() {
do kid.with_mut_base |child_node| {
child_node.position.origin.y = cur_y;
cur_y = cur_y + child_node.position.size.height;
};
}
let mut height = cur_y - top_offset;
let mut noncontent_width = Au(0);
let mut noncontent_height = Au(0);
self.box.map(|&box| {
do box.with_mut_base |base| {
//The associated box is the border box of this flow
base.position.origin.y = base.model.margin.top;
noncontent_width = base.model.padding.left + base.model.padding.right +
base.model.border.left + base.model.border.right;
noncontent_height = base.model.padding.top + base.model.padding.bottom +
base.model.border.top + base.model.border.bottom;
base.position.size.height = height + noncontent_height;
}
});
//TODO(eatkinson): compute heights properly using the 'height' property.
for &box in self.box.iter() {
let height_prop =
MaybeAuto::from_height(box.style().height(),
Au(0),
box.style().font_size()).specified_or_zero();
height = geometry::max(height, height_prop) + noncontent_height;
debug!("assign_height_float -- height: %?", height);
do box.with_mut_base |base| {
base.position.size.height = height;
}
}
}
pub fn build_display_list_float<E:ExtraDisplayListData>(@mut self,
builder: &DisplayListBuilder,
dirty: &Rect<Au>,
list: &Cell<DisplayList<E>>)
-> bool {
let abs_rect = Rect(self.common.abs_position, self.common.position.size);
if !abs_rect.intersects(dirty) {
return false;
}
let offset = self.common.abs_position + self.rel_pos;
// add box that starts block context
self.box.map(|&box| {
box.build_display_list(builder, dirty, &offset, list)
});
// TODO: handle any out-of-flow elements
// go deeper into the flow tree
let flow = FloatFlow(self);
for child in flow.children() {
do child.with_mut_base |base| {
base.abs_position = offset + base.position.origin;
}
}
true
}
}