use super::*;
use crate::layout::flex::layout::placed_flex_item_style;
use crate::units::{Definite, border_box_to_content_box_length, content_box_to_border_box_length};
impl<'a> LayoutBuilder<'a> {
pub(super) fn measure_final_normal_flow_line_box_spans(
&mut self,
states: &mut [FlexItemSizingState],
children: &[StyledChild<'_>],
container_style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
available: FlexAvailableSpace,
) {
const SCRATCH_TOP: f32 = 1.0;
let direction = PhysicalFlexDirection::new(physical_flex_direction(container_style));
for (state, child) in states.iter_mut().zip(children) {
if flex_item_is_collapsed(&child.style) {
continue;
}
if child.style.writing_mode.has_vertical_lines() {
continue;
}
let estimate = state.estimate();
let replay_dimensions = state.allocation().replay_dimensions();
let mut replay_style = child.style.clone();
freeze_replayed_item_padding(
&mut replay_style,
flex_item_used_padding(&child.style, container_style, available),
);
let measurement_mode = FinalNormalFlowMeasurementMode::for_item(
&child.style,
physical_flex_direction(container_style),
estimate.main_size_provenance,
child.is_replaced_element(),
available
.definite_cross_size(physical_flex_direction(container_style))
.is_some(),
);
let mut placed_style = placed_flex_item_style(
&replay_style,
replay_dimensions.border_box_width(),
replay_dimensions.border_box_height(),
direction,
);
measurement_mode.prepare_placed_style(&mut placed_style, &replay_style);
let percentage_height_basis = flex_item_final_percentage_height_basis(
state.allocation(),
estimate,
child,
container_style,
physical_flex_direction(container_style),
available,
);
let span = self.with_speculative_layout(|layout| {
layout.with_placed_formatting_context(
PlacedFormattingContext {
content_left: 0.0,
content_width: replay_dimensions.available_width_for_replay(),
content_height: (!measurement_mode.measures_automatic_block_size()).then(
|| Definite::new(replay_dimensions.available_height_for_replay()),
),
table_wrapper_border_box_block_size: (!measurement_mode
.measures_automatic_block_size())
.then(|| {
auto_table_wrapper_block_size_override(
&child.style,
replay_dimensions.border_box_height(),
)
})
.flatten(),
replay_logical_inline_size: child
.anonymous_content()
.is_some()
.then(|| {
replay_dimensions
.logical_inline_size_for_replay(WritingMode::HorizontalTb, None)
})
.flatten()
.or_else(|| {
Some(
replay_dimensions
.logical_inline_content_size_for_replay(&placed_style),
)
}),
cursor_y: SCRATCH_TOP,
page_start_margin_policy: PageStartMarginPolicy::Suppress,
float_scope: ReplayFloatScope::IsolatedFormattingContext,
},
&placed_style,
|layout| {
layout.layout_flex_item_contents(
child,
&placed_style,
stylesheets,
percentage_height_basis,
PrincipalBoxPaintMode::RootPaints,
);
final_normal_flow_content_block_span(
border_box_pt((SCRATCH_TOP - layout.cursor_y).max(0.0)),
&placed_style,
)
},
)
});
state.estimate_mut().set_normal_flow_line_box_span(span);
}
}
}
pub(in crate::layout::flex) fn final_normal_flow_content_block_span(
replayed_border_box_span: BorderBoxLength,
placed_style: &ComputedStyle,
) -> PhysicalContentHeight {
PhysicalContentHeight::new(border_box_to_content_box_length(
replayed_border_box_span,
non_content_pt(
placed_style.padding.top
+ placed_style.padding.bottom
+ vertical_border_width(placed_style),
),
))
}
pub(in crate::layout::flex) fn apply_final_normal_flow_item_block_spans(
items: &mut [FlexItemLayout],
estimates: &mut [FlexItemEstimate],
children: &[StyledChild<'_>],
container_style: &ComputedStyle,
physical_direction: FlexDirection,
cross_size_is_definite: bool,
) {
for ((item, estimate), child) in items.iter_mut().zip(estimates).zip(children) {
if !final_normal_flow_block_span_replaces_provisional_height_with_cross_definiteness(
&child.style,
physical_direction,
estimate.main_size_provenance,
cross_size_is_definite,
) {
continue;
}
let Some(span) = estimate.normal_flow_line_box_span() else {
continue;
};
if physical_direction.is_row_axis() {
estimate.replace_row_cross_metrics_with_final_normal_flow_span(span);
}
let baseline_participant = flex_baseline_set(&child.style, container_style).is_some()
&& !flex_item_has_auto_cross_margin(&child.style, physical_direction)
&& flex_item_baseline_axis_is_parallel_to_main_axis(&child.style, physical_direction);
if baseline_participant {
let border_box_span = content_box_to_border_box_length(
span.content_box_length(),
non_content_pt(
child.style.padding.top
+ child.style.padding.bottom
+ vertical_border_width(&child.style),
),
);
item.set_height(FlexPhysicalVerticalSize::new(border_box_span.points()));
}
}
}
pub(in crate::layout::flex) fn final_normal_flow_block_span_replaces_provisional_height_with_cross_definiteness(
style: &ComputedStyle,
physical_direction: FlexDirection,
main_size_provenance: FlexMainSizeProvenance,
cross_size_is_definite: bool,
) -> bool {
physical_direction.is_row_axis()
&& style.box_values.height.is_auto()
&& main_size_provenance.permits_final_normal_flow_block_span()
&& !(cross_size_is_definite
&& (!style.box_values.min_height.is_auto() || !style.box_values.max_height.is_auto()))
}
pub(in crate::layout::flex) fn assign_flex_item_percentage_height_bases(
states: &mut [FlexItemSizingState],
children: &[StyledChild<'_>],
container_style: &ComputedStyle,
physical_direction: FlexDirection,
available: FlexAvailableSpace,
) {
for (state, child) in states.iter_mut().zip(children) {
let basis = flex_item_final_percentage_height_basis(
state.allocation(),
state.estimate(),
child,
container_style,
physical_direction,
available,
);
let item = state.allocation_mut();
item.percentage_height_basis = basis;
}
}
pub(in crate::layout::flex) fn flex_item_final_percentage_height_basis(
item: &FlexItemLayout,
estimate: FlexItemEstimate,
child: &StyledChild<'_>,
container_style: &ComputedStyle,
physical_direction: FlexDirection,
available: FlexAvailableSpace,
) -> FlexPercentageBasis {
let vertical_non_content = non_content_pt(
child.style.padding.top + child.style.padding.bottom + vertical_border_width(&child.style),
);
if physical_direction.is_row_axis()
&& used_content_box_height_or_auto_with_basis(
&child.style,
available.height_basis,
vertical_non_content,
)
.is_some()
{
return flex_item_replay_percentage_height_basis(
&child.style,
item.border_box_height(),
FlexDefiniteSizeSource::ResolvedLineCrossSize,
);
}
if physical_direction.is_row_axis() && available.height_basis.is_definite() {
return flex_item_replay_percentage_height_basis(
&child.style,
item.border_box_height(),
FlexDefiniteSizeSource::ResolvedLineCrossSize,
);
}
if physical_direction.is_column_axis() && available.height_basis.is_definite() {
return flex_item_replay_percentage_height_basis(
&child.style,
item.border_box_height(),
FlexDefiniteSizeSource::PostFlexingMainSizeFromDefiniteContainer,
);
}
if physical_direction.is_column_axis()
&& (estimate.main_size_provenance.is_definite()
|| definite_post_flexing_main_size(&child.style, physical_direction, available)
.is_some())
{
return flex_item_replay_percentage_height_basis(
&child.style,
item.border_box_height(),
FlexDefiniteSizeSource::PostFlexingMainSizeFromDefiniteFlexBase,
);
}
if physical_direction.is_row_axis()
&& let Some(stretched_height) = stretched_flex_item_cross_size(
&child.style,
container_style,
physical_direction,
available,
)
{
return flex_item_replay_percentage_height_basis(
&child.style,
border_box_pt(stretched_height.points()),
FlexDefiniteSizeSource::StretchedCrossSizeFromDefiniteSingleLineContainer,
);
}
if physical_direction.is_row_axis()
&& child.element_parts().is_some()
&& child.style.box_values.height.is_auto()
&& !flex_item_has_auto_cross_margin(&child.style, physical_direction)
&& matches!(
effective_align_self(&child.style, container_style).keyword,
SelfAlignmentKeyword::Auto
| SelfAlignmentKeyword::Normal
| SelfAlignmentKeyword::Stretch
)
{
return flex_item_replay_percentage_height_basis(
&child.style,
item.border_box_height(),
FlexDefiniteSizeSource::StretchedCrossSizeFromResolvedLine,
);
}
PercentageBasis::indefinite()
}