Support ragged-right paragraphs with an infinite rightskip
This commit is contained in:
1 parent
f488dd3b6c
commit
448152cea4
7 files changed
+81
-10
No files matched your search
@@ -116,6 +116,10 @@ pub struct ParagraphStyle {
|
||||
pub centered: bool,
|
||||
#[serde(default)]
|
||||
pub right_aligned: bool,
|
||||
/// Ragged-right setting (`\raggedright`): lines keep their natural width
|
||||
/// and the missing space is absorbed by an infinite `\rightskip`.
|
||||
#[serde(default)]
|
||||
pub ragged_right: bool,
|
||||
pub par_indent: i64,
|
||||
/// Extra stretch added to every line when TeX's emergency pass runs
|
||||
/// (`\emergencystretch`).
|
||||
@@ -246,6 +250,7 @@ impl Default for ParagraphStyle {
|
||||
lineskiplimit: 0,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
ragged_right: false,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
pretolerance: 100,
|
||||
@@ -533,7 +538,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
};
|
||||
let target = style.line_target(node.line);
|
||||
let stretch = stretch + extra_stretch;
|
||||
let (ratio, badness) = if natural < target {
|
||||
let (mut ratio, mut badness) = if natural < target {
|
||||
if stretch > 0 {
|
||||
let ratio = (target - natural) as f64 / stretch as f64;
|
||||
(ratio, (100.0 * ratio.powi(3)).round().min(10_000.0) as i64)
|
||||
@@ -550,6 +555,12 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
} else {
|
||||
(0.0, 0)
|
||||
};
|
||||
// \raggedright puts an infinite `fil` in \rightskip, so
|
||||
// any line that fits has zero badness and natural glue.
|
||||
if style.ragged_right && natural <= target {
|
||||
ratio = 0.0;
|
||||
badness = 0;
|
||||
}
|
||||
// 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 {
|
||||
@@ -631,6 +642,12 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
break;
|
||||
}
|
||||
}
|
||||
// With an infinite `fil` in `\rightskip` every line that fits has zero
|
||||
// badness, so TeX fills each line greedily; the demerits search would
|
||||
// instead pick the shortest first line among the equal-cost solutions.
|
||||
if style.ragged_right {
|
||||
solution = None;
|
||||
}
|
||||
let mut ranges = solution.unwrap_or_else(|| {
|
||||
// TeX's final pass forces an overfull break only as a last resort.
|
||||
// Emulate that choice: take the longest prefix that still satisfies
|
||||
@@ -700,7 +717,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
if overflow.is_none() && natural >= target {
|
||||
overflow = Some(end_index);
|
||||
}
|
||||
let badness = if final_line && style.par_fillskip_fil && natural <= target {
|
||||
let mut badness = if final_line && style.par_fillskip_fil && natural <= target {
|
||||
0
|
||||
} else if natural < target {
|
||||
if stretch > 0 {
|
||||
@@ -717,6 +734,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
} else {
|
||||
10_000
|
||||
};
|
||||
if style.ragged_right && natural <= target {
|
||||
badness = 0;
|
||||
}
|
||||
if badness <= style.tolerance {
|
||||
feasible = Some(end_index);
|
||||
}
|
||||
@@ -814,7 +834,11 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
// 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);
|
||||
let delta = if style.ragged_right {
|
||||
(target - natural).min(0).max(-shrink)
|
||||
} else {
|
||||
(target - natural).max(-shrink)
|
||||
};
|
||||
if delta > 0 {
|
||||
if !is_last || !style.par_fillskip_fil {
|
||||
let mut remaining = delta;
|
||||
@@ -1217,6 +1241,39 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ragged_right_leaves_lines_at_their_natural_width() {
|
||||
let width = |line: &[HorizontalItem]| -> i64 {
|
||||
line.iter()
|
||||
.map(|item| match item {
|
||||
HorizontalItem::Box(box_item) => box_item.width,
|
||||
HorizontalItem::Glue { width, .. } => *width,
|
||||
HorizontalItem::Penalty { width, .. } => *width,
|
||||
HorizontalItem::Discretionary { .. } => 0,
|
||||
})
|
||||
.sum()
|
||||
};
|
||||
let style = |ragged_right: bool| ParagraphStyle {
|
||||
line_width: 200,
|
||||
par_fillskip_fil: false,
|
||||
ragged_right,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let justified = layout_paragraph("alpha beta", &style(false));
|
||||
let ragged = layout_paragraph("alpha beta", &style(true));
|
||||
assert_eq!(justified.lines.len(), 1);
|
||||
assert_eq!(
|
||||
width(&justified.lines[0]),
|
||||
200,
|
||||
"justified line fills the measure"
|
||||
);
|
||||
assert_eq!(
|
||||
width(&ragged.lines[0]),
|
||||
94,
|
||||
"ragged line keeps its natural width"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn words_break_at_glue_and_lines_are_justified() {
|
||||
let style = ParagraphStyle {
|
||||
|
||||
Reference in new issue
Block a user