Files
wordcraft/crates/layout/src/boxes.rs
T
6c6ac698ca Layout: a paragraph with a hidden mark is shown on one line with the next one (#3986) (#6913)
* Layout: a paragraph with a hidden mark is shown on one line with the next one (#3986) (#4134)

Lands #4134 rebased onto main; only docs conflicts were resolved (both sides kept).

Co-Authored-By: Menonro <25549525+Menonro@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCXtSjB6e5jmy9Uq7u8

* Layout: hidden-mark join fixes from review of #6913

- A paragraph moved off the shared line (next column or page) gets its
  own top and bottom border room again (flow.rs `Rooms` were stale).
- Hit testing cuts a line at a joined paragraph only when that paragraph
  still shares the line (`shares_line`), not when its first word wrapped below.
- A cell or header paragraph going on from a hidden mark takes the shared
  line's top as its paragraph origin, so paragraph-anchored drawings follow it.
- A text frame is placed apart: it is not joined, keeps its number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* #4134: joined text's later lines start below a taller shared line; table frames don't join

A paragraph going on from the line of one whose mark is hidden kept only its
first line clear of that line: under a taller heading its second line started
inside the heading's line, back at the left margin. Its later lines now keep
clear of the rest of the shared line, with the room for its bottom border, in
the body and in boxes (cells, headers, text boxes).

The guard against joining a text frame resolves the paragraph with the table's
paragraph formatting, as its layout does, so a frame given by the table style
is placed apart with its list number instead of joining.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* #4134: joined text's first line shares the line again with exact spacing

The area that holds a joined paragraph's later lines below a taller shared
line started just below its first line. The line breaker checks a row
against wrap areas at its own estimate, the mark's font height, which is
taller than an exact line height below it: the first row then met the area
and moved below the shared line. The area now starts below whichever is
taller, so the first line shares the line under every line spacing rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx

* #4134: joined text's later lines clear the shared line when a large word wraps

The area that keeps a continuation's later lines below the shared line now starts just below the first row at the line breaker's row estimate, which is what the breaker tests rows with. The probe's first line can be taller than the real one when a large word fits it but wraps off the narrower shared line, and starting the area below that taller line let the real second line start inside the heading's line. The test adds a 30 pt word at three places that wrap it to the second line, in the body and in a cell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx

---------

Co-authored-by: Menonro <25549525+Menonro@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-26 11:48:47 +01:00

952 lines
44 KiB
Rust

use super::ctx::{Ctx, Joining, page_anchored};
use super::floats::{
ObjFrame, ObjectPlacement, WrapArea, align_in, float_rect, floating, left_out_objects, place_objects, rel_exclusions, wrap_area,
};
use super::flow::again;
use super::page::PageFrame;
use super::placed::Placed;
use super::table_flow::below;
use super::{
Anchor, Arc, Block, Blocks, Border, Document, Float, FloatAlign, HashMap, ParaLayout, Paragraph, Path, Rect, StoryRef, TableFloat, TextDirection,
Wrap, WrapText, display, para, table,
};
/// Cell identity is independent of optional table-style formatting.
pub(crate) enum BoxContext<'a> {
Other,
/// Outer cell edges relative to its text origin (not including wrap distances).
Cell {
text: Option<&'a para::CellText>,
left: f32,
right: f32,
/// False for a turned text frame: its x axis becomes page y after rotation.
clamp: bool,
},
}
pub(crate) struct BoxInput<'a> {
pub(crate) story: StoryRef,
pub(crate) blocks: &'a Blocks,
pub(crate) prefix: &'a [u32],
pub(crate) width: f32,
pub(crate) context: BoxContext<'a>,
pub(crate) depth: usize,
pub(crate) frame: Option<PageFrame<'a>>,
}
impl<'a> BoxInput<'a> {
pub(crate) fn other(story: StoryRef, blocks: &'a Blocks, width: f32, depth: usize, frame: Option<PageFrame<'a>>) -> Self {
Self { story, blocks, prefix: &[], width, context: BoxContext::Other, depth, frame }
}
pub(crate) fn cell(story: StoryRef, blocks: &'a Blocks, prefix: &'a [u32], width: f32, cell: Option<&'a para::CellText>, depth: usize) -> Self {
Self { story, blocks, prefix, width, context: BoxContext::Cell { text: cell, left: 0.0, right: width, clamp: false }, depth, frame: None }
}
}
struct ParagraphSpacing<'a> {
index: usize,
listed: bool,
style: &'a str,
contextual: bool,
before_auto: bool,
space_before: f32,
allow_same_style_space: bool,
previous_style: Option<&'a (String, bool)>,
previous_listed: bool,
previous_after_auto: bool,
previous_after: &'a mut f32,
y: &'a mut f32,
sum_paragraph_spacing: bool,
}
struct TableBlockContext<'ctx, 'state, 'doc> {
ctx: &'ctx mut Ctx<'doc>,
story: StoryRef,
table: &'state wordcraft_doc::Table,
path: &'state [u32],
width: f32,
frame: Option<PageFrame<'state>>,
in_cell: bool,
depth: usize,
y: &'state mut f32,
items: &'state mut Vec<Placed>,
exclusions: &'state mut Vec<WrapArea>,
float_tables: &'state mut Vec<Rect>,
table_areas: &'state mut Vec<Rect>,
page_table_wraps: &'state mut Vec<WrapArea>,
previous_after: &'state mut f32,
previous_style: &'state mut Option<(String, bool)>,
}
fn paragraph_spacing(input: ParagraphSpacing<'_>) -> (bool, f32) {
let ParagraphSpacing {
index,
listed,
style,
contextual,
before_auto,
space_before,
allow_same_style_space,
previous_style,
previous_listed,
previous_after_auto,
previous_after,
y,
sum_paragraph_spacing,
} = input;
let contextual = contextual && !allow_same_style_space;
let mut before = space_before;
if let Some((previous, previous_contextual)) = previous_style
&& previous == style
&& !allow_same_style_space
{
if *previous_contextual {
*y -= *previous_after;
before = (before - *previous_after).max(0.0);
*previous_after = 0.0;
}
if contextual {
before = 0.0;
}
}
if index > 0 && previous_listed && listed {
if previous_after_auto {
*y -= *previous_after;
*previous_after = 0.0;
}
if before_auto {
before = 0.0;
}
}
if before_auto && index == 0 {
before = 0.0;
} else if index > 0 && before > 0.0 && *previous_after > 0.0 && !sum_paragraph_spacing {
before = (before - *previous_after).max(0.0);
}
(contextual, before)
}
fn layout_table_block(input: TableBlockContext<'_, '_, '_>) -> bool {
let TableBlockContext {
ctx,
story,
table: t,
path,
width,
frame,
in_cell,
depth,
y,
items,
exclusions,
float_tables,
table_areas,
page_table_wraps,
previous_after,
previous_style,
} = input;
if depth > 8 {
return false;
}
// Page tables do not contribute to the footer text's intrinsic height.
if frame.is_none()
&& !in_cell
&& matches!(story, StoryRef::Part(id) if ctx.doc.parts.get(&id).is_some_and(|p| p.kind == wordcraft_doc::PartKind::Footer))
&& t.props.float.is_some_and(|f| f.v_rel == Anchor::Page)
{
return false;
}
let layout = table::layout_table(ctx, story, t, path, width, depth + 1);
let (tx, ty) = match t.props.float {
Some(float) => {
let legacy = (ctx.doc.settings.compat_mode < 15).then(|| table::first_cell_left_margin(ctx, t));
let (rect, area) = box_float_table_rect(frame, width, *y, &layout, &float, legacy, float_tables);
exclusions.push((area, false, None, WrapText::BothSides, true));
float_tables.push(area);
table_areas.push(area);
if float.v_rel == Anchor::Page {
page_table_wraps.push((area, false, None, WrapText::BothSides, true));
}
(rect.x, rect.y)
}
None => (layout.x, below(exclusions, *y, layout.x, layout.x + layout.width)),
};
let mut row_y = ty;
for row in &layout.rows {
for item in &row.items {
let mut item = item.clone();
item.translate(tx, row_y);
items.push(item);
}
row_y += row.height;
}
if t.props.float.is_none() {
*y = row_y;
*previous_after = 0.0;
*previous_style = None;
}
true
}
/// Whether `story` is a header in a document of Word 2010's layout or older (compatibility mode
/// below 15), whose text keeps its place beside the header's own floating objects (#6805).
fn is_legacy_header(ctx: &Ctx, story: StoryRef) -> bool {
ctx.doc.settings.compat_mode < wordcraft_doc::COMPAT_MODE_CURRENT
&& matches!(story, StoryRef::Part(id) if ctx.doc.parts.get(&id).is_some_and(|p| p.kind == wordcraft_doc::PartKind::Header))
}
/// Where a top and bottom object at `r` starts and ends for such a header.
/// The distance below it counts (Word: 14.4 pt more for 14.4 pt); the one above neither moves it
/// nor makes the header taller.
fn legacy_reach_of(float: &wordcraft_doc::para::Float, r: Rect) -> Option<(f32, f32)> {
let below = if float.dist_bottom.is_finite() { float.dist_bottom.clamp(0.0, 1584.0) } else { 0.0 };
let bounds = float.spin().bounds(r);
(float.wrap == Wrap::TopAndBottom).then_some((bounds.y, bounds.bottom() + below))
}
/// The bottom of the lowest top and bottom object (`reach`) that starts above `text_bottom`, the
/// end of the header's text with the space after its last paragraph (Word counts an object
/// starting in it). Word also counts one starting a little further down (about 25 pt below one
/// line of text), not modelled here. Zero when there is none.
fn legacy_header_reach(text_bottom: f32, reach: &[(f32, f32)]) -> f32 {
reach.iter().filter(|(top, _)| *top < text_bottom).map(|(_, bottom)| *bottom).fold(0.0, f32::max)
}
/// Lay out a block list into a free-standing box of `width` (no page breaks): table cells,
/// headers, footers, notes, text boxes. Returns items relative to (0, 0) and the height. `frame`
/// says where the box sits on its page, for floating objects positioned relative to the page or
/// its margins; without it (cells, notes, text boxes) they're positioned in the box.
pub(crate) fn layout_box(ctx: &mut Ctx, input: BoxInput<'_>) -> BoxLayout {
let BoxInput { story, blocks, prefix, width, context, depth, frame } = input;
// Deletions in balloons leave the body's text only: a header, note or text box keeps its own
// inline (restored for the story around this box when it's done).
let outer_hide = std::mem::replace(&mut ctx.hide_deleted, ctx.opts.hides_deleted(story));
let empty_cell = para::CellText::default();
let (in_cell, table, cell_edges) = match context {
BoxContext::Other => (false, None, None),
BoxContext::Cell { text, left, right, clamp } => (true, Some(text.unwrap_or(&empty_cell)), clamp.then_some((left, right))),
};
// Word 16.113.3: the override applies in modes 11/12/14, but not mode 15.
let allow_same_style_space =
in_cell && ctx.doc.settings.allow_space_of_same_style_in_table && matches!(ctx.doc.settings.compat_mode, 11 | 12 | 14);
let mut items = Vec::new();
// Pictures behind the text go first; `behind` counts them.
let mut behind = 0;
// Wrap areas of the box's floating objects.
let mut excl: Vec<WrapArea> = Vec::new();
// Areas of the box's floating tables, which other floating tables may have to avoid.
let mut float_tables: Vec<Rect> = Vec::new();
// Deepest a floating picture or shape that takes part in the text flow reaches, so the box
// (a table cell especially) grows to hold it instead of letting it hang past the box's own
// bottom edge (#3108). Behind-text and in-front-of-text objects don't: a page-anchored logo or
// watermark behind a cell's text must not stretch its row (see `align_h` in table.rs).
let mut float_objects_bottom = 0.0f32;
// A header in a document of Word 2010's layout or older (compatibility mode below 15): its
// text keeps its place beside its own objects, which wrap nothing, and only a top and bottom
// object reaching down from the text makes the header taller (#6805). Not in a table cell.
let legacy_header = !in_cell && is_legacy_header(ctx, story);
// Contour wraps were measured in mode 14; earlier modes keep using their existing rule.
let legacy_header_contours = legacy_header && ctx.doc.settings.compat_mode == 14;
// Top and bottom objects of such a header: where each starts and ends.
let mut legacy_reach: Vec<(f32, f32)> = Vec::new();
let mut table_areas = Vec::new();
let mut page_table_wraps = Vec::new();
let mut y = 0.0f32;
let mut prev_after = 0.0f32;
let mut prev_after_auto = false;
let mut prev_listed = false;
let mut prev_style: Option<(String, bool)> = None;
let mut last_top = 0.0f32;
// Word has no frames in a text box: a framed paragraph there (its style's frame, usually)
// runs with the box's text (#2357).
let frames = !matches!(story, StoryRef::Part(id) if ctx.doc.parts.get(&id).is_some_and(|p| p.kind == wordcraft_doc::PartKind::TextBox));
// The bottom of the last paragraph's lines.
let mut prev_end = 0.0f32;
// Where the last paragraph, whose mark is hidden, ended: the next one goes on from there.
let mut joint: Option<Joint> = None;
let outer_joined = ctx.joined;
for (i, b) in blocks.iter().enumerate() {
last_top = y;
let mut path = prefix.to_vec();
path.push(i as u32);
match &**b {
Block::Para(p) => {
// A paragraph after one whose mark is hidden goes on from that one's last line
// (Word, #3986; see `Joint`); one that shows nothing passes the place on.
let (mark_hidden, shows_nothing, taken) = Joint::arrive(ctx, &mut joint, (i, p), (width, frames), table);
let p = taken.as_ref().map_or(p, |t| &t.para);
let label = ctx.next_label(p);
let listed = label.is_some();
let mut pl = ctx.para_labelled(p, width, table, &[], label.clone());
// A text frame (a footer's page number in a frame, say) is placed apart, like a
// floating object, and the text after it wraps around it; it takes no room in
// the flow.
if let Some(fr) = pl.rp.frame.clone().filter(|_| frames) {
let fw = fr.w.unwrap_or_else(|| natural_width(&pl)).clamp(1.0, 1584.0);
let pf = if (fw - width).abs() > 0.01 { ctx.para_labelled(p, fw, table, &[], label) } else { pl };
let h = match fr.h {
Some(h) if fr.h_exact => h,
Some(h) => h.max(pf.height),
None => pf.height,
};
let float = frame_float(&fr);
let r = float_rect(frame, (0.0, width), y, fw, h, &float);
push_para(&mut items, (story, &path), &pf, (0, pf.lines.len()), (r.x, r.y), fw, Edges::own(&pf, 0, pf.lines.len()));
excl.extend(wrap_area(r, &float, None));
float_tables.push(r);
continue;
}
let (ctxl, before) = paragraph_spacing(ParagraphSpacing {
index: i,
listed,
style: &pl.rp.style,
contextual: pl.rp.contextual_spacing,
before_auto: pl.rp.before_auto,
space_before: pl.rp.space_before,
allow_same_style_space,
previous_style: prev_style.as_ref(),
previous_listed: prev_listed,
previous_after_auto: prev_after_auto,
previous_after: &mut prev_after,
y: &mut y,
sum_paragraph_spacing: ctx.doc.settings.sum_paragraph_spacing,
});
// Paragraph-relative floats start above the effective space before, as in the
// body path. Keep text and wrap exclusions relative to the text's actual top.
// A paragraph going on from a shared line has its origin at that line's top.
let para_top = taken.as_ref().map_or(y, |t| t.joint.top);
y += if i == 0 { before } else { before.max(0.0) };
TakenUp::start(taken.as_ref(), &mut y, &mut excl);
// Floating objects anchored here: place them, then wrap the text around them.
// One left out of the layout (hidden, or deleted in the final text) takes no room.
let mut floats = HashMap::new();
// A header or footer cell's floats placed on the page: no room taken in the cell.
let mut pinned = HashMap::new();
let left_out = left_out_objects(ctx.doc, p, table.map(|t| &t.chr), ctx.opts.show_hidden, ctx.hide_deleted);
for (oi, o) in p.objects.iter().enumerate() {
let Some((w, h, float)) = floating(o) else { continue };
if left_out(oi) {
continue;
}
let h = ctx.float_height(o, w, h);
if let Some(pin) = ctx.pin.as_ref().filter(|pin| in_cell && pin.story == story && float.outside_cell && page_anchored(float)) {
let page =
PageFrame { sect: &pin.sect, margin_top: pin.margin_top, origin: (0.0, 0.0), number: pin.number, mirror: pin.mirror };
pinned.insert(oi, float_rect(Some(page), (0.0, width), para_top, w, h, float));
continue;
}
// Only paragraph-relative placement excludes space-before. Line anchors
// and frameless page/margin fallbacks retain the text-top origin.
let float_y = if float.v_rel == Anchor::Paragraph { para_top } else { y };
let mut r = float_rect(frame, (0.0, width), float_y, w, h, float);
// Only move an offset-positioned float when its box crosses the outer
// cell edge. Cell text margins and wrap distances do not move the object.
if let Some((left, right)) = cell_edges
&& !float.outside_cell
&& float.h_align.is_none()
&& float.wrap != Wrap::InFrontOfText
{
r.x = keep_in_cell(r.x, r.w, left, right);
}
if (legacy_header && matches!(float.wrap, Wrap::TopAndBottom | Wrap::Square))
|| (legacy_header_contours && matches!(float.wrap, Wrap::Tight | Wrap::Through))
{
// These objects do not reflow the legacy header's own text in Word.
legacy_reach.extend(legacy_reach_of(float, r));
} else {
let outline = ctx.wrap_outline(o);
excl.extend(wrap_area(r, float, outline.as_deref().map(Vec::as_slice)));
}
floats.insert(oi, r);
}
let rel = rel_exclusions(&excl, 0.0, y);
if !rel.is_empty() {
pl = ctx.para_labelled(p, width, table, &rel, label);
}
TakenUp::clear(taken.as_ref(), &mut excl);
// Bordered paragraphs: room for their borders, those of a group joined in one box.
let cell = table.map(|t| &t.para);
let (above, below) = (joined(ctx.doc, blocks, i, i.checked_sub(1), cell), joined(ctx.doc, blocks, i, Some(i + 1), cell));
let mut edges = Edges::own(&pl, 0, pl.lines.len()).sharing(&pl);
if above {
edges.top = 0.0;
}
if below {
edges.bottom = 0.0;
}
edges.joined = [above, below];
let lead = pl.lines.first().map_or(0.0, |l| l.top.max(0.0)) + edges.top;
if above {
edges.up = (y + lead - prev_end).max(0.0);
}
prev_end = push_para(&mut items, (story, &path), &pl, (0, pl.lines.len()), (0.0, y + lead), width, edges);
joint = Joint::after(&pl, (0, pl.lines.len()), i, (0.0, y + lead), edges.bottom).filter(|_| mark_hidden && !shows_nothing).or(joint);
// Pictures, shapes and text boxes: drawn, their areas and text boxes' text.
if !p.objects.is_empty() {
let at = ObjFrame {
page: frame,
col: (0.0, width),
para_y: para_top,
table_chr: table.map(|t| &t.chr),
pinned: Some(&pinned),
late: None,
};
let (back, front) = place_objects(
ctx,
&ObjectPlacement {
story,
path: &path,
p,
pl: &pl,
lines: (0, pl.lines.len()),
origin: (0.0, y + lead),
at: &at,
floats: &floats,
depth,
},
);
for placed in back.iter().chain(front.iter()) {
if let Placed::Object { rect, spin, wrap, .. } = placed
&& !matches!(wrap, Wrap::Inline | Wrap::BehindText | Wrap::InFrontOfText)
{
float_objects_bottom = float_objects_bottom.max(spin.bounds(*rect).bottom());
}
}
let at = behind.min(items.len());
behind += back.len();
items.splice(at..at, back);
items.extend(front);
}
// Automatic space after: none after the last paragraph of a cell or box. Nor
// contextual spacing after a cell's last paragraph in the default style (or an
// undefined one, formatted as Normal): the end-of-cell mark is a Normal paragraph.
let last = i + 1 == blocks.len();
let normal = || pl.rp.style == "Normal" || ctx.doc.styles.get(&pl.rp.style).is_none();
let after = if last && (pl.rp.after_auto || (in_cell && ctxl && normal())) { 0.0 } else { pl.rp.space_after };
y += edges.top + pl.height + edges.bottom;
// One going on from the line of the paragraph before ends below that line (#3986).
y = TakenUp::end(taken.as_ref(), ctx, (y, after), outer_joined);
prev_after = after;
prev_after_auto = pl.rp.after_auto;
prev_listed = listed;
prev_style = Some((pl.rp.style.clone(), ctxl));
}
Block::Table(t) => {
if !layout_table_block(TableBlockContext {
ctx,
story,
table: t,
path: &path,
width,
frame,
in_cell,
depth,
y: &mut y,
items: &mut items,
exclusions: &mut excl,
float_tables: &mut float_tables,
table_areas: &mut table_areas,
page_table_wraps: &mut page_table_wraps,
previous_after: &mut prev_after,
previous_style: &mut prev_style,
}) {
continue;
}
}
}
}
y = y.max(legacy_header_reach(y, &legacy_reach));
let floats_bottom = float_tables.iter().map(|r| r.bottom()).fold(float_objects_bottom, f32::max);
ctx.hide_deleted = outer_hide;
ctx.joined = outer_joined;
BoxLayout { items, height: y.max(0.0), floats_bottom, last_top: last_top.max(0.0), table_areas, page_table_wraps }
}
/// The width a text frame without one takes: its widest line, with the paragraph's indents.
fn natural_width(pl: &ParaLayout) -> f32 {
let widest = (0..pl.lines.len())
.filter_map(|li| {
let l = pl.lines.get(li)?;
Some((pl.x_of(li, l.stop)? - pl.x_of(li, l.start)?).abs())
})
.fold(0.0f32, f32::max);
widest + pl.rp.indent_left.max(0.0) + pl.rp.indent_right.max(0.0)
}
/// A text frame's placement as a floating object's (`w:framePr` → anchors, alignment, wrap).
fn frame_float(fr: &wordcraft_doc::props::TextFrame) -> Float {
let anchor = |a: &str, text: Anchor| match a {
"page" => Anchor::Page,
"margin" => Anchor::Margin,
_ => text,
};
let align = |a: Option<&str>| match a {
Some("left" | "top") => Some(FloatAlign::Start),
Some("center") => Some(FloatAlign::Center),
Some("right" | "bottom") => Some(FloatAlign::End),
Some("inside") => Some(FloatAlign::Inside),
Some("outside") => Some(FloatAlign::Outside),
_ => None,
};
Float {
wrap: match fr.wrap.as_str() {
"notBeside" => Wrap::TopAndBottom,
"none" => Wrap::InFrontOfText,
"tight" => Wrap::Tight,
"through" => Wrap::Through,
_ => Wrap::Square,
},
h_rel: anchor(&fr.h_anchor, Anchor::Column),
v_rel: anchor(&fr.v_anchor, Anchor::Paragraph),
x: fr.x,
y: fr.y,
h_align: align(fr.x_align.as_deref()),
v_align: align(fr.y_align.as_deref()),
dist: fr.h_space,
dist_top: fr.v_space,
dist_bottom: fr.v_space,
..Default::default()
}
}
/// A laid-out box: items relative to its top-left, the height of its text flow, where its
/// floating tables end (a table cell grows to hold them) and where its last block starts (the
/// height without it).
pub(crate) struct BoxLayout {
pub(crate) items: Vec<Placed>,
pub(crate) table_areas: Vec<Rect>,
pub(crate) page_table_wraps: Vec<WrapArea>,
pub(crate) height: f32,
pub(crate) floats_bottom: f32,
pub(crate) last_top: f32,
}
/// Where floating table `tl` goes in a box `width` wide whose next paragraph starts at `y`, and
/// the area text keeps clear of. Positions relative to the page or its margins need the box's
/// `frame`; without one (a table cell) they're relative to the box. `legacy` is the first cell's
/// left margin before Word 2013 layout, `None` in it.
fn box_float_table_rect(
frame: Option<PageFrame>,
width: f32,
y: f32,
tl: &table::TableLayout,
f: &TableFloat,
legacy: Option<f32>,
float_tables: &[Rect],
) -> (Rect, Rect) {
let h = tl.rows.iter().map(|r| r.height).sum::<f32>().clamp(0.0, 100_000.0);
let w = tl.width.clamp(1.0, 100_000.0);
// Across, as laid out before an even page is mirrored (see `float_rect`).
let page_x = |fr: &PageFrame| {
let (left, _, shift) = fr.sides();
match f.h_rel {
Anchor::Page => Some((-shift, fr.sect.page_w)),
Anchor::Margin => Some((left - shift, fr.sect.text_width())),
_ => None,
}
};
let page_y = |fr: &PageFrame| {
let s = fr.sect;
match f.v_rel {
Anchor::Page => Some((0.0, s.page_h)),
Anchor::Margin => {
let mut reference = s.clone();
reference.margin_top = fr.margin_top.unwrap_or(s.margin_top);
Some((reference.margin_top, reference.text_height()))
}
_ => None,
}
};
let h_area = frame.and_then(|fr| page_x(&fr).map(|(a, e)| (a - fr.origin.0, e))).unwrap_or((0.0, width));
// In a cell an offset from the page or margin has nothing to measure from: the table stands
// where the text is.
let (v_area, y_off) = match frame.and_then(|fr| page_y(&fr).map(|(a, e)| (a - fr.origin.1, e))) {
Some(area) => (area, f.y),
None if matches!(f.v_rel, Anchor::Page | Anchor::Margin) => ((y, 0.0), 0.0),
None => ((y, 0.0), f.y),
};
// Nor is there an area to align in: the table starts where the text is.
let v_align = if frame.is_none() && matches!(f.v_rel, Anchor::Page | Anchor::Margin) { None } else { f.v_align };
let x = align_in(h_area, f.x, w, PageFrame::h_align(frame.as_ref(), f.h_align)) - if f.h_align.is_none() { legacy.unwrap_or(0.0) } else { 0.0 };
let r = clear_of_float_tables(Rect::new(x, align_in(v_area, y_off, h, v_align), w, h), f, legacy.is_none(), h_area.0, float_tables);
let [dl, dt, dr, db] = f.dist_from_text();
(r, r.inset(-dl, -dt, -dr, -db))
}
/// Floating table `f` at `r`, moved below the floating tables in its way when it may not
/// overlap them. In Word 2013+ layout (`modern`) one placed by an offset keeps its distance from
/// the text's left edge (`text_left`); before it, it keeps its own left edge, also when its first
/// cell has no left margin.
pub(crate) fn clear_of_float_tables(mut r: Rect, f: &TableFloat, modern: bool, text_left: f32, others: &[Rect]) -> Rect {
let [dl, dt, dr, db] = f.dist_from_text();
let keeps_text_distance = modern && f.h_align.is_none() && f.x >= 0.0 && matches!(f.h_rel, Anchor::Margin | Anchor::Column);
for _ in 0..64 {
let area = r.inset(-dl, -dt, -dr, -db);
let Some(below) = others.iter().filter(|o| !f.overlap && o.intersects(&area)).map(|o| o.bottom()).reduce(f32::max) else { break };
r.y = below + dt;
if keeps_text_distance {
r.x = r.x.max(text_left + dl);
}
}
r
}
/// Keep a float's box within the outer cell edges, relative to the cell's text origin.
/// Offsets that fit are unchanged, including those inside the text margins. Wrap distances
/// affect surrounding text only. An oversized object starts at the outer left edge.
pub(crate) fn keep_in_cell(x: f32, w: f32, left: f32, right: f32) -> f32 {
let left = if left.is_finite() { left } else { 0.0 };
let end = right - w;
if !end.is_finite() || end <= left {
return left;
}
// max/min also place a non-finite offset at a finite edge without panicking.
x.max(left).min(end)
}
/// A text box's default internal margins (Word's: 0.1" left/right, 0.05" top/bottom); a box's
/// own come from its [`wordcraft_doc::TextBody`].
#[cfg(test)]
pub(crate) const BOX_INSET_X: f32 = 7.2;
#[cfg(test)]
pub(crate) const BOX_INSET_Y: f32 = 3.6;
/// Keep what fits in a box `height` tall (items relative to its top): lines that don't fit are
/// dropped (whole lines; a line that only partly fits too) and so is anything starting below.
/// The first line always stays, so even a tiny box shows the start of its text.
pub(crate) fn fit_box(items: Vec<Placed>, height: f32) -> Vec<Placed> {
let limit = height + 0.5;
let mut out = Vec::with_capacity(items.len());
let mut kept_line = false;
for it in items {
if let Some(r) = it.turned_bounds() {
// Turned text (in a table cell) stays whole.
if r.y < limit {
out.push(it);
}
continue;
}
match it {
Placed::Lines { story, path, para, l0, l1, x, y, turn } => {
let Some(first) = para.lines.get(l0) else { continue };
let mut end = l0;
for k in l0..l1 {
let Some(l) = para.lines.get(k) else { break };
if y + (l.top - first.top) + l.height > limit && kept_line {
break;
}
end = k + 1;
kept_line = true;
}
if end > l0 {
out.push(Placed::Lines { story, path, para, l0, l1: end, x, y, turn });
}
}
Placed::Fill { rect, color } if rect.y < limit => {
out.push(Placed::Fill { rect: Rect::new(rect.x, rect.y, rect.w, rect.h.min(limit - rect.y)), color });
}
Placed::Rule { x0, y0, x1, y1, border } if y0.min(y1) < limit => {
out.push(Placed::Rule { x0, y0: y0.min(limit), x1, y1: y1.min(limit), border });
}
Placed::Image { rect, .. }
| Placed::Shape { rect, .. }
| Placed::Graphic { rect, .. }
| Placed::Clip { rect, .. }
| Placed::Cell { rect, .. }
| Placed::Object { rect, .. }
if rect.y < limit =>
{
out.push(it);
}
_ => {}
}
}
out
}
/// Word joins neighbouring paragraphs with the same borders and indents into one box: the top
/// border goes above the first of them only, the bottom one below the last, and the shading and
/// side borders run on through the space between them (`table`: the cell's paragraph properties).
fn border_group(doc: &Document, a: &Paragraph, b: &Paragraph, table: Option<&wordcraft_doc::props::ParaProps>) -> bool {
let (a, b) = (doc.styles.resolve_para_in(&a.props, table), doc.styles.resolve_para_in(&b.props, table));
a.borders.is_some() && a.borders == b.borders && a.indent_left == b.indent_left && a.indent_right == b.indent_right
}
/// Whether paragraph `i` of `blocks` and the one at `j` are of one border group.
pub(crate) fn joined(doc: &Document, blocks: &Blocks, i: usize, j: Option<usize>, table: Option<&wordcraft_doc::props::ParaProps>) -> bool {
match (blocks.get(i).map(|b| &**b), j.and_then(|j| blocks.get(j)).map(|b| &**b)) {
(Some(Block::Para(a)), Some(Block::Para(b))) => border_group(doc, a, b, table),
_ => false,
}
}
/// The room a paragraph's top or bottom border takes in the text flow: its distance from the
/// text plus its width (Word).
fn border_room(b: Option<Border>) -> f32 {
b.filter(Border::is_visible).map_or(0.0, |b| {
let extent = b.space + b.width;
if extent.is_finite() { extent.max(0.0) } else { 0.0 }
})
}
/// How a piece of a paragraph meets the paragraphs around it: the room taken by its top and
/// bottom borders (0: none drawn there — the paragraph goes on from or into one of its border
/// group, or it is split there) and how far its shading and side borders reach up to join the
/// paragraph above in its group, and whether it goes on from the paragraph above and into the one
/// below in that group (its side lines don't run on past those ends).
#[derive(Clone, Copy, Default)]
pub(crate) struct Edges {
pub(crate) top: f32,
pub(crate) bottom: f32,
pub(crate) up: f32,
pub(crate) joined: [bool; 2],
}
impl Edges {
/// A paragraph on its own: its top and bottom borders at its ends.
pub(crate) fn own(pl: &ParaLayout, l0: usize, l1: usize) -> Edges {
let b = pl.rp.borders.unwrap_or_default();
Edges {
top: if l0 == 0 { border_room(b.top) } else { 0.0 },
bottom: if l1 >= pl.lines.len() { border_room(b.bottom) } else { 0.0 },
up: 0.0,
joined: [false; 2],
}
}
/// A paragraph going on from the line of the one before, whose mark is hidden (#3986), takes
/// no room for its borders on that line, nor below it when that line is all it has: the
/// shared line shows the first paragraph's shading and borders (Word).
pub(crate) fn sharing(mut self, pl: &ParaLayout) -> Edges {
if pl.shares_line() {
self.top = 0.0;
if pl.lines.len() <= 1 {
self.bottom = 0.0;
}
}
self
}
}
/// Where a paragraph whose mark is hidden ends (#3986). Word shows it on one line with the next
/// paragraph: that one's text goes on from where this one's ends, beside it on its last line.
/// Paragraphs that show nothing (an empty one with a hidden mark, say) pass the place on.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Joint {
/// The block that goes on from here.
pub(crate) next: usize,
/// The page (the count of pages) and column of the last line (body text only).
pub(crate) page: usize,
pub(crate) col: usize,
/// The last line's top, height and baseline (from its top), the left edge of its column and
/// the x its text ends at.
pub(crate) top: f32,
pub(crate) height: f32,
baseline: f32,
left: f32,
end: f32,
/// The room under the line for the paragraph's bottom border: what follows the shared line
/// starts below it.
room: f32,
}
impl Joint {
/// After lines `l0..l1` of paragraph layout `pl`, block `block`, the first of them at `y` in
/// a column whose left edge is at `x`, with `room` under them for its bottom border.
pub(crate) fn after(pl: &ParaLayout, (l0, l1): (usize, usize), block: usize, (x, y): (f32, f32), room: f32) -> Option<Joint> {
let li = l1.checked_sub(1)?;
let (first, last) = (pl.lines.get(l0)?, pl.lines.get(li)?);
// Right-to-left text runs the other way: its lines aren't joined.
if pl.rp.bidi || !last.vis.is_empty() {
return None;
}
let end = pl.x_of(li, pl.text_len)?;
let (top, height, baseline) = (y + last.top - first.top, last.height, last.baseline - last.top);
Some(Joint { next: block + 1, page: 0, col: 0, top, height, baseline, left: x, end: x + end, room })
}
/// Paragraph `p`, block `i`, arriving at `joint`: whether its mark is hidden, whether it shows
/// nothing, and how it goes on from the place the one before left (if that one's mark was hidden).
fn arrive(
ctx: &mut Ctx,
joint: &mut Option<Joint>,
(i, p): (usize, &Paragraph),
(width, frames): (f32, bool),
table: Option<&para::CellText>,
) -> (bool, bool, Option<TakenUp>) {
let chr = table.map(|t| &t.chr);
let mark_hidden = ctx.mark_hidden(p, chr);
let shows_nothing = mark_hidden && ctx.text_hidden(p, chr);
let came = joint.take().filter(|j| j.next == i);
*joint = came.filter(|_| shows_nothing).map(|j| Joint { next: i + 1, ..j });
let taken = came.filter(|_| !shows_nothing).and_then(|j| j.take_up(ctx, p, (width, frames), table));
(mark_hidden, shows_nothing, taken)
}
/// The bottom of the shared line, with the room for the first paragraph's bottom border.
pub(crate) fn bottom(&self) -> f32 {
self.top + self.height + self.room
}
/// How the next paragraph `q`, laid out `width` wide, goes on from here: how far below the
/// shared line's top it starts (its first line's text sits on this paragraph's baseline, not
/// above it), and the areas it keeps clear of: its first line, this paragraph's text, down to
/// the bottom of that line only; its later lines, the rest of the shared line (a heading's,
/// taller than the text going on from it), with the room for its bottom border (Word).
pub(crate) fn fit(&self, ctx: &mut Ctx, q: &Paragraph, width: f32, table: Option<&para::CellText>) -> (f32, Vec<WrapArea>) {
let eq = ctx.eq_count;
let probe = again(ctx, eq, |ctx| ctx.para_labelled(q, width, table, &[], None));
let (gap, height) = probe.lines.first().map_or((0.0, self.height), |l| ((self.baseline - (l.baseline - l.top)).max(0.0), l.height));
let rect = Rect::new(self.left, self.top, (self.end - self.left).max(0.0), (gap + height - 0.01).max(0.01));
let mut areas = vec![(rect, false, None, WrapText::Right, false)];
// Just below the first row as the line breaker tests it: the breaker tests every row at
// its estimate (`row_est`), not at the line's height, so the first row never meets this
// area and every later row, starting lower, does and moves below it. The probe's own first
// line can be taller than the real one (a large word that wraps off the narrower shared
// line), so its height can't be used here.
let below = self.top + gap + probe.row_est + 0.01;
if self.bottom() > below {
areas.push((Rect::new(self.left, below, width.max(0.01), self.bottom() - below), true, None, WrapText::BothSides, false));
}
(gap, areas)
}
/// The areas [`Joint::fit`] gave taken out of `excl` again, the paragraph laid out.
pub(crate) fn unfit(areas: &[WrapArea], excl: &mut Vec<WrapArea>) {
for area in areas {
if let Some(k) = excl.iter().position(|a| a.0 == area.0 && a.1 == area.1 && a.2.is_none() && a.3 == area.3) {
excl.remove(k);
}
}
}
/// Paragraph `p` of a box `width` wide going on from here, unless a page break comes before
/// it, it runs right to left or it is a text frame (`frames`: placed apart, sharing no line):
/// without its space before, as [`Joint::fit`] lays it out.
fn take_up(self, ctx: &mut Ctx, p: &Paragraph, (width, frames): (f32, bool), table: Option<&para::CellText>) -> Option<TakenUp> {
let own = ctx.doc.styles.resolve_para_in(&p.props, table.map(|t| &t.para));
if own.page_break_before || own.bidi || (frames && own.frame.is_some()) {
return None;
}
let mut para = p.clone();
para.props.space_before = Some(0.0);
para.props.space_before_auto = Some(false);
ctx.joined = Some(Joining::new(para.rev, 0.0));
let (gap, areas) = self.fit(ctx, &para, width, table);
Some(TakenUp { joint: self, para, gap, areas })
}
}
/// A paragraph of a box going on from a [`Joint`]: the paragraph without its space before, how
/// far below the shared line's top it starts and the areas its lines keep clear of.
struct TakenUp {
joint: Joint,
para: Paragraph,
gap: f32,
areas: Vec<WrapArea>,
}
impl TakenUp {
/// Where it starts: `y` below the shared line's top, its lines keeping clear of that line.
fn start(taken: Option<&TakenUp>, y: &mut f32, excl: &mut Vec<WrapArea>) {
let Some(t) = taken else { return };
*y = t.joint.top + t.gap;
excl.extend(t.areas.iter().cloned());
}
/// Its lines laid out: the areas they kept clear of come out of `excl`.
fn clear(taken: Option<&TakenUp>, excl: &mut Vec<WrapArea>) {
if let Some(t) = taken {
Joint::unfit(&t.areas, excl);
}
}
/// Where what follows a paragraph ending at `y`, with `after` its space after, starts: below
/// the shared line at least, with the first paragraph's bottom border. With the copy laid
/// out, the paragraph's own layouts are its own again (`outer`).
fn end(taken: Option<&TakenUp>, ctx: &mut Ctx, (y, after): (f32, f32), outer: Option<Joining>) -> f32 {
let Some(t) = taken else { return y + after };
ctx.joined = outer;
y.max(t.joint.bottom()) + after
}
}
/// Paragraph lines plus their shading and borders; returns the bottom of the lines. The
/// shading fills the box out to the borders, the room they take included (Word).
pub(crate) fn push_para(
items: &mut Vec<Placed>,
(story, path): (StoryRef, &[u32]),
pl: &Arc<ParaLayout>,
(l0, l1): (usize, usize),
(x, y): (f32, f32),
width: f32,
e: Edges,
) -> f32 {
let (Some(first), Some(last)) = (pl.lines.get(l0), l1.checked_sub(1).and_then(|k| pl.lines.get(k))) else {
return y;
};
let h = last.top + last.height - first.top;
let lines = Placed::Lines { story, path: Path(path.to_vec()), para: pl.clone(), l0, l1, x, y, turn: TextDirection::Horizontal };
// The line a paragraph shares with the one before, whose mark is hidden, shows that one's
// shading and borders: this one's start on its own next line, without a top border (Word, #3986).
let shared = l0 == 0 && pl.shares_line();
let Some(own) = pl.lines.get(if shared { 1 } else { l0 }).filter(|_| !shared || l1 > 1) else {
items.push(lines);
return y + h;
};
let (y_own, e) = if shared { (y + own.top - first.top, Edges { top: 0.0, up: 0.0, ..e }) } else { (y, e) };
// Indents are logical: a right-to-left paragraph's start indent (and hanging indent) is on the right.
let start = pl.rp.indent_left.min(pl.rp.indent_left + pl.rp.indent_first);
let (li, ri) = if pl.rp.bidi { (pl.rp.indent_right, start) } else { (start, pl.rp.indent_right) };
let left = x + li;
let right = x + width - ri;
let (top, bottom) = (y_own - e.top - e.up, y + h + e.bottom);
// The outer edges of the side borders.
let sides = pl.rp.borders.unwrap_or_default();
let (lx, rx) = (left - border_room(sides.left), right + border_room(sides.right));
if let Some(c) = pl.rp.shading {
let pad = pl.rp.borders.as_ref().map(|b| b.left.map(|l| l.space).unwrap_or(4.0)).unwrap_or(0.0);
let (sl, sr) = (lx.min(left - pad), rx.max(right + pad));
items.push(Placed::Fill { rect: Rect::new(sl, top, sr - sl, bottom - top), color: c });
}
items.push(lines);
if let Some(b) = &pl.rp.borders {
// All four lines lie beyond their distance from the text (`lx`, `rx`: the side lines'
// outer edges); `box_rules` runs the top and bottom over the side lines to those edges.
let half = |o: Option<Border>| o.filter(Border::is_visible).map_or(0.0, |t| t.width / 2.0);
let (cl, cr) = (lx + half(b.left), rx - half(b.right));
let tb = b.top.filter(|t| e.top > 0.0 && t.is_visible());
let bb = b.bottom.filter(|t| e.bottom > 0.0 && t.is_visible());
let ty = tb.map_or(top, |t| y_own - t.space - t.width / 2.0);
let by = bb.map_or(bottom, |t| y + h + t.space + t.width / 2.0);
// Open at the top where it goes on over the shared line.
box_rules(items, (cl, ty, cr, by), [tb, bb, b.left, b.right], [e.joined[0] || shared, e.joined[1]]);
}
y + h
}
/// The visible lines of a border box with corners `(x0, y0)` and `(x1, y1)` (`[top, bottom, left,
/// right]`), ending square as Word draws them: a top or bottom line runs on over half the side
/// line it meets, or half its own width where there is none, so single-line boxes close at the
/// corners; a side line runs on to the outer edge of the top or bottom line it meets, or by half
/// its own width where there is none, unless the box goes on there (`joined`: at the top, at the
/// bottom).
pub(crate) fn box_rules(items: &mut Vec<Placed>, (x0, y0, x1, y1): (f32, f32, f32, f32), sides: [Option<Border>; 4], joined: [bool; 2]) {
let [top, bottom, left, right] = sides.map(|b| b.filter(Border::is_visible));
let half = |t: &Border| display::rule_width(t) / 2.0;
for (t, y) in [(top, y0), (bottom, y1)] {
if let Some(t) = t {
let over = |side: Option<Border>| half(&side.unwrap_or(t));
items.push(Placed::Rule { x0: x0 - over(left), y0: y, x1: x1 + over(right), y1: y, border: t });
}
}
for (t, x) in [(left, x0), (right, x1)] {
if let Some(t) = t {
let end = |o: Option<Border>, joined: bool| if joined { 0.0 } else { half(&o.unwrap_or(t)) };
items.push(Placed::Rule { x0: x, y0: y0 - end(top, joined[0]), x1: x, y1: y1 + end(bottom, joined[1]), border: t });
}
}
}