mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 02:26:48 +00:00
527 lines
27 KiB
Rust
527 lines
27 KiB
Rust
use super::common::{WidgetRest, clamp_opt, divide, is_row, moved, solid_extent, split_line, split_widget, splittable, widget_box};
|
||
use super::items::Item;
|
||
use super::{
|
||
Ctx, Frame, Layout, Layouter, MAX_DEPTH, MAX_INSTANCES, MAX_MEASURES, Node, Rect, SomIndices, Splitting, Subform, XfaError, data_path,
|
||
subform_instances,
|
||
};
|
||
|
||
impl Layouter<'_> {
|
||
/// Place `node` as one block of size (`w`, `h`) at the cursor: in this content area when it fits,
|
||
/// in the next when it fits there, and across as many as it takes when it is taller than a
|
||
/// content area (a positioned or fixed-height subform holding more than a page).
|
||
fn flow_block(&mut self, node: &Node, x: f64, size: (f64, f64), ctx: &Ctx, depth: usize, sib: usize) -> Result<(), XfaError> {
|
||
self.place_block(node, x, size, ctx, depth, sib, true)
|
||
}
|
||
|
||
/// [`Self::flow_block`]; `may_split` is false when a field that might have been split turned
|
||
/// out not to be (see below), to place it as any other block.
|
||
#[allow(clippy::too_many_arguments)]
|
||
fn place_block(&mut self, node: &Node, x: f64, (w, h): (f64, f64), ctx: &Ctx, depth: usize, sib: usize, may_split: bool) -> Result<(), XfaError> {
|
||
let (rect, y, _) = self.cursor();
|
||
// A field a page break may split starts here, and what doesn't fit goes on in the next
|
||
// content area (XFA 3.3 pp. 291–292), rather than the whole field moving there.
|
||
let splits = may_split && matches!(node, Node::Field(f) if f.splits_at_page_break());
|
||
// A growable flowing subform (`lr-tb`/`rl-tb`, no `h`) is split where its objects allow
|
||
// ("splittable: in margins and where consensus exists among contained objects", XFA 3.3
|
||
// p. 1520), so it starts here and the rest goes on in the next content area, as one
|
||
// taller than a content area does. Anything else that fits one moves there whole.
|
||
let flows_on = matches!(node, Node::Subform(s) if matches!(s.layout, Layout::LrTb | Layout::RlTb) && s.common.h.is_none());
|
||
if !splits && !flows_on && y + h > rect.bottom() + 0.01 && h <= rect.h + 0.01 {
|
||
self.make_room(h, 0.0)?;
|
||
}
|
||
let (rect, y, _) = self.cursor();
|
||
let first = self.pages.last().map_or(0, |p| p.items.len());
|
||
// What drawing it changes besides the page, to undo if it then isn't split: only a field that
|
||
// doesn't fit here can turn out so. The item count goes back with the items taken off the
|
||
// page; the measurement budget doesn't, as that work was done. (A text field draws no
|
||
// picture, so the picture budget is untouched.)
|
||
let undo = (splits && y + h > rect.bottom() + 0.01).then(|| (self.fields, self.names.clone(), self.taken.clone(), self.items));
|
||
self.leaves.clear();
|
||
self.field_border = None;
|
||
self.draw_node(node, Rect::new(x, y, w, h), ctx, depth, sib)?;
|
||
let border_items = self.field_border.take();
|
||
let page_h = self.pages.last().map_or(f64::INFINITY, |p| p.height);
|
||
// What was drawn, when it is lower than the block's own height says (content that
|
||
// outgrows a fixed height) and ends off the page.
|
||
let drawn = self
|
||
.pages
|
||
.last()
|
||
.and_then(|p| p.items.get(first..))
|
||
.unwrap_or_default()
|
||
.iter()
|
||
.filter_map(solid_extent)
|
||
.map(|e| e.1)
|
||
.fold(y + h, f64::max);
|
||
if y + h <= rect.bottom() + 0.01 && drawn <= page_h + 0.01 {
|
||
self.leaves.clear();
|
||
self.place(h);
|
||
return Ok(());
|
||
}
|
||
let Some(som) = (if splits { self.splittable_widget_since(first, rect.bottom() - self.frames_bottom()) } else { None }) else {
|
||
if let Some((fields, names, taken, items)) = undo {
|
||
// Not split after all (a value, or no room for a line of it here): undone, and
|
||
// placed as before, moving to the next content area whole.
|
||
if let Some(p) = self.pages.last_mut() {
|
||
p.items.truncate(first);
|
||
}
|
||
self.fields = fields;
|
||
self.names = names;
|
||
self.taken = taken;
|
||
self.items = items;
|
||
self.leaves.clear();
|
||
return self.place_block(node, x, (w, h), ctx, depth, sib, false);
|
||
}
|
||
return self.split_block(first, y, drawn, flows_on);
|
||
};
|
||
// The field breaks into pieces, each with the field's border all round (a border's
|
||
// default `break="close"`, XFA 3.3 pp. 629–630): the border drawn round the whole field
|
||
// comes out, to be drawn round each piece once the break is known.
|
||
self.leaves.clear();
|
||
let border = match (node, border_items) {
|
||
(Node::Field(f), Some((start, end))) if first <= start => {
|
||
if let Some(p) = self.pages.last_mut()
|
||
&& let Some(drawn) = p.items.get(start..end)
|
||
&& !drawn.is_empty()
|
||
{
|
||
p.items.drain(start..end);
|
||
}
|
||
f.common.border.as_ref()
|
||
}
|
||
_ => None,
|
||
};
|
||
self.splitting = Some(Splitting { som, rect: Rect::new(x, y, w, h), pieces: Vec::new() });
|
||
let split = self.split_block(first, y, drawn, false);
|
||
let pieces = self.splitting.take().map(|s| s.pieces).unwrap_or_default();
|
||
split?;
|
||
if let Some(b) = border {
|
||
// The last piece first: a piece's place on its page stays where it was found.
|
||
for (page, at, piece) in pieces.into_iter().rev() {
|
||
self.insert_border(b, piece, page, at)?;
|
||
}
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// The SOM path of the empty widget drawn on the last page since item `first`, when the page
|
||
/// break at `bottom` leaves room above it for a line of its text: only then is the field split.
|
||
/// A value can't run on from one widget into the next piece, and a field with no line on this
|
||
/// page moves on whole.
|
||
fn splittable_widget_since(&self, first: usize, bottom: f64) -> Option<String> {
|
||
self.pages.last()?.items.get(first..)?.iter().find_map(|item| match item {
|
||
Item::Widget(w) => (w.value.as_deref().is_none_or(|v| v.trim().is_empty()) && w.rect.y + split_line(w) <= bottom).then(|| w.som.clone()),
|
||
_ => None,
|
||
})
|
||
}
|
||
|
||
/// Remember that the items the last page got since `start` are one field or draw at
|
||
/// `rect`, to be kept together by a page break.
|
||
pub(super) fn keep_whole(&mut self, start: usize, rect: Rect) {
|
||
let end = self.pages.last().map_or(0, |p| p.items.len());
|
||
if end > start {
|
||
self.leaves.push((start, end, rect.y, rect.bottom()));
|
||
}
|
||
}
|
||
|
||
pub(super) fn item_count(&self) -> usize {
|
||
self.pages.last().map_or(0, |p| p.items.len())
|
||
}
|
||
|
||
/// Spread the items the last page holds from index `first` on (a block drawn from `top` to
|
||
/// `end`, past the bottom of its content area) over this and the following content areas.
|
||
/// Breaks fall between text, pictures and fields, never through one that fits in an area;
|
||
/// fills and lines are cut at the break. A flowing subform (`flows_on`: one [`Self::flow_block`]
|
||
/// splits where it would otherwise move whole) breaks the flowed subforms round it there too
|
||
/// ([`Self::break_frames`]), so they keep their border; anything else just moves on, and
|
||
/// those subforms are drawn without theirs ([`Self::finish_frame`]).
|
||
fn split_block(&mut self, first: usize, top: f64, end: f64, flows_on: bool) -> Result<(), XfaError> {
|
||
let items: Vec<Item> = match self.pages.last_mut() {
|
||
Some(p) if first <= p.items.len() => p.items.split_off(first),
|
||
_ => return Ok(()),
|
||
};
|
||
// Each item with the extent of the field or draw it belongs to: such a leaf moves
|
||
// whole (its border with its text), unless it is taller than a content area.
|
||
let area_h = self.cursor().0.h;
|
||
let mut extents: Vec<Option<(f64, f64)>> = vec![None; items.len()];
|
||
for (start, end, top, bottom) in std::mem::take(&mut self.leaves) {
|
||
if bottom - top <= area_h + 0.01
|
||
&& let Some(slots) = start.checked_sub(first).and_then(|s| extents.get_mut(s..end.saturating_sub(first)))
|
||
{
|
||
slots.fill(Some((top, bottom)));
|
||
}
|
||
}
|
||
// The empty field being split: its widget is no solid item, so the break may go through it.
|
||
let (som, field) = match &self.splitting {
|
||
Some(s) => (Some(s.som.clone()), Some(s.rect)),
|
||
None => (None, None),
|
||
};
|
||
let loose = |item: &Item| matches!((item, &som), (Item::Widget(w), Some(som)) if w.som == *som);
|
||
let mut rest: Vec<(Item, Option<(f64, f64)>)> = items.into_iter().zip(extents).collect();
|
||
let origin_x = self.cursor().0.x;
|
||
// An item's place on the current page is its drawn place moved by (dx, -shift).
|
||
let (mut dx, mut shift) = (0.0, 0.0);
|
||
let mut seg_top = top;
|
||
let mut first_segment = true;
|
||
let mut widget_rest: Option<WidgetRest> = None;
|
||
loop {
|
||
// Each pass rescans every item still to be placed. A block split across very many
|
||
// content areas, and so across very many pages, would do this once per page, costing
|
||
// time quadratic in its items; the still-unplaced count spends the work budget so a
|
||
// crafted template stops rather than run for seconds before the page limit catches it.
|
||
self.measures.set(self.measures.get().saturating_add(rest.len()));
|
||
if self.measures.get() > MAX_MEASURES {
|
||
return Err(XfaError::TooLarge("a subform is split across too many content areas to lay out".into()));
|
||
}
|
||
let (rect, _, placed) = self.cursor();
|
||
// A field being split leaves room for the bottom margins of the flowed subforms round it.
|
||
let margins = if field.is_some() { self.frames_bottom() } else { 0.0 };
|
||
let mut limit = rect.bottom() - margins + shift;
|
||
if field.is_some() && !first_segment && limit <= seg_top + 0.01 {
|
||
// The subforms' margins leave nothing of this content area (a smaller one than
|
||
// the field started in): breaking on would leave the same nothing in each like it.
|
||
// The field goes on past their bottom margins, and from the top of the area when
|
||
// their top margins took all of it, and they, which can't hold it here, are drawn
|
||
// without their border, as round anything that runs out of them.
|
||
limit = rect.bottom() + shift;
|
||
if limit <= seg_top + 0.01 {
|
||
shift = seg_top - rect.y;
|
||
limit = rect.bottom() + shift;
|
||
}
|
||
for f in &mut self.frames {
|
||
f.lost = true;
|
||
}
|
||
}
|
||
let cut = if rect.h < 1.0 {
|
||
// No room to split into: leave the rest here.
|
||
f64::INFINITY
|
||
} else {
|
||
let solid = rest.iter().filter(|(item, _)| !loose(item)).filter_map(|(item, kept)| kept.or_else(|| solid_extent(item)));
|
||
lowest_break(solid, limit, seg_top, first_segment && placed && seg_top > rect.y + 0.01)
|
||
};
|
||
let mut here = Vec::new();
|
||
let mut below = Vec::new();
|
||
// The rest of the split widget's box: a piece down to this break, closed on every
|
||
// side, and what is left of it goes on.
|
||
if let Some((r, border)) = widget_rest.take() {
|
||
let bottom = r.bottom().min(cut);
|
||
if bottom - r.y > 0.01 {
|
||
here.extend(widget_box(Rect::new(r.x, r.y, r.w, bottom - r.y), &border));
|
||
}
|
||
if r.bottom() > cut + 0.01 {
|
||
let y = cut.max(r.y);
|
||
widget_rest = Some((Rect::new(r.x, y, r.w, r.bottom() - y), border));
|
||
}
|
||
}
|
||
for (item, kept) in rest {
|
||
if loose(&item) {
|
||
let (upper, whole, rest_box) = split_widget(item, cut);
|
||
here.extend(upper);
|
||
below.extend(whole.map(|it| (it, None)));
|
||
if rest_box.is_some() {
|
||
widget_rest = rest_box;
|
||
}
|
||
continue;
|
||
}
|
||
if let Some((top, _)) = kept {
|
||
if top < cut - 0.01 {
|
||
here.push(item)
|
||
} else {
|
||
below.push((item, kept))
|
||
}
|
||
continue;
|
||
}
|
||
match divide(item, cut) {
|
||
(Some(a), None) => here.push(a),
|
||
(None, Some(b)) => below.push((b, None)),
|
||
(Some(a), Some(b)) => {
|
||
here.push(a);
|
||
below.push((b, None));
|
||
}
|
||
(None, None) => {}
|
||
}
|
||
}
|
||
// The piece of the field being split on this page, where its border goes.
|
||
if let Some(f) = field {
|
||
let (from, to) = (f.y.max(seg_top), f.bottom().min(cut));
|
||
let page = self.pages.len().saturating_sub(1);
|
||
let at = self.pages.last().map_or(0, |p| p.items.len());
|
||
if to - from > 0.01
|
||
&& let Some(s) = &mut self.splitting
|
||
{
|
||
s.pieces.push((page, at, Rect::new(f.x + dx, from - shift, f.w, to - from)));
|
||
// The flowed subforms round it hold this piece here, even if it draws nothing.
|
||
let here: Vec<bool> = self.frames.iter().map(|fr| self.on_area(&fr.part)).collect();
|
||
for (fr, here) in self.frames.iter_mut().zip(here) {
|
||
fr.held |= here;
|
||
}
|
||
}
|
||
}
|
||
if let Some(p) = self.pages.last_mut() {
|
||
p.items.extend(here.into_iter().map(|it| moved(it, dx, -shift)));
|
||
}
|
||
// A field being split goes on to its own bottom, even when what is left of it draws
|
||
// nothing (no fill, no border).
|
||
if below.is_empty() && widget_rest.is_none() && !field.is_some_and(|f| f.bottom() > cut + 0.01) {
|
||
if let Some(c) = &mut self.cur {
|
||
c.y = (end - shift).min(c.rect.bottom()).max(c.y);
|
||
c.placed = true;
|
||
}
|
||
self.overflowed(end - shift);
|
||
return Ok(());
|
||
}
|
||
rest = below;
|
||
seg_top = cut;
|
||
first_segment = false;
|
||
let (rect, y) = if field.is_some() || flows_on {
|
||
// A field breaks the flowed subforms round it as a field moving on whole does, and
|
||
// so does a flowing subform: each closes its part at the break, after its bottom
|
||
// margin, and opens a new one, after its top margin, in the next content area.
|
||
let area_h = self.cursor().0.h;
|
||
if let Some(c) = &mut self.cur {
|
||
c.y = (cut - shift).min(c.rect.bottom()).max(c.y);
|
||
if field.is_some() {
|
||
c.placed = true;
|
||
}
|
||
}
|
||
// A flowing subform's rest that fits an area has the top margins give way to it,
|
||
// as `make_room` does.
|
||
let left = (end - cut).max(0.0);
|
||
self.break_frames(if flows_on && left <= area_h + 0.01 { left } else { 0.0 }, false)?;
|
||
let (rect, y, _) = self.cursor();
|
||
(rect, y)
|
||
} else {
|
||
self.next_area()?;
|
||
let (rect, _, _) = self.cursor();
|
||
(rect, rect.y)
|
||
};
|
||
shift = cut - y;
|
||
dx = rect.x - origin_x;
|
||
}
|
||
}
|
||
|
||
fn page_has_flow(&self) -> bool {
|
||
self.cur.as_ref().is_some_and(|c| c.placed || c.content > 0)
|
||
}
|
||
|
||
// ───────────────────────────────────────────────────────────────────────── flow
|
||
|
||
/// Flow subform `node` (each instance the data asks for; anything else is ignored) into the
|
||
/// content areas, shown where it and what it is inside say ([`Common::views`]); hidden from
|
||
/// both, it still takes its room.
|
||
pub(super) fn flow_subform(&mut self, node: &Node, avail: Option<(f64, f64)>, ctx: &Ctx, depth: usize, sib: usize) -> Result<(), XfaError> {
|
||
self.warn_other_views(node.common());
|
||
let views = self.views.and(node.common().views());
|
||
let outer = std::mem::replace(&mut self.views, views);
|
||
let flowed = self.flow_subform_in(node, avail, ctx, depth, sib);
|
||
self.views = outer;
|
||
flowed
|
||
}
|
||
|
||
/// [`Self::flow_subform`] once where the subform shows is set.
|
||
pub(super) fn flow_subform_in(&mut self, node: &Node, avail: Option<(f64, f64)>, ctx: &Ctx, depth: usize, sib: usize) -> Result<(), XfaError> {
|
||
if depth > MAX_DEPTH {
|
||
return Ok(());
|
||
}
|
||
let Node::Subform(sf) = node else { return Ok(()) };
|
||
self.check_measures()?;
|
||
if sf.break_before_page && self.page_has_flow() {
|
||
self.break_frames(0.0, true)?;
|
||
}
|
||
// Data adds instances of a repeating subform (rows added in another viewer).
|
||
let instances = self.laid_out_instances(node, &ctx.som);
|
||
let ctx_parent = ctx;
|
||
for i in 0..instances {
|
||
// Every instance spends budget, so nested repeats that place nothing still stop.
|
||
self.measures.set(self.measures.get().saturating_add(1));
|
||
self.check_measures()?;
|
||
let ctx = ctx_parent.with_som(&sf.common, sib + i);
|
||
let (rect, _, _) = self.cursor();
|
||
let (x, w) = avail.unwrap_or((rect.x, rect.w));
|
||
let w = clamp_opt(sf.common.w.unwrap_or(w), sf.common.min_w, sf.common.max_w).min(w.max(1.0));
|
||
if !matches!(sf.layout, Layout::Tb | Layout::Table) || sf.common.h.is_some() {
|
||
// Not splittable: one block.
|
||
let (_, h) = self.size_at(node, w, ctx_parent, depth, sib + i);
|
||
self.flow_block(node, x, (w, h), ctx_parent, depth, sib + i)?;
|
||
continue;
|
||
}
|
||
let m = sf.common.margin;
|
||
// Its border and fill go round what it takes from here, a part on each content area.
|
||
let framed = match self.flow_mark() {
|
||
Some(part) => {
|
||
self.frames.push(Frame { x, w, margin: m, border: sf.common.border.clone(), part, parts: Vec::new(), lost: false, held: false });
|
||
true
|
||
}
|
||
None => false,
|
||
};
|
||
self.advance(m.top);
|
||
let inner_x = x + m.left;
|
||
let inner_w = (w - m.horizontal()).max(1.0);
|
||
match sf.layout {
|
||
Layout::Table => self.flow_table(sf, inner_x, inner_w, &ctx, depth)?,
|
||
_ => {
|
||
let sibs = SomIndices::of(&sf.children);
|
||
for (ci, child) in sf.children.iter().enumerate() {
|
||
if !child.common().presence.occupies() {
|
||
continue;
|
||
}
|
||
let csib = sibs.at(ci);
|
||
if let Node::Subform(_) = child
|
||
&& splittable(child)
|
||
{
|
||
self.flow_subform(child, Some((inner_x, inner_w)), &ctx, depth + 1, csib)?;
|
||
continue;
|
||
}
|
||
self.flow_instances(child, inner_x, inner_w, &ctx, depth + 1, csib)?;
|
||
}
|
||
}
|
||
}
|
||
self.advance(m.bottom);
|
||
if framed && let Some(frame) = self.frames.pop() {
|
||
self.finish_frame(frame)?;
|
||
}
|
||
}
|
||
if sf.break_after_page {
|
||
self.break_frames(0.0, true)?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
pub(super) fn columns(&self, sf: &Subform, inner_w: f64) -> Vec<f64> {
|
||
if !sf.column_widths.is_empty() {
|
||
return sf.column_widths.clone();
|
||
}
|
||
let cells = sf.children.iter().filter_map(|c| if let Node::Subform(r) = c { Some(r.children.len()) } else { None }).max().unwrap_or(1).max(1);
|
||
vec![inner_w / cells as f64; cells]
|
||
}
|
||
|
||
fn flow_table(&mut self, sf: &Subform, x: f64, inner_w: f64, ctx: &Ctx, depth: usize) -> Result<(), XfaError> {
|
||
let cols = self.columns(sf, inner_w);
|
||
let leader =
|
||
sf.overflow_leader.as_ref().and_then(|name| sf.children.iter().position(|c| c.common().name.as_deref() == Some(name) && is_row(c)));
|
||
// The overflow leader starts each part after the first, before the row that moved.
|
||
let lead = leader.and_then(|li| match sf.children.get(li) {
|
||
Some(Node::Subform(l)) => Some((li, l.as_ref())),
|
||
_ => None,
|
||
});
|
||
let sibs = SomIndices::of(&sf.children);
|
||
let lead_h = lead.map_or(0.0, |(li, l)| self.row_height(l, &cols, inner_w, ctx, depth + 1, sibs.at(li)));
|
||
for (ci, child) in sf.children.iter().enumerate() {
|
||
if !child.common().presence.occupies() {
|
||
continue;
|
||
}
|
||
let csib = sibs.at(ci);
|
||
if let Node::Subform(row) = child
|
||
&& row.layout == Layout::Row
|
||
{
|
||
// Rows added as data (in another viewer) repeat the row.
|
||
let instances = self.laid_out_instances(child, &ctx.som);
|
||
for inst in 0..instances {
|
||
let h = self.row_height(row, &cols, inner_w, ctx, depth + 1, csib.saturating_add(inst));
|
||
let led = lead.filter(|(li, _)| *li != ci);
|
||
if self.make_room(h, led.map_or(0.0, |_| lead_h))?
|
||
&& let Some((li, l)) = led
|
||
{
|
||
let (_, y, _) = self.cursor();
|
||
self.draw_row(l, &cols, Rect::new(x, y, inner_w, lead_h), ctx, depth + 1, sibs.at(li))?;
|
||
self.place(lead_h);
|
||
}
|
||
let (_, y, _) = self.cursor();
|
||
self.draw_row(row, &cols, Rect::new(x, y, inner_w, h), ctx, depth + 1, csib + inst)?;
|
||
self.place(h);
|
||
}
|
||
continue;
|
||
}
|
||
if splittable(child)
|
||
&& let Node::Subform(_) = child
|
||
{
|
||
self.flow_subform(child, Some((x, inner_w)), ctx, depth + 1, csib)?;
|
||
continue;
|
||
}
|
||
self.flow_instances(child, x, inner_w, ctx, depth + 1, csib)?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// Flow `node`, which isn't split across content areas, as one block per instance (a
|
||
/// repeating positioned subform gets one for each row the data or its `occur` gives it, XFA
|
||
/// 3.3, "Repeating Elements using Occurrence Limits", p. 263), each as tall as its own
|
||
/// content; the data rows past them are reported.
|
||
fn flow_instances(&mut self, node: &Node, x: f64, inner_w: f64, ctx: &Ctx, depth: usize, sib: usize) -> Result<(), XfaError> {
|
||
for instance in 0..self.laid_out_instances(node, &ctx.som) {
|
||
// Every instance spends budget, so nested repeats that place nothing still stop.
|
||
self.measures.set(self.measures.get().saturating_add(1));
|
||
self.check_measures()?;
|
||
let sib = sib.saturating_add(instance);
|
||
let size = self.size_at(node, inner_w, ctx, depth, sib);
|
||
self.flow_block(node, x, size, ctx, depth, sib)?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
// ───────────────────────────────────────────────────────────────────────── sizing
|
||
|
||
/// The size a node takes when given `avail_w` of width. Containers are measured once per
|
||
/// (node, width, depth): measuring a width-less container measures its content twice (for
|
||
/// its width, then its height), which would otherwise be exponential in the nesting.
|
||
/// How many instances of `node` to lay out under the container at `parent_som`: a
|
||
/// repeating subform's `occur` and data instances ([`instance_count`]); one for anything else.
|
||
pub(super) fn instances(&self, node: &Node, parent_som: &str) -> usize {
|
||
let Node::Subform(sf) = node else { return 1 };
|
||
subform_instances(self.tpl, self.data, sf, parent_som)
|
||
}
|
||
|
||
/// [`Self::instances`] for laying `node` out: the count every layout that repeats a subform
|
||
/// draws (flowed or positioned, top to bottom, wrapped or a table's rows), with the data rows
|
||
/// past it reported ([`Self::warn_truncated`]).
|
||
pub(super) fn laid_out_instances(&mut self, node: &Node, parent_som: &str) -> usize {
|
||
let instances = self.instances(node, parent_som);
|
||
self.warn_truncated(node, parent_som, instances);
|
||
instances
|
||
}
|
||
|
||
/// The data holds more instances of `node` than `laid_out`: say so, since calculations
|
||
/// then see only those, and say why. (The template's own counts can't ask for more: the
|
||
/// parser reads `initial` and `max` up to [`MAX_INSTANCES`].)
|
||
pub(super) fn warn_truncated(&mut self, node: &Node, parent_som: &str, laid_out: usize) {
|
||
let Node::Subform(sf) = node else { return };
|
||
let from_data = match (&sf.common.name, self.data) {
|
||
(Some(name), Some(d)) if sf.occur.max != Some(1) => d.count(&data_path(self.tpl, parent_som), name),
|
||
_ => 0,
|
||
};
|
||
if from_data <= laid_out {
|
||
return;
|
||
}
|
||
let name = sf.common.name.as_deref().unwrap_or("a subform");
|
||
// The template's own `occur max` is the form's rule: past it, the extra data is ignored
|
||
// (XFA 3.3, p. 336). Only the layout's own limit needs naming as a limit.
|
||
let why = if sf.occur.max.is_some_and(|m| m <= laid_out) {
|
||
String::new()
|
||
} else {
|
||
format!("; {MAX_INSTANCES} is the most one repeating subform lays out")
|
||
};
|
||
self.warn_once(format!("{name}: the data holds {from_data} rows; only the first {laid_out} are laid out (calculations see those){why}"));
|
||
}
|
||
}
|
||
|
||
/// The lowest break at or above `limit` that crosses none of the `solid` items' extents: going
|
||
/// through them from the lowest top up, a break moved to an item's top can't cross one already
|
||
/// passed. When that leaves nothing below `seg_top` (an item taller than what is left), the
|
||
/// item starts in a fresh area if this one already holds something (`fresh`); otherwise the
|
||
/// break cuts through it at `limit`.
|
||
fn lowest_break(solid: impl Iterator<Item = (f64, f64)>, limit: f64, seg_top: f64, fresh: bool) -> f64 {
|
||
let mut solid: Vec<(f64, f64)> = solid.filter(|e| e.0 < limit - 0.01).collect();
|
||
solid.sort_by(|a, b| b.0.total_cmp(&a.0));
|
||
let mut cut = limit;
|
||
for (t, b) in solid {
|
||
if t < cut - 0.01 && b > cut + 0.01 {
|
||
cut = t;
|
||
}
|
||
}
|
||
if cut > seg_top + 0.01 {
|
||
cut
|
||
} else if fresh {
|
||
seg_top
|
||
} else {
|
||
limit
|
||
}
|
||
}
|