mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 09:54:09 +01:00
Retype: a compound path of more than 65,536 path elements is not split into glyphs
Splitting a compound path copies its contours and compares each with the others. A selected compound of a few contours with a very large number of path elements was split however many elements it had. Such a shape is now matched whole only, like one of more than 256 groups or 512 contours.
This commit is contained in:
1 parent
b237da4672
commit
d1859cf03f
3 files changed
+36
-7
No files matched your search
@@ -48,7 +48,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Retype",
|
||||
["Type"],
|
||||
None,
|
||||
"{fonts?: [family…], tolerance?: 0..0.05 (default 0: exact matches only)} turn the selected outlined text (glyph paths and compound paths, inside groups too) back into live type: each shape is matched against the glyphs of the fonts the app has loaded, the document's fonts and the families in fonts, the same up to uniform scale, rotation and translation; with a tolerance above 0, a shape that is no glyph exactly may nearly be one (an outline simplified, a curve split in two, contours in another order, start or direction) when its leftover distance from the glyph, relative to the shape's outline spread (the root mean square distance of its sampled outline points from their center), is at most the tolerance (VectorCraft's own measure; shapes of at most 8 contours); a compound path holding several glyphs (a word outlined as one path, at most 256 contour groups and 512 contours, and 16,384 contours per command) is split into them, and becomes type only when every one of them matches; matched glyphs become point text in that font and size at the same baseline, one object per line unless lines stack into one (below), with the first glyph's fill, stroke, opacity and blend mode, in one undo step; a line of three or more exactly matched glyphs takes the spacing most of its gaps share as its tracking (pair kerning left out) when tracked type draws all of it in place, or more of it than untracked type (when untracked type also draws it, untracked type is kept if the tracking is under one unit or the tracked type would drop a gap one space wide (a b c keeps its spaces), and the tracked type otherwise), and negative tracking only when the line is opaque, Normal, with a solid fill or none and no stroke (its glyphs may overlap); lines that follow each other in paint order with the same group, paint, font, size, turn and tracking (an untracked line of one or two glyphs takes the others' tracking), each one the same leading (0.25 to 3 times the size) below the one before, and lined up at their starts, ends or centers become one area type object with that leading (Auto when Auto gives it) and left, right or center alignment, or one multi-line point type object when area type (after Text Wrap) would not draw the outlines' glyphs in their places or when checking the Text Wrap objects on the changed layers would copy more than 2,000,000 anchors, subpaths and shapes; where even that type would move a glyph, the lines before that glyph's line stay one object and the rest are joined again from that line; lines with an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke stay one object each; a line splits where its type would not draw the outlines' glyphs in their places (uneven spacing, manual kerning, font substitutions; with a tolerance, within it), and before a near-matched glyph the line's size and turn would draw further from its shape than the tolerance; a glyph must also paint the area the shape paints under its fill rule (a nonzero compound whose inner contour runs the same way as its outer one paints its center, so a ring glyph does not replace it; a counter narrower than three times the outline tolerance across in some direction, an outline too large to check, or parts of a split compound that overlap where its fill rule leaves the overlap unpainted, also keep the shape an outline); shapes that match no font, glyphs type cannot draw as they are, and shapes with their own opacity mask stay as they are → {ids (the new type), texts (lines joined by \\n), fonts: [{family, style, size, tracking (1/1000 em), leading (pt, null for Auto or one line), area (bool), score (how closely the line's least close glyph fits, 1 for exact, 0 at a leftover distance of 0.05)}], unmatched: [id…] (shapes that stayed as they are when another shape became type), truncated (shapes past the limits of 2048 shapes or 16 nested groups were left out)}; an error, with no unmatched list, when no shape becomes type, or when tolerance is not a number from 0 to 0.05",
|
||||
"{fonts?: [family…], tolerance?: 0..0.05 (default 0: exact matches only)} turn the selected outlined text (glyph paths and compound paths, inside groups too) back into live type: each shape is matched against the glyphs of the fonts the app has loaded, the document's fonts and the families in fonts, the same up to uniform scale, rotation and translation; with a tolerance above 0, a shape that is no glyph exactly may nearly be one (an outline simplified, a curve split in two, contours in another order, start or direction) when its leftover distance from the glyph, relative to the shape's outline spread (the root mean square distance of its sampled outline points from their center), is at most the tolerance (VectorCraft's own measure; shapes of at most 8 contours); a compound path holding several glyphs (a word outlined as one path, at most 256 contour groups, 512 contours and 65,536 path elements, and 16,384 contours per command) is split into them, and becomes type only when every one of them matches; matched glyphs become point text in that font and size at the same baseline, one object per line unless lines stack into one (below), with the first glyph's fill, stroke, opacity and blend mode, in one undo step; a line of three or more exactly matched glyphs takes the spacing most of its gaps share as its tracking (pair kerning left out) when tracked type draws all of it in place, or more of it than untracked type (when untracked type also draws it, untracked type is kept if the tracking is under one unit or the tracked type would drop a gap one space wide (a b c keeps its spaces), and the tracked type otherwise), and negative tracking only when the line is opaque, Normal, with a solid fill or none and no stroke (its glyphs may overlap); lines that follow each other in paint order with the same group, paint, font, size, turn and tracking (an untracked line of one or two glyphs takes the others' tracking), each one the same leading (0.25 to 3 times the size) below the one before, and lined up at their starts, ends or centers become one area type object with that leading (Auto when Auto gives it) and left, right or center alignment, or one multi-line point type object when area type (after Text Wrap) would not draw the outlines' glyphs in their places or when checking the Text Wrap objects on the changed layers would copy more than 2,000,000 anchors, subpaths and shapes; where even that type would move a glyph, the lines before that glyph's line stay one object and the rest are joined again from that line; lines with an opacity below 100%, a blend mode other than Normal, a gradient or pattern fill or a stroke stay one object each; a line splits where its type would not draw the outlines' glyphs in their places (uneven spacing, manual kerning, font substitutions; with a tolerance, within it), and before a near-matched glyph the line's size and turn would draw further from its shape than the tolerance; a glyph must also paint the area the shape paints under its fill rule (a nonzero compound whose inner contour runs the same way as its outer one paints its center, so a ring glyph does not replace it; a counter narrower than three times the outline tolerance across in some direction, an outline too large to check, or parts of a split compound that overlap where its fill rule leaves the overlap unpainted, 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
|
||||
)]
|
||||
@@ -1201,13 +1201,18 @@ const MAX_SPLIT_CONTOURS: usize = 512;
|
||||
/// 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, and for more contours than
|
||||
/// `budget` holds. A path of two or more contours takes them from `budget` when it is grouped.
|
||||
/// [`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<BezPath> {
|
||||
// Grouping compares every contour with every other: count them first.
|
||||
// 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 {
|
||||
if !(2..=MAX_SPLIT_CONTOURS).contains(&n) || n > *budget || path.elements().len() > MAX_SPLIT_ELEMENTS {
|
||||
return vec![];
|
||||
}
|
||||
*budget -= n;
|
||||
|
||||
@@ -1565,6 +1565,30 @@ fn a_shape_that_lost_a_glyph_drops_the_lines_of_its_other_glyphs() {
|
||||
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;
|
||||
|
||||
+2
-2
@@ -11340,8 +11340,8 @@ counter 10 units wide in a 100 by 100 shape, or a 60 by 10 one turned 45 degrees
|
||||
another, so a split whose parts overlap where the shape's fill rule leaves the overlap unpainted (two o's on each
|
||||
other under the even-odd rule) stays an outline. When the split with the longest runs fails that check, the split with the
|
||||
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 or 512 contours
|
||||
is matched whole only. One command groups at most 16,384 contours, in selection order; the shapes after them are
|
||||
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; past them, the glyphs not yet compared are left
|
||||
out. It looks glyphs up for at most 1,024 distinct shapes and runs per command: every selected shape first, in
|
||||
|
||||
Reference in new issue
Block a user