mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
.ai import: made point type is matched with the page's type where it draws
Type made from a file's text document has no layout when the import matches it with the page's type, so the box it was matched by was the estimate of type without a layout: as wide as an average font would set it, running right from its anchor. Right-aligned point type draws on the other side of its anchor, centered type lies half outside that box, and vertical type runs down the page across it. Where the text document makes the type of every text object that shows (`made_only`), the page's type there was then nobody's: the import noted "some of the page's type has no text object in the layers, so it is left out" for type that is in the layers, and the made type, not seen on the page, was weighed against the page as type the page may not draw, and sometimes taken out. `made_only` (`crates/eps/src/import/native/plan.rs`) now takes the box of made point type that shows from its layout (`drawn_box`), in the fonts the import has, as the document measures it once it is open. The node itself keeps no layout, so the document's type is measured when it opens, as before. Area type and type on a path are matched by their frame and path, as before. Where some text object's type isn't made (`plan`), the box stays the estimate: there the page's pieces in the box where made type draws can belong to text objects stacked on it whose type isn't made, and the made type would take them all. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b7dd67ac0
commit
d0dc0c41b1
2 files changed
+88
-7
No files matched your search
@@ -145,7 +145,8 @@ pub(super) fn made_only(
|
||||
warn: &mut Vec<String>,
|
||||
) -> Vec<Content> {
|
||||
let [shown, hidden] = pages;
|
||||
let mut content = made_content(infos, made);
|
||||
// Every text object that shows has made type: each is matched with the page's type where it draws.
|
||||
let mut content = made_content(infos, made, true);
|
||||
// The page's type where no made type draws, in the copies that looks draw, is theirs (past
|
||||
// `MAX_PAIRS` comparisons it isn't looked for).
|
||||
let looks = infos.iter().any(|i| i.shown && i.look.is_some());
|
||||
@@ -315,21 +316,44 @@ fn object_words(o: &[Arc<Node>]) -> Vec<String> {
|
||||
}
|
||||
|
||||
/// Each slot's made type as its content, else nothing yet. Made type that shows has where it draws,
|
||||
/// or where its story lies when it draws nothing.
|
||||
fn made_content(infos: &[SlotInfo], made: Vec<Option<Made>>) -> Vec<Content> {
|
||||
/// or where its story lies when it draws nothing. `laid`: point type is laid out for its box
|
||||
/// ([`drawn_box`]); without it the box is that of type without a layout, as it always was.
|
||||
fn made_content(infos: &[SlotInfo], made: Vec<Option<Made>>, laid: bool) -> Vec<Content> {
|
||||
infos
|
||||
.iter()
|
||||
.zip(made)
|
||||
.map(|(info, m)| match m {
|
||||
Some((node, region)) => {
|
||||
let drawn = node.visual_bounds().filter(|b| !b.is_zero_area()).or(region);
|
||||
Content::Made(node, drawn.filter(|_| info.shown), false)
|
||||
// Type that doesn't show has no region: it isn't laid out for one.
|
||||
let own = || if laid { drawn_box(&node) } else { node.visual_bounds() };
|
||||
let drawn = info.shown.then(|| own().filter(|b| !b.is_zero_area() && b.is_finite()).or(region)).flatten();
|
||||
Content::Made(node, drawn, false)
|
||||
}
|
||||
None => Content::Empty,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Where made type draws: its visual box, point type laid out first. Made type has no layout yet,
|
||||
/// and without one the box of point type is an estimate that runs right from its anchor
|
||||
/// ([`vectorcraft_doc::TextObject::estimate_bounds`]): beside where right-aligned type draws, over
|
||||
/// one half of centered type, across vertical type, and as wide as an average font would set it.
|
||||
/// The node keeps no layout: the document's type is measured when it opens. [`made_only`] matches
|
||||
/// by this box; [`plan`] doesn't yet.
|
||||
fn drawn_box(node: &Node) -> Option<Rect> {
|
||||
if let NodeKind::Text(t) = &node.kind
|
||||
&& matches!(t.kind, TextKind::Point)
|
||||
&& t.cached_bounds.is_none()
|
||||
{
|
||||
let mut laid = node.clone();
|
||||
if let NodeKind::Text(t) = &mut laid.kind {
|
||||
t.cached_bounds = Some(vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t).bounds);
|
||||
}
|
||||
return laid.visual_bounds();
|
||||
}
|
||||
node.visual_bounds()
|
||||
}
|
||||
|
||||
/// Mark the made type that has at least half of a piece of the page's type where it draws (or round
|
||||
/// 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
|
||||
@@ -391,8 +415,11 @@ pub(super) fn plan(
|
||||
// Where the made type that doesn't show lies, by slot.
|
||||
let hidden_at: Vec<(usize, Rect)> =
|
||||
infos.iter().zip(&made).enumerate().filter_map(|(k, (info, m))| Some((k, m.as_ref().filter(|_| !info.shown)?.1?))).collect();
|
||||
// Type the text document makes, shown or not: the page's pieces of it aren't needed.
|
||||
let mut content = made_content(infos, made);
|
||||
// Type the text document makes, shown or not: the page's pieces of it aren't needed. Its box
|
||||
// stays that of type without a layout here: with text objects whose type isn't made stacked on
|
||||
// made type that has their words (the copies of an outlined heading), the page's pieces in the
|
||||
// box where the made type draws are theirs too, and the made type would take them all.
|
||||
let mut content = made_content(infos, made, false);
|
||||
// Is `object` the type of a text object that doesn't show, as the page draws it: in its frame,
|
||||
// with its words? (A copied object's lines are where its original's are, so they don't say.)
|
||||
let hidden_type = |object: &[Arc<Node>]| {
|
||||
@@ -547,3 +574,37 @@ fn fill_hidden(infos: &[SlotInfo], stories: &[Option<Arc<Story>>], content: &mut
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vectorcraft_doc::{CharStyle, Justify, TextObject};
|
||||
|
||||
/// Made point type is matched with the page's type where it draws: right-aligned type before its
|
||||
/// anchor, centered type round it, vertical type down from it. Its node keeps no layout.
|
||||
#[test]
|
||||
fn made_point_type_s_box_is_where_its_alignment_and_orientation_draw_it() {
|
||||
let anchor = 300.0;
|
||||
let made = |justify: Justify, vertical: bool| {
|
||||
let mut t = TextObject::point(Point::new(anchor, 200.0), "Wide heading", CharStyle { size: 40.0, ..CharStyle::default() });
|
||||
t.para.justify = justify;
|
||||
for p in t.para_styles_mut() {
|
||||
p.justify = justify;
|
||||
}
|
||||
t.vertical = vertical;
|
||||
Node::new(NodeId(1), NodeKind::Text(Box::new(t)))
|
||||
};
|
||||
let boxed = |justify, vertical| {
|
||||
let node = made(justify, vertical);
|
||||
let b = drawn_box(&node).unwrap();
|
||||
assert!(matches!(&node.kind, NodeKind::Text(t) if t.cached_bounds.is_none()));
|
||||
b
|
||||
};
|
||||
let (left, right, centered, down) =
|
||||
(boxed(Justify::Left, false), boxed(Justify::Right, false), boxed(Justify::Center, false), boxed(Justify::Left, true));
|
||||
assert!(left.x0 > anchor - 20.0 && left.width() > 100.0, "{left:?}");
|
||||
assert!(right.x1 < anchor + 20.0 && (right.width() - left.width()).abs() < 1.0, "{right:?}");
|
||||
assert!((centered.center().x - anchor).abs() < 20.0 && (centered.width() - left.width()).abs() < 1.0, "{centered:?}");
|
||||
assert!(down.height() > 2.0 * down.width() && down.height() > 100.0, "{down:?}");
|
||||
}
|
||||
}
|
||||
@@ -5509,6 +5509,26 @@ fn a_small_label_of_the_page_in_no_text_object_is_noted() {
|
||||
assert!(note.is_some_and(|w| crate::is_loss(w)), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
/// Point type that isn't aligned left is matched with the page's type where it draws: right-aligned
|
||||
/// type ends at its anchor and centered type lies round it, so the page's type there is theirs and
|
||||
/// nothing is noted as left out. (The box of type that isn't laid out yet runs right from its
|
||||
/// anchor, past the page's type.)
|
||||
#[test]
|
||||
fn right_aligned_and_centered_point_type_takes_the_pages_type_where_it_draws() {
|
||||
// "Hi there" in 14 point Helvetica is about 49 points wide. The anchors are 90 and 50 across the
|
||||
// art; the page draws the line so that it ends at the first and lies round the second.
|
||||
for (align, anchor, at) in [(1, 8231.5, 41.0), (2, 8191.5, 25.5)] {
|
||||
let doc = text_document(&[("Hi there\r", align, (anchor, 8180.0), (0.0, 0.0), 0.0)]);
|
||||
let art = native_with_text(&layer("Art", true, &text_object(0)), &doc);
|
||||
let page = format!("0 1 0 0 setcmykcolor /Helvetica findfont 14 scalefont setfont {at} 61.5 moveto (Hi there) show");
|
||||
let r = import(&eps(&page, &art)).unwrap();
|
||||
assert_eq!(names(&r.document), ["Art"], "{align}: {:?}", r.warnings);
|
||||
let texts: Vec<String> = text_of(&r.document.layers[0]).iter().map(|t| t.plain_text()).collect();
|
||||
assert_eq!(texts, ["Hi there"], "{align}: {:?}", r.warnings);
|
||||
assert!(!r.warnings.iter().any(|w| crate::is_loss(w)), "{align}: {:?}", r.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
/// A ligature the page shows by a glyph its font's encoding names `uniE028` (a private-use
|
||||
/// character, without the letters it stands for) is the letters of its text object: the page's type
|
||||
/// has one, and nothing is noted as left out.
|
||||
|
||||
Reference in new issue
Block a user