Apply TeX sentence spacing to interword glue

This commit is contained in:
gloamxun committed 2026-10-23 01:26:04 +08:00
1 parent bd109d03c5
commit 295c934438
8 files changed
+137 -23

No files matched your search

+4
View File
@@ -138,6 +138,10 @@ impl Cmr10Metrics {
pub fn shrink(&self) -> i64 {
self.metrics().shrink()
}
pub fn extra_space(&self) -> i64 {
self.metrics().extra_space()
}
}
#[cfg(test)]
+81 -3
View File
@@ -94,6 +94,10 @@ pub struct ParagraphStyle {
/// Whether `\parfillskip` ends in `fil` glue (the TeX default).
#[serde(default = "default_par_fillskip_fil")]
pub par_fillskip_fil: bool,
/// `\frenchspacing`: every character gets space factor 1000, so
/// punctuation adds no sentence spacing.
#[serde(default)]
pub frenchspacing: bool,
/// Center each line in the measure instead of justifying it.
#[serde(default)]
pub centered: bool,
@@ -206,6 +210,7 @@ impl Default for ParagraphStyle {
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
centered: false,
right_aligned: false,
par_indent: 0,
@@ -835,12 +840,24 @@ pub fn text_items_with_font(
let words: Vec<&str> = text.split_whitespace().collect();
let mut previous_box: Option<usize> = None;
let mut previous_character: Option<char> = None;
// TeX's space factor starts at 1000 and is updated by each character's
// `\sfcode`; it scales the next interword glue (and adds `\fontdimen7`
// once it reaches 2000).
let mut space_factor = 1000_i64;
for (word_index, word) in words.iter().enumerate() {
if word_index > 0 {
let factor = space_factor.max(1000);
items.push(HorizontalItem::Glue {
width: font.space(),
stretch: font.stretch(),
shrink: font.shrink(),
width: font.space()
+ if factor >= 2000 {
font.extra_space()
} else {
0
},
stretch: font.stretch() * factor / 1000,
// TeX divides the shrink by the space factor (`\sfcode`
// shrinks less after punctuation) while multiplying stretch.
shrink: font.shrink() * 1000 / factor,
});
// TeX never kerns across interword glue.
previous_box = None;
@@ -905,6 +922,7 @@ pub fn text_items_with_font(
});
}
}
space_factor = space_factor_after(word, style.frenchspacing);
}
while matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
items.pop();
@@ -917,6 +935,21 @@ pub fn text_items_with_font(
items
}
/// TeX's default `\sfcode` values for sentence punctuation (`plain.tex`'s
/// `\nonfrenchspacing`); every other character is 1000.
fn space_factor_after(word: &str, frenchspacing: bool) -> i64 {
if frenchspacing {
return 1000;
}
match word.chars().last() {
Some('.') | Some('?') | Some('!') => 3000,
Some(':') => 2000,
Some(';') => 1500,
Some(',') => 1250,
_ => 1000,
}
}
/// Paragraph builder driven by a real TFM font instead of fixed widths.
pub fn layout_paragraph_with_font(
text: &str,
@@ -1374,4 +1407,49 @@ mod tests {
.sum();
assert_eq!(last_width, 100, "parfillskip leaves its width at the end");
}
#[test]
fn sentence_punctuation_scales_the_interword_glue() {
let style = ParagraphStyle::default();
let font = crate::cmr10::Cmr10Metrics::Roman;
let items = text_items_with_font("alpha. beta", &style, &font);
let (width, stretch, shrink) = items
.iter()
.find_map(|item| match item {
HorizontalItem::Glue {
width,
stretch,
shrink,
} => Some((*width, *stretch, *shrink)),
_ => None,
})
.expect("interword glue");
assert_eq!(width, font.space() + font.extra_space());
assert_eq!(stretch, font.stretch() * 3);
assert_eq!(shrink, font.shrink() * 1000 / 3000);
}
#[test]
fn frenchspacing_keeps_a_plain_space() {
let style = ParagraphStyle {
frenchspacing: true,
..ParagraphStyle::default()
};
let font = crate::cmr10::Cmr10Metrics::Roman;
let items = text_items_with_font("alpha. beta", &style, &font);
let (width, stretch, shrink) = items
.iter()
.find_map(|item| match item {
HorizontalItem::Glue {
width,
stretch,
shrink,
} => Some((*width, *stretch, *shrink)),
_ => None,
})
.expect("interword glue");
assert_eq!(width, font.space());
assert_eq!(stretch, font.stretch());
assert_eq!(shrink, font.shrink());
}
}
+6
View File
@@ -255,6 +255,12 @@ impl TfmMetrics {
pub fn quad(&self) -> i64 {
self.param(5)
}
/// `\fontdimen7`: extra space inserted after punctuation with a space
/// factor of 2000 or more.
pub fn extra_space(&self) -> i64 {
self.param(6)
}
}
fn parse_kerns(