Replace font glue with explicit spaceskip and xspaceskip

This commit is contained in:
gloamxun committed 2026-10-23 02:38:12 +08:00
1 parent 4acccaee86
commit 4be0ff5787
7 files changed
+161 -25

No files matched your search

+111 -17
View File
@@ -159,6 +159,14 @@ pub struct ParagraphStyle {
pub space_width: i64,
pub space_stretch: i64,
pub space_shrink: i64,
/// TeX `\spaceskip`: explicit interword glue `(width, stretch, shrink)`
/// that replaces the font glue when set.
#[serde(default)]
pub space_skip: Option<(i64, i64, i64)>,
/// TeX `\xspaceskip`: glue used instead of `\spaceskip` once the space
/// factor reaches 2000.
#[serde(default)]
pub xspace_skip: Option<(i64, i64, i64)>,
}
fn default_par_fillskip_fil() -> bool {
@@ -254,6 +262,8 @@ impl Default for ParagraphStyle {
space_width: 4,
space_stretch: 6,
space_shrink: 2,
space_skip: None,
xspace_skip: None,
}
}
}
@@ -947,33 +957,68 @@ pub fn text_items_with_font(
for (word_index, word) in words.iter().enumerate() {
if word_index > 0 {
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
// `\fontdimen7` is still added from factor 2000 on.
// TeX uses `\xspaceskip` verbatim once the space factor reaches
// 2000 (pdfTeX dumps it as 8.0 plus 4.0 minus 2.0 even at 3000).
// `\spaceskip` keeps its width but its stretch/shrink are scaled
// by the space factor like normal font glue; fontdimen7 is never
// added when an explicit skip is in force.
let (width, stretch, shrink) = if factor >= 2000
&& let Some((width, stretch, shrink)) = style.xspace_skip
{
(width, stretch, shrink)
} else if let Some((width, stretch, shrink)) = style.space_skip {
(width, stretch * factor / 1000, shrink * 1000 / factor)
} else {
(
font.space()
+ if factor >= 2000 {
font.extra_space()
} else {
0
},
font.stretch() * factor / 1000,
font.shrink() * 1000 / factor,
)
};
items.push(HorizontalItem::Glue {
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,
width,
stretch,
shrink,
});
// TeX never kerns across interword glue.
previous_box = None;
previous_character = None;
}
let points: std::collections::HashSet<usize> = if word.is_ascii()
&& word
.chars()
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
// lookup (for example `epsilon:` can still break as `ep-`).
let lead = original
.iter()
.take_while(|character| !character.is_ascii_alphabetic())
.count();
let trail = original
.iter()
.rev()
.take_while(|character| !character.is_ascii_alphabetic())
.count();
let core_len = original.len().saturating_sub(lead + trail);
let points: std::collections::HashSet<usize> = if core_len >= 5
&& original[lead..lead + core_len]
.iter()
.all(|character| character.is_ascii_alphabetic())
&& word.len() >= 5
{
hyphenator.calculate_indices(&caser, word).collect()
let core: String = original[lead..lead + core_len].iter().collect();
hyphenator
.calculate_indices(&caser, &core)
.map(|boundary| boundary + lead)
.collect()
} else {
std::collections::HashSet::new()
};
let original: Vec<char> = word.chars().collect();
let mut resolved: Vec<(char, char)> = Vec::new();
let mut cursor = 0;
while cursor < original.len() {
@@ -1014,7 +1059,7 @@ pub fn text_items_with_font(
if !ligated
&& points.contains(&boundary)
&& boundary >= 2
&& boundary + 3 <= word.chars().count()
&& boundary + 3 <= lead + core_len
{
items.push(HorizontalItem::Discretionary {
pre_break_width: font.width('-'),
@@ -1586,6 +1631,55 @@ mod tests {
assert_eq!(width, font.space());
}
#[test]
fn explicit_spaceskip_replaces_the_font_glue() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
space_skip: Some((6 * 65_536, 65_536, 32_768)),
..ParagraphStyle::default()
};
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, 6 * 65_536);
assert_eq!(stretch, 65_536);
assert_eq!(shrink, 32_768);
}
#[test]
fn xspaceskip_applies_after_sentence_punctuation() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let style = ParagraphStyle {
space_skip: Some((6 * 65_536, 65_536, 32_768)),
xspace_skip: Some((8 * 65_536, 4 * 65_536, 2 * 65_536)),
..ParagraphStyle::default()
};
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, 8 * 65_536);
assert_eq!(stretch, 4 * 65_536);
assert_eq!(shrink, 2 * 65_536);
}
#[test]
fn baselines_use_baselineskip_then_lineskip() {
let font = crate::cmr10::Cmr10Metrics::new();