use crate::glue::{Glue, Order};
use crate::node::{
BoxNode, GlueNode, GlueSign, GlueSource, Node, RuleNode, Scaled, IGNORE_DEPTH, INF_BAD,
MAX_DIMEN,
};
pub fn badness(t: Scaled, s: Scaled) -> i64 {
if t == 0 {
return 0;
}
if s <= 0 {
return INF_BAD;
}
let r = if t <= 7_230_584 {
(t * 297) / s
} else if s >= 1_663_497 {
t / (s / 297)
} else {
t
};
if r > 1290 {
return INF_BAD;
}
(r * r * r + 0o400_000) / 0o1_000_000
}
pub fn x_over_n(x: Scaled, n: i64) -> Scaled {
if n == 0 {
return 0;
}
let (x, n) = match n < 0 {
true => (-x, -n),
false => (x, n),
};
match x >= 0 {
true => x / n,
false => -((-x) / n),
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Spec {
Exactly(Scaled),
Additional(Scaled),
}
pub const NATURAL: Spec = Spec::Additional(0);
#[derive(Clone, Copy, Debug, Default)]
pub struct Tolerances {
pub hbadness: i64,
pub vbadness: i64,
pub hfuzz: Scaled,
pub vfuzz: Scaled,
pub overfull_rule: Scaled,
}
impl Tolerances {
pub fn plain() -> Tolerances {
Tolerances {
hbadness: 1000,
vbadness: 1000,
hfuzz: 6554,
vfuzz: 6554,
overfull_rule: 0,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Report {
Underfull(i64),
Loose(i64),
Tight(i64),
Overfull(Scaled),
}
impl Report {
pub fn message(&self, vertical: bool) -> String {
let (b, dim) = match vertical {
true => ("\\vbox", "pt too high"),
false => ("\\hbox", "pt too wide"),
};
match self {
Report::Underfull(n) => format!("Underfull {b} (badness {n})"),
Report::Loose(n) => format!("Loose {b} (badness {n})"),
Report::Tight(n) => format!("Tight {b} (badness {n})"),
Report::Overfull(by) => {
format!("Overfull {b} ({}{dim})", crate::dimen::print_scaled(*by))
}
}
}
}
#[derive(Clone, Debug)]
pub struct Packed {
pub node: BoxNode,
pub badness: i64,
pub report: Option<Report>,
pub total_shrink: [Scaled; 4],
pub total_stretch: [Scaled; 4],
}
#[derive(Clone, Copy, Debug, Default)]
struct Totals([Scaled; 4]);
impl Totals {
fn add(&mut self, amount: Scaled, order: Order) {
let order = order.clamp(0, 3) as usize;
self.0[order] += amount;
}
fn order(&self) -> Order {
(0..4).rev().find(|&o| self.0[o] != 0).unwrap_or(0) as Order
}
fn at(&self, order: Order) -> Scaled {
self.0[order.clamp(0, 3) as usize]
}
}
pub fn hpack(
list: Vec<Node>,
spec: Spec,
tol: Tolerances,
adjust: Option<&mut Vec<Node>>,
) -> Packed {
let mut r = BoxNode {
vertical: false,
..BoxNode::null()
};
let mut h: Scaled = 0;
let mut d: Scaled = 0;
let mut x: Scaled = 0;
let mut stretch = Totals::default();
let mut shrink = Totals::default();
let kept: Vec<Node> = match adjust {
Some(tail) => {
let mut kept = Vec::with_capacity(list.len());
for node in list {
match node {
Node::Ins(_) | Node::Mark(_) | Node::Adjust(_) => {
transfer_to_adjustment(node, tail)
}
_ => kept.push(node),
}
}
kept
}
None => list,
};
for node in &kept {
if let Some((above, below)) = hlist_extent(node) {
h = h.max(above);
d = d.max(below);
}
match node {
Node::Char(c) | Node::Ligature(c, _) => x += c.width,
Node::Box(_) | Node::Rule(_) => x += box_dimensions(node).0,
Node::Glue(g) => {
x += g.spec.natural;
stretch.add(g.spec.stretch, g.spec.stretch_order);
shrink.add(g.spec.shrink, g.spec.shrink_order);
}
Node::Kern { width, .. }
| Node::AccKern(width)
| Node::Math(width)
| Node::MathOff(width) => x += *width,
Node::Disc(_) | Node::Whatsit(_) | Node::Out(_) | Node::Penalty(_) => {}
Node::Ins(_) | Node::Mark(_) | Node::Adjust(_) => {}
}
}
r.height = h;
r.depth = d;
let (badness, report) = set_glue(&mut r, x, spec, stretch, shrink, &kept, tol);
r.list = kept;
if let Some(Report::Overfull(over)) = report {
if tol.overfull_rule > 0 && over > tol.hfuzz {
r.list.push(Node::Rule(RuleNode {
width: tol.overfull_rule,
..RuleNode::running()
}));
}
}
Packed {
node: r,
badness,
report,
total_shrink: shrink.0,
total_stretch: stretch.0,
}
}
pub fn vpackage(list: Vec<Node>, spec: Spec, max_depth: Scaled, tol: Tolerances) -> Packed {
let mut r = BoxNode {
vertical: true,
..BoxNode::null()
};
let mut w: Scaled = 0;
let mut d: Scaled = 0;
let mut x: Scaled = 0;
let mut stretch = Totals::default();
let mut shrink = Totals::default();
for node in &list {
match node {
Node::Box(_) | Node::Rule(_) => {
let (bw, bh, bd) = box_dimensions(node);
x += d + bh;
d = bd;
w = w.max(bw + node.shift_amount());
}
Node::Glue(g) => {
x += d;
d = 0;
x += g.spec.natural;
stretch.add(g.spec.stretch, g.spec.stretch_order);
shrink.add(g.spec.shrink, g.spec.shrink_order);
if let (true, Some(leader)) = (g.kind.is_leaders(), &g.leader) {
let (lw, _, _) = box_dimensions(leader);
w = w.max(lw);
}
}
Node::Kern { width, .. } => {
x += d + width;
d = 0;
}
_ => {}
}
}
r.width = w;
if d > max_depth {
x += d - max_depth;
r.depth = max_depth;
} else {
r.depth = d;
}
let (badness, report) = set_glue(&mut r, x, spec, stretch, shrink, &list, tol);
r.list = list;
Packed {
node: r,
badness,
report,
total_shrink: shrink.0,
total_stretch: stretch.0,
}
}
pub fn vpack(list: Vec<Node>, spec: Spec, tol: Tolerances) -> Packed {
vpackage(list, spec, MAX_DIMEN, tol)
}
pub fn vtop(mut packed: Packed) -> Packed {
let h = match packed.node.list.first() {
Some(first) if first.type_code() <= 2 => box_dimensions(first).1,
_ => 0,
};
packed.node.depth = packed.node.depth - h + packed.node.height;
packed.node.height = h;
packed
}
fn hlist_extent(node: &Node) -> Option<(Scaled, Scaled)> {
match node {
Node::Char(c) | Node::Ligature(c, _) => Some((c.height, c.depth)),
Node::Box(_) | Node::Rule(_) => {
let (_, height, depth) = box_dimensions(node);
let s = node.shift_amount();
Some((height - s, depth + s))
}
Node::Glue(g) => match (g.kind.is_leaders(), &g.leader) {
(true, Some(leader)) => {
let (_, height, depth) = box_dimensions(leader);
Some((height, depth))
}
_ => None,
},
_ => None,
}
}
pub fn hlist_height_depth(list: &[Node]) -> (Scaled, Scaled) {
list.iter()
.filter_map(hlist_extent)
.fold((0, 0), |(h, d), (above, below)| {
(h.max(above), d.max(below))
})
}
fn box_dimensions(node: &Node) -> (Scaled, Scaled, Scaled) {
match node {
Node::Box(b) => (b.width, b.height, b.depth),
Node::Rule(r) => (r.width, r.height, r.depth),
Node::Char(c) | Node::Ligature(c, _) => (c.width, c.height, c.depth),
_ => (0, 0, 0),
}
}
fn transfer_to_adjustment(node: Node, tail: &mut Vec<Node>) {
match node {
Node::Adjust(list) => tail.extend(list),
other => tail.push(other),
}
}
fn set_glue(
r: &mut BoxNode,
natural: Scaled,
spec: Spec,
stretch: Totals,
shrink: Totals,
list: &[Node],
tol: Tolerances,
) -> (i64, Option<Report>) {
let vertical = r.vertical;
let target = match spec {
Spec::Exactly(w) => w,
Spec::Additional(w) => natural + w,
};
match vertical {
true => r.height = target,
false => r.width = target,
}
let x = target - natural;
let (badness_limit, fuzz) = match vertical {
true => (tol.vbadness, tol.vfuzz),
false => (tol.hbadness, tol.hfuzz),
};
if x == 0 {
r.glue_sign = GlueSign::Normal;
r.glue_order = 0;
r.glue_set = 0.0;
return (0, None);
}
if x > 0 {
let o = stretch.order();
r.glue_order = o;
r.glue_sign = GlueSign::Stretching;
if stretch.at(o) != 0 {
r.glue_set = x as f64 / stretch.at(o) as f64;
} else {
r.glue_sign = GlueSign::Normal;
r.glue_set = 0.0;
}
if o != 0 || list.is_empty() {
return (0, None);
}
let b = badness(x, stretch.at(0));
if b > badness_limit {
let report = match b > 100 {
true => Report::Underfull(b),
false => Report::Loose(b),
};
return (b, Some(report));
}
return (b, None);
}
let o = shrink.order();
r.glue_order = o;
r.glue_sign = GlueSign::Shrinking;
if shrink.at(o) != 0 {
r.glue_set = (-x) as f64 / shrink.at(o) as f64;
} else {
r.glue_sign = GlueSign::Normal;
r.glue_set = 0.0;
}
if shrink.at(o) < -x && o == 0 && !list.is_empty() {
r.glue_set = 1.0;
let over = -x - shrink.at(0);
if over > fuzz || badness_limit < 100 {
return (1_000_000, Some(Report::Overfull(over)));
}
return (1_000_000, None);
}
if o != 0 || list.is_empty() {
return (0, None);
}
let b = badness(-x, shrink.at(0));
if b > badness_limit {
return (b, Some(Report::Tight(b)));
}
(b, None)
}
const BILLION: f64 = 1_000_000_000.0;
pub struct Setter {
glue_set: f64,
sign: GlueSign,
order: Order,
cur_glue: f64,
cur_g: Scaled,
}
impl Setter {
pub fn new(b: &BoxNode) -> Setter {
Setter {
glue_set: b.glue_set,
sign: b.glue_sign,
order: b.glue_order,
cur_glue: 0.0,
cur_g: 0,
}
}
pub fn glue(&mut self, spec: &Glue) -> Scaled {
let mut width = spec.natural - self.cur_g;
match self.sign {
GlueSign::Stretching if spec.stretch_order == self.order => {
self.cur_glue += spec.stretch as f64;
self.cur_g = self.rounded();
}
GlueSign::Shrinking if spec.shrink_order == self.order => {
self.cur_glue -= spec.shrink as f64;
self.cur_g = self.rounded();
}
_ => {}
}
width += self.cur_g;
width
}
fn rounded(&self) -> Scaled {
let temp = (self.glue_set * self.cur_glue).clamp(-BILLION, BILLION);
temp.round() as Scaled
}
}
pub fn glue_widths(b: &BoxNode) -> Vec<Scaled> {
let mut setter = Setter::new(b);
b.list
.iter()
.filter_map(|n| match n {
Node::Glue(g) => Some(setter.glue(&g.spec)),
_ => None,
})
.collect()
}
pub fn leader_positions(
kind: crate::node::LeaderKind,
leader_width: Scaled,
space: Scaled,
cur_h: Scaled,
left_edge: Scaled,
) -> Vec<Scaled> {
use crate::node::LeaderKind;
if leader_width <= 0 || space <= 0 {
return Vec::new();
}
let rule_wd = space + 10;
let edge = cur_h + rule_wd;
let mut lx: Scaled = 0;
let mut h = match kind {
LeaderKind::Aligned => {
let aligned = left_edge + leader_width * ((cur_h - left_edge) / leader_width);
match aligned < cur_h {
true => aligned + leader_width,
false => aligned,
}
}
LeaderKind::Centred => {
let lr = rule_wd % leader_width;
cur_h + lr / 2
}
LeaderKind::Expanded => {
let lq = rule_wd / leader_width;
let lr = rule_wd % leader_width;
lx = lr / (lq + 1);
cur_h + (lr - (lq - 1) * lx) / 2
}
LeaderKind::Normal => return Vec::new(),
};
let mut out = Vec::new();
while h + leader_width <= edge {
out.push(h);
h += leader_width + lx;
}
out
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Baselines {
pub baseline_skip: Glue,
pub line_skip: Glue,
pub line_skip_limit: Scaled,
}
impl Baselines {
pub fn plain() -> Baselines {
Baselines {
baseline_skip: Glue::fixed(12 * crate::dimen::UNITY),
line_skip: Glue::fixed(crate::dimen::UNITY),
line_skip_limit: 0,
}
}
}
pub fn append_to_vlist(list: &mut Vec<Node>, b: BoxNode, prev_depth: &mut Scaled, p: Baselines) {
if *prev_depth > IGNORE_DEPTH {
let d = p.baseline_skip.natural - *prev_depth - b.height;
let glue = match d < p.line_skip_limit {
true => GlueNode::param(p.line_skip, GlueSource::LineSkip),
false => GlueNode::param(
Glue {
natural: d,
..p.baseline_skip
},
GlueSource::BaselineSkip,
),
};
list.push(Node::Glue(glue));
}
*prev_depth = b.depth;
list.push(Node::Box(b));
}
pub fn resolve_rule(rule: RuleNode, container: &BoxNode) -> RuleNode {
let mut out = rule;
match container.vertical {
true => {
if RuleNode::is_running(out.width) {
out.width = container.width;
}
}
false => {
if RuleNode::is_running(out.height) {
out.height = container.height;
}
if RuleNode::is_running(out.depth) {
out.depth = container.depth;
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dimen::UNITY;
use crate::node::{CharNode, LeaderKind};
fn pt(n: i64) -> Scaled {
n * UNITY
}
fn ch(width: Scaled, height: Scaled, depth: Scaled) -> Node {
Node::Char(CharNode {
font: 0,
character: 'x',
width,
height,
depth,
})
}
fn glue(natural: Scaled, stretch: Scaled, shrink: Scaled) -> Node {
Node::Glue(GlueNode::new(Glue {
natural,
stretch,
shrink,
..Glue::default()
}))
}
fn fil(order: Order) -> Node {
Node::Glue(GlueNode::new(Glue {
stretch: UNITY,
stretch_order: order,
..Glue::default()
}))
}
#[test]
fn badness_is_the_integer_method_tex_specifies() {
assert_eq!(badness(0, 0), 0);
assert_eq!(badness(1, 0), INF_BAD);
assert_eq!(badness(UNITY, UNITY), 100);
assert_eq!(badness(2 * UNITY, UNITY), 800);
assert_eq!(badness(3 * UNITY, UNITY), 2698);
assert_eq!(badness(1290 * UNITY, 297 * UNITY), 8189);
assert_eq!(badness(1291 * UNITY, 297 * UNITY), INF_BAD);
assert!(badness(UNITY + 1, UNITY) >= badness(UNITY, UNITY));
assert!(badness(UNITY, UNITY) >= badness(UNITY, UNITY + 1));
}
#[test]
fn x_over_n_truncates_towards_zero() {
assert_eq!(x_over_n(7, 2), 3);
assert_eq!(x_over_n(-7, 2), -3);
assert_eq!(x_over_n(7, -2), -3);
assert_eq!(x_over_n(7, 0), 0);
}
#[test]
fn an_hbox_takes_its_natural_size_from_what_is_in_it() {
let list = vec![
ch(pt(10), pt(7), pt(2)),
glue(pt(4), pt(2), pt(1)),
ch(pt(6), pt(9), pt(1)),
];
let packed = hpack(list, NATURAL, Tolerances::plain(), None);
assert_eq!(packed.node.width, pt(20));
assert_eq!(packed.node.height, pt(9));
assert_eq!(packed.node.depth, pt(2));
assert_eq!(packed.node.glue_sign, GlueSign::Normal);
assert_eq!(packed.badness, 0);
}
#[test]
fn stretching_sets_the_glue_ratio_and_the_badness() {
let list = vec![ch(pt(10), 0, 0), glue(pt(4), pt(2), pt(1)), ch(pt(6), 0, 0)];
let packed = hpack(list, Spec::Exactly(pt(21)), Tolerances::plain(), None);
assert_eq!(packed.node.width, pt(21));
assert_eq!(packed.node.glue_sign, GlueSign::Stretching);
assert_eq!(packed.node.glue_order, 0);
assert!((packed.node.glue_set - 0.5).abs() < 1e-12);
assert_eq!(packed.badness, badness(pt(1), pt(2)));
assert_eq!(packed.badness, 12);
assert_eq!(packed.report, None);
}
#[test]
fn an_underfull_box_is_reported_the_way_tex_reports_it() {
let list = vec![ch(pt(10), 0, 0), glue(pt(4), pt(2), pt(1))];
let packed = hpack(list, Spec::Exactly(pt(20)), Tolerances::plain(), None);
assert_eq!(packed.badness, 2698);
assert_eq!(packed.report, Some(Report::Underfull(2698)));
assert_eq!(
packed.report.expect("reported").message(false),
"Underfull \\hbox (badness 2698)"
);
let list = vec![ch(pt(10), 0, 0), glue(pt(4), pt(2), pt(1))];
let quiet = Tolerances {
hbadness: 10000,
..Tolerances::plain()
};
let packed = hpack(list, Spec::Exactly(pt(20)), quiet, None);
assert_eq!(packed.badness, 2698);
assert_eq!(packed.report, None);
}
#[test]
fn an_overfull_box_says_how_far_past_the_measure_it_runs() {
let list = vec![ch(pt(10), 0, 0), glue(pt(4), pt(2), pt(1)), ch(pt(6), 0, 0)];
let packed = hpack(list, Spec::Exactly(pt(15)), Tolerances::plain(), None);
assert_eq!(packed.badness, 1_000_000);
assert_eq!(packed.report, Some(Report::Overfull(pt(4))));
assert_eq!(
packed.report.expect("reported").message(false),
"Overfull \\hbox (4.0pt too wide)"
);
assert!((packed.node.glue_set - 1.0).abs() < 1e-12);
}
#[test]
fn only_the_highest_order_of_infinity_stretches() {
let spec = |stretch: Scaled, order: Order| {
Node::Glue(GlueNode::new(Glue {
stretch,
stretch_order: order,
..Glue::default()
}))
};
let list = vec![
spec(pt(3), 0),
spec(pt(8), 2),
spec(pt(5), 1),
spec(pt(6), 0),
spec(-pt(3), 1),
spec(-pt(8), 2),
];
let packed = hpack(list, Spec::Exactly(pt(6)), Tolerances::plain(), None);
assert_eq!(packed.node.glue_order, 1);
assert_eq!(packed.node.glue_sign, GlueSign::Stretching);
assert!((packed.node.glue_set - 3.0).abs() < 1e-12);
let widths = glue_widths(&packed.node);
assert_eq!(widths, vec![0, 0, pt(15), 0, -pt(9), 0]);
assert_eq!(packed.badness, 0);
assert_eq!(packed.report, None);
}
#[test]
fn accumulated_rounding_makes_the_widths_sum_to_the_box() {
let mut list: Vec<Node> = Vec::new();
for _ in 0..40 {
list.push(ch(pt(1), 0, 0));
list.push(glue(pt(1), pt(1), pt(1)));
}
let target = pt(80) + 12345;
let packed = hpack(list, Spec::Exactly(target), Tolerances::plain(), None);
let widths = glue_widths(&packed.node);
assert_eq!(widths.len(), 40);
let set: Scaled = widths.iter().sum();
assert_eq!(set + pt(40), target);
}
#[test]
fn a_vbox_stacks_heights_and_keeps_the_last_depth() {
let boxed = |h: Scaled, d: Scaled, w: Scaled| {
Node::Box(BoxNode {
width: w,
height: h,
depth: d,
..BoxNode::null()
})
};
let list = vec![
boxed(pt(7), pt(2), pt(30)),
Node::Glue(GlueNode::new(Glue::fixed(pt(3)))),
boxed(pt(5), pt(4), pt(50)),
];
let packed = vpack(list, NATURAL, Tolerances::plain());
assert_eq!(packed.node.height, pt(17));
assert_eq!(packed.node.depth, pt(4));
assert_eq!(packed.node.width, pt(50));
}
#[test]
fn depth_past_the_limit_becomes_height() {
let list = vec![Node::Box(BoxNode {
height: pt(10),
depth: pt(6),
..BoxNode::null()
})];
let packed = vpackage(list, NATURAL, pt(2), Tolerances::plain());
assert_eq!(packed.node.depth, pt(2));
assert_eq!(packed.node.height, pt(14));
}
#[test]
fn a_vtop_takes_its_height_from_its_first_box() {
let list = vec![
Node::Box(BoxNode {
height: pt(7),
depth: pt(2),
..BoxNode::null()
}),
Node::Box(BoxNode {
height: pt(5),
depth: pt(3),
..BoxNode::null()
}),
];
let packed = vtop(vpack(list, NATURAL, Tolerances::plain()));
assert_eq!(packed.node.height, pt(7));
assert_eq!(packed.node.depth, pt(10));
}
#[test]
fn interline_glue_puts_the_baselines_where_baselineskip_says() {
let mut list = Vec::new();
let mut prev_depth = IGNORE_DEPTH;
let p = Baselines::plain();
let first = BoxNode {
height: pt(7),
depth: pt(2),
..BoxNode::null()
};
append_to_vlist(&mut list, first, &mut prev_depth, p);
assert_eq!(list.len(), 1);
assert_eq!(prev_depth, pt(2));
let second = BoxNode {
height: pt(4),
depth: pt(1),
..BoxNode::null()
};
append_to_vlist(&mut list, second, &mut prev_depth, p);
assert_eq!(list.len(), 3);
let Node::Glue(g) = &list[1] else {
panic!("interline glue is a glue node");
};
assert_eq!(g.spec.natural, pt(6));
assert_eq!(g.source, GlueSource::BaselineSkip);
assert_eq!(pt(2) + g.spec.natural + pt(4), pt(12));
}
#[test]
fn lineskip_takes_over_when_the_lines_would_touch() {
let mut list = Vec::new();
let mut prev_depth = pt(6);
let p = Baselines::plain();
let tall = BoxNode {
height: pt(9),
depth: pt(1),
..BoxNode::null()
};
append_to_vlist(&mut list, tall, &mut prev_depth, p);
let Node::Glue(g) = &list[0] else {
panic!("interline glue is a glue node");
};
assert_eq!(g.spec.natural, pt(1));
assert_eq!(g.source, GlueSource::LineSkip);
}
#[test]
fn hpack_moves_insertions_and_adjustments_out_of_the_line() {
let list = vec![
ch(pt(10), 0, 0),
Node::Ins(crate::node::InsNode {
number: 1,
height: pt(20),
depth: 0,
float_cost: 100,
split_top_skip: Glue::default(),
list: Vec::new(),
}),
Node::Mark("chapter".into()),
Node::Adjust(vec![Node::Penalty(-50)]),
ch(pt(6), 0, 0),
];
let mut adjust = Vec::new();
let packed = hpack(list, NATURAL, Tolerances::plain(), Some(&mut adjust));
assert_eq!(packed.node.width, pt(16));
assert_eq!(packed.node.list.len(), 2);
assert_eq!(adjust.len(), 3);
assert!(matches!(adjust[0], Node::Ins(_)));
assert!(matches!(adjust[1], Node::Mark(_)));
assert!(matches!(adjust[2], Node::Penalty(-50)));
let list = vec![ch(pt(10), 0, 0), Node::Mark("chapter".into())];
let packed = hpack(list, NATURAL, Tolerances::plain(), None);
assert_eq!(packed.node.list.len(), 2);
}
#[test]
fn a_running_rule_takes_the_dimensions_of_its_box() {
let container = BoxNode {
height: pt(8),
depth: pt(3),
width: pt(100),
..BoxNode::null()
};
let resolved = resolve_rule(RuleNode::running(), &container);
assert_eq!(resolved.height, pt(8));
assert_eq!(resolved.depth, pt(3));
assert!(RuleNode::is_running(resolved.width));
let vertical = BoxNode {
width: pt(100),
vertical: true,
..BoxNode::null()
};
let resolved = resolve_rule(RuleNode::running(), &vertical);
assert_eq!(resolved.width, pt(100));
assert!(RuleNode::is_running(resolved.height));
}
#[test]
fn leaders_lend_their_height_to_the_line() {
let dots = BoxNode {
width: pt(5),
height: pt(6),
depth: pt(1),
..BoxNode::null()
};
let list = vec![Node::Glue(GlueNode {
spec: Glue {
stretch: UNITY,
stretch_order: 1,
..Glue::default()
},
kind: crate::node::LeaderKind::Aligned,
leader: Some(Box::new(Node::Box(dots))),
..GlueNode::default()
})];
let packed = hpack(list, Spec::Exactly(pt(50)), Tolerances::plain(), None);
assert_eq!(packed.node.height, pt(6));
assert_eq!(packed.node.depth, pt(1));
}
#[test]
fn a_box_with_nothing_to_stretch_is_still_the_width_asked_for() {
let list = vec![ch(pt(10), 0, 0)];
let packed = hpack(list, Spec::Exactly(pt(30)), Tolerances::plain(), None);
assert_eq!(packed.node.width, pt(30));
assert_eq!(packed.node.glue_sign, GlueSign::Normal);
assert_eq!(packed.node.glue_set, 0.0);
}
#[test]
fn an_empty_box_is_never_underfull() {
let packed = hpack(
Vec::new(),
Spec::Exactly(pt(100)),
Tolerances::default(),
None,
);
assert_eq!(packed.report, None);
assert_eq!(packed.badness, 0);
assert_eq!(packed.node.width, pt(100));
}
#[test]
fn a_higher_infinity_takes_all_of_the_slack() {
let list = vec![fil(1), ch(pt(10), 0, 0), fil(2)];
let packed = hpack(list, Spec::Exactly(pt(50)), Tolerances::plain(), None);
assert_eq!(packed.node.glue_order, 2);
let widths = glue_widths(&packed.node);
assert_eq!(widths, vec![0, pt(40)]);
}
#[test]
fn aligned_leaders_sit_on_the_boxs_grid_and_not_on_the_glues() {
let dot = pt(10);
let from_25 = leader_positions(LeaderKind::Aligned, dot, pt(40), pt(25), 0);
assert_eq!(from_25, vec![pt(30), pt(40), pt(50)]);
let from_27 = leader_positions(LeaderKind::Aligned, dot, pt(40), pt(27), 0);
assert_eq!(from_27, vec![pt(30), pt(40), pt(50)]);
let shifted = leader_positions(LeaderKind::Aligned, dot, pt(40), pt(25), pt(3));
assert_eq!(shifted, vec![pt(33), pt(43), pt(53)]);
}
#[test]
fn cleaders_centre_the_copies_and_xleaders_spread_them() {
let dot = pt(10);
let space = pt(25);
let centred = leader_positions(LeaderKind::Centred, dot, space, 0, 0);
assert_eq!(centred, vec![163_845, 819_205]);
let edge = space + 10;
assert_eq!(
centred[0],
edge - (centred[1] + dot),
"the same slack before the first copy and after the last"
);
assert_eq!(centred[1] - centred[0], dot, "no gap between the copies");
let expanded = leader_positions(LeaderKind::Expanded, dot, space, 0, 0);
assert_eq!(expanded, vec![109_230, 873_820]);
assert_eq!(expanded.len(), centred.len(), "the same number of copies");
assert_eq!(
expanded[0],
edge - (expanded[1] + dot),
"still symmetric about the space"
);
assert!(
expanded[1] - expanded[0] > centred[1] - centred[0],
"the copies are further apart than \\cleaders puts them"
);
}
#[test]
fn the_ten_scaled_points_of_slack_keep_the_last_copy() {
let dot = pt(10);
let space = 3 * dot - 5;
let with_slack = leader_positions(LeaderKind::Aligned, dot, space, 0, 0);
assert_eq!(with_slack.len(), 3);
let strict = (0..)
.map(|i| i * dot)
.take_while(|h| h + dot <= space)
.count();
assert_eq!(strict, 2);
}
#[test]
fn leaders_too_wide_for_their_space_draw_nothing() {
assert!(leader_positions(LeaderKind::Aligned, pt(20), pt(15), 0, 0).is_empty());
assert!(leader_positions(LeaderKind::Centred, pt(20), pt(15), 0, 0).is_empty());
assert!(leader_positions(LeaderKind::Expanded, pt(20), pt(15), 0, 0).is_empty());
assert!(leader_positions(LeaderKind::Aligned, pt(10), 0, 0, 0).is_empty());
assert!(leader_positions(LeaderKind::Normal, pt(10), pt(100), 0, 0).is_empty());
}
}