Support explicit hfil and hfill glue orders

This commit is contained in:
gloamxun committed 2026-10-23 13:15:40 +08:00
1 parent 3df21201c2
commit d63db8d12d
4 files changed
+146 -16

No files matched your search

+129 -9
View File
@@ -20,6 +20,9 @@ pub enum HorizontalItem {
width: i64,
stretch: i64,
shrink: i64,
/// TeX glue order: 0 normal, 1 fil, 2 fill, 3 filll.
#[serde(default)]
order: u8,
},
Penalty {
width: i64,
@@ -364,6 +367,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
width: style.space_width,
stretch: style.space_stretch,
shrink: style.space_shrink,
order: 0,
});
}
} else {
@@ -477,6 +481,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
let mut natural = 0_i64;
let mut stretch = 0_i64;
let mut shrink = 0_i64;
let mut stretch_by_order = [0_i64; 4];
let mut has_box = false;
let mut trailing_penalty = 0_i64;
for (offset, item) in items[start..end].iter().enumerate() {
@@ -489,13 +494,18 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
width,
stretch: stretch_amount,
shrink: shrink_amount,
order,
..
} => {
if start + offset + 1 == end {
continue;
}
natural += *width;
stretch += *stretch_amount;
shrink += *shrink_amount;
match *order {
0 => stretch += *stretch_amount,
order => stretch_by_order[order as usize] += *stretch_amount,
}
}
HorizontalItem::Penalty { penalty, .. } => trailing_penalty = *penalty,
HorizontalItem::Discretionary {
@@ -537,9 +547,12 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
continue;
};
let target = style.line_target(node.line);
let infinite_order = (1..4).rev().find(|order| stretch_by_order[*order] > 0);
let stretch = stretch + extra_stretch;
let (mut ratio, mut badness) = if natural < target {
if stretch > 0 {
if infinite_order.is_some() {
(0.0, 0)
} else if stretch > 0 {
let ratio = (target - natural) as f64 / stretch as f64;
(ratio, (100.0 * ratio.powi(3)).round().min(10_000.0) as i64)
} else {
@@ -587,7 +600,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} else {
3
};
let fitness = if natural < target && stretch == 0 {
let fitness = if natural < target && stretch == 0 && infinite_order.is_none() {
3
} else {
fitness
@@ -673,6 +686,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
let mut natural = 0_i64;
let mut stretch = 0_i64;
let mut shrink = 0_i64;
let mut stretch_by_order = [0_i64; 4];
let mut has_box = false;
for (offset, item) in items[start..end].iter().enumerate() {
let last_item = start + offset + 1 == end;
@@ -685,13 +699,18 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
width,
stretch: stretch_amount,
shrink: shrink_amount,
order,
..
} => {
if last_item {
continue;
}
natural += *width;
stretch += *stretch_amount;
shrink += *shrink_amount;
match *order {
0 => stretch += *stretch_amount,
order => stretch_by_order[order as usize] += *stretch_amount,
}
}
HorizontalItem::Penalty { .. } => {}
HorizontalItem::Discretionary {
@@ -717,10 +736,13 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
if overflow.is_none() && natural >= target {
overflow = Some(end_index);
}
let infinite_order = (1..4).rev().find(|order| stretch_by_order[*order] > 0);
let mut badness = if final_line && style.par_fillskip_fil && natural <= target {
0
} else if natural < target {
if stretch > 0 {
if infinite_order.is_some() {
0
} else if stretch > 0 {
(100.0 * ((target - natural) as f64 / stretch as f64).powi(3))
.round()
.min(10_000.0) as i64
@@ -799,6 +821,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
// glue, so the shipped line (and therefore the PDF) is flush.
let target = style.line_target(line_index);
let (mut natural, mut stretch, mut shrink) = (0_i64, 0_i64, 0_i64);
let mut stretch_by_order = [0_i64; 4];
for item in &line {
match item {
HorizontalItem::Box(box_item) => natural += box_item.width,
@@ -806,10 +829,15 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
width,
stretch: stretch_amount,
shrink: shrink_amount,
order,
..
} => {
natural += *width;
stretch += *stretch_amount;
shrink += *shrink_amount;
match *order {
0 => stretch += *stretch_amount,
order => stretch_by_order[order as usize] += *stretch_amount,
}
}
HorizontalItem::Penalty { width, .. } => natural += *width,
HorizontalItem::Discretionary { .. } => {}
@@ -839,18 +867,28 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} else {
(target - natural).max(-shrink)
};
let infinite_order = (1..4).rev().find(|order| stretch_by_order[*order] > 0);
let chosen_order = infinite_order.unwrap_or(0);
let chosen_stretch = if chosen_order == 0 {
stretch
} else {
stretch_by_order[chosen_order as usize]
};
if delta > 0 {
if !is_last || !style.par_fillskip_fil {
if !is_last || !style.par_fillskip_fil || infinite_order.is_some() {
let mut remaining = delta;
for item in &mut line {
if let HorizontalItem::Glue {
width,
stretch: stretch_amount,
order,
..
} = item
&& *order == chosen_order as u8
&& *stretch_amount > 0
&& chosen_stretch > 0
{
let share = delta * *stretch_amount / stretch;
let share = delta * *stretch_amount / chosen_stretch;
*width += share;
remaining -= share;
}
@@ -987,8 +1025,9 @@ pub fn text_items_with_font(
// `\sfcode`; it scales the next interword glue (and adds `\fontdimen7`
// once it reaches 2000).
let mut space_factor = 1000_i64;
let mut after_control_word = false;
for (word_index, word) in words.iter().enumerate() {
if word_index > 0 {
if word_index > 0 && !after_control_word {
let factor = space_factor.max(1000);
// TeX's `\spaceskip`/`\xspaceskip` replace the font's interword
// glue; the space factor still scales stretch and shrink, and
@@ -1020,11 +1059,25 @@ pub fn text_items_with_font(
width,
stretch,
shrink,
order: 0,
});
// TeX never kerns across interword glue.
previous_box = None;
previous_character = None;
}
if matches!(*word, "\\hfill" | "\\hfil") {
items.push(HorizontalItem::Glue {
width: 0,
stretch: 1,
shrink: 0,
order: if *word == "\\hfill" { 2 } else { 1 },
});
previous_box = None;
previous_character = None;
after_control_word = true;
continue;
}
after_control_word = false;
let original: Vec<char> = word.chars().collect();
// TeX hyphenates the letter run of a word: leading or trailing
// punctuation stays on the word and does not suppress the pattern
@@ -1795,6 +1848,7 @@ mod tests {
width,
stretch,
shrink,
..
} => Some((*width, *stretch, *shrink)),
_ => None,
})
@@ -1858,6 +1912,7 @@ mod tests {
width,
stretch,
shrink,
..
} => Some((*width, *stretch, *shrink)),
_ => None,
})
@@ -1883,6 +1938,7 @@ mod tests {
width,
stretch,
shrink,
..
} => Some((*width, *stretch, *shrink)),
_ => None,
})
@@ -1930,6 +1986,7 @@ mod tests {
width,
stretch,
shrink,
..
} => Some((*width, *stretch, *shrink)),
_ => None,
})
@@ -1938,4 +1995,67 @@ mod tests {
assert_eq!(stretch, font.stretch());
assert_eq!(shrink, font.shrink());
}
#[test]
fn hfill_absorbs_the_line_stretch() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
line_width: 200 * 65_536,
par_indent: 0,
line_height: 12 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font("A \\hfill B", &style, &font);
assert_eq!(paragraph.lines.len(), 1);
let line = &paragraph.lines[0];
let fill = line
.iter()
.filter(|item| matches!(item, HorizontalItem::Glue { order: 2, .. }))
.count();
assert_eq!(fill, 1, "\\hfill should be one fill-order glue");
let width: i64 = line
.iter()
.map(|item| match item {
HorizontalItem::Box(box_) => box_.width,
HorizontalItem::Glue { width, .. } => *width,
HorizontalItem::Penalty { width, .. } => *width,
HorizontalItem::Discretionary { .. } => 0,
})
.sum();
assert_eq!(width, 200 * 65_536);
}
#[test]
fn fill_glue_wins_over_fil() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
line_width: 300 * 65_536,
par_indent: 0,
line_height: 12 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font("A \\hfil B \\hfill C", &style, &font);
assert_eq!(paragraph.lines.len(), 1);
let line = &paragraph.lines[0];
let fil: i64 = line
.iter()
.filter_map(|item| match item {
HorizontalItem::Glue {
width, order: 1, ..
} => Some(*width),
_ => None,
})
.sum();
let fill: i64 = line
.iter()
.filter_map(|item| match item {
HorizontalItem::Glue {
width, order: 2, ..
} => Some(*width),
_ => None,
})
.sum();
assert_eq!(fil, 0, "\\hfil must stay at its natural width");
assert!(fill > 0, "\\hfill must absorb the whole page set");
}
}
+10
View File
@@ -795,3 +795,13 @@ fn leftskip_matches_pdftex() {
style.left_skip = 20 * 65_536;
});
}
#[test]
fn hfill_matches_pdftex() {
compare_fixture_with("alpha \\hfill beta", 150, "", |_| {});
}
#[test]
fn hfill_and_hfil_match_pdftex() {
compare_fixture_with("alpha \\hfil beta \\hfill gamma", 150, "", |_| {});
}