Add bounded line expansion for Times body text

This commit is contained in:
gloamxun committed 2026-10-28 23:19:16 +08:00
1 parent 8fef7df187
commit 01b3f518cb
10 files changed
+113 -9

No files matched your search

+2
View File
@@ -7758,6 +7758,7 @@ mod tests {
left_skip: 0,
line_indents: vec![0; 10],
line_baselines: (0..10).map(|line| 8 + line * 10).collect(),
line_scales: Vec::new(),
centered: false,
right_aligned: false,
};
@@ -9468,6 +9469,7 @@ b & 2 \\
left_skip: 0,
line_indents: vec![0],
line_baselines: vec![8 * 65_536],
line_scales: Vec::new(),
centered: false,
right_aligned: false,
};
+76 -1
View File
@@ -136,6 +136,11 @@ fn default_font_size() -> i64 {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParagraphStyle {
pub line_width: i64,
/// Bounded line expansion in thousandths, microtype-like. A line may be
/// broken up to `line_width * (1000 + line_expansion) / 1000` and is
/// compressed back to the real measure when painted.
#[serde(default)]
pub line_expansion: i64,
/// Left skip inserted before every line (`\leftskip`).
pub left_skip: i64,
/// Right skip reserved at the end of every line (`\rightskip`).
@@ -299,16 +304,22 @@ impl ParagraphStyle {
}
}
fn line_target(&self, line: usize) -> i64 {
pub fn line_measure(&self, line: usize) -> i64 {
let (_, width) = self.line_geometry(line);
(width - self.left_skip - self.right_skip).max(1) - self.par_indent_for(line)
}
fn line_target(&self, line: usize) -> i64 {
let measure = self.line_measure(line);
(measure * (1000 + self.line_expansion) / 1000).max(measure)
}
}
impl Default for ParagraphStyle {
fn default() -> Self {
Self {
line_width: 400,
line_expansion: 0,
left_skip: 0,
right_skip: 0,
hang_indent: 0,
@@ -559,6 +570,10 @@ pub struct Paragraph {
/// Baseline of each line relative to the top of the paragraph box.
#[serde(default)]
pub line_baselines: Vec<i64>,
/// Horizontal compression per line (thousandths), used by the bounded
/// expansion model when a line was broken against an expanded measure.
#[serde(default)]
pub line_scales: Vec<i64>,
#[serde(default)]
pub centered: bool,
#[serde(default)]
@@ -597,6 +612,11 @@ impl Paragraph {
let tail_depth = line_extent(tail_lines.last().map(Vec::as_slice).unwrap_or(&[])).1;
self.height = head_height;
self.depth = head_depth;
let tail_scales = if self.line_scales.len() > lines {
self.line_scales.split_off(lines)
} else {
Vec::new()
};
Paragraph {
lines: tail_lines,
height: tail_height,
@@ -607,6 +627,7 @@ impl Paragraph {
left_skip: self.left_skip,
line_indents: tail_indents,
line_baselines: tail_baselines,
line_scales: tail_scales,
centered: self.centered,
right_aligned: self.right_aligned,
}
@@ -778,6 +799,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
left_skip: style.left_skip,
line_indents: Vec::new(),
line_baselines: Vec::new(),
line_scales: Vec::new(),
centered: style.centered,
right_aligned: style.right_aligned,
};
@@ -1436,6 +1458,27 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
let depth = extents.last().map_or(0, |(_, descent)| *descent);
let line_scales: Vec<i64> = lines
.iter()
.enumerate()
.map(|(index, line)| {
let actual = style.line_measure(index).max(1);
let natural: i64 = line
.iter()
.map(|item| match item {
HorizontalItem::Box(box_item) => box_item.width,
HorizontalItem::Glue { width, .. } => *width,
HorizontalItem::Penalty { width, .. } => *width,
HorizontalItem::Discretionary { .. } => 0,
})
.sum();
if natural > actual {
(actual * 1000 / natural).clamp(900, 1000)
} else {
1000
}
})
.collect();
Paragraph {
// The page builder still budgets paragraphs on the line grid; the
// baseline table below carries TeX's interline glue for painting.
@@ -1452,6 +1495,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
left_skip: style.left_skip,
line_indents,
line_baselines,
line_scales,
centered: style.centered,
right_aligned: style.right_aligned,
}
@@ -2142,6 +2186,37 @@ mod tests {
.count()
}
#[test]
fn bounded_expansion_records_per_line_scales() {
let text = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu";
let base = ParagraphStyle {
line_width: 180,
..ParagraphStyle::default()
};
let expanded = ParagraphStyle {
line_width: 180,
line_expansion: 30,
..ParagraphStyle::default()
};
let plain = layout_paragraph(text, &base);
let stretched = layout_paragraph(text, &expanded);
assert!(stretched.lines.len() <= plain.lines.len());
assert_eq!(stretched.line_scales.len(), stretched.lines.len());
assert!(
stretched
.line_scales
.iter()
.all(|scale| (900..=1000).contains(scale)),
"{:?}",
stretched.line_scales
);
assert!(
stretched.line_scales.iter().any(|scale| *scale < 1000),
"no line was compressed: {:?}",
stretched.line_scales
);
}
#[test]
fn font_size_scales_glyphs_and_line_spacing() {
let font = crate::cmr10::Cmr10Metrics::Roman;
+4
View File
@@ -14,6 +14,7 @@ use texrra_tex_core::{
fn body_style() -> ParagraphStyle {
let font = Cmr10Metrics::default();
ParagraphStyle {
line_expansion: 0,
font_size: 10 * 65_536,
line_width: 480 * 65_536,
left_skip: 0,
@@ -214,6 +215,7 @@ fn route_d_page_matches_pdftex_raster_geometry() {
std::fs::create_dir_all(&root).expect("create raster dir");
let style = ParagraphStyle {
line_expansion: 0,
font_size: 10 * 65_536,
line_width: 480 * 65_536,
left_skip: 0,
@@ -395,6 +397,7 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
std::fs::create_dir_all(&root).expect("create raster dir");
let style = ParagraphStyle {
line_expansion: 0,
font_size: 10 * 65_536,
line_width: 480 * 65_536,
left_skip: 0,
@@ -791,6 +794,7 @@ fn route_d_multiline_bands_track_pdftex() {
let text = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu";
let style = ParagraphStyle {
line_expansion: 0,
font_size: 10 * 65_536,
line_width: 150 * 65_536,
left_skip: 0,