use f_office_units::Length;
use super::gap_minimums;
use crate::body::GrownMark;
use crate::grid::Device;
use crate::lines::Piece;
use crate::params;
use crate::props::RunProps;
use crate::text::Shaper;
#[derive(Default)]
pub(in crate::layout) struct Grown {
open: Vec<OpenGrown>,
space: Option<usize>,
sub_formula: bool,
}
struct OpenGrown {
start: usize,
characters: Vec<Character>,
space: Option<usize>,
nested_from: Option<usize>,
nested: Vec<(Length, Length)>,
}
struct Character {
at: usize,
ch: char,
leading: Length,
}
pub(in crate::layout) fn mark(
(shaper, device): (&mut Shaper, Device),
(grown, pieces): (&mut Grown, &mut [Piece]),
mark: GrownMark,
props: &RunProps,
) {
match mark {
GrownMark::SpaceBefore => grown.space = pieces.len().checked_sub(1),
GrownMark::Start => grown.open.push(OpenGrown {
start: pieces.len(),
characters: Vec::new(),
space: grown.space.take(),
nested_from: None,
nested: Vec::new(),
}),
GrownMark::NestedStart | GrownMark::NestedEnd => {
if let Some(open) = grown.open.last_mut() {
nest((shaper, device), (open, pieces), mark);
}
}
GrownMark::Character(ch) => {
let leading = shaper.equation_leading(props);
if let (Some(grown), Some(leading)) = (grown.open.last_mut(), leading) {
grown.characters.push(Character {
at: pieces.len(),
ch,
leading,
});
}
}
GrownMark::End => {
if let Some(open) = grown.open.pop() {
grown.sub_formula = grow((shaper, device), pieces, &open);
if !grown.sub_formula {
hide(pieces, open.space);
}
}
}
GrownMark::SpaceAfter => {
if !grown.sub_formula {
let last = pieces.len().checked_sub(1);
hide(pieces, last);
}
}
}
}
fn nest(
(shaper, device): (&Shaper, Device),
(open, pieces): (&mut OpenGrown, &[Piece]),
mark: GrownMark,
) {
if mark == GrownMark::NestedStart {
open.nested_from = Some(pieces.len());
return;
}
let in_fraction = open
.characters
.first()
.and_then(|c| match pieces.get(c.at) {
Some(Piece::Glyph(glyph)) => shaper.faces().get(glyph.face),
_ => None,
})
.is_some_and(|face| face.math.in_fraction);
let nested = pieces.get(open.nested_from.take().unwrap_or(pieces.len())..);
if let Some(extent) =
gap_minimums::boxed_on_line(shaper, &device, nested.unwrap_or(&[]), in_fraction)
{
open.nested.push(extent);
}
}
pub(in crate::layout) fn hide(pieces: &mut [Piece], at: Option<usize>) {
if let Some(Piece::Glyph(glyph)) = at.and_then(|at| pieces.get_mut(at)) {
glyph.advance = Length::ZERO;
}
}
fn grow((shaper, device): (&mut Shaper, Device), pieces: &mut [Piece], grown: &OpenGrown) -> bool {
let characters: Vec<&Character> = grown
.characters
.iter()
.filter(|c| matches!(pieces.get(c.at), Some(Piece::Glyph(g)) if g.ch == c.ch))
.collect();
let mut ink: Option<(Length, Length)> = None;
let mut from = grown.start;
let ends = characters.iter().map(|c| c.at).chain([pieces.len()]);
for end in ends {
let part = gap_minimums::ink(shaper, &device, pieces.get(from..end).unwrap_or(&[]));
ink = match (ink, part) {
(Some((low, high)), Some((l, h))) => Some((low.min(l), high.max(h))),
(found, None) | (None, found) => found,
};
from = end + 1;
}
let Some((low, high)) = ink else {
return false;
};
let (axis, axis_em) = params::MATH_AXIS_HEIGHT;
let mut sub_formula = false;
for character in characters {
let Some(Piece::Glyph(glyph)) = pieces.get_mut(character.at) else {
continue;
};
let Some(face) = shaper.faces().get(glyph.face) else {
continue;
};
let axis = device.shift_dots(face.rise) + (face.size * axis).div_round(axis_em);
let need = (high - axis).max(axis - low) * 2;
let nested = grown
.nested
.iter()
.map(|&(low, high)| (high - axis).max(axis - low) * 2)
.fold(Length::ZERO, Length::max);
sub_formula |= shaper.grow(glyph, (need, nested), character.leading);
}
sub_formula
}
#[cfg(test)]
mod tests {
use crate::layout::testing::FakeMath;
use crate::lines::Piece;
use crate::ns::{M, W};
use crate::quirks::GrownDelimiters;
use crate::text::MathRoom;
fn parentheses(math: &str, grown: GrownDelimiters) -> Vec<(u32, i64, MathRoom)> {
let xml = format!(
r#"<w:hdr xmlns:w="{W}" xmlns:m="{M}"><w:p><m:oMath>{math}</m:oMath></w:p></w:hdr>"#
);
super::super::tests::measured(
&xml,
&FakeMath,
|p| p.with_variant(grown),
|layout, pieces| {
pieces
.iter()
.filter_map(|piece| match piece {
Piece::Glyph(glyph) if matches!(glyph.ch, '(' | ')') => {
let face = layout.shaper.faces().get(glyph.face)?;
Some((glyph.id, face.rise.units(), face.math.room))
}
_ => None,
})
.collect()
},
)
}
const AROUND_A_FRACTION: &str = "<m:d><m:e><m:f><m:num><m:r><m:t>x</m:t></m:r></m:num><m:den><m:r><m:t>y</m:t></m:r></m:den></m:f></m:e></m:d>";
#[test]
fn grows_the_parentheses_around_a_fraction_on_a_target_that_grows_them() {
let base = parentheses(AROUND_A_FRACTION, GrownDelimiters::BaseGlyph);
assert_eq!(
base,
[(0x28, 0, MathRoom::Lines), (0x29, 0, MathRoom::Lines)]
);
let grown = parentheses(AROUND_A_FRACTION, GrownDelimiters::Variant);
let ids: Vec<u32> = grown.iter().map(|p| p.0).collect();
assert!(ids == [8000, 8000] || ids == [8001, 8001], "{grown:?}");
assert_eq!(grown[0].1, grown[1].1);
assert!(grown.iter().all(|p| matches!(p.2, MathRoom::Ink { .. })));
}
#[test]
fn keeps_the_base_glyphs_around_line_high_content() {
let letter = "<m:d><m:e><m:r><m:t>x</m:t></m:r></m:e></m:d>";
let plain = parentheses(letter, GrownDelimiters::Variant);
assert_eq!(plain, parentheses(letter, GrownDelimiters::BaseGlyph));
assert_eq!(plain.iter().map(|p| p.0).collect::<Vec<_>>(), [0x28, 0x29]);
}
#[test]
fn grows_parentheses_around_parentheses_of_line_high_content() {
let nested = "<m:d><m:e><m:d><m:e><m:r><m:t>x</m:t></m:r></m:e></m:d></m:e></m:d>";
let ids: Vec<u32> = parentheses(nested, GrownDelimiters::Variant)
.iter()
.map(|p| p.0)
.collect();
assert_eq!(ids.get(1..3), Some(&[0x28, 0x29][..]), "{ids:?}");
assert!(matches!(ids.first(), Some(8000 | 8001)), "{ids:?}");
assert_eq!(ids.first(), ids.last());
}
}