Honour finite parfillskip on the last line
This commit is contained in:
1 parent
8993186005
commit
bd109d03c5
6 files changed
+146
-16
No files matched your search
@@ -85,6 +85,15 @@ pub struct ParagraphStyle {
|
||||
/// Explicit `(indent, width)` pairs for `\parshape`.
|
||||
#[serde(default)]
|
||||
pub par_shape: Vec<(i64, i64)>,
|
||||
/// `\parfillskip` width, inserted before the paragraph's final break.
|
||||
#[serde(default)]
|
||||
pub par_fillskip: i64,
|
||||
/// Finite stretch of `\parfillskip`.
|
||||
#[serde(default)]
|
||||
pub par_fillskip_stretch: i64,
|
||||
/// Whether `\parfillskip` ends in `fil` glue (the TeX default).
|
||||
#[serde(default = "default_par_fillskip_fil")]
|
||||
pub par_fillskip_fil: bool,
|
||||
/// Center each line in the measure instead of justifying it.
|
||||
#[serde(default)]
|
||||
pub centered: bool,
|
||||
@@ -135,6 +144,10 @@ pub struct ParagraphStyle {
|
||||
pub space_shrink: i64,
|
||||
}
|
||||
|
||||
fn default_par_fillskip_fil() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl ParagraphStyle {
|
||||
/// `(indent, measure)` for a zero-based line number, honouring
|
||||
/// `\parshape` and `\hangindent`/`\hangafter`.
|
||||
@@ -190,6 +203,9 @@ impl Default for ParagraphStyle {
|
||||
hang_indent: 0,
|
||||
hang_after: 1,
|
||||
par_shape: Vec::new(),
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
par_indent: 0,
|
||||
@@ -413,6 +429,10 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
}
|
||||
if is_last {
|
||||
trailing_penalty = 0;
|
||||
if !style.par_fillskip_fil {
|
||||
natural += style.par_fillskip;
|
||||
stretch += style.par_fillskip_stretch;
|
||||
}
|
||||
} else if hyphen_break {
|
||||
// TeX charges \exhyphenpenalty after an explicit hyphen
|
||||
// and \hyphenpenalty at an inserted hyphenation point.
|
||||
@@ -449,8 +469,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
} else {
|
||||
(0.0, 0)
|
||||
};
|
||||
// TeX's final line has \parfillskip's infinite stretch.
|
||||
let badness = if is_last && natural <= target {
|
||||
// TeX's default final line has \parfillskip's infinite
|
||||
// stretch; without fil the last line is set like any other.
|
||||
let badness = if is_last && style.par_fillskip_fil && natural <= target {
|
||||
0
|
||||
} else {
|
||||
badness
|
||||
@@ -583,10 +604,15 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
if !has_box {
|
||||
continue;
|
||||
}
|
||||
let final_line = end == items.len();
|
||||
if final_line && !style.par_fillskip_fil {
|
||||
natural += style.par_fillskip;
|
||||
stretch += style.par_fillskip_stretch;
|
||||
}
|
||||
if overflow.is_none() && natural >= target {
|
||||
overflow = Some(end_index);
|
||||
}
|
||||
let badness = if end == items.len() && natural <= target {
|
||||
let badness = if final_line && style.par_fillskip_fil && natural <= target {
|
||||
0
|
||||
} else if natural < target {
|
||||
if stretch > 0 {
|
||||
@@ -682,7 +708,19 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
}
|
||||
}
|
||||
let is_last = line_index + 1 == line_count;
|
||||
if !is_last {
|
||||
let fillskip = if is_last && !style.par_fillskip_fil {
|
||||
style.par_fillskip
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let fillskip_stretch = if is_last && !style.par_fillskip_fil {
|
||||
style.par_fillskip_stretch
|
||||
} 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
|
||||
@@ -1284,4 +1322,56 @@ mod tests {
|
||||
"exceptions insert a discretionary point"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parfillskip_without_fil_stretches_the_last_line() {
|
||||
let text = "alpha beta";
|
||||
let style = ParagraphStyle {
|
||||
line_width: 120,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let default = layout_paragraph(text, &style);
|
||||
let mut flush = style.clone();
|
||||
flush.par_fillskip_fil = false;
|
||||
let justified = layout_paragraph(text, &flush);
|
||||
let last_width = |paragraph: &Paragraph| {
|
||||
paragraph
|
||||
.lines
|
||||
.last()
|
||||
.expect("a line")
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
HorizontalItem::Box(box_item) => box_item.width,
|
||||
HorizontalItem::Glue { width, .. } => *width,
|
||||
_ => 0,
|
||||
})
|
||||
.sum::<i64>()
|
||||
};
|
||||
assert!(last_width(&default) < 120, "fil leaves the last line short");
|
||||
assert_eq!(last_width(&justified), 120, "0pt parfillskip is flush");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parfillskip_width_leaves_a_gap_on_the_last_line() {
|
||||
let text = "alpha beta";
|
||||
let style = ParagraphStyle {
|
||||
line_width: 120,
|
||||
par_fillskip: 20,
|
||||
par_fillskip_fil: false,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let paragraph = layout_paragraph(text, &style);
|
||||
let last_width: i64 = paragraph
|
||||
.lines
|
||||
.last()
|
||||
.expect("a line")
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
HorizontalItem::Box(box_item) => box_item.width,
|
||||
HorizontalItem::Glue { width, .. } => *width,
|
||||
_ => 0,
|
||||
})
|
||||
.sum();
|
||||
assert_eq!(last_width, 100, "parfillskip leaves its width at the end");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user