Merge pull request #9835

This commit is contained in:
autocraft-bot[bot]
2026-12-06 12:46:59 +00:00
2 changed files with 296 additions and 11 deletions
+89 -11
View File
@@ -2,8 +2,8 @@ use super::boxes::{BoxInput, BoxLayout, Edges, Joint, frame_float, joined, layou
use super::cache::has_page_number_fields;
use super::ctx::{Ctx, Joining};
use super::floats::{
AnchoredFloat, FloatAnchor, LateFloats, ObjFrame, ObjectPlacement, anchor_floats, body_wraps, continued_exclusions, fitting, float_rect,
late_floats, place_late, place_objects, wrap_area,
AnchoredFloat, FloatAnchor, LateFloats, ObjFrame, ObjectPlacement, WrapArea, anchor_floats, body_wraps, continued_exclusions, drawn_size,
fitting, float_rect, floating, late_floats, left_out_objects, place_late, place_objects, rel_exclusions, wrap_area,
};
use super::notes::{DECOR_STORY, NoteBox, para_notes};
use super::page::{PageBuilder, PageFrame, item_bottom};
@@ -109,29 +109,107 @@ fn place_page_text_frame(ctx: &mut Ctx, pb: &mut PageBuilder, p: &Paragraph, blo
}
let width = fr.w.unwrap_or_else(|| pb.col_w()).clamp(1.0, 1584.0);
let label = ctx.next_label(p);
let pl = ctx.para_labelled(p, width, None, &[], 0.0, label);
let float = frame_float(&fr);
let page = PageFrame { sect: pb.sect, margin_area: None, origin: (0.0, 0.0), number: pb.number, mirror: ctx.mirror };
let height = |text: f32| fr.h.map_or(text, |h| if fr.h_exact { h } else { h.max(text) });
// Each layout after the first counts its automatic equation numbers from where the first did.
let eq_start = ctx.eq_count;
let mut pl = ctx.para_labelled(p, width, None, &[], 0.0, label.clone());
let mut rect = float_rect(Some(page), (pb.col_x(), pb.col_w()), pb.y, width, height(pl.height), &float);
let plain_top = pl.lines.first().map_or(0.0, |l| l.top);
// Its floating objects, placed from the frame: its own text goes around them as their
// wrapping says, as the body text after it does.
// A frame placed from its height (aligned to the bottom or the middle) moves when its text
// does, and its floats with it: laid out again until it stays put (a few times at most).
let mut own = frame_float_areas(ctx, p, page, rect, width);
let mut settled = own.is_empty();
for _ in 0..MAX_FRAME_RELAYOUTS {
if settled {
break;
}
let excl = rel_exclusions(&own, rect.x, rect.y, 0.0);
pl = again(ctx, eq_start, |ctx| ctx.para_labelled(p, width, None, &excl, 0.0, label.clone()));
let moved = float_rect(Some(page), (pb.col_x(), pb.col_w()), pb.y, width, height(pl.height), &float);
settled = (moved.y - rect.y).abs() < 0.01 && (moved.x - rect.x).abs() < 0.01;
rect = moved;
own = frame_float_areas(ctx, p, page, rect, width);
}
// Still moving (its text going back and forth around a float): the frame stays where it
// ended up, and its text goes around the floats there. It is as tall as that text is, so the
// body text after it keeps clear of all its lines.
if !settled {
let excl = rel_exclusions(&own, rect.x, rect.y, 0.0);
pl = again(ctx, eq_start, |ctx| ctx.para_labelled(p, width, None, &excl, 0.0, label));
rect.h = height(pl.height);
}
// A float above the first line (wrapped top and bottom) moves the text down in the frame, as
// the flow's lead does: the lines start that much further below the frame's top.
let lead = pl.lines.first().map_or(0.0, |l| (l.top - plain_top).max(0.0));
let origin = (rect.x, rect.y + lead);
let notes = para_notes(ctx.doc, p, &pl);
let mut new_notes = Vec::new();
for line in &pl.lines {
new_notes.extend(notes_on_line(ctx, pb, line, &notes, &new_notes));
}
add_notes(pb, new_notes, &notes, pl.lines.last().map_or(0, |line| line.c1));
let height = fr.h.map_or(pl.height, |h| if fr.h_exact { h } else { h.max(pl.height) });
let float = frame_float(&fr);
let page = PageFrame { sect: pb.sect, margin_area: None, origin: (0.0, 0.0), number: pb.number, mirror: ctx.mirror };
let rect = float_rect(Some(page), (pb.col_x(), pb.col_w()), pb.y, width, height, &float);
let path = [block as u32];
if let Some(page) = pb.page() {
push_para(&mut page.items, (StoryRef::Body, &path), &pl, (0, pl.lines.len()), (rect.x, rect.y), width, Edges::own(&pl, 0, pl.lines.len()));
}
// The body text after the frame wraps around it as its wrap mode says.
// Its pictures, shapes and text boxes go with it, in line with its text or floating from the
// frame (a letterhead's logo in a frame, say, #8530).
let at = ObjFrame { page: Some(page), col: (rect.x, width), para_y: rect.y, table_chr: None, pinned: None, late: None };
let (behind, front) = place_objects(
ctx,
&ObjectPlacement {
story: StoryRef::Body,
path: &path,
p,
pl: &pl,
lines: (0, pl.lines.len()),
origin,
at: &at,
floats: &HashMap::new(),
depth: 0,
},
);
// As the flow adds a piece: behind the text at the front, the rest after it, its floating
// objects raised over the text of the page laid out later.
let mut items = Vec::new();
push_para(&mut items, (StoryRef::Body, &path), &pl, (0, pl.lines.len()), origin, width, Edges::own(&pl, 0, pl.lines.len()));
let raised = items.len()..items.len() + front.len();
items.extend(front);
add_items(pb, behind, items, raised);
// The body text after the frame wraps around it as its wrap mode says, and around its
// floating objects as theirs say.
if let Some(area) = wrap_area(rect, &float, None) {
pb.framed.push(area.0);
pb.excl.push(area);
}
// They obstruct the page's top as the frame does: a page they reach into is not fresh.
pb.framed.extend(own.iter().map(|a| a.0));
pb.excl.extend(own);
true
}
/// Most times a page-anchored framed paragraph is laid out again for its own floats' wrap areas:
/// a frame placed from its height can move each time its text does.
const MAX_FRAME_RELAYOUTS: usize = 4;
/// The wrap areas of the floating objects of `p`, a paragraph framed at `rect` (`width` wide) on
/// `page`: each placed as [`place_objects`] places it from the frame.
fn frame_float_areas(ctx: &mut Ctx, p: &Paragraph, page: PageFrame, rect: wordcraft_geom::Rect, width: f32) -> Vec<WrapArea> {
let left_out = left_out_objects(ctx.doc, p, None, ctx.opts.show_hidden, ctx.hide_deleted);
let mut areas = Vec::new();
for (oi, o) in p.objects.iter().enumerate() {
let Some((w, h, float)) = floating(o) else { continue };
if left_out(oi) {
continue;
}
let (w, h) = drawn_size(ctx, Some(&page), o, w, h, float);
let r = float_rect(Some(page), (rect.x, width), rect.y, w, h, float);
let outline = ctx.wrap_outline(o, (r.w, r.h));
areas.extend(wrap_area(r, float, outline.as_deref().map(Vec::as_slice)));
}
areas
}
/// A closing mark before a continuous section takes no room after content. An otherwise empty
/// section starting a new page retains its paragraph line.
fn place_para_alone(ctx: &mut Ctx, pb: &mut PageBuilder, p: &Paragraph, block: usize, body_top: f32) {
+207
View File
@@ -21677,6 +21677,111 @@ fn top_and_bottom_float_in_cell_keeps_cache_hits_on_shift() {
assert_eq!(c.misses, misses + 1, "only the edited cell paragraph should miss the cache");
}
/// #8530: a body paragraph framed against the page draws its pictures and shapes at the frame, in
/// line with its text and floating (a letterhead's logo in a frame, say). They were left out.
#[test]
fn a_page_anchored_frame_draws_its_objects() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Logo \nBody");
let frame = TextFrame {
x: 300.0,
y: 100.0,
w: Some(200.0),
h_anchor: "page".into(),
v_anchor: "page".into(),
wrap: "around".into(),
..Default::default()
};
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
d.insert_object(&Pos::body(0, 5), rect_shape(40.0, 20.0, Default::default()), &Default::default()).unwrap();
d.insert_object(&Pos::body(0, 0), rect_shape(30.0, 30.0, page_float(400.0, 500.0)), &Default::default()).unwrap();
let l = lay(&d);
// The floating one is drawn where it says; the one in line is drawn with its line.
let s = shapes(&l.pages[0].items);
assert!(s.iter().any(|r| (r.x - 400.0).abs() < 0.5 && (r.y - 500.0).abs() < 0.5), "floating, where it says: {s:?}");
// Both can be clicked and selected there: the one in line in the frame.
let objects: Vec<Rect> = l.pages[0]
.items
.iter()
.filter_map(|it| match it {
Placed::Object { rect, .. } => Some(*rect),
_ => None,
})
.collect();
assert_eq!(objects.len(), 2, "{objects:?}");
assert!(
objects.iter().any(|r| (r.w - 40.0).abs() < 0.5 && r.x >= 300.0 && r.x < 500.0 && r.y >= 99.0 && r.y < 140.0),
"in line, in the frame: {objects:?}"
);
}
/// #9835 review: the body text after a page-anchored framed paragraph wraps around the paragraph's
/// floating objects as their wrapping says, as it did when the paragraph flowed.
#[test]
fn body_text_wraps_around_a_page_anchored_frames_floats() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Logo\nBody text that runs along the top of the page beside the logo.");
let frame = TextFrame {
x: 400.0,
y: 20.0,
w: Some(100.0),
h_anchor: "page".into(),
v_anchor: "page".into(),
wrap: "around".into(),
..Default::default()
};
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
let float = wordcraft_doc::para::Float { wrap: wordcraft_doc::para::Wrap::Square, ..page_float(72.0, 72.0) };
d.insert_object(&Pos::body(0, 0), rect_shape(150.0, 100.0, float), &Default::default()).unwrap();
let l = lay(&d);
let body = l.caret(&Pos::body(1, 0)).unwrap();
assert!(body.x >= 222.0, "the body text starts right of the logo: {body:?}");
}
/// #9835 review: a page-anchored framed paragraph's own text goes around its floating objects as
/// their wrapping says, as the flow's does.
#[test]
fn a_page_anchored_frames_text_wraps_around_its_floats() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Framed text that runs over a few lines of the frame beside its picture.\nBody");
let frame = TextFrame {
x: 300.0,
y: 20.0,
w: Some(200.0),
h_anchor: "page".into(),
v_anchor: "page".into(),
wrap: "around".into(),
..Default::default()
};
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
let float = wordcraft_doc::para::Float { wrap: wordcraft_doc::para::Wrap::Square, ..page_float(300.0, 20.0) };
d.insert_object(&Pos::body(0, 0), rect_shape(80.0, 60.0, float), &Default::default()).unwrap();
let l = lay(&d);
let first = l.caret(&Pos::body(0, 1)).unwrap();
assert!(first.x >= 380.0, "the frame's first line starts right of its picture: {first:?}");
}
/// #9835 review: a page-anchored framed paragraph's object in front of the text is raised over
/// the text laid out after it, as the flow's floating objects are.
#[test]
fn a_page_anchored_frames_front_object_is_drawn_over_later_text() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Logo\nBody text under the logo.");
let frame =
TextFrame { x: 300.0, y: 20.0, w: Some(100.0), h_anchor: "page".into(), v_anchor: "page".into(), wrap: "none".into(), ..Default::default() };
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
d.insert_object(&Pos::body(0, 0), rect_shape(150.0, 100.0, page_float(72.0, 72.0)), &Default::default()).unwrap();
let l = lay(&d);
let items = &l.pages[0].items;
let shape = items.iter().position(|it| matches!(it, Placed::Shape { .. })).unwrap();
let body = items.iter().position(|it| matches!(it, Placed::Lines { path, .. } if *path == Path::top(1))).unwrap();
assert!(shape > body, "the shape ({shape}) comes after the body text ({body})");
}
/// #9791: removing a section break from an empty shaded paragraph must invalidate its cached
/// layout and bump its revision so its shading is drawn on the next pass.
#[test]
@@ -21700,3 +21805,105 @@ fn removing_section_break_invalidates_cached_empty_para_layout() {
assert!(c.misses > misses, "removing the section break must miss the layout cache");
assert_eq!(fill_count(&after), 1, "shading appears once the section break is removed");
}
/// #9835 review: a framed paragraph's float that covers the rest of the page is an obstruction at
/// the page's top as the frame is: the body text it pushes down goes on to the next page.
#[test]
fn a_frames_float_covering_the_page_moves_the_body_text_on() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Logo\nBody");
let frame =
TextFrame { x: 300.0, y: 20.0, w: Some(100.0), h_anchor: "page".into(), v_anchor: "page".into(), wrap: "none".into(), ..Default::default() };
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
let float = wordcraft_doc::para::Float { wrap: wordcraft_doc::para::Wrap::TopAndBottom, ..page_float(72.0, 72.0) };
d.insert_object(&Pos::body(0, 0), rect_shape(200.0, 720.0, float), &Default::default()).unwrap();
let l = lay(&d);
let body = l.caret(&Pos::body(1, 0)).unwrap();
assert_eq!(body.page, 1, "the body text below the float's foot is on the next page: {body:?}");
}
/// #9835 review: a float wrapped top and bottom over a frame's first line moves the frame's text
/// below it, as in the flow: the text is not drawn back at the frame's top, over the float.
#[test]
fn a_frames_top_and_bottom_float_moves_its_text_down() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Framed text.\nBody");
let frame = TextFrame {
x: 300.0,
y: 100.0,
w: Some(200.0),
h_anchor: "page".into(),
v_anchor: "page".into(),
wrap: "around".into(),
..Default::default()
};
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
let float = wordcraft_doc::para::Float { wrap: wordcraft_doc::para::Wrap::TopAndBottom, ..page_float(300.0, 100.0) };
d.insert_object(&Pos::body(0, 0), rect_shape(200.0, 40.0, float), &Default::default()).unwrap();
let l = lay(&d);
let first = l.caret(&Pos::body(0, 1)).unwrap();
assert!(first.top >= 140.0 - 0.5, "the frame's text starts below its float: {first:?}");
}
/// #9835 review: a frame aligned to the page's bottom moves up when its own float pushes its text
/// down; its text is laid out again around the float where the frame ends up, not where it was.
#[test]
fn a_bottom_aligned_frames_text_keeps_clear_of_its_float() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let mut d = Document::from_text("Framed text that runs over a few lines at the foot of the page here.\nBody");
let frame = TextFrame {
w: Some(150.0),
y_align: Some("bottom".into()),
h_anchor: "page".into(),
v_anchor: "page".into(),
wrap: "around".into(),
x: 300.0,
..Default::default()
};
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.frame = Some(frame);
let page_h = d.last_section.page_h;
let float = wordcraft_doc::para::Float { wrap: wordcraft_doc::para::Wrap::TopAndBottom, ..page_float(300.0, page_h - 30.0) };
d.insert_object(&Pos::body(0, 0), rect_shape(150.0, 20.0, float), &Default::default()).unwrap();
let l = lay(&d);
let (top, bottom) = (page_h - 30.0, page_h - 10.0);
let len = d.para(StoryRef::Body, &Path::top(0)).unwrap().len();
for off in 1..len {
let c = l.caret(&Pos::body(0, off)).unwrap();
assert!(c.top + c.height <= top + 0.5 || c.top >= bottom - 0.5, "a line at {off} overlaps the float ({top}..{bottom}): {c:?}");
}
}
/// #9835 review: laying a framed paragraph out again for its floats doesn't count its automatic
/// equation number again: it and the next paragraph's read (1) and (2).
#[test]
fn a_frame_laid_out_again_keeps_its_equation_number() {
use wordcraft_doc::props::TextFrame;
crate::test_fonts();
let eq = |s: &str| InlineObject::Equation {
linear: s.into(),
display: true,
math: wordcraft_doc::math::Math { nodes: wordcraft_doc::math::parse_linear(s), ..Default::default() },
};
let mut d = Document::from_text("\nAfter");
let frame = TextFrame {
x: 300.0,
y: 100.0,
w: Some(200.0),
h_anchor: "page".into(),
v_anchor: "page".into(),
wrap: "around".into(),
..Default::default()
};
let p0 = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap();
p0.props.frame = Some(frame);
p0.insert_object(0, eq("a=b#"), &Default::default()).unwrap();
let float = wordcraft_doc::para::Float { wrap: wordcraft_doc::para::Wrap::Square, ..page_float(300.0, 100.0) };
d.insert_object(&Pos::body(0, 0), rect_shape(40.0, 40.0, float), &Default::default()).unwrap();
let p1 = d.para_mut(StoryRef::Body, &Path::top(1)).unwrap();
let n = p1.len();
p1.insert_object(n, eq("c=d#"), &Default::default()).unwrap();
assert_eq!(equation_numbers(&lay(&d)), ["(1)", "(2)"]);
}