Shrink overfull final lines and widen the oracle matrix
This commit is contained in:
1 parent
bcd3b6fa66
commit
6305f059ac
5 files changed
+98
-50
No files matched your search
@@ -799,15 +799,17 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
} else {
|
||||
0
|
||||
};
|
||||
if !is_last || !style.par_fillskip_fil {
|
||||
natural += fillskip;
|
||||
stretch += fillskip_stretch;
|
||||
// TeX can stretch normal glue past its nominal stretch (badness
|
||||
// simply exceeds 100), but it never shrinks past the declared
|
||||
// shrink: an overfull line keeps its overflow instead of pulling
|
||||
// glue beyond its limits.
|
||||
let delta = (target - natural).max(-shrink);
|
||||
if delta > 0 && stretch > 0 {
|
||||
natural += fillskip;
|
||||
stretch += fillskip_stretch;
|
||||
// TeX can stretch normal glue past its nominal stretch (badness
|
||||
// simply exceeds 100), but it never shrinks past the declared
|
||||
// shrink: an overfull line keeps its overflow instead of pulling
|
||||
// glue beyond its limits. A final line with a `fil` parfillskip only
|
||||
// shrinks: the infinite stretch absorbs any shortfall, so only a
|
||||
// line wider than the measure adjusts its finite glue.
|
||||
let delta = (target - natural).max(-shrink);
|
||||
if delta > 0 {
|
||||
if !is_last || !style.par_fillskip_fil {
|
||||
let mut remaining = delta;
|
||||
for item in &mut line {
|
||||
if let HorizontalItem::Glue {
|
||||
@@ -830,30 +832,30 @@ 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,
|
||||
shrink: shrink_amount,
|
||||
..
|
||||
} = item
|
||||
&& *shrink_amount > 0
|
||||
{
|
||||
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 { .. }))
|
||||
}
|
||||
} else if delta < 0 {
|
||||
let mut remaining = delta;
|
||||
for item in &mut line {
|
||||
if let HorizontalItem::Glue {
|
||||
width,
|
||||
shrink: shrink_amount,
|
||||
..
|
||||
} = item
|
||||
&& *shrink_amount > 0
|
||||
{
|
||||
*width += remaining;
|
||||
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
|
||||
.iter()
|
||||
@@ -1181,6 +1183,33 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overfull_final_line_shrinks_its_glue() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let style = ParagraphStyle {
|
||||
line_width: 23 * 65_536,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
line_height: 12 * 65_536,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let paragraph = layout_paragraph_with_font("aa bb", &style, &font);
|
||||
assert_eq!(paragraph.line_count(), 1);
|
||||
let glue = paragraph.lines[0].iter().find_map(|item| match item {
|
||||
HorizontalItem::Glue { width, .. } => Some(*width),
|
||||
_ => None,
|
||||
});
|
||||
let glue = glue.expect("the final line must keep its interword glue");
|
||||
assert!(
|
||||
glue < font.space(),
|
||||
"an overfull final line must shrink its glue"
|
||||
);
|
||||
assert!(
|
||||
glue >= font.space() - font.shrink(),
|
||||
"the final line must not shrink past the declared shrink"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn words_break_at_glue_and_lines_are_justified() {
|
||||
let style = ParagraphStyle {
|
||||
|
||||
Reference in new issue
Block a user