mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 14:34:34 +00:00
Retype: a glyph in an even-odd compound matches with only some of its contours reversed
An even-odd compound paints the same whichever way each contour runs, so an o's counter may run the same way as its outer contour. Such a part matched neither as it is nor with all its contours reversed, and stayed an outline at tolerance 0. A part of a split compound is now also read with each contour turned the way its depth gives (outer contours one way, their counters the other), both ways round. In a nonzero compound those contours paint the counter, and the painted-area check still keeps the part an outline. The containment test that splits a compound into glyphs moves into its own function, `containers`, which the new reading also calls. Docs: docs/mcp.md, docs/type-parity.md.
This commit is contained in:
+174
-35
@@ -360,13 +360,25 @@ impl RetypeIndex {
|
||||
|
||||
/// [`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.
|
||||
/// it cuts a hole in the paths behind it. When neither reading fits, the contours are read the
|
||||
/// way their depth gives, outer contours one way and their counters the other, both ways round:
|
||||
/// an even-odd compound paints the same whichever way each contour runs, so an o's counter may
|
||||
/// run the same way as its outer contour.
|
||||
pub fn matches_either_way(&self, shape: &BezPath) -> Vec<GlyphMatch> {
|
||||
let found = self.matches_read(shape, shape);
|
||||
if !found.is_empty() {
|
||||
return found;
|
||||
}
|
||||
self.matches_read(shape, &shape.reverse_subpaths())
|
||||
let found = self.matches_read(shape, &shape.reverse_subpaths());
|
||||
if !found.is_empty() {
|
||||
return found;
|
||||
}
|
||||
let Some(nested) = nested_reading(shape) else { return vec![] };
|
||||
let found = self.matches_read(shape, &nested);
|
||||
if !found.is_empty() {
|
||||
return found;
|
||||
}
|
||||
self.matches_read(shape, &nested.reverse_subpaths())
|
||||
}
|
||||
|
||||
/// [`Self::all_matches`] of `shape`, its contours read as in `reading` (the same outline).
|
||||
@@ -880,39 +892,7 @@ pub fn clusters(path: &BezPath) -> Vec<BezPath> {
|
||||
},
|
||||
}
|
||||
}
|
||||
let boxes: Vec<kurbo::Rect> = 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<Vec<Point>> = subs
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let anchors: Vec<Point> = 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<Option<usize>> = (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 parent = containers(&subs);
|
||||
let root = |mut i: usize| {
|
||||
// Containers are strictly larger, so the chain ends; the bound keeps it so.
|
||||
for _ in 0..subs.len() {
|
||||
@@ -935,6 +915,90 @@ pub fn clusters(path: &BezPath) -> Vec<BezPath> {
|
||||
groups.into_iter().map(|(_, p)| p).collect()
|
||||
}
|
||||
|
||||
/// Each contour's container among `subs` (one contour each): the smallest other contour that holds
|
||||
/// it, `None` for an outer contour.
|
||||
fn containers(subs: &[BezPath]) -> Vec<Option<usize>> {
|
||||
let boxes: Vec<kurbo::Rect> = 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<Vec<Point>> = subs
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let anchors: Vec<Point> = 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.
|
||||
(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()
|
||||
}
|
||||
|
||||
/// `path` with each contour turned to run the way its depth gives (an outer contour one way, its
|
||||
/// counters the other, what lies in those the first way again), the outer contours keeping the way
|
||||
/// the first of them runs. `None` when that turns no contour (the path read as it is), for a
|
||||
/// contour that encloses no area, and for more than [`MAX_CONTOURS`] contours.
|
||||
fn nested_reading(path: &BezPath) -> Option<BezPath> {
|
||||
let mut subs: Vec<BezPath> = vec![];
|
||||
for el in path.elements() {
|
||||
if matches!(el, PathEl::MoveTo(_)) {
|
||||
if subs.len() >= MAX_CONTOURS {
|
||||
return None;
|
||||
}
|
||||
subs.push(BezPath::new());
|
||||
}
|
||||
subs.last_mut()?.push(*el);
|
||||
}
|
||||
let parent = containers(&subs);
|
||||
let depth = |mut i: usize| {
|
||||
let mut d = 0usize;
|
||||
// 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;
|
||||
d += 1;
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
d
|
||||
};
|
||||
let areas: Vec<f64> = subs.iter().map(Shape::area).collect();
|
||||
if areas.iter().any(|a| !a.is_normal()) {
|
||||
return None;
|
||||
}
|
||||
let outer = (0..subs.len()).find(|&i| depth(i) == 0).and_then(|i| areas.get(i)).map(|a| a.signum())?;
|
||||
let turned: Vec<bool> = areas.iter().enumerate().map(|(i, a)| a.signum() != if depth(i) % 2 == 0 { outer } else { -outer }).collect();
|
||||
if turned.iter().all(|t| !*t) {
|
||||
return None;
|
||||
}
|
||||
let mut out = BezPath::new();
|
||||
for (s, t) in subs.iter().zip(&turned) {
|
||||
out.extend(if *t { s.reverse_subpaths() } else { s.clone() }.iter());
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// `a` wrapped into −π..π.
|
||||
fn wrapped(a: f64) -> f64 {
|
||||
(a + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) - std::f64::consts::PI
|
||||
@@ -2271,6 +2335,81 @@ mod tests {
|
||||
assert!(clusters(&BezPath::new()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_nested_reading_turns_counters_against_their_outer_contour() {
|
||||
let split = |p: &BezPath| -> Vec<BezPath> {
|
||||
let mut out: Vec<BezPath> = vec![];
|
||||
for el in p.elements() {
|
||||
if matches!(el, PathEl::MoveTo(_)) {
|
||||
out.push(BezPath::new());
|
||||
}
|
||||
out.last_mut().unwrap().push(*el);
|
||||
}
|
||||
out
|
||||
};
|
||||
let join = |cs: &[BezPath]| {
|
||||
let mut p = BezPath::new();
|
||||
for c in cs {
|
||||
p.extend(c.iter());
|
||||
}
|
||||
p
|
||||
};
|
||||
let cs = split(&letter());
|
||||
let (outer, counter) = (cs[0].clone(), cs[1].clone());
|
||||
let (same, opposite) = if outer.area().signum() == counter.area().signum() {
|
||||
(counter.clone(), counter.reverse_subpaths())
|
||||
} else {
|
||||
(counter.reverse_subpaths(), counter.clone())
|
||||
};
|
||||
// A counter running the same way as its outer contour is turned; the outer one is kept.
|
||||
let read = nested_reading(&join(&[outer.clone(), same.clone()])).unwrap();
|
||||
let rs = split(&read);
|
||||
assert_eq!(rs[0], outer);
|
||||
assert_eq!(rs[1].area().signum(), opposite.area().signum());
|
||||
// So is the outer contour's way when it comes second.
|
||||
let read = nested_reading(&join(&[opposite.clone(), outer.reverse_subpaths()])).unwrap();
|
||||
let rs = split(&read);
|
||||
assert_eq!(rs[1], outer.reverse_subpaths());
|
||||
assert_eq!(rs[0].area().signum(), outer.area().signum());
|
||||
// Already nested so: nothing to turn.
|
||||
assert!(nested_reading(&join(&[outer.clone(), opposite])).is_none());
|
||||
// A contour enclosing no area gives none.
|
||||
let mut flat = BezPath::new();
|
||||
flat.move_to((0.0, 0.0));
|
||||
flat.line_to((10.0, 0.0));
|
||||
flat.close_path();
|
||||
assert!(nested_reading(&join(&[outer, flat])).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_part_with_some_contours_turned_still_matches_exactly() {
|
||||
let db = FontDb::with_font_dirs(vec![]);
|
||||
assert_eq!(db.add_font(crate::test_fonts::named_copy(false, ("Turntest", "Regular", "Version 1.000", "Turntest-Regular")).unwrap()), 1);
|
||||
let face = db.face("Turntest", "Regular").unwrap();
|
||||
let o = Affine::translate((40.0, 90.0)) * Affine::scale(0.03) * db.outline_uncached(&face, face.glyph_for('o'));
|
||||
let mut cs: Vec<BezPath> = vec![];
|
||||
for el in o.elements() {
|
||||
if matches!(el, PathEl::MoveTo(_)) {
|
||||
cs.push(BezPath::new());
|
||||
}
|
||||
cs.last_mut().unwrap().push(*el);
|
||||
}
|
||||
assert_eq!(cs.len(), 2, "an o is two contours");
|
||||
let keys = std::iter::once(shape_key(&o).unwrap()).collect();
|
||||
let index = RetypeIndex::build(&db, vec![face], &keys);
|
||||
assert!(!index.all_matches(&o).is_empty());
|
||||
for turned in 0..2 {
|
||||
let mut shape = BezPath::new();
|
||||
for (i, c) in cs.iter().enumerate() {
|
||||
shape.extend(if i == turned { c.reverse_subpaths() } else { c.clone() }.iter());
|
||||
}
|
||||
assert!(index.all_matches(&shape).is_empty(), "contour {turned} turned is no glyph as it is");
|
||||
assert!(index.all_matches(&shape.reverse_subpaths()).is_empty(), "nor read the other way round");
|
||||
let found = index.matches_either_way(&shape);
|
||||
assert!(found.iter().any(|m| m.ch == 'o'), "contour {turned} turned still matches the o");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_same_corners_joined_another_way_are_another_outline() {
|
||||
let poly = |pts: &[(f64, f64)]| {
|
||||
|
||||
Reference in New Issue
Block a user