diff --git a/crates/engine/src/cmd/retype.rs b/crates/engine/src/cmd/retype.rs index 174d77c4a9..b0d37a1d61 100644 --- a/crates/engine/src/cmd/retype.rs +++ b/crates/engine/src/cmd/retype.rs @@ -13,8 +13,14 @@ //! per line of glyphs, with the first glyph's fill and stroke. A line whose glyphs are evenly spaced //! apart takes that spacing as its tracking ([`Line::tracked`]), and lines stacked at one leading //! and lined up at their starts, ends or centers become one area type object ([`blocks`]). Shapes -//! that match no font stay as they are and are reported. Near matches (another version of the -//! font, simplified outlines, merged glyphs) and images are phase 2. +//! that match no font stay as they are and are reported. +//! +//! With a `tolerance`, a shape that matches no glyph exactly may still nearly match one: another +//! version of the font, or the outline simplified or built another way +//! ([`RetypeIndex::near_matches`]); each line reports how closely its glyphs fit. A compound path +//! holding several glyphs (a word outlined as one path) is split into its glyphs +//! ([`vectorcraft_text::retype::clusters`]), and becomes type only when every one of them does. +//! The tolerance, the score and the splitting rules are VectorCraft's own. Images are phase 2. use std::f64::consts::{PI, TAU}; use std::sync::Arc; @@ -22,6 +28,7 @@ use std::sync::Arc; use kurbo::{BezPath, Point, Vec2}; use serde_json::{Value, json}; use vectorcraft_doc::{Appearance, CharStyle, Document, Node, NodeId, NodeKind, TextObject}; +use vectorcraft_geom::FillRule; use vectorcraft_text::FontDb; use vectorcraft_text::retype::{GlyphMatch, RetypeIndex}; @@ -41,7 +48,7 @@ pub fn specs() -> Vec { "Retype", ["Type"], None, - "{fonts?: [family…]} 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 (exact matches only); 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 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); 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)}], unmatched: [id…], truncated (shapes past the limits of 2048 shapes or 16 nested groups were left out)}; an error when no shape matches", + "{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 )] @@ -60,6 +67,10 @@ struct Piece { /// art must stay painted after it: the line ends here. after_barrier: bool, path: BezPath, + /// The shape is filled by the nonzero rule, not the even-odd rule. + nonzero: bool, + /// The shape has a visible fill: a shape that only has a stroke paints no area to compare. + filled: bool, appearance: Appearance, /// The shape's own transparency, which the new type takes. opacity: f32, @@ -83,11 +94,13 @@ fn collect(n: &Node, parent: Option, depth: usize, barrier: &mut bool, t *barrier = true; return; } - let piece = |path: BezPath, after_barrier: bool| Piece { + let piece = |path: BezPath, after_barrier: bool, rule: FillRule| Piece { id: n.id, parent, after_barrier, path, + nonzero: rule == FillRule::NonZero, + filled: n.appearance.fill().is_some_and(|f| f.visible && !f.paint.is_none()), appearance: (*n.appearance).clone(), opacity: n.opacity, blend: n.blend, @@ -100,8 +113,10 @@ fn collect(n: &Node, parent: Option, depth: usize, barrier: &mut bool, t } } // 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, .. } => { + NodeKind::Path { path, rule, clipping: false, guide: false, .. } if path.is_closed() => { + out.push(piece(path.to_bezpath(), std::mem::take(barrier), *rule)) + } + NodeKind::Compound { children, rule } => { let mut path = BezPath::new(); for c in children { match &c.kind { @@ -113,7 +128,7 @@ fn collect(n: &Node, parent: Option, depth: usize, barrier: &mut bool, t } } } - out.push(piece(path, std::mem::take(barrier))); + out.push(piece(path, std::mem::take(barrier), *rule)); } // Live type, images, clip groups and the rest stay, and the type after them stays after them. _ => *barrier = true, @@ -185,6 +200,9 @@ struct Line { parent: Option, opacity: f32, blend: vectorcraft_color::BlendMode, + /// The tolerance near matches were taken with (0: exact only): a near-matched glyph may differ + /// that much in size, turn and place from the rest of its line ([`Self::slack_for`]). + slack: f64, } impl Line { @@ -192,6 +210,12 @@ impl Line { Vec2::new(self.angle.cos(), self.angle.sin()) } + /// The slack glyph `m` of this line is allowed: [`Self::slack`] when `m` or the line's first + /// glyph, whose size and turn the line takes, is a near match, else 0. + fn slack_for(&self, m: &GlyphMatch) -> f64 { + if m.exact && self.matches.first().is_none_or(|f| f.exact) { 0.0 } else { self.slack } + } + /// Does glyph `m` of shape `pc` continue this line (same group, paint and transparency, same /// font, size and turn, on its baseline, after its end)? fn continues(&self, pc: &Piece, m: &GlyphMatch) -> bool { @@ -199,15 +223,16 @@ impl Line { let d = m.origin - self.origin; let dir = self.dir(); let across = d.cross(dir).abs(); + let slack = self.slack_for(m); pc.parent == self.parent && pc.appearance == self.appearance && pc.opacity == self.opacity && pc.blend == self.blend && 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.scale - self.scale).abs() <= self.scale * slack.max(1e-4) // Wrapped: type turned 180° has angles near +π and −π. - && ((m.angle - self.angle + PI).rem_euclid(TAU) - PI).abs() < 1e-5 + && ((m.angle - self.angle + PI).rem_euclid(TAU) - PI).abs() < slack.max(1e-5) && across < size * 0.05 // After the line's end, or, for type tracked tighter than that, still ahead of the // last glyph's start (by more than rounding); the layout check decides whether it fits. @@ -319,6 +344,7 @@ impl Line { parent: self.parent, opacity: self.opacity, blend: self.blend, + slack: self.slack, }; for (id, m) in ids.iter().zip(ms).skip(1) { l.push(*id, m); @@ -370,7 +396,7 @@ impl Line { /// (a substitution the font makes) or the same glyph elsewhere (tracking, manual spacing, /// more space than whole spaces make); `None` when the type draws every glyph in its place. fn first_mismatch(&self, t: &TextObject) -> Option { - first_mismatch(t, &self.matches, self.size()) + first_mismatch(t, &self.matches, self.size(), self.slack) } /// Along the line, where its first glyph starts and its last glyph's advance ends, from @@ -395,14 +421,16 @@ impl Line { /// untracked type does. When untracked type with the spaces also draws the whole line, the line stays /// untracked if the tracking is under one unit or the tracked reading drops a gap untracked /// type reads as one space. The line also stays as it is when its tracking is 0 or outside - /// -1000..10000, and when its tracking is negative and the line does not paint the same as one + /// -1000..10000, when one of its glyphs is a near match, and when its tracking is negative and the line does not paint the same as one /// object ([`paints_as_one`]: its glyphs may overlap). Each of the nine layouts at most takes /// its characters, spaces included, from `budget` before it runs; without enough left, the /// line stays as it is, or becomes the best tracked reading laid out before the budget ran out. fn tracked(self, system_layout: bool, budget: &mut usize) -> Line { let size = self.size(); let n = self.pieces.len(); - if n < 3 || size <= 0.0 { + // A near-matched glyph sits within the tolerance of where the font draws it, so the gaps + // of a line with one say nothing about spacing. + if n < 3 || size <= 0.0 || self.matches.iter().any(|m| !m.exact) { return self; } // Each layout takes its characters, spaces included, from `budget` before it runs. @@ -568,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. @@ -596,21 +630,64 @@ fn round_tracking(t: f64) -> f64 { /// The first of `matches` that type `t` (at `size`) does not draw as the outlines did: another /// glyph (a substitution the font makes) or the same glyph elsewhere (tracking, manual spacing, /// more space than whole spaces make); `None` when the type draws every glyph in its place. -fn first_mismatch(t: &TextObject, matches: &[GlyphMatch], size: f64) -> Option { +fn first_mismatch(t: &TextObject, matches: &[GlyphMatch], size: f64, slack: f64) -> Option { let lay = vectorcraft_text::layout(FontDb::global(), t); let mut drawn = lay.glyphs.iter().filter(|g| !g.outline.elements().is_empty()); - let tol = (size * 0.01).max(0.01); + // With a tolerance, a near-matched glyph, or any glyph of a line whose first glyph is one, + // may sit that much further (as a fraction of the size) from where the type draws it. + let first_exact = matches.first().is_none_or(|f| f.exact); for (k, m) in matches.iter().enumerate() { let Some(g) = drawn.next() else { return Some(k) }; + let tol = (size * (0.01 + if m.exact && first_exact { 0.0 } else { slack })).max(0.01); let at = t.xf * (g.xf * Point::ORIGIN); 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. @@ -739,6 +816,8 @@ struct Made { text: String, face: Arc, size: f64, + /// The tolerance near matches were taken with ([`Line::slack`]). + slack: f64, tracking: f64, /// The leading set on the type: None for Auto or a single line. leading: Option, @@ -756,6 +835,7 @@ impl Made { text: l.text.clone(), face: l.face.clone(), size: l.size(), + slack: l.slack, tracking: l.tracking, leading: None, opacity: l.opacity, @@ -888,7 +968,7 @@ impl Block { t.xf = kurbo::Affine::translate(m.origin - at) * t.xf; } super::typecmd::refresh_bounds(&mut t); - if let Some(k) = first_mismatch(&t, &matches, size) { + if let Some(k) = first_mismatch(&t, &matches, size, first.slack) { // The line that holds glyph `k`. let mut seen = 0usize; let at = self.lines.iter().position(|l| { @@ -917,7 +997,7 @@ impl Block { let mut area = t.clone(); super::typemenu::point_to_area(&mut area); let mut point = None; - if in_range(&area) && first_mismatch(&area, &matches, size).is_none() { + if in_range(&area) && first_mismatch(&area, &matches, size, first.slack).is_none() { point = Some(std::mem::replace(&mut t, area)); } vec![Made { @@ -928,6 +1008,7 @@ impl Block { text: all.text, face: first.face.clone(), size, + slack: first.slack, tracking: all.tracking, leading: set_leading, opacity: first.opacity, @@ -964,7 +1045,7 @@ fn share_tracking(lines: &mut [Line], system_layout: bool) { /// `lines`, in paint order, as the objects Retype makes: runs of lines that stack as the lines of /// one paragraph do become one object ([`Block`]). `follows(a, b)`: shape `b` comes right after -/// shape `a`, with nothing between them in paint order. `budget`: see [`Block::made`]. +/// shape `a`, or is `a`, with nothing between them in paint order. `budget`: see [`Block::made`]. fn blocks(lines: Vec, follows: impl Fn(NodeId, NodeId) -> bool, system_layout: bool, budget: &mut usize) -> Vec { let mut out = vec![]; let mut block: Option = None; @@ -1054,11 +1135,13 @@ fn reposed(l: &Line, id: NodeId, m: &GlyphMatch) -> Line { parent: l.parent, opacity: l.opacity, blend: l.blend, + slack: l.slack, } } -/// A new line of shape `pc` alone, as the first of its candidate glyphs `cands` (none: `None`). -fn new_line(pc: &Piece, cands: &[GlyphMatch]) -> Option { +/// A new line of shape `pc` alone, as the first of its candidate glyphs `cands` (none: `None`), +/// with near matches taken at tolerance `slack`. +fn new_line(pc: &Piece, cands: &[GlyphMatch], slack: f64) -> Option { let m = cands.first()?; let dir = Vec2::new(m.angle.cos(), m.angle.sin()); Some(Line { @@ -1076,18 +1159,344 @@ fn new_line(pc: &Piece, cands: &[GlyphMatch]) -> Option { parent: pc.parent, opacity: pc.opacity, blend: pc.blend, + slack, }) } +/// The lines, by index, to drop so that no shape of several glyphs becomes type in part: each line +/// is given by the shapes it sets, one per glyph, and `glyphs_of` says how many glyphs a shape +/// is. A line setting a glyph of a shape not all of whose glyphs are set goes, and then so do the +/// lines setting the other glyphs of the shapes it held. +fn partial_lines(lines: &[Vec], glyphs_of: &std::collections::HashMap) -> std::collections::HashSet { + let mut dropped = std::collections::HashSet::new(); + loop { + let mut set: std::collections::HashMap = std::collections::HashMap::new(); + for (_, l) in lines.iter().enumerate().filter(|(i, _)| !dropped.contains(i)) { + for id in l { + *set.entry(*id).or_default() += 1; + } + } + let partial: std::collections::HashSet = + set.iter().filter(|(id, n)| glyphs_of.get(*id).is_some_and(|want| *n < want)).map(|(id, _)| *id).collect(); + let before = dropped.len(); + for (i, l) in lines.iter().enumerate() { + if l.iter().any(|id| partial.contains(id)) { + dropped.insert(i); + } + } + if dropped.len() == before { + return dropped; + } + } +} + +/// Glyph runs longer than this (in contour groups) are not tried when a shape is split. +const MAX_RUN: usize = 3; + +/// Complete splits of one shape that one [`split_parts`] search (exact or near) checks the paint +/// of, at most. When none of them is accepted, that search leaves the shape whole. +const MAX_SPLIT_TRIES: usize = 32; + +/// A shape of more contour groups than this is not split: it is matched whole only. +const MAX_SPLIT_GROUPS: usize = 256; + +/// A shape of more contours than this is not split either, and its contours are not grouped. +const MAX_SPLIT_CONTOURS: usize = 512; + +/// Contours of all the selected shapes one command groups, at most: the shapes past them are +/// matched whole only. +const MAX_SPLIT_TOTAL_CONTOURS: usize = 16_384; + +/// A shape of more path elements than this is not split, and its contours are not grouped: 16 +/// times the most a glyph may have ([`vectorcraft_text::retype::MAX_ELEMENTS`]). +const MAX_SPLIT_ELEMENTS: usize = 16 * vectorcraft_text::retype::MAX_ELEMENTS; + +/// `path`'s contour groups when it is split into glyphs: none for fewer than two or more than +/// [`MAX_SPLIT_GROUPS`], for more than [`MAX_SPLIT_CONTOURS`] contours or [`MAX_SPLIT_ELEMENTS`] +/// path elements, and for more contours than `budget` holds. A path of two or more contours takes +/// them from `budget` when it is grouped. +fn split_groups(path: &BezPath, budget: &mut usize) -> Vec { + // Grouping copies the contours and compares every contour with every other: count first. + let n = path.elements().iter().filter(|e| matches!(e, kurbo::PathEl::MoveTo(_))).count(); + if !(2..=MAX_SPLIT_CONTOURS).contains(&n) || n > *budget || path.elements().len() > MAX_SPLIT_ELEMENTS { + return vec![]; + } + *budget -= n; + let groups = vectorcraft_text::retype::clusters(path); + if (2..=MAX_SPLIT_GROUPS).contains(&groups.len()) { groups } else { vec![] } +} + +/// The parts of shape `path` to match: the whole shape, or, when it is no glyph and holds several +/// contour `groups` ([`split_groups`]), runs of up to [`MAX_RUN`] groups in order, the longest that matches first (an i before a dotless i and a dot) among the runs after +/// which the rest of the groups can still be matched. Each part with its candidates; empty when +/// the shape matches nothing and its groups cannot all be matched. `find(path, part, near)` gives +/// a path's candidates: its `part` flag is set for a part of a split shape, whose contours Make +/// Compound Path may have turned the other way round, and its `near` flag allows near matches. +/// Exact matches come first: the whole shape, then its groups, then the whole shape nearly, then +/// its groups nearly. A split of several parts is kept only when `together(runs, near)` accepts the +/// paths of its runs (the parts, set one beside another, paint what the shape paints); `near` is +/// set when the parts were found by near matching. +fn parts( + path: &BezPath, + groups: &[BezPath], + find: &dyn Fn(&BezPath, bool, bool) -> Vec, + together: &dyn Fn(&[BezPath], bool) -> bool, +) -> Vec> { + let whole = find(path, false, false); + if !whole.is_empty() { + return vec![whole]; + } + let exact = split_parts(groups, &|run| find(run, true, false), &|runs| together(runs, false)); + if !exact.is_empty() { + return exact; + } + let whole = find(path, false, true); + if !whole.is_empty() { + return vec![whole]; + } + split_parts(groups, &|run| find(run, true, true), &|runs| together(runs, true)) +} + +/// [`parts`] of a shape split into contour `groups`, each run's candidates from `find`; empty for +/// no groups, when the groups cannot all be matched and when `together` rejects every split of +/// several parts it is asked about. The complete splits are tried with the longest runs first, +/// then with the shortest runs, then the others in order, a longer run at an earlier group first. +/// `together` is asked about at most [`MAX_SPLIT_TRIES`] splits. `find` is asked about a run only +/// when the search reaches it, and about each run at most once, so at most [`MAX_RUN`] times per +/// group. +fn split_parts(groups: &[BezPath], find: &dyn Fn(&BezPath) -> Vec, together: &dyn Fn(&[BezPath]) -> bool) -> Vec> { + let g = groups.len(); + let mut s = Splits { groups, find, found: (0..g).map(|_| (0..MAX_RUN).map(|_| None).collect()).collect(), whole: vec![None; g] }; + // A split is the runs it takes, each as (start, number of groups). + let mut first = vec![]; + if g == 0 || !s.extend(&mut first, false) { + return vec![]; + } + let runs_of = + |split: &[(usize, usize)]| -> Vec { split.iter().map(|&(at, n)| groups.get(at..at + n).map(joined).unwrap_or_default()).collect() }; + let accepted = 'search: { + if first.len() == 1 { + break 'search Some(first); + } + // The shortest runs are looked up only once the longest are rejected. + let mut shortest = vec![]; + let mut shortest_next = true; + let mut rest = first.clone(); + let mut candidate = Some(first.clone()); + let mut tried = 0; + while let Some(split) = candidate.take() { + if tried == MAX_SPLIT_TRIES { + break; + } + tried += 1; + if together(&runs_of(&split)) { + break 'search Some(split); + } + if std::mem::take(&mut shortest_next) && s.extend(&mut shortest, true) && shortest != first { + candidate = Some(shortest.clone()); + continue; + } + candidate = loop { + if !s.advance(&mut rest) { + break None; + } + if rest != shortest { + break Some(rest.clone()); + } + }; + } + None + }; + let Some(split) = accepted else { return vec![] }; + let mut out = vec![]; + for (at, n) in split { + let Some(Some(cands)) = s.found.get_mut(at).and_then(|f| f.get_mut(n.wrapping_sub(1))).map(Option::take) else { return vec![] }; + out.push(cands); + } + out +} + +/// The state of one [`split_parts`] search: each run's candidates and whether the groups from each +/// start can all be matched, each found when the search first needs it. +struct Splits<'a, M> { + groups: &'a [BezPath], + find: &'a dyn Fn(&BezPath) -> Vec, + /// By start and length less one: the run's candidates, once looked up. + found: Vec>>>, + /// By start: whether the groups from there on can all be matched, once known. + whole: Vec>, +} + +impl Splits<'_, M> { + /// Whether the run of `n` groups at `at` matches, looking it up the first time. + fn matches(&mut self, at: usize, n: usize) -> bool { + let Some(slot) = self.found.get_mut(at).and_then(|f| f.get_mut(n.wrapping_sub(1))) else { return false }; + if slot.is_none() { + let cands = self.groups.get(at..at + n).map(|run| (self.find)(&joined(run))).unwrap_or_default(); + *slot = Some(cands); + } + slot.as_ref().is_some_and(|c| !c.is_empty()) + } + + /// Whether the groups from `at` on can all be matched. The search goes from `at` forward, the + /// longest run first, and no start is searched twice, so it ends after at most [`MAX_RUN`] + /// lookups and checks per group. + fn completes(&mut self, at: usize) -> bool { + let g = self.groups.len(); + if at >= g { + return at == g; + } + if let Some(known) = self.whole.get(at).copied().flatten() { + return known; + } + // From `at`: the starts still to settle, each with the run length to try next. + let mut stack: Vec<(usize, usize)> = vec![(at, MAX_RUN.min(g - at))]; + while let Some(&(i, n)) = stack.last() { + if self.whole.get(i).copied().flatten().is_some() { + stack.pop(); + continue; + } + if n == 0 { + if let Some(w) = self.whole.get_mut(i) { + *w = Some(false); + } + stack.pop(); + continue; + } + if let Some(top) = stack.last_mut() { + top.1 = n - 1; + } + if !self.matches(i, n) { + continue; + } + let next = i + n; + match if next == g { Some(true) } else { self.whole.get(next).copied().flatten() } { + Some(true) => { + if let Some(w) = self.whole.get_mut(i) { + *w = Some(true); + } + stack.pop(); + } + Some(false) => {} + None => { + // Settle `next` first, then try this run again. + if let Some(top) = stack.last_mut() { + top.1 = n; + } + stack.push((next, MAX_RUN.min(g - next))); + } + } + } + self.whole.get(at).copied().flatten() == Some(true) + } + + /// Whether `n` groups at `at` are a run a complete split can take. + fn fits(&mut self, at: usize, n: usize) -> bool { + self.matches(at, n) && self.completes(at + n) + } + + /// Takes `split` on to the last group, with the longest run that fits at each start (or, with + /// `shortest`, the shortest); false when no run fits. + fn extend(&mut self, split: &mut Vec<(usize, usize)>, shortest: bool) -> bool { + let g = self.groups.len(); + let mut i = split.last().map_or(0, |&(at, n)| at + n); + while i < g { + let most = MAX_RUN.min(g - i); + let lens: Vec = if shortest { (1..=most).collect() } else { (1..=most).rev().collect() }; + let Some(n) = lens.into_iter().find(|&n| self.fits(i, n)) else { return false }; + split.push((i, n)); + i += n; + } + true + } + + /// Moves `split` on to the next complete split, in order: the runs before the last start where + /// a shorter run fits kept, that shorter run, and the longest runs after it. False past the last. + fn advance(&mut self, split: &mut Vec<(usize, usize)>) -> bool { + while let Some((at, n)) = split.pop() { + if let Some(m) = (1..n).rev().find(|&m| self.fits(at, m)) { + split.push((at, m)); + return self.extend(split, false); + } + } + false + } +} + +/// Contour groups joined into one path. +fn joined(run: &[BezPath]) -> BezPath { + let mut p = BezPath::new(); + for g in run { + p.extend(g.iter()); + } + p +} + +/// Every run of contour `groups` ([`split_groups`]) [`parts`] may try. +fn runs(groups: &[BezPath]) -> Vec { + let mut out = vec![]; + for i in 0..groups.len() { + for n in 1..=MAX_RUN.min(groups.len() - i) { + if let Some(run) = groups.get(i..i + n) { + out.push(joined(run)); + } + } + } + out +} + +/// The keys the index looks glyphs up by for the `shapes` (each with its contour groups) and +/// every run of their groups: their exact [`shape_key`](vectorcraft_text::retype::shape_key)s and, +/// with a `tolerance` above 0, at most [`MAX_NEAR_KEYS`](vectorcraft_text::retype::MAX_NEAR_KEYS) +/// distinct [`near_key`](vectorcraft_text::retype::near_key)s: every whole shape's first, in the +/// order the shapes come, then the runs of their groups. Paths past that many near keys are not +/// looked up for near matches. +fn lookup_keys<'a>( + shapes: impl IntoIterator, + tolerance: f64, +) -> (std::collections::BTreeSet, Vec) { + use vectorcraft_text::retype::{MAX_NEAR_KEYS, near_key, shape_key}; + let mut wanted = std::collections::BTreeSet::new(); + let mut near_wanted = vec![]; + let mut add = |run: &BezPath| { + wanted.extend(shape_key(run)); + if tolerance > 0.0 + && near_wanted.len() < MAX_NEAR_KEYS + && let Some(k) = near_key(run) + && !near_wanted.contains(&k) + { + near_wanted.push(k); + } + }; + let shapes: Vec<(&BezPath, &[BezPath])> = shapes.into_iter().collect(); + // The whole shapes first, then the runs of their groups. + for (path, _) in &shapes { + add(path); + } + for (_, groups) in &shapes { + for run in runs(groups) { + add(&run); + } + } + (wanted, near_wanted) +} + fn retype(s: &mut Session, p: &Value) -> Result { const C: &str = "type.retype"; let fonts: Vec = match p.get("fonts") { - None | Some(Value::Null) => vec![], + None => vec![], Some(Value::Array(a)) => { a.iter().map(|v| v.as_str().map(str::to_owned).ok_or_else(|| bad(C, "fonts must be a list of family names"))).collect::>()? } Some(_) => return Err(bad(C, "fonts must be a list of family names")), }; + let tolerance = match p.get("tolerance") { + None => 0.0, + Some(v) => match v.as_f64() { + Some(t) if (0.0..=vectorcraft_text::retype::MAX_TOLERANCE).contains(&t) => t, + _ => return Err(bad(C, "tolerance must be a number from 0 to 0.05")), + }, + }; let roots = selected_roots(s)?; let db = FontDb::global(); let system_layout = s.doc()?.doc.setup.system_layout; @@ -1114,59 +1523,130 @@ 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())); } - // 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(); + // Only glyphs with a selected shape's key (segments per contour) can match one exactly, and + // only glyphs with as many contours and a close area-to-length ratio nearly: the others are + // not sampled. A shape of several glyphs is looked up by the runs of its groups too. + let mut budget = MAX_SPLIT_TOTAL_CONTOURS; + let groups: Vec> = pieces.iter().map(|pc| if pc.keeps_outline { vec![] } else { split_groups(&pc.path, &mut budget) }).collect(); + let (wanted, near_wanted) = + lookup_keys(pieces.iter().zip(&groups).filter(|(pc, _)| !pc.keeps_outline).map(|(pc, g)| (&pc.path, g.as_slice())), tolerance); // Nothing to match (every shape stays an outline): no font is read. - let index = if wanted.is_empty() { RetypeIndex::default() } else { 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(); + let index = if wanted.is_empty() && near_wanted.is_empty() { + RetypeIndex::default() + } else { + RetypeIndex::build_near(db, candidate_faces(db, d, &fonts), &wanted, &near_wanted) + }; + // Exact matches first; near ones, when allowed, only for a path with none. + // With a visible fill, a glyph is kept only when it paints the area the shape paints under + // the shape's fill rule (`fill` is `Some(nonzero)`). + let find = |path: &BezPath, part: bool, near: bool, fill: Option| { + let painted = |ms: Vec, tol: f64| -> Vec { + match fill { + Some(nonzero) => ms.iter().filter_map(|m| index.painted_agrees(m, path, nonzero, tol)).collect(), + None => ms, + } + }; + let exact = painted(if part { index.matches_either_way(path) } else { index.all_matches(path) }, 0.0); + if exact.is_empty() && near { painted(index.near_matches(path, tolerance), tolerance) } else { exact } + }; + // Each shape's parts (one, unless it holds several glyphs), each with its candidates. + let found: Vec>> = pieces + .iter() + .zip(&groups) + .map(|(pc, g)| { + if pc.keeps_outline { + return vec![]; + } + 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| { + 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) + }) + .collect(); // The face most shapes match, so a glyph several fonts share is set in the same font as - // the glyphs around it. - let mut votes: Vec<(u32, usize)> = vec![]; - for ms in &found { - // Each shape votes once for each face it fits. - let mut faces: Vec = ms.iter().map(|m| m.face.id()).collect(); - faces.sort_unstable(); - faces.dedup(); - for f in faces { - match votes.iter_mut().find(|(v, _)| *v == f) { - Some(v) => v.1 += 1, - None => votes.push((f, 1)), + // the glyphs around it. Among faces that as many shapes match, the one whose glyphs fit + // them closest in all (the least leftover distance summed over those shapes), so every + // part that fits it takes the same face, exact matches as near ones. Simplified outlines + // of one font can nearly fit another font's look-alike glyphs, some of them more closely. + // A part's matches are all exact or all near, so the distances a face sums compare alike. + let mut votes: Vec<(u32, usize, f64)> = vec![]; + for ms in found.iter().flatten() { + // Each shape votes once for each face it fits, with the leftover distance of its + // closest glyph in that face. + let mut faces: Vec<(u32, f64)> = vec![]; + for m in ms { + let residual = if m.residual.is_finite() { m.residual } else { f64::INFINITY }; + match faces.iter_mut().find(|(f, _)| *f == m.face.id()) { + Some(f) => f.1 = f.1.min(residual), + None => faces.push((m.face.id(), residual)), + } + } + for (f, residual) in faces { + match votes.iter_mut().find(|(v, ..)| *v == f) { + Some(v) => { + v.1 += 1; + v.2 += residual; + } + None => votes.push((f, 1, residual)), } } } - let rank = |face: u32| votes.iter().find(|(f, _)| *f == face).map_or(0, |v| v.1); + let rank = |face: u32| votes.iter().find(|(f, ..)| *f == face).map_or((0, f64::INFINITY), |v| (v.1, v.2)); + // Most votes, then the least summed distance, then the first (the matches come upright and + // closest first). + let ranked = |(i, a): &(usize, &GlyphMatch), (j, b): &(usize, &GlyphMatch)| { + let ((va, da), (vb, db)) = (rank(a.face.id()), rank(b.face.id())); + va.cmp(&vb).then(db.total_cmp(&da)).then(j.cmp(i)) + }; let mut lines: Vec = vec![]; let mut unmatched = vec![]; // 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) { + // How many glyphs each shape is: a shape of several becomes type only when all of them do. + let mut glyphs_of: std::collections::HashMap = std::collections::HashMap::new(); + for (pc, parts) in pieces.iter().zip(&found) { if pc.after_barrier { open = false; } - // The shape's candidates in the face most shapes fit (the first such face, as the - // matches come upright and closest first). - let face = ms.iter().enumerate().max_by_key(|(i, m)| (rank(m.face.id()), std::cmp::Reverse(*i))).map(|(_, m)| m.face.id()); - let cands: Vec = ms.iter().filter(|m| Some(m.face.id()) == face).cloned().collect(); - if cands.is_empty() { + if parts.is_empty() { unmatched.push(pc.id); open = false; continue; } - let joined = match lines.last_mut() { - Some(l) if open => join(l, pc, &cands), - _ => false, - }; - if !joined { - lines.extend(new_line(pc, &cands)); + glyphs_of.insert(pc.id, parts.len()); + for ms in parts { + // The part's candidates in the face most shapes fit (see `ranked`). + let face = ms.iter().enumerate().max_by(ranked).map(|(_, m)| m.face.id()); + let cands: Vec = ms.iter().filter(|m| Some(m.face.id()) == face).cloned().collect(); + let joined = match lines.last_mut() { + Some(l) if open => join(l, pc, &cands), + _ => false, + }; + if !joined { + lines.extend(new_line(pc, &cands, tolerance)); + } + open = true; } - open = true; } // 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. Lone means alone on its baseline as the outlines lie, before the - // layout check splits lines (tracking and manual kerning split them glyph by glyph). + // art (a dot, a bar): beside other shapes, the other glyphs of a split compound path + // included, it stays an outline. Selected on its own, it is what was asked for. Lone means + // alone on its baseline as the outlines lie, before the layout check splits lines (tracking + // and manual kerning split them glyph by glyph). let lone_pieces: std::collections::HashSet = lines.iter().flat_map(Line::lone_one_contour).collect(); // Each line's tracking is measured with nine layouts at most: the line as read (its // characters), its glyphs without and with one space between them (three times its @@ -1177,7 +1657,7 @@ fn retype(s: &mut Session, p: &Value) -> Result { let mut lines: Vec = lines.into_iter().map(|l| l.tracked(system_layout, &mut budget)).collect(); share_tracking(&mut lines, system_layout); let mut lines = verified(lines, system_layout, &mut unmatched); - if pieces.len() > 1 { + if pieces.len() > 1 || glyphs_of.values().any(|n| *n > 1) { lines.retain(|l| { let lone = matches!(l.pieces.as_slice(), [id] if lone_pieces.contains(id)); if lone { @@ -1186,9 +1666,24 @@ fn retype(s: &mut Session, p: &Value) -> Result { !lone }); } + let dropped = partial_lines(&lines.iter().map(|l| l.pieces.clone()).collect::>(), &glyphs_of); + let mut k = 0; + lines.retain(|l| { + let keep = !dropped.contains(&k); + k += 1; + if !keep { + unmatched.extend(l.pieces.iter().copied()); + } + keep + }); + // A shape of several glyphs is reported once, and not when it became type. + let kept: std::collections::HashSet = lines.iter().flat_map(|l| l.pieces.iter().copied()).collect(); + let mut seen = std::collections::HashSet::new(); + unmatched.retain(|id| !kept.contains(id) && seen.insert(*id)); let at: std::collections::HashMap = pieces.iter().enumerate().map(|(i, pc)| (pc.id, i)).collect(); + // Lines split from one compound path share its shape, with nothing between them. let follows = |a: NodeId, b: NodeId| match (at.get(&a), at.get(&b)) { - (Some(i), Some(j)) => *j == i + 1 && pieces.get(*j).is_some_and(|pc| !pc.after_barrier), + (Some(i), Some(j)) => *j == *i || (*j == i + 1 && pieces.get(*j).is_some_and(|pc| !pc.after_barrier)), _ => false, }; // Joining lines takes at most four tries' work per glyph (see [`Block::made`]), with room @@ -1204,6 +1699,9 @@ fn retype(s: &mut Session, p: &Value) -> Result { let before = lines.iter().any(|l| l.point.is_some()).then(|| d.clone()); let mut ids = vec![]; let mut emptied = vec![]; + // Every line's type goes in before the shapes go: a shape of several glyphs is the first + // shape of more than one line. Each goes just before its first shape, so lines from one + // shape keep their order. for l in &lines { let Some(first) = l.pieces.first() else { continue }; let (par, idx, _) = d.position(*first).ok_or(EngineError::NoNode(*first))?; @@ -1213,14 +1711,18 @@ fn retype(s: &mut Session, p: &Value) -> Result { node.opacity = l.opacity; node.blend = l.blend; d.insert(par, idx, node)?; - for pc in &l.pieces { - if let Some((Some(parent), _, _)) = d.position(*pc) { - emptied.push(parent); - } - d.remove(*pc)?; - } ids.push(id); } + let mut removed = std::collections::HashSet::new(); + for pc in lines.iter().flat_map(|l| l.pieces.iter()) { + if !removed.insert(*pc) { + continue; + } + if let Some((Some(parent), _, _)) = d.position(*pc) { + emptied.push(parent); + } + d.remove(*pc)?; + } // A group left holding only the new type (Create Outlines' group) gives way to it, unless // the group has transparency, effects, a mask or a name of its own, which then keep // applying to the type inside it. A group left empty goes. @@ -1284,7 +1786,7 @@ fn retype(s: &mut Session, p: &Value) -> Result { for (l, id) in lines.iter().zip(&ids).filter(|_| units > 0) { let Some(point) = &l.point else { continue }; let Some(NodeKind::Text(t)) = probe.node(*id).map(|n| &n.kind) else { continue }; - if flowed && (t.wrap.is_empty() || first_mismatch(t, &l.matches, l.size).is_none()) { + if flowed && (t.wrap.is_empty() || first_mismatch(t, &l.matches, l.size, l.slack).is_none()) { continue; } if let Some(NodeKind::Text(t)) = d.node_mut(*id).map(|n| &mut n.kind) { @@ -1306,14 +1808,18 @@ fn retype(s: &mut Session, p: &Value) -> Result { "fonts": lines .iter() .zip(&made) - .map(|(l, id)| json!({ - "family": l.face.family, - "style": l.face.style, - "size": l.size, - "tracking": l.tracking, - "leading": l.leading, - "area": area(id), - })) + .map(|(l, id)| { + let score = l.matches.iter().map(GlyphMatch::score).fold(1.0, f64::min); + json!({ + "family": l.face.family, + "style": l.face.style, + "size": l.size, + "tracking": l.tracking, + "leading": l.leading, + "area": area(id), + "score": (score * 1000.0).round() / 1000.0, + }) + }) .collect::>(), "unmatched": unmatched.iter().map(|i| i.0).collect::>(), "truncated": truncated, diff --git a/crates/engine/src/cmd/retype_tests.rs b/crates/engine/src/cmd/retype_tests.rs index de2122fc09..2727dd4432 100644 --- a/crates/engine/src/cmd/retype_tests.rs +++ b/crates/engine/src/cmd/retype_tests.rs @@ -10,7 +10,13 @@ fn session() -> Session { } /// Point text, outlined: the id of the outlines' group. -fn outlined(s: &mut Session, p: serde_json::Value) -> u64 { +fn outlined(s: &mut Session, mut p: serde_json::Value) -> u64 { + // A text without a font is set in the system's default face, which differs from machine to + // machine and decides which glyphs a near match can tell apart. + if p.get("font").is_none() { + p["font"] = json!("Source Sans 3"); + p["style"] = json!("Regular"); + } let id = s.execute("text.create", &p).unwrap()["id"].as_u64().unwrap(); s.execute("select.set", &json!({"ids": [id]})).unwrap(); s.execute("type.createOutlines", &json!({})).unwrap()["ids"][0].as_u64().unwrap() @@ -84,7 +90,7 @@ fn a_drawn_shape_is_reported_and_left_alone() { 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"})] { + for bad in [json!("Source Sans 3"), json!([1]), json!({"family": "x"}), json!(null)] { 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}"); } @@ -539,6 +545,118 @@ fn a_long_tracked_line_is_checked_in_time() { eprintln!("450 tracked glyphs: {:?}, {} objects", t0.elapsed(), texts.len()); } +/// Every path in the document flattened into lines `tol` from its curves, each contour started at +/// its second point and run backwards: outlines simplified and built nothing like the font's. +fn simplify_every_path(s: &mut Session, tol: f64) { + let mut ids = vec![]; + s.doc().unwrap().doc.walk(|n| { + if matches!(n.kind, NodeKind::Path { .. }) { + ids.push(n.id); + } + }); + simplify_paths(s, &ids, tol); +} + +/// [`simplify_every_path`] for the paths `ids` only. +fn simplify_paths(s: &mut Session, ids: &[NodeId], tol: f64) { + s.edit("test", |d, _| { + for id in ids { + let data = d.node_mut(*id).unwrap().path_data_mut().unwrap(); + let mut contours: Vec> = vec![]; + kurbo::flatten(data.to_bezpath().iter(), tol, |el| match el { + kurbo::PathEl::MoveTo(q) => contours.push(vec![q]), + kurbo::PathEl::LineTo(q) => contours.last_mut().unwrap().push(q), + _ => {} + }); + let mut out = kurbo::BezPath::new(); + for mut c in contours { + if c.len() > 1 && c.first() == c.last() { + c.pop(); + } + c.rotate_left(1); + c.reverse(); + out.move_to(c[0]); + for q in &c[1..] { + out.line_to(*q); + } + out.close_path(); + } + *data = vectorcraft_geom::PathData::from_bezpath(&out); + } + Ok(()) + }) + .unwrap(); +} + +#[test] +fn simplified_outlines_retype_only_with_a_tolerance_and_report_their_score() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Rag", "size": 40, "font": "Source Sans 3", "style": "Regular"})); + simplify_every_path(&mut s, 0.05); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let e = s.execute("type.retype", &json!({})).unwrap_err().to_string(); + assert!(e.contains("no selected shape matches"), "no exact match: {e}"); + for bad in [json!(-0.01), json!(0.06), json!("0.02"), json!(true), json!(null)] { + let e = s.execute("type.retype", &json!({"tolerance": bad})).unwrap_err().to_string(); + assert!(e.contains("tolerance must be a number from 0 to 0.05"), "{e}"); + } + assert!(s.doc().unwrap().doc == before, "nothing changes"); + let r = s.execute("type.retype", &json!({"tolerance": 0.02})).unwrap(); + assert_eq!(r["texts"], json!(["Rag"]), "{r}"); + assert_eq!(r["unmatched"], json!([])); + assert_eq!((r["fonts"][0]["family"].as_str(), r["fonts"][0]["style"].as_str()), (Some("Source Sans 3"), Some("Regular"))); + let score = r["fonts"][0]["score"].as_f64().unwrap(); + assert!(score > 0.5 && score < 1.0, "a near match scores below an exact one: {score}"); + let t = text_of(&s, r["ids"][0].as_u64().unwrap()); + assert!(near(t.first_style().size, 40.0, 0.4), "size {}", t.first_style().size); + let o = t.xf.translation(); + assert!(near(o.x, 40.0, 0.4) && near(o.y, 100.0, 0.4), "baseline start {o:?}"); +} + +#[test] +fn exact_matches_score_one() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Rag", "size": 40})); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.02})).unwrap(); + assert_eq!(r["texts"], json!(["Rag"]), "{r}"); + assert_eq!(r["fonts"][0]["score"], json!(1.0)); +} + +#[test] +fn exact_matches_with_a_small_leftover_still_score_one() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Rag", "size": 40})); + // Each glyph stretched 0.1% across its own centre: still within the exact matcher's reach, with + // a leftover distance above 0. + let mut ids = vec![]; + s.doc().unwrap().doc.walk(|n| { + if matches!(n.kind, NodeKind::Path { .. }) { + ids.push(n.id); + } + }); + s.edit("test", |d, _| { + for id in &ids { + let data = d.node_mut(*id).unwrap().path_data_mut().unwrap(); + let p = data.to_bezpath(); + let c = kurbo::Shape::bounding_box(&p).center(); + let xf = kurbo::Affine::translate(c.to_vec2()) * kurbo::Affine::scale_non_uniform(1.001, 1.0) * kurbo::Affine::translate(-c.to_vec2()); + *data = vectorcraft_geom::PathData::from_bezpath(&(xf * p)); + } + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + // Each glyph's size now differs a little, so each is its own line. + let texts: Vec<&str> = r["texts"].as_array().unwrap().iter().map(|t| t.as_str().unwrap()).collect(); + assert_eq!(texts.concat(), "Rag", "{r}"); + for f in r["fonts"].as_array().unwrap() { + assert_eq!(f["score"], json!(1.0), "{r}"); + } +} + #[test] fn two_overlapping_lone_glyphs_beside_art_stay_outlines() { // Two M outlines start 0.3 em apart, closer than the first one's advance: a tight-tracking @@ -583,6 +701,55 @@ fn assert_same_places(a: &TextObject, b: &TextObject) { } } +#[test] +fn exact_glyphs_of_another_size_are_another_line_even_with_a_tolerance() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Tia", "size": 40})); + // The a (two contours, so not a lone one-contour glyph) made 4% larger about where it starts + // on the baseline: still exactly the glyph. + let a = *children_of(&s, g).last().unwrap(); + // The a's paths (a compound path's, or its own). + let paths: Vec = match s.doc().unwrap().doc.node(NodeId(a)).unwrap().children() { + Some(c) => c.iter().map(|c| c.id.0).collect(), + None => vec![a], + }; + s.edit("test", |d, _| { + let x0 = paths + .iter() + .map(|id| kurbo::Shape::bounding_box(&d.node_mut(NodeId(*id)).unwrap().path_data_mut().unwrap().to_bezpath()).x0) + .fold(f64::INFINITY, f64::min); + let at = kurbo::Vec2::new(x0, 100.0); + let xf = kurbo::Affine::translate(at) * kurbo::Affine::scale(1.04) * kurbo::Affine::translate(-at); + for id in &paths { + let data = d.node_mut(NodeId(*id)).unwrap().path_data_mut().unwrap(); + *data = vectorcraft_geom::PathData::from_bezpath(&(xf * data.to_bezpath())); + } + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.05})).unwrap(); + assert_eq!(r["texts"], json!(["Ti", "a"]), "{r}"); + let sizes: Vec = r["fonts"].as_array().unwrap().iter().map(|f| f["size"].as_f64().unwrap()).collect(); + assert!(near(sizes[0], 40.0, 0.01) && near(sizes[1], 41.6, 0.01), "{sizes:?}"); + assert!(r["fonts"].as_array().unwrap().iter().all(|f| f["score"] == json!(1.0)), "{r}"); +} + +#[test] +fn turned_near_matched_round_glyphs_keep_the_line_turn() { + // Round glyphs (o, O, the period) and glyphs that look alike half round (N, x, z) fit + // simplified outlines at more than one turn: each takes the turn of its line. + for (text, rotation) in [("no.", 90), ("no.", 180), ("zoo", 135), ("NOON", 180), ("xoxo", 200), ("on", 120)] { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 300, "y": 200, "text": text, "size": 40, "rotation": rotation})); + simplify_every_path(&mut s, 0.05); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.02})).unwrap(); + assert_eq!(r["texts"], json!([text]), "turned {rotation}: {r}"); + assert_eq!(r["unmatched"], json!([]), "{r}"); + } +} + #[test] fn several_lines_become_one_area_type_object() { let mut s = session(); @@ -870,6 +1037,660 @@ fn spaced_letters_keep_their_spaces_when_type_draws_them() { } } +#[test] +fn a_near_glyph_moved_by_more_than_the_tolerance_ends_the_line() { + // The m (one path) of simplified outlines moved along the baseline by `shift` of the font + // size (40), retyped with a tolerance of 0.03: it may sit 1% + 3% of the size from where the + // type draws it. + let run = |shift: f64| { + 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 m = *children_of(&s, g).last().unwrap(); + s.edit("test", |d, _| { + let data = d.node_mut(NodeId(m)).unwrap().path_data_mut().unwrap(); + *data = vectorcraft_geom::PathData::from_bezpath(&(kurbo::Affine::translate((40.0 * shift, 0.0)) * data.to_bezpath())); + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.03})).unwrap(); + let texts: Vec = r["texts"].as_array().unwrap().iter().map(|t| t.as_str().unwrap().to_owned()).collect(); + assert_eq!(texts.concat(), "Tim", "{r}"); + texts.len() + }; + assert_eq!(run(0.02), 1, "within the tolerance: one 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(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Tim", "size": 40})); + let m = *children_of(&s, g).last().unwrap(); + s.edit("test", |d, _| { + let data = d.node_mut(NodeId(m)).unwrap().path_data_mut().unwrap(); + *data = vectorcraft_geom::PathData::from_bezpath(&(kurbo::Affine::translate((40.0 * 0.03, 0.0)) * data.to_bezpath())); + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.05})).unwrap(); + assert_eq!(r["texts"], json!(["Ti", "m"]), "exact glyphs keep the exact limits: {r}"); +} + +#[test] +fn an_exact_glyph_after_a_near_one_keeps_the_exact_limits() { + // T exact, o simplified (a near match), m exact moved by 3% of the size along the baseline. + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Tom", "size": 40})); + let kids = children_of(&s, g); + let o: Vec = match s.doc().unwrap().doc.node(NodeId(kids[1])).unwrap().children() { + Some(c) => c.iter().map(|c| c.id).collect(), + None => vec![NodeId(kids[1])], + }; + simplify_paths(&mut s, &o, 0.05); + let m = kids[2]; + s.edit("test", |d, _| { + let data = d.node_mut(NodeId(m)).unwrap().path_data_mut().unwrap(); + *data = vectorcraft_geom::PathData::from_bezpath(&(kurbo::Affine::translate((40.0 * 0.03, 0.0)) * data.to_bezpath())); + Ok(()) + }) + .unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({"tolerance": 0.05})).unwrap(); + assert_eq!(r["texts"], json!(["To", "m"]), "the near o does not loosen the m's limits: {r}"); +} + +/// The outlines of `text` made into one compound path: the compound's id. +fn one_compound(s: &mut Session, text: &str) -> u64 { + let g = outlined(s, json!({"x": 40, "y": 100, "text": text, "size": 40})); + let glyphs = children_of(s, g); + s.execute("select.set", &json!({"ids": glyphs})).unwrap(); + s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap() +} + +#[test] +fn a_word_outlined_as_one_compound_path_splits_into_its_glyphs() { + let mut s = session(); + let c = one_compound(&mut s, "Tim"); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!(["Tim"]), "the i's stem and dot are one glyph: {r}"); + assert_eq!(r["unmatched"], json!([])); + assert!(s.doc().unwrap().doc.node(NodeId(c)).is_none(), "the compound becomes the type"); +} + +#[test] +fn a_lone_period_in_a_compound_selected_alone_stays_an_outline() { + let mut s = session(); + // An A and, far below it, a period, made into one compound path and selected alone: the + // period, alone on its baseline beside the A, is a dot as much as a period, so it stays an + // outline, and so does the A in the same compound. + let t = outlined(&mut s, json!({"x": 40, "y": 100, "text": "A", "size": 40})); + let dot = outlined(&mut s, json!({"x": 40, "y": 300, "text": ".", "size": 40})); + let mut ids = children_of(&s, t); + ids.extend(children_of(&s, dot)); + s.execute("select.set", &json!({"ids": ids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let e = match s.execute("type.retype", &json!({})) { + Ok(r) => panic!("the period became type: {r}"), + Err(e) => e.to_string(), + }; + assert!(e.contains("(1 checked)"), "{e}"); + assert!(s.doc().unwrap().doc == before, "nothing changes"); +} + +#[test] +fn lines_from_one_compound_path_keep_their_order() { + let mut s = session(); + // Two lines of outlines made into one compound path, farther apart than the lines of one + // paragraph, so each stays an object of its own. + let top = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Tim", "size": 40})); + let low = outlined(&mut s, json!({"x": 40, "y": 300, "text": "Ham", "size": 40})); + let mut glyphs = children_of(&s, top); + glyphs.extend(children_of(&s, low)); + s.execute("select.set", &json!({"ids": glyphs})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + let ids: Vec = r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect(); + assert_eq!(ids.len(), 2, "{r}"); + let texts: Vec = ids.iter().map(|id| text_of(&s, *id).plain_text()).collect(); + let d = &s.doc().unwrap().doc; + let at: Vec = ids.iter().map(|id| d.position(NodeId(*id)).unwrap().1).collect(); + // The lines stack in the order of the compound's contours: the first line behind the second. + let first = texts.iter().position(|t| t == "Tim").unwrap(); + let second = texts.iter().position(|t| t == "Ham").unwrap(); + assert!(at[first] < at[second], "{texts:?} at {at:?}"); +} + +#[test] +fn stacked_lines_from_one_compound_path_become_one_object() { + let mut s = session(); + // Two lines of one paragraph, outlined and made into one compound path: they join as the + // lines of separate outlines do. + let text = "Tim\nHam"; + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": text, "size": 40})); + let glyphs = children_of(&s, g); + s.execute("select.set", &json!({"ids": glyphs})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!([text]), "{r}"); + assert_eq!(r["unmatched"], json!([]), "{r}"); + assert!(s.doc().unwrap().doc.node(NodeId(c)).is_none(), "the compound becomes the type"); +} + +#[test] +fn a_compound_whose_parts_are_not_all_glyphs_stays_whole() { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "Ti", "size": 40})); + let mut ids = children_of(&s, g); + // A shape no glyph is, beside the letters, in the same compound. + ids.push(s.execute("shape.star", &json!({"cx": 140, "cy": 85, "radius1": 12, "radius2": 5, "points": 7})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": ids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let e = s.execute("type.retype", &json!({})).unwrap_err().to_string(); + assert!(e.contains("(1 checked)"), "reported once: {e}"); + assert!(s.doc().unwrap().doc == before, "nothing changes"); +} + +#[test] +fn glyphs_that_overlap_in_an_even_odd_compound_stay_an_outline() { + let mut s = session(); + // Two o's moved onto each other and made into one compound path: the even-odd rule leaves + // the overlap unpainted, and two glyphs set side by side would paint it. + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "oo", "size": 40})); + let kids = children_of(&s, g); + s.execute("object.move", &json!({"ids": [kids[1]], "dx": -9, "dy": 0})).unwrap(); + s.execute("select.set", &json!({"ids": kids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let e = match s.execute("type.retype", &json!({})) { + Ok(r) => panic!("the overlapping o's became type: {r}"), + Err(e) => e.to_string(), + }; + assert!(e.contains("(1 checked)"), "{e}"); + 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. + for dx in [-9.0, -11.0, -12.0, -13.0, -14.0] { + let mut s = session(); + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "oo", "size": 40})); + let kids = children_of(&s, g); + s.execute("object.move", &json!({"ids": [kids[1]], "dx": dx, "dy": 0})).unwrap(); + s.execute("select.set", &json!({"ids": kids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + for tolerance in [0.0, 0.05] { + if let Ok(r) = s.execute("type.retype", &json!({"tolerance": tolerance})) { + panic!("the overlapping o's became type at dx {dx}, tolerance {tolerance}: {r}"); + } + } + } +} + +#[test] +fn exact_parts_of_a_compound_are_checked_at_tolerance_zero() { + let mut s = session(); + // A T and an o whose boxes overlap by 4 units, one painted area apart (the o sits under the + // bar's arm): a strip that the probes of tolerance 0.05 cannot see into but those of an exact + // match can. Both parts match exactly, so asking for near matches does not change the result. + let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "To", "size": 40})); + let kids = children_of(&s, g); + let (t, o) = (kids[0], kids[1]); + let bounds = |s: &Session, id: u64| s.doc().unwrap().doc.node(NodeId(id)).unwrap().geometric_bounds().unwrap(); + let dx = bounds(&s, t).x1 - 4.0 - bounds(&s, o).x0; + s.execute("object.move", &json!({"ids": [o], "dx": dx, "dy": 0})).unwrap(); + s.execute("select.set", &json!({"ids": kids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + for params in [json!({}), json!({"tolerance": 0.05})] { + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let r = s.execute("type.retype", ¶ms).unwrap(); + assert_eq!(r["unmatched"], json!([]), "{params}: {r}"); + assert!(s.doc().unwrap().doc.node(NodeId(c)).is_none(), "{params}: the compound becomes type"); + s.execute("edit.undo", &json!({})).unwrap(); + assert!(s.doc().unwrap().doc == before, "{params}: undo restores it"); + } +} + +#[test] +fn glyphs_side_by_side_in_an_even_odd_compound_still_become_type() { + let mut s = session(); + let c = one_compound(&mut s, "oo"); + s.execute("select.set", &json!({"ids": [c]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + assert_eq!(r["texts"], json!(["oo"]), "{r}"); +} + +#[test] +fn a_compound_whose_glyph_stays_an_outline_keeps_its_other_glyphs_too() { + let mut s = session(); + // An A (two contours), and far below it a period: a lone one-contour glyph beside other + // shapes stays an outline, so the A, in the same compound, must stay too. + let t = outlined(&mut s, json!({"x": 40, "y": 100, "text": "A", "size": 40})); + let dot = outlined(&mut s, json!({"x": 40, "y": 300, "text": ".", "size": 40})); + let paths = |s: &Session, id: u64| match s.doc().unwrap().doc.node(NodeId(id)).unwrap().children() { + Some(c) => c.iter().map(|c| c.id.0).collect(), + None => vec![id], + }; + let mut ids: Vec = paths(&s, t); + ids.extend(paths(&s, dot)); + assert_eq!(ids.len(), 2, "one path each"); + s.execute("select.set", &json!({"ids": ids})).unwrap(); + let c = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap(); + let r = s.execute("shape.rectangle", &json!({"x": 300, "y": 300, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [c, r]})).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let e = match s.execute("type.retype", &json!({})) { + Ok(r) => panic!("the A became type without its period: {r}"), + Err(e) => e.to_string(), + }; + assert!(e.contains("(2 checked)"), "{e}"); + assert!(s.doc().unwrap().doc == before, "nothing changes"); +} + +#[test] +fn a_split_takes_a_shorter_run_when_the_longest_leaves_a_group_unmatched() { + use kurbo::Shape; + // Three groups A, B and C side by side. A and B together are a glyph, but then C alone is + // none; A alone and B and C together are glyphs, so the split is A, BC. + let mut p = kurbo::BezPath::new(); + for x in [0.0, 100.0, 200.0] { + p.extend(kurbo::Rect::new(x, 0.0, x + 40.0, 40.0).path_elements(0.1)); + } + let find = |path: &kurbo::BezPath, part: bool, _near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + match (part, b.x0.round() as i64, b.x1.round() as i64) { + (true, 0, 140) => vec!["AB"], + (true, 0, 40) => vec!["A"], + (true, 100, 240) => vec!["BC"], + _ => vec![], + } + }; + let groups = all_groups(&p); + assert_eq!(super::parts(&p, &groups, &find, &|_, _| true), vec![vec!["A"], vec!["BC"]]); + // A split the parts' paint does not allow is dropped. + assert!(super::parts(&p, &groups, &find, &|_, _| false).is_empty()); + // With no way to match every group, nothing. + let none = |path: &kurbo::BezPath, part: bool, _near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + if part && b.x0.round() as i64 == 0 && b.x1.round() as i64 == 140 { vec!["AB"] } else { vec![] } + }; + assert!(super::parts(&p, &groups, &none, &|_, _| true).is_empty()); +} + +#[test] +fn a_split_the_paint_check_rejects_gives_way_to_another_complete_split() { + use kurbo::Shape; + let mut p = kurbo::BezPath::new(); + for x in [0.0, 100.0, 200.0] { + p.extend(kurbo::Rect::new(x, 0.0, x + 40.0, 40.0).path_elements(0.1)); + } + let find = |path: &kurbo::BezPath, part: bool, _near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + match (part, b.x0.round() as i64, b.x1.round() as i64) { + (true, 0, 140) => vec!["AB"], + (true, 0, 40) => vec!["A"], + (true, 100, 140) => vec!["B"], + (true, 200, 240) => vec!["C"], + _ => vec![], + } + }; + let groups = all_groups(&p); + // The longest runs first (AB, C) are rejected; the three single glyphs are kept. + let singles = |runs: &[kurbo::BezPath], _near: bool| runs.len() == 3; + assert_eq!(super::parts(&p, &groups, &find, &singles), vec![vec!["A"], vec!["B"], vec!["C"]]); + // When no complete split is accepted, nothing. + assert!(super::parts(&p, &groups, &find, &|_, _| false).is_empty()); +} + +#[test] +fn a_split_between_the_longest_and_the_shortest_runs_is_tried() { + use kurbo::Shape; + // Four groups A, B, C and D. ABC and D (the longest runs first) and the four single glyphs + // (the shortest) are complete splits the paint check rejects; AB and CD is the one it accepts. + let mut p = kurbo::BezPath::new(); + for x in [0.0, 100.0, 200.0, 300.0] { + p.extend(kurbo::Rect::new(x, 0.0, x + 40.0, 40.0).path_elements(0.1)); + } + let find = |path: &kurbo::BezPath, part: bool, _near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + match (part, b.x0.round() as i64, b.x1.round() as i64) { + (true, 0, 240) => vec!["ABC"], + (true, 0, 140) => vec!["AB"], + (true, 200, 340) => vec!["CD"], + (true, 0, 40) => vec!["A"], + (true, 100, 140) => vec!["B"], + (true, 200, 240) => vec!["C"], + (true, 300, 340) => vec!["D"], + _ => vec![], + } + }; + let groups = all_groups(&p); + let pairs = |runs: &[kurbo::BezPath], _near: bool| runs.len() == 2 && runs.first().is_some_and(|r| r.bounding_box().x1.round() as i64 == 140); + assert_eq!(super::parts(&p, &groups, &find, &pairs), vec![vec!["AB"], vec!["CD"]]); +} + +#[test] +fn at_most_max_split_tries_complete_splits_are_checked() { + use kurbo::Shape; + // Twelve groups, every run of up to three of them a glyph: far more complete splits than the + // cap. The paint check is asked about the cap's number of them and no more, and each run is + // looked up at most once. + let mut p = kurbo::BezPath::new(); + for i in 0..12 { + let x = f64::from(i) * 100.0; + p.extend(kurbo::Rect::new(x, 0.0, x + 40.0, 40.0).path_elements(0.1)); + } + let groups = all_groups(&p); + assert_eq!(groups.len(), 12); + let looked_up = std::cell::Cell::new(0usize); + let find = |_: &kurbo::BezPath| -> Vec<&'static str> { + looked_up.set(looked_up.get() + 1); + vec!["glyph"] + }; + let asked = &std::cell::Cell::new(0usize); + let accept_at = |n: usize| { + move |_: &[kurbo::BezPath]| { + asked.set(asked.get() + 1); + asked.get() == n + } + }; + // Rejected every time: the search stops at the cap. + assert!(super::split_parts(&groups, &find, &accept_at(usize::MAX)).is_empty()); + assert_eq!(asked.get(), super::MAX_SPLIT_TRIES); + assert!(looked_up.get() <= super::MAX_RUN * groups.len(), "{} lookups", looked_up.get()); + // A first split that is accepted looks up its own runs only: four runs of three groups. + asked.set(0); + looked_up.set(0); + assert_eq!(super::split_parts(&groups, &find, &accept_at(1)).len(), 4); + assert_eq!(looked_up.get(), 4); + // The last split tried is still taken; the one past the cap is not tried. + asked.set(0); + assert!(super::split_parts(&groups, &find, &accept_at(super::MAX_SPLIT_TRIES)).len() > 1); + asked.set(0); + assert!(super::split_parts(&groups, &find, &accept_at(super::MAX_SPLIT_TRIES + 1)).is_empty()); + assert_eq!(asked.get(), super::MAX_SPLIT_TRIES); +} + +/// `path`'s contour groups, with no limit on the contours grouped. +fn all_groups(path: &kurbo::BezPath) -> Vec { + let mut budget = usize::MAX; + super::split_groups(path, &mut budget) +} + +#[test] +fn exact_matches_of_the_parts_come_before_a_near_match_of_the_whole() { + use kurbo::Shape; + // Two groups side by side, each exactly a glyph (two periods), and the whole only nearly one + // (a dieresis): the parts win. + let mut p = kurbo::BezPath::new(); + for x in [0.0, 100.0] { + p.extend(kurbo::Rect::new(x, 0.0, x + 40.0, 40.0).path_elements(0.1)); + } + let find = |path: &kurbo::BezPath, part: bool, near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + match (part, near, b.x0.round() as i64, b.x1.round() as i64) { + (false, true, 0, 140) => vec!["dieresis"], + (true, _, 0, 40) | (true, _, 100, 140) => vec!["period"], + _ => vec![], + } + }; + let groups = all_groups(&p); + assert_eq!(super::parts(&p, &groups, &find, &|_, _| true), vec![vec!["period"], vec!["period"]]); + // When the parts match only nearly too, the whole near match comes first. + let near_only = |path: &kurbo::BezPath, part: bool, near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + match (part, near, b.x0.round() as i64, b.x1.round() as i64) { + (false, true, 0, 140) => vec!["dieresis"], + (true, true, 0, 40) | (true, true, 100, 140) => vec!["period"], + _ => vec![], + } + }; + assert_eq!(super::parts(&p, &groups, &near_only, &|_, _| true), vec![vec!["dieresis"]]); + // The split's paint check is told whether the parts were found nearly: exact parts are + // checked at tolerance 0 whatever tolerance the command was given. + let seen = std::cell::RefCell::new(vec![]); + let record = |_: &[kurbo::BezPath], near: bool| { + seen.borrow_mut().push(near); + true + }; + assert_eq!(super::parts(&p, &groups, &find, &record), vec![vec!["period"], vec!["period"]]); + assert_eq!(*seen.borrow(), vec![false]); + seen.borrow_mut().clear(); + let split_near_only = |path: &kurbo::BezPath, part: bool, near: bool| -> Vec<&'static str> { + let b = path.bounding_box(); + match (part, near, b.x0.round() as i64, b.x1.round() as i64) { + (true, true, 0, 40) | (true, true, 100, 140) => vec!["period"], + _ => vec![], + } + }; + assert_eq!(super::parts(&p, &groups, &split_near_only, &record), vec![vec!["period"], vec!["period"]]); + assert_eq!(*seen.borrow(), vec![true]); +} + +#[test] +fn a_shape_that_lost_a_glyph_drops_the_lines_of_its_other_glyphs() { + use std::collections::HashMap; + // Shapes 1 and 2 are two glyphs each, shape 3 one. Line 0 sets one glyph of 1 and one of 2; + // line 1 sets the other glyph of 2 and shape 3. Shape 1's other glyph is set nowhere. + let (a, b, c) = (NodeId(1), NodeId(2), NodeId(3)); + let glyphs_of: HashMap = [(a, 2), (b, 2), (c, 1)].into(); + let lines = vec![vec![a, b], vec![b, c]]; + let dropped = super::partial_lines(&lines, &glyphs_of); + assert!(dropped.contains(&0), "shape 1 is set only in part: {dropped:?}"); + assert!(dropped.contains(&1), "then shape 2 is too: {dropped:?}"); + // Every glyph set: nothing goes. + let whole = vec![vec![a, b], vec![a, b, c]]; + assert!(super::partial_lines(&whole, &glyphs_of).is_empty()); +} + +#[test] +fn a_compound_of_too_many_path_elements_is_not_grouped() { + // Two contours side by side, each a polygon of `k` corners. + let two = |k: usize| { + let mut p = kurbo::BezPath::new(); + for x in [0.0, 300.0] { + for i in 0..k { + let a = i as f64 / k as f64 * std::f64::consts::TAU; + let q = kurbo::Point::new(x + 100.0 + 100.0 * a.cos(), 100.0 + 100.0 * a.sin()); + if i == 0 { p.move_to(q) } else { p.line_to(q) } + } + p.close_path(); + } + p + }; + let mut budget = usize::MAX; + assert_eq!(super::split_groups(&two(100), &mut budget).len(), 2, "a small compound is grouped"); + let big = two(super::MAX_SPLIT_ELEMENTS / 2); + assert!(big.elements().len() > super::MAX_SPLIT_ELEMENTS); + let mut budget = usize::MAX; + assert!(super::split_groups(&big, &mut budget).is_empty(), "past the element limit it is matched whole only"); + assert_eq!(budget, usize::MAX, "and takes no contours from the budget"); +} + +#[test] +fn the_runs_and_near_keys_a_selection_is_looked_up_by_are_bounded() { + use kurbo::Shape; + use vectorcraft_text::retype::MAX_NEAR_KEYS; + // A compound path of `n` rectangles side by side, each of another width: every run of up to + // three of them has its own near key. + let row = |n: usize| { + let mut p = kurbo::BezPath::new(); + for i in 0..n { + let x = i as f64 * 100.0; + p.extend(kurbo::Rect::new(x, 0.0, x + 10.0 + i as f64 * 0.37, 40.0).path_elements(0.1)); + } + p + }; + // Each shape with its contour groups, as the command looks them up. + let with_groups = |shapes: &[kurbo::BezPath]| -> Vec<(kurbo::BezPath, Vec)> { + let mut budget = super::MAX_SPLIT_TOTAL_CONTOURS; + shapes.iter().map(|p| (p.clone(), super::split_groups(p, &mut budget))).collect() + }; + let keys_of = |shapes: &[(kurbo::BezPath, Vec)], tolerance: f64| { + super::lookup_keys(shapes.iter().map(|(p, g)| (p, g.as_slice())), tolerance) + }; + let few = with_groups(&[row(20)]); + assert_eq!(super::runs(&few[0].1).len(), 20 + 19 + 18, "every run of 1 to 3 groups"); + let (_, keys) = keys_of(&few, 0.01); + assert!(keys.len() > 20, "{}", keys.len()); + assert!(keys_of(&few, 0.0).1.is_empty(), "no near keys without a tolerance"); + // Past 256 groups the shape is matched whole only, and past 512 contours its contours are not + // grouped at all. + assert!(all_groups(&row(300)).is_empty()); + assert_eq!(all_groups(&row(256)).len(), 256); + let mut nested = kurbo::BezPath::new(); + for i in 0..200 { + // Each square with two squares inside it: 200 groups of 3 contours. + let x = i as f64 * 100.0; + nested.extend(kurbo::Rect::new(x, 0.0, x + 90.0, 90.0).path_elements(0.1)); + nested.extend(kurbo::Rect::new(x + 10.0, 10.0, x + 40.0, 40.0).path_elements(0.1)); + nested.extend(kurbo::Rect::new(x + 50.0, 50.0, x + 80.0, 80.0).path_elements(0.1)); + } + assert!(all_groups(&nested).is_empty(), "600 contours"); + assert_eq!(vectorcraft_text::retype::clusters(&nested).len(), 200, "which would make 200 groups"); + // Many shapes with many runs each: the near keys stop at the cap. + let mut shapes: Vec = (0..8).map(|k| kurbo::Affine::scale_non_uniform(1.0, 1.0 + k as f64 * 0.5) * row(200)).collect(); + let (_, keys) = keys_of(&with_groups(&shapes), 0.01); + assert_eq!(keys.len(), MAX_NEAR_KEYS); + // A plain shape selected after them still gets its own near key: whole shapes come first. + let plain = kurbo::Circle::new((0.0, 0.0), 30.0).to_path(0.1); + let key = vectorcraft_text::retype::near_key(&plain).unwrap(); + shapes.push(plain); + let (_, keys) = keys_of(&with_groups(&shapes), 0.01); + assert!(keys.contains(&key)); + // One command groups at most MAX_SPLIT_TOTAL_CONTOURS contours: the shapes after them are + // matched whole only. + let full = vec![row(256); super::MAX_SPLIT_TOTAL_CONTOURS / 256 + 1]; + let split: Vec = with_groups(&full).iter().map(|(_, g)| g.len()).collect(); + assert!(split[..split.len() - 1].iter().all(|n| *n == 256), "{split:?}"); + assert_eq!(split.last(), Some(&0)); +} + #[test] fn slight_tracking_without_spaces_is_kept() { // Tracking 3 moves the last glyph less than the layout check notices: both readings draw it. @@ -897,7 +1718,7 @@ fn lines_in(s: &Session, g: u64) -> Vec { for pc in &pieces { let cands: Vec<_> = index.matches(&pc.path).into_iter().filter(|m| m.face.id() == face).collect(); if !lines.last_mut().is_some_and(|l| super::join(l, pc, &cands)) { - lines.extend(super::new_line(pc, &cands)); + lines.extend(super::new_line(pc, &cands, 0.0)); } } lines diff --git a/crates/text/src/retype.rs b/crates/text/src/retype.rs index d7c7be71d6..0a991c669f 100644 --- a/crates/text/src/retype.rs +++ b/crates/text/src/retype.rs @@ -8,13 +8,22 @@ //! best similarity transform (a Procrustes fit) leaves a tiny leftover distance, and the whole //! outline, turned onto the shape, must then lie on it. That holds for outlines made from a font //! the app has (Create Outlines' output, and other apps' that keep each glyph's contours whole and -//! in the font's order). Near matches (other versions of a font, hinted or simplified outlines, -//! faux styles, glyphs merged into one path) are left unmatched; fuzzy identification is phase 2. +//! in the font's order). +//! +//! Near matches ([`RetypeIndex::near_matches`]) take a tolerance: the shape and the glyph are +//! both read at equal steps of length from any start and in either direction, their contours paired +//! by length, so outlines built another way (a simplified outline, a curve split in two, contours +//! in another order or direction) still fit, and [`GlyphMatch::score`] says how closely. +//! [`clusters`] splits a path holding several glyphs (a word outlined as one compound path) into +//! the contour groups that can each be a glyph. Glyphs fused into one contour are not split. use std::collections::{BTreeSet, HashMap}; use std::sync::Arc; +use std::sync::atomic::{AtomicU64, Ordering}; -use kurbo::{Affine, BezPath, CubicBez, Line, ParamCurve, ParamCurveArclen, ParamCurveNearest, PathEl, PathSeg, Point, QuadBez, Shape}; +use kurbo::{ + Affine, BezPath, CubicBez, Line, ParamCurve, ParamCurveArclen, ParamCurveArea, ParamCurveNearest, PathEl, PathSeg, Point, QuadBez, Shape, +}; use crate::{FontDb, FontFace}; @@ -42,10 +51,38 @@ const OUTLINE_TOLERANCE: f64 = 5e-3; 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; -/// The most glyphs one shape is reported to be ([`RetypeIndex::matches`]). +/// The most glyphs one shape is reported to be ([`RetypeIndex::matches`]), and the most of one face +/// [`RetypeIndex::near_matches`] reports. pub const MAX_CANDIDATES: usize = 16; /// How far apart two shapes' spread-to-length ratios may be and still be compared. const INVARIANT_SLACK: f64 = 0.02; +/// The largest tolerance [`RetypeIndex::near_matches`] takes, and the leftover distance at which +/// [`GlyphMatch::score`] reaches 0. +pub const MAX_TOLERANCE: f64 = 0.05; +/// Points sampled along each contour of a shape for near matching, per point of a glyph's. +const NEAR_OVERSAMPLE: usize = 4; +/// Shapes (and glyphs) with more contours than this are not near matched. +const MAX_NEAR_CONTOURS: usize = 8; +/// How far apart two shapes' spread-to-length ratios may be and still be near matched. +const NEAR_INVARIANT_SLACK: f64 = 0.1; +/// How far apart two shapes' area-to-squared-length ratios may be and still be near matched. +const NEAR_COMPACT_SLACK: f64 = 0.15; +/// Glyphs drawn to check whether they could near match a shape, at most. +const MAX_NEAR_CHECKS: usize = 262_144; +/// Glyphs indexed for near matching, at most. +const MAX_NEAR_GLYPHS: usize = 32_768; +/// Near keys [`RetypeIndex::build_near`] looks glyphs up by, at most: past them, the rest are left +/// out. +pub const MAX_NEAR_KEYS: usize = 1024; +/// Point comparisons near matching makes in all, over every shape of one index: past them, shapes +/// are not near matched. +const NEAR_WORK: u64 = 2_000_000_000; +/// Point comparisons near matching makes for one shape, at most. +const NEAR_SHAPE_WORK: u64 = 50_000_000; +/// Glyphs one shape is near matched against, at most: those closest to it in proportion. +const NEAR_FITS: usize = 256; +/// Contours [`clusters`] reads, at most: past it, none. +const MAX_CLUSTER_CONTOURS: usize = 1024; /// One glyph an outlined shape matched. #[derive(Clone)] @@ -62,10 +99,18 @@ pub struct GlyphMatch { pub origin: Point, /// The leftover distance, relative to the shape's spread. pub residual: f64, + /// Whether the shape is exactly the glyph ([`RetypeIndex::matches`]) rather than nearly + /// ([`RetypeIndex::near_matches`]). + pub exact: bool, /// How many contours the glyph has. pub contours: usize, /// Other poses the glyph fits the shape in equally (a glyph that looks the same turned). pub alts: Vec, + /// The leftover distance of each of [`Self::alts`], in the same order. + pub alt_residuals: Vec, + /// The leftover distance of the glyph's own pose: [`Self::residual`] is the least over all its + /// poses, which orders the matches. + pub pose_residual: f64, } /// Where and how a glyph lies: its size, turn and origin. @@ -80,13 +125,28 @@ pub struct Pose { } impl GlyphMatch { + /// How closely the glyph fits the shape: 1 for an exact match, and for a near one from 1 down + /// to 0 at a leftover distance of [`MAX_TOLERANCE`] or more. + pub fn score(&self) -> f64 { + if self.exact { + return 1.0; + } + if !self.residual.is_finite() { + return 0.0; + } + (1.0 - self.residual / MAX_TOLERANCE).clamp(0.0, 1.0) + } + /// This match in each pose it fits: its own first, then [`Self::alts`]. pub fn poses(&self) -> impl Iterator + '_ { - let own = Pose { scale: self.scale, angle: self.angle, origin: self.origin }; - std::iter::once(own).chain(self.alts.iter().copied()).map(move |p| GlyphMatch { + let own = (Pose { scale: self.scale, angle: self.angle, origin: self.origin }, self.pose_residual); + let alts = self.alts.iter().copied().zip(self.alt_residuals.iter().copied().chain(std::iter::repeat(self.residual))); + std::iter::once(own).chain(alts).map(move |(p, residual)| GlyphMatch { scale: p.scale, angle: p.angle, origin: p.origin, + residual, + pose_residual: residual, ..self.clone() }) } @@ -102,6 +162,8 @@ struct Signature { spread: f64, /// `spread` / the total contour length: unchanged by scale, turn and move. invariant: f64, + /// Each contour's length. + lens: Vec, } struct Entry { @@ -111,6 +173,9 @@ struct Entry { sig: Signature, /// The glyph's outline in font units, for the whole-outline check. outline: BezPath, + /// Whether the glyph was indexed for its exact key: only these take part in exact matching, + /// and the ones indexed only for near matching ([`RetypeIndex::near_matches`]) do not. + exact: bool, } /// The glyphs of a set of faces, ready to match shapes against. @@ -120,6 +185,10 @@ pub struct RetypeIndex { entries: Vec, /// Entry indices by contour count, sorted by invariant. buckets: HashMap>, + /// Entry indices by face id and glyph id. + by_glyph: HashMap<(u32, u32), usize>, + /// The point comparisons [`Self::near_matches`] may still make, over every shape. + near_work: AtomicU64, } impl RetypeIndex { @@ -128,6 +197,32 @@ impl RetypeIndex { /// 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: &BTreeSet) -> Self { + Self::build_near(db, faces, keys, &[]) + } + + /// [`Self::build`], also indexing for [`Self::near_matches`] the glyphs that could near match + /// a shape with one of the [`near_key`]s in `near` (the first [`MAX_NEAR_KEYS`]): the same + /// number of contours (at most 8) and a close area-to-length ratio. At most 262,144 glyphs are + /// drawn to check that, and at most 32,768 indexed for it, each face taking an equal share of + /// both; of a face's glyphs, those closest in that ratio are indexed. + pub fn build_near(db: &FontDb, faces: impl IntoIterator>, keys: &BTreeSet, near: &[NearKey]) -> Self { + Self::build_near_within(db, faces, keys, near, MAX_NEAR_GLYPHS) + } + + /// [`Self::build_near`] indexing at most `near_glyphs` glyphs for near matching. + fn build_near_within( + db: &FontDb, + faces: impl IntoIterator>, + keys: &BTreeSet, + near: &[NearKey], + near_glyphs: usize, + ) -> Self { + let near: Vec = near + .iter() + .copied() + .filter(|k| k.contours <= MAX_NEAR_CONTOURS && k.compact.is_finite() && k.compact > 0.0) + .take(MAX_NEAR_KEYS) + .collect(); let mut kept: Vec> = vec![]; for f in faces { if kept.len() >= MAX_FACES { @@ -137,19 +232,52 @@ impl RetypeIndex { kept.push(f); } } + // Each face gets an equal share of the near checks and entries, so a face with many glyphs + // (a CJK face) does not use them up before the faces after it. + let share = |cap: usize| cap / kept.len().max(1); let mut entries = vec![]; 'faces: for (fi, face) in kept.iter().enumerate() { - for &(gid, ch, key) in face_keys(db, face).iter() { + let mut face_checks = 0usize; + // The face's glyphs that could near match, with how far their key is from the closest + // wanted one: the closest share of them are indexed, whatever their glyph order. + let keep = share(near_glyphs); + let mut close: Vec<(f64, u32, char, BezPath)> = vec![]; + let trim = |close: &mut Vec<(f64, u32, char, BezPath)>| { + close.sort_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1))); + close.truncate(keep); + }; + for &(gid, ch, key, count) 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 'faces; } - if !keys.contains(&key) { + if keys.contains(&key) { + let outline = db.outline_uncached(face, gid); + if let Some(sig) = signatures(&outline, false).into_iter().next() { + entries.push(Entry { face: fi, gid, ch, sig, outline, exact: true }); + } continue; } + let count = usize::from(count); + if keep == 0 || face_checks >= share(MAX_NEAR_CHECKS) || !near.iter().any(|k| k.contours == count) { + continue; + } + face_checks += 1; let outline = db.outline_uncached(face, gid); + let Some(g) = near_key(&outline) else { continue }; + let Some(d) = near.iter().filter(|k| k.close_to(&g)).map(|k| k.apart(&g)).min_by(f64::total_cmp) else { continue }; + close.push((d, gid, ch, outline)); + if close.len() >= keep.saturating_mul(2) { + trim(&mut close); + } + } + trim(&mut close); + for (_, gid, ch, outline) in close { + if entries.len() >= MAX_GLYPHS { + break 'faces; + } if let Some(sig) = signatures(&outline, false).into_iter().next() { - entries.push(Entry { face: fi, gid, ch, sig, outline }); + entries.push(Entry { face: fi, gid, ch, sig, outline, exact: false }); } } } @@ -163,7 +291,39 @@ impl RetypeIndex { x.total_cmp(&y) }); } - RetypeIndex { faces: kept, entries, buckets } + let by_glyph = entries.iter().enumerate().filter_map(|(i, e)| Some(((kept.get(e.face)?.id(), e.gid), i))).collect(); + RetypeIndex { faces: kept, entries, buckets, by_glyph, near_work: AtomicU64::new(NEAR_WORK) } + } + + /// `m` kept to the poses in which the glyph paints the area `shape` paints, the shape filled + /// by the nonzero rule when `nonzero` is set and by the even-odd rule otherwise, the glyph + /// filled by the nonzero rule. Two outlines on each other can paint different areas: a ring + /// whose inner contour runs the same way as its outer one, filled by the nonzero rule, has no + /// hole. Probes lie on both sides of the shape's outline, left out where either outline is + /// within reach of them. `tolerance` is the one `m` was found with (0 for an exact match). The + /// first pose that agrees comes first; `None` when none does. + pub fn painted_agrees(&self, m: &GlyphMatch, shape: &BezPath, nonzero: bool, tolerance: f64) -> Option { + let Some(e) = self.by_glyph.get(&(m.face.id(), m.gid)).and_then(|i| self.entries.get(*i)) else { return Some(m.clone()) }; + let bb = shape.bounding_box(); + let tol = bb.width().hypot(bb.height()) * 0.5 * OUTLINE_TOLERANCE.max(3.0 * tolerance.clamp(0.0, MAX_TOLERANCE)); + let mut agreeing = m.poses().filter(|p| { + let onto = Affine::new([ + p.scale * p.angle.cos(), + p.scale * p.angle.sin(), + -p.scale * p.angle.sin(), + p.scale * p.angle.cos(), + p.origin.x, + p.origin.y, + ]); + paints_alike(shape, nonzero, &(onto * e.outline.clone()), tol) + }); + let first = agreeing.next()?; + let rest: Vec = agreeing.collect(); + Some(GlyphMatch { + alts: rest.iter().map(|p| Pose { scale: p.scale, angle: p.angle, origin: p.origin }).collect(), + alt_residuals: rest.iter().map(|p| p.pose_residual).collect(), + ..first + }) } /// The faces indexed. @@ -195,7 +355,23 @@ impl RetypeIndex { /// Every glyph `shape` fits exactly, in every face, in the order of [`RetypeIndex::matches`], /// which returns the first [`MAX_CANDIDATES`] of them. pub fn all_matches(&self, shape: &BezPath) -> Vec { - let variants = signatures(shape, true); + self.matches_read(shape, shape) + } + + /// [`Self::all_matches`], and when the shape fits no glyph as it is, its contours read the + /// other way round: for a part of a compound path, which Make Compound Path may have turned so + /// it cuts a hole in the paths behind it. + pub fn matches_either_way(&self, shape: &BezPath) -> Vec { + let found = self.matches_read(shape, shape); + if !found.is_empty() { + return found; + } + self.matches_read(shape, &shape.reverse_subpaths()) + } + + /// [`Self::all_matches`] of `shape`, its contours read as in `reading` (the same outline). + fn matches_read(&self, shape: &BezPath, reading: &BezPath) -> Vec { + let variants = signatures(reading, true); let Some(first) = variants.first() else { return vec![] }; let Some(list) = self.buckets.get(&first.contours) else { return vec![] }; let lo = first.invariant * (1.0 - INVARIANT_SLACK); @@ -210,6 +386,9 @@ impl RetypeIndex { if e.sig.invariant > hi { break; } + if !e.exact { + continue; + } let Some(face) = self.faces.get(e.face) else { continue }; // Each reading of the shape (its tied starting corners) that fits the glyph is a pose. let mut poses: Vec<(Pose, f64)> = vec![]; @@ -236,7 +415,7 @@ impl RetypeIndex { } // The upright-most pose first. poses.sort_by(|a, b| wrapped(a.0.angle).abs().total_cmp(&wrapped(b.0.angle).abs())); - let Some(((pose, _), rest)) = poses.split_first() else { continue }; + let Some(((pose, own_residual), rest)) = poses.split_first() else { continue }; let residual = poses.iter().map(|(_, r)| *r).fold(f64::INFINITY, f64::min); let m = GlyphMatch { face: face.clone(), @@ -246,8 +425,11 @@ impl RetypeIndex { angle: pose.angle, origin: pose.origin, residual, + exact: true, contours: e.sig.contours, alts: rest.iter().map(|(p, _)| *p).collect(), + alt_residuals: rest.iter().map(|(_, r)| *r).collect(), + pose_residual: *own_residual, }; out.push((e.face, m)); } @@ -257,6 +439,500 @@ impl RetypeIndex { }); out.into_iter().map(|(_, m)| m).collect() } + + /// The glyphs `shape` (in document space) nearly is: every glyph of the index with as many + /// contours whose best fit leaves a leftover distance of at most `tolerance` (relative to the + /// shape's spread, at most [`MAX_TOLERANCE`]) and whose whole outline, turned onto the shape, + /// lies within three times the tolerance of it (at least 0.5%). The shape and the glyph are + /// read from any start, in either direction, with their contours paired by length, so the + /// shape need not be built as the glyph is. Upright ones first, then the closest; the other + /// turns a glyph fits in (at most 7) are kept in [`GlyphMatch::alts`]; at most + /// [`MAX_CANDIDATES`] for each face. Empty for a tolerance of 0 or less, and for shapes of more + /// than 8 contours. A shape is compared with at most the 256 glyphs closest to it in proportion + /// (the spread-to-length ratio, and the share of the length each contour has), closest first, + /// in at most 50 million point comparisons, and the index's calls make at most 2 billion in + /// all (ranking a glyph by proportion counts as one per contour and one more); once they are + /// spent, the glyphs not yet ranked or compared are left out. + pub fn near_matches(&self, shape: &BezPath, tolerance: f64) -> Vec { + // NaN too. + if tolerance.is_nan() || tolerance <= 0.0 { + return vec![]; + } + let tolerance = tolerance.min(MAX_TOLERANCE); + // With no comparisons left, no shape is sampled or ranked. + if self.near_work.load(Ordering::Acquire) == 0 { + return vec![]; + } + let Some(s) = near_signature(shape) else { return vec![] }; + if !self.buckets.contains_key(&s.contours) { + return vec![]; + } + let tol = s.spread * OUTLINE_TOLERANCE.max(3.0 * tolerance); + let mut shape_flat: Option> = None; + let mut out: Vec<(usize, GlyphMatch)> = vec![]; + // This call's allowance comes out of the index's budget in one step, so calls that overlap + // never spend the same comparisons; what it leaves unspent goes back afterward. + let mut seen = self.near_work.load(Ordering::Acquire); + let mut work = loop { + let take = seen.min(NEAR_SHAPE_WORK); + match self.near_work.compare_exchange_weak(seen, seen - take, Ordering::AcqRel, Ordering::Acquire) { + Ok(_) => break take, + Err(now) => seen = now, + } + }; + let near = self.ranked_near(&s, &mut work); + for (_, e) in near { + if work == 0 { + break; + } + let Some(face) = self.faces.get(e.face) else { continue }; + let fits = near_fit(&e.sig, &s, tolerance, &mut work); + let flat = shape_flat.get_or_insert_with(|| Flat::new(shape, tol)); + let Some(flat) = flat.as_ref() else { break }; + // Every turn the glyph fits the shape at, nearest upright first: the first is its + // pose, the others its alternatives (a glyph that looks alike turned). + let mut poses = + fits.into_iter().filter(|f| f.1 <= tolerance).filter_map(|(z, r)| Some((near_pose(e, &s, z, flat, tol)?, r))).take(MAX_NEAR_POSES); + let Some((pose, residual)) = poses.next() else { continue }; + let rest: Vec<(Pose, f64)> = poses.collect(); + let m = GlyphMatch { + face: face.clone(), + gid: e.gid, + ch: e.ch, + scale: pose.scale, + angle: pose.angle, + origin: pose.origin, + residual, + exact: false, + contours: e.sig.contours, + alts: rest.iter().map(|(p, _)| *p).collect(), + alt_residuals: rest.iter().map(|(_, r)| *r).collect(), + pose_residual: residual, + }; + out.push((e.face, m)); + } + self.near_work.fetch_add(work, Ordering::AcqRel); + let upright = |m: &GlyphMatch| wrapped(m.angle).abs() >= 1e-3; + out.sort_by(|a, b| { + upright(&a.1).cmp(&upright(&b.1)).then(a.1.residual.total_cmp(&b.1.residual)).then(a.0.cmp(&b.0)).then(a.1.ch.cmp(&b.1.ch)) + }); + keep_per_face(&mut out); + out.into_iter().map(|(_, m)| m).collect() + } + + /// The glyphs of as many contours as near-matching shape `s` that are closest to it in + /// proportion, at most [`NEAR_FITS`], closest first: their spread-to-length ratio, and the + /// share of the length each contour has, longest first. Ranking a glyph takes one comparison + /// per contour, and one more, from `work`; once it is spent, the glyphs not yet ranked are + /// left out. + fn ranked_near(&self, s: &Signature, work: &mut u64) -> Vec<(f64, &Entry)> { + let Some(list) = self.buckets.get(&s.contours) else { return vec![] }; + let lo = s.invariant * (1.0 - NEAR_INVARIANT_SLACK); + let hi = s.invariant * (1.0 + NEAR_INVARIANT_SLACK); + let start = list.partition_point(|i| self.entries.get(*i).is_some_and(|e| e.sig.invariant < lo)); + // Each contour's share of the length, longest first (a near-matched shape has at most + // MAX_NEAR_CONTOURS contours, and the glyphs ranked as many). + let shares = |sig: &Signature| { + let mut v = [0.0f64; MAX_NEAR_CONTOURS]; + let total: f64 = sig.lens.iter().sum(); + for (o, l) in v.iter_mut().zip(&sig.lens) { + *o = l / total; + } + v.sort_by(|a, b| b.total_cmp(a)); + v + }; + let own = shares(s); + let apart = |e: &Entry| { + let d = (e.sig.invariant - s.invariant).abs() / s.invariant + shares(&e.sig).iter().zip(&own).map(|(a, b)| (a - b).abs()).sum::(); + if d.is_finite() { d } else { f64::INFINITY } + }; + let cost = s.contours as u64 + 1; + let mut near: Vec<(f64, &Entry)> = vec![]; + for e in list.iter().skip(start).filter_map(|i| self.entries.get(*i)).take_while(|e| e.sig.invariant <= hi) { + let Some(left) = work.checked_sub(cost) else { break }; + *work = left; + near.push((apart(e), e)); + } + if near.len() > NEAR_FITS { + near.select_nth_unstable_by(NEAR_FITS, |a, b| a.0.total_cmp(&b.0)); + near.truncate(NEAR_FITS); + } + near.sort_by(|a, b| a.0.total_cmp(&b.0)); + near + } +} + +/// Turns of one glyph [`RetypeIndex::near_matches`] keeps, at most: its pose and its alternatives. +const MAX_NEAR_POSES: usize = 8; + +/// `out` (each match with its face's index) without the matches past the first +/// [`MAX_CANDIDATES`] of each face, so faces drawn alike (copies of one font under other names) +/// do not crowd out the glyphs of the faces after them. +fn keep_per_face(out: &mut Vec<(usize, GlyphMatch)>) { + let mut seen: HashMap = HashMap::new(); + out.retain(|(face, _)| { + let n = seen.entry(*face).or_default(); + *n += 1; + *n <= MAX_CANDIDATES + }); +} + +/// Entry `e`'s pose on near-matching shape `s` at scaled turn `z`, when its whole outline, so +/// placed, lies within `tol` of the shape's (`flat`). +fn near_pose(e: &Entry, s: &Signature, z: (f64, f64), flat: &Flat, tol: f64) -> Option { + let gc = mul(z, e.sig.centre); + let origin = Point::new(s.centre.x - gc.x, s.centre.y - gc.y); + let onto = Affine::new([z.0, z.1, -z.1, z.0, origin.x, origin.y]); + Flat::new(&(onto * e.outline.clone()), tol).is_some_and(|g| g.agrees(flat)).then(|| Pose { scale: z.0.hypot(z.1), angle: z.1.atan2(z.0), origin }) +} + +/// What near matching compares first: a shape's number of contours and its area over its squared +/// length, both unchanged by scale, turn and move, and close for outlines drawn alike. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct NearKey { + pub contours: usize, + pub compact: f64, +} + +impl NearKey { + /// Could shapes with these keys near match? + fn close_to(&self, other: &NearKey) -> bool { + self.contours == other.contours && self.apart(other) <= NEAR_COMPACT_SLACK + } + + /// How far apart the two keys' area-to-squared-length ratios are, relative to the larger. + fn apart(&self, other: &NearKey) -> f64 { + (self.compact - other.compact).abs() / self.compact.max(other.compact) + } +} + +/// `path`'s [`NearKey`]; `None` past the size caps, for an empty shape or one of more than 8 +/// contours. +pub fn near_key(path: &BezPath) -> Option { + let (cs, _, _) = measured(path)?; + if cs.is_empty() || cs.len() > MAX_NEAR_CONTOURS { + return None; + } + let (mut area, mut len) = (0.0, 0.0); + for (c, lens) in &cs { + area += c.iter().map(|s| s.signed_area()).sum::().abs(); + len += lens.iter().sum::(); + } + let compact = area / (len * len); + (compact.is_finite() && compact > 0.0).then_some(NearKey { contours: cs.len(), compact }) +} + +/// `path` read for near matching: points at equal steps of length along each contour from where +/// it starts, [`NEAR_OVERSAMPLE`] times as many as a glyph's. `None` for more than 8 contours. +fn near_signature(path: &BezPath) -> Option { + let (cs, _, accuracy) = measured(path)?; + if cs.is_empty() || cs.len() > MAX_NEAR_CONTOURS { + return None; + } + let starts = vec![vec![0usize]; cs.len()]; + let pick = vec![0usize; cs.len()]; + sample(&cs, &starts, &pick, accuracy, SAMPLES * NEAR_OVERSAMPLE) +} + +/// The scaled turn that best takes glyph `g`'s points onto near-matching shape `s`'s (read by +/// [`near_signature`]), whatever contour each starts at, in either direction and order, and the +/// leftover distance relative to the shape's spread. The longest contours are paired first and give +/// the turns to try (spread over the circle when that pair fits about as well at many). Under each +/// turn, every glyph contour takes the shape contour, start and direction closest to it, and the +/// turn is fitted again over them all, for up to [`REFITS`] rounds. The turns that fit about +/// equally well (a glyph that looks alike turned) and within `limit` are kept, the one nearest +/// upright first ([`fits_within`]). Each point comparison is taken from `work`; none when it runs +/// out before the first turn is fitted, and the turns fitted so far when it runs out after. +fn near_fit(g: &Signature, s: &Signature, limit: f64, work: &mut u64) -> Vec<((f64, f64), f64)> { + fits_within(near_turns(g, s, work).unwrap_or_default(), limit) +} + +/// The turns [`near_fit`] fits, each with its leftover distance. +fn near_turns(g: &Signature, s: &Signature, work: &mut u64) -> Option> { + let nc = g.contours; + let m = SAMPLES * NEAR_OVERSAMPLE; + if nc == 0 || s.contours != nc || g.pts.len() != nc * SAMPLES || s.pts.len() != nc * m || g.lens.len() != nc || s.lens.len() != nc { + return None; + } + // Comparisons the longest pair's every start and direction take, and one round of pairing + // every contour and fitting the turn. + let first_cost = (4 * m * SAMPLES) as u64; + let round_cost = ((nc * (nc + 1) * m + 20 * nc) * SAMPLES) as u64; + let Some(left) = work.checked_sub(first_cost) else { + *work = 0; + return None; + }; + *work = left; + let by_len = |lens: &[f64]| { + let mut o: Vec = (0..lens.len()).collect(); + o.sort_by(|a, b| lens.get(*b).copied().unwrap_or(0.0).total_cmp(&lens.get(*a).copied().unwrap_or(0.0))); + o + }; + let (go, so) = (by_len(&g.lens), by_len(&s.lens)); + let (&ga, &sa) = (go.first()?, so.first()?); + let gpts = |c: usize| g.pts.get(c * SAMPLES..(c + 1) * SAMPLES); + let spts = |c: usize| s.pts.get(c * m..(c + 1) * m); + // The point of shape contour `b` matched to glyph sample j, from start k (in samples, and + // between two samples read on the line joining them), forward or back. + let at = |b: &[Point], k: f64, j: usize, back: bool| { + let step = (j * NEAR_OVERSAMPLE) as f64; + let x = (if back { k - step } else { k + step }).rem_euclid(m as f64); + let i = (x.floor() as usize).min(m - 1); + let (p, q) = (b.get(i).copied().unwrap_or(Point::ZERO), b.get((i + 1) % m).copied().unwrap_or(Point::ZERO)); + p.lerp(q, x - i as f64) + }; + // The fit of glyph contour `a` onto shape contour `b` from start k: Σ b·conj(a) and Σ|a|². + let sums = |a: &[Point], b: &[Point], k: f64, back: bool| { + let (mut re, mut im, mut aa) = (0.0, 0.0, 0.0); + for (j, p) in a.iter().enumerate() { + let q = at(b, k, j, back); + re += q.x * p.x + q.y * p.y; + im += q.y * p.x - q.x * p.y; + aa += p.x * p.x + p.y * p.y; + } + (re, im, aa) + }; + let sse = |a: &[Point], b: &[Point], k: f64, back: bool, z: (f64, f64)| { + a.iter().enumerate().map(|(j, p)| at(b, k, j, back).distance_squared(mul(z, *p))).sum::() + }; + // Every start and direction of the longest pair, with its own best turn. + let (a0, b0) = (gpts(ga)?, spts(sa)?); + let mut firsts: Vec<(f64, (f64, f64))> = vec![]; + for back in [false, true] { + for k in 0..m { + let (re, im, aa) = sums(a0, b0, k as f64, back); + if aa <= 0.0 || !aa.is_finite() { + return None; + } + let z = (re / aa, im / aa); + firsts.push((sse(a0, b0, k as f64, back, z), z)); + } + } + firsts.sort_by(|a, b| a.0.total_cmp(&b.0)); + let best = firsts.first()?.0; + // The turns that fit the longest pair about as well as the best. A longest contour that is + // nearly round fits at every angle, and only the other contours tell the turns apart, so + // besides the turns nearest upright, the best in each of TURN_BINS equal ranges of angle is + // tried, at most MAX_TURNS of them, evenly by angle. + const TURN_BINS: usize = 64; + const MAX_TURNS: usize = 24; + let window = best * 1.5 + 1e-12 * s.spread * s.spread; + let mut bins: Vec> = vec![None; TURN_BINS]; + for &(e, z) in firsts.iter().take_while(|(e, _)| *e <= window) { + let ang = z.1.atan2(z.0); + if !ang.is_finite() { + continue; + } + let b = (((ang + std::f64::consts::PI) / std::f64::consts::TAU * TURN_BINS as f64) as usize).min(TURN_BINS - 1); + if let Some(slot) = bins.get_mut(b) + && slot.is_none_or(|(old, _)| e < old) + { + *slot = Some((e, z)); + } + } + // First the best turn and up to five others nearest upright, told apart by their angle, then + // the spread ones. + let mut turns: Vec<(f64, f64)> = firsts.first().map(|f| f.1).into_iter().collect(); + let mut upright: Vec<(f64, f64)> = firsts.iter().take_while(|(e, _)| *e <= window).map(|(_, z)| *z).collect(); + upright.sort_by(|a, b| wrapped(a.1.atan2(a.0)).abs().total_cmp(&wrapped(b.1.atan2(b.0)).abs())); + for z in upright { + if turns.len() >= 6 { + break; + } + let ang = z.1.atan2(z.0); + if turns.iter().all(|t| wrapped(t.1.atan2(t.0) - ang).abs() > 0.05) { + turns.push(z); + } + } + // One contour: every start of it was tried above, and its own fit is final. + if nc > 1 { + let spread: Vec<(f64, f64)> = bins.into_iter().flatten().map(|(_, z)| z).collect(); + let step = spread.len().div_ceil(MAX_TURNS).max(1); + turns.extend(spread.into_iter().step_by(step)); + } + let rounds = if nc > 1 { REFITS } else { 1 }; + let n = (nc * SAMPLES) as f64; + // Each glyph contour, longest first, takes the shape contour, start and direction that fit it + // best under turn `z`; then the turn is fitted over every pair. The new turn and the leftover + // distance; `None` inside for a fit with nothing to go on, and `None` for malformed readings. + let pair_and_fit = |z: (f64, f64)| -> Option> { + let mut free: Vec = so.clone(); + let mut pairs: Vec<(usize, usize, f64, bool)> = vec![]; + for &gc in &go { + let a = gpts(gc)?; + let mut pick: Option<(f64, usize, f64, bool)> = None; + for (fi, &sc) in free.iter().enumerate() { + let b = spts(sc)?; + for back in [false, true] { + for k in 0..m { + let e = sse(a, b, k as f64, back, z); + if pick.is_none_or(|p| e < p.0) { + pick = Some((e, fi, k as f64, back)); + } + } + } + } + let (mut e, fi, mut k, back) = pick?; + let sc = free.remove(fi); + // The start between samples: the shape's samples need not fall where the glyph's do. + let b = spts(sc)?; + let k0 = k; + for d in 1..16 { + let kd = k0 - 0.5 + d as f64 / 16.0; + let ed = sse(a, b, kd, back, z); + if ed < e { + (e, k) = (ed, kd); + } + } + pairs.push((gc, sc, k, back)); + } + let (mut re, mut im, mut aa) = (0.0, 0.0, 0.0); + for &(gc, sc, k, back) in &pairs { + let (r, i, a) = sums(gpts(gc)?, spts(sc)?, k, back); + re += r; + im += i; + aa += a; + } + if aa <= 0.0 || !aa.is_finite() { + return Some(None); + } + let z = (re / aa, im / aa); + let mut total = 0.0; + for &(gc, sc, k, back) in &pairs { + total += sse(gpts(gc)?, spts(sc)?, k, back, z); + } + let rel = (total / n).sqrt() / s.spread; + Some(rel.is_finite().then_some((z, rel))) + }; + let mut results: Vec<((f64, f64), f64)> = vec![]; + 'turns: for z0 in turns { + // Each glyph contour is paired under the turn, the turn is fitted again over the pairs, + // and the contours are paired again under the new turn, until it settles: a turn that + // starts a little off (a round longest contour) moves onto the one the other contours set. + let mut z = z0; + let mut fitted: Option<((f64, f64), f64)> = None; + for _ in 0..rounds { + let Some(left) = work.checked_sub(round_cost) else { + *work = 0; + results.extend(fitted); + break 'turns; + }; + *work = left; + let Some((nz, rel)) = pair_and_fit(z)? else { break }; + let settled = (nz.0 - z.0).hypot(nz.1 - z.1) <= 1e-6 * z.0.hypot(z.1); + if fitted.is_none_or(|f| rel <= f.1) { + fitted = Some((nz, rel)); + } + z = nz; + if settled { + break; + } + } + results.extend(fitted); + } + Some(results) +} + +/// Of fitted turns and their leftover distances, those that fit about as well as the best (within +/// 1.5 times its distance) and within `limit`, nearest upright first, one per turn (turns less +/// than 0.001 apart are one); the best one alone when none is within `limit`. +fn fits_within(results: Vec<((f64, f64), f64)>, limit: f64) -> Vec<((f64, f64), f64)> { + let best = results.iter().map(|r| r.1).fold(f64::INFINITY, f64::min); + let cap = (best * 1.5 + 1e-9).min(limit.max(best)); + let turn = |r: &((f64, f64), f64)| r.0.1.atan2(r.0.0); + let mut kept: Vec<((f64, f64), f64)> = results.into_iter().filter(|r| r.1 <= cap).collect(); + kept.sort_by(|a, b| wrapped(turn(a)).abs().total_cmp(&wrapped(turn(b)).abs()).then(a.1.total_cmp(&b.1))); + let mut out: Vec<((f64, f64), f64)> = vec![]; + for r in kept { + if out.iter().all(|o| wrapped(turn(o) - turn(&r)).abs() >= 1e-3) { + out.push(r); + } + } + out +} + +/// Rounds of pairing and fitting [`near_fit`] gives each turn, at most. +const REFITS: usize = 4; + +/// Anchor points of a contour [`clusters`] tests against a candidate container, at most. +const MAX_CONTAINMENT_PROBES: usize = 8; + +/// `path`'s contours in groups that can each be one glyph: a contour with the contours that lie +/// inside it (a letter's counters, and what lies inside those), in the order the outer contours +/// come. A path of several glyphs (a word outlined as one compound path) gives one group per glyph +/// part (an i gives two: its stem and its dot). Empty past 1,024 contours. +pub fn clusters(path: &BezPath) -> Vec { + let mut subs: Vec = vec![]; + for el in path.elements() { + match el { + PathEl::MoveTo(_) => { + if subs.len() >= MAX_CLUSTER_CONTOURS { + return vec![]; + } + subs.push(BezPath::new()); + if let Some(s) = subs.last_mut() { + s.push(*el); + } + } + _ => match subs.last_mut() { + Some(s) => s.push(*el), + None => return vec![], + }, + } + } + let boxes: Vec = subs.iter().map(Shape::bounding_box).collect(); + // Up to MAX_CONTAINMENT_PROBES of each contour's anchor points, evenly spread: a contour lies in + // another only when every one of them does. + let probes: Vec> = subs + .iter() + .map(|s| { + let anchors: Vec = s.elements().iter().filter_map(|e| e.end_point()).collect(); + let step = anchors.len().div_ceil(MAX_CONTAINMENT_PROBES).max(1); + anchors.into_iter().step_by(step).collect() + }) + .collect(); + // Each contour's container: the smallest contour around it. + let parent: Vec> = (0..subs.len()) + .map(|i| { + let (Some(bi), Some(pi)) = (boxes.get(i), probes.get(i)) else { return None }; + if pi.is_empty() { + return None; + } + (0..subs.len()) + .filter(|&j| j != i) + .filter_map(|j| Some((j, boxes.get(j)?, subs.get(j)?))) + .filter(|(_, bj, sj)| { + bj.area() > bi.area() + && bj.x0 <= bi.x0 + && bj.y0 <= bi.y0 + && bj.x1 >= bi.x1 + && bj.y1 >= bi.y1 + && pi.iter().all(|p| sj.winding(*p) != 0) + }) + .min_by(|a, b| a.1.area().total_cmp(&b.1.area())) + .map(|(j, _, _)| j) + }) + .collect(); + let root = |mut i: usize| { + // Containers are strictly larger, so the chain ends; the bound keeps it so. + for _ in 0..subs.len() { + match parent.get(i).copied().flatten() { + Some(p) => i = p, + None => break, + } + } + i + }; + let mut groups: Vec<(usize, BezPath)> = vec![]; + for (i, s) in subs.iter().enumerate() { + let r = root(i); + match groups.iter_mut().find(|(g, _)| *g == r) { + Some((_, p)) => p.extend(s.iter()), + None => groups.push((r, s.clone())), + } + } + groups.sort_by_key(|(r, _)| *r); + groups.into_iter().map(|(_, p)| p).collect() } /// `a` wrapped into −π..π. @@ -264,11 +940,11 @@ fn wrapped(a: f64) -> f64 { (a + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) - std::f64::consts::PI } -/// 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>; +/// A face's glyphs as the index looks them up: (glyph, character, [`shape_key`], contours), one per +/// glyph under the lowest character that maps to it. +type FaceKeys = Arc>; -/// How many glyph keys [`face_keys`] keeps in all (16 bytes each). +/// How many glyph keys [`face_keys`] keeps in all (24 bytes each). const KEPT_KEYS: usize = 1 << 20; /// The faces' keys [`face_keys`] keeps, for the database generation they were read in. @@ -306,7 +982,14 @@ fn face_keys(db: &FontDb, face: &FontFace) -> FaceKeys { } 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 keys: FaceKeys = Arc::new( + gids.into_iter() + .filter_map(|(gid, ch)| { + let (key, count) = key_and_count(&db.outline_uncached(face, gid))?; + Some((gid, ch, key, u16::try_from(count).ok()?)) + }) + .collect(), + ); let mut k = kept.lock().unwrap_or_else(|e| e.into_inner()); if k.generation != generation { return keys; @@ -475,6 +1158,186 @@ impl Flat { } } +/// Shape segments [`paints_alike`] probes beside, at most. +const MAX_FILL_PROBES: usize = 512; + +/// Do `shape` (filled by the nonzero rule when `nonzero` is set, else by the even-odd rule) and +/// `glyph` (filled by the nonzero rule), whose outlines lie within `tol` of each other, paint the +/// same area? Each probe lies `1.5 tol` or `3 tol` to a side of the middle of a segment of the shape's +/// outline; one within `tol` of either outline is skipped, so a region narrower than that is not +/// probed. `false` when a counter of the shape (a contour inside another) is narrower than `3 tol` +/// across in some direction, however it is turned, since the probes cannot then tell whether the +/// area inside it is painted, and `false` when an outline is too large to probe. +fn paints_alike(shape: &BezPath, nonzero: bool, glyph: &BezPath, tol: f64) -> bool { + if narrow_contour(shape, 3.0 * tol) { + return false; + } + let (Some(fs), Some(fg)) = (Flat::new(shape, tol), Flat::new(glyph, tol)) else { return false }; + let step = fs.segs.len().div_ceil(MAX_FILL_PROBES).max(1); + for (s, e) in fs.segs.iter().step_by(step) { + let d = *e - *s; + let len = d.hypot(); + if len <= 0.0 || !len.is_finite() { + continue; + } + let mid = s.midpoint(*e); + // The nearer probes reach counters and gaps too narrow for the farther ones. + for reach in [1.5, 3.0] { + let side = kurbo::Vec2::new(-d.y, d.x) * (reach * tol / len); + for q in [mid + side, mid - side] { + if fs.near(q) || fg.near(q) { + continue; + } + let w = shape.winding(q); + let painted = if nonzero { w != 0 } else { w.rem_euclid(2) == 1 }; + if painted != (glyph.winding(q) != 0) { + return false; + } + } + } + } + true +} + +/// The least width of the convex hull of `path` over every direction it could be measured in, its +/// points flattened to within `tol`; 0 when the hull has fewer than three corners and when the +/// path flattens to more than [`MAX_OUTLINE_POINTS`] points. +fn hull_width(path: &BezPath, tol: f64) -> f64 { + let mut pts: Vec = vec![]; + let mut too_many = false; + kurbo::flatten(path.iter(), tol, |el| { + if let PathEl::MoveTo(q) | PathEl::LineTo(q) = el { + if pts.len() >= MAX_OUTLINE_POINTS { + too_many = true; + } else { + pts.push(q); + } + } + }); + if too_many { + return 0.0; + } + pts.sort_by(|a, b| a.x.total_cmp(&b.x).then(a.y.total_cmp(&b.y))); + // Points a thousandth of `tol` apart are one point: their chord has no direction to speak of. + let eps = tol * 1e-3; + pts.dedup_by(|b, a| a.distance(*b) <= eps); + let cross = |o: Point, a: Point, b: Point| (a - o).cross(b - o); + let mut hull: Vec = vec![]; + for pass in 0..2 { + let from = hull.len(); + let ordered: Vec = if pass == 0 { pts.clone() } else { pts.iter().rev().copied().collect() }; + for p in ordered { + while hull.len() >= from + 2 && hull.get(hull.len() - 2).zip(hull.last()).is_some_and(|(a, b)| cross(*a, *b, p) <= 0.0) { + hull.pop(); + } + hull.push(p); + } + hull.pop(); + } + if hull.len() < 3 { + return 0.0; + } + // Rotating calipers: the point farthest from each edge only moves forward from one edge to the + // next, so the whole walk visits each hull point a bounded number of times. + let n = hull.len(); + let mut least = f64::INFINITY; + let mut j = 1; + for (i, a) in hull.iter().enumerate() { + let Some(b) = hull.get((i + 1) % n) else { continue }; + let edge = *b - *a; + let len = edge.hypot(); + if len <= 0.0 || !len.is_finite() { + continue; + } + let away = |k: usize| hull.get(k % n).map_or(0.0, |p| (edge.cross(*p - *a) / len).abs()); + let mut steps = 0; + while steps < n && away(j + 1) >= away(j) { + j += 1; + steps += 1; + } + least = least.min(away(j)); + j %= n; + } + if least.is_finite() { least } else { 0.0 } +} + +/// Does `path` have a contour inside another contour (a counter) whose convex hull is no wider +/// than `width` in some direction? +fn narrow_contour(path: &BezPath, width: f64) -> bool { + if !width.is_finite() || width <= 0.0 { + return false; + } + let mut all: Vec = vec![]; + for el in path.elements() { + if matches!(el, PathEl::MoveTo(_)) { + all.push(BezPath::new()); + } + if let Some(cur) = all.last_mut() { + cur.push(*el); + } + } + all.iter().enumerate().any(|(i, c)| { + let Some(PathEl::MoveTo(at)) = c.elements().first().copied() else { return false }; + all.iter().enumerate().any(|(j, o)| j != i && o.winding(at) != 0) && hull_width(c, width / 30.0) <= width + }) +} + +/// Do `parts` (the runs of contours a compound path `shape` is split into, each already checked +/// by [`RetypeIndex::painted_agrees`] on its own) paint together the area `shape` paints, the +/// shape filled by the nonzero rule when `nonzero` is set and by the even-odd rule otherwise? Each +/// part is read by the same rule, and the glyphs that replace them are set one beside another, so +/// an overlap that the shape's rule cancels would come out painted. Probes lie on both sides of +/// every part's outline, as in [`RetypeIndex::painted_agrees`]; `tolerance` is the one the parts +/// were found with (0 for exact matches). `false` when an outline is too large to probe, and when +/// two parts' boxes overlap in a strip no wider than `3 tol`, which the probes cannot see into. +pub fn parts_paint_alike(shape: &BezPath, parts: &[BezPath], nonzero: bool, tolerance: f64) -> bool { + let bb = shape.bounding_box(); + let tol = bb.width().hypot(bb.height()) * 0.5 * OUTLINE_TOLERANCE.max(3.0 * tolerance.clamp(0.0, MAX_TOLERANCE)); + let painted = |p: &BezPath, q: Point| { + let w = p.winding(q); + if nonzero { w != 0 } else { w.rem_euclid(2) == 1 } + }; + let mut flats = vec![]; + for p in parts.iter().chain(std::iter::once(shape)) { + let Some(f) = Flat::new(p, tol) else { return false }; + flats.push(f); + } + // Two parts whose boxes overlap in a strip too thin to probe cannot be told from parts that + // overlap in their paint. + let boxes: Vec = parts.iter().map(Shape::bounding_box).collect(); + for (i, a) in boxes.iter().enumerate() { + for b in boxes.iter().skip(i + 1) { + let both = a.intersect(*b); + if both.area() > 0.0 && both.width().min(both.height()) <= 3.0 * tol { + return false; + } + } + } + for own in flats.iter().take(parts.len()) { + let step = own.segs.len().div_ceil(MAX_FILL_PROBES).max(1); + for (s, e) in own.segs.iter().step_by(step) { + let d = *e - *s; + let len = d.hypot(); + if len <= 0.0 || !len.is_finite() { + continue; + } + let mid = s.midpoint(*e); + for reach in [1.5, 3.0] { + let side = kurbo::Vec2::new(-d.y, d.x) * (reach * tol / len); + for q in [mid + side, mid - side] { + if flats.iter().any(|f| f.near(q)) { + continue; + } + if painted(shape, q) != parts.iter().any(|o| painted(o, q)) { + return false; + } + } + } + } + } + true +} + /// Do outlines `a` and `b` (in one space) lie on each other: every point of each, flattened, within /// `tol` of the other? `false` when either is too large to check. #[cfg(test)] @@ -580,6 +1443,11 @@ fn contours(path: &BezPath) -> Option>> { /// each of its contours has, in order, as [`contours`] reads them, hashed. Glyphs whose key no /// selected shape has are not sampled. `None` past the size caps or for an empty shape. pub fn shape_key(path: &BezPath) -> Option { + key_and_count(path).map(|(k, _)| k) +} + +/// [`shape_key`] and the number of contours it counts. +fn key_and_count(path: &BezPath) -> Option<(u64, usize)> { let cs = contours(path)?; if cs.is_empty() { return None; @@ -592,32 +1460,14 @@ pub fn shape_key(path: &BezPath) -> Option { h = h.wrapping_mul(0x0000_0100_0000_01b3); } } - Some(h) + Some((h, cs.len())) } /// `path` read for matching: one reading per choice of starting corners (the corner of each contour /// farthest from the centre of all the shape's corners; every tied corner when `all_ties`, else the /// first), at most [`MAX_VARIANTS`] tried. Empty for a shape with nothing to match. fn signatures(path: &BezPath, all_ties: bool) -> Vec { - let bounds = path.bounding_box(); - let size = bounds.width().hypot(bounds.height()); - if !size.is_finite() || size <= 0.0 { - return vec![]; - } - let accuracy = size * 1e-6; - let Some(cs) = contours(path) else { return vec![] }; - // Each contour with length, with its segments' lengths. - let cs: Vec<(Vec, Vec)> = cs - .into_iter() - .map(|c| { - let lens: Vec = c.iter().map(|s| s.arclen(accuracy)).collect(); - (c, lens) - }) - .filter(|(_, lens)| { - let len: f64 = lens.iter().sum(); - len.is_finite() && len > size * 1e-9 - }) - .collect(); + let Some((cs, size, accuracy)) = measured(path) else { return vec![] }; let corners: Vec = cs.iter().flat_map(|(c, _)| c.iter().map(|s| s.start())).collect(); if corners.is_empty() { return vec![]; @@ -644,7 +1494,7 @@ fn signatures(path: &BezPath, all_ties: bool) -> Vec { break; } tried += 1; - if let Some(sig) = sample(&cs, &starts, &pick, accuracy) { + if let Some(sig) = sample(&cs, &starts, &pick, accuracy, SAMPLES) { out.push(sig); } // The next choice of tied corners (a counter in mixed radix). @@ -662,19 +1512,47 @@ fn signatures(path: &BezPath, all_ties: bool) -> Vec { out } -/// One reading of the contours `cs`, contour `i` starting at segment `starts[i][pick[i]]`. -fn sample(cs: &[(Vec, Vec)], starts: &[Vec], pick: &[usize], accuracy: f64) -> Option { - let mut pts = Vec::with_capacity(cs.len() * SAMPLES); +/// `path`'s contours as [`contours`] reads them, each with its segments' lengths, leaving out +/// contours of no length, the shape's size (its bounding box's diagonal) and the accuracy lengths +/// are measured to (a millionth of the size). `None` past the size caps and for a shape of no size. +#[allow(clippy::type_complexity)] +fn measured(path: &BezPath) -> Option<(Vec<(Vec, Vec)>, f64, f64)> { + let bounds = path.bounding_box(); + let size = bounds.width().hypot(bounds.height()); + if !size.is_finite() || size <= 0.0 { + return None; + } + let accuracy = size * 1e-6; + let cs = contours(path)? + .into_iter() + .map(|c| { + let lens: Vec = c.iter().map(|s| s.arclen(accuracy)).collect(); + (c, lens) + }) + .filter(|(_, lens)| { + let len: f64 = lens.iter().sum(); + len.is_finite() && len > size * 1e-9 + }) + .collect(); + Some((cs, size, accuracy)) +} + +/// One reading of the contours `cs`, contour `i` starting at segment `starts[i][pick[i]]`, `n` +/// points along each. +fn sample(cs: &[(Vec, Vec)], starts: &[Vec], pick: &[usize], accuracy: f64, n: usize) -> Option { + let mut pts = Vec::with_capacity(cs.len() * n); + let mut lens_out = Vec::with_capacity(cs.len()); let mut total = 0.0; for (((c, lens), s), p) in cs.iter().zip(starts).zip(pick) { let from = *s.get(*p)?; let m = c.len(); let len: f64 = lens.iter().sum(); total += len; + lens_out.push(len); let (mut k, mut before) = (0usize, 0.0); let at_k = |k: usize| (from + k) % m; - for j in 0..SAMPLES { - let at = len * j as f64 / SAMPLES as f64; + for j in 0..n { + let at = len * j as f64 / n as f64; while k + 1 < m && before + lens.get(at_k(k)).copied().unwrap_or(0.0) < at { before += lens.get(at_k(k)).copied().unwrap_or(0.0); k += 1; @@ -696,7 +1574,7 @@ fn sample(cs: &[(Vec, Vec)], starts: &[Vec], pick: &[usize] if !spread.is_finite() || spread <= 0.0 || !total.is_finite() || total <= 0.0 { return None; } - Some(Signature { contours: cs.len(), pts, centre, spread, invariant: spread / total }) + Some(Signature { contours: cs.len(), pts, centre, spread, invariant: spread / total, lens: lens_out }) } /// The first reading of `path` (for tests). @@ -899,6 +1777,500 @@ mod tests { assert!(t0.elapsed() < std::time::Duration::from_secs(5), "{:?}", t0.elapsed()); } + /// `p` flattened into lines `tol` from its curves, each contour started at its second point and + /// run backwards: a simplified outline built nothing like the original. + fn simplified(p: &BezPath, tol: f64) -> BezPath { + let mut contours: Vec> = vec![]; + kurbo::flatten(p.iter(), tol, |el| match el { + PathEl::MoveTo(q) => contours.push(vec![q]), + PathEl::LineTo(q) => { + if let Some(c) = contours.last_mut() { + c.push(q); + } + } + _ => {} + }); + let mut out = BezPath::new(); + for mut c in contours { + c.rotate_left(1); + c.reverse(); + out.move_to(c[0]); + for q in &c[1..] { + out.line_to(*q); + } + out.close_path(); + } + out + } + + fn near_residual(glyph: &BezPath, shape: &BezPath) -> f64 { + near_fit_all(&signature(glyph).unwrap(), &near_signature(shape).unwrap()).map_or(f64::INFINITY, |(_, r)| r) + } + + /// [`near_fit`]'s first fit, with no limit on its work. + fn near_fit_all(g: &Signature, s: &Signature) -> Option<((f64, f64), f64)> { + let mut work = u64::MAX; + near_fit(g, s, MAX_TOLERANCE, &mut work).into_iter().next() + } + + /// [`fits_within`]'s first fit. + fn nearest_upright(results: Vec<((f64, f64), f64)>, limit: f64) -> Option<((f64, f64), f64)> { + fits_within(results, limit).into_iter().next() + } + + #[test] + fn a_simplified_outline_near_matches_and_another_shape_does_not() { + let xf = Affine::translate((300.0, 200.0)) * Affine::rotate(0.4) * Affine::scale(0.024); + let shape = xf * simplified(&letter(), 2.0); + assert_ne!(shape_key(&shape), shape_key(&letter()), "built another way: the exact index never samples it"); + assert!(best_fit(&letter(), &shape) > TOLERANCE, "no exact match"); + let r = near_residual(&letter(), &shape); + assert!(r < 0.005, "near match {r}"); + let (z, _) = near_fit_all(&signature(&letter()).unwrap(), &near_signature(&shape).unwrap()).unwrap(); + // With too little work left, no fit, and nothing left after. + let mut work = 1000; + assert!(near_fit(&signature(&letter()).unwrap(), &near_signature(&shape).unwrap(), MAX_TOLERANCE, &mut work).is_empty()); + assert_eq!(work, 0); + assert!((z.0.hypot(z.1) - 0.024).abs() < 0.024 * 0.01 && (z.1.atan2(z.0) - 0.4).abs() < 0.01, "{z:?}"); + let (a, b) = (near_key(&letter()).unwrap(), near_key(&shape).unwrap()); + assert!(a.close_to(&b), "{a:?} {b:?}"); + // The exact reading of the letter itself near matches it too, with next to nothing left over. + assert!(near_residual(&letter(), &letter()) < 1e-3); + // A box is not near a circle. + let mut o = BezPath::new(); + o.extend(kurbo::Circle::new((200.0, -350.0), 300.0).path_elements(0.1)); + let mut box_ = BezPath::new(); + box_.extend(kurbo::Rect::new(0.0, -700.0, 600.0, 0.0).path_elements(0.1)); + assert!(near_residual(&o, &box_) > MAX_TOLERANCE); + assert!(!near_key(&o).unwrap().close_to(&near_key(&box_).unwrap())); + } + + #[test] + fn a_turned_round_glyph_near_matches_upright() { + // A circle fits at every turn: the turn kept is the upright one. + let mut o = BezPath::new(); + o.extend(kurbo::Circle::new((0.0, 0.0), 300.0).path_elements(0.1)); + let (z, r) = near_fit_all(&signature(&o).unwrap(), &near_signature(&simplified(&o, 1.0)).unwrap()).unwrap(); + assert!(r < 0.005, "{r}"); + assert!(z.1.atan2(z.0).abs() < 0.06, "{z:?}"); + } + + #[test] + fn the_upright_turn_is_kept_only_within_the_limit() { + let turned = |a: f64, r: f64| ((a.cos(), a.sin()), r); + // The best fit is turned a quarter round; an upright one fits nearly as well. + let fits = || vec![turned(std::f64::consts::FRAC_PI_2, 0.010), turned(0.0, 0.014)]; + let angle = |f: Option<((f64, f64), f64)>| f.map(|((c, s), _)| s.atan2(c)); + assert_eq!(angle(nearest_upright(fits(), 0.02)), Some(0.0), "both within the limit: the upright one"); + let kept = nearest_upright(fits(), 0.012).unwrap(); + assert!((kept.1 - 0.010).abs() < 1e-12, "only the best is within the limit: the best {kept:?}"); + let kept = nearest_upright(fits(), 0.005).unwrap(); + assert!((kept.1 - 0.010).abs() < 1e-12, "none within the limit: the best {kept:?}"); + assert!(nearest_upright(vec![], 0.05).is_none()); + // Every turn within the limit is kept, upright first, once each. + let all = fits_within( + vec![turned(std::f64::consts::PI, 0.011), turned(std::f64::consts::FRAC_PI_2, 0.010), turned(0.0, 0.014), turned(1e-4, 0.0141)], + 0.02, + ); + let angles: Vec = all.iter().map(|((c, s), _)| s.atan2(*c)).collect(); + assert_eq!(angles.len(), 3, "{angles:?}"); + assert!(angles[0] == 0.0 && (angles[1] - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "{angles:?}"); + } + + #[test] + fn a_curve_split_in_two_still_near_matches() { + // letter() with its cubic split at its middle: exactly another shape, nearly the same. + let (a, b) = kurbo::CubicBez::new((0.0, -700.0), (300.0, -700.0), (450.0, -600.0), (450.0, -480.0)).subdivide(); + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.line_to((0.0, -700.0)); + p.curve_to(a.p1, a.p2, a.p3); + p.curve_to(b.p1, b.p2, b.p3); + p.quad_to((450.0, -300.0), (120.0, -300.0)); + p.line_to((120.0, 0.0)); + p.close_path(); + p.move_to((120.0, -600.0)); + p.line_to((120.0, -400.0)); + p.quad_to((330.0, -400.0), (330.0, -500.0)); + p.quad_to((330.0, -600.0), (120.0, -600.0)); + p.close_path(); + assert_ne!(shape_key(&p), shape_key(&letter())); + let shape = Affine::translate((40.0, 90.0)) * Affine::scale(0.03) * p; + assert!(near_residual(&letter(), &shape) < 0.005); + } + + #[test] + fn contours_in_another_order_still_near_match() { + // letter() with its counter first, simplified. + let l = letter(); + let els = l.elements(); + let second = els.iter().rposition(|e| matches!(e, PathEl::MoveTo(_))).unwrap(); + let mut back = BezPath::new(); + back.extend(els[second..].iter().copied()); + back.extend(els[..second].iter().copied()); + let shape = Affine::translate((40.0, 90.0)) * Affine::rotate(0.3) * Affine::scale(0.03) * simplified(&back, 2.0); + assert!(near_residual(&letter(), &shape) < 0.005); + } + + #[test] + fn copies_of_a_font_do_not_crowd_out_the_faces_after_them() { + use crate::test_fonts::{HEIGHT_FAMILY, HEIGHT_OUTSIDE_FAMILY, HEIGHTLESS_FAMILY, NUMBERLESS_FAMILY}; + // Source Sans 3 Regular and four copies of it under other names: each face has several + // glyphs drawn as the period (. · ∙ ․), more than MAX_CANDIDATES in all. + let db = FontDb::with_font_dirs(vec![]); + for bytes in [ + crate::test_fonts::height_font(true), + crate::test_fonts::height_font(false), + crate::test_fonts::height_outside_font(), + crate::test_fonts::numberless_font(), + ] { + db.add_font(bytes.unwrap()); + } + let faces: Vec> = ["Source Sans 3", HEIGHT_FAMILY, HEIGHTLESS_FAMILY, HEIGHT_OUTSIDE_FAMILY, NUMBERLESS_FAMILY] + .iter() + .map(|f| db.face(f, "Regular").unwrap()) + .collect(); + let gid = faces[0].mapped_chars().iter().find(|(c, _)| *c == '.').map(|(_, g)| *g).unwrap(); + let period = Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * db.outline_uncached(&faces[0], gid); + let has_every_period = |found: &[GlyphMatch]| faces.iter().all(|f| found.iter().any(|m| m.ch == '.' && m.face.id() == f.id())); + let keys: BTreeSet = shape_key(&period).into_iter().collect(); + let index = RetypeIndex::build(&db, faces.clone(), &keys); + // The parts of a split compound path are looked up in every face, as whole shapes are. + let found = index.matches_either_way(&period); + assert!(found.len() > MAX_CANDIDATES && has_every_period(&found), "exact: {} matches", found.len()); + let simple = simplified(&period, 0.05); + let index = RetypeIndex::build_near(&db, faces.clone(), &BTreeSet::new(), &[near_key(&simple).unwrap()]); + let found = index.near_matches(&simple, 0.02); + assert!(found.len() > MAX_CANDIDATES && has_every_period(&found), "near: {} matches", found.len()); + } + + #[test] + fn a_face_past_its_near_share_keeps_the_glyphs_closest_to_the_shapes() { + // One glyph of the face is indexed for near matching: the one closest to a simplified o, + // whatever comes before the o in glyph order. + let db = FontDb::global(); + let face = db.face("Source Sans 3", "Regular").unwrap(); + let gid = face.mapped_chars().iter().find(|(c, _)| *c == 'o').map(|(_, g)| *g).unwrap(); + let shape = Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * simplified(&db.outline_uncached(&face, gid), 4.0); + let key = near_key(&shape).unwrap(); + let index = RetypeIndex::build_near_within(db, [face], &BTreeSet::new(), &[key], 1); + assert_eq!(index.len(), 1); + let found: Vec = index.near_matches(&shape, 0.02).iter().map(|m| m.ch).collect(); + // The Latin o or the Greek omicron, drawn the same. + assert!(found.iter().any(|c| matches!(c, 'o' | 'ο')), "{found:?}"); + } + + /// `p` with its first curve split in two at its middle: the same outline with another key. + fn first_curve_split(p: &BezPath) -> BezPath { + let mut out = BezPath::new(); + let (mut last, mut done) = (Point::ZERO, false); + for el in p.elements() { + match *el { + PathEl::CurveTo(a, b, c) if !done => { + let (x, y) = kurbo::CubicBez::new(last, a, b, c).subdivide(); + out.curve_to(x.p1, x.p2, x.p3); + out.curve_to(y.p1, y.p2, y.p3); + done = true; + last = c; + } + PathEl::QuadTo(a, c) if !done => { + let (x, y) = kurbo::QuadBez::new(last, a, c).subdivide(); + out.quad_to(x.p1, x.p2); + out.quad_to(y.p1, y.p2); + done = true; + last = c; + } + e => { + out.push(e); + if let PathEl::MoveTo(q) | PathEl::LineTo(q) | PathEl::QuadTo(_, q) | PathEl::CurveTo(_, _, q) = e { + last = q; + } + } + } + } + out + } + + #[test] + fn a_glyph_indexed_only_for_near_matching_is_no_exact_match() { + let db = FontDb::global(); + let face = db.face("Source Sans 3", "Regular").unwrap(); + let gid = face.mapped_chars().iter().find(|(c, _)| *c == 'o').map(|(_, g)| *g).unwrap(); + let split = first_curve_split(&db.outline_uncached(&face, gid)); + let shape = Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * split; + assert_ne!(shape_key(&shape), shape_key(&(Affine::scale(0.04) * db.outline_uncached(&face, gid))), "another key"); + let keys: BTreeSet = shape_key(&shape).into_iter().collect(); + let index = RetypeIndex::build_near(db, [face], &keys, &[near_key(&shape).unwrap()]); + assert!(index.all_matches(&shape).is_empty(), "exact matching sees only the glyphs of the exact keys"); + assert!(index.matches_either_way(&shape).is_empty()); + assert!(index.near_matches(&shape, 0.02).iter().any(|m| m.ch == 'o')); + } + + #[test] + fn each_pose_of_a_match_reports_its_own_residual() { + let db = FontDb::global(); + let face = db.face("Source Sans 3", "Regular").unwrap(); + let turn = std::f64::consts::FRAC_PI_2; + let m = GlyphMatch { + face, + gid: 1, + ch: 'o', + scale: 1.0, + angle: 0.0, + origin: Point::ZERO, + residual: 0.010, + exact: false, + contours: 1, + alts: vec![Pose { scale: 1.0, angle: turn, origin: Point::ZERO }], + alt_residuals: vec![0.010], + pose_residual: 0.014, + }; + let seen: Vec<(f64, f64)> = m.poses().map(|p| (p.angle, p.residual)).collect(); + assert_eq!(seen, vec![(0.0, 0.014), (turn, 0.010)]); + } + + #[test] + fn a_ring_whose_contours_run_one_way_paints_no_hole() { + let db = FontDb::global(); + let face = db.face("Source Sans 3", "Regular").unwrap(); + let gid = face.mapped_chars().iter().find(|(c, _)| *c == 'o').map(|(_, g)| *g).unwrap(); + let ring = db.outline_uncached(&face, gid); + let els = ring.elements(); + let second = els.iter().rposition(|e| matches!(e, PathEl::MoveTo(_))).unwrap(); + let (mut outer, mut inner) = (BezPath::new(), BezPath::new()); + outer.extend(els[..second].iter().copied()); + inner.extend(els[second..].iter().copied()); + let mut same_way = outer.clone(); + same_way.extend(inner.reverse_subpaths().iter()); + let (ring, same_way) = + (Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * ring, Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * same_way); + let keys: BTreeSet = shape_key(&ring).into_iter().collect(); + let index = RetypeIndex::build_near(db, [face], &keys, &[near_key(&same_way).unwrap()]); + // Near matching reads each contour in either direction, so the outline matches the glyph's: + // only what it paints tells them apart. + let found = index.near_matches(&same_way, 0.02); + assert!(found.iter().any(|m| m.ch == 'o'), "the outline matches"); + assert!(found.iter().all(|m| index.painted_agrees(m, &same_way, true, 0.02).is_none()), "nonzero paints the counter"); + assert!(found.iter().any(|m| index.painted_agrees(m, &same_way, false, 0.02).is_some()), "even-odd leaves it empty"); + let found = index.all_matches(&ring); + assert!(found.iter().any(|m| index.painted_agrees(m, &ring, true, 0.0).is_some()), "the glyph's own windings paint a ring"); + } + + #[test] + fn a_counter_too_narrow_to_probe_does_not_count_as_agreeing() { + let square = |x0: f64, y0: f64, x1: f64, y1: f64, back: bool| { + let p = kurbo::Rect::new(x0, y0, x1, y1).to_path(0.1); + if back { p.reverse_subpaths() } else { p } + }; + // A 100 by 100 square with a 10 by 10 counter: the shape's counter runs the way its outer + // contour does (filled by the nonzero rule), the glyph's runs the other way (a hole). + let mut filled = square(0.0, 0.0, 100.0, 100.0, false); + filled.extend(square(45.0, 45.0, 55.0, 55.0, false).iter()); + let mut holed = square(0.0, 0.0, 100.0, 100.0, false); + holed.extend(square(45.0, 45.0, 55.0, 55.0, true).iter()); + let tol = 100.0_f64.hypot(100.0) * 0.5 * 3.0 * MAX_TOLERANCE; + assert!(!paints_alike(&filled, true, &holed, tol), "no probe can see the counter"); + // A counter wide enough to probe is told apart as before. + let mut wide = square(0.0, 0.0, 100.0, 100.0, false); + wide.extend(square(10.0, 10.0, 90.0, 90.0, false).iter()); + let mut wide_hole = square(0.0, 0.0, 100.0, 100.0, false); + wide_hole.extend(square(10.0, 10.0, 90.0, 90.0, true).iter()); + assert!(!paints_alike(&wide, true, &wide_hole, 1.0)); + assert!(paints_alike(&wide_hole, true, &wide_hole, 1.0)); + } + + #[test] + fn a_counter_turned_too_narrow_to_probe_does_not_count_as_agreeing() { + let square = |x0: f64, y0: f64, x1: f64, y1: f64, back: bool| { + let p = kurbo::Rect::new(x0, y0, x1, y1).to_path(0.1); + if back { p.reverse_subpaths() } else { p } + }; + // A 60 by 10 counter turned 45 degrees: its box is about 49.5 square, wider than the + // threshold of about 31.8, but it is 10 across. + let strip = |back: bool| { + let turn = Affine::translate((50.0, 50.0)) * Affine::rotate(std::f64::consts::FRAC_PI_4); + let mut p = kurbo::Rect::new(-30.0, -5.0, 30.0, 5.0).to_path(0.1); + if back { + p = p.reverse_subpaths(); + } + turn * p + }; + let mut filled = square(0.0, 0.0, 100.0, 100.0, false); + filled.extend(strip(false).iter()); + let mut holed = square(0.0, 0.0, 100.0, 100.0, false); + holed.extend(strip(true).iter()); + let tol = 100.0_f64.hypot(100.0) * 0.5 * 3.0 * MAX_TOLERANCE; + assert!(narrow_contour(&filled, 3.0 * tol)); + assert!(!paints_alike(&filled, true, &holed, tol)); + // The same strip with its sides along the axes is told apart as before. + let mut level = square(0.0, 0.0, 100.0, 100.0, false); + level.extend(square(20.0, 45.0, 80.0, 55.0, false).iter()); + assert!(narrow_contour(&level, 3.0 * tol)); + // A counter wider than the threshold in every direction is not narrow. + let mut wide = square(0.0, 0.0, 100.0, 100.0, false); + wide.extend(square(10.0, 10.0, 90.0, 90.0, false).iter()); + assert!(!narrow_contour(&wide, 3.0 * tol)); + } + + #[test] + fn a_contour_reaching_outside_a_concave_one_is_no_counter_of_it() { + // An L, and a triangle that starts inside the L and inside its box but reaches outside it. + let mut l = BezPath::new(); + for (i, (x, y)) in [(0.0, 0.0), (100.0, 0.0), (100.0, 20.0), (20.0, 20.0), (20.0, 100.0), (0.0, 100.0)].into_iter().enumerate() { + if i == 0 { l.move_to((x, y)) } else { l.line_to((x, y)) } + } + l.close_path(); + let mut tri = BezPath::new(); + tri.move_to((5.0, 5.0)); + tri.line_to((90.0, 90.0)); + tri.line_to((5.0, 50.0)); + tri.close_path(); + let mut both = l.clone(); + both.extend(tri.iter()); + assert_eq!(clusters(&both).len(), 2); + // A contour wholly inside stays with its container. + let mut inside = l.clone(); + inside.extend(kurbo::Rect::new(5.0, 5.0, 15.0, 15.0).to_path(0.1).iter()); + assert_eq!(clusters(&inside).len(), 1); + } + + #[test] + fn the_hull_width_is_the_least_width_in_any_direction() { + let strip = kurbo::Rect::new(-30.0, -5.0, 30.0, 5.0).to_path(0.1); + for deg in [0.0_f64, 17.0, 45.0, 90.0, 133.0] { + let w = hull_width(&(Affine::rotate(deg.to_radians()) * strip.clone()), 0.01); + assert!((w - 10.0).abs() < 0.05, "{deg} degrees: {w}"); + } + let circle = kurbo::Circle::new((5.0, 7.0), 50.0).to_path(0.1); + assert!((hull_width(&circle, 0.01) - 100.0).abs() < 0.5, "{}", hull_width(&circle, 0.01)); + // A triangle's least width is its shortest height. + let mut tri = BezPath::new(); + tri.move_to((0.0, 0.0)); + tri.line_to((40.0, 0.0)); + tri.line_to((0.0, 30.0)); + tri.close_path(); + assert!((hull_width(&tri, 0.01) - 24.0).abs() < 1e-9); + assert_eq!(hull_width(&BezPath::new(), 0.01), 0.0); + } + + #[test] + fn an_outline_too_large_to_probe_does_not_count_as_agreeing() { + let n = MAX_OUTLINE_POINTS + 64; + let mut many = BezPath::new(); + for i in 0..n { + let a = std::f64::consts::TAU * i as f64 / n as f64; + let p = Point::new(50.0 + 50.0 * a.cos(), 50.0 + 50.0 * a.sin()); + if i == 0 { many.move_to(p) } else { many.line_to(p) } + } + many.close_path(); + assert!(Flat::new(&many, 0.5).is_none(), "too many segments to flatten"); + assert!(!paints_alike(&many, true, &many, 0.5)); + assert!(paints_alike(&kurbo::Circle::new((50.0, 50.0), 50.0).to_path(0.1), true, &kurbo::Circle::new((50.0, 50.0), 50.0).to_path(0.1), 0.5)); + } + + #[test] + fn parts_that_overlap_under_the_even_odd_rule_do_not_paint_the_shape() { + let square = |x0: f64, y0: f64, x1: f64, y1: f64| kurbo::Rect::new(x0, y0, x1, y1).to_path(0.1); + let (a, b) = (square(0.0, 0.0, 60.0, 60.0), square(40.0, 0.0, 100.0, 60.0)); + let mut both = a.clone(); + both.extend(b.iter()); + assert!(!parts_paint_alike(&both, &[a.clone(), b.clone()], false, 0.0), "the overlap is a hole in the shape"); + assert!(parts_paint_alike(&both, &[a.clone(), b.clone()], true, 0.0), "the nonzero rule paints it"); + // Windings that cancel under the nonzero rule leave the overlap unpainted too. + let mut turned = a.clone(); + turned.extend(b.reverse_subpaths().iter()); + assert!(!parts_paint_alike(&turned, &[a.clone(), b.reverse_subpaths()], true, 0.0)); + // An overlap too thin for the probes to land in is not taken for none. + let thin = square(59.9, 0.0, 100.0, 60.0); + let mut strip = a.clone(); + strip.extend(thin.iter()); + assert!(!parts_paint_alike(&strip, &[a.clone(), thin], false, 0.0)); + let apart = square(80.0, 0.0, 140.0, 60.0); + let mut aside = a.clone(); + aside.extend(apart.iter()); + assert!(parts_paint_alike(&aside, &[a, apart], false, 0.0)); + } + + #[test] + fn near_matching_with_no_work_left_returns_nothing() { + let db = FontDb::global(); + let face = db.face("Source Sans 3", "Regular").unwrap(); + let gid = face.mapped_chars().iter().find(|(c, _)| *c == 'o').map(|(_, g)| *g).unwrap(); + let shape = Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * simplified(&db.outline_uncached(&face, gid), 4.0); + let index = RetypeIndex::build_near(db, [face], &BTreeSet::new(), &[near_key(&shape).unwrap()]); + assert!(!index.near_matches(&shape, 0.02).is_empty()); + let after = index.near_work.load(Ordering::Acquire); + assert!((NEAR_WORK - NEAR_SHAPE_WORK..NEAR_WORK).contains(&after), "one allowance at most: {after}"); + index.near_work.store(0, Ordering::Release); + assert!(index.near_matches(&shape, 0.02).is_empty()); + assert_eq!(index.near_work.load(Ordering::Acquire), 0); + } + + #[test] + fn ranking_near_match_candidates_takes_from_the_work_budget() { + let db = FontDb::global(); + let face = db.face("Source Sans 3", "Regular").unwrap(); + let gid = face.mapped_chars().iter().find(|(c, _)| *c == 'o').map(|(_, g)| *g).unwrap(); + let shape = Affine::translate((100.0, 200.0)) * Affine::scale(0.04) * simplified(&db.outline_uncached(&face, gid), 4.0); + let index = RetypeIndex::build_near(db, [face], &BTreeSet::new(), &[near_key(&shape).unwrap()]); + let s = near_signature(&shape).unwrap(); + let mut all = u64::MAX; + let ranked = index.ranked_near(&s, &mut all).len(); + assert!(ranked >= 1, "the o is ranked"); + assert_eq!(u64::MAX - all, ranked as u64 * 3, "each glyph of two contours takes three"); + // Enough to rank one glyph of two contours and nothing more. + let mut work = 4; + assert_eq!(index.ranked_near(&s, &mut work).len(), 1); + assert_eq!(work, 1); + } + + #[test] + fn a_round_glyph_with_an_off_centre_counter_near_matches_at_its_turn() { + // A round outer contour fits the shape at every turn; only the counter, off the centre, + // tells the turn, which is far from upright. + let polygon = |cx: f64, cy: f64, r: f64, n: usize, back: bool| { + let mut p = BezPath::new(); + for i in 0..n { + let a = std::f64::consts::TAU * i as f64 / n as f64 * if back { -1.0 } else { 1.0 }; + let q = Point::new(cx + r * a.cos(), cy + r * a.sin()); + if i == 0 { p.move_to(q) } else { p.line_to(q) } + } + p.close_path(); + p + }; + // The outer contour has 720 corners: as round as the samples can tell. + let mut g = polygon(0.0, 0.0, 300.0, 720, false); + g.extend(polygon(120.0, 40.0, 80.0, 24, true).iter()); + let xf = Affine::translate((500.0, 400.0)) * Affine::rotate(2.0) * Affine::scale(0.05); + let shape = xf * simplified(&g, 1.0); + let (z, r) = near_fit_all(&signature(&g).unwrap(), &near_signature(&shape).unwrap()).unwrap(); + assert!(r < 0.01, "{r}"); + assert!(wrapped(z.1.atan2(z.0) - 2.0).abs() < 0.02, "{z:?}"); + } + + #[test] + fn clusters_split_a_word_into_its_glyph_parts() { + let mut word = letter(); + word.extend((Affine::translate((600.0, 0.0)) * letter()).iter()); + // An i: a stem and a dot beside it. + word.extend(kurbo::Rect::new(1200.0, -500.0, 1260.0, 0.0).path_elements(0.1)); + word.extend(kurbo::Circle::new((1230.0, -600.0), 40.0).path_elements(0.1)); + let parts = clusters(&word); + assert_eq!(parts.len(), 4, "two letters (each with its counter), a stem and a dot"); + assert_eq!(shape_key(&parts[0]), shape_key(&letter())); + assert_eq!(shape_key(&parts[1]), shape_key(&letter())); + assert_eq!(parts[2].elements().iter().filter(|e| matches!(e, PathEl::MoveTo(_))).count(), 1); + // A counter written before its outer contour still goes with it, in the outer's place. + let mut back = BezPath::new(); + let l = letter(); + let els = l.elements(); + let second = els.iter().rposition(|e| matches!(e, PathEl::MoveTo(_))).unwrap(); + back.extend(els[second..].iter().copied()); + back.extend(els[..second].iter().copied()); + assert_eq!(clusters(&back).len(), 1); + assert!(clusters(&BezPath::new()).is_empty()); + } + #[test] fn the_same_corners_joined_another_way_are_another_outline() { let poly = |pts: &[(f64, f64)]| { diff --git a/docs/gaps.md b/docs/gaps.md index 618b48ff4c..549d74f46b 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -515,7 +515,7 @@ macOS cursor delivery (#2292) is narrowed to a native host adapter using the exi - **Missing:** CJK vertical composition (custom mojikumi sets and the Mojikumi Settings dialog, kinsoku settings #633, manual tate-chu-yoko), Middle Eastern features beyond bidi, digit types, Character Direction and Kashidas (a split caret; the Middle Eastern & South Asian composers are done, #4935), - Touch Type, Retype beyond exact matches of available fonts (phase 1 is in, #4934, and the Retype panel lists the exactly matching fonts; near matches, installed fonts not yet loaded and text in images remain), Snap to Glyph; optional preservation of font sizes during group scaling (#1034 enhancement). + Touch Type, Retype beyond exact matches of available fonts (phase 1 is in, #4934, the Retype panel lists the exactly matching fonts, and `type.retype` takes near matches within a tolerance and words outlined as one compound path; glyphs fused into one contour, near matches in the panel's list, installed fonts not yet loaded and text in images remain), Snap to Glyph; optional preservation of font sizes during group scaling (#1034 enhancement). Itemized in [type-parity.md](type-parity.md). - **Impact:** professional typography and Japanese/Chinese publishing. - **Estimate:** 34–47 h (type core 14–19 h blocks beta). diff --git a/docs/mcp.md b/docs/mcp.md index 4536d98b89..62ecf0fd2c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -11431,6 +11431,47 @@ hidden state and stay live on the group; none of them is copied onto the glyph p glyph. `object.expand` outlines type with this command, so Fill and Stroke expand those paths' gradients, patterns and strokes. +`type.retype` (Type › Retype) turns outlined glyphs back into point text. By default a shape must be exactly a glyph of +an available font. With `tolerance` (0 to 0.05), a shape of at most 8 contours that is no glyph exactly becomes the +glyph it is closest to when its leftover distance from that 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: 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. 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 +remaining groups can still all be matched, so an i's stem and dot become one i. A group whose contours run the other +way round, as Make Compound Path leaves some of them, still matches exactly. Exact matches of the whole shape come +first, then exact matches of its groups, then near matches of the whole shape, then near matches of its groups. The +compound becomes type only when every one of its glyphs does; otherwise it stays as it is and is reported once in +`unmatched` when another shape of the selection becomes type, and the command returns an error when no shape does. A +glyph also has to paint the area the shape paints under the shape's fill rule: a compound filled by the nonzero rule +whose inner contour runs the same way as its outer one paints its center, so a ring glyph does not replace it. A shape with a counter narrower than three times the +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. 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 +matched whole only. Near matching compares a shape with at most the 256 glyphs closest to it in proportion, in at +most 50 million point comparisons per shape and 2 billion per command, ranking a glyph by proportion counting as one +comparison per contour and one more; past them, the glyphs not yet ranked or compared are left out. It looks glyphs up for at most 1,024 distinct shapes and runs per command: every selected shape first, in +selection order, then the runs of groups of the compound paths. Glyphs close only to the shapes and runs past that +limit are not looked up. At most 262,144 glyphs are drawn to check whether they could near match, and at most 32,768 +are indexed for it, each face taking an equal share of both; of a face's glyphs, those closest to the selected shapes +in proportion are indexed. + +```json +{"name":"run_command","arguments":{"command":"type.retype","params":{"tolerance":0.02}}} +``` + `type.createOutlines` (Type › Create Outlines) and `effect.expandAppearance` (when it outlines type, as Expand Appearance describes) take an outlined frame of threaded text out of its thread with the text it shows, and the other frames keep the text they hold. When the thread's last frame is outlined, the text it has no room for, which its @@ -12587,7 +12628,7 @@ on an open path that are not drawn because they fall before the path's start can ## Retype tracking `type.retype {fonts?}` turns selected outlined text back into point text when its shapes are glyphs of a font the app -has. A line of three or more glyphs gets the spacing that most of its gaps share as its tracking, in 1/1000 em. Retype +has. A line of three or more exactly matched glyphs (none a near match, see `tolerance`) gets the spacing that most of its gaps share as its tracking, in 1/1000 em. Retype tries the value rounded to a whole number first when it is within 0.05 of one, then the value as measured. The font's advances and pair kerning are left out of the measure, and word spaces are counted after the tracking. The tracking is kept when the tracked type draws the whole line in place, or more of it from its start than untracked type does. When @@ -12607,7 +12648,7 @@ tracking their own gaps give. These rules are VectorCraft's own. Each entry of t ## Retype several lines -`type.retype` joins lines that follow each other in paint order, with the same group, paint, font, size, turn and +`type.retype` joins lines that follow each other in paint order (lines split from one compound path do), with the same group, paint, font, size, turn and tracking (an untracked line of one or two glyphs takes the tracking of the others), into one type object when each line's baseline is the same distance below the one before (between 0.25 and 3 times the size) and the lines line up at their starts, ends or centers. The object is area type with Convert To Area Type's frame, its lines separated by line breaks, with left, right or center alignment, checked in that order. Its @@ -12629,7 +12670,8 @@ the result's `texts` list holds an object's lines joined by `\n`, and each entry `type.retypeFonts {count?}` lists the fonts whose glyphs match the selected outlined text. It compares each selected path and compound path, inside groups too, with the glyphs of up to 96 loaded fonts, those of the document's live type -first, by the matching `type.retype` uses, and it counts only exact matches. When two versions of a font are loaded, +first, by the matching `type.retype` uses, and it counts only exact matches of whole shapes: it does not split a +compound path into glyphs or take near matches. When two versions of a font are loaded, only the version its family and style name gives is compared, which is the version that choosing the font sets. The fonts rank by how many shapes each matches, then by the mean distance left after fitting, then by family and style name. `count` is a whole number from 1 to 96 (default 10); any other value is an error. The reply is diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index a2422690d2..ba55faed15 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -3081,7 +3081,11 @@ 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. 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). +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; near matching with a tolerance (below) reads it as the glyph. 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`). Installed fonts not yet loaded and images remain (phase 2). + +## Retype near matches (2026-11-18) + +The public page orders Retype's matching fonts by how close each is to the text's appearance ([identify fonts](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/identify-fonts-using-retype.html)), so outlines need not be exactly a font's glyphs. `type.retype` takes a `tolerance` (0 to 0.05, default 0 for exact matches only). Above 0, a shape of up to 8 contours that is no glyph exactly is read at equal steps of length from any start and in either direction, its contours paired with the glyph's by length, and it becomes the glyph it is closest to when the leftover distance, relative to the shape's size, is at most the tolerance and the glyph's outline and the shape's each lie within three times the tolerance (relative to the shape's size, and at least 0.5%) of the other. 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 line in the result has a `score` from 1 (exact) to 0. A compound path holding several glyphs (a word made into one compound path) is split into groups of contours and matched in runs of up to three groups, and becomes type only when every glyph does; exact matching of those groups also reads contours that run the other way round, as Make Compound Path leaves them. Exact matches of the whole shape come first, then exact matches of its groups, then near matches. The tolerance, the score and the splitting are VectorCraft's own. Tested with outlines flattened into lines, restarted, reversed and simplified, a curve split in two, contours in another order, turned lines of round glyphs, a word made into one compound path, and a compound with a part that is no glyph (`crates/engine/src/cmd/retype_tests.rs`, `crates/text/src/retype.rs`). The Retype panel's list (below) still takes exact matches of whole shapes only. Glyphs fused into one contour, installed fonts not yet loaded and images remain. ## Retype panel (2026-11-18) diff --git a/docs/type-parity.md b/docs/type-parity.md index 111ee5d838..868b944bcd 100644 --- a/docs/type-parity.md +++ b/docs/type-parity.md @@ -56,7 +56,7 @@ against Illustrator's public documentation; **estimated** unless noted. | Alt + numeric-keypad codes while typing (Windows: `Alt+0176` → °) | partial | #5108: on Windows the character Windows posts when Alt comes up after keypad digits is typed at the caret like any other, in the typing's undo step, and the keypad digits typed with Alt run no shortcut. With NumLock off the keypad keys (Insert, End, arrows, Clear, Home, Page Up/Down without the extended-key flag) count as digits too and don't move the caret (`windows_alt_codes` unit tests; neither NumLock on nor off has been tried on a Windows machine yet). `type.insert {code}` (Windows Alt+0 code, Windows-1252 for 128–159) and `{unicode}` (hex code point) insert a character by its code (`menucmds::insert_a_character_by_its_code`). macOS and Linux have no such system convention and egui reports a keypad digit as the top-row digit, so there it does nothing; a floating panel window doesn't receive the character | | Caret: the arrows and Delete step over whole clusters (Thai consonant with its marks, combining accents, emoji); Backspace removes one character | done | in text with right-to-left characters the arrows move in visual order and stop on both sides of a character drawn with no width, such as a zero width non-joiner | | Touch Type tool | done | moves, scales and rotates one character through its kerning, baseline shift, scales and rotation (`text.touchType`); a character with a preceding logical character on the same line can move along the line in either reading direction; the first logical character moves only up and down. Positive `dx` follows reading order. These direction rules are VectorCraft's choices. | -| Retype (identify fonts in outlines/images) | partial | Phase 1 (#4934): Type › Retype (`type.retype`) turns outlined glyphs back into type when each shape is exactly a glyph of a loaded font, a font the document uses or a family named in `fonts` (the same up to uniform scale, rotation and translation): one object per line unless lines stack into one object as described below, in that font and size, at the same baseline and turn, with the first glyph's fill and stroke, in one undo step; shapes that match no font stay as they are and are reported. Evenly spaced glyphs keep their spacing as tracking (gaps one space wide stay word spaces when type draws them), and lines stacked at one leading and lined up at their starts, ends or centers become one area type object with that leading and alignment (VectorCraft's own rules, checked against the layout). Window › Retype shows the Retype panel, which lists the fonts whose glyphs exactly match at least one selected outlined shape, comparing up to 96 loaded fonts with those of the document's live type first, ranked by how many shapes each matches, with samples, and sets a listed font on selected live type (`retype.find`, `type.retypeFonts`). Missing (phase 2): near or fuzzy matches (another version of the font, simplified outlines, glyphs merged into one path), opening the panel from the Type menu, the context menu and the Properties panel's Quick Actions, filtering the list by font source and favorites, installed fonts not yet loaded, text in images. Public behaviour: [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) | +| Retype (identify fonts in outlines/images) | partial | Phase 1 (#4934): Type › Retype (`type.retype`) turns outlined glyphs back into type when each shape is exactly a glyph of a loaded font, a font the document uses or a family named in `fonts` (the same up to uniform scale, rotation and translation): one object per line unless lines stack into one object as described below, in that font and size, at the same baseline and turn, with the first glyph's fill and stroke, in one undo step; shapes that match no font stay as they are and are reported. Near matches: with `tolerance` (0 to 0.05), shapes of up to 8 contours whose leftover distance from a 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 (simplified outlines, curves split in two, contours in another order or direction) match too, and each line reports a `score`; a compound path holding several glyphs is split into them and becomes type only when all of them match. A line of three or more exactly matched glyphs keeps the spacing most of its gaps share as tracking (gaps one space wide stay word spaces when type draws them). A line of two glyphs that untracked type does not draw in place takes the tracking of the nearest tracked line in the same font, size and turn, and keeps it only when type at that tracking draws both glyphs in place; a line of one or two glyphs that joins tracked lines in one object takes their tracking. Other lines are set untracked, each glyph at its own position, and lines stacked at one leading and lined up at their starts, ends or centers become one area type object with that leading and alignment (VectorCraft's own rules, checked against the layout). Window › Retype shows the Retype panel, which lists the fonts whose glyphs exactly match at least one selected outlined shape, comparing up to 96 loaded fonts with those of the document's live type first, ranked by how many shapes each matches, with samples, and sets a listed font on selected live type (`retype.find`, `type.retypeFonts`). Missing (phase 2): glyphs fused into one contour, near matches and compound paths of several glyphs in the panel's list, opening the panel from the Type menu, the context menu and the Properties panel's Quick Actions, filtering the list by font source and favorites, installed fonts not yet loaded, text in images. Public behaviour: [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) | | Snap to Glyph | done | View › Snap to Glyph (`view.snapToGlyph`): with Smart Guides on, new point type and drawing tools snap to live-text baselines and glyph bounds, the corners of each glyph's box included, turning with rotated type (#5112; `guides::tests::a_drawn_point_lands_on_a_corner_of_a_glyphs_bounds`, `Targets::for_drawing`, `tools::text_tests::snap_to_glyph_lines_up_new_point_type_with_a_baseline`). With Alignment Guides on as well, and Snap to Grid and Snap to Pixel off, both ends of a Measure or Measure & Inspect drag snap to those guides and show them during the drag (`Targets::for_measure`, `xform::measure::tests::its_ends_land_on_glyph_guides_with_snap_to_glyph`). That the Measure tools take the guides is VectorCraft's choice; they leave out Anchor Point, which [the help page](https://helpx.adobe.com/illustrator/desktop/design-with-text/special-characters-glyphs/snap-to-glyph.html) limits to the Pen tool and objects moved with the Selection tool. Character panel menu › Show Snap to Glyph Options: Baseline, x-height, Glyph Bounds, Proximity Guides, Angular Guides and Anchor Point each turn their guides on or off (`view.snapToGlyph.options`, all on by default; #4232), after a Snap to Glyph checkbox for View › Snap to Glyph; showing them scrolls the panel to them (#5273) | | Thai, Lao, Khmer, Myanmar: shaping (marks, reordering), lines broken between words, word-by-word caret and double-click | done | words found by ICU4X's line segmenter: dictionaries on the desktop, LSTM models on the web | | Middle Eastern: bidirectional layout, Paragraph Direction | done | PDF export keeps harakat and zero-advance Noto Arabic dot glyphs in one compatible batch per layout line, with per-glyph vertical offsets (#6309). `crates/pdf/src/tests/realtext.rs` (`mark_text_*`) checks decoded cluster mappings, mixed Arabic/Latin/digits, contour placement against outline export, vertical columns, differing path rotations and PDF/X-1a/3/4. Editable-text import still needs ActualText support (#6165); separately styled or differently rotated clusters and external viewer search/copy remain outside this export-only evidence. |