Honour explicit sfcode assignments

This commit is contained in:
gloamxun committed 2026-10-23 02:29:40 +08:00
1 parent b5ec36ae2a
commit 4acccaee86
7 files changed
+99 -18

No files matched your search

+70 -8
View File
@@ -98,6 +98,10 @@ pub struct ParagraphStyle {
/// punctuation adds no sentence spacing.
#[serde(default)]
pub frenchspacing: bool,
/// Per-character `\sfcode` overrides (the last matching assignment wins).
/// A code of zero leaves the previous space factor unchanged, as in TeX.
#[serde(default)]
pub space_factor_codes: Vec<(char, i64)>,
/// TeX `\baselineskip` (12pt for 10pt text).
#[serde(default = "default_baselineskip")]
pub baselineskip: i64,
@@ -228,6 +232,7 @@ impl Default for ParagraphStyle {
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
space_factor_codes: Vec::new(),
baselineskip: 12 * 65_536,
lineskip: 65_536,
lineskiplimit: 0,
@@ -1017,7 +1022,12 @@ pub fn text_items_with_font(
});
}
}
space_factor = space_factor_after(word, style.frenchspacing);
space_factor = space_factor_after(
word,
space_factor,
style.frenchspacing,
&style.space_factor_codes,
);
}
while matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
items.pop();
@@ -1031,16 +1041,29 @@ pub fn text_items_with_font(
}
/// 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 {
/// `\nonfrenchspacing`); every other character is 1000. Explicit
/// `\sfcode` assignments in `space_factor_codes` override the defaults, and a
/// zero code leaves the running factor unchanged.
fn space_factor_after(
word: &str,
previous: i64,
frenchspacing: bool,
codes: &[(char, i64)],
) -> i64 {
if frenchspacing {
return 1000;
}
match word.chars().last() {
Some('.') | Some('?') | Some('!') => 3000,
Some(':') => 2000,
Some(';') => 1500,
Some(',') => 1250,
let Some(last) = word.chars().last() else {
return previous;
};
if let Some((_, code)) = codes.iter().rev().find(|(character, _)| *character == last) {
return if *code == 0 { previous } else { *code };
}
match last {
'.' | '?' | '!' => 3000,
':' => 2000,
';' => 1500,
',' => 1250,
_ => 1000,
}
}
@@ -1524,6 +1547,45 @@ mod tests {
assert_eq!(shrink, font.shrink() * 1000 / 3000);
}
#[test]
fn explicit_space_factor_code_overrides_sentence_spacing() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let glue_width = |style: &ParagraphStyle| {
text_items_with_font("alpha. beta", style, &font)
.iter()
.find_map(|item| match item {
HorizontalItem::Glue { width, .. } => Some(*width),
_ => None,
})
.expect("interword glue")
};
let default = glue_width(&ParagraphStyle::default());
let style = ParagraphStyle {
space_factor_codes: vec![('.', 1000)],
..ParagraphStyle::default()
};
assert_eq!(glue_width(&style), font.space());
assert!(glue_width(&style) < default);
}
#[test]
fn zero_space_factor_code_keeps_the_previous_factor() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
space_factor_codes: vec![('.', 0)],
..ParagraphStyle::default()
};
let width = text_items_with_font("alpha. beta", &style, &font)
.iter()
.find_map(|item| match item {
HorizontalItem::Glue { width, .. } => Some(*width),
_ => None,
})
.expect("interword glue");
// "alpha" leaves the factor at 1000, and a zero `\sfcode` keeps it.
assert_eq!(width, font.space());
}
#[test]
fn baselines_use_baselineskip_then_lineskip() {
let font = crate::cmr10::Cmr10Metrics::new();
+12
View File
@@ -363,6 +363,18 @@ fn sentence_spacing_matches_pdftex_at_150pt() {
assert_lines_match("sentence spacing 150pt", &ours, &reference);
}
#[test]
fn explicit_sfcode_matches_pdftex() {
let width = 150;
let text = "alpha. beta gamma, delta; epsilon: zeta eta theta iota kappa lambda mu";
// `\sfcode`.=1000` makes the period behave like an ordinary character for
// the following interword glue, so the sentence space shrinks.
let prefix = "\\sfcode`.=1000\\relax ";
compare_fixture_with(text, width, prefix, |style| {
style.space_factor_codes = vec![('.', 1000)];
});
}
#[test]
fn paragraph_breaks_match_pdftex_at_150pt() {
compare_fixture(WORDS, 150);
+4
View File
@@ -23,6 +23,7 @@ fn body_style() -> ParagraphStyle {
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
space_factor_codes: Vec::new(),
baselineskip: 12 * 65_536,
lineskip: 65_536,
lineskiplimit: 0,
@@ -189,6 +190,7 @@ fn route_d_page_matches_pdftex_raster_geometry() {
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
space_factor_codes: Vec::new(),
baselineskip: 12 * 65_536,
lineskip: 65_536,
lineskiplimit: 0,
@@ -364,6 +366,7 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
space_factor_codes: Vec::new(),
baselineskip: 12 * 65_536,
lineskip: 65_536,
lineskiplimit: 0,
@@ -665,6 +668,7 @@ fn route_d_multiline_bands_track_pdftex() {
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
space_factor_codes: Vec::new(),
baselineskip: 12 * 65_536,
lineskip: 65_536,
lineskiplimit: 0,