mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-26 23:58:39 +00:00
M4: justified Chinese, Japanese and Korean text spreads the room between its characters (#1970)
Cause: justified lines only widened their spaces, so a line of ideographs, written without any, kept its natural width and its right edge never reached the box's. Fix: the room on a line (but a paragraph's last) also goes into the gaps next to an ideograph, kana, Hangul or full-width form wherever a line could break there (`breaks::justify_between`, beside `break_between`): each is a word of its own, as for line breaking, and no gap opens before a closing mark or after an opening one. Spaces widen as before; mixed text widens both. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
6f780211dd
commit
95d71a6825
@@ -79,6 +79,15 @@ pub(crate) fn is_space(c: char) -> bool {
|
||||
class(c) == Class::Space
|
||||
}
|
||||
|
||||
/// Whether justified text may widen the gap between `a` and the `b` after it (a space widens on
|
||||
/// its own): next to an ideograph, kana, Hangul or full-width form, which are written without
|
||||
/// spaces, so each is a word of its own, as for line breaking, wherever a line may break there (no
|
||||
/// gap before a closing mark or after an opening one).
|
||||
pub(crate) fn justify_between(a: char, b: char) -> bool {
|
||||
let (ca, cb) = (class(a), class(b));
|
||||
(ca == Class::Ideo || cb == Class::Ideo) && ca != Class::Space && break_between(a, b)
|
||||
}
|
||||
|
||||
/// Whether `c` is in a script written without spaces between words that the line segmenter has
|
||||
/// word data for (UAX #14 class SA): Thai, Lao, Burmese (with the Myanmar extensions) and Khmer.
|
||||
fn writes_no_spaces(c: char) -> bool {
|
||||
|
||||
@@ -1259,8 +1259,13 @@ pub fn layout(text: &TextData, box_width: Option<f64>, box_height: Option<f64>)
|
||||
let hang_r =
|
||||
items.get(vis_end.wrapping_sub(1)).filter(|it| hang_end && !rtl_line && vis_end > *s + 1 && closes(it.ch)).map_or(0.0, |it| it.adv);
|
||||
let gstart = out.glyphs.len();
|
||||
// Justify: extra space per gap.
|
||||
let gaps = items.get(*s..vis_end).unwrap_or(&[]).iter().filter(|it| it.stretches()).count();
|
||||
// Justify: extra space per gap (a space, or between ideographs, whose text has no spaces).
|
||||
let widens = |k: usize| {
|
||||
items.get(k).is_some_and(|it| {
|
||||
it.stretches() || (k + 1 < vis_end && items.get(k + 1).is_some_and(|next| crate::breaks::justify_between(it.ch, next.ch)))
|
||||
})
|
||||
};
|
||||
let gaps = (*s..vis_end).filter(|k| widens(*k)).count();
|
||||
let justify_extra = match (text.align_h, wrap) {
|
||||
(TextAlignH::Justified, Some(maxw)) if !para_end && !line_break_after(&items, *e) && gaps > 0 => {
|
||||
((maxw - line_indent + hang_l + hang_r - width) / gaps as f64).max(0.0)
|
||||
@@ -1349,13 +1354,14 @@ pub fn layout(text: &TextData, box_width: Option<f64>, box_height: Option<f64>)
|
||||
} else {
|
||||
(0..line_items.len()).collect()
|
||||
};
|
||||
for it in order.iter().filter_map(|k| line_items.get(*k)) {
|
||||
for k in order.iter().copied() {
|
||||
let Some(it) = line_items.get(k) else { continue };
|
||||
if is_line_break(it.ch) {
|
||||
continue;
|
||||
}
|
||||
out.glyphs.extend(glyph_at(&styles, it, pen, baseline));
|
||||
pen += it.adv;
|
||||
if it.stretches() {
|
||||
if widens(*s + k) {
|
||||
pen += justify_extra;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -590,6 +590,51 @@ fn lower_case_writes_final_sigma() {
|
||||
assert_eq!(gids(&t).iter().map(|g| g.0).collect::<Vec<_>>(), want.iter().map(|g| g.0).collect::<Vec<_>>(), "split runs");
|
||||
}
|
||||
|
||||
/// Chinese is written without spaces: justified, the room goes between its characters (each a word
|
||||
/// of its own, as for line breaking), so the last one reaches the right edge too, and none opens
|
||||
/// where a line couldn't break (before a closing mark, after an opening one). "Justify aligns the
|
||||
/// first word in each line of text to the left-most edge, and the last to the right-most edge."
|
||||
#[test]
|
||||
fn justified_ideographs_spread_the_room_between_characters() {
|
||||
if !have_fonts() {
|
||||
return;
|
||||
}
|
||||
let lay_out = |s: &str, align, w| {
|
||||
let mut t = TextData::new(s);
|
||||
t.style.font_size = 20.0;
|
||||
t.align_h = align;
|
||||
t.auto_resize = figcraft_doc::TextAutoResize::Height;
|
||||
layout(&t, w, None)
|
||||
};
|
||||
// Box width cut inside char `fit`, so the first line is chars 0..fit; how many gaps lie before
|
||||
// each of its chars.
|
||||
for (s, fit, gaps_before, why) in [
|
||||
("一二三四五六七八九十", 6, [0, 1, 2, 3, 4, 5], "one gap between each pair of ideographs"),
|
||||
("一二三。四五六七八九", 6, [0, 1, 2, 2, 3, 4], "none before the full stop, which never starts a line"),
|
||||
("一二三「四五六七八九", 6, [0, 1, 2, 3, 3, 4], "none after the opening bracket, which never ends a line"),
|
||||
] {
|
||||
let natural = lay_out(s, TextAlignH::Left, None);
|
||||
let (fits, next) = (natural.caret(fit).0, natural.caret(fit + 1).0);
|
||||
assert!(next > fits, "{why}: chars need advances");
|
||||
let w = fits + (next - fits) / 4.0;
|
||||
let left = lay_out(s, TextAlignH::Left, Some(w));
|
||||
let just = lay_out(s, TextAlignH::Justified, Some(w));
|
||||
assert!(just.lines.len() >= 2 && just.lines[0].char_end == fit, "{why}: {:?}", just.lines);
|
||||
let extra = (w - left.lines[0].width) / f64::from(gaps_before[fit - 1]);
|
||||
assert!(extra > 0.0, "{why}");
|
||||
assert_eq!(left.glyphs.len(), just.glyphs.len());
|
||||
for (l, j) in left.glyphs.iter().zip(&just.glyphs).filter(|(g, _)| g.char_end <= fit) {
|
||||
let want = f64::from(gaps_before[l.char_start]) * extra;
|
||||
assert!((j.x - l.x - want).abs() < 1e-6, "{why}: char {} moved {} (want {want})", l.char_start, j.x - l.x);
|
||||
}
|
||||
let last = just.glyphs.iter().rfind(|g| g.char_end <= fit).unwrap();
|
||||
assert!((last.x + last.advance - w).abs() < 1e-6, "{why}: the last char ends at the right edge, not {}", last.x + last.advance);
|
||||
// The last line is left-aligned, as always.
|
||||
let (l2, j2) = (left.glyphs.iter().find(|g| g.char_start == fit).unwrap(), just.glyphs.iter().find(|g| g.char_start == fit).unwrap());
|
||||
assert!((j2.x - l2.x).abs() < 1e-6, "{why}: the last line is not justified");
|
||||
}
|
||||
}
|
||||
|
||||
/// Thai is written without spaces between words; U+200B ZERO WIDTH SPACE marks where a line may
|
||||
/// break. ภาษา|ไทย|เขียน|ติดกัน ("Thai is written joined up") ไม่มี|ช่องว่าง|ระหว่าง|คำ ("with no
|
||||
/// spaces between words"), with one real space between the two phrases.
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user