Carry TeX plus and minus on paragraph skips

This commit is contained in:
gloamxun committed 2026-10-23 14:18:13 +08:00
1 parent 0102a20120
commit 94559ee431
3 files changed
+113 -14

No files matched your search

+68 -11
View File
@@ -511,21 +511,46 @@ struct SourceParagraph {
math: bool,
left_skip: i64,
space_before: i64,
space_stretch: i64,
space_shrink: i64,
}
/// Recognise an explicit vertical skip command standing alone between
/// paragraphs.
fn vertical_skip(text: &str) -> Option<i64> {
fn vertical_skip(text: &str) -> Option<(i64, i64, i64)> {
match text.trim() {
"\\smallskip" => Some(3 * SCALE),
"\\medskip" => Some(6 * SCALE),
"\\bigskip" => Some(12 * SCALE),
"\\smallskip" => Some((3 * SCALE, SCALE, SCALE)),
"\\medskip" => Some((6 * SCALE, 2 * SCALE, 2 * SCALE)),
"\\bigskip" => Some((12 * SCALE, 4 * SCALE, 4 * SCALE)),
other => other
.strip_prefix("\\vskip")
.and_then(|rest| parse_dimension(rest.trim_start())),
.and_then(|rest| skip_glue(rest.trim_start())),
}
}
/// Parse `\vskip` glue: a width with optional `plus` and `minus` parts.
fn skip_glue(rest: &str) -> Option<(i64, i64, i64)> {
let mut width = None;
let mut stretch = 0_i64;
let mut shrink = 0_i64;
let mut mode = 0_u8;
for token in rest.split_whitespace() {
match token {
"plus" => mode = 1,
"minus" => mode = 2,
_ => {
let value = parse_dimension(token)?;
match mode {
0 => width = Some(value),
1 => stretch = value,
_ => shrink = value,
}
}
}
}
Some((width?, stretch, shrink))
}
fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
let paragraphs: Vec<&str> = source
.split("\n\n")
@@ -536,16 +561,20 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
return Err("route-D preview needs at least one paragraph".to_string());
}
let mut result = Vec::new();
let mut pending = 0_i64;
let mut pending = (0_i64, 0_i64, 0_i64);
for chunk in paragraphs {
if let Some(skip) = vertical_skip(chunk) {
pending += skip;
pending.0 += skip.0;
pending.1 += skip.1;
pending.2 += skip.2;
continue;
}
let mut blocks = parse_blocks(chunk);
if let Some(first) = blocks.first_mut() {
first.space_before += pending;
pending = 0;
first.space_before += pending.0;
first.space_stretch += pending.1;
first.space_shrink += pending.2;
pending = (0, 0, 0);
}
result.extend(blocks);
}
@@ -589,6 +618,8 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
}
})
.collect();
@@ -611,6 +642,8 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
}];
}
}
@@ -642,6 +675,8 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
}
})
.collect();
@@ -673,6 +708,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
};
}
let candidates = [
@@ -691,6 +728,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
};
}
}
@@ -703,6 +742,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
}
}
@@ -718,6 +759,8 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
&Cmr10Metrics::default(),
);
unit.space_before = paragraph.space_before;
unit.space_stretch = paragraph.space_stretch;
unit.space_shrink = paragraph.space_shrink;
return unit;
}
let style = ParagraphStyle {
@@ -775,6 +818,8 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
paragraph.font,
);
unit.space_before = paragraph.space_before;
unit.space_stretch = paragraph.space_stretch;
unit.space_shrink = paragraph.space_shrink;
unit
})
.collect();
@@ -1086,12 +1131,19 @@ mod source_tests {
}
#[test]
fn paragraphs_read_vertical_skip_commands() {
let source = "Alpha.\n\n\\medskip\n\nBeta.\n\n\\bigskip\n\nGamma.";
let source = "Alpha.\n\n\\medskip\n\nBeta.\n\n\\bigskip\n\nGamma.\n\n\\vskip 5pt plus 2pt minus 1pt\n\nDelta.";
let parsed = paragraphs(source).expect("paragraphs");
assert_eq!(parsed.len(), 3);
assert_eq!(parsed.len(), 4);
assert_eq!(parsed[0].space_before, 0);
assert_eq!(parsed[1].space_before, 6 * SCALE);
assert_eq!(parsed[1].space_stretch, 2 * SCALE);
assert_eq!(parsed[1].space_shrink, 2 * SCALE);
assert_eq!(parsed[2].space_before, 12 * SCALE);
assert_eq!(parsed[2].space_stretch, 4 * SCALE);
assert_eq!(parsed[2].space_shrink, 4 * SCALE);
assert_eq!(parsed[3].space_before, 5 * SCALE);
assert_eq!(parsed[3].space_stretch, 2 * SCALE);
assert_eq!(parsed[3].space_shrink, SCALE);
assert!(
!parsed
.iter()
@@ -1099,6 +1151,11 @@ mod source_tests {
);
let document = build_document(&parsed, &PageGeometry::default());
assert_eq!(document.units[1].space_before, 6 * SCALE);
assert_eq!(document.units[1].space_stretch, 2 * SCALE);
assert_eq!(document.units[1].space_shrink, 2 * SCALE);
assert_eq!(document.units[2].space_before, 12 * SCALE);
assert_eq!(document.units[3].space_before, 5 * SCALE);
assert_eq!(document.units[3].space_stretch, 2 * SCALE);
assert_eq!(document.units[3].space_shrink, SCALE);
}
}