From f9548440bd1ae09c3a153aab4e73c144c2f5041b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 01:40:36 -0800 Subject: [PATCH] Retype: a translucent compound path whose glyphs overlap stays an outline The glyphs of a split compound path can become separate type objects, for example on two lines. A compound filled by the nonzero rule whose glyphs overlap passed the paint check, and with an opacity below 100% the two objects then painted the overlap twice. A split whose parts overlap now keeps the shape an outline when the shape has an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke. --- crates/engine/src/cmd/retype.rs | 32 ++++++++++++++------ crates/engine/src/cmd/retype_tests.rs | 43 +++++++++++++++++++++++++++ docs/mcp.md | 4 ++- 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/retype.rs b/crates/engine/src/cmd/retype.rs index f4efd65cb3..b464696542 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, 512 contours and 65,536 path elements, 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", + "{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, 512 contours and 65,536 path elements, 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, or that overlap at all when the compound has an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke, 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 )] @@ -596,10 +596,16 @@ impl Line { /// Normal blend, a solid fill or none, and no stroke? One object composites its transparency and /// blend once, spans a gradient over all of it, and strokes after filling every glyph. fn paints_as_one(l: &Line) -> bool { - l.opacity == 1.0 - && l.blend == vectorcraft_color::BlendMode::Normal - && matches!(l.appearance.fill_paint(), vectorcraft_color::Paint::None | vectorcraft_color::Paint::Solid { .. }) - && matches!(l.appearance.stroke_paint(), vectorcraft_color::Paint::None) + paints_once(l.opacity, l.blend, &l.appearance) +} + +/// Does art with `opacity`, `blend` and `appearance` paint its overlaps the same as one object +/// and as several ([`paints_as_one`])? +fn paints_once(opacity: f32, blend: vectorcraft_color::BlendMode, appearance: &Appearance) -> bool { + opacity == 1.0 + && blend == vectorcraft_color::BlendMode::Normal + && matches!(appearance.fill_paint(), vectorcraft_color::Paint::None | vectorcraft_color::Paint::Solid { .. }) + && matches!(appearance.stroke_paint(), vectorcraft_color::Paint::None) } /// The tracking Retype sets, in 1/1000 em. @@ -1554,11 +1560,19 @@ fn retype(s: &mut Session, p: &Value) -> Result { let fill = pc.filled.then_some(pc.nonzero); // Parts that all match exactly are checked at tolerance 0, in the near phase as in // the exact one; only a part that matches nearly brings the tolerance in. + // The glyphs of a split may become separate objects: when the shape does not paint + // its overlaps as several objects would (translucent, another blend, a gradient or + // pattern fill, a stroke), its parts must not overlap, which the parts read by the + // even-odd rule against them all joined show. + let once = paints_once(pc.opacity, pc.blend, &pc.appearance); let together = |runs: &[BezPath], near: bool| { - fill.is_none_or(|nz| { - let nearly = near && runs.iter().any(|r| find(r, true, false, fill).is_empty()); - vectorcraft_text::retype::parts_paint_alike(&pc.path, runs, nz, if nearly { tolerance } else { 0.0 }) - }) + if fill.is_none() && once { + return true; + } + let nearly = near && runs.iter().any(|r| find(r, true, false, fill).is_empty()); + let tol = if nearly { tolerance } else { 0.0 }; + fill.is_none_or(|nz| vectorcraft_text::retype::parts_paint_alike(&pc.path, runs, nz, tol)) + && (once || vectorcraft_text::retype::parts_paint_alike(&joined(runs), runs, false, tol)) }; parts(&pc.path, g, &|p, part, near| find(p, part, near, fill), &together) }) diff --git a/crates/engine/src/cmd/retype_tests.rs b/crates/engine/src/cmd/retype_tests.rs index 4ee5388692..f939dcf410 100644 --- a/crates/engine/src/cmd/retype_tests.rs +++ b/crates/engine/src/cmd/retype_tests.rs @@ -1289,6 +1289,49 @@ fn glyphs_that_overlap_in_an_even_odd_compound_stay_an_outline() { assert!(s.doc().unwrap().doc == before, "nothing changes"); } +#[test] +fn a_translucent_compound_whose_glyphs_overlap_stays_an_outline() { + // Two i's, the second moved half onto the first and down off its baseline, made into one + // compound path filled by the nonzero rule with every contour running one way: the parts paint + // the shape's area, but on two lines they become two objects, and at 50% opacity their + // overlap would be painted twice. + for opacity in [1.0f32, 0.5] { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "ii", "size": 40})); + let kids = children_of(&s, g); + let dx = shape_box(&s, kids[0]).x0 - shape_box(&s, kids[1]).x0 + 2.0; + s.execute("object.move", &json!({"ids": [kids[1]], "dx": dx, "dy": 6})).unwrap(); + s.execute("select.set", &json!({"ids": kids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + let paths = children_of(&s, c); + s.edit("test", |d, _| { + // Make Compound Path turns the second glyph's contours the other way round. + for p in &paths { + let data = d.node_mut(NodeId(*p)).unwrap().path_data_mut().unwrap(); + let b = data.to_bezpath(); + if kurbo::Shape::area(&b) < 0.0 { + *data = vectorcraft_geom::PathData::from_bezpath(&b.reverse_subpaths()); + } + } + let n = d.node_mut(NodeId(c)).unwrap(); + n.opacity = opacity; + if let NodeKind::Compound { rule, .. } = &mut n.kind { + *rule = vectorcraft_geom::FillRule::NonZero; + } + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let r = s.execute("type.retype", &json!({})); + if opacity == 1.0 { + let r = r.unwrap(); + assert_eq!(r["texts"], json!(["i", "i"]), "opaque, the two lines become type: {r}"); + } else { + assert!(r.is_err(), "translucent, the compound stays an outline: {r:?}"); + } + } +} + #[test] fn overlapping_exact_glyphs_stay_an_outline_when_near_matches_are_allowed() { // Exact parts the even-odd rule rejects stay rejected when the near phase finds them again. diff --git a/docs/mcp.md b/docs/mcp.md index bb74d45a8f..dcaccb2992 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -11338,7 +11338,9 @@ whose inner contour runs the same way as its outer one paints its center, so a r outline tolerance across in some direction, turned or not, cannot be checked this way and stays an outline: at `tolerance` 0.05, a counter 10 units wide in a 100 by 100 shape, or a 60 by 10 one turned 45 degrees. So does a shape whose outline is too large to check. The glyphs of a compound path that splits into several are set one beside another, so a split whose parts overlap where the shape's fill rule leaves the overlap unpainted (two o's on each -other under the even-odd rule) stays an outline. When the split with the longest runs fails that check, the split with the +other under the even-odd rule) stays an outline. A split whose parts overlap at all also stays an outline when the +compound has an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke, since the +glyphs may become separate objects that paint the overlap twice. When the split with the longest runs fails that check, the split with the shortest runs is checked, then the other complete splits in order, up to 32 splits per shape for exact matches and 32 for near matches; when none passes, the shape stays an outline. Glyphs fused into one contour are not split, and a compound path of more than 256 groups, 512 contours or 65,536 path elements is matched whole only. One command groups at most 16,384 contours, in selection order; the shapes after them are