From b237da4672231767e8d8ca526e49e78d83d4ff77 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 00:24:20 -0800 Subject: [PATCH] Retype: a near glyph whose size and turn would draw it past the tolerance ends the line A near-matched glyph joins a line when its size and turn are each within the tolerance of the line's, and the line's type then draws it at the line's size and turn. Together with the glyph's own leftover distance, that change could put the drawn glyph further from its shape than the tolerance: an m 4% larger and turned 0.04 radians stayed on its line at tolerance 0.05. The layout check now also measures, for each near-matched glyph (and each glyph of a line whose first glyph is one), how far the change from its matched size and turn moves the drawn outline relative to its spread, combined with its own leftover distance, and splits the line before it when that passes the tolerance, or when the glyph's center sits further away than its origin may. --- crates/engine/src/cmd/retype.rs | 42 +++++++++++++++- crates/engine/src/cmd/retype_tests.rs | 69 +++++++++++++++++++++++++++ docs/mcp.md | 4 +- 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/retype.rs b/crates/engine/src/cmd/retype.rs index 69272c1928..26991f032c 100644 --- a/crates/engine/src/cmd/retype.rs +++ b/crates/engine/src/cmd/retype.rs @@ -48,7 +48,7 @@ pub fn specs() -> Vec { "Retype", ["Type"], None, - "{fonts?: [family…], tolerance?: 0..0.05 (default 0: exact matches only)} turn the selected outlined text (glyph paths and compound paths, inside groups too) back into live type: each shape is matched against the glyphs of the fonts the app has loaded, the document's fonts and the families in fonts, the same up to uniform scale, rotation and translation; with a tolerance above 0, a shape that is no glyph exactly may nearly be one (an outline simplified, a curve split in two, contours in another order, start or direction) when its leftover distance from the glyph, relative to the shape's outline spread (the root mean square distance of its sampled outline points from their center), is at most the tolerance (VectorCraft's own measure; shapes of at most 8 contours); a compound path holding several glyphs (a word outlined as one path, at most 256 contour groups and 512 contours, and 16,384 contours per command) is split into them, and becomes type only when every one of them matches; matched glyphs become point text in that font and size at the same baseline, one object per line unless lines stack into one (below), with the first glyph's fill, stroke, opacity and blend mode, in one undo step; a line of three or more exactly matched glyphs takes the spacing most of its gaps share as its tracking (pair kerning left out) when tracked type draws all of it in place, or more of it than untracked type (when untracked type also draws it, untracked type is kept if the tracking is under one unit or the tracked type would drop a gap one space wide (a b c keeps its spaces), and the tracked type otherwise), and negative tracking only when the line is opaque, Normal, with a solid fill or none and no stroke (its glyphs may overlap); lines that follow each other in paint order with the same group, paint, font, size, turn and tracking (an untracked line of one or two glyphs takes the others' tracking), each one the same leading (0.25 to 3 times the size) below the one before, and lined up at their starts, ends or centers become one area type object with that leading (Auto when Auto gives it) and left, right or center alignment, or one multi-line point type object when area type (after Text Wrap) would not draw the outlines' glyphs in their places or when checking the Text Wrap objects on the changed layers would copy more than 2,000,000 anchors, subpaths and shapes; where even that type would move a glyph, the lines before that glyph's line stay one object and the rest are joined again from that line; lines with an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke stay one object each; a line splits where its type would not draw the outlines' glyphs in their places (uneven spacing, manual kerning, font substitutions; with a tolerance, within it); a glyph must also paint the area the shape paints under its fill rule (a nonzero compound whose inner contour runs the same way as its outer one paints its center, so a ring glyph does not replace it; a counter narrower than three times the outline tolerance across in some direction, an outline too large to check, or parts of a split compound that overlap where its fill rule leaves the overlap unpainted, also keep the shape an outline); shapes that match no font, glyphs type cannot draw as they are, and shapes with their own opacity mask stay as they are → {ids (the new type), texts (lines joined by \\n), fonts: [{family, style, size, tracking (1/1000 em), leading (pt, null for Auto or one line), area (bool), score (how closely the line's least close glyph fits, 1 for exact, 0 at a leftover distance of 0.05)}], unmatched: [id…] (shapes that stayed as they are when another shape became type), truncated (shapes past the limits of 2048 shapes or 16 nested groups were left out)}; an error, with no unmatched list, when no shape becomes type, or when tolerance is not a number from 0 to 0.05", + "{fonts?: [family…], tolerance?: 0..0.05 (default 0: exact matches only)} turn the selected outlined text (glyph paths and compound paths, inside groups too) back into live type: each shape is matched against the glyphs of the fonts the app has loaded, the document's fonts and the families in fonts, the same up to uniform scale, rotation and translation; with a tolerance above 0, a shape that is no glyph exactly may nearly be one (an outline simplified, a curve split in two, contours in another order, start or direction) when its leftover distance from the glyph, relative to the shape's outline spread (the root mean square distance of its sampled outline points from their center), is at most the tolerance (VectorCraft's own measure; shapes of at most 8 contours); a compound path holding several glyphs (a word outlined as one path, at most 256 contour groups and 512 contours, and 16,384 contours per command) is split into them, and becomes type only when every one of them matches; matched glyphs become point text in that font and size at the same baseline, one object per line unless lines stack into one (below), with the first glyph's fill, stroke, opacity and blend mode, in one undo step; a line of three or more exactly matched glyphs takes the spacing most of its gaps share as its tracking (pair kerning left out) when tracked type draws all of it in place, or more of it than untracked type (when untracked type also draws it, untracked type is kept if the tracking is under one unit or the tracked type would drop a gap one space wide (a b c keeps its spaces), and the tracked type otherwise), and negative tracking only when the line is opaque, Normal, with a solid fill or none and no stroke (its glyphs may overlap); lines that follow each other in paint order with the same group, paint, font, size, turn and tracking (an untracked line of one or two glyphs takes the others' tracking), each one the same leading (0.25 to 3 times the size) below the one before, and lined up at their starts, ends or centers become one area type object with that leading (Auto when Auto gives it) and left, right or center alignment, or one multi-line point type object when area type (after Text Wrap) would not draw the outlines' glyphs in their places or when checking the Text Wrap objects on the changed layers would copy more than 2,000,000 anchors, subpaths and shapes; where even that type would move a glyph, the lines before that glyph's line stay one object and the rest are joined again from that line; lines with an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke stay one object each; a line splits where its type would not draw the outlines' glyphs in their places (uneven spacing, manual kerning, font substitutions; with a tolerance, within it), and before a near-matched glyph the line's size and turn would draw further from its shape than the tolerance; a glyph must also paint the area the shape paints under its fill rule (a nonzero compound whose inner contour runs the same way as its outer one paints its center, so a ring glyph does not replace it; a counter narrower than three times the outline tolerance across in some direction, an outline too large to check, or parts of a split compound that overlap where its fill rule leaves the overlap unpainted, also keep the shape an outline); shapes that match no font, glyphs type cannot draw as they are, and shapes with their own opacity mask stay as they are → {ids (the new type), texts (lines joined by \\n), fonts: [{family, style, size, tracking (1/1000 em), leading (pt, null for Auto or one line), area (bool), score (how closely the line's least close glyph fits, 1 for exact, 0 at a leftover distance of 0.05)}], unmatched: [id…] (shapes that stayed as they are when another shape became type), truncated (shapes past the limits of 2048 shapes or 16 nested groups were left out)}; an error, with no unmatched list, when no shape becomes type, or when tolerance is not a number from 0 to 0.05", has_selected_path, retype )] @@ -637,11 +637,51 @@ fn first_mismatch(t: &TextObject, matches: &[GlyphMatch], size: f64, slack: f64) if g.font_id != m.face.id() || g.gid != m.gid || at.distance(m.origin) > tol { return Some(k); } + // The type sets the glyph at its line's size and turn, which may differ from the glyph's + // own: the glyph drawn so must still lie within the tolerance of the shape (its center + // within the same distance as its origin). + if !(m.exact && first_exact) { + let (shape, moved) = set_apart(t, g, m); + // An exact match's own leftover is within the exact matcher's reach, not the tolerance. + let own = if m.exact { 0.0 } else { m.pose_residual }; + if own.hypot(shape) > slack + 1e-9 || moved > tol { + return Some(k); + } + } } // Ink the outlines did not have: the last glyph is where it went wrong. drawn.next().map(|_| matches.len().saturating_sub(1)) } +/// How glyph `g`, as type `t` draws it, differs from glyph `m` in the size, turn and place `m` +/// was matched in. The first value bounds, whatever points the outline is sampled at, the root +/// mean square distance between the two outlines' points about their center, relative to the +/// root mean square distance of `m`'s points from that center (the measure of near matches' +/// leftover distance): the largest stretch of the change from `m`'s size and turn to the drawn +/// one, which for a change of size and turn alone is that distance exactly. The second is how far +/// apart the two draw the center of the glyph's bounding box. Both are infinite when they cannot +/// be measured. +fn set_apart(t: &TextObject, g: &vectorcraft_text::PositionedGlyph, m: &GlyphMatch) -> (f64, f64) { + let (sin, cos) = m.angle.sin_cos(); + let matched = kurbo::Affine::new([m.scale * cos, m.scale * sin, -m.scale * sin, m.scale * cos, m.origin.x, m.origin.y]); + let (gd, md) = (g.xf.determinant(), matched.determinant()); + if !gd.is_finite() || gd.abs() < 1e-12 || !md.is_finite() || md.abs() < 1e-12 { + return (f64::INFINITY, f64::INFINITY); + } + let drawn = t.xf * g.xf; + // The change from the matched glyph to the drawn one, less the identity, without the move. + let [a, b, c, d, _, _] = (drawn * matched.inverse()).as_coeffs(); + let (a, d) = (a - 1.0, d - 1.0); + // The largest singular value of [[a, c], [b, d]]. + let (p, q) = ((a * a + b * b + c * c + d * d) / 2.0, a * d - b * c); + let shape = (p + (p * p - q * q).max(0.0).sqrt()).sqrt(); + // The center of the glyph's box, in font units. + let center = g.xf.inverse() * kurbo::Shape::bounding_box(&g.outline).center(); + let moved = (drawn * center).distance(matched * center); + let finite = |v: f64| if v.is_finite() { v } else { f64::INFINITY }; + (finite(shape), finite(moved)) +} + /// `lines` checked against their own layout: a line splits where its type stops drawing the /// outlines, and a glyph its type cannot draw even on its own stays an outline (`unmatched`). /// The glyphs laid out are bounded by a multiple of the number of glyphs. diff --git a/crates/engine/src/cmd/retype_tests.rs b/crates/engine/src/cmd/retype_tests.rs index 61cbe532bd..1d8097f54c 100644 --- a/crates/engine/src/cmd/retype_tests.rs +++ b/crates/engine/src/cmd/retype_tests.rs @@ -1063,6 +1063,75 @@ fn a_near_glyph_moved_by_more_than_the_tolerance_ends_the_line() { assert!(run(0.06) > 1, "past it: the line splits where the m was moved"); } +/// The bounding box of shape `id` (a path, or a compound path's paths), in document space. +fn shape_box(s: &Session, id: u64) -> kurbo::Rect { + let d = &s.doc().unwrap().doc; + let n = d.node(NodeId(id)).unwrap(); + let paths: Vec<&vectorcraft_doc::Node> = match n.children() { + Some(c) => c.iter().map(|c| &**c).collect(), + None => vec![n], + }; + paths + .iter() + .filter_map(|p| match &p.kind { + NodeKind::Path { path, .. } => Some(kurbo::Shape::bounding_box(&path.to_bezpath())), + _ => None, + }) + .reduce(|a, b| a.union(b)) + .unwrap() +} + +/// The bounding box of each glyph with ink that type `t` draws, in document space. +fn drawn_boxes(t: &TextObject) -> Vec { + let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t); + lay.glyphs.iter().filter(|g| !g.outline.elements().is_empty()).map(|g| kurbo::Shape::bounding_box(&(t.xf * g.outline.clone()))).collect() +} + +#[test] +fn a_near_glyph_whose_size_and_turn_differ_past_the_tolerance_ends_the_line() { + // The m of simplified outlines made 4% larger, and turned 0.04 radians as well, about where it + // starts on the baseline, retyped with a tolerance of 0.05. Each change alone is within the + // size and turn a near-matched line allows, but drawn at the line's size and turn the m would + // lie further than 0.05 of its spread from its outline: it becomes type of its own. Larger + // alone, it is still within the tolerance and stays on the line. + for (scale, turn, lines) in [(1.04, 0.0, 1), (1.04, 0.04, 2)] { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Tim", "size": 40})); + simplify_every_path(&mut s, 0.05); + let kids = children_of(&s, g); + let m = *kids.last().unwrap(); + let x0 = shape_box(&s, m).x0; + s.edit("test", |d, _| { + let at = kurbo::Vec2::new(x0, 100.0); + let xf = kurbo::Affine::translate(at) * kurbo::Affine::rotate(turn) * kurbo::Affine::scale(scale) * kurbo::Affine::translate(-at); + let data = d.node_mut(NodeId(m)).unwrap().path_data_mut().unwrap(); + *data = vectorcraft_geom::PathData::from_bezpath(&(xf * data.to_bezpath())); + Ok(()) + }) + .unwrap(); + let before: Vec = kids.iter().map(|k| shape_box(&s, *k)).collect(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.05})).unwrap(); + let texts: Vec<&str> = r["texts"].as_array().unwrap().iter().map(|t| t.as_str().unwrap()).collect(); + assert_eq!(texts.concat(), "Tim", "{r}"); + assert_eq!(texts.len(), lines, "scale {scale}, turn {turn}: {r}"); + if lines == 1 { + continue; + } + let drawn: Vec = r["ids"].as_array().unwrap().iter().flat_map(|id| drawn_boxes(&text_of(&s, id.as_u64().unwrap()))).collect(); + assert_eq!(drawn.len(), before.len(), "{r}"); + for b in &before { + // The glyph drawn nearest where the outline was. + let d = drawn.iter().min_by(|p, q| p.center().distance(b.center()).total_cmp(&q.center().distance(b.center()))).unwrap(); + let size = b.width().hypot(b.height()); + assert!( + (d.width() - b.width()).abs() < size * 0.01 && (d.height() - b.height()).abs() < size * 0.01, + "scale {scale}, turn {turn}: the outline's box {b:?}, the type's {d:?}: {r}" + ); + } + } +} + #[test] fn an_exact_glyph_moved_splits_the_line_whatever_the_tolerance() { let mut s = session(); diff --git a/docs/mcp.md b/docs/mcp.md index 543e4472c3..a75c2f8ff8 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -11322,7 +11322,9 @@ mean square distance of its sampled outline points from their center), is at mos tolerance: an outline simplified, a curve split in two, or contours in another order, start or direction. The tolerance and this measure are VectorCraft's own. A near-matched glyph may differ from its line by the tolerance in size, turn and place; an exact glyph keeps the exact limits unless the line's first glyph, whose size and turn the line takes, is -a near match. Each entry of `fonts` in the result has a `score`, from 1 when every glyph of the line is exact down to 0 +a near match. The line also splits before a near-matched glyph, or a glyph of a line whose first glyph is one, that +the type would draw further from its shape than the tolerance by the same measure (its own leftover distance together +with the change of size and turn) or with its center further away than its origin may be. Each entry of `fonts` in the result has a `score`, from 1 when every glyph of the line is exact down to 0 at a leftover distance of 0.05. A compound path that holds several glyphs, such as a word made into one compound path, is split into groups of contours (a contour with the contours inside it) in path order, and runs of up to three groups are matched, the longest first among those after which the