Skip to content

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

Compose: fixed-width spaces keep their width on justified lines

aiongg40c3a78e6e7a
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 3dd7c25b3..c5e828884 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -2547,7 +2547,7 @@ fn distribute(line: &[Glyph], spaces: &[usize], extra: f64, sp: &Spacing, add: &
.iter()
.map(|&i| {
let g = &line[i];
- if g.ch != ' ' && !(g.ch == '\u{3000}' && g.ideographic_space_elastic) {
+ if !breaker::elastic_space(g) {
0.0
} else if stretch {
g.space * (sp.word_max - sp.word_desired).max(0.0)
@@ -2592,18 +2592,33 @@ fn distribute(line: &[Glyph], spaces: &[usize], extra: f64, sp: &Spacing, add: &
let tg = rem.min(yg);
rem -= tg;
let sign = if stretch { 1.0 } else { -1.0 };
- // Stretch left over beyond every limit is shared equally by the word spaces; shrink never goes
- // past the minimums (that would overlap glyphs), so an overfull line stays overfull.
- let over = if stretch && !spaces.is_empty() { rem / spaces.len() as f64 } else { 0.0 };
+ // Stretch left over beyond every limit is shared equally by the word spaces (fixed-width spaces
+ // keep their width), else by the letter gaps; shrink never goes past the minimums (that would
+ // overlap glyphs), so an overfull line stays overfull.
+ let elastic = spaces.iter().filter(|&&i| breaker::elastic_space(&line[i])).count();
+ let gaps = line.iter().enumerate().filter(|&(i, g)| is_box(i, g)).count();
+ let (over, over_gap) = match (stretch && rem > 0.0, elastic, gaps) {
+ (false, _, _) => (0.0, 0.0),
+ (true, 0, 0) => (0.0, 0.0),
+ (true, 0, n) => (0.0, rem / n as f64),
+ (true, n, _) => (rem / n as f64, 0.0),
+ };
for (k, &i) in spaces.iter().enumerate() {
let share = if yw > 1e-9 { tw * word[k] / yw } else { 0.0 };
- add[i] += sign * share + over;
+ add[i] += sign * share + if breaker::elastic_space(&line[i]) { over } else { 0.0 };
}
if yl > 1e-9 && tl > 0.0 {
for (i, l) in letter.iter().enumerate() {
add[i] += sign * tl * l / yl;
}
}
+ if over_gap > 0.0 {
+ for (i, g) in line.iter().enumerate() {
+ if is_box(i, g) {
+ add[i] += over_gap;
+ }
+ }
+ }
if yg > 1e-9 && tg > 0.0 {
for (i, gcap) in glyph.iter().enumerate() {
if *gcap > 0.0 {
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 04cf3e447..eaca3026c 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -2802,3 +2802,42 @@ fn tight_word_spacing_does_not_isolate_a_word() {
}
}
}
+
+#[test]
+fn fixed_width_spaces_do_not_stretch_on_justified_lines() {
+ // Long words in a narrow column: lines stretch past the maximum word spacing. Only the word
+ // spaces take that stretch; em, en and thin spaces keep their width.
+ let words = ["characteristically", "misunderstanding", "unquestionably", "responsibility", "administration", "photographer"];
+ let seps = [" ", "\u{2003}", " ", "\u{2002}", " ", "\u{2009}"];
+ let text: String = (0..60).map(|i| format!("{}{}", words[i % words.len()], seps[i % seps.len()])).collect();
+ let text = text.trim_end().to_string();
+ let fixed = |c: char| matches!(c, '\u{2003}' | '\u{2002}' | '\u{2009}');
+ // (character, advance) of each fixed space, per line, and whether the line ends the paragraph.
+ let spaces = |align: Align, w: f64| -> Vec<(Vec<(char, f64)>, bool)> {
+ let para = ParaAttrs { align: Some(align), hyphenate: Some(false), ..Default::default() };
+ let (d, sid, _) = doc_with(&text, Rect::new(0.0, 0.0, w, 20000.0), para);
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ all_lines(&cs)
+ .iter()
+ .map(|l| {
+ let found = l.glyphs.iter().filter_map(|g| text[g.byte..].chars().next().filter(|&c| fixed(c)).map(|c| (c, g.adv))).collect();
+ (found, l.last_in_para)
+ })
+ .collect()
+ };
+ let mut checked = 0;
+ for w in [190.0, 230.0, 270.0] {
+ let natural: Vec<(char, f64)> = spaces(Align::Left, w).into_iter().flat_map(|l| l.0).collect();
+ for (found, last) in spaces(Align::LeftJustified, w) {
+ if last {
+ continue;
+ }
+ for (c, a) in found {
+ let Some(&(_, n)) = natural.iter().find(|x| x.0 == c) else { continue };
+ assert!((a - n).abs() < 1e-6, "width {w}: U+{:04X} is {a:.2} wide, not {n:.2}", c as u32);
+ checked += 1;
+ }
+ }
+ }
+ assert!(checked > 5, "{checked} fixed spaces on justified lines");
+}