Replace font glue with explicit spaceskip and xspaceskip
This commit is contained in:
1 parent
4acccaee86
commit
4be0ff5787
7 files changed
+161
-25
No files matched your search
@@ -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();
|
||||
|
||||
@@ -363,6 +363,25 @@ fn sentence_spacing_matches_pdftex_at_150pt() {
|
||||
assert_lines_match("sentence spacing 150pt", &ours, &reference);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spaceskip_matches_pdftex() {
|
||||
let prefix = "\\spaceskip=6pt plus 1pt minus 0.5pt\\relax ";
|
||||
compare_fixture_with(WORDS, 150, prefix, |style| {
|
||||
style.space_skip = Some((6 * 65_536, 65_536, 32_768));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xspaceskip_matches_pdftex() {
|
||||
let text = "alpha. beta gamma, delta; epsilon: zeta eta theta iota kappa lambda mu";
|
||||
let prefix = "\\spaceskip=6pt plus 1pt minus 0.5pt\\relax \
|
||||
\\xspaceskip=8pt plus 4pt minus 2pt\\relax ";
|
||||
compare_fixture_with(text, 150, prefix, |style| {
|
||||
style.space_skip = Some((6 * 65_536, 65_536, 32_768));
|
||||
style.xspace_skip = Some((8 * 65_536, 4 * 65_536, 2 * 65_536));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_sfcode_matches_pdftex() {
|
||||
let width = 150;
|
||||
|
||||
@@ -43,6 +43,8 @@ fn body_style() -> ParagraphStyle {
|
||||
space_width: font.space(),
|
||||
space_stretch: font.stretch(),
|
||||
space_shrink: font.shrink(),
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,6 +212,8 @@ fn route_d_page_matches_pdftex_raster_geometry() {
|
||||
space_width: 4 * 65_536,
|
||||
space_stretch: 6 * 65_536,
|
||||
space_shrink: 2 * 65_536,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let document = Document::new(
|
||||
700 * 65_536,
|
||||
@@ -386,6 +390,8 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
|
||||
space_width: 4 * 65_536,
|
||||
space_stretch: 6 * 65_536,
|
||||
space_shrink: 2 * 65_536,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let document = Document::new(
|
||||
20 * 65_536,
|
||||
@@ -688,6 +694,8 @@ fn route_d_multiline_bands_track_pdftex() {
|
||||
space_width: 4 * 65_536,
|
||||
space_stretch: 6 * 65_536,
|
||||
space_shrink: 2 * 65_536,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let document = Document::new(
|
||||
700 * 65_536,
|
||||
|
||||
Reference in new issue
Block a user