use std::collections::HashMap;
use std::ops::Range;
use f_office_units::Length;
use crate::body::GapMinimum;
use crate::grid::Device;
use crate::lines::Piece;
use crate::params;
use crate::text::{FaceIndex, Shaper};
pub(in crate::layout) fn bar_edges(device: &Device, size: Length) -> (Length, Length) {
let at = |(units, em): (i64, i64)| (size * units).div_round(em);
let (axis, thickness) = (
at(params::MATH_AXIS_HEIGHT),
at(params::MATH_FRACTION_RULE_THICKNESS),
);
let half = thickness.div_toward_zero(2);
(device.snap(axis + half), device.snap(axis - half))
}
pub(in crate::layout) struct Stacked {
pub(in crate::layout) bar: usize,
pub(in crate::layout) minimum: GapMinimum,
pub(in crate::layout) numerator: Range<usize>,
pub(in crate::layout) denominator: Range<usize>,
}
pub(in crate::layout) fn keep(
(shaper, device): (&mut Shaper, Device),
pieces: &mut [Piece],
stacked: &Stacked,
) {
let Some(&Piece::Bar { face, .. }) = pieces.get(stacked.bar) else {
return;
};
let Some(bar) = shaper.faces().get(face) else {
return;
};
let base = device.shift_dots(bar.rise);
let (top, bottom) = bar_edges(&device, bar.size);
let (units, em) = match stacked.minimum {
GapMinimum::Display => params::MATH_FRACTION_DISPLAY_GAP_MIN,
GapMinimum::Line => params::MATH_FRACTION_GAP_MIN,
};
let minimum = (bar.size * units).div_round(em);
let numerator = stacked.numerator.clone();
if let Some((low, _)) = ink(
shaper,
&device,
pieces.get(numerator.clone()).unwrap_or(&[]),
) {
let short = minimum - (low - base - top);
if short > Length::ZERO
&& let Some(part) = pieces.get_mut(numerator)
{
move_part((shaper, device), part, short);
}
}
let denominator = stacked.denominator.clone();
if let Some((_, high)) = ink(
shaper,
&device,
pieces.get(denominator.clone()).unwrap_or(&[]),
) {
let short = minimum - (base + bottom - high);
if short > Length::ZERO
&& let Some(part) = pieces.get_mut(denominator)
{
move_part((shaper, device), part, -short);
}
}
}
pub(super) fn ink(shaper: &Shaper, device: &Device, pieces: &[Piece]) -> Option<(Length, Length)> {
extent(shaper, device, pieces, Clear::Never)
}
pub(super) fn boxed(
shaper: &Shaper,
device: &Device,
pieces: &[Piece],
) -> Option<(Length, Length)> {
extent(shaper, device, pieces, Clear::Every)
}
pub(super) fn boxed_on_line(
shaper: &Shaper,
device: &Device,
pieces: &[Piece],
in_fraction: bool,
) -> Option<(Length, Length)> {
extent(shaper, device, pieces, Clear::InFraction(in_fraction))
}
#[derive(Clone, Copy)]
enum Clear {
Never,
Every,
InFraction(bool),
}
impl Clear {
fn holds(self, shaper: &Shaper, face: FaceIndex) -> bool {
match self {
Self::Never => false,
Self::Every => true,
Self::InFraction(inside) => shaper
.faces()
.get(face)
.is_some_and(|f| f.math.in_fraction == inside),
}
}
}
fn extent(
shaper: &Shaper,
device: &Device,
pieces: &[Piece],
clear: Clear,
) -> Option<(Length, Length)> {
pieces
.iter()
.filter_map(|piece| {
let (face, (low, high)) = match piece {
Piece::Glyph(glyph) => (glyph.face, shaper.ink(glyph)?),
Piece::Vinculum {
face,
top,
thickness,
clear: above,
..
} if *thickness > Length::ZERO => {
let above = if clear.holds(shaper, *face) {
*above
} else {
Length::ZERO
};
(*face, (*top - *thickness, *top + above))
}
_ => return None,
};
let base = device.shift_dots(shaper.faces().get(face)?.rise);
Some((base + low, base + high))
})
.reduce(|(low, high), (l, h)| (low.min(l), high.max(h)))
}
pub(super) fn move_part((shaper, device): (&mut Shaper, Device), pieces: &mut [Piece], by: Length) {
move_faces((shaper, device), pieces, by, Shaper::moved);
}
pub(super) fn lift_part((shaper, device): (&mut Shaper, Device), pieces: &mut [Piece], by: Length) {
move_faces((shaper, device), pieces, by, Shaper::lifted);
}
pub(super) fn lift_part_with_leading(
(shaper, device): (&mut Shaper, Device),
pieces: &mut [Piece],
by: Length,
leading: Length,
) {
let by = device.snap(by);
if by == Length::ZERO {
return;
}
let mut faces = HashMap::new();
for piece in pieces {
let face = match piece {
Piece::Glyph(glyph) => &mut glyph.face,
Piece::Bar { face, .. }
| Piece::Vinculum { face, .. }
| Piece::Diagonal { face, .. }
| Piece::Strut(face) => face,
_ => continue,
};
*face = *faces
.entry(*face)
.or_insert_with(|| shaper.lifted_with_leading(*face, by, leading));
}
}
fn move_faces<'a>(
(shaper, device): (&mut Shaper<'a>, Device),
pieces: &mut [Piece],
by: Length,
mover: fn(&mut Shaper<'a>, FaceIndex, Length) -> FaceIndex,
) {
let by = device.snap(by);
if by == Length::ZERO {
return;
}
let mut faces = HashMap::new();
for piece in pieces {
let face = match piece {
Piece::Glyph(glyph) => &mut glyph.face,
Piece::Bar { face, .. }
| Piece::Vinculum { face, .. }
| Piece::Diagonal { face, .. }
| Piece::Strut(face) => face,
_ => continue,
};
*face = *faces
.entry(*face)
.or_insert_with(|| mover(shaper, *face, by));
}
}