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
use super::*;
/// The physical containing space supplied while measuring an automatic
/// positioned block axis. Keeping both axes together prevents a vertical
/// writing-mode measurement from accidentally replacing its logical inline
/// containing size with the unbounded horizontal block-axis sentinel.
/// <https://www.w3.org/TR/css-position-3/#abspos-layout>
/// <https://www.w3.org/TR/css-writing-modes-4/#dimension-mapping>
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct PositionedAutoBlockMeasurementSpace {
pub(in crate::layout) content_width: PhysicalContentWidth,
pub(in crate::layout) available_physical_height: PhysicalContentHeight,
}
/// The percentage-resolution boundary for the principal box of an automatic
/// positioned-block measurement.
///
/// The principal resolves its percentages against its absolute-position
/// containing block even while this probe determines an automatic size. Normal
/// block layout creates the later descendant boundary, where the principal's
/// automatic block size remains cyclic.
/// <https://drafts.csswg.org/css-position-3/#abspos-layout>
/// <https://drafts.csswg.org/css-sizing-3/#behaving-as-auto>
/// <https://www.w3.org/TR/css-writing-modes-4/#dimension-mapping>
#[derive(Debug, Clone, Copy)]
struct PositionedAutoBlockMeasurementPrincipalContext {
principal_block_size_basis: BlockSizePercentageBasis,
child_available_space: ChildAvailableSpace,
}
impl PositionedAutoBlockMeasurementPrincipalContext {
fn new(
containing_writing_mode: WritingMode,
space: PositionedAutoBlockMeasurementSpace,
) -> Self {
Self {
// CSS Positioned Layout resolves percentages on this principal
// against the absolute-position containing block. The principal
// formatting context then establishes its own descendant basis.
principal_block_size_basis: PercentageBasis::definite_from(
space.available_physical_height.content_box_length(),
BlockSizeBasisSource::AbsolutePositioned,
),
child_available_space: ChildAvailableSpace::new(
containing_writing_mode,
space.content_width,
true,
Some(space.available_physical_height),
space.available_physical_height,
),
}
}
}
impl<'a> LayoutBuilder<'a> {
pub(in crate::layout) fn measure_auto_positioned_block_height(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
measurement_space: PositionedAutoBlockMeasurementSpace,
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
table_fragment: Option<&box_tree::TableFragment<'_>>,
) -> ContentBoxLength {
let vertical_border_width_for_positioning =
self.positioned_vertical_border_width(element, style, stylesheets, table_fragment);
let principal_context = PositionedAutoBlockMeasurementPrincipalContext::new(
self.containing_block_writing_mode,
measurement_space,
);
self.with_speculative_layout(|layout| {
// Flex and grid intrinsic sizing may recursively use this same
// surrogate for an in-flow descendant. The transaction restores
// the enclosing measurement mode and owns all descendant output.
layout.layout_pass_kind = LayoutPassKind::PositionedAutoSizeMeasurement;
layout.content_left = 0.0;
layout.content_right = measurement_space
.content_width
.points()
.max(style.font_size);
let start_page_index = layout.pages.len();
let start_page_context = layout.current_page_context;
layout.cursor_y = layout.page_top();
// A horizontal auto-height measurement may traverse arbitrary page
// fragments, so its synthetic block-axis extent remains effectively
// unbounded. In a vertical writing mode, however, physical height is
// the logical inline axis. It is the definite inline containing size
// used to fit the positioned box's lines; replacing it with the
// measurement sentinel would make a one-glyph abspos box stretch to
// that sentinel instead of shrink-wrapping to its containing block.
// <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-flows>
// <https://www.w3.org/TR/css-position-3/#abspos-layout>
let measurement_height = if style.writing_mode.has_vertical_lines() {
measurement_space
.available_physical_height
.points()
.max(0.0)
} else {
10_000.0
};
let containing_block = layout.positioned_containing_block_context(
ContainingBlock::from_page_top_rect(PageTopRect::new(
layout.content_left,
layout.cursor_y,
layout.content_right - layout.content_left,
measurement_height,
)),
);
layout.containing_blocks.push(containing_block);
// Scope the established absolute-position containing block for the
// principal. Entering its automatic formatting context creates the
// separate descendant basis, preserving cyclic descendant percentages.
layout
.block_percentage_context_stack
.push_percentage_basis(principal_context.principal_block_size_basis);
layout
.child_available_space_stack
.push(principal_context.child_available_space);
// Match final absolute-positioned replay: the box is an independent
// block formatting context, so ambient source-float exclusions cannot
// inflate its measured auto height.
// <https://www.w3.org/TR/CSS22/visuren.html#dis-pos-flo>
layout.push_float_context();
// Automatic abspos sizing measures the principal formatting context,
// not the sequence of fragmentainers that its final layout may later
// occupy. Suppress both forced and automatic fragmentation here so a
// descendant `break-after` cannot turn page-area gaps into used
// content height.
// <https://drafts.csswg.org/css-position-3/#abspos-layout>
layout.fragmentation_suppression_depth += 1;
layout.layout_element_inner(
element,
style,
stylesheets,
&[],
child_boxes,
table_fragment,
);
layout.fragmentation_suppression_depth -= 1;
layout.pop_float_context();
layout.child_available_space_stack.pop();
layout.block_percentage_context_stack.pop();
layout.containing_blocks.pop();
let consumed = layout.positioned_measurement_fragmented_block_extent(
start_page_index,
start_page_context,
);
// CSS 2.2 absolute positioning equations use content height as the
// `height` term and add padding/borders separately. Collapsed table
// borders contribute resolved outer grid insets rather than authored
// full border widths, so use the same vertical non-content size that
// will be used by the absolute-position equation.
content_box_pt(
(consumed
- style.padding.top
- style.padding.bottom
- vertical_border_width_for_positioning)
.max(0.0),
)
})
}
/// Returns the continuous block-axis extent traversed by a positioned
/// auto-height measurement that crossed rollback-only fragmentainers.
///
/// Descendant break controls still affect the final fragments of an
/// absolutely positioned box. The measurement therefore retains their
/// continuous geometric advance while the enclosing snapshot discards the
/// temporary pages and all page-local output.
/// <https://drafts.csswg.org/css-position-3/#fragmenting-absolutely-positioned-elements>
/// <https://drafts.csswg.org/css-break-3/#break-between>
fn positioned_measurement_fragmented_block_extent(
&mut self,
start_page_index: usize,
start_page_context: PageContext,
) -> f32 {
let completed_page_area_height = if self.pages.len() <= start_page_index {
0.0
} else {
let later_page_sizes = self
.pages
.iter()
.skip(start_page_index + 1)
.map(|page| PageSize::from_points(page.width(), page.height()))
.collect::<Vec<_>>();
let mut height = start_page_context.area_height();
for (offset, page_size) in later_page_sizes.into_iter().enumerate() {
let page_index = start_page_index + offset + 1;
height += self
.finished_page_context(page_index + 1, page_size)
.area_height();
}
height
};
completed_page_area_height + (self.page_top() - self.cursor_y).max(0.0)
}
pub(in crate::layout) fn positioned_vertical_border_width(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
table_fragment: Option<&box_tree::TableFragment<'_>>,
) -> f32 {
if is_html_table_element(element) {
self.collapsed_table_outer_vertical_insets(style, stylesheets, table_fragment)
.map(NonContentLength::points)
.unwrap_or_else(|| vertical_border_width(style))
} else {
vertical_border_width(style)
}
}
pub(in crate::layout) fn page_containing_block(&self) -> ContainingBlock {
ContainingBlock::from_page_top_rect(PageTopRect::new(
self.page_left(),
self.page_top(),
self.page_area_width(),
// Out-of-flow layout suppresses fragmentation while collecting
// descendants, but it must not enlarge the initial containing
// block. Its physical dimensions are always the current page
// area's dimensions, including for percentage block sizes:
// <https://www.w3.org/TR/css-display-3/#initial-containing-block>
// and <https://www.w3.org/TR/css-position-3/#def-cb>.
self.current_page_context.area_height(),
))
// This is the initial containing block, whose rectangle is established
// by the first page area. Explicit absolute offsets remain anchored to
// it even when ordinary flow has generated later pages before this
// out-of-flow element is collected. CSS Positioned Layout resolves the
// box against that containing block as a continuous flow, then permits
// printers latitude when its resolved position crosses pages. Any
// WeasyPrint-style source-page anchoring must therefore be an explicit
// compatibility policy, never an implicit replacement here. Auto static
// positions retain their source fragment separately in
// `layout_positioned_block`.
// <https://www.w3.org/TR/css-page-3/#page-model>
// <https://drafts.csswg.org/css-position-3/#fragmentation>
.on_page(0)
}
pub(in crate::layout) fn current_containing_block(&self) -> ContainingBlock {
self.containing_blocks
.last()
.map(|context| context.geometry)
.unwrap_or_else(|| self.page_containing_block())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn measurement_space() -> PositionedAutoBlockMeasurementSpace {
PositionedAutoBlockMeasurementSpace {
content_width: PhysicalContentWidth::new(content_box_pt(320.0)),
available_physical_height: PhysicalContentHeight::new(content_box_pt(180.0)),
}
}
#[test]
fn horizontal_principal_measurement_exports_absolute_containing_block_bases() {
let context = PositionedAutoBlockMeasurementPrincipalContext::new(
WritingMode::HorizontalTb,
measurement_space(),
);
assert!(context.principal_block_size_basis.is_definite());
assert_eq!(context.principal_block_size_basis.points(), Some(180.0));
assert!(context.child_available_space.physical_width_is_definite);
assert!(
context
.child_available_space
.physical_height_percentage_basis()
.is_definite()
);
assert_eq!(
context
.child_available_space
.available_physical_height()
.points(),
180.0
);
}
#[test]
fn vertical_principal_measurement_projects_absolute_containing_block_bases() {
let context = PositionedAutoBlockMeasurementPrincipalContext::new(
WritingMode::HorizontalTb,
measurement_space(),
);
assert_eq!(
context.child_available_space.writing_mode,
WritingMode::HorizontalTb
);
assert!(context.principal_block_size_basis.is_definite());
assert!(context.child_available_space.physical_width_is_definite);
assert_eq!(
context
.child_available_space
.available_physical_height()
.points(),
180.0
);
assert_eq!(
context
.child_available_space
.logical_inline_size_for(WritingMode::VerticalLr)
.points(),
180.0
);
assert!(
context
.child_available_space
.physical_height_percentage_basis()
.is_definite()
);
assert!(
context
.child_available_space
.logical_inline_percentage_basis_for(WritingMode::VerticalLr)
.is_definite()
);
}
#[test]
fn automatic_principal_establishes_an_indefinite_descendant_block_basis() {
let context = PositionedAutoBlockMeasurementPrincipalContext::new(
WritingMode::HorizontalTb,
measurement_space(),
);
let principal = ComputedStyle::initial();
let descendant_space = crate::layout::block::child_available_space_for_block(
&principal,
measurement_space().content_width,
None,
context.child_available_space.orthogonal_available_height,
measurement_space().available_physical_height,
);
assert!(context.principal_block_size_basis.is_definite());
assert!(
!descendant_space
.physical_height_percentage_basis()
.is_definite()
);
}
}