Fix #10785: .ai import opens turned area type and type on a path turned

A text frame's FrameMatrix was read only for point type. Area frames and
type on a path were built from the frame's segments as stored, without
the matrix, so area type turned a quarter opened unturned with its lines
across the page, and a turned path for type on a path opened unturned.

- The frame's segments are carried onto the canvas by its matrix for
  every frame kind, and line starts use the first frame's matrix.
- Area type keeps the matrix's turn in its transform, with the frame in
  the type's own space, so its lines run along the turned frame. A
  scaled matrix scales the type, and a mirrored one is not made from the
  text document, as for point type.
- The object's own path, which replaces the frame on import, is turned
  back into the type's space before it becomes the frame.
- The file's first line is compared with the layout in the type's own
  space.
This commit is contained in:
dotCooCoo committed 2026-12-05 00:32:43 -08:00
1 parent f10dd40cb4
commit 64c1dabeb5
2 files changed
+187 -37

No files matched your search

+163 -36
View File
@@ -279,11 +279,35 @@ enum FrameKind {
/// Point type: the matrix carries the layout (its anchor at the origin of its lines) onto the
/// canvas.
Point([f64; 6]),
/// Area type flowed inside a path: its cubic segments on the canvas, four points each, and the
/// Area Type Options the frame keeps.
Area(Vec<[f64; 8]>, AreaFrame),
/// Type on a path, from `start` to `end` (segment index and the place in that segment).
OnPath { segments: Vec<[f64; 8]>, start: f64, end: f64, options: PathFrame },
/// Area type flowed inside a path: its cubic segments on the canvas, four points each, the Area
/// Type Options the frame keeps, and the frame's matrix (the segments are already on the
/// canvas; the matrix turns and scales the lines inside them).
Area(Vec<[f64; 8]>, AreaFrame, [f64; 6]),
/// Type on a path, from `start` to `end` (segment index and the place in that segment), its
/// segments on the canvas, and the frame's matrix.
OnPath { segments: Vec<[f64; 8]>, start: f64, end: f64, options: PathFrame, matrix: [f64; 6] },
}
/// The matrix of a frame that keeps none: its frame and layout are already on the canvas.
const NO_MATRIX: [f64; 6] = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
/// Segment `s` (four points) carried onto the canvas by the frame matrix `m`.
fn on_canvas(m: &[f64; 6], s: &[f64; 8]) -> [f64; 8] {
let [a, b, c, d, e, f] = *m;
let mut out = *s;
for [x, y] in out.as_chunks_mut::<2>().0 {
(*x, *y) = (a * *x + c * *y + e, b * *x + d * *y + f);
}
out
}
/// The scale of the frame matrix `m` and its turn (the matrix without its scale or its move), or
/// `None` for a matrix that mirrors, has no size or isn't finite.
fn turn_of(m: &[f64; 6]) -> Option<(f64, Affine)> {
let [a, b, c, d, ..] = *m;
let det = a * d - b * c;
let scale = det.abs().sqrt();
(scale.is_finite() && scale > 1e-6 && det > 0.0).then(|| (scale, Affine::new([a / scale, b / scale, c / scale, d / scale, 0.0, 0.0])))
}
/// The Type on a Path Options a frame of type on a path keeps at key 11. Each stays at its default
@@ -732,19 +756,21 @@ impl Texts {
fn frame(&self, i: usize) -> Option<FrameKind> {
let f = self.frames.get(i)?.get("0")?;
let carries = f.get("2")?;
// Key 2, the frame's matrix: it carries the frame's segments and its layout onto the canvas.
// Type that is neither turned nor scaled has none (Illustrator 24 leaves it out): its frame
// and layout are already where they go on the canvas.
let matrix: [f64; 6] = match carries.get("2") {
None => NO_MATRIX,
Some(m) => m.nums()?.try_into().ok()?,
};
let segments = || -> Option<Vec<[f64; 8]>> {
let n = f.get("1")?.get("0")?.nums()?;
let (chunks, rest) = n.as_chunks::<8>();
(rest.is_empty() && !chunks.is_empty() && chunks.len() <= MAX_SEGMENTS).then(|| chunks.to_vec())
(rest.is_empty() && !chunks.is_empty() && chunks.len() <= MAX_SEGMENTS).then(|| chunks.iter().map(|s| on_canvas(&matrix, s)).collect())
};
match carries.get("0").and_then(Val::num).unwrap_or(0.0) as i64 {
// Point type that is neither turned nor scaled has no matrix (Illustrator 24 leaves it out):
// its layout is already where it goes on the canvas.
0 => Some(FrameKind::Point(match carries.get("2") {
None => [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
Some(m) => m.nums()?.try_into().ok()?,
})),
1 => Some(FrameKind::Area(segments()?, AreaFrame::read(carries))),
0 => Some(FrameKind::Point(matrix)),
1 => Some(FrameKind::Area(segments()?, AreaFrame::read(carries), matrix)),
2 => {
let range = carries.get("6").and_then(Val::nums).unwrap_or_default();
let segments = segments()?;
@@ -760,7 +786,7 @@ impl Texts {
let start = finite(range.first()).unwrap_or(0.0).clamp(0.0, n);
(start, finite(range.get(1)).unwrap_or(n).clamp(start, n))
};
Some(FrameKind::OnPath { segments, start, end, options: PathFrame::read(carries) })
Some(FrameKind::OnPath { segments, start, end, options: PathFrame::read(carries), matrix })
}
_ => None,
}
@@ -1219,11 +1245,11 @@ impl Story {
frame == 0 && matches!(self.frames.first(), Some(FrameKind::Point(_))) && self.is_blank()
}
/// The point frame's matrix (the first frame's, when it is one), else the identity.
/// The first frame's matrix, which carries its layout onto the canvas, else the identity.
fn matrix(&self) -> [f64; 6] {
match self.frames.first() {
Some(FrameKind::Point(m)) => *m,
_ => [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
Some(FrameKind::Point(m) | FrameKind::Area(_, _, m) | FrameKind::OnPath { matrix: m, .. }) => *m,
None => NO_MATRIX,
}
}
@@ -1256,7 +1282,7 @@ impl Story {
let size = self.runs.iter().map(|(_, s)| s.size).fold(0.0, f64::max) * art_scale(to_doc);
let bounds = |segments: &[[f64; 8]]| path_of(segments, template, to_doc).bounds();
let r = match self.frames.first()? {
FrameKind::Area(s, _) => bounds(s)?,
FrameKind::Area(s, ..) => bounds(s)?,
FrameKind::OnPath { segments, .. } => bounds(segments)?.inflate(size * 1.5, size * 1.5),
FrameKind::Point(m) => {
let longest = self.text.split(['\r', '\x03']).map(|l| l.chars().count()).max().unwrap_or(0) as f64;
@@ -1312,23 +1338,29 @@ impl Story {
let threaded = frame > 0;
let (scale, xf, kind) = match kind {
FrameKind::Point(m) => {
let [a, b, c, d, ..] = *m;
let scale = (a * d - b * c).abs().sqrt();
if !(scale.is_finite() && scale > 1e-6 && a * d - b * c > 0.0) || threaded {
return None;
}
let (scale, turn) = turn_of(m).filter(|_| !threaded)?;
let (x, y) = self.place(template);
let at = to_doc * Point::new(x, y);
(scale, Affine::new([a / scale, b / scale, c / scale, d / scale, at.x, at.y]), TextKind::Point)
(scale, Affine::translate(at.to_vec2()) * turn, TextKind::Point)
}
FrameKind::Area(s, _) => (1.0, Affine::IDENTITY, TextKind::Area { frame: path_of(s, template, to_doc) }),
FrameKind::OnPath { segments, start, end, options } => {
// Turned area type keeps its turn: its lines run along the turned frame, which is kept
// in the text's own space (the frame on the canvas turned back).
FrameKind::Area(s, _, m) => {
let (scale, turn) = turn_of(m)?;
(scale, turn, TextKind::Area { frame: path_of(s, template, to_doc).transformed(turn.inverse()) })
}
FrameKind::OnPath { segments, start, end, options, matrix } => {
let (scale, _) = turn_of(matrix)?;
let n = segments.len() as f64;
// A closed path keeps its end bracket wherever it is (the same place as the start: a
// full turn, see `TextKind::path_span`); an open one's end at its end is none, unless
// a flip makes it the place the type starts.
let end = (closed(segments) || options.flip || *end < n - 0.02).then(|| length_fraction(segments, *end));
(1.0, Affine::IDENTITY, TextKind::OnPath { path: path_of(segments, template, to_doc), start: length_fraction(segments, *start), end })
(
scale,
Affine::IDENTITY,
TextKind::OnPath { path: path_of(segments, template, to_doc), start: length_fraction(segments, *start), end },
)
}
};
// A `\x03` ends a line inside its paragraph: a forced line break (U+2028), which the
@@ -1389,7 +1421,7 @@ impl Story {
}
// Each frame of a thread keeps its own options.
match self.frames.get(frame) {
Some(FrameKind::Area(_, options)) => options.apply(&mut t.area, scale),
Some(FrameKind::Area(_, options, _)) => options.apply(&mut t.area, scale),
Some(FrameKind::OnPath { options, .. }) => {
t.path_effect = options.effect;
t.path_align = options.align;
@@ -1450,7 +1482,7 @@ impl Story {
pub(super) fn place_first_line(&self, node: &mut Node, frame: usize, template: (f64, f64), to_doc: Affine) -> bool {
// Closer than this (points of the document) and the line is where the file has it.
const SAME: f64 = 0.5;
if matches!(self.frames.get(frame), Some(FrameKind::Area(_, AreaFrame { first_baseline: Some(_), .. }))) {
if matches!(self.frames.get(frame), Some(FrameKind::Area(_, AreaFrame { first_baseline: Some(_), .. }, _))) {
return false;
}
let NodeKind::Text(t) = &mut node.kind else { return false };
@@ -1458,7 +1490,8 @@ impl Story {
return false;
}
let Some((_, (x, y))) = self.line_starts(template).into_iter().next() else { return false };
let at = to_doc * Point::new(x, y);
// In the text's own space, where its lines are laid out (turned area type's is turned).
let at = t.xf.inverse() * (to_doc * Point::new(x, y));
let db = vectorcraft_text::FontDb::global();
// A frame too short for the line lays nothing out: as the line needs it (see `fit_first_line`).
let mut probe = TextObject::clone(t);
@@ -2229,7 +2262,7 @@ mod tests {
for frame in [
FrameKind::Point([0.0, 1.0, -1.0, 0.0, 30.0, 40.0]),
FrameKind::Point([2.0, 0.0, 0.0, 2.0, -7.0, 3.0]),
FrameKind::Area(area_segments(), AreaFrame::default()),
FrameKind::Area(area_segments(), AreaFrame::default(), NO_MATRIX),
] {
let mut st = story("type", &[0.0], &Para::default());
st.frames = vec![frame.clone()];
@@ -2987,12 +3020,12 @@ mod tests {
let mut s = story("one\rtwo\rthree\r", &[0.0, 15.6, 31.2], &Para::default());
// A point-type frame (the helper's) isn't fitted.
assert!(!s.fit_last_lines(&mut t));
s.frames = vec![FrameKind::Area(vec![], AreaFrame::default())];
s.frames = vec![FrameKind::Area(vec![], AreaFrame::default(), NO_MATRIX)];
assert!(s.fit_last_lines(&mut t));
assert_eq!(vectorcraft_text::layout(db, &t).lines.len(), 3);
// A story threaded through two frames: its lines are the first frame's alone.
let mut t = area(third.baseline - 20.0 + third.descent / 2.0);
s.frames.push(FrameKind::Area(vec![], AreaFrame::default()));
s.frames.push(FrameKind::Area(vec![], AreaFrame::default(), NO_MATRIX));
assert!(!s.fit_last_lines(&mut t));
}
@@ -3017,7 +3050,7 @@ mod tests {
(placed, l.lines.first().map(|l| l.baseline), t.area.first_baseline)
};
let mut s = story("Heading\r", &[], &Para::default());
s.frames = vec![FrameKind::Area(frame, AreaFrame::default())];
s.frames = vec![FrameKind::Area(frame, AreaFrame::default(), NO_MATRIX)];
// Where this lays the line out on its own: the file agrees, and nothing changes.
s.lines = vec![(10.0, 20.0)];
let (_, Some(own), _) = first(&s) else { panic!("a line") };
@@ -3030,6 +3063,32 @@ mod tests {
assert!(at.is_some_and(|y| (y - (own - 3.0)).abs() < 1e-6), "{at:?} for {}", own - 3.0);
}
#[test]
fn turned_area_type_s_first_line_is_compared_in_its_own_space() {
// GUARDS: the file's first line was compared with the layout where it is on the page, not
// in the turned type's own space: turned area type whose line is where the file has it got
// a fixed first baseline it doesn't keep.
let (template, to_doc) = ((CANVAS_CENTRE, -CANVAS_CENTRE), Affine::new([1.0, 0.0, 0.0, -1.0, 0.0, 0.0]));
let first = |s: &Story| {
let mut node = s.node(NodeId(0), 0, template, to_doc).unwrap();
let placed = s.place_first_line(&mut node, 0, template, to_doc);
let NodeKind::Text(t) = node.kind else { panic!("area type") };
let l = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t);
(placed, l.lines.first().map(|l| l.baseline), t.area.first_baseline)
};
let mut s = story("Heading\r", &[], &Para::default());
// A quarter turn round the canvas's origin: the frame's own space is the text document's.
s.frames = vec![area_frame("/2 [ 0 1 -1 0 0 0 ]")];
s.lines = vec![(10.0, 20.0)];
let (_, Some(own), _) = first(&s) else { panic!("a line") };
s.lines = vec![(10.0, own)];
assert_eq!(first(&s), (false, Some(own), vectorcraft_doc::FirstBaseline::Ascent));
s.lines = vec![(10.0, own - 3.0)];
let (placed, at, fb) = first(&s);
assert!(placed && fb == vectorcraft_doc::FirstBaseline::Fixed, "{fb:?}");
assert!(at.is_some_and(|y| (y - (own - 3.0)).abs() < 1e-6), "{at:?} for {}", own - 3.0);
}
/// A text document's area frame, the rectangle 10, 20 to 210, 120 of the canvas, whose frame
/// data has `options` besides its kind, written by hand.
fn area_frame(options: &str) -> FrameKind {
@@ -3065,6 +3124,74 @@ mod tests {
t.area
}
/// The text object a story in the area frame [`area_frame`] makes with `options` comes in as,
/// or `None`.
fn area_type(options: &str) -> Option<TextObject> {
let mut s = story("Some area type\r", &[], &Para::default());
s.frames = vec![area_frame(options)];
let to_doc = Affine::new([1.0, 0.0, 0.0, -1.0, 0.0, 0.0]);
match s.node(NodeId(0), 0, (CANVAS_CENTRE, -CANVAS_CENTRE), to_doc)?.kind {
NodeKind::Text(t) => Some(*t),
_ => None,
}
}
/// Width and height of the area frame of `t`, in its own space.
fn own_frame_size(t: &TextObject) -> (f64, f64) {
let vectorcraft_doc::TextKind::Area { frame } = &t.kind else { panic!("area type") };
let r = frame.bounds().unwrap();
(r.width(), r.height())
}
#[test]
fn turned_area_type_comes_in_turned_with_its_lines_along_its_frame() {
// GUARDS: area type's frame matrix was left out: a frame turned a quarter came in unturned,
// its lines across the page where the file runs them down it.
let plain = area_type("").unwrap();
let turned = area_type("/2 [ 0 1 -1 0 300 0 ]").unwrap();
let (w, h) = own_frame_size(&plain);
assert!(w > h, "{w} by {h}");
// On the page the frame stands on its end ...
let page = turned.bounds().unwrap();
assert!((page.width() - h).abs() < 1e-6 && (page.height() - w).abs() < 1e-6, "{page:?}");
// ... and in the text's own space, where its lines run, it is as wide as the unturned one.
let (tw, th) = own_frame_size(&turned);
assert!((tw - w).abs() < 1e-6 && (th - h).abs() < 1e-6, "{tw} by {th}");
let along = turned.xf * Point::new(1.0, 0.0) - turned.xf * Point::ZERO;
assert!(along.x.abs() < 1e-9 && (along.y.abs() - 1.0).abs() < 1e-9, "{along:?}");
// A turn alone leaves the type's size as it is.
assert_eq!(turned.runs[0].style.size, plain.runs[0].style.size);
}
#[test]
fn a_scaled_area_frame_scales_its_type_and_a_mirrored_one_isnt_made() {
// GUARDS: area type's frame matrix was left out: a frame scaled up came in at its unscaled
// size, and a mirrored one unmirrored.
let plain = area_type("").unwrap();
let double = area_type("/2 [ 2 0 0 2 0 0 ]").unwrap();
assert_eq!(double.runs[0].style.size, plain.runs[0].style.size * 2.0);
let ((w, h), (dw, dh)) = (own_frame_size(&plain), own_frame_size(&double));
assert!((dw - 2.0 * w).abs() < 1e-6 && (dh - 2.0 * h).abs() < 1e-6, "{dw} by {dh}");
// Mirrored type isn't made from the text document, as point type isn't.
assert!(area_type("/2 [ -1 0 0 1 0 0 ]").is_none());
}
#[test]
fn type_on_a_turned_path_runs_along_the_path_where_the_canvas_has_it() {
// GUARDS: type on a path's frame matrix was left out: the path of a frame turned a quarter
// came in unturned.
let open = [[0.0, 0.0, 30.0, 0.0, 70.0, 0.0, 100.0, 0.0], [100.0, 0.0, 130.0, 0.0, 170.0, 0.0, 200.0, 0.0]];
let path = |options: &str| {
let (t, _) = on_path("type", on_path_frame_with(&open, [0.0, 2.0], options).unwrap());
let vectorcraft_doc::TextKind::OnPath { path, .. } = &t.kind else { panic!("type on a path") };
path.bounds().unwrap()
};
let plain = path("");
assert!(plain.width() > 100.0 && plain.height() < 1e-6, "{plain:?}");
let turned = path("/2 [ 0 1 -1 0 0 0 ]");
assert!(turned.width() < 1e-6 && (turned.height() - plain.width()).abs() < 1e-6, "{turned:?}");
}
#[test]
fn area_frame_options_are_read() {
// GUARDS: area type came in with one row, one column and the 18 pt gutter whatever its frame kept.
@@ -3152,7 +3279,7 @@ mod tests {
#[test]
fn area_frame_without_options_comes_in_as_before() {
assert_eq!(area_of(vec![area_frame("")], 0, 1.0), vectorcraft_doc::AreaOptions::default());
assert_eq!(area_frame(""), FrameKind::Area(area_segments(), AreaFrame::default()));
assert_eq!(area_frame(""), FrameKind::Area(area_segments(), AreaFrame::default(), NO_MATRIX));
}
/// The segments [`area_frame`] writes.
@@ -3199,7 +3326,7 @@ mod tests {
(placed, l.baselines().first().map(|b| b.0.x), t.area.first_baseline)
};
let mut s = story("§§\r", &[], &Para::default());
s.frames = vec![FrameKind::Area(frame, AreaFrame::default())];
s.frames = vec![FrameKind::Area(frame, AreaFrame::default(), NO_MATRIX)];
s.vertical = true;
s.anchor = (210.0, 20.0);
s.lines = vec![(0.0, 0.0)];
+24 -1
View File
@@ -38,7 +38,9 @@ fn frame_text_from_art(info: &SlotInfo, node: &mut Node, region: &mut Option<Rec
let size = text.runs.iter().map(|r| r.style.size).fold(0.0, f64::max);
match &mut text.kind {
TextKind::Area { frame } => {
*frame = path.clone();
// The object's path is on the page; turned area type keeps its frame in its own
// (turned) space.
*frame = path.transformed(text.xf.inverse());
*region = path.bounds();
}
TextKind::OnPath { path: on, .. } => {
@@ -738,4 +740,25 @@ mod tests {
assert_eq!(has_made_words(&present, &content, &spelled, &mut few), Some(true));
assert!(one_word_everywhere - few.0 < one_word_everywhere / 2, "{} searches", one_word_everywhere - few.0);
}
/// The object's own path frames turned area type where the path is on the page: the frame is
/// kept in the type's turned space.
#[test]
fn an_object_s_path_frames_turned_area_type_where_the_path_is() {
// GUARDS: the path replaced the frame as it is on the page, and the type's turn then turned
// it a second time.
let mut node = text(1, "Turned");
let NodeKind::Text(t) = &mut node.kind else { panic!("type") };
t.xf = Affine::new([0.0, 1.0, -1.0, 0.0, 0.0, 0.0]);
t.kind = TextKind::Area { frame: vectorcraft_geom::shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)) };
let on_page = vectorcraft_geom::shapes::rectangle(Rect::new(50.0, 20.0, 150.0, 220.0));
let info = SlotInfo { story: Some(0), frame: 0, shown: true, art_path: Some(on_page.clone()), look: None, group: None };
let mut region = None;
frame_text_from_art(&info, &mut node, &mut region);
let NodeKind::Text(t) = &node.kind else { panic!("type") };
let (drawn, want) = (t.bounds().unwrap(), on_page.bounds().unwrap());
let near = |a: f64, b: f64| (a - b).abs() < 1e-6;
assert!(near(drawn.x0, want.x0) && near(drawn.y0, want.y0) && near(drawn.x1, want.x1) && near(drawn.y1, want.y1), "{drawn:?} {want:?}");
assert_eq!(region, Some(want));
}
}