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
use super::*;
impl<'a> LayoutBuilder<'a> {
/// Measures the content-size suggestion used by a flex item's automatic
/// main-axis minimum.
///
/// A definite preferred main size supplies the flex base, but Flexbox
/// measures the content-size suggestion with that preferred size
/// suppressed. This keeps the automatic-minimum input distinct from a
/// definite flex basis in both intrinsic contribution sizing and final
/// flexible-length resolution:
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>.
pub(in crate::layout::flex) fn estimate_flex_item_automatic_main_min_content(
&mut self,
child: &StyledChild<'_>,
stylesheets: &Stylesheets<'_>,
item_available: FlexItemAvailableSpace,
physical_direction: FlexDirection,
) -> Option<ContentBoxLength> {
let specified_min = if physical_direction.is_row_axis() {
child.style.box_values.min_width.clone()
} else {
child.style.box_values.min_height.clone()
};
flex_main_axis_content_based_minimum_kind(&specified_min, &child.style, physical_direction)
.is_some()
.then(|| {
let mut content_child = child.clone();
let mut content_item_available = item_available;
if physical_direction.is_row_axis() {
content_child.style.box_values.width =
css::ComputedLengthPercentageOrAuto::Auto;
} else {
content_child
.style
.box_values
.height
.replace_with_used(css::ComputedLengthPercentageOrAuto::Auto);
// The probe has removed the item's preferred physical main
// height. Its retained numeric constraint is still useful to
// the flex estimator, but it must no longer make descendant
// percentage heights definite.
content_item_available.make_height_percentage_basis_indefinite();
}
let content_estimate = self.estimate_flex_item_size(
&content_child,
stylesheets,
content_item_available,
physical_direction,
);
if physical_direction.is_row_axis() {
content_estimate.min_width
} else {
content_estimate.min_height
}
})
}
/// Estimates the hypothetical content size of a flex item.
///
/// CSS Flexbox defines flex base sizes and intrinsic contributions before
/// the flex formatting algorithm distributes free space. Inline
/// contributions are measured through `InlineOpportunityGraph` so flex
/// estimates use the same CSS Text break opportunities as inline layout:
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm>,
/// <https://www.w3.org/TR/css-flexbox-1/#intrinsic-sizes>, and
/// <https://www.w3.org/TR/css-text-3/#line-breaking>.
pub(in crate::layout::flex) fn estimate_flex_item_size(
&mut self,
child: &StyledChild<'_>,
stylesheets: &Stylesheets<'_>,
available: FlexItemAvailableSpace,
physical_direction: FlexDirection,
) -> FlexItemEstimate {
let vertical_non_content = non_content_pt(
child.style.padding.top
+ child.style.padding.bottom
+ vertical_border_width(&child.style),
);
let percentage_height_basis = flex_item_estimate_percentage_height_basis(
&child.style,
available,
vertical_non_content,
);
let mut estimate =
self.with_flex_item_percentage_height_basis(percentage_height_basis, |layout| {
layout.estimate_flex_item_size_with_percentage_basis(
child,
stylesheets,
available,
physical_direction,
)
});
if child
.element_parts()
.is_some_and(|(element, _, _)| used_property_containment(element, &child.style).layout)
{
// A layout-contained principal box exports no first/last baseline;
// its flex/grid parent must use the synthesized fallback from the
// border box instead.
// <https://www.w3.org/TR/css-contain-1/#containment-layout>
estimate.metrics.clear_block_baselines();
estimate.baselines = FlexItemBaselineEstimate::default();
}
estimate
}
pub(in crate::layout::flex) fn estimate_flex_item_size_with_percentage_basis(
&mut self,
child: &StyledChild<'_>,
stylesheets: &Stylesheets<'_>,
available: FlexItemAvailableSpace,
physical_direction: FlexDirection,
) -> FlexItemEstimate {
// Flex keeps the child's source style for descendant cascade. This
// intrinsic multicol probe is a layout consumer, so use a separate
// normalized multicol style for its tracks and balancing geometry.
let multicol_style = self.multicol_used_style(&child.style);
let style = &multicol_style;
let context = super::item_special_cases::FlexItemEstimateContext::new(
style,
available,
physical_direction,
);
let style = context.style;
let available = context.available;
if let Some(children) = child.anonymous_content() {
return self.estimate_anonymous_flex_item(children, stylesheets, context);
}
let Some((element, signature, child_boxes)) = child.element_parts() else {
return FlexItemEstimate::fixed(
PhysicalContentWidth::new(content_box_pt(0.0)),
PhysicalContentHeight::new(content_box_pt(0.0)),
);
};
if used_property_containment(element, style).size
&& replaced_element_kind(element).is_none()
{
return self.estimate_size_contained_flex_item(
element,
signature,
child_boxes,
stylesheets,
context,
);
}
if style.display.is_flex()
&& let Some(estimate) = self.estimate_nested_flex_container_item(
element,
signature,
child_boxes,
stylesheets,
context,
)
{
return estimate;
}
if style.display.inner == DisplayInner::Grid
&& let Some(estimate) =
self.estimate_grid_flex_item(element, signature, child_boxes, stylesheets, context)
{
return estimate;
}
let replaced_intrinsic = match replaced_element_kind(element) {
Some(ReplacedElementKind::Canvas) => Some(if element.tag == "iframe" {
intrinsic_iframe_size(element)
} else {
intrinsic_canvas_size(element)
}),
Some(ReplacedElementKind::Image) => intrinsic_image_size(
element,
style,
self.base_url,
self.root_url,
self.resource_cache,
)
.map(|image| image.replaced_size())
// A `<video>` without a poster still has a CSS replaced box. Its
// media frame is unavailable to this static PDF renderer, but
// Flexbox must use the HTML default object size for flex base and
// automatic-minimum sizing rather than treating it as an empty
// ordinary block.
// <https://html.spec.whatwg.org/multipage/media.html#the-video-element>
// <https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width>
.or_else(|| (element.tag == "video").then(intrinsic_unavailable_image_size)),
Some(ReplacedElementKind::Svg) => intrinsic_svg_size(element),
None => None,
};
if let Some(intrinsic) = replaced_intrinsic
&& let Some(size) = estimate_replaced_flex_item(
intrinsic,
style,
used_property_containment(element, style).size,
available.width,
available,
)
{
return size;
}
if has_direct_inline_replaced_child(element)
&& !has_direct_flow_child_with_font_metrics(
element,
style,
stylesheets,
&mut self.font_system,
)
{
return self.estimate_inline_replaced_row_flex_item(
element,
stylesheets,
context,
child_boxes,
);
}
self.estimate_normal_flow_flex_item(
child,
element,
signature,
child_boxes,
stylesheets,
context,
)
}
}