mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
Merge pull request #10206 from jmverges/fix/ai-page-word-of-two-text-objects
.ai import: a word the page draws from two text objects in a row is their type
This commit is contained in:
3 files changed
+87
-8
No files matched your search
@@ -338,32 +338,78 @@ fn made_content(infos: &[SlotInfo], made: Vec<Option<Made>>, laid: bool) -> Vec<
|
||||
/// it, as far as strokes round type reach) as type the page draws. How many of the page's text
|
||||
/// objects no made type takes, shown or not: none that holds a piece of the object has its words
|
||||
/// ([`spelling`]). Past `MAX_PAIRS` comparisons nothing is marked or counted (`None`).
|
||||
///
|
||||
/// The page can draw two text objects as one word: a word and a letter set apart from it (a swash
|
||||
/// "h" after "Spirit") come out of the page as one piece, "Spirith", that neither spells. A word
|
||||
/// left that is the last word of one text object holding the piece joined to the first word of
|
||||
/// another that holds it and draws after it, along the first one's lines, is theirs
|
||||
/// ([`joined_by_two`]).
|
||||
fn see_page_type(content: &mut [Content], shown: &[Vec<Arc<Node>>]) -> Option<usize> {
|
||||
let texts = |o: &[Arc<Node>]| o.iter().filter_map(|n| if let NodeKind::Text(_) = &n.kind { Some(n.clone()) } else { None }).collect::<Vec<_>>();
|
||||
// Each object's pieces of type, and the words of them that no made type holding it has yet.
|
||||
let mut objects: Vec<(Vec<Rect>, Vec<String>, bool)> =
|
||||
shown.iter().map(|o| (texts(o).iter().filter_map(|n| n.visual_bounds()).collect(), object_words(o), false)).collect();
|
||||
let pieces: usize = objects.iter().map(|o| o.0.len()).sum();
|
||||
if content.len().saturating_mul(pieces) > MAX_PAIRS {
|
||||
return None;
|
||||
}
|
||||
let mut budget = MAX_PAIRS.checked_sub(content.len().checked_mul(pieces)?)?;
|
||||
let holds = |drawn: Rect, r: &Rect| drawn.inflate(OUTLINE_REACH, OUTLINE_REACH).intersect(*r).area() >= 0.5 * r.area().max(1e-9);
|
||||
for c in content {
|
||||
let Content::Made(node, region, seen) = c else { continue };
|
||||
// The made type holding each object, in order: where it draws and its words.
|
||||
let mut holders: Vec<Vec<Arc<Holder>>> = vec![vec![]; objects.len()];
|
||||
let mut seen_slots = Vec::new();
|
||||
for (slot, c) in content.iter().enumerate() {
|
||||
let Content::Made(node, region, _) = c else { continue };
|
||||
// Hidden made type has no region: where it draws, shown, is its own box.
|
||||
let Some(drawn) = region.or_else(|| node.visual_bounds()) else { continue };
|
||||
let spelled = if let NodeKind::Text(t) = &node.kind { spelling(&t.plain_text()) } else { String::new() };
|
||||
for (rects, words, held) in &mut objects {
|
||||
let (spelled, holder) = match &node.kind {
|
||||
NodeKind::Text(t) => {
|
||||
let text = t.plain_text();
|
||||
let [a, b, ..] = t.xf.as_coeffs();
|
||||
(spelling(&text), Some(Arc::new((drawn.center(), (a, b), word_pieces(&text).map(spelling).filter(|w| !w.is_empty()).collect()))))
|
||||
}
|
||||
_ => (String::new(), None),
|
||||
};
|
||||
for ((rects, words, held), by) in objects.iter_mut().zip(&mut holders) {
|
||||
if rects.iter().any(|r| holds(drawn, r)) {
|
||||
*held = true;
|
||||
*seen |= region.is_some();
|
||||
if region.is_some() && seen_slots.last() != Some(&slot) {
|
||||
seen_slots.push(slot);
|
||||
}
|
||||
words.retain(|w| !spelled.contains(w.as_str()));
|
||||
by.extend(holder.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
for ((_, words, _), by) in objects.iter_mut().zip(&holders) {
|
||||
if !words.is_empty() && !by.is_empty() {
|
||||
// Each unmatched word can visit every ordered pair of holders, plus the first-holder scan.
|
||||
budget = budget.checked_sub(words.len().checked_mul(by.len())?.checked_mul(by.len().checked_add(1)?)?)?;
|
||||
words.retain(|w| !joined_by_two(w, by));
|
||||
}
|
||||
}
|
||||
for slot in seen_slots {
|
||||
if let Content::Made(_, _, seen) = &mut content[slot] {
|
||||
*seen = true;
|
||||
}
|
||||
}
|
||||
Some(objects.iter().filter(|(_, words, held)| !held || !words.is_empty()).count())
|
||||
}
|
||||
|
||||
/// A made text object that holds a piece of the page's type: the centre of where it draws, the
|
||||
/// way its lines run (the first column of its transform) and its words ([`spelling`]).
|
||||
type Holder = (Point, (f64, f64), Vec<String>);
|
||||
|
||||
/// Is `word` (a [`spelling`]) the last word of one of `by` joined to the first word of another of
|
||||
/// them that draws after it, along the first one's lines? Each part is a whole word of its text
|
||||
/// object, so a piece is never made up of bits of words.
|
||||
fn joined_by_two(word: &str, by: &[Arc<Holder>]) -> bool {
|
||||
by.iter().map(Arc::as_ref).any(|(at, (a, b), first)| {
|
||||
let Some(head) = first.last().filter(|h| word.len() > h.len() && word.starts_with(h.as_str())) else { return false };
|
||||
let tail = &word[head.len()..];
|
||||
by.iter()
|
||||
.map(Arc::as_ref)
|
||||
.any(|(next, _, second)| (next.x - at.x) * a + (next.y - at.y) * b > 0.0 && second.first().is_some_and(|w| w == tail))
|
||||
})
|
||||
}
|
||||
|
||||
/// What goes into each of `infos`' slots when the file's text document can't make the type of every
|
||||
/// slot that shows: type made from the text document (`made`) where it can, else the page's type that
|
||||
/// the text document puts where the page has it, else the page's type in a copy that a layer's or
|
||||
|
||||
@@ -8663,3 +8663,35 @@ fn trusted_editing_data_keeps_an_artboard_taller_than_its_pdf_page() {
|
||||
let (_, w, read) = crate::layered_ai(&private(&art), Document::new(12000.0, 14400.0), vec![], false, true, 1.0);
|
||||
assert!(!read, "the uncapped dimension must still match: {w:?}");
|
||||
}
|
||||
|
||||
/// The page's "2014" drawn as one piece 20 points across the art, and two text objects "20" and
|
||||
/// "14" anchored at `twenty` and `fourteen`, set in the bundled Source Sans 3 at 14 points (a digit
|
||||
/// 6.958 points wide) so that where they draw doesn't hang on the fonts installed.
|
||||
fn word_of_two_text_objects(fourteen: f64, twenty: f64) -> crate::import::Imported {
|
||||
let stories = [("20\r", 0, (twenty, 8180.0), (0.0, 0.0), 0.0), ("14\r", 0, (fourteen, 8180.0), (0.0, 0.0), 0.0)];
|
||||
let doc = text_document(&stories).replacen("/0 (Helvetica)", "/0 (SourceSans3-Regular)", 1);
|
||||
let art = native_with_text(&layer("Art", true, &(text_object(0) + &text_object(1))), &doc);
|
||||
let page = "0 1 0 0 setcmykcolor /SourceSans3-Regular findfont 14 scalefont setfont 20 61.5 moveto (2014) show";
|
||||
import(&eps(page, &art)).unwrap()
|
||||
}
|
||||
|
||||
/// The page can draw two text objects as one word: "20" and "14" set one after the other come out
|
||||
/// of the page as one piece, "2014", that neither spells. The word is the last word of the one
|
||||
/// joined to the first word of the one after it, so the page's type is theirs and nothing is noted
|
||||
/// as left out.
|
||||
#[test]
|
||||
fn a_word_the_page_draws_from_two_text_objects_in_a_row_is_their_type() {
|
||||
let r = word_of_two_text_objects(8161.5 + 13.916, 8161.5);
|
||||
assert_eq!(names(&r.document), ["Art"], "{:?}", r.warnings);
|
||||
let texts: Vec<String> = text_of(&r.document.layers[0]).iter().map(|t| t.plain_text()).collect();
|
||||
assert_eq!(texts, ["20", "14"], "{:?}", r.warnings);
|
||||
assert!(!r.warnings.iter().any(|w| crate::is_loss(w)), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
/// The two text objects the other way round ("14" before "20") don't make the page's "2014": its
|
||||
/// word keeps the loss note.
|
||||
#[test]
|
||||
fn two_text_objects_the_other_way_round_arent_the_word_the_page_draws() {
|
||||
let r = word_of_two_text_objects(8161.5, 8161.5 + 13.916);
|
||||
assert!(r.warnings.iter().any(|w| w.contains("some of the page's type has no text object in the layers")), "{:?}", r.warnings);
|
||||
}
|
||||
@@ -425,6 +425,7 @@ Stop opacity on translated artboards remains outside this regression coverage.
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-30 | minor | `.ai` and EPS import: a word the page draws from two text objects in a row is their type. Where the text document makes the type of every text object that shows, a word of the page's type is the type of the made type holding its piece when that type spells it. The page can draw two text objects as one word: a word and a letter set apart from it (a swash "h" after "Spirit") come out of the page as one piece, "Spirith", that neither spells. It was counted as type with no text object, with the note "some of the page's type has no text object in the layers, so it is left out" (a loss, so `convert` wouldn't write over the file), though both text objects come in and draw it. Now a word left that is the last word of one text object holding the piece joined to the first word of another holding it that draws after it, along the first one's lines, is theirs (`joined_by_two` in `see_page_type`). Each part is a whole word of its text object, and the two the other way round don't make the word. Tests: `a_word_the_page_draws_from_two_text_objects_in_a_row_is_their_type`, `two_text_objects_the_other_way_round_arent_the_word_the_page_draws`. |
|
||||
| 2026-11-27 | minor | `.ai` save: type in a spot ink, a global process colour or Registration keeps its swatch link and its tint. Its fill and stroke in the text document name (key 5 of the paint) an art style of one paint, `0 1 Xd`, whose paint is the colour as a path's fill writes it (`x`, `Xx`, `Xk`, `Xs`), the form `.ai` import reads since the change below; key 0 still holds the process values at the tint. Each style is read back before it is written and kept only when the reader gives the same swatch and tint, so such type no longer counts among the attributes of type left out; a link to a process swatch that isn't global still does. Test `ai_editing::tests::type_in_spot_and_global_colors_reads_back_linked_at_its_tint` |
|
||||
| 2026-11-27 | minor | `.ai` and EPS import: type set in a spot ink or a global process colour keeps its ink. In the text document a character's fill or stroke is a colour (key 0: grey, RGB or CMYK) and the name of an art style (key 5). In users' files (used locally, never committed) that style is one paint (`/SimpleStyle`), always its fill's, for a stroke too; for a spot ink or a global process colour it is `c m y k (name) tint x`, `Xx` or `Xk`, and key 0 keeps only the ink's process values at that tint. Such type came in as that process colour, so it separated onto the process plates, and white type in a white ink onto none. It now takes the style's paint: the colour at its tint, linked to the document's swatch of that name, which the style's colour makes when the art doesn't (after the art's colours, before the header's custom colours). A style whose paint is a process colour or a gradient, one that also sets a stroke's colour or pattern (before or after the fill's) or another fill paint after it, one whose `Xd` doesn't turn on its fill alone, and a paint that names no style of the file, keep the colour of key 0 as before; type filled with a gradient still comes in black. Tests `type_set_in_a_spot_ink_takes_it_from_the_art_style_its_paint_names` in `crates/eps/src/import/tests_native.rs`, `a_run_painted_by_a_swatch_colours_art_style_takes_the_swatch` in `crates/eps/src/import/ate.rs` and `only_a_style_of_one_swatch_colour_holds_it_for_type` in `crates/eps/src/import/ai/style/tests.rs`. Scores and estimates unchanged. |
|
||||
| 2026-11-26 | minor | `.ai` and EPS import: where the text document makes the type of every text object that shows, point type made from it is matched with the page's type where it draws. Made type has no layout when it is matched, and its box was the estimate of type without one (0.55 em a character, running right from its anchor): beside right-aligned type, over half of centered type, across vertical type, and too short for a wide font. The page's type there was counted as type with no text object, with the note "some of the page's type has no text object in the layers, so it is left out" (a loss, so `convert` wouldn't write over the file), and the made type went on to be weighed against the page as type the page may not draw, and could be taken out. The box of made point type that shows now comes from its layout in the fonts the import has; the text object keeps no layout, so the document measures it when it opens, as before. Files where some text object's type isn't made keep the estimate: there the page's pieces where made type draws can belong to text objects stacked on it. The page comparison takes the same box for the layers' type areas (`type_areas` in `compare.rs`): made point type that differs from the page (laid out again, or in another font) counts as type where it draws, whatever its alignment. Counted as art beside the estimate, it could send the file to its PDF part or page, or be noted as art that differs. A file whose layers failed the comparison that way and opened without it now passes it and shows its notes. Tests `right_aligned_and_centered_point_type_takes_the_pages_type_where_it_draws`, `centered_point_type_at_a_heading_s_size_takes_the_pages_type_where_it_draws` and `right_aligned_point_type_that_differs_from_the_page_differs_as_type` in `crates/eps/src/import/tests_native.rs`, and `made_point_type_s_box_is_where_its_alignment_and_orientation_draw_it` in `crates/eps/src/import/native/slots.rs`. Scores and estimates unchanged. |
|
||||
|
||||
Reference in new issue
Block a user