use f_office_units::scale::LINE;
use f_office_units::{Length, div_round, div_round_i128, saturated};
use super::face_of;
use crate::grid::{Device, LowBaseline, Rounding};
use crate::layout::decisions;
use crate::layout::{BaselineForm, Layout, Within};
use crate::lines::{Line, LineEnd, Piece, Placed};
use crate::params;
use crate::props::{LineSpacing, ParaProps};
use crate::text::{Face, FaceIndex, MathRoom};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::layout) struct LineBox {
pub(in crate::layout) ascent: Length,
pub(in crate::layout) descent: Length,
pub(in crate::layout) height: Length,
pub(in crate::layout) text_height: Length,
pub(in crate::layout) baseline: Length,
pub(in crate::layout) text_bottom: Length,
pub(in crate::layout) single: bool,
pub(in crate::layout) from_bottom: FromBottom,
pub(in crate::layout) multiple: bool,
pub(in crate::layout) after: Length,
pub(in crate::layout) top_room: Length,
pub(in crate::layout) rest: i64,
pub(in crate::layout) twips: Option<i64>,
pub(in crate::layout) centered: Option<super::centered::CenteredFace>,
pub(in crate::layout) grid_ties: Option<Rounding>,
}
pub(in crate::layout) const REST_PARTS: i64 = params::HEIGHT_PARTS_PER_POINT >> SHARED_TWOS;
const REST_PER_PART: i64 = Length::UNITS_PER_POINT >> SHARED_TWOS;
const SHARED_TWOS: u32 = {
let points = Length::UNITS_PER_POINT.trailing_zeros();
let parts = params::HEIGHT_PARTS_PER_POINT.trailing_zeros();
if points < parts { points } else { parts }
};
const _: () = assert!(params::HEIGHT_PARTS_PER_POINT.count_ones() == 1);
struct LineMetrics {
ascent: Length,
descent: Length,
rise: Length,
drop: Length,
whole_single: Option<i64>,
whole_rise: i64,
shift_room: Length,
face_room: (Length, Option<i64>),
size: Length,
}
fn whole_height(single: i64, once: i64, n: i64) -> (Length, i64) {
let exact = (div_round_i128(i128::from(single - once) * i128::from(n), i128::from(LINE))
+ i128::from(once))
* i128::from(REST_PER_PART);
let height = div_round_i128(exact, i128::from(REST_PARTS));
let rest = i64::try_from(exact - height * i128::from(REST_PARTS)).unwrap_or(0);
(Length::from_units(saturated(height)), rest)
}
pub(in crate::layout) fn twips_rest(device: &Device, length: Length) -> Option<i64> {
if length.units() % Length::UNITS_PER_TWIP != 0 {
return None;
}
let twips = length.units() / Length::UNITS_PER_TWIP;
Some(device.twip_parts(twips).map_or(0, |parts| {
parts * REST_PER_PART - length.units() * REST_PARTS
}))
}
fn height_twips(device: &Device, height: Length, rest: i64) -> Option<i64> {
if rest == 0 {
twips_rest(device, height)
} else {
None
}
}
fn bottom_rest(size: &LineBox, twips: Option<i64>) -> i64 {
size.rest
+ match (twips, size.twips) {
(Some(_), Some(own)) => own,
(Some(above), None) => -above,
(None, _) => 0,
}
}
fn whole_spacing(rule: LineSpacing, single: Length) -> Option<i64> {
match rule {
LineSpacing::Auto(n) if n >= LINE => Some(n),
LineSpacing::AtLeast(h) if h <= single => Some(LINE),
_ => None,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct ExactPlace {
at: Length,
rest: i64,
}
impl ExactPlace {
fn carried(at: Length, rest: i64) -> Self {
let carry = div_round(rest, REST_PARTS);
Self {
at: at + Length::from_units(carry),
rest: rest - carry * REST_PARTS,
}
}
fn rested_dot(self, device: &Device) -> Option<Length> {
if self.rest == 0 {
return None;
}
Some(if device.half_dot_side(self.at).is_eq() {
let below = device.count(self.at, Rounding::Down);
device.dots(if self.rest > 0 { below + 1 } else { below })
} else {
device.snap(self.at)
})
}
}
impl std::ops::Add<Length> for ExactPlace {
type Output = Self;
fn add(self, length: Length) -> Self {
Self {
at: self.at + length,
rest: self.rest,
}
}
}
impl std::ops::Sub<Length> for ExactPlace {
type Output = Self;
fn sub(self, length: Length) -> Self {
Self {
at: self.at - length,
rest: self.rest,
}
}
}
impl LineBox {
pub(in crate::layout) fn step(&self) -> Length {
if self.multiple {
self.ascent + self.descent
} else {
self.height
}
}
pub(in crate::layout) fn low_multiple(&self) -> bool {
self.multiple && self.text_height < self.ascent + self.descent
}
pub(in crate::layout) fn least_extra(&self) -> Length {
if matches!(self.from_bottom, FromBottom::AtLeast) {
self.height - (self.ascent + self.descent)
} else {
Length::ZERO
}
}
pub(in crate::layout) fn multiple_extra(&self) -> Length {
if self.multiple {
(self.height - (self.ascent + self.descent)).max(Length::ZERO)
} else {
Length::ZERO
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::layout) enum FromBottom {
No,
Exact,
ExactSpaced,
AtLeast,
}
impl FromBottom {
fn of(rule: LineSpacing, excess: Length, single: Length) -> Self {
match rule {
LineSpacing::Exact(_) => Self::Exact,
LineSpacing::AtLeast(h) if excess <= Length::ZERO && h > single => Self::AtLeast,
_ => Self::No,
}
}
fn exact(self) -> bool {
matches!(self, Self::Exact | Self::ExactSpaced)
}
}
fn times_lines(length: Length, lines: i64) -> Length {
let product = i128::from(length.units()) * i128::from(lines);
Length::from_units(saturated(div_round_i128(product, i128::from(LINE))))
}
impl Layout<'_> {
fn box_after(&self, props: &ParaProps, border: Length, spaced: bool) -> Length {
let rule = props.line.unwrap_or(LineSpacing::Auto(LINE));
if matches!(rule, LineSpacing::Exact(_)) || matches!(rule, LineSpacing::Auto(n) if n < LINE)
{
border
} else {
self.device.held_after(
props.space_after() + border,
rule,
spaced && self.grid_pitch.is_none(),
self.column.cell.is_some(),
)
}
}
pub(in crate::layout) fn line_box_of(
&self,
line: &Line,
mark: (Option<FaceIndex>, bool),
props: &ParaProps,
) -> LineBox {
let rule = props.line.unwrap_or(LineSpacing::Auto(LINE));
let border = self.held_space_after(mark.1);
let spaced = mark.1 && self.kept_after(props, true) + border > Length::ZERO;
let LineMetrics {
ascent,
descent,
rise,
drop,
whole_single,
whole_rise,
shift_room,
face_room,
size,
} = self.line_metrics(line, mark);
let (ascent, descent, whole_single) =
self.turned_room((ascent, descent, whole_single), size);
let (descent, whole_single) = self
.centered_line(line, props)
.and_then(|centered| centered.descent)
.map_or((descent, whole_single), |descent| (descent, None));
let (ascent, rise, whole_single, lifted) =
self.object_rise(line, rule, (ascent, descent), (rise, whole_single));
let text = ascent + descent;
let (face_room, whole_face_room) = self.face_room_once(rule, face_room);
let rise = rise + self.room_once(rule, shift_room) + face_room;
let excess = self.object_excess(line, rule, ascent, descent);
let box_parts = (ascent, descent, rise);
let (mut height, mut baseline) = line_box(&self.device, box_parts, rule, (excess, spaced));
let (smartart_base, smartart_tall) =
self.smartart_baseline(rule, (ascent, descent, height, excess), baseline);
baseline = smartart_base;
let whole = self.whole_box(
(rule, text, excess),
(whole_single, whole_face_room, whole_rise),
height,
);
let whole_parts = whole.is_some();
let rest = whole.map_or(0, |(_, rest)| rest);
height = whole.map_or(height, |(whole, _)| whole);
let mut text_height = height;
height += below_text(rule, excess, drop);
self.adjust_at_least_drop(
line,
rule,
(ascent, descent, rise, excess, drop, spaced),
(&mut height, &mut text_height, &mut baseline),
);
if let LineSpacing::Auto(n) = rule
&& excess > Length::ZERO
&& holds_glyph_besides_white_space(line)
{
height = height.max(text_below_object(n, (ascent, descent), baseline));
}
height = self.object_alone_room(line, rule, (descent, excess), (height, baseline));
let depth = object_depth(line, rule);
height = reaching_below(height, baseline, (depth, above_single(rule, text - lifted)));
let (height, baseline, grid_ties) = self.on_grid(
rule,
(ascent, descent),
(excess, spaced),
(height, baseline),
);
let (single, multiple, text_bottom, after_ok) = self.line_box_tail(
line,
rule,
(ascent, descent, excess, drop, spaced),
(baseline, depth, mark.1, smartart_tall),
);
LineBox {
ascent,
descent,
height,
text_height,
baseline,
text_bottom,
single,
from_bottom: self.bottom_kind(rule, (excess, text + drop), spaced),
multiple,
after: self.box_after(props, border, after_ok),
top_room: self.held_borders.top,
rest,
twips: height_twips(&self.device, height, rest)
.or((whole_parts && height == text_height).then_some(0)),
centered: self.centered_face(line, props, excess),
grid_ties,
}
}
fn on_grid(
&self,
rule: LineSpacing,
(ascent, descent): (Length, Length),
(excess, spaced): (Length, bool),
(height, baseline): (Length, Length),
) -> (Length, Length, Option<Rounding>) {
let Some(pitch) = self.grid_pitch else {
return (height, baseline, None);
};
match rule {
LineSpacing::Auto(n) => self.on_grid_auto(
n,
pitch,
(ascent, descent),
(excess, spaced),
(height, baseline),
),
LineSpacing::AtLeast(min) => self.on_grid_at_least(
min,
pitch,
(ascent, descent),
(excess, spaced),
(height, baseline),
),
LineSpacing::Exact(_) => (height, baseline, None),
}
}
fn on_grid_auto(
&self,
n: i64,
pitch: Length,
(ascent, descent): (Length, Length),
(excess, spaced): (Length, bool),
(height, baseline): (Length, Length),
) -> (Length, Length, Option<Rounding>) {
let steps = |length: Length| (length.units() + pitch.units() - 1) / pitch.units();
let rounded = (n == LINE && excess <= Length::ZERO)
.then(|| {
decisions::grid_single_parts(
&self.context.quirks,
&self.device,
(ascent, descent),
pitch,
)
})
.flatten();
if let Some(((above, below), ties)) = rounded {
let parts = height - ascent - descent + above + below;
let grown = pitch * steps(height).max(1);
let added = self.device.dots(self.device.count(grown, ties)) - parts;
let baseline = above + self.device.snap_half_away(added.div_toward_zero(2));
(grown, baseline, Some(ties))
} else if n == LINE || excess > Length::ZERO {
let grown = pitch * steps(height).max(1);
(grown, baseline + (grown - height).div_toward_zero(2), None)
} else {
let text = ascent + descent;
let multiplied = times_lines(pitch, n);
let grown = multiplied.max(pitch * steps(text).max(1));
let baseline = text_baseline(&self.device, (ascent, descent), n, spaced)
+ (grown - text).div_toward_zero(2);
(grown, baseline, None)
}
}
fn on_grid_at_least(
&self,
min: Length,
pitch: Length,
(ascent, descent): (Length, Length),
(excess, spaced): (Length, bool),
(height, baseline): (Length, Length),
) -> (Length, Length, Option<Rounding>) {
let text = ascent + descent;
let natural = if excess <= Length::ZERO && min > text && height == min {
(
text,
text_baseline(&self.device, (ascent, descent), LINE, spaced),
)
} else {
(height, baseline)
};
let (grown, grid_base, grid_ties) =
self.on_grid_auto(LINE, pitch, (ascent, descent), (excess, spaced), natural);
if min <= grown {
return (grown, grid_base, grid_ties);
}
if let Some(ties) = grid_ties {
let grown_dots = self.device.dots(self.device.count(grown, ties));
let grid_descent = grown_dots - grid_base;
(min, self.device.snap(min) - grid_descent, None)
} else {
let grid_descent = grown - grid_base;
(min, min - grid_descent, None)
}
}
fn bottom_kind(
&self,
rule: LineSpacing,
(excess, text): (Length, Length),
spaced: bool,
) -> FromBottom {
match FromBottom::of(rule, excess, text) {
FromBottom::AtLeast if self.grid_pitch.is_some() => FromBottom::No,
FromBottom::Exact if spaced && self.column.cell.is_none() => self
.device
.spaced_exact_bottom(FromBottom::ExactSpaced)
.unwrap_or(FromBottom::Exact),
kind => kind,
}
}
fn whole_box(
&self,
(rule, text, excess): (LineSpacing, Length, Length),
(single, once, rise): (Option<i64>, Option<i64>, i64),
height: Length,
) -> Option<(Length, i64)> {
if excess > Length::ZERO || self.grid_pitch.is_some() {
return None;
}
self.device.whole_line_height(|| {
let n = whole_spacing(rule, text)?;
let (whole, whole_rest) = whole_height(single?, once? + rise, n);
if rise == 0 {
return Some((whole, whole_rest));
}
let exact = i128::from(whole.units()) * i128::from(REST_PARTS) + i128::from(whole_rest);
let kept = i128::from(height.units()) * i128::from(REST_PARTS);
Some((height, i64::try_from(exact - kept).unwrap_or(whole_rest)))
})
}
fn turned_room(
&self,
(ascent, descent, whole_single): (Length, Length, Option<i64>),
size: Length,
) -> (Length, Length, Option<i64>) {
let turned = matches!(self.column.within, Within::TurnedFrame(..));
let Some((part, whole)) = turned
.then(|| decisions::turned_line_room(&self.context.quirks))
.flatten()
else {
return (ascent, descent, whole_single);
};
let room = size * part;
let half = room.div_round(whole * 2);
let parts = room.units() * params::HEIGHT_PARTS_PER_POINT;
let per_part = whole * Length::UNITS_PER_POINT;
let whole_room = (parts % per_part == 0).then_some(parts / per_part);
(
ascent + half,
descent + (room.div_round(whole) - half),
whole_single
.zip(whole_room)
.map(|(single, room)| single + room),
)
}
fn held_space_after(&self, last: bool) -> Length {
if last {
self.held_borders.bottom
} else {
Length::ZERO
}
}
fn foot_border_room(&self, last: bool) -> Length {
if last {
self.held_borders.foot
} else {
Length::ZERO
}
}
fn line_metrics(&self, line: &Line, (mark, last): (Option<FaceIndex>, bool)) -> LineMetrics {
let only_blanks = line.placed.iter().all(is_blank);
let sizing: Vec<&Placed> = line
.placed
.iter()
.filter(|p| only_blanks || !is_blank(p))
.collect();
let apart = |p: &&&Placed| match p.piece {
Piece::Glyph(g) => {
g.label
|| self
.shaper
.faces()
.get(g.face)
.is_some_and(|f| f.math.in_fraction)
}
_ => false,
};
let faces_of = |pieces: &mut dyn Iterator<Item = &&Placed>| -> Vec<FaceIndex> {
pieces
.filter_map(|p| face_of(&p.piece))
.filter(|i| !self.inked(*i))
.collect()
};
let mut faces = faces_of(&mut sizing.iter());
let mut text = faces_of(&mut sizing.iter().filter(|p| !apart(p)));
let alone = text.is_empty();
let object_faces = self.object_run_faces(&sizing);
self.fill_empty_line_faces(
line,
&sizing,
(mark, alone),
&object_faces,
(&mut faces, &mut text),
);
let metric = |faces: &[FaceIndex], pick: fn(&Face) -> Length| {
faces
.iter()
.filter_map(|i| self.shaper.faces().get(*i))
.map(pick)
.max()
.unwrap_or(Length::ZERO)
};
if let Some(face) = blank_line_end_face(line.end, mark, last).filter(|_| only_blanks) {
faces = vec![face];
text = vec![face];
}
let ascent = metric(&faces, Face::raised_ascent);
let rise = (ascent - metric(&text, Face::raised_ascent)).max(Length::ZERO);
let below = [
text.clone(),
self.counted_label_faces(&sizing).unwrap_or_default(),
]
.concat();
let descent = metric(&below, Face::raised_descent);
let shift_room = (metric(&text, Face::shifted_ascent) - metric(&text, Face::ascent)
+ (metric(&text, Face::shifted_descent) - metric(&text, Face::descent)))
.max(Length::ZERO);
let parts: Vec<FaceIndex> = faces
.iter()
.copied()
.filter(|i| {
self.shaper
.faces()
.get(*i)
.is_some_and(|f| f.math.in_fraction && f.math.room != MathRoom::InkBelow)
})
.collect();
let depth = metric(&parts, Face::raised_descent).max(self.fraction_ink_depth(&sizing));
let (top, bottom) = self.operator_ink(&sizing);
let rise = rise.max(top - (ascent - rise));
let ascent = ascent.max(top);
let drop = (depth.max(bottom) - descent).max(Length::ZERO);
let plain = drop == Length::ZERO && ascent == metric(&faces, Face::raised_ascent);
let (whole_single, whole_text, whole_rise) =
self.whole_singles(plain, &faces, (&text, &below));
let (rise, whole_rise) = self.alone_rise(alone, &sizing, (rise, whole_rise));
let size = faces
.iter()
.filter_map(|i| self.shaper.faces().get(*i))
.map(|f| f.line_size)
.max()
.unwrap_or(Length::ZERO);
let mut metrics = LineMetrics {
ascent,
descent,
rise,
drop,
whole_single,
whole_rise,
shift_room,
face_room: self.face_room(&text, whole_text),
size,
};
if !alone && !object_faces.is_empty() {
self.apply_object_run_height(&mut metrics, &object_faces);
}
metrics
}
fn fill_empty_line_faces(
&self,
line: &Line,
sizing: &[&Placed],
(mark, alone): (Option<FaceIndex>, bool),
object_faces: &[FaceIndex],
(faces, text): (&mut Vec<FaceIndex>, &mut Vec<FaceIndex>),
) {
if alone && !object_faces.is_empty() {
faces.extend(object_faces.iter().copied());
text.extend(object_faces.iter().copied());
return;
}
let broken = match line.end {
LineEnd::Break(face) => face,
_ => None,
};
let empty = broken.or(mark);
if faces.is_empty() || (alone && self.label_only_line(sizing)) {
faces.extend(empty);
}
if text.is_empty() {
text.extend(empty);
}
if text.is_empty() {
text.clone_from(faces);
}
}
fn uses_object_run_faces(&self) -> bool {
self.settings
.mode_rules
.scales_object_lines_by_object_runs()
&& decisions::object_line_multiple(&self.context.quirks, || Some(())).is_some()
}
fn object_run_faces(&self, sizing: &[&Placed]) -> Vec<FaceIndex> {
if !self.uses_object_run_faces() {
return Vec::new();
}
sizing
.iter()
.filter_map(|p| match p.piece {
Piece::Object {
run_face: Some(face),
..
} if !self.inked(face) => Some(face),
_ => None,
})
.collect()
}
fn apply_object_run_height(&self, metrics: &mut LineMetrics, object_faces: &[FaceIndex]) {
let Some(tallest) = object_faces
.iter()
.filter_map(|i| self.shaper.faces().get(*i))
.max_by_key(|f| f.line_height())
else {
return;
};
let single = tallest.line_height();
if single > metrics.ascent + metrics.descent {
metrics.ascent = single - metrics.descent;
metrics.rise = Length::ZERO;
metrics.whole_single = tallest.whole_line().map(|(up, down)| up + down);
metrics.whole_rise = 0;
metrics.shift_room = Length::ZERO;
metrics.face_room = (Length::ZERO, Some(0));
metrics.size = metrics.size.max(tallest.line_size);
}
}
fn label_only_line(&self, sizing: &[&Placed]) -> bool {
let mut has_label = false;
for placed in sizing {
if let Piece::Glyph(g) = placed.piece {
if g.label {
has_label = true;
} else if self
.shaper
.faces()
.get(g.face)
.is_some_and(|f| f.math.in_fraction)
{
return false;
}
}
}
has_label
}
fn alone_rise(&self, alone: bool, sizing: &[&Placed], rise: (Length, i64)) -> (Length, i64) {
let label = || (sizing.iter()).any(|p| matches!(p.piece, Piece::Glyph(g) if g.label));
(alone && label())
.then(|| decisions::scaled_label_rise(&self.context.quirks, || (Length::ZERO, 0)))
.flatten()
.unwrap_or(rise)
}
fn inked(&self, i: FaceIndex) -> bool {
self.shaper
.faces()
.get(i)
.is_some_and(|f| matches!(f.math.room, MathRoom::Ink { .. }))
}
fn counted_label_faces(&self, sizing: &[&Placed]) -> Option<Vec<FaceIndex>> {
decisions::counted_labels(&self.context.quirks, || {
(sizing.iter())
.filter(|p| matches!(p.piece, Piece::Glyph(g) if g.label))
.filter_map(|p| face_of(&p.piece))
.filter(|i| !self.inked(*i))
.collect()
})
}
fn whole_singles(
&self,
plain: bool,
faces: &[FaceIndex],
(text, below): (&[FaceIndex], &[FaceIndex]),
) -> (Option<i64>, Option<i64>, i64) {
let own = plain.then(|| self.whole_single(text, below)).flatten();
let all = own.and(self.whole_single(faces, below));
let rise = all.zip(own).map_or(0, |(all, own)| all - own);
(all, own, rise)
}
fn whole_single(&self, faces: &[FaceIndex], text: &[FaceIndex]) -> Option<i64> {
let whole = |faces: &[FaceIndex], pick: fn((i64, i64)) -> i64| {
faces
.iter()
.map(|i| {
self.shaper
.faces()
.get(*i)
.and_then(Face::whole_line)
.map(pick)
})
.collect::<Option<Vec<i64>>>()?
.into_iter()
.max()
};
Some(whole(faces, |(up, _)| up)? + whole(text, |(_, down)| down)?)
}
fn face_room(&self, text: &[FaceIndex], whole_single: Option<i64>) -> (Length, Option<i64>) {
let largest = |pick: fn(&Face) -> Length| {
text.iter()
.filter_map(|i| self.shaper.faces().get(*i))
.map(pick)
.max()
.unwrap_or(Length::ZERO)
};
let room = (largest(Face::ascent) + largest(Face::descent) - largest(Face::line_height))
.max(Length::ZERO);
let whole = whole_single
.zip(self.whole_face_line(text))
.map(|(single, line)| (single - line).max(0));
(room, whole)
}
fn whole_face_line(&self, text: &[FaceIndex]) -> Option<i64> {
text.iter()
.map(|i| {
self.shaper
.faces()
.get(*i)
.and_then(Face::whole_line)
.map(|(up, down)| up + down)
})
.collect::<Option<Vec<i64>>>()?
.into_iter()
.max()
}
fn object_rise(
&self,
line: &Line,
rule: LineSpacing,
(ascent, descent): (Length, Length),
(rise, whole_single): (Length, Option<i64>),
) -> (Length, Length, Option<i64>, Length) {
let kept = (ascent, rise, whole_single, Length::ZERO);
if !matches!(rule, LineSpacing::Auto(n) if n >= LINE) {
return kept;
}
let text = holds_glyph_besides_white_space(line);
decisions::object_ascent(&self.context.quirks, || {
let above = line
.placed
.iter()
.filter_map(|p| match p.piece {
Piece::Object { height, raised, .. }
if raised < Length::ZERO || (text && raised == Length::ZERO) =>
{
Some(height + raised)
}
_ => None,
})
.max()
.unwrap_or(Length::ZERO);
(above > ascent && above <= ascent + descent)
.then(|| (above, rise + (above - ascent), None, above - ascent))
})
.unwrap_or(kept)
}
fn face_room_once(
&self,
rule: LineSpacing,
room: (Length, Option<i64>),
) -> (Length, Option<i64>) {
matches!(rule, LineSpacing::Auto(n) if n > LINE)
.then(|| decisions::face_room_once(&self.context.quirks, room))
.flatten()
.unwrap_or((Length::ZERO, Some(0)))
}
fn room_once(&self, rule: LineSpacing, shift_room: Length) -> Length {
matches!(rule, LineSpacing::Auto(n) if n > LINE)
.then(|| decisions::raised_room_once(&self.context.quirks, shift_room))
.flatten()
.unwrap_or(Length::ZERO)
}
pub(in crate::layout) fn holds_two_heights(&self, line: &Line) -> bool {
let mut heights =
line.placed
.iter()
.filter(|p| !is_blank(p))
.filter_map(|p| match p.piece {
Piece::Object { height, raised, .. } => {
Some((height + raised, (-raised).max(Length::ZERO)))
}
ref piece => face_of(piece)
.and_then(|i| self.shaper.faces().get(i))
.map(|face| (face.ascent(), face.descent())),
});
heights
.next()
.is_some_and(|first| heights.any(|height| height != first))
}
fn operator_ink(&self, sizing: &[&Placed]) -> (Length, Length) {
sizing
.iter()
.filter_map(|placed| {
let glyph = match placed.piece {
Piece::Glyph(glyph) => glyph,
Piece::Vinculum {
face,
top,
thickness,
beyond: (above, below),
..
} => {
let rise = self.device.shift_dots(self.shaper.faces().get(face)?.rise);
return Some((top + rise + above, thickness - top - rise + below));
}
_ => return None,
};
let face = self.shaper.faces().get(glyph.face)?;
let (leading, shift) = match face.math.room {
MathRoom::Ink { leading } => (leading, face.raised),
MathRoom::LinesAndInk { leading } => (leading, face.rise),
MathRoom::Lines | MathRoom::InkBelow => return None,
};
let (low, high) = self.shaper.ink(&glyph)?;
Some((high + shift + leading, -(low + shift)))
})
.fold(
(Length::ZERO, Length::ZERO),
|(top, bottom), (high, low)| (top.max(high), bottom.max(low)),
)
}
fn fraction_ink_depth(&self, sizing: &[&Placed]) -> Length {
let mut depth = Length::ZERO;
for placed in sizing {
let Piece::Glyph(glyph) = placed.piece else {
continue;
};
let Some(face) = self.shaper.faces().get(glyph.face) else {
continue;
};
if face.math.room != MathRoom::InkBelow {
continue;
}
let Some((low, _)) = self.shaper.ink(&glyph) else {
continue;
};
depth = depth.max(-(low + face.raised));
}
depth
}
pub(super) fn shape_drop(&self, line: &Line, rule: LineSpacing, size: &LineBox) -> Length {
match rule {
LineSpacing::Auto(n)
if n != LINE
&& !holds_glyph_besides_white_space(line)
&& self.object_excess(line, rule, size.ascent, size.descent)
<= Length::ZERO =>
{
size.height - (size.ascent + size.descent)
}
_ => Length::ZERO,
}
}
fn tallest_object_underlined(&self, line: &Line, rule: LineSpacing) -> bool {
let anchored = self.anchor_line(line, rule);
line.placed
.iter()
.filter_map(|p| match p.piece {
Piece::Object {
height,
raised,
drawing,
underlined,
..
} => {
let room = drawing
.filter(|_| anchored)
.and_then(|index| self.anchor_room(index))
.map_or(height, |room| room.above + room.below);
Some((room + raised, underlined))
}
_ => None,
})
.max_by_key(|(room, _)| *room)
.is_some_and(|(_, underlined)| underlined)
}
fn object_alone_room(
&self,
line: &Line,
rule: LineSpacing,
(descent, excess): (Length, Length),
(height, baseline): (Length, Length),
) -> Length {
if !matches!(rule, LineSpacing::Auto(_))
|| excess <= Length::ZERO
|| holds_glyph_besides_white_space(line)
{
return height;
}
let mut height = height;
if let LineSpacing::Auto(n) = rule
&& n > LINE
&& self.uses_object_run_faces()
&& let Some(run_line) = self.object_runs_line(line)
{
height = baseline + above_single(rule, run_line);
}
if decisions::underlined_object_room(&self.context.quirks)
&& self.tallest_object_underlined(line, rule)
{
height = height.max(baseline + descent);
}
height
}
fn object_runs_line(&self, line: &Line) -> Option<Length> {
line.placed
.iter()
.filter_map(|p| match p.piece {
Piece::Object {
run_face: Some(face),
..
} => self.shaper.faces().get(face).map(Face::line_height),
_ => None,
})
.max()
.filter(|run_line| *run_line > Length::ZERO)
}
fn tallest_real_object_room(&self, line: &Line, rule: LineSpacing) -> Option<Length> {
let anchored = self.anchor_line(line, rule);
line.placed
.iter()
.filter_map(|p| match p.piece {
Piece::Object {
height,
raised,
picture,
drawing,
..
} if picture.is_some() || drawing.is_some() => {
let room = drawing
.filter(|_| anchored)
.and_then(|index| self.anchor_room(index))
.map_or(height, |room| room.above + room.below);
Some(room + raised)
}
_ => None,
})
.max()
}
fn holds_vml_baseline_object(&self, line: &Line, multiple: bool) -> bool {
line.placed.iter().any(|p| match p.piece {
Piece::Object {
drawing: Some(index),
..
} => self
.output
.drawing(index)
.is_some_and(|d| d.extent.is_none()),
Piece::Object {
picture: Some(index),
..
} if multiple => self.output.pictures.get(index).is_some_and(|pic| pic.drops),
_ => false,
})
}
fn holds_only_objects_and_space(line: &Line) -> bool {
!line.placed.iter().any(|p| {
matches!(
p.piece,
Piece::Glyph(g) if g.ch != ' '
) || matches!(
p.piece,
Piece::Strut(_)
| Piece::Rule { .. }
| Piece::CheckBox { .. }
| Piece::Note(_)
| Piece::Endnote(_)
| Piece::NoteMarkEnd
| Piece::EndnoteMarkEnd(_)
| Piece::Shift(_)
| Piece::Advance(_)
| Piece::Bar { .. }
| Piece::Vinculum { .. }
| Piece::Diagonal { .. }
)
})
}
fn line_box_tail(
&self,
line: &Line,
rule: LineSpacing,
(ascent, descent, excess, drop, spaced): (Length, Length, Length, Length, bool),
(baseline, depth, last, smartart_tall): (Length, Length, bool, bool),
) -> (bool, bool, Length, bool) {
let text = ascent + descent;
let single = !spaced
&& excess <= Length::ZERO
&& match rule {
LineSpacing::Auto(n) => n == LINE && self.grid_pitch.is_none(),
LineSpacing::AtLeast(h) => h <= text + drop && self.grid_pitch.is_none(),
LineSpacing::Exact(_) => false,
};
let multiple = !smartart_tall
&& excess <= Length::ZERO
&& self.grid_pitch.is_none()
&& matches!(rule, LineSpacing::Auto(n) if n != LINE);
let end_drop = if matches!(rule, LineSpacing::AtLeast(_)) {
drop
} else {
Length::ZERO
};
let text_bottom = reaching_below(
text_end(
ascent,
descent,
rule,
excess,
baseline,
self.object_alone_no_descent(line, rule),
) + end_drop,
baseline,
(depth, Length::ZERO),
) + self.foot_border_room(last);
let hold_after = !smartart_tall
&& spaced
&& excess <= Length::ZERO
&& (drop <= Length::ZERO || !matches!(rule, LineSpacing::AtLeast(_)));
(single, multiple, text_bottom, hold_after)
}
fn smartart_baseline(
&self,
rule: LineSpacing,
(ascent, descent, height, excess): (Length, Length, Length, Length),
baseline: Length,
) -> (Length, bool) {
if !self.smartart_text() || excess > Length::ZERO {
return (baseline, false);
}
let spacing = decisions::SmartArtSpacing::of(&self.context.quirks);
match rule {
LineSpacing::Auto(n) if n < LINE => {
(spacing.low_baseline((height, descent), baseline), false)
}
LineSpacing::Auto(n) if n > LINE => {
match spacing.tall_baseline((ascent, descent, height)) {
Some(tall) => (tall, true),
None => (baseline, false),
}
}
_ => (baseline, false),
}
}
fn adjust_at_least_drop(
&self,
line: &Line,
rule: LineSpacing,
(ascent, descent, rise, excess, drop, spaced): (
Length,
Length,
Length,
Length,
Length,
bool,
),
(height, text_height, baseline): (&mut Length, &mut Length, &mut Length),
) {
let LineSpacing::AtLeast(h) = rule else {
return;
};
if drop <= Length::ZERO {
return;
}
let text = ascent + descent;
if excess <= Length::ZERO {
if h <= text + drop {
let (single_h, single_b) = line_box(
&self.device,
(ascent, descent, rise),
LineSpacing::Auto(LINE),
(excess, spaced),
);
*text_height = single_h;
*height = single_h + drop;
*baseline = single_b;
} else {
*text_height = h;
*height = h;
*baseline = h - descent - drop;
}
} else if holds_glyph_besides_white_space(line) {
*height = h.max(text + excess + descent + drop);
*baseline = *height - descent - drop;
}
}
pub(in crate::layout) fn placed_line_baseline(
&self,
line: &Line,
rule: LineSpacing,
size: &LineBox,
at: (Length, SpaceBefore),
rounding: (Ties, BoxTop),
rests: (i64, Option<i64>),
) -> (Length, BaselineForm, Length) {
if let Some(offset) = self.object_alone_baseline(line, rule, size) {
let (base, form) = placed_object_baseline(&self.device, at.0, offset, rounding, rests);
(base, form, offset)
} else {
let (base, form) = placed_baseline_and_form(&self.device, at, size, rounding, rests);
(base, form, size.baseline)
}
}
pub(in crate::layout) fn object_alone_baseline(
&self,
line: &Line,
rule: LineSpacing,
size: &LineBox,
) -> Option<Length> {
let text = size.ascent + size.descent;
if self.grid_pitch.is_some()
|| self.smartart_text()
|| !Self::holds_only_objects_and_space(line)
|| self
.tallest_real_object_room(line, rule)
.is_none_or(|room| room <= Length::ZERO)
|| self.object_excess(line, rule, size.ascent, size.descent) > Length::ZERO
|| object_depth(line, rule) > Length::ZERO
|| !self.object_alone_no_descent(line, rule)
{
return None;
}
match rule {
LineSpacing::Auto(n) if !self.holds_vml_baseline_object(line, n != LINE) => {
Some(if n >= LINE { text } else { size.height })
}
LineSpacing::AtLeast(h)
if h >= text && !self.holds_vml_baseline_object(line, false) =>
{
Some(size.height)
}
_ => None,
}
}
pub(super) fn object_excess(
&self,
line: &Line,
rule: LineSpacing,
ascent: Length,
descent: Length,
) -> Length {
self.tallest_real_object_room(line, rule)
.map_or(Length::ZERO, |room| {
(room - ascent - descent).max(Length::ZERO)
})
}
fn object_alone_no_descent(&self, line: &Line, rule: LineSpacing) -> bool {
let underlined = decisions::underlined_object_room(&self.context.quirks)
&& self.tallest_object_underlined(line, rule);
!holds_glyph_besides_white_space(line) && !underlined
}
}
pub(in crate::layout) fn text_end(
ascent: Length,
descent: Length,
rule: LineSpacing,
excess: Length,
baseline: Length,
object_alone: bool,
) -> Length {
match rule {
LineSpacing::Auto(_) if excess == Length::ZERO => ascent + descent,
LineSpacing::Auto(_) if excess > Length::ZERO && object_alone => baseline,
LineSpacing::AtLeast(h) if excess <= Length::ZERO && h <= ascent + descent => {
ascent + descent
}
LineSpacing::AtLeast(h)
if excess > Length::ZERO && object_alone && h < ascent + descent + excess =>
{
baseline
}
LineSpacing::Exact(h) => h,
_ => baseline + descent,
}
}
pub(in crate::layout) fn text_baseline(
device: &Device,
(ascent, descent): (Length, Length),
lines: i64,
spaced: bool,
) -> Length {
let single = ascent + descent;
if lines != LINE || spaced {
device.snap(ascent)
} else if device.snap(single) > single {
single - device.snap(descent)
} else {
device.rounded_down_baseline((ascent, descent), single, false)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::layout) enum SpaceBefore {
Absent,
Held(Length),
Dropped(Length),
}
impl SpaceBefore {
pub(in crate::layout) fn of(first: Length, before: Length) -> Self {
if before > Length::ZERO {
Self::Held(before)
} else if first > Length::ZERO {
Self::Dropped(first)
} else {
Self::Absent
}
}
pub(in crate::layout) fn length(self) -> Length {
match self {
Self::Held(before) => before,
Self::Absent | Self::Dropped(_) => Length::ZERO,
}
}
fn below_space(self) -> bool {
self != Self::Absent
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::layout) enum Ties {
AwayFromTop,
Cell,
TowardTop,
Footer,
TurnedFrame,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::layout) enum BoxTop {
PageTop,
Lower,
}
fn place(device: &Device, at: ExactPlace, ties: Ties) -> Length {
if let Some(dot) = at.rested_dot(device) {
return dot;
}
match ties {
Ties::AwayFromTop | Ties::Cell | Ties::Footer | Ties::TurnedFrame => device.snap(at.at),
Ties::TowardTop => device.header_dot(at.at),
}
}
fn bottom_place(device: &Device, at: ExactPlace, ties: Ties) -> Length {
match ties {
Ties::Footer if at.rest == 0 => device.footer_bottom_dot(at.at),
Ties::AwayFromTop | Ties::Cell | Ties::TowardTop | Ties::Footer | Ties::TurnedFrame => {
place(device, at, ties)
}
}
}
fn top_place(device: &Device, at: ExactPlace, ties: Ties) -> Length {
if let Some(dot) = at.rested_dot(device) {
return dot;
}
match ties {
Ties::AwayFromTop | Ties::Cell | Ties::Footer => device.top_dot(at.at),
Ties::TowardTop | Ties::TurnedFrame => place(device, at, ties),
}
}
fn box_top_place(device: &Device, at: ExactPlace, ties: Ties, box_top: BoxTop) -> Length {
match box_top {
BoxTop::PageTop => top_place(device, at, ties),
BoxTop::Lower => place(device, at, ties),
}
}
pub(in crate::layout) fn row_line_dot(
device: &Device,
top: Length,
rest: Option<i64>,
box_top: BoxTop,
) -> Length {
let at = ExactPlace::carried(top, rest.unwrap_or(0));
box_top_place(device, at, Ties::AwayFromTop, box_top)
}
fn boxed_baseline_and_form(
device: &Device,
(top, space): (Length, SpaceBefore),
size: &LineBox,
(ties, box_top): (Ties, BoxTop),
(rest, twips): (i64, Option<i64>),
) -> (Length, BaselineForm) {
let space = boxed_space(device, space, box_top);
let before = space.length();
let boxed = before + size.height;
let exact_top = top;
let top = ExactPlace::carried(exact_top, rest + twips.unwrap_or(0));
let own = bottom_rest(size, twips);
let found = own_box_baseline(
device,
(exact_top, top, space),
size,
ties,
(rest, twips, own),
)
.or_else(|| bottom_room_baseline(device, (top, before), size, ties))
.or_else(|| low_room_baseline(device, (top, before), size, ties));
if let Some(found) = found {
found
} else if boxes_spaces(device, before, size) {
let ascent = held_ascent(size);
let height = before + size.text_height + size.after;
let descent = descent_part(device, (before + ascent, size.descent), height);
let held = ExactPlace::carried(
exact_top,
rest + box_twips(device, twips, before).unwrap_or(0),
);
let at = box_top_place(device, held - before, ties, box_top)
+ held_ascent_part(device, before, size).max(descent);
(at, BaselineForm::HeldSpaces)
} else if size.from_bottom == FromBottom::AtLeast && device.snap(boxed) < boxed {
let at = place(device, top - before, ties) + device.snap(boxed)
- device.snap(size.height - size.baseline);
(at, BaselineForm::AtLeastBox)
} else if size.from_bottom.exact()
&& let Some(offset) = device.exact_box_baseline(
before,
(size.height, size.baseline, exact_below(device, size)),
(twips_rest(device, before), twips_rest(device, size.height)),
space.below_space(),
)
{
let at = place(device, top - before, ties) + offset;
(at, BaselineForm::ExactBox)
} else if size.from_bottom != FromBottom::No {
bottom_baseline_and_form(device, top, (size, own), ties)
} else if size.multiple && before > Length::ZERO {
let at = box_top_place(device, top - before, ties, box_top)
+ device.snap(before)
+ device.snap(size.baseline);
(at, BaselineForm::SpaceAndAscent)
} else if device.snap(size.baseline) == size.baseline {
let at = box_top_place(device, top, ties, box_top) + size.baseline;
(at, BaselineForm::WholeDotsBelowTop)
} else {
rounded_baseline_and_form(device, top, size, ties)
}
}
fn bottom_room_baseline(
device: &Device,
(top, before): (ExactPlace, Length),
size: &LineBox,
ties: Ties,
) -> Option<(Length, BaselineForm)> {
if !size.from_bottom.exact() || size.after <= Length::ZERO {
return None;
}
let offset = device.centred_room_baseline((before, size.after), (size.height, size.baseline));
Some((
place(device, top - before, ties) + offset,
BaselineForm::ExactBox,
))
}
fn low_room_baseline(
device: &Device,
(top, before): (ExactPlace, Length),
size: &LineBox,
ties: Ties,
) -> Option<(Length, BaselineForm)> {
if !size.low_multiple() || (size.after <= Length::ZERO && size.top_room <= Length::ZERO) {
return None;
}
if size.after <= Length::ZERO {
let bottom = place(device, top + size.height, ties)
.min(place(device, top - before, ties) + device.snap(before + size.height));
let at = bottom - device.snap(size.height - size.baseline);
return Some((at, BaselineForm::HeldSpaces));
}
let ascent = device.snap(before) + device.snap(size.baseline);
let descent = device.snap(before + size.height + size.after)
- device.snap(size.after)
- device.snap(size.height - size.baseline);
let at = place(device, top - before, ties) + ascent.max(descent);
Some((at, BaselineForm::HeldSpaces))
}
fn own_box_baseline(
device: &Device,
(exact_top, top, space): (Length, ExactPlace, SpaceBefore),
size: &LineBox,
ties: Ties,
(rest, twips, own): (i64, Option<i64>, i64),
) -> Option<(Length, BaselineForm)> {
if size.single {
let box_size = (size.ascent, size.descent, own);
let held = ExactPlace::carried(
exact_top,
rest + box_twips(device, twips, space.length()).unwrap_or(0),
);
let box_top = single_box_top(ties, (exact_top, rest), held);
return Some(rested_boxed_baseline(
device,
(top, box_top, space),
box_size,
ties,
));
}
size.grid_ties.map(|grid| {
let at = device.dots(device.count(exact_top, grid));
(at + size.baseline, BaselineForm::GridSteps)
})
}
fn rounded_baseline_and_form(
device: &Device,
top: ExactPlace,
size: &LineBox,
ties: Ties,
) -> (Length, BaselineForm) {
if size.low_multiple()
&& let Some(low) = device.low_multiple_baseline(size.height, size.baseline)
{
let at = match low {
LowBaseline::BelowTop(offset) => place(device, top, ties) + offset,
LowBaseline::AboveBottom(offset) => place(device, top + size.height, ties) - offset,
};
(at, BaselineForm::HeightFraction)
} else {
(
place(device, top + size.baseline, ties),
BaselineForm::Baseline,
)
}
}
pub(in crate::layout) fn placed_object_baseline(
device: &Device,
top: Length,
offset: Length,
(ties, box_top): (Ties, BoxTop),
(rest, twips): (i64, Option<i64>),
) -> (Length, BaselineForm) {
let top = ExactPlace::carried(top, rest + twips.unwrap_or(0));
if device.snap(offset) == offset {
(
box_top_place(device, top, ties, box_top) + offset,
BaselineForm::WholeDotsBelowTop,
)
} else {
(place(device, top + offset, ties), BaselineForm::Baseline)
}
}
pub(in crate::layout) fn placed_baseline_and_form(
device: &Device,
at: (Length, SpaceBefore),
size: &LineBox,
rounding: (Ties, BoxTop),
rests: (i64, Option<i64>),
) -> (Length, BaselineForm) {
centered_baseline(device, at, size, rounding, rests).map_or_else(
|| boxed_baseline_and_form(device, at, size, rounding, rests),
|at| (at, BaselineForm::CenteredMiddle),
)
}
fn centered_baseline(
device: &Device,
(top, space): (Length, SpaceBefore),
size: &LineBox,
(ties, box_top): (Ties, BoxTop),
(rest, twips): (i64, Option<i64>),
) -> Option<Length> {
let face = size
.centered
.filter(|_| !size.single && size.text_height >= size.ascent + size.descent)?;
let before = boxed_space(device, space, box_top).length();
let top = ExactPlace::carried(top, rest + twips.unwrap_or(0));
let height = before + size.text_height + size.after;
let from_top = device.ascent_part(before, size.ascent - face.middle);
let from_bottom = device.snap(height)
- device.snap(height - (before + size.ascent + size.descent))
- device.snap(face.half);
Some(
box_top_place(device, top - before, ties, box_top)
+ from_top.max(from_bottom)
+ face.pixels,
)
}
pub(in crate::layout) fn placed_baseline(
device: &Device,
at: (Length, SpaceBefore),
size: &LineBox,
rounding: (Ties, BoxTop),
rests: (i64, Option<i64>),
) -> Length {
placed_baseline_and_form(device, at, size, rounding, rests).0
}
fn boxed_space(device: &Device, space: SpaceBefore, box_top: BoxTop) -> SpaceBefore {
match (space, box_top) {
(SpaceBefore::Dropped(dropped), BoxTop::PageTop) => device
.held_dropped_space(dropped)
.map_or(space, SpaceBefore::Held),
_ => space,
}
}
fn box_twips(device: &Device, twips: Option<i64>, before: Length) -> Option<i64> {
twips
.zip(twips_rest(device, before))
.map(|(at, space)| at - space)
}
fn held_ascent(size: &LineBox) -> Length {
if size.from_bottom == FromBottom::AtLeast {
size.baseline
} else {
size.ascent
}
}
fn held_ascent_part(device: &Device, before: Length, size: &LineBox) -> Length {
if size.from_bottom == FromBottom::AtLeast {
let room = size.top_room.min(before);
device.ascent_part(before - room, size.baseline - size.ascent)
+ device.snap(room + size.height)
- device.snap(size.height)
+ device.snap(size.ascent)
} else {
device.ascent_part(before, size.ascent)
}
}
fn boxes_spaces(device: &Device, before: Length, size: &LineBox) -> bool {
if size.from_bottom == FromBottom::AtLeast {
return size.after > Length::ZERO;
}
(size.multiple && before > Length::ZERO || size.after > Length::ZERO)
&& device
.descent_above_extra(|| size.text_height >= size.ascent + size.descent)
.unwrap_or(false)
}
fn exact_baseline(
device: &Device,
top: ExactPlace,
(height, below, rest): (Length, Length, i64),
ties: Ties,
) -> Length {
let bottom = ExactPlace::carried(top.at + height, top.rest + rest);
place(device, bottom, ties) - below
}
fn bottom_baseline_and_form(
device: &Device,
top: ExactPlace,
(size, own): (&LineBox, i64),
ties: Ties,
) -> (Length, BaselineForm) {
let below = exact_below(device, size);
if size.from_bottom == FromBottom::ExactSpaced {
let at = spaced_exact_baseline(device, top, (size.height, below), ties);
(at, BaselineForm::SpacedBottom)
} else {
let at = exact_baseline(device, top, (size.height, below, own), ties);
(at, BaselineForm::Bottom)
}
}
fn spaced_exact_baseline(
device: &Device,
top: ExactPlace,
(height, below): (Length, Length),
ties: Ties,
) -> Length {
place(device, top, ties) + device.snap(height) - below
}
fn exact_below(device: &Device, size: &LineBox) -> Length {
let below = size.height - size.baseline;
let entered = size
.twips
.filter(|_| size.from_bottom.exact())
.map(|rest| entered_below_rest(size.height, below, rest));
device.exact_below(below, entered)
}
fn entered_below_rest(height: Length, below: Length, rest: i64) -> i64 {
let (part, whole) = params::EXACT_BASELINE;
let entered = height.units() * REST_PARTS + rest;
let parts = (entered * (whole - part)).div_euclid(whole * REST_PER_PART);
parts * REST_PER_PART - below.units() * REST_PARTS
}
fn single_box_top(ties: Ties, (exact_top, rest): (Length, i64), held: ExactPlace) -> ExactPlace {
match ties {
Ties::Cell => ExactPlace::carried(exact_top, rest),
Ties::AwayFromTop | Ties::TowardTop | Ties::Footer | Ties::TurnedFrame => held,
}
}
#[cfg(test)]
pub(in crate::layout) fn boxed_baseline(
device: &Device,
(top, space): (Length, SpaceBefore),
(ascent, descent): (Length, Length),
ties: Ties,
) -> Length {
let top = ExactPlace { at: top, rest: 0 };
rested_boxed_baseline(device, (top, top, space), (ascent, descent, 0), ties).0
}
fn rested_boxed_baseline(
device: &Device,
(top, box_top, space): (ExactPlace, ExactPlace, SpaceBefore),
(ascent, descent, rest): (Length, Length, i64),
ties: Ties,
) -> (Length, BaselineForm) {
let before = space.length();
let single = ascent + descent;
let height = before + single;
if device.snap(height) > height {
let bottom = ExactPlace::carried(top.at + single, top.rest + rest);
let at = bottom_place(device, bottom, ties) - device.snap(descent);
(at, BaselineForm::SingleBottom)
} else {
let at = top_place(device, box_top - before, ties)
+ device.rounded_down_baseline((before + ascent, descent), height, space.below_space());
(at, BaselineForm::SingleTop)
}
}
pub(in crate::layout) fn text_below_object(
n: i64,
(ascent, descent): (Length, Length),
baseline: Length,
) -> Length {
let single = ascent + descent;
baseline + descent + times_lines(single, n) - single
}
fn multiple_baseline(
device: &Device,
(ascent, descent): (Length, Length),
(rise, n): (Length, i64),
spaced: bool,
) -> Length {
if n < LINE {
return rise + times_lines(ascent - rise, n);
}
let above_extra = || {
let height = times_lines(ascent + descent - rise, n) + rise;
descent_above_extra(device, (ascent, descent), height)
};
(n > LINE)
.then(|| device.descent_above_extra(above_extra))
.flatten()
.unwrap_or_else(|| text_baseline(device, (ascent, descent), n, spaced))
}
fn descent_above_extra(
device: &Device,
(ascent, descent): (Length, Length),
height: Length,
) -> Length {
device
.snap(ascent)
.max(descent_part(device, (ascent, descent), height))
}
fn descent_part(device: &Device, (ascent, descent): (Length, Length), height: Length) -> Length {
device.snap(height) - device.snap(height - (ascent + descent)) - device.snap(descent)
}
pub(in crate::layout) fn line_box(
device: &Device,
(ascent, descent, rise): (Length, Length, Length),
rule: LineSpacing,
(excess, spaced): (Length, bool),
) -> (Length, Length) {
let single = ascent + descent;
match rule {
LineSpacing::Auto(n) if rise > Length::ZERO && excess <= Length::ZERO => {
let text = ascent - rise;
(
times_lines(text + descent, n) + rise,
multiple_baseline(device, (ascent, descent), (rise, n), spaced),
)
}
LineSpacing::Auto(n) if excess > Length::ZERO => {
(times_lines(single, n) + excess, ascent + excess + descent)
}
LineSpacing::Auto(n) => (
times_lines(single, n),
multiple_baseline(device, (ascent, descent), (Length::ZERO, n), spaced),
),
LineSpacing::Exact(h) => {
let (part, whole) = params::EXACT_BASELINE;
(h, (h * part).div_round(whole))
}
LineSpacing::AtLeast(h) if excess <= Length::ZERO && h <= single => line_box(
device,
(ascent, descent, rise),
LineSpacing::Auto(LINE),
(excess, spaced),
),
LineSpacing::AtLeast(h) if excess > Length::ZERO && h < single + excess => {
(single + excess, single + excess)
}
LineSpacing::AtLeast(h) => {
let height = h.max(single + excess);
(height, height - descent)
}
}
}
pub(in crate::layout) fn below_text(rule: LineSpacing, excess: Length, drop: Length) -> Length {
if matches!(rule, LineSpacing::Auto(_)) && excess <= Length::ZERO {
drop
} else {
Length::ZERO
}
}
fn object_depth(line: &Line, rule: LineSpacing) -> Length {
if matches!(rule, LineSpacing::Exact(_)) {
return Length::ZERO;
}
line.placed
.iter()
.filter_map(|p| match p.piece {
Piece::Object { raised, .. } => Some(-raised),
_ => None,
})
.fold(Length::ZERO, Length::max)
}
fn reaching_below(length: Length, baseline: Length, (depth, below): (Length, Length)) -> Length {
if depth > Length::ZERO {
length.max(baseline + depth + below)
} else {
length
}
}
fn above_single(rule: LineSpacing, single: Length) -> Length {
match rule {
LineSpacing::Auto(n) if n > LINE => times_lines(single, n) - single,
_ => Length::ZERO,
}
}
fn blank_line_end_face(end: LineEnd, mark: Option<FaceIndex>, last: bool) -> Option<FaceIndex> {
match end {
LineEnd::Break(face) => face,
_ if last => mark,
_ => None,
}
}
pub(super) fn is_blank(placed: &Placed) -> bool {
matches!(placed.piece, Piece::Glyph(g) if g.ch == ' ' && g.space != crate::text::SpaceKind::Assembly)
|| matches!(placed.piece, Piece::Tab(_) | Piece::PositionTab(..))
}
pub(in crate::layout) fn holds_glyph_besides_white_space(line: &Line) -> bool {
line.placed
.iter()
.any(|p| matches!(p.piece, Piece::Glyph(g) if !g.ch.is_whitespace()))
}
#[cfg(test)]
mod tests;