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
5 changed files with 52 additions and 22 deletions
+2 -1
View File
File diff suppressed because one or more lines are too long
+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