M7/G4: retype review follow-ups (#4934)

List type.retype among the commands plugins may call. Only closed shapes
match glyphs. Live type, images and other art between glyphs, and
unselected art between selected objects, end the line. Each selected
object's own parent decides its group. Retyped type turns ligatures off,
as the outlines drew each glyph on its own.
This commit is contained in:
ruchirguitar
2026-11-18 08:38:40 +05:30
parent 90b37ad269
commit 290a58fc18
3 changed files with 82 additions and 13 deletions
@@ -683,6 +683,7 @@ pub(super) const ALLOWED: &[&str] = &[
"type.pathOptions",
"type.resetHorizontalScale",
"type.resetTracking",
"type.retype",
"type.ruby",
"type.scaleTextAttributes",
"type.size.decrease",
+44 -13
View File
@@ -47,9 +47,12 @@ pub fn specs() -> Vec<CommandSpec> {
/// A shape to match: a path or compound path, in document space.
struct Piece {
id: NodeId,
/// The group holding it (`None` for a selected shape at the top): glyphs of different
/// groups, which may composite differently, never share a line.
/// The group or layer holding it: glyphs of different groups, which may composite
/// differently, never share a line.
parent: Option<NodeId>,
/// Other art (not retyped) lies between it and the shape before it, so the type after that
/// art must stay painted after it: the line ends here.
after_barrier: bool,
path: BezPath,
appearance: Appearance,
/// The shape's own transparency, which the new type takes.
@@ -57,28 +60,45 @@ struct Piece {
blend: vectorcraft_color::BlendMode,
}
fn collect(n: &Node, parent: Option<NodeId>, depth: usize, out: &mut Vec<Piece>) {
/// The shapes in `n` (held by `parent`), in paint order. `barrier` is set when art that is not a
/// shape lies before the next one; it starts set for a root that does not follow the last root.
fn collect(n: &Node, parent: Option<NodeId>, depth: usize, barrier: &mut bool, out: &mut Vec<Piece>) {
if out.len() >= MAX_PIECES || depth > MAX_DEPTH {
*barrier = true;
return;
}
let piece = |path: BezPath| Piece { id: n.id, parent, path, appearance: (*n.appearance).clone(), opacity: n.opacity, blend: n.blend };
let piece = |path: BezPath, after_barrier: bool| Piece {
id: n.id,
parent,
after_barrier,
path,
appearance: (*n.appearance).clone(),
opacity: n.opacity,
blend: n.blend,
};
match &n.kind {
NodeKind::Group { children, clip: false } => {
for c in children {
collect(c, Some(n.id), depth + 1, out);
collect(c, Some(n.id), depth + 1, barrier, out);
}
}
NodeKind::Path { path, clipping: false, guide: false, .. } => out.push(piece(path.to_bezpath())),
// Only closed shapes can be glyphs: an open path stroked along a glyph's outline is not one.
NodeKind::Path { path, clipping: false, guide: false, .. } if path.is_closed() => out.push(piece(path.to_bezpath(), std::mem::take(barrier))),
NodeKind::Compound { children, .. } => {
let mut path = BezPath::new();
for c in children {
if let NodeKind::Path { path: p, .. } = &c.kind {
path.extend(p.to_bezpath());
match &c.kind {
NodeKind::Path { path: p, .. } if p.is_closed() => path.extend(p.to_bezpath()),
_ => {
*barrier = true;
return;
}
}
}
out.push(piece(path));
out.push(piece(path, std::mem::take(barrier)));
}
_ => {}
// Live type, images, clip groups and the rest stay, and the type after them stays after them.
_ => *barrier = true,
}
}
@@ -194,10 +214,16 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
let (lines, unmatched) = {
let d = &s.doc()?.doc;
let mut pieces = vec![];
let mut last: Option<(Option<NodeId>, usize)> = None;
for r in &roots {
if let Some(n) = d.node(*r) {
collect(n, None, 0, &mut pieces);
}
let Some(n) = d.node(*r) else { continue };
let parent = d.parent_of(*r);
let idx = d.position(*r).map(|(_, i, _)| i);
// A root right after the last one, in the same parent, continues its lines; unselected
// art between two roots is a barrier.
let mut barrier = !matches!((last, idx), (Some((p, i)), Some(j)) if p == parent && i + 1 == j);
last = idx.map(|i| (parent, i));
collect(n, parent, 0, &mut barrier, &mut pieces);
}
if pieces.is_empty() {
return Err(EngineError::Other("Retype: select outlined text (paths or compound paths)".into()));
@@ -219,6 +245,9 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
// An unmatched shape between glyphs ends the line: the type after it is painted after it.
let mut open = false;
for (pc, ms) in pieces.iter().zip(&found) {
if pc.after_barrier {
open = false;
}
// Earlier matches win ties: they are the closer fits.
let best = ms.iter().enumerate().max_by_key(|(i, m)| (rank(m.face.id()), std::cmp::Reverse(*i))).map(|(_, m)| m);
let Some(m) = best else {
@@ -249,6 +278,8 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
font_style: l.face.style.clone(),
// The version that matched, when two of the family and style are loaded.
font_version: (!l.face.version.is_empty()).then(|| l.face.version.clone()),
// Each outlined glyph was drawn on its own; a ligature would draw another shape.
features: vec!["-liga".into()],
size: l.scale * l.face.units_per_em(),
fill: l.appearance.fill_paint(),
stroke: l.appearance.stroke_paint(),
+37
View File
@@ -166,3 +166,40 @@ fn glyphs_of_different_groups_and_transparency_stay_apart() {
assert_eq!(children_of(&s, b), vec![ids[1], ids[2]]);
assert_eq!(s.doc().unwrap().doc.node(NodeId(ids[2])).unwrap().opacity, 0.25, "the glyph's own opacity carries over");
}
#[test]
fn live_type_between_glyphs_ends_the_line_too() {
let mut s = session();
let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "AB", "size": 40}));
let live = s.execute("text.create", &json!({"x": 30, "y": 90, "text": "x", "size": 60})).unwrap()["id"].as_u64().unwrap();
s.edit("test", |d, _| {
let n = d.node(NodeId(live)).unwrap().clone();
d.remove(NodeId(live))?;
d.insert(Some(NodeId(g)), 1, n)?;
Ok(())
})
.unwrap();
s.execute("select.set", &json!({"ids": [g]})).unwrap();
let r = s.execute("type.retype", &json!({})).unwrap();
assert_eq!(r["texts"], json!(["A", "B"]), "{r}");
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect();
assert_eq!(children_of(&s, g), vec![ids[0], live, ids[1]], "B stays over the live type");
let t = text_of(&s, ids[0]);
assert!(t.first_style().features.iter().any(|f| f == "-liga"), "glyphs drawn one by one: no ligatures");
}
#[test]
fn an_open_path_along_a_glyph_is_not_a_glyph() {
let mut s = session();
let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "I", "size": 40}));
s.edit("test", |d, _| {
let glyph = d.node(NodeId(g)).unwrap().children().unwrap()[0].id;
for sp in &mut d.node_mut(glyph).unwrap().path_data_mut().unwrap().subpaths {
sp.closed = false;
}
Ok(())
})
.unwrap();
s.execute("select.set", &json!({"ids": [g]})).unwrap();
assert!(s.execute("type.retype", &json!({})).is_err(), "nothing matches");
}