Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Text: a justified line with a tab justifies the text after its last tab

aiongg58388e6ec504
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 5a3056d8f..84ad2a434 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1568,6 +1568,8 @@ fn layout_line(
let mut x = 0.0;
let mut i = 0;
let mut leaders: Vec<(usize, String)> = Vec::new();
+ // The line's last tab and whether it is a left tab (only text after a left tab justifies).
+ let mut last_tab: Option<(usize, bool)> = None;
while i < line.len() {
if line[i].ch == '\t' {
let abs = x0 + x - tab_origin;
@@ -1596,9 +1598,11 @@ fn layout_line(
}
};
line[i].adv = w.max(0.0);
+ last_tab = Some((i, align == TabAlign::Left));
} else if line[i].ch == story::RIGHT_INDENT_TAB {
let rest: f64 = line[i + 1..].iter().map(|g| g.adv).sum();
line[i].adv = (measure - x - rest).max(0.0);
+ last_tab = Some((i, false));
}
x += line[i].adv;
i += 1;
@@ -1623,7 +1627,11 @@ fn layout_line(
}
let natural: f64 = line.iter().map(|g| g.adv).sum();
let mut extra = measure - natural;
- let spaces: Vec<usize> = line.iter().enumerate().filter(|(_, g)| g.is_space() && !g.no_break).map(|(i, _)| i).collect();
+ // Justification adjusts only the text after the last tab (InDesign keeps tab stops aligned);
+ // a line whose last tab is a right, centre, character or right-indent tab is not justified.
+ let seg = last_tab.map_or(0, |(i, _)| i + 1);
+ let seg_justifies = last_tab.is_none_or(|(_, left)| left);
+ let spaces: Vec<usize> = line.iter().enumerate().skip(seg).filter(|(_, g)| g.is_space() && !g.no_break).map(|(i, _)| i).collect();
let align = match pp.align {
Align::TowardsSpine => {
if left_page {
@@ -1641,9 +1649,9 @@ fn layout_line(
}
a => a,
};
- let justify_this = align.is_justified() && (!last || align == Align::FullyJustified || forced_mid) && !has_tab;
+ let justify_this = align.is_justified() && (!last || align == Align::FullyJustified || forced_mid) && seg_justifies;
// A justified paragraph's last line may have been composed with shrunk spaces: shrink it too.
- let squeeze_last = align.is_justified() && !justify_this && !has_tab && extra < 0.0 && !spaces.is_empty();
+ let squeeze_last = align.is_justified() && !justify_this && seg_justifies && extra < 0.0 && !spaces.is_empty();
// Extra advance per glyph (word spaces and letter gaps) and horizontal scale per glyph.
let mut add = vec![0.0; line.len()];
let mut scale = vec![1.0; line.len()];
@@ -1651,7 +1659,7 @@ fn layout_line(
// Kashidas: in justified Arabic, the joins of words take the extra length first.
let mut kashidas: Vec<(usize, f64)> = Vec::new();
if justify_this && extra > 0.0 && pp.kashidas && pp.arabic_justification != "DefaultJustification" {
- let points = kashida_points(&line);
+ let points: Vec<usize> = kashida_points(&line).into_iter().filter(|&i| i >= seg).collect();
if !points.is_empty() {
let total = extra.min(points.iter().map(|&i| line[i].size * 1.5).sum());
let per = total / points.len() as f64;
@@ -1663,8 +1671,11 @@ fn layout_line(
}
}
if (justify_this || squeeze_last) && !spaces.is_empty() {
- distribute(&line, &spaces, extra, sp, &mut add, &mut scale);
- } else if justify_this && spaces.is_empty() && line.len() > 1 && !last {
+ let rebased: Vec<usize> = spaces.iter().map(|&i| i - seg).collect();
+ let (seg_line, seg_add, seg_scale) =
+ (line.get(seg..).unwrap_or(&[]), add.get_mut(seg..).unwrap_or(&mut []), scale.get_mut(seg..).unwrap_or(&mut []));
+ distribute(seg_line, &rebased, extra, sp, seg_add, seg_scale);
+ } else if justify_this && spaces.is_empty() && line.len() > seg + 1 && !last {
// Single word: Single Word Justification.
match pp.single_word_justify {
Align::FullyJustified => {
@@ -1672,6 +1683,7 @@ fn layout_line(
.iter()
.enumerate()
.take(line.len() - 1)
+ .skip(seg)
.filter(|(_, g)| !g.locked_advance || g.break_after != Some(false))
.map(|(i, _)| i)
.collect();
@@ -1682,8 +1694,14 @@ fn layout_line(
}
}
}
- Align::Center => offset = extra / 2.0,
- Align::Right => offset = extra,
+ // After a tab the word moves by widening the tab, so text before it stays put.
+ Align::Center | Align::Right => {
+ let shift = if pp.single_word_justify == Align::Center { extra / 2.0 } else { extra };
+ match seg.checked_sub(1).and_then(|t| add.get_mut(t)) {
+ Some(tab) => *tab += shift,
+ None => offset = shift,
+ }
+ }
_ => {}
}
} else {
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 549e7bccf..d73c19e73 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -1877,3 +1877,43 @@ fn vertical_lines_fit_the_em_box() {
assert!((l.baseline - col.y0 - ascent).abs() < 1e-9, "{family}: {} {}", l.baseline, col.y0);
}
}
+
+#[test]
+fn justified_line_with_a_tab_justifies_the_text_after_its_last_tab() {
+ // InDesign justifies only the text after a line's last (left) tab; tab stops stay aligned.
+ let stop = |align| designcraft_doc::TabStop { position: 100.0, align, leader: String::new(), align_on: String::new() };
+ let text = "Name\t42 and some words\u{2028}more";
+ let x_of = |l: &Line, byte: usize| l.glyphs.iter().find(|g| g.byte == byte && g.len > 0).map(|g| g.x).unwrap();
+ let left = ParaAttrs { align: Some(Align::LeftJustified), tabs: Some(vec![stop(TabAlign::Left)]), ..Default::default() };
+ let (d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 100.0), left.clone());
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let l = all_lines(&cs)[0];
+ assert!((l.end_x - l.x1).abs() < 0.6, "line with a left tab ends at {} not {}", l.end_x, l.x1);
+ assert!((x_of(l, 5) - 100.0).abs() < 0.5, "text after the tab starts at the stop: {}", x_of(l, 5));
+ let (d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs { align: Some(Align::Left), ..left });
+ let ragged = compose_story(&d, sid, &ComposeOptions::default());
+ let r = all_lines(&ragged)[0];
+ for byte in 0..4 {
+ assert!((x_of(l, byte) - x_of(r, byte)).abs() < 1e-6, "text before the tab keeps its natural position");
+ }
+
+ // After a right tab the line stays as set.
+ let right = ParaAttrs { align: Some(Align::LeftJustified), tabs: Some(vec![stop(TabAlign::Right)]), ..Default::default() };
+ let (d, sid, _) = doc_with("Name\t42\u{2028}more", Rect::new(0.0, 0.0, 300.0, 100.0), right);
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let l = all_lines(&cs)[0];
+ assert!((l.end_x - 100.0).abs() < 0.5, "right-tab line ends at its stop: {}", l.end_x);
+
+ // A footnote's number and tab separator: its first line is justified like the others.
+ let (mut d, sid, fid) = doc_with("Body text.", Rect::new(0.0, 0.0, 200.0, 400.0), ParaAttrs::default());
+ let note = ParaFormat { para: ParaAttrs { align: Some(Align::LeftJustified), ..Default::default() }, ..Default::default() };
+ d.story_mut(sid).unwrap().insert_note(4, LOREM, note);
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let n = &cs.frame(fid).unwrap().notes[0];
+ let lines = all_lines(&n.text);
+ assert!(lines.len() > 2);
+ for l in &lines[..lines.len() - 1] {
+ assert!((l.end_x - l.x1).abs() < 0.6, "footnote line ends at {} not {}", l.end_x, l.x1);
+ }
+ assert!((x_of(lines[0], 0) - 36.0).abs() < 0.5, "note text starts at the default tab stop: {}", x_of(lines[0], 0));
+}