mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 03:51:16 +00:00
fix(text): review findings for the Standard composers
Unshaped complex scripts: no joiner boxes, no kashidas or path bending as if joined, a toggle that keeps shared Standard shaping, a test font fix, and doc wording for the scripts left unshaped.
This commit is contained in:
@@ -220,7 +220,7 @@ fn all_four_composers_round_trip() {
|
||||
use vectorcraft_doc::{TextKind, TextObject};
|
||||
let one = |c: Composer| {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let mut t = TextObject::point(kurbo::Point::ZERO, "ક\u{ACD}ક", Default::default());
|
||||
let mut t = TextObject::point(vectorcraft_geom::Point::ZERO, "ક\u{ACD}ક", Default::default());
|
||||
t.para.composer = c;
|
||||
t.kind = TextKind::Point;
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -392,7 +392,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"type.lineComposer",
|
||||
"Toggle Line Composer",
|
||||
"Cmd+Alt+Shift+C",
|
||||
"{} the selected type's paragraphs (or those the Type tool's selection touches) use the Single-line Composer when they all use the Every-line Composer, else the Every-line Composer, each keeping its shaping: Middle Eastern & South Asian or Standard (text.setFormat {composer}) → \"singleLine\" | \"everyLine\" | \"standardSingleLine\" | \"standardEveryLine\"",
|
||||
"{} the selected type's paragraphs (or those the Type tool's selection touches) use the Single-line Composer when they all use the Every-line Composer, else the Every-line Composer; paragraphs that all use a Standard composer keep it, any other mix takes the Middle Eastern & South Asian one (text.setFormat {composer}) → \"singleLine\" | \"everyLine\" | \"standardSingleLine\" | \"standardEveryLine\"",
|
||||
),
|
||||
(
|
||||
"type.hyphenate",
|
||||
|
||||
@@ -164,6 +164,7 @@ impl OtFeatures {
|
||||
extra: u32::MAX >> (32 - EXTRA_FEATURES.len()),
|
||||
extra_values: [0; EXTRA_FEATURES.len()],
|
||||
swash_value: 0,
|
||||
unshaped_complex: false,
|
||||
};
|
||||
change(&mut off);
|
||||
change(&mut on);
|
||||
|
||||
@@ -78,10 +78,11 @@ fn joining(c: char) -> Joining {
|
||||
}
|
||||
|
||||
/// Is the cluster of glyph `a` joined to the cluster of glyph `b` that follows it in `text`: both
|
||||
/// right to left in one face, `a`'s last character other than a mark joining the letter after it,
|
||||
/// and `b` starting with a letter that joins the one before it?
|
||||
/// right to left in one face and set as joining letters (not by a Standard composer), `a`'s last
|
||||
/// character other than a mark joining the letter after it, and `b` starting with a letter that
|
||||
/// joins the one before it?
|
||||
fn joined(text: &str, a: &SGlyph, b: &SGlyph) -> bool {
|
||||
if a.face.id() != b.face.id() || a.level != b.level || !a.level.is_rtl() || a.byte + a.len != b.byte {
|
||||
if a.face.id() != b.face.id() || a.level != b.level || !a.level.is_rtl() || a.byte + a.len != b.byte || !(a.cursive && b.cursive) {
|
||||
return false;
|
||||
}
|
||||
let last = text.get(a.byte..a.byte + a.len).and_then(|s| s.chars().rev().find(|&c| joining(c) != Joining::Transparent));
|
||||
|
||||
@@ -22,7 +22,7 @@ use crate::shape::{
|
||||
Punct, SGlyph, Tcy, cap_x_heights, hyphen_glyph, is_cjk, mark_word_ends, no_line_end, no_line_start, punct, run_face, shape_range, shape_runs,
|
||||
soft_line_start, style_metrics,
|
||||
};
|
||||
use crate::{Composer, FirstBaseline, InlineGlyph, LayoutOptions, LineInfo, ListMarker, OtFeatures, PositionedGlyph, TextLayout, VerticalAlign};
|
||||
use crate::{FirstBaseline, InlineGlyph, LayoutOptions, LineInfo, ListMarker, OtFeatures, PositionedGlyph, TextLayout, VerticalAlign};
|
||||
|
||||
mod bidi;
|
||||
mod breaking;
|
||||
|
||||
@@ -8,9 +8,9 @@ use super::vertical::{
|
||||
EM_CENTER, MojikumiRules, align_shift, compress_punctuation, dot_quarter, glyph_em, halve_punctuation, punct_half, space_japanese_and_latin,
|
||||
};
|
||||
use super::{
|
||||
Affine, BidiInfo, Burasagari, CharStyle, Composer, Ctx, EPS, Flex, Justify, Kinsoku, KinsokuShoriType, LeadingModel, LineFit, LineInfo,
|
||||
ListMarker, ParaStyle, Params, Point, Punct, Range, SGlyph, SingleWordJustify, TextObject, apply_desired, cap_x_heights, compose, fit_line,
|
||||
hyphen_glyph, is_cjk, is_word_space, kashida, mark_word_ends, no_line_end, no_line_start, style_metrics,
|
||||
Affine, BidiInfo, Burasagari, CharStyle, Ctx, EPS, Flex, Justify, Kinsoku, KinsokuShoriType, LeadingModel, LineFit, LineInfo, ListMarker,
|
||||
ParaStyle, Params, Point, Punct, Range, SGlyph, SingleWordJustify, TextObject, apply_desired, cap_x_heights, compose, fit_line, hyphen_glyph,
|
||||
is_cjk, is_word_space, kashida, mark_word_ends, no_line_end, no_line_start, style_metrics,
|
||||
};
|
||||
use crate::justify::is_format_control;
|
||||
use crate::shape::{keep_inseparable_pairs, keep_korean_words};
|
||||
|
||||
@@ -123,7 +123,8 @@ pub(crate) fn joins(script: Script) -> bool {
|
||||
}
|
||||
|
||||
/// Is `script` one a Standard composer leaves unshaped: Indic (conjuncts, half forms, marks placed
|
||||
/// by the font's layout tables), Arabic-like ([`joins`]) and Hebrew?
|
||||
/// by the font's layout tables), the joining scripts ([`joins`]: Arabic, Syriac, N'Ko, Mongolian…)
|
||||
/// and Hebrew?
|
||||
fn is_complex_script(script: Script) -> bool {
|
||||
joins(script)
|
||||
|| matches!(
|
||||
@@ -1395,11 +1396,12 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
let text_seg = &text[range.clone()];
|
||||
// The segment's script: that of its first character of a script of its own.
|
||||
let first_script = text_seg.chars().map(|c| c.script()).find(|s| !matches!(s, Script::Common | Script::Inherited));
|
||||
let cursive = first_script.is_some_and(joins);
|
||||
// A Standard composer sets a complex script glyph by glyph (no layout tables, nominal glyphs).
|
||||
// shortcut: decided for the whole segment by its first strong script, as `cursive` is; segments
|
||||
// already split where the script changes.
|
||||
let unshaped = feats.unshaped_complex && first_script.is_some_and(is_complex_script);
|
||||
// Unshaped letters don't join, so they are not bent along a path as joined ones are.
|
||||
let cursive = first_script.is_some_and(joins) && !unshaped;
|
||||
let full = st.size.max(0.0);
|
||||
// Superscript/subscript and small capitals shrink the glyphs; line metrics keep the full size.
|
||||
let (_, pos_shift) = st.position.scale_shift(full);
|
||||
@@ -1593,15 +1595,17 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
let c = st.digits.map(c);
|
||||
let chars: Vec<char> = if upper { c.to_uppercase().collect() } else { vec![c] };
|
||||
for u in chars {
|
||||
let g = cmap.map(u).unwrap_or_default();
|
||||
let adv = gm.advance_width(g).unwrap_or(face.upem as f32 * 0.5);
|
||||
// Format characters (joiners, direction marks) have no glyph of their own in most
|
||||
// fonts, and the shaper hides them: draw the space's glyph with no advance.
|
||||
let (g, adv) = if crate::justify::is_format_control(u) {
|
||||
(cmap.map(' ').unwrap_or_default(), 0.0)
|
||||
} else {
|
||||
let g = cmap.map(u).unwrap_or_default();
|
||||
(g, gm.advance_width(g).unwrap_or(face.upem as f32 * 0.5))
|
||||
};
|
||||
raw.push((g.to_u32(), cl, adv.round() as i32, 0, 0));
|
||||
}
|
||||
}
|
||||
// Shaped right-to-left text comes out in visual order, which layout expects.
|
||||
if unshaped && level.is_rtl() {
|
||||
raw.reverse();
|
||||
}
|
||||
}
|
||||
}
|
||||
shaped
|
||||
|
||||
@@ -33,8 +33,8 @@ fn g(c: char) -> u32 {
|
||||
u32::from(source_sans_gid(c).expect("a Source Sans glyph"))
|
||||
}
|
||||
|
||||
/// Under the ME&SA composers the conjunct forms (Devanagari: the vowel sign I drawn before its
|
||||
/// syllable and Ka's half form; Gujarati: the half form); under the Standard ones each character
|
||||
/// Under the ME&SA composers the conjunct forms (the vowel sign I drawn before its syllable and Ka's
|
||||
/// half form, in Devanagari and in Gujarati); under the Standard ones each character
|
||||
/// has its own glyph, in text order, as the font maps it.
|
||||
#[test]
|
||||
fn standard_composers_set_indic_text_glyph_by_glyph() {
|
||||
@@ -42,7 +42,7 @@ fn standard_composers_set_indic_text_glyph_by_glyph() {
|
||||
let ids = |v: Vec<(u32, usize)>| v.into_iter().map(|(gid, _)| gid).collect::<Vec<_>>();
|
||||
for composer in [Composer::SingleLine, Composer::EveryLine] {
|
||||
assert_eq!(ids(drawn(&db, DEVANAGARI, composer)), [g('i'), g('k'), g('K')], "{composer:?} Devanagari");
|
||||
assert_eq!(ids(drawn(&db, GUJARATI, composer)), [g('k'), g('K'), g('i')], "{composer:?} Gujarati");
|
||||
assert_eq!(ids(drawn(&db, GUJARATI, composer)), [g('i'), g('k'), g('K')], "{composer:?} Gujarati");
|
||||
}
|
||||
for composer in [Composer::StandardSingleLine, Composer::StandardEveryLine] {
|
||||
let plain = [g('K'), g('.'), g('K'), g('i')];
|
||||
@@ -94,3 +94,18 @@ fn composers_split_into_breaking_and_shaping() {
|
||||
}
|
||||
assert_eq!(Composer::default(), EveryLine, "new type and old documents are ME&SA Every-line");
|
||||
}
|
||||
|
||||
/// A joiner or non-joiner has no glyph in most fonts and the shaper hides it; a Standard composer
|
||||
/// draws it as no width at all, not as a missing-glyph box.
|
||||
#[test]
|
||||
fn standard_composers_give_format_characters_no_width() {
|
||||
let db = db();
|
||||
let text = "क\u{200C}क";
|
||||
for composer in [Composer::StandardSingleLine, Composer::StandardEveryLine] {
|
||||
let l = layout(&db, &object(text, composer));
|
||||
let widths: Vec<f64> = l.glyphs.iter().map(|g| g.advance).collect();
|
||||
assert_eq!(widths.len(), 3, "{composer:?}: {widths:?}");
|
||||
assert_eq!(widths[1], 0.0, "{composer:?}: the non-joiner takes no room: {widths:?}");
|
||||
assert!(widths[0] > 0.0 && widths[2] > 0.0, "{composer:?}: {widths:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,7 +393,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"type.lineComposer",
|
||||
"Toggle Line Composer",
|
||||
"Cmd+Alt+Shift+C",
|
||||
"{} the selected type's paragraphs (or those the Type tool's selection touches) use the Single-line Composer when they all use the Every-line Composer, else the Every-line Composer, each keeping its shaping: Middle Eastern & South Asian or Standard (text.setFormat {composer}) → \"singleLine\" | \"everyLine\" | \"standardSingleLine\" | \"standardEveryLine\"",
|
||||
"{} the selected type's paragraphs (or those the Type tool's selection touches) use the Single-line Composer when they all use the Every-line Composer, else the Every-line Composer; paragraphs that all use a Standard composer keep it, any other mix takes the Middle Eastern & South Asian one (text.setFormat {composer}) → \"singleLine\" | \"everyLine\" | \"standardSingleLine\" | \"standardEveryLine\"",
|
||||
),
|
||||
(
|
||||
"type.hyphenate",
|
||||
|
||||
@@ -54,13 +54,15 @@ pub(crate) fn align(app: &mut VectorcraftApp, justify: &str) -> Result<Value, St
|
||||
}
|
||||
|
||||
/// `type.lineComposer` (Cmd+Alt+Shift+C): the Single-line Composer for the paragraphs the panel
|
||||
/// acts on when they all use the Every-line Composer, else the Every-line Composer; each keeps its
|
||||
/// shaping (a Standard composer toggles to the other Standard one). Returns the composer set.
|
||||
/// acts on when they all break their lines every-line (either shaping), else the Every-line
|
||||
/// Composer. Paragraphs that all use a Standard composer keep it; any other mix takes the Middle
|
||||
/// Eastern & South Asian one. Returns the composer set.
|
||||
pub(crate) fn toggle_composer(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
text_style(app).ok_or("select some type first")?;
|
||||
let shared = shared_composer(app);
|
||||
let to_single = shared.is_some_and(|c| !c.is_single_line());
|
||||
let id = composer_id(shared.unwrap_or_default().with_breaking(to_single));
|
||||
let to_single = shared_para(app, |p| p.composer.is_single_line()) == Some(false);
|
||||
let standard = shared_para(app, |p| p.composer.shapes_complex_scripts()) == Some(false);
|
||||
let base = if standard { Composer::StandardEveryLine } else { Composer::EveryLine };
|
||||
let id = composer_id(base.with_breaking(to_single));
|
||||
format(app, json!({ "composer": id }));
|
||||
Ok(json!(id))
|
||||
}
|
||||
|
||||
@@ -97,11 +97,16 @@ fn toggling_the_line_composer_keeps_the_shaping() {
|
||||
assert_eq!(toggle_composer(&mut app).unwrap(), json!(first), "{from}");
|
||||
assert_eq!(toggle_composer(&mut app).unwrap(), json!(second), "{from} again");
|
||||
}
|
||||
// Paragraphs that differ take Every-line, as before the Standard composers existed.
|
||||
// Paragraphs that break differently take Every-line, as before the Standard composers existed.
|
||||
app.run("text.setFormat", json!({"id": id, "composer": "standardSingleLine"})).unwrap();
|
||||
app.run("text.setFormat", json!({"id": id, "start": 2, "end": 3, "composer": "singleLine"})).unwrap();
|
||||
app.session.set_tool_option("selectAll", &json!(true));
|
||||
assert_eq!(toggle_composer(&mut app).unwrap(), json!("everyLine"));
|
||||
// Paragraphs that both break every-line but differ in shaping: Single-line, Middle Eastern & South Asian.
|
||||
app.run("text.setFormat", json!({"id": id, "composer": "everyLine"})).unwrap();
|
||||
app.run("text.setFormat", json!({"id": id, "start": 2, "end": 3, "composer": "standardEveryLine"})).unwrap();
|
||||
app.session.set_tool_option("selectAll", &json!(true));
|
||||
assert_eq!(toggle_composer(&mut app).unwrap(), json!("singleLine"));
|
||||
}
|
||||
|
||||
/// The Paragraph panel menu lists the four composers, and ticks the one every selected paragraph has.
|
||||
@@ -126,15 +131,23 @@ fn the_paragraph_menu_sets_each_of_the_four_composers() {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let texts = items(&mut app);
|
||||
for label in [
|
||||
"Middle Eastern & South Asian Single-line Composer",
|
||||
"Middle Eastern & South Asian Every-line Composer",
|
||||
"Standard Single-line Composer",
|
||||
"Standard Every-line Composer",
|
||||
] {
|
||||
assert!(texts.iter().any(|t| t == label), "{label} in {texts:?}");
|
||||
let labels = [
|
||||
("singleLine", "Middle Eastern & South Asian Single-line Composer"),
|
||||
("everyLine", "Middle Eastern & South Asian Every-line Composer"),
|
||||
("standardSingleLine", "Standard Single-line Composer"),
|
||||
("standardEveryLine", "Standard Every-line Composer"),
|
||||
];
|
||||
// The menu lists all four, each row ticked (and only that one) when every selected paragraph has it.
|
||||
for (value, _) in labels {
|
||||
app.run("text.setFormat", json!({"id": id, "composer": value})).unwrap();
|
||||
app.session.set_tool_option("selectAll", &json!(true));
|
||||
let texts = items(&mut app);
|
||||
for (v, label) in labels {
|
||||
let row = if v == value { format!("✓ {label}") } else { format!(" {label}") };
|
||||
assert!(texts.iter().any(|t| *t == row), "{value}: {row:?} in {texts:?}");
|
||||
}
|
||||
}
|
||||
// Choosing a row sets its composer: the row is a `text.setFormat {composer}` with the row's id.
|
||||
for (value, want) in [
|
||||
("standardEveryLine", Composer::StandardEveryLine),
|
||||
("singleLine", Composer::SingleLine),
|
||||
@@ -142,6 +155,7 @@ fn the_paragraph_menu_sets_each_of_the_four_composers() {
|
||||
] {
|
||||
app.run("text.setFormat", json!({"id": id, "composer": value})).unwrap();
|
||||
assert_eq!(shared_composer(&app), Some(want), "{value}");
|
||||
assert_eq!(composer_id(want), value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user