Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor 7937840ffb style: rustfmt 2026-11-03 17:20:00 +00:00
Dylan M. Taylor f9c089f722 fix(layout): linked text boxes copy and keep only what they show 2026-11-03 11:50:09 +00:00
7 changed files with 54 additions and 42 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+2 -5
View File
@@ -474,17 +474,14 @@ fn parse_blocks(lines: &[String], depth: usize, level: u8, refs: &Refs) -> Vec<F
let p = Para { align: a, inlines: parse_inlines(s, &Fmt::default(), depth, refs), ..Default::default() };
Cell { blocks: vec![FBlock::Para(p)], header, ..Default::default() }
};
// A document row keeps MAX_COLS cells: only those are built, so a wide header
// doesn't pad every sparse row below it out to its width.
let cols = aligns.len().min(wordcraft_doc::table::MAX_COLS);
table.rows.push(head.iter().zip(&aligns).take(cols).map(|(s, a)| cell(s, true, *a)).collect());
table.rows.push(head.iter().zip(&aligns).map(|(s, a)| cell(s, true, *a)).collect());
i += 2;
while let Some(l) = lines.get(i) {
if is_blank(l) || !l.contains('|') || interrupts(l) {
break;
}
let mut cells = split_row(l);
cells.resize(cols, String::new());
cells.resize(aligns.len(), String::new());
table.rows.push(cells.iter().zip(&aligns).map(|(s, a)| cell(s, false, *a)).collect());
i += 1;
}
-14
View File
@@ -714,17 +714,3 @@ fn exported_link_destinations_read_back_unchanged() {
assert_eq!(md("https://example.com/a b"), "[x](<https://example.com/a b>)");
assert_eq!(md("https://e.com/x\ny"), "[x](https://e.com/xy)");
}
#[test]
fn a_wide_table_builds_only_the_cells_a_row_keeps() {
// A 5,000-column header padded every one-cell row below it to 5,000 cells.
let n = 5000;
let md = format!("{}|\n{}|\n{}", "|h".repeat(n), "|-".repeat(n), "|x|\n".repeat(200));
let flow = parse(&md);
let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") };
assert_eq!(t.rows.len(), 201);
assert!(t.rows.iter().all(|r| r.len() == wordcraft_doc::table::MAX_COLS));
// A narrow table is unchanged.
let FBlock::Table(t) = &parse("| a | b |\n| - | - |\n| x |\n").blocks[0] else { panic!("expected table") };
assert_eq!(t.rows[1].len(), 2);
}
+3 -1
View File
@@ -519,7 +519,9 @@ pub(crate) const BOX_INSET_Y: f32 = 3.6;
/// 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());
// Sized to what is kept, not to `items`: a box showing a few lines of a long story keeps
// no room for the rest of it.
let mut out = Vec::new();
let mut kept_line = false;
for it in items {
if let Some(r) = it.turned_bounds() {
+14
View File
@@ -294,3 +294,17 @@ fn a_kept_outline_holds_on_to_its_pictures_allocation() {
drop(disc);
assert!(weak.upgrade().is_none(), "the bytes are freed with the document; only their allocation is kept");
}
#[test]
fn a_box_keeps_room_only_for_what_it_shows() {
// A linked box showing a few lines of a long story kept room for all of it (each of up to
// 256 boxes), and copied the whole shared layout before cutting it.
let color = wordcraft_doc::Rgb(0, 0, 0);
let items: Vec<Placed> = (0..10_000).map(|i| Placed::Fill { rect: Rect::new(0.0, i as f32 * 10.0, 5.0, 10.0), color }).collect();
let kept = boxes::fit_box(items.clone(), 25.0);
assert_eq!(kept.len(), 3);
assert!(kept.capacity() < 100, "{}", kept.capacity());
let below = textbox::from_y(&items, 99_950.0);
assert_eq!(below.len(), 6);
assert!(below.capacity() < 100, "{}", below.capacity());
}
+32 -19
View File
@@ -252,22 +252,35 @@ fn flow(ctx: &mut Ctx, chain: &[u32], depth: usize) -> Vec<Vec<Placed>> {
// Past the cap the box takes the nearest layout in its direction: the widest line length
// that fits it, else the narrowest. The first box in a direction is laid out (one more
// layout for each of the few directions at most).
let near = || layouts.iter().filter(|((_, v), _)| *v == key.1).min_by_key(|((k, _), _)| (*k > key.0, k.abs_diff(key.0)));
let kept = layouts.iter().find(|(k, _)| *k == key).or_else(|| if layouts.len() >= MAX_FLOW_WIDTHS { near() } else { None });
let items = match kept {
Some((_, items)) => items.clone(),
let near = || {
layouts
.iter()
.enumerate()
.filter(|(_, ((_, v), _))| *v == key.1)
.min_by_key(|(_, ((k, _), _))| (*k > key.0, k.abs_diff(key.0)))
.map(|(i, _)| i)
};
let kept = layouts.iter().position(|(k, _)| *k == key).or_else(|| if layouts.len() >= MAX_FLOW_WIDTHS { near() } else { None });
let at = match kept {
Some(at) => at,
None => {
(ctx.counters, ctx.eq_count) = (snap.0.clone(), snap.1);
let items = lay(ctx, head, &blocks, len, body.vert, depth);
after.get_or_insert_with(|| (ctx.counters.clone(), ctx.eq_count));
layouts.push((key, items.clone()));
items
layouts.push((key, items));
layouts.len() - 1
}
};
// The shared layout is read in place: a box copies only the items from its own top
// down, not the whole story first.
let Some((_, shared)) = layouts.get(at) else {
out.push(Vec::new());
continue;
};
let items = match &cut {
None => items,
Some(cut) => match cut_y(&items, cut) {
Some(top) => from_y(items, top),
None => shared.clone(),
Some(cut) => match cut_y(shared, cut) {
Some(top) => from_y(shared, top),
None => {
out.push(Vec::new());
continue;
@@ -298,28 +311,28 @@ fn cut_y(items: &[Placed], cut: &Cut) -> Option<f32> {
}
/// The items from `top` down, moved up to start at 0.
fn from_y(items: Vec<Placed>, top: f32) -> Vec<Placed> {
pub(crate) fn from_y(items: &[Placed], top: f32) -> Vec<Placed> {
let lim = top - 0.5;
let mut out = Vec::with_capacity(items.len());
let mut out = Vec::new();
for it in items {
if let Some(r) = it.turned_bounds() {
if r.y >= lim {
out.push(it);
out.push(it.clone());
}
continue;
}
match it {
Placed::Lines { story, path, para, l0, l1, x, y, turn } => {
let Some(first) = para.lines.get(l0) else { continue };
let Some(k) = (l0..l1).find(|k| para.lines.get(*k).is_some_and(|l| y + l.top - first.top >= lim)) else { continue };
let ky = para.lines.get(k).map_or(y, |l| y + l.top - first.top);
out.push(Placed::Lines { story, path, para, l0: k, l1, x, y: ky, turn });
let Some(first) = para.lines.get(*l0) else { continue };
let Some(k) = (*l0..*l1).find(|k| para.lines.get(*k).is_some_and(|l| y + l.top - first.top >= lim)) else { continue };
let ky = para.lines.get(k).map_or(*y, |l| y + l.top - first.top);
out.push(Placed::Lines { story: *story, path: path.clone(), para: para.clone(), l0: k, l1: *l1, x: *x, y: ky, turn: *turn });
}
Placed::Fill { rect, color } if rect.bottom() > lim => {
let y0 = rect.y.max(top);
out.push(Placed::Fill { rect: Rect::new(rect.x, y0, rect.w, rect.bottom() - y0), color });
out.push(Placed::Fill { rect: Rect::new(rect.x, y0, rect.w, rect.bottom() - y0), color: *color });
}
Placed::Rule { y0, y1, .. } if y0.max(y1) >= lim => out.push(it),
Placed::Rule { y0, y1, .. } if y0.max(*y1) >= lim => out.push(it.clone()),
Placed::Image { rect, .. }
| Placed::Shape { rect, .. }
| Placed::Graphic { rect, .. }
@@ -328,7 +341,7 @@ fn from_y(items: Vec<Placed>, top: f32) -> Vec<Placed> {
| Placed::Object { rect, .. }
if rect.y >= lim =>
{
out.push(it)
out.push(it.clone())
}
_ => {}
}
+1 -1
View File
File diff suppressed because one or more lines are too long