Add a pdfLaTeX oracle and clamp justification glue
This commit is contained in:
1 parent
1e2d4afd6b
commit
9726f5f736
6 files changed
+379
-18
No files matched your search
@@ -342,7 +342,10 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
}
|
||||
let is_last = line_index + 1 == line_count;
|
||||
if !is_last {
|
||||
let delta = target - natural;
|
||||
// TeX clamps the glue set ratio to the available stretch or
|
||||
// shrink; an overfull line stays overfull instead of pulling its
|
||||
// glue past the declared shrink.
|
||||
let delta = (target - natural).clamp(-shrink, stretch);
|
||||
if delta > 0 && stretch > 0 {
|
||||
let mut remaining = delta;
|
||||
for item in &mut line {
|
||||
@@ -367,6 +370,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
*width += remaining;
|
||||
}
|
||||
} else if delta < 0 && shrink > 0 {
|
||||
let mut remaining = delta;
|
||||
for item in &mut line {
|
||||
if let HorizontalItem::Glue {
|
||||
width,
|
||||
@@ -375,9 +379,19 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
} = item
|
||||
&& *shrink_amount > 0
|
||||
{
|
||||
*width += delta * *shrink_amount / shrink;
|
||||
let share = delta * *shrink_amount / shrink;
|
||||
*width += share;
|
||||
remaining -= share;
|
||||
}
|
||||
}
|
||||
if remaining != 0
|
||||
&& let Some(HorizontalItem::Glue { width, .. }) = line
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.find(|item| matches!(item, HorizontalItem::Glue { .. }))
|
||||
{
|
||||
*width += remaining;
|
||||
}
|
||||
}
|
||||
}
|
||||
let width: i64 = line
|
||||
@@ -430,6 +444,9 @@ pub fn text_items_with_font(
|
||||
stretch: font.stretch(),
|
||||
shrink: font.shrink(),
|
||||
});
|
||||
// TeX never kerns across interword glue.
|
||||
previous_box = None;
|
||||
previous_character = None;
|
||||
}
|
||||
let points: std::collections::HashSet<usize> = if word.is_ascii()
|
||||
&& word
|
||||
@@ -531,6 +548,42 @@ mod tests {
|
||||
.count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overfull_lines_do_not_exceed_the_declared_shrink() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let style = ParagraphStyle {
|
||||
line_width: 150 * 65_536,
|
||||
par_indent: 0,
|
||||
line_height: 12 * 65_536,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let paragraph = layout_paragraph_with_font(
|
||||
"alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu",
|
||||
&style,
|
||||
&font,
|
||||
);
|
||||
let first = ¶graph.lines[0];
|
||||
let mut total = 0_i64;
|
||||
for item in first {
|
||||
match item {
|
||||
HorizontalItem::Box(box_) => total += box_.width,
|
||||
HorizontalItem::Glue { width, shrink, .. } => {
|
||||
total += *width;
|
||||
assert!(
|
||||
*width >= font.space() - *shrink,
|
||||
"glue shrank past its declared shrink"
|
||||
);
|
||||
}
|
||||
HorizontalItem::Penalty { width, .. } => total += *width,
|
||||
HorizontalItem::Discretionary { .. } => {}
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
total > style.line_width,
|
||||
"an overfull line must stay overfull instead of shrinking glue past its limits"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn words_break_at_glue_and_lines_are_justified() {
|
||||
let style = ParagraphStyle {
|
||||
|
||||
@@ -295,14 +295,20 @@ fn parse_kerns(
|
||||
};
|
||||
if let Some(unicode) = unicode {
|
||||
let right = char::from_u32(u32::from(next)).unwrap_or('?');
|
||||
ligatures.insert((left, right), (unicode, slot_char));
|
||||
ligatures
|
||||
.entry((left, right))
|
||||
.or_insert((unicode, slot_char));
|
||||
}
|
||||
} else {
|
||||
let kern_index = usize::from(op - 128) * 256 + usize::from(remainder);
|
||||
if let Some(raw) = kern_table.get(kern_index) {
|
||||
let right = char::from_u32(u32::from(next)).unwrap_or('?');
|
||||
// Kern values are design-size-relative, like character
|
||||
// widths; at the design size they are in 1/16 pt units.
|
||||
let value = (f64::from(*raw) * design_size_pt / 16.0).round() as i64;
|
||||
kerns.insert((left, right), value);
|
||||
// TeX stops at the first instruction that matches the
|
||||
// pair, so later instructions must not overwrite it.
|
||||
kerns.entry((left, right)).or_insert(value);
|
||||
}
|
||||
}
|
||||
if skip >= 128 {
|
||||
@@ -333,6 +339,21 @@ mod tests {
|
||||
assert!(font.depth('g') > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cmr10_kerning_is_design_size_relative() {
|
||||
let font = cmr10();
|
||||
let kern = font.kern('m', 'b');
|
||||
assert!(
|
||||
(kern + 18_205).abs() <= 4,
|
||||
"m/b kern should be -0.277779pt, got {kern}"
|
||||
);
|
||||
let apostrophe = font.kern('f', '\'');
|
||||
assert!(
|
||||
(apostrophe - 50_973).abs() <= 4,
|
||||
"f/apostrophe kern should be +0.777779pt, got {apostrophe}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cmr10_has_an_fi_ligature() {
|
||||
let font = cmr10();
|
||||
|
||||
Reference in new issue
Block a user