Skip to content

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

Merge nobreak-justify: No Break runs and nonbreaking spaces stretch on justified lines (#368)

aiongg5b060a8de2f0
diff --cc crates/compose/src/breaker.rs
index 33e55002f,9688c1f0d..f6d583db5
--- a/crates/compose/src/breaker.rs
+++ b/crates/compose/src/breaker.rs
@@@ -81,48 -78,29 +81,48 @@@ impl Default for Spacing
impl Spacing {
/// Per-tier (word, letter, glyph) stretch and shrink of glyph `g` as a box.
fn box_elastic(&self, g: &Glyph) -> ([f64; 3], [f64; 3]) {
- if !self.justify || g.locked_advance {
+ if g.locked_advance {
return ([0.0; 3], [0.0; 3]);
}
+ let moji_stretch = if self.justify { g.moji.stretch } else { 0.0 };
+ let moji_shrink = if self.kinsoku_priority == 3 { 0.0 } else { g.moji.shrink };
+ if !self.justify {
+ return ([0.0; 3], [moji_shrink, 0.0, 0.0]);
+ }
+ // A space that is no break opportunity (No Break, U+00A0) is a box with word-space elasticity.
+ if g.is_justify_space() {
+ let (st, sh) = self.space_elastic(g);
- return ([st, 0.0, 0.0], [sh, 0.0, 0.0]);
++ return ([moji_stretch + st, 0.0, 0.0], [moji_shrink + sh, 0.0, 0.0]);
+ }
let natural = g.adv / self.glyph_desired.max(0.01);
(
- [0.0, g.space * (self.letter_max - self.letter_desired).max(0.0), natural * (self.glyph_max - self.glyph_desired).max(0.0)],
- [0.0, g.space * (self.letter_desired - self.letter_min).max(0.0), natural * (self.glyph_desired - self.glyph_min).max(0.0)],
+ [moji_stretch, g.space * (self.letter_max - self.letter_desired).max(0.0), natural * (self.glyph_max - self.glyph_desired).max(0.0)],
+ [moji_shrink, g.space * (self.letter_desired - self.letter_min).max(0.0), natural * (self.glyph_desired - self.glyph_min).max(0.0)],
)
}
- /// Word-space stretch and shrink of space glyph `g` (only U+0020 is elastic).
+ /// Word-space stretch and shrink of space glyph `g` (see [`Glyph::is_word_space`]).
fn space_elastic(&self, g: &Glyph) -> (f64, f64) {
- if !elastic_space(g) {
+ if !g.is_word_space() {
return (0.0, 0.0);
}
(g.space * (self.word_max - self.word_desired).max(0.0), g.space * (self.word_desired - self.word_min).max(0.0))
}
+ /// The unit in which stretch past the maximum word spacing is rated (see [`tiered_ratio`]): the
+ /// word space's own stretch, but at least [`OVERFLOW_STRETCH`] of its width, so a tight
+ /// spacing range doesn't make a loose line with several word spaces look worse than a line
+ /// letter-spaced across the measure.
+ fn space_overflow(&self, g: &Glyph) -> f64 {
- if !elastic_space(g) {
++ if !g.is_word_space() {
+ return 0.0;
+ }
+ g.space * (self.word_max - self.word_desired).max(OVERFLOW_STRETCH)
+ }
}
- /// Does `g` stretch and shrink as a word space? Fixed-width spaces (em, en, thin…) don't.
- pub(crate) fn elastic_space(g: &Glyph) -> bool {
- g.ch == ' ' || (g.ch == '\u{3000}' && g.ideographic_space_elastic)
- }
-
+/// Minimum stretch of a word space, as a fraction of its width, for rating lines that stretch
+/// past the maximum word spacing (the default range's 133% − 100%).
+const OVERFLOW_STRETCH: f64 = 0.33;
+
/// Relative cost of using each tier of elastic material (word, letter, glyph).
pub const TIER_COST: [f64; 3] = [1.0, 0.75, 1.0];
diff --cc crates/compose/src/lib.rs
index 37683e497,30c0a0fab..e40fa3407
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@@ -2995,20 -2592,12 +2995,20 @@@ fn distribute(line: &[Glyph], spaces: &
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 elastic = spaces.iter().filter(|&&i| line[i].is_word_space()).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 + if breaker::elastic_space(&line[i]) { over } else { 0.0 };
- add[i] += sign * share + over;
++ add[i] += sign * share + if line[i].is_word_space() { over } else { 0.0 };
}
if yl > 1e-9 && tl > 0.0 {
for (i, l) in letter.iter().enumerate() {