use std::num::NonZeroUsize;
use super::*;
pub(in crate::layout) fn multicol_gap_decoration_primitives(
style: &ComputedStyle,
content_left: f32,
content_top: f32,
content_bottom: f32,
column_width: f32,
gap: f32,
column_count: usize,
) -> Vec<PaintPrimitive> {
let topology = multicol_gap_topology_for_row(MulticolGapTopologyRowInput {
style,
content_left,
content_top,
column_height: (content_top - content_bottom).max(0.0),
inline_size: (column_width * column_count as f32
+ gap * column_count.saturating_sub(1) as f32)
.max(0.0),
column_width,
column_gap: gap,
column_count,
row: MulticolumnRowIndex::new(0),
previous_row_gap: None,
boundary: MulticolRowBoundary::Final,
});
gap_decoration_primitives_for_topology(style, &topology)
}
#[derive(Clone, Copy)]
pub(in crate::layout) struct MulticolGapTopologyRowInput<'a> {
pub(in crate::layout) style: &'a ComputedStyle,
pub(in crate::layout) content_left: f32,
pub(in crate::layout) content_top: f32,
pub(in crate::layout) column_height: f32,
pub(in crate::layout) inline_size: f32,
pub(in crate::layout) column_width: f32,
pub(in crate::layout) column_gap: f32,
pub(in crate::layout) column_count: usize,
pub(in crate::layout) row: MulticolumnRowIndex,
pub(in crate::layout) previous_row_gap: Option<f32>,
pub(in crate::layout) boundary: MulticolRowBoundary,
}
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) enum MulticolRowBoundary {
Final,
FollowedBy { gap: f32, slot: GapRuleSlot },
}
pub(in crate::layout) fn multicol_gap_topology_for_row(
input: MulticolGapTopologyRowInput<'_>,
) -> ResolvedGapTopology {
let MulticolGapTopologyRowInput {
style,
content_left,
content_top,
column_height,
inline_size,
column_width,
column_gap,
column_count,
row,
previous_row_gap,
boundary,
} = input;
let column_height = column_height.max(0.0);
let following_row_gap = match boundary {
MulticolRowBoundary::Final => None,
MulticolRowBoundary::FollowedBy { gap, .. } => Some(gap.max(0.0)),
};
let mut topology = ResolvedGapTopology::multicol(GapDecorationContainer::new(
content_left,
content_top,
inline_size.max(0.0),
column_height + following_row_gap.unwrap_or(0.0),
));
let column_segment = GapAxisSpan::new(0.0, column_height);
let column_rule_sequence = NonZeroUsize::new(column_count.saturating_sub(1));
for physical_index in 0..column_count.saturating_sub(1) {
let start = (column_width + column_gap) * physical_index as f32 + column_width;
let rule_index = if physical_column_rule_sequence_is_reversed(style) {
column_count
.saturating_sub(2)
.saturating_sub(physical_index)
} else {
physical_index
};
let portion = MulticolGapPortion::new(
row,
GapBand {
start,
end: start + column_gap,
grid_line: None,
segment_range: Some(column_segment),
rule_index: None,
},
GapRuleSlot::new(
GapRuleIndex::new(rule_index),
column_rule_sequence.expect("a multicolumn gap has a rule sequence"),
)
.expect("physical multicol gap index is inside its rule sequence"),
);
topology.column_gaps.push(portion.band);
topology.row_crossings.push(portion.band.band);
}
if let Some(previous_gap) = previous_row_gap.filter(|gap| *gap >= 0.0) {
topology.column_crossings.push(GapBand {
start: -previous_gap,
end: 0.0,
grid_line: None,
segment_range: Some(GapAxisSpan::new(0.0, inline_size)),
rule_index: None,
});
}
if let MulticolRowBoundary::FollowedBy { slot, .. } = boundary {
let next_gap = following_row_gap.expect("following row boundary carries its gap");
let row_gap = GapBand {
start: column_height,
end: column_height + next_gap,
grid_line: None,
segment_range: Some(GapAxisSpan::new(0.0, inline_size)),
rule_index: None,
};
topology.column_crossings.push(row_gap);
topology.row_gaps.push(AssignedGapBand::new(row_gap, slot));
}
topology
}
pub(in crate::layout) fn multicol_decorated_column_count(
style: &ComputedStyle,
occupied: usize,
available: usize,
) -> usize {
match style.column_rule.visibility_items {
css::GapRuleVisibilityItems::All
if matches!(style.column_count, css::ColumnCount::Auto) =>
{
occupied.min(available)
}
css::GapRuleVisibilityItems::All => available,
css::GapRuleVisibilityItems::Around if occupied > 0 => (occupied + 1).min(available),
css::GapRuleVisibilityItems::Normal
| css::GapRuleVisibilityItems::Between
| css::GapRuleVisibilityItems::Around => occupied.min(available),
}
}
#[allow(clippy::too_many_arguments)]
pub(in crate::layout) fn multicol_row_gap_decoration_primitives(
style: &ComputedStyle,
content_left: f32,
content_top: f32,
inline_size: f32,
column_height: f32,
row_gap: f32,
row_index: usize,
row_gap_count: usize,
column_width: f32,
column_gap: f32,
column_count: usize,
) -> Vec<PaintPrimitive> {
if style.visibility != Visibility::Visible || row_gap < 0.0 {
return Vec::new();
}
let Some(rule_sequence) = NonZeroUsize::new(row_gap_count) else {
return Vec::new();
};
let row_start = column_height + row_index as f32 * (column_height + row_gap);
let row_rule_index = if style.writing_mode.ltr_inline_progresses_upward() {
row_gap_count.saturating_sub(1).saturating_sub(row_index)
} else {
row_index
};
let row_gap_band = GapBand {
start: row_start,
end: row_start + row_gap,
grid_line: None,
segment_range: Some(GapAxisSpan::new(0.0, inline_size)),
rule_index: None,
};
let row_crossings = (0..column_count.saturating_sub(1))
.map(|index| {
let start = (column_width + column_gap) * index as f32 + column_width;
GapBand {
start,
end: start + column_gap,
grid_line: None,
segment_range: None,
rule_index: None,
}
})
.collect::<Vec<_>>();
let mut topology = ResolvedGapTopology::multicol(GapDecorationContainer::new(
content_left,
content_top,
inline_size,
row_start + row_gap,
));
let Some(row_slot) = GapRuleSlot::new(GapRuleIndex::new(row_rule_index), rule_sequence) else {
return Vec::new();
};
topology
.row_gaps
.push(AssignedGapBand::new(row_gap_band, row_slot));
topology.row_crossings = row_crossings;
gap_decoration_primitives_for_topology(style, &topology)
}