diff --git a/crates/text/src/layout/breaking.rs b/crates/text/src/layout/breaking.rs index f319e92867..f5e7b1d45f 100644 --- a/crates/text/src/layout/breaking.rs +++ b/crates/text/src/layout/breaking.rs @@ -240,29 +240,47 @@ pub(super) fn leader_outline(db: &FontDb, g: &SGlyph, leader: &str, origin: f64, out } -/// Width of the word starting at glyph `i`: its advances up to the first break opportunity (spaces -/// left out, the hyphen of a soft hyphen in), the least room a line needs not to break inside it. -/// A list marker at `i` (glyphs of length 0) counts whole, with the word after it. -pub(super) fn word_width(g: &[SGlyph], i: usize) -> f64 { +/// Width of the word starting at glyph `i`: its advances up to the first place a line may break +/// (spaces left out, the hyphen of a soft hyphen in), the least room a line needs not to break +/// inside it. As in [`break_line`], kinsoku keeps a character that may not end a line with the one +/// after it, a No Break run (its spaces inside it included) is one word, and a cluster is never +/// split. A list marker at `i` (glyphs of length 0) counts whole, with the word after it. A word +/// that ends where a line may end is measured as the line is set: without the spaces at its end, +/// and with its last glyph as narrow as `line_end` sets it; one that ends at a soft hyphen or past +/// the measured glyphs is not. +pub(super) fn word_width(g: &[SGlyph], i: usize, line_end: &[f64], kinsoku: Kinsoku) -> f64 { /// Glyphs measured at most: a word wrapped over many lines is measured again from each line's /// start, and one this long is broken inside whatever cell it meets. const MAX_GLYPHS: usize = 1024; let rest = g.get(i..).unwrap_or_default(); let marker = rest.iter().take_while(|gl| gl.len == 0).count(); let mut w: f64 = rest.iter().take(marker).map(|gl| gl.adv).sum(); - for gl in rest.iter().skip(marker).take(MAX_GLYPHS) { - if gl.is_space() { - break; + let start = i + marker; + let mut end = start; + // The spaces at the end of what is measured so far (inside a No Break run), and the end of its + // last glyph that is not a space. + let (mut trailing, mut ink) = (0.0, start); + let set = |w: f64, trailing: f64, ink: usize| w - trailing - line_end_credit(g, line_end, ink); + while let Some(gl) = g.get(end).filter(|_| end - start < MAX_GLYPHS) { + if gl.is_space() && (end == start || !splits_no_break(g, end)) { + return set(w, trailing, ink); } if gl.is_soft_hyphen() { return w + hyphen_glyph(gl).adv; } w += gl.adv; - if gl.break_after() { - break; + end += 1; + if gl.is_space() { + trailing += gl.adv; + } else { + (trailing, ink) = (0.0, end); + } + let cluster_ends = g.get(end).is_none_or(|next| next.byte != gl.byte); + if gl.break_after() && cluster_ends && kinsoku_allows(g, end - 1, kinsoku) && !splits_no_break(g, end) { + return set(w, trailing, ink); } } - w + if end >= g.len() { set(w, trailing, ink) } else { w } } /// Greedy break: returns (end glyph index, hyphenated) for a line starting at `i` of `width`. diff --git a/crates/text/src/layout/flow.rs b/crates/text/src/layout/flow.rs index 5617902f1d..05ec2e0e76 100644 --- a/crates/text/src/layout/flow.rs +++ b/crates/text/src/layout/flow.rs @@ -71,6 +71,12 @@ fn compose_para( return None; } let total: f64 = sg.iter().map(|g| g.adv).sum(); + // The same rule as the line loop below: an opening bracket starting a wrapped line gives up + // the space before it. + let start_credit = |i: usize| { + let flush = !rtl && para.mojikumi == Mojikumi::LineEndHalf; + sg.get(i).filter(|g| flush && g.lead <= 0.0).and_then(|g| punct_half(g, Punct::Opening)).unwrap_or(0.0) + }; let mut sim = pen.clone(); let mut widths = vec![]; let mut acc = 0.0; @@ -80,8 +86,9 @@ fn compose_para( let first = widths.is_empty(); let ind_l = para.laid_out_left_indent() + if first && !continued { para.first_line_indent } else { 0.0 }; // Only the first line's first word is known here. Later lines are placed as the composed - // lines need: each fits its width, so its first word does too. - let word = if first { word_width(sg, 0) } else { 0.0 }; + // lines need: each fits its width, so its first word does too. It is measured as the line + // loop measures it, with Mojikumi's narrower start and end. + let word = if first { word_width(sg, 0, line_end, para.kinsoku) - start_credit(0) } else { 0.0 }; let Some((b, x0, x1, _)) = sim.place(m, ind_l, para.right_indent, word) else { break }; let w = x1 - x0 - ind_l - para.right_indent; widths.push(w); @@ -106,13 +113,7 @@ fn compose_para( // stop at four times its stretch and fall back to the single-line composer; the documented // default range (word spacing up to 133%) is narrow enough that most paragraphs would. let (strict, loose) = (1.0, f64::INFINITY); - // The same rule as the line loop below: an opening bracket starting a wrapped line gives up - // the space before it. - let start_credit = |i: usize| { - let flush = !rtl && para.mojikumi == Mojikumi::LineEndHalf; - sg.get(i).filter(|g| flush && g.lead <= 0.0).and_then(|g| punct_half(g, Punct::Opening)).unwrap_or(0.0) - }; - // …and its last character is set narrower, as the line loop sets it. + // A line's last character is set narrower, as the line loop sets it. let end_credit = |end: usize| end.checked_sub(1).and_then(|k| line_end.get(k)).copied().unwrap_or(0.0); let pass = |p: &Params| compose(sg, &width, &start_credit, &end_credit, &cands, flex, p); pass(¶ms(strict)).or_else(|| pass(¶ms(loose))) @@ -352,8 +353,6 @@ fn place_flow_line(cx: &mut Ctx<'_>, pen: &mut Pen<'_>, context: &mut PlacementC let est = if *i < n { Metrics::of(&sg[*i]) } else { *pm }; let first_line = *para_line == 0 && !*continued; let (ind_l, ind_r) = line_indents(para, first_line, *mirror); - // Measured before Mojikumi narrows an opening bracket, as the composer measures it. - let word = word_width(sg, *i); // Mojikumi: an opening bracket starting a line is set flush with the line's start (the // space before it goes), and the line has that much more room. At a paragraph's start // that is the first-line indent: JIS X 4051's principle (JLREQ 3.1.5, Figure 71 ①). @@ -366,6 +365,8 @@ fn place_flow_line(cx: &mut Ctx<'_>, pen: &mut Pen<'_>, context: &mut PlacementC glyph.adv -= half; glyph.lead += half; } + // The line's first word, as narrow as the line sets it at both ends. + let word = word_width(sg, *i, line_end, para.kinsoku); // Place, break, then settle the baseline on the line's real metrics (moving on to the // next row/column if it no longer fits). loop { diff --git a/crates/text/src/tests/vertical.rs b/crates/text/src/tests/vertical.rs index 52061213a2..e76aee72f0 100644 --- a/crates/text/src/tests/vertical.rs +++ b/crates/text/src/tests/vertical.rs @@ -1145,6 +1145,106 @@ fn mojikumi_line_end_half_width_counts_when_a_line_breaks() { } } +/// Mojikumi: in a frame that is narrow at the top, and in a frame a wrap object narrows at the top, +/// a line's first word is measured as the line sets it. A word ending with a closing bracket that +/// fits the narrow lines only with the bracket half width, and an opening bracket that fits them +/// only flush with the line's start, start on the first line, with both composers. An opening +/// bracket that kinsoku keeps with the character after it, and a No Break run, are measured whole. +#[test] +fn mojikumi_narrows_a_lines_first_word_when_looking_for_room() { + use crate::test_fonts::{FULL_WIDTH_FAMILY, full_width_font}; + use vectorcraft_doc::{Composer, Kinsoku, KinsokuShoriType, Mojikumi, TextWrap, WrapShape}; + let db = FontDb::with_font_dirs(vec![]); + db.add_font(full_width_font().expect("test font")); + // The top 40 pt of the frame are `top` wide, the rest 200: an L-shaped frame, or a rectangle + // with a wrap object over the top right. + let lay = |text: &str, styled: fn(&mut TextObject), composer: Composer, top: f64, wrapped: bool| { + let style = CharStyle { size: 20.0, font_family: FULL_WIDTH_FAMILY.into(), font_style: "Regular".into(), ..CharStyle::default() }; + let mut t = TextObject::point(Point::ZERO, text, style); + t.xf = Affine::IDENTITY; + styled(&mut t); + t.para.mojikumi = Mojikumi::LineEndHalf; + t.para.composer = composer; + t.para.kinsoku = Kinsoku::Hard; + t.para.kinsoku_type = KinsokuShoriType::PushOutOnly; + let frame = if wrapped { + t.wrap = vec![WrapShape { + path: PathData::from_bezpath(&Rect::new(top, -10.0, 210.0, 40.0).to_path(0.1)), + wrap: TextWrap { offset: 0.0, invert: false }, + }]; + Rect::new(0.0, 0.0, 200.0, 200.0).to_path(0.1) + } else { + let mut p = kurbo::BezPath::new(); + p.move_to((0.0, 0.0)); + p.line_to((top, 0.0)); + p.line_to((top, 40.0)); + p.line_to((200.0, 40.0)); + p.line_to((200.0, 200.0)); + p.line_to((0.0, 200.0)); + p.close_path(); + p + }; + t.kind = TextKind::Area { frame: PathData::from_bezpath(&frame) }; + layout(&db, &t) + }; + let plain: fn(&mut TextObject) = |_| {}; + let wide_bracket: fn(&mut TextObject) = |t| edit::style_range(&mut t.runs, 0, '「'.len_utf8(), |s| s.h_scale = 250.0); + let no_break: fn(&mut TextObject) = |t| { + let n = t.plain_text().len(); + edit::style_range(&mut t.runs, 0, n, |s| s.no_break = true); + }; + let no_break_but_the_last_word: fn(&mut TextObject) = |t| { + let n = t.plain_text().rfind(' ').map_or(0, |k| k + 1); + edit::style_range(&mut t.runs, 0, n, |s| s.no_break = true); + }; + // "ABCD」" is `full` wide inside a line; ending one, 」 is half width. + let closing = "ABCD」EFGH"; + let open = lay(closing, plain, Composer::SingleLine, 200.0, false); + let full: f64 = open.glyphs[..5].iter().map(|g| g.advance).sum(); + assert!((open.glyphs[4].advance - 20.0).abs() < 1e-9, "」 is one em inside a line"); + for wrapped in [false, true] { + for composer in [Composer::SingleLine, Composer::EveryLine] { + let at = format!("wrapped {wrapped} {composer:?}"); + let top = full - 5.0; + let l = lay(closing, plain, composer, top, wrapped); + let first = l.lines.first().expect("a line"); + assert_eq!(&closing[first.start..first.end], "ABCD」", "{at}"); + assert!(first.baseline < 40.0, "{at}: the word starts on the narrow lines: {}", first.baseline); + let set: f64 = l.glyphs[first.glyph_start..first.glyph_end].iter().map(|g| g.advance).sum(); + assert!(set <= top + 1e-9, "{at}: {set} in {top}"); + // 「 is 50 pt at 250%, 25 pt flush with the line's start: 「一 is 45 pt. + let opening = "「一二三四五六"; + let l = lay(opening, wide_bracket, composer, 47.0, wrapped); + let first = l.lines.first().expect("a line"); + assert_eq!(&opening[first.start..first.end], "「一", "{at}"); + assert!(first.baseline < 40.0, "{at}: 「 starts on the narrow lines: {}", first.baseline); + let set: f64 = l.glyphs[first.glyph_start..first.glyph_end].iter().map(|g| g.advance).sum(); + assert!(set <= 47.0 + 1e-9, "{at}: {set}"); + // Kinsoku keeps 「 with 一: in 40 pt they don't fit, and the line starts below, whole. + let l = lay(opening, wide_bracket, composer, 40.0, wrapped); + let first = l.lines.first().expect("a line"); + assert!(first.baseline > 40.0 && &opening[first.start..first.end] != "「", "{at}: kinsoku: {:?}", placed_lines(&l, opening)); + // A No Break run is one word, its space included: it doesn't fit the narrow lines. + let run_text = "ABCD EFGH"; + let run: f64 = lay(run_text, no_break, composer, 200.0, false).glyphs.iter().map(|g| g.advance).sum(); + let l = lay(run_text, no_break, composer, run - 5.0, wrapped); + let first = l.lines.first().expect("a line"); + assert!(first.baseline > 40.0 && &run_text[first.start..first.end] == run_text, "{at}: no break: {:?}", placed_lines(&l, run_text)); + // …but the space ending a No Break run is not set at the line's end: the run fits a + // line 2 pt wider than it is without that space. + let ending = "ABCD EFGH IJ"; + let l = lay(ending, no_break_but_the_last_word, composer, run + 2.0, wrapped); + let first = l.lines.first().expect("a line"); + assert!(first.baseline < 40.0 && ending[first.start..first.end].trim_end() == run_text, "{at}: {:?}", placed_lines(&l, ending)); + } + } +} + +/// Each line's baseline and text. +fn placed_lines(l: &TextLayout, text: &str) -> Vec<(f64, String)> { + l.lines.iter().map(|line| (line.baseline, text[line.start..line.end].to_string())).collect() +} + /// Burasagari: in a measure of exactly five ems, a comma that would be the sixth character goes to /// the next line with the character before it (None) or hangs outside the line (Standard, Forced); /// a full stop that is the fifth character stays inside (None, Standard) or hangs while the four diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e2d7c73167..90ed2fc30b 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1188,7 +1188,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur When a drag on the Tabs panel's ruler or a value typed in its X field moves a tab stop past another stop, the moved stop stays selected. The Tabs panel menu's Clear All Tabs (`text.tabs.clear`), Delete Tab and Repeat Tab (`text.tabs.set`) can be chosen while the panel's ruler shows tab stops. Delete Tab removes the stop selected on the ruler. Repeat Tab replaces the stops after the selected stop with up to 20 copies of it. The copies are spaced by the selected stop's distance from the stop before it, or from the paragraph's left indent (18 pt further in for each list level past the first) when it is the first stop, and no copy is placed at 2,000 pt or beyond. Clear All Tabs removes every stop, so the default stops every ½ inch apply again. - **Interface languages, vertical type:** Preferences ▸ User Interface (or VectorCraft ▸ Language on macOS) picks Automatic (the system locale) or a registered language, persisted as `interfaceLanguage`. Translations come from per-language catalogs (`i18n/*.tsv`): Traditional and Simplified Chinese, Japanese, German (every `de-*` locale), Spanish (neutral international Spanish, for every `es-*` locale), French (every `fr-*` locale), Italian (every `it-*` locale), Russian (every `ru-*` locale) and Ukrainian (every `uk-*` locale), Catalan (every `ca-*` locale) and Polish (every `pl-*` locale; AI-drafted, native review pending) cover the whole interface, Czech every menu label; Czech, German, Greek, Spanish, French, Hungarian, Italian, Japanese, Korean, Vietnamese, Polish, Brazilian Portuguese, Russian, Simplified Chinese, Traditional Chinese, Swedish, Ukrainian and Finnish also translate status-bar and error messages as they are shown (`@msg` templates, every message literal tested; the messages themselves stay English for agents); untranslated text stays English. Japanese and Chinese glyphs come from the craft-fonts build input (release builds) or the installed fonts, Czech, German, Spanish, French, Italian, Russian and Ukrainian ones from the bundled UI fonts. The web app follows the browser's languages (or `?lang=`). The Basic toolbar's Type button holds Vertical Type in its flyout. Vertical Type, Vertical Area Type and Vertical Type on a Path create vertical text (columns right to left, upright CJK glyphs and marks with `vert` alternates, Latin and longer numbers on their side, two- and three-digit numbers set as tate-chu-yoko in one em of the column (kept whole when justified), upright glyphs centred on the column (tracked or justified too), point type's anchor on the first column's centre line; on a vertical path, upright glyphs and tate-chu-yoko blocks face the path's start, and letters on their side are a quarter turn clockwise from them (with Skew, 3D Ribbon and Stair Step only on a path that runs to the right), in one column that Align to Path Baseline or Center centers on the path and that the caret and selection span); kinsoku applies to horizontal and vertical Japanese (Paragraph panel › Kinsoku Set, `text.setFormat {kinsoku}`: Hard, the default, keeps closing marks, iteration marks, the prolonged sound mark and small kana off the line start and opening brackets off its end; Soft lets 々, ー and small kana start a line as JLREQ's level 3 rules do; None turns it off), and justified Japanese lines spread their room between characters (not inside Latin words); Paragraph panel › Mojikumi Set (shown with Preferences › Type › Show East Asian Options, `text.setFormat {mojikumi}`): None or Line-end Punctuation Half Width (new type's default, as in Illustrator; older documents and imported text keep None) shares one half-em space between consecutive punctuation, sets a closing mark ending a line half width and an opening bracket starting a line flush with its start (a paragraph's first line at its first-line indent, JIS X 4051's principle in JLREQ 3.1.5), and puts a quarter em between Japanese and Latin letters or digits; Paragraph panel menu › Top-to-Top / Bottom-to-Bottom Leading measures leading from em box top to top (area type's first line touches the frame) or from baseline to baseline (new type in a Japanese interface starts top to top, with em box centre character alignment, #432); Paragraph panel menu › Burasagari None / Regular / Force (`text.setFormat {burasagari: "none"|"standard"|"forced"}`) hangs an East Asian comma or full stop (、。,.、。) ending an area type line outside it, Regular (new type's default) when it doesn't fit and Force always (the rest of the line then fills the measure; closing brackets and Latin punctuation don't hang); Character panel menu › Character Alignment (with the East Asian options; `text.setFormat` or `text.setRangeStyle {charAlign}`) lines characters smaller than the largest on their line up on the Roman baseline, the em box top/right, centre or bottom/left, or the ICF (ideographic character face) top/right or bottom/left, from the font's BASE table or, without one, the average ink box of some ideographs and kana; Proportional Metrics (the same menu; `{proportionalMetrics}`) sets full-width glyphs on the font's proportional widths (OpenType `palt`) in horizontal type and upright glyphs on its proportional heights (`vpal`, shaped top to bottom; fonts without vertical metrics keep their cells) in vertical type, and Line-end Punctuation Half Width takes nothing more off the punctuation they re-spaced; Type ▸ Type Orientation switches existing text; caret, selection, hit testing and arrow keys follow the writing direction. Vertical text exports to PDF as real text and to SVG as outlines. The interface is available in Polish (`pl`): menus, panels, dialogs and messages, with one/few/many plurals; AI-drafted, native review pending (#1498). - With Line-end Punctuation Half Width, both composers measure a line with its last character set as narrow as Mojikumi sets it ([the help page](https://helpx.adobe.com/illustrator/desktop/design-with-text/edit-format-text/use-mojikumi-for-japanese-text-composition.html)): a closing bracket, comma or full stop that fits at half width ends the line, and the character before it no longer goes to the next line with it. A mark that already lost its half to the punctuation after it and a line that ends in a hyphen take nothing more off, and the quarter em between Japanese and Latin text counts only where the line ends there. Kinsoku Shori Type and Burasagari Regular measure lines the same way; right-to-left lines and Mojikumi None break as before (`crates/text/src/layout/wrapping_tests.rs`, `crates/text/src/tests/vertical.rs`). Scores and estimates are unchanged. + With Line-end Punctuation Half Width, both composers measure a line with its last character set as narrow as Mojikumi sets it ([the help page](https://helpx.adobe.com/illustrator/desktop/design-with-text/edit-format-text/use-mojikumi-for-japanese-text-composition.html)): a closing bracket, comma or full stop that fits at half width ends the line, and the character before it no longer goes to the next line with it. A mark that already lost its half to the punctuation after it and a line that ends in a hyphen take nothing more off, and the quarter em between Japanese and Latin text counts only where the line ends there. Kinsoku Shori Type and Burasagari Regular measure lines the same way; right-to-left lines and Mojikumi None break as before. In a frame that is not a rectangle, or with text wrap, a line's first word is measured as it is set (an opening bracket at its start flush, its last character narrowed) when layout looks for a line wide enough for it, and it ends only where the line may break, so kinsoku keeps an opening bracket with the character after it and a No Break run counts whole (`crates/text/src/layout/wrapping_tests.rs`, `crates/text/src/tests/vertical.rs`). Scores and estimates are unchanged. Interface text in characters the interface's fonts lack, drawn in a bundled Japanese, Georgian or Devanagari face or in an installed font that covers them, sits on the baseline of the Latin text beside it, monospace text included (#5816; `crates/ui-egui/src/ui_fonts.rs`, `crates/ui-egui/src/theme.rs`). Character panel menu › Standard Vertical Roman Alignment (with the East Asian options; `text.setFormat` or `text.setRangeStyle {standardVerticalRoman}`, off by default) stands the selected Latin letters, digits and other half-width characters upright one below the other in vertical type instead of rotating them ([formatting Asian characters](https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html)); spaces keep their width, tate-chu-yoko blocks stay across the column, and Character Style Options gives it its own checkbox beside Character Alignment. Character panel menu › Tate-chu-yoko Settings… (with the East Asian options; `ui.tateChuYokoSettings`, `text.setFormat` or `text.setRangeStyle {tcyUpDown, tcyLeftRight}`, in points, 0 by default) moves a marked tate-chu-yoko block in vertical type up by a positive Up/Down and right by a positive Left/Right ([format East Asian characters in vertical text](https://helpx.adobe.com/illustrator/desktop/design-with-text/edit-format-text/format-east-asian-characters-in-vertical-text.html), [formatting Asian characters](https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html)); the block moves as one piece with its first character's values and keeps its one em down the column, on a vertical path it moves toward the path's start and to the path's left, automatic digit blocks do not move, Reset Panel sets both back to 0, and character styles and the native file keep them.