M7/G4: retype checks points along every edge; keys charged once (#4934)

The whole-outline check tests points along every edge, so outlines with
the same corners joined another way do not match; past a work limit it
rejects. Face keys another Retype kept meanwhile are reused and charged
once. The fonts parameter test uses outlines that would match. The parity
note states the search limits.
This commit is contained in:
ruchirguitar
2026-11-18 15:25:41 +05:30
parent 06e8ce62b9
commit c1613f2bf5
3 changed files with 53 additions and 4 deletions
+9 -1
View File
@@ -80,7 +80,15 @@ fn a_drawn_shape_is_reported_and_left_alone() {
let e = s.execute("type.retype", &json!({})).unwrap_err().to_string();
assert!(e.contains("no selected shape matches"), "{e}");
assert!(s.doc().unwrap().doc == before, "nothing changes");
assert!(s.execute("type.retype", &json!({"fonts": "Source Sans 3"})).is_err(), "fonts is a list");
// With outlines that would match, a bad `fonts` is refused for itself, and nothing changes.
let g = outlined(&mut s, json!({"x": 40, "y": 200, "text": "AB", "size": 40}));
s.execute("select.set", &json!({"ids": [g]})).unwrap();
let before = s.doc().unwrap().doc.clone();
for bad in [json!("Source Sans 3"), json!([1]), json!({"family": "x"})] {
let e = s.execute("type.retype", &json!({"fonts": bad})).unwrap_err().to_string();
assert!(e.contains("fonts must be a list of family names"), "{e}");
}
assert!(s.doc().unwrap().doc == before);
}
#[test]
+43 -2
View File
@@ -40,6 +40,8 @@ const TOLERANCE: f64 = 2e-3;
const OUTLINE_TOLERANCE: f64 = 5e-3;
/// Points of a flattened outline past which the whole-outline check gives up (and so rejects).
const MAX_OUTLINE_POINTS: usize = 8192;
/// Points along an outline past which the whole-outline check gives up (and so rejects).
const MAX_CHECKED_POINTS: usize = 65_536;
/// How far apart two shapes' spread-to-length ratios may be and still be compared.
const INVARIANT_SLACK: f64 = 0.02;
@@ -250,7 +252,14 @@ fn face_keys(db: &FontDb, face: &FontFace) -> FaceKeys {
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 k = kept.lock().unwrap_or_else(|e| e.into_inner());
if k.generation == generation && k.glyphs + keys.len() <= KEPT_KEYS {
if k.generation != generation {
return keys;
}
// Another Retype may have worked them out meanwhile: keep and charge one list.
if let Some(already) = k.faces.get(&at) {
return already.clone();
}
if k.glyphs + keys.len() <= KEPT_KEYS {
k.glyphs += keys.len();
k.faces.insert(at, keys.clone());
}
@@ -331,7 +340,22 @@ fn outlines_agree(a: &BezPath, b: &BezPath, tol: f64) -> bool {
};
let (Some(sa), Some(sb)) = (flat(a), flat(b)) else { return false };
let near = |p: Point, segs: &[(Point, Point)]| segs.iter().any(|(s, e)| Line::new(*s, *e).nearest(p, 1e-9).distance_sq <= tol * tol);
sa.iter().all(|(p, _)| near(*p, &sb)) && sb.iter().all(|(p, _)| near(*p, &sa))
// Points along every edge, not only its ends: two outlines with the same corners joined in
// another order differ between them. Spaced `tol` apart; past the work limit, not a match.
let along = |segs: &[(Point, Point)]| -> Option<Vec<Point>> {
let mut pts = vec![];
for (s, e) in segs {
let steps = (s.distance(*e) / tol).ceil();
if !steps.is_finite() || pts.len() as f64 + steps > MAX_CHECKED_POINTS as f64 {
return None;
}
let steps = (steps as usize).max(1);
pts.extend((0..steps).map(|i| s.lerp(*e, i as f64 / steps as f64)));
}
Some(pts)
};
let (Some(pa), Some(pb)) = (along(&sa), along(&sb)) else { return false };
pa.iter().all(|p| near(*p, &sb)) && pb.iter().all(|p| near(*p, &sa))
}
/// `seg` as a line when it is one (a curve with its handles on its ends or on its line), else as it is.
@@ -728,4 +752,21 @@ mod tests {
assert!(!outlines_agree(&out, &inn, tol), "the whole outline does");
assert!(outlines_agree(&out, &out, tol));
}
#[test]
fn the_same_corners_joined_another_way_are_another_outline() {
let poly = |pts: &[(f64, f64)]| {
let mut p = BezPath::new();
p.move_to(pts[0]);
for q in &pts[1..] {
p.line_to(*q);
}
p.close_path();
p
};
let square = poly(&[(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)]);
let bow = poly(&[(0.0, 0.0), (100.0, 100.0), (100.0, 0.0), (0.0, 100.0)]);
assert!(!outlines_agree(&square, &bow, 1.0), "every corner lies on the other, the edges do not");
assert!(outlines_agree(&square, &square, 1.0));
}
}
+1 -1
View File
@@ -2861,7 +2861,7 @@ Check Spelling followed (2026-11-15): Edit › Spelling › Check Spelling… (`
## Retype, phase 1 (2026-11-18)
The public pages describe Retype finding the fonts that match selected outlined text (or text in an image), from the fonts on the device among others, and applying them to live text ([identify fonts](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/identify-fonts-using-retype.html), [apply to live text](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/apply-retype-suggested-fonts-to-live-text.html)). Phase 1 (#4934) is local and exact: Type › Retype (`type.retype`) compares each selected glyph shape with the glyphs of the loaded fonts, the document's fonts and any families named in `fonts`, the same up to uniform scale, rotation and translation, and sets the matched characters as point text in that font and size at the same baseline, one object per line, in one undo step. Shapes that match no font stay and are reported. Tested by round trips through Create Outlines (plain, bold and turned, serif, word spaces), a drawn shape and a mixed selection (`crates/engine/src/cmd/retype_tests.rs`). Near matches, the ranked font list, installed fonts not yet loaded and images remain (phase 2).
The public pages describe Retype finding the fonts that match selected outlined text (or text in an image), from the fonts on the device among others, and applying them to live text ([identify fonts](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/identify-fonts-using-retype.html), [apply to live text](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/apply-retype-suggested-fonts-to-live-text.html)). Phase 1 (#4934) is local and exact: Type › Retype (`type.retype`) compares each selected glyph shape with the glyphs of the loaded fonts, the document's fonts and any families named in `fonts`, the same up to uniform scale, rotation and translation, and sets the matched characters as point text in that font and size at the same baseline, one object per line, in one undo step. Shapes that match no font stay and are reported. The search is bounded, not exhaustive: at most 96 faces (named families first, then the document's fonts, then the other loaded faces), the first 65,536 encoded characters of each, and glyphs of plain-shape characters (box drawing, blocks, geometric shapes, private use) are left out; a glyph outside those limits is reported as matching no font. Exact matching also reads a curve an exporter split in two as a different shape (phase 2, with the other near matches). Tested by round trips through Create Outlines (plain, bold and turned, serif, word spaces), a drawn shape and a mixed selection (`crates/engine/src/cmd/retype_tests.rs`). Near matches, the ranked font list, installed fonts not yet loaded and images remain (phase 2).
## Control bar: Paragraph section while type is selected (2026-11-06)