From 783437f7754e7bc1084e52af6008e29c1c0fa08a Mon Sep 17 00:00:00 2001 From: ruchirguitar <113586074+ruchirguitar@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:58:41 +0530 Subject: [PATCH] M7/G4: retype review: hidden art, compositing, half turns, cost (#4934) Hidden and locked shapes stay outlines and end the line. Isolated and knockout groups keep their type inside. Type turned 180 degrees stays one object (angles compared wrapped). A lone one-contour glyph beside other shapes stays an outline unless it is the only shape selected. The index samples only glyphs whose key (segments per contour) a selected shape has, reads outlines past the shared outline cache, keeps each face's keys once worked out, and loads no more faces than it takes. With Microsoft YaHei (29,905 glyphs) loaded: 877 ms the first time, 23 ms after. --- crates/engine/src/cmd/retype.rs | 44 +++++++++-- crates/engine/src/cmd/retype_tests.rs | 104 ++++++++++++++++++++++++-- crates/text/src/fontdb/lookup.rs | 12 +++ crates/text/src/retype.rs | 90 ++++++++++++++++++---- 4 files changed, 222 insertions(+), 28 deletions(-) diff --git a/crates/engine/src/cmd/retype.rs b/crates/engine/src/cmd/retype.rs index be7df50d95..28a74ac6a9 100644 --- a/crates/engine/src/cmd/retype.rs +++ b/crates/engine/src/cmd/retype.rs @@ -14,6 +14,7 @@ //! they are and are reported. Near matches (another version of the font, simplified outlines, //! merged glyphs) and images are phase 2. +use std::f64::consts::{PI, TAU}; use std::sync::Arc; use kurbo::{BezPath, Point, Vec2}; @@ -72,6 +73,11 @@ fn collect(n: &Node, parent: Option, depth: usize, barrier: &mut bool, t *truncated = true; return; } + // Hidden and locked art is left as it is: a hidden glyph must not come back as visible type. + if !n.visible || n.locked { + *barrier = true; + return; + } let piece = |path: BezPath, after_barrier: bool| Piece { id: n.id, parent, @@ -126,15 +132,20 @@ fn candidate_faces(db: &FontDb, d: &Document, fonts: &[String]) -> Vec= cap { + break 'families; + } if let Some(face) = db.face(f, &style) { faces.push(face); } } } - faces.extend(db.loaded_faces()); + faces.extend(db.loaded_faces().into_iter().take(cap.saturating_sub(faces.len()))); faces } @@ -174,7 +185,8 @@ impl Line { && m.face.id() == self.face.id() // One size and turn for the whole line: the type sets every glyph at the first one's. && (m.scale - self.scale).abs() <= self.scale * 1e-4 - && (m.angle - self.angle).abs() < 1e-5 + // Wrapped: type turned 180° has angles near +π and −π. + && ((m.angle - self.angle + PI).rem_euclid(TAU) - PI).abs() < 1e-5 && across < size * 0.05 && (m.origin - self.end).dot(dir) > -size * 0.25 } @@ -362,7 +374,11 @@ fn retype(s: &mut Session, p: &Value) -> Result { if pieces.is_empty() { return Err(EngineError::Other("Retype: select outlined text (paths or compound paths)".into())); } - let index = RetypeIndex::build(db, candidate_faces(db, d, &fonts)); + // Only glyphs with a selected shape's key (segments per contour) can match one: the others are + // not sampled. + let wanted: std::collections::BTreeSet> = + pieces.iter().filter(|pc| !pc.keeps_outline).filter_map(|pc| vectorcraft_text::retype::shape_key(&pc.path)).collect(); + let index = RetypeIndex::build(db, candidate_faces(db, d, &fonts), &wanted); let found: Vec> = pieces.iter().map(|pc| if pc.keeps_outline { vec![] } else { index.matches(&pc.path) }).collect(); // The face most shapes match, so a glyph several fonts share is set in the same font as // the glyphs around it. @@ -395,7 +411,19 @@ fn retype(s: &mut Session, p: &Value) -> Result { } open = true; } - let lines = verified(lines, system_layout, &mut unmatched); + let mut lines = verified(lines, system_layout, &mut unmatched); + // A lone one-contour glyph (a period, a hyphen, a bullet) is too plain to tell from drawn + // art (a dot, a bar): beside other shapes it stays an outline. Selected on its own, it is + // what was asked for. + if pieces.len() > 1 { + lines.retain(|l| { + let lone = matches!(l.matches.as_slice(), [m] if m.contours == 1); + if lone { + unmatched.extend(l.pieces.iter().copied()); + } + !lone + }); + } (lines, unmatched, truncated) }; if lines.is_empty() { @@ -434,7 +462,11 @@ fn retype(s: &mut Session, p: &Value) -> Result { && n.blend == vectorcraft_color::BlendMode::Normal && *n.appearance == Appearance::default() && n.name.is_none() - && n.extras().mask.is_none(); + && n.extras().mask.is_none() + // Isolation and knockout change how the group composites its type. + && !n.isolate + && n.knockout == vectorcraft_doc::Knockout::default() + && !n.knockout_shape; let only_type = children.iter().all(|c| ids.contains(&c.id)); if children.is_empty() { d.remove(g)?; diff --git a/crates/engine/src/cmd/retype_tests.rs b/crates/engine/src/cmd/retype_tests.rs index 4c331720a1..cbbf1e0302 100644 --- a/crates/engine/src/cmd/retype_tests.rs +++ b/crates/engine/src/cmd/retype_tests.rs @@ -148,7 +148,7 @@ fn a_shape_between_glyphs_ends_the_line_so_paint_order_holds() { fn glyphs_of_different_groups_and_transparency_stay_apart() { let mut s = session(); let a = outlined(&mut s, json!({"x": 40, "y": 100, "text": "ab", "size": 20})); - let b = outlined(&mut s, json!({"x": 120, "y": 100, "text": "cd", "size": 20})); + let b = outlined(&mut s, json!({"x": 120, "y": 100, "text": "ed", "size": 20})); let first_d = children_of(&s, b)[1]; s.edit("test", |d, _| { d.node_mut(NodeId(b)).unwrap().opacity = 0.5; @@ -158,7 +158,7 @@ fn glyphs_of_different_groups_and_transparency_stay_apart() { .unwrap(); s.execute("select.set", &json!({"ids": [a, b]})).unwrap(); let r = s.execute("type.retype", &json!({})).unwrap(); - assert_eq!(r["texts"], json!(["ab", "c", "d"]), "same baseline, other group; d has its own opacity: {r}"); + assert_eq!(r["texts"], json!(["ab", "e", "d"]), "same baseline, other group; d has its own opacity: {r}"); let ids: Vec = r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect(); let d = &s.doc().unwrap().doc; assert!(d.node(NodeId(a)).is_none(), "a plain group gives way to its type"); @@ -232,11 +232,11 @@ fn live_type_selected_between_loose_glyphs_still_ends_the_line() { fn glyphs_selected_inside_groups_join_only_their_siblings() { let mut s = session(); let g1 = outlined(&mut s, json!({"x": 40, "y": 100, "text": "ab", "size": 20})); - let g2 = outlined(&mut s, json!({"x": 70, "y": 100, "text": "cd", "size": 20})); + let g2 = outlined(&mut s, json!({"x": 70, "y": 100, "text": "ed", "size": 20})); let (a, b, c) = (children_of(&s, g1)[0], children_of(&s, g1)[1], children_of(&s, g2)[0]); s.execute("select.set", &json!({"ids": [a, b, c]})).unwrap(); let r = s.execute("type.retype", &json!({})).unwrap(); - assert_eq!(r["texts"], json!(["ab", "c"]), "a and b are siblings; c is in another group: {r}"); + assert_eq!(r["texts"], json!(["ab", "e"]), "a and b are siblings; e is in another group: {r}"); let d = &s.doc().unwrap().doc; assert!(d.node(NodeId(g2)).is_some(), "d's group stays with c's type and d in it"); } @@ -244,7 +244,7 @@ fn glyphs_selected_inside_groups_join_only_their_siblings() { #[test] fn tracked_type_splits_where_its_spacing_cannot_be_set_again() { let mut s = session(); - let id = s.execute("text.create", &json!({"x": 40, "y": 100, "text": "abc", "size": 30})).unwrap()["id"].as_u64().unwrap(); + let id = s.execute("text.create", &json!({"x": 40, "y": 100, "text": "abe", "size": 30})).unwrap()["id"].as_u64().unwrap(); // Less than half a space: no whole number of spaces makes up the gap (0.4 em would be two // spaces in this font, and the outlines cannot tell those apart from tracking). s.execute("text.setRangeStyle", &json!({"id": id, "start": 1, "end": 3, "tracking": 50})).unwrap(); @@ -253,7 +253,7 @@ fn tracked_type_splits_where_its_spacing_cannot_be_set_again() { s.execute("select.set", &json!({"ids": [g]})).unwrap(); let r = s.execute("type.retype", &json!({})).unwrap(); let texts: Vec = r["texts"].as_array().unwrap().iter().map(|t| t.as_str().unwrap().to_owned()).collect(); - assert_eq!(texts.concat(), "abc", "{r}"); + assert_eq!(texts.concat(), "abe", "{r}"); assert!(texts.len() > 1, "the tracked glyphs are set where the outlines are, not where untracked type puts them: {r}"); assert_eq!(r["unmatched"], json!([])); } @@ -319,3 +319,95 @@ fn a_glyph_painted_beyond_what_type_takes_stays_an_outline() { assert_eq!(r["texts"], json!(["B"]), "two fills: A stays an outline: {r}"); assert_eq!(r["unmatched"], json!([a])); } + +#[test] +fn a_hidden_or_locked_glyph_stays_an_outline() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "ABD", "size": 40})); + let k = children_of(&s, g); + s.edit("test", |d, _| { + d.node_mut(NodeId(k[0])).unwrap().visible = false; + d.node_mut(NodeId(k[2])).unwrap().locked = true; + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!(["B"]), "{r}"); + let d = &s.doc().unwrap().doc; + assert!(!d.node(NodeId(k[0])).unwrap().visible, "A stays a hidden outline"); + assert!(d.node(NodeId(k[2])).unwrap().locked, "D stays a locked outline"); +} + +#[test] +fn an_isolated_or_knockout_group_keeps_its_type_inside() { + for knock in [false, true] { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "AB", "size": 40})); + s.edit("test", |d, _| { + let n = d.node_mut(NodeId(g)).unwrap(); + if knock { + n.knockout = vectorcraft_doc::Knockout::On; + } else { + n.isolate = true; + } + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + let id = r["ids"][0].as_u64().unwrap(); + assert_eq!(children_of(&s, g), vec![id], "knockout {knock}: the group stays, holding the type"); + } +} + +#[test] +fn type_turned_half_round_stays_one_object() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 300, "y": 200, "text": "Upside", "size": 30, "rotation": 180})); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!(["Upside"]), "{r}"); +} + +#[test] +fn a_drawn_dot_or_bar_beside_type_stays_art() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "AB", "size": 40})); + let dot = s.execute("shape.ellipse", &json!({"x": 300, "y": 60, "width": 8, "height": 8})).unwrap()["id"].as_u64().unwrap(); + // A bar that is exactly a hyphen of the font, somewhere else: the worst case. + let bar = outlined(&mut s, json!({"x": 200, "y": 300, "text": "-", "size": 50})); + s.execute("select.set", &json!({"ids": [g, dot, bar]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!(["AB"]), "{r}"); + let un = r["unmatched"].as_array().unwrap(); + assert!(un.contains(&json!(dot)) && un.len() == 2, "the dot and the bar stay art: {r}"); + // Selected on its own, the hyphen is what was asked for. + s.execute("select.set", &json!({"ids": [bar]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!(["-"]), "{r}"); +} + +/// How long Retype takes with a large CJK face to match against (run with `--ignored +/// --nocapture`; skips when no such face is installed). Not a pass/fail budget: it prints. +#[test] +#[ignore = "timing: needs an installed CJK font"] +fn timing_with_a_large_cjk_face() { + let db = vectorcraft_text::FontDb::global(); + let Some(family) = ["Microsoft YaHei", "Yu Gothic", "MS Gothic", "Noto Sans CJK JP", "Hiragino Sans"].into_iter().find(|f| db.has_family(f)) + else { + eprintln!("skipped: no large CJK font installed"); + return; + }; + let face = db.resolve(family, "Regular").map(|(f, _)| f).unwrap(); + let mut s = session(); + let g = outlined(&mut s, json!({"x": 20, "y": 80, "text": "漢字のテスト ABC", "size": 24, "font": family, "style": face.style})); + for run in ["cold", "warm"] { + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let t0 = std::time::Instant::now(); + let r = s.execute("type.retype", &json!({"fonts": [family]})).unwrap(); + let ms = t0.elapsed().as_secs_f64() * 1000.0; + eprintln!("{run}: Retype with {family} ({} encoded glyphs) loaded: {ms:.0} ms → {}", face.mapped_chars().len(), r["texts"]); + s.execute("edit.undo", &json!({})).unwrap(); + } +} diff --git a/crates/text/src/fontdb/lookup.rs b/crates/text/src/fontdb/lookup.rs index ef394cb079..99844220f5 100644 --- a/crates/text/src/fontdb/lookup.rs +++ b/crates/text/src/fontdb/lookup.rs @@ -675,6 +675,18 @@ impl FontDb { p } + /// [`Self::outline`] without the cache: for reading many glyphs once (Retype's index), which + /// would otherwise push out the outlines the canvas keeps there. + pub fn outline_uncached(&self, face: &FontFace, gid: u32) -> BezPath { + let mut pen = FlipPen(BezPath::new()); + if let Some(f) = face.skrifa() + && let Some(g) = f.outline_glyphs().get(GlyphId::new(gid)) + { + let _ = g.draw(DrawSettings::unhinted(Size::unscaled(), face.location()), &mut pen); + } + pen.0 + } + /// Glyph `gid`'s colour bitmap (`sbix`, `CBDT`) in `face`, cached per (face, glyph). pub fn glyph_image(&self, face: &FontFace, gid: u32) -> Option> { if !face.colour_bitmaps { diff --git a/crates/text/src/retype.rs b/crates/text/src/retype.rs index c014951008..30a640c3c7 100644 --- a/crates/text/src/retype.rs +++ b/crates/text/src/retype.rs @@ -49,6 +49,8 @@ pub struct GlyphMatch { pub origin: Point, /// The leftover distance, relative to the shape's spread. pub residual: f64, + /// How many contours the glyph has. + pub contours: usize, } /// A shape reduced for matching: points along each contour, centred. @@ -79,8 +81,11 @@ pub struct RetypeIndex { } impl RetypeIndex { - /// Index the outline glyphs of `faces` (the first [`MAX_FACES`], each face once). - pub fn build(db: &FontDb, faces: impl IntoIterator>) -> Self { + /// Index the outline glyphs of `faces` (the first [`MAX_FACES`], each face once) that have one + /// of the shape keys in `keys` (the selected shapes' [`shape_key`]s): the others could match no + /// shape, so they are not sampled. Outlines are read past the shared outline + /// cache, which keeps the canvas's glyphs. + pub fn build(db: &FontDb, faces: impl IntoIterator>, keys: &std::collections::BTreeSet>) -> Self { let mut kept: Vec> = vec![]; for f in faces { if kept.len() >= MAX_FACES { @@ -95,22 +100,16 @@ impl RetypeIndex { if entries.len() >= MAX_GLYPHS { break; } - // One entry per glyph, under the lowest character that maps to it. - let mut by_gid: HashMap = HashMap::new(); - for &(c, gid) in face.mapped_chars().iter().take(MAX_GLYPHS_PER_FACE) { - if c.is_whitespace() || c.is_control() || is_shape_symbol(c) { - continue; - } - by_gid.entry(gid).and_modify(|e| *e = (*e).min(c)).or_insert(c); - } - let mut gids: Vec<(u32, char)> = by_gid.into_iter().collect(); - gids.sort_unstable(); - for (gid, ch) in gids { - // The budget holds inside a face too: no glyph is loaded past it. + for (gid, ch, key) in face_keys(db, face).iter() { + // The budget holds inside a face too: no glyph is sampled past it. if entries.len() >= MAX_GLYPHS { break; } - let outline = db.outline(face, gid); + if !keys.contains(key) { + continue; + } + let (gid, ch) = (*gid, *ch); + let outline = db.outline_uncached(face, gid); if let Some(sig) = signature(&outline) { entries.push(Entry { face: fi, gid, ch, sig }); } @@ -165,7 +164,16 @@ impl RetypeIndex { // The glyph's origin is (0, 0) in font units: it lands at centre − z·glyph centre. let gc = mul(z, e.sig.centre); let origin = Point::new(sig.centre.x - gc.x, sig.centre.y - gc.y); - let m = GlyphMatch { face: face.clone(), gid: e.gid, ch: e.ch, scale: z.0.hypot(z.1), angle: z.1.atan2(z.0), origin, residual }; + let m = GlyphMatch { + face: face.clone(), + gid: e.gid, + ch: e.ch, + scale: z.0.hypot(z.1), + angle: z.1.atan2(z.0), + origin, + residual, + contours: e.sig.contours, + }; match best.get(&e.face) { Some(b) if b.residual < m.residual || (b.residual == m.residual && b.ch <= m.ch) => {} _ => { @@ -179,6 +187,41 @@ impl RetypeIndex { } } +/// A face's glyphs as the index looks them up: (glyph, character, [`shape_key`]), one per glyph +/// under the lowest character that maps to it. +type FaceKeys = Arc)>>; + +/// How many faces' keys [`face_keys`] keeps. +const KEPT_FACE_KEYS: usize = 32; + +/// [`FaceKeys`] of `face`, worked out once per face (each glyph drawn once, past the outline +/// cache) and kept: a later Retype samples only the glyphs whose key a selected shape has. +fn face_keys(db: &FontDb, face: &FontFace) -> FaceKeys { + type Kept = std::sync::Mutex>; + static KEPT: std::sync::OnceLock = std::sync::OnceLock::new(); + let kept = KEPT.get_or_init(Default::default); + let at = (db.id(), face.id()); + if let Some(k) = kept.lock().unwrap_or_else(|e| e.into_inner()).get(&at) { + return k.clone(); + } + let mut by_gid: HashMap = HashMap::new(); + for &(c, gid) in face.mapped_chars().iter().take(MAX_GLYPHS_PER_FACE) { + if c.is_whitespace() || c.is_control() || is_shape_symbol(c) { + continue; + } + by_gid.entry(gid).and_modify(|e| *e = (*e).min(c)).or_insert(c); + } + let mut gids: Vec<(u32, char)> = by_gid.into_iter().collect(); + gids.sort_unstable(); + let keys: FaceKeys = Arc::new(gids.into_iter().filter_map(|(gid, ch)| Some((gid, ch, shape_key(&db.outline_uncached(face, gid))?))).collect()); + let mut kept = kept.lock().unwrap_or_else(|e| e.into_inner()); + if kept.len() >= KEPT_FACE_KEYS { + kept.clear(); + } + kept.insert(at, keys.clone()); + keys +} + /// Characters whose glyphs are plain shapes (box drawing, blocks, geometric shapes, legacy /// computing symbols) or a font's own icons (private use): a drawn rectangle or circle is the same /// shape as one of them, so matching them would turn art into text. They are not matched. @@ -266,6 +309,21 @@ fn contours(path: &BezPath) -> Option>> { (out.len() <= MAX_CONTOURS).then_some(out) } +/// How many contours with length `path` has, as matching counts them; `None` past the size caps. +/// A cheap key two shapes share when one is the other scaled, turned and moved: how many segments +/// each of its contours has, in order (segments that go nowhere left out, a closing line counted +/// once whether the path draws it or closes onto it). Glyphs whose key no selected shape has are +/// not sampled. `None` past the size caps. +pub fn shape_key(path: &BezPath) -> Option> { + let moves = |s: &PathSeg| match s { + PathSeg::Line(l) => l.p0 != l.p1, + PathSeg::Quad(q) => q.p0 != q.p1 || q.p1 != q.p2, + PathSeg::Cubic(c) => c.p0 != c.p1 || c.p1 != c.p2 || c.p2 != c.p3, + }; + let key: Vec = contours(path)?.iter().map(|c| c.iter().filter(|s| moves(s)).count()).filter(|n| *n > 0).collect(); + (!key.is_empty()).then_some(key) +} + fn signature(path: &BezPath) -> Option { let bounds = path.bounding_box(); let size = bounds.width().hypot(bounds.height());