Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich20912a76adec
diff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rs
index 099a0e5..0772aa9 100644
--- a/crates/liteparse/src/extract.rs
+++ b/crates/liteparse/src/extract.rs
@@ -574,7 +574,11 @@ fn extract_page_text_items(
gap,
loose_gap,
space_w,
- if space_w > 0.0 { loose_gap / space_w } else { 0.0 },
+ if space_w > 0.0 {
+ loose_gap / space_w
+ } else {
+ 0.0
+ },
fs,
if fs > 0.0 { gap / fs } else { 0.0 },
seg.avg_char_width(),
@@ -613,7 +617,11 @@ fn extract_page_text_items(
let space_w = ch.font_space_width().map(|w| w * em_vp).unwrap_or(0.0);
let loose_gap = vp_strict.left - seg.last_char_loose_right;
let both_alnum = c.is_ascii_alphanumeric()
- && seg.text.chars().last().is_some_and(|p| p.is_ascii_alphanumeric());
+ && seg
+ .text
+ .chars()
+ .last()
+ .is_some_and(|p| p.is_ascii_alphanumeric());
let thresh = if space_w > 0.0 {
0.7 * space_w
} else {
@@ -691,12 +699,43 @@ fn dedup_overlapping_items(items: &mut Vec<TextItem>, debug: bool) {
let smaller_area = area_a.min(area_b);
if items[i].text == items[j].text {
- // Exact text match: any overlap → drop the earlier item
- // (later items are rendered on top in PDF paint order)
+ // Exact text match: require strong bounding-box overlap before
+ // dedup. Originally this fired on *any* overlap on the theory
+ // that overlapping identical text is two layers of the same
+ // content (re-stamped redactions, etc). But the same word
+ // routinely appears more than once on a page in different
+ // positions; if the items' bboxes happen to share even a sliver
+ // of area (e.g. one column's word vertically adjacent to
+ // another column's identical word with a slack loose-box), we
+ // were silently dropping the legitimate first occurrence.
+ //
+ // Reproducer: olmocr 0083fb21…page_8 — column 1 has the word
+ // "source" in row 2 and again in row 3; the dedup nuked the
+ // row 2 "source" in favor of the row 3 one (which later merged
+ // into "source income is reduced (the"), making the row 2
+ // sentence read as "made to U.S. \n . In situations…" with
+ // "source income" missing.
+ //
+ // Require ≥50% overlap of the smaller item — same threshold
+ // as the non-exact branch. True duplicate stamps overlap
+ // essentially 100%; unrelated repeats overlap 0%.
+ let strong_overlap = smaller_area > 0.0 && intersection / smaller_area > 0.5;
+ if !strong_overlap {
+ continue;
+ }
if debug {
eprintln!(
- "[extract-debug] DEDUP exact-match drop i={i} text='{}' at ({:.1},{:.1}) in favor of j={j} at ({:.1},{:.1})",
- items[i].text, items[i].x, items[i].y, items[j].x, items[j].y
+ "[extract-debug] DEDUP exact-match drop i={i} text='{}' at ({:.1},{:.1} {}x{}) in favor of j={j} at ({:.1},{:.1} {}x{}) overlap_ratio={:.2}",
+ items[i].text,
+ items[i].x,
+ items[i].y,
+ items[i].width,
+ items[i].height,
+ items[j].x,
+ items[j].y,
+ items[j].width,
+ items[j].height,
+ intersection / smaller_area
);
}
keep[i] = false;
diff --git a/crates/liteparse/src/markdown_layout/classify.rs b/crates/liteparse/src/markdown_layout/classify.rs
index 216c2cb..ad5dc37 100644
--- a/crates/liteparse/src/markdown_layout/classify.rs
+++ b/crates/liteparse/src/markdown_layout/classify.rs
@@ -22,6 +22,7 @@ use super::tables::{detect_ruled_tables, detect_tables, merge_table_runs};
/// image ref). Collected into one y-sorted stream so the two kinds interleave
/// correctly by vertical position rather than emitting all of one before the
/// other.
+#[derive(Clone)]
enum Interruption {
Hr,
Figure(crate::types::ImageRef),
@@ -69,43 +70,6 @@ pub fn classify_page_with_filters(
image_mode: crate::config::ImageMode,
chrome_indices: &std::collections::HashSet<usize>,
) -> Vec<Block> {
- let mut blocks: Vec<Block> = Vec::new();
- let mut paragraph: Option<ParaAccum> = None;
- // Active fenced code block being accumulated (consecutive `all_mono` lines).
- let mut code: Option<Vec<String>> = None;
- // Tracks the "level 0" indent of the current contiguous list run so we can
- // bucket deeper items into nesting levels. Reset whenever a non-list block
- // breaks the run.
- let mut list_base_indent: Option<f32> = None;
- // Index into `blocks` of the most recent ListItem in the current run — used
- // to merge wrapped continuation lines into the same item.
- let mut last_list_item_idx: Option<usize> = None;
- // Index (into `lines`) of the most recent line appended to the active list
- // item, for gap/font-size checks on continuation lines. An index rather than
- // a cloned line so the per-line hot loop doesn't deep-copy `spans` each time.
- let mut last_list_line: Option<usize> = None;
- // (level, last line) of a heading block currently being accumulated. Lets a
- // multi-line wrapped heading/caption (e.g. a photo caption set one size
- // above body) merge into one Heading block instead of shredding into one
- // `###` per wrapped line. Adjacency is enforced by checking `blocks.last()`,
- // so this never reaches across an intervening block.
- let mut heading_run: Option<(u8, usize)> = None;
-
- let flush_paragraph = |blocks: &mut Vec<Block>, p: Option<ParaAccum>| {
- if let Some(acc) = p
- && !acc.raw.trim().is_empty()
- {
- blocks.push(paragraph_from_accum(acc));
- }
- };
- let flush_code = |blocks: &mut Vec<Block>, c: Option<Vec<String>>| {
- if let Some(lines) = c
- && !lines.is_empty()
- {
- blocks.push(Block::CodeBlock { lines });
- }
- };
-
let debug = std::env::var("LITEPARSE_DEBUG_MD").is_ok();
// TOC suppression: when ≥3 lines on this page look like TOC entries
@@ -138,27 +102,174 @@ pub fn classify_page_with_filters(
&filtered_owned
};
- // Pre-pass: detect tabular regions so the per-line classifier below can
- // skip over them. Tables take priority over heading / paragraph / list
- // classification because a row of bold short cells would otherwise be
- // misread as a heading or list item.
- //
- // Two detectors run in sequence: ruled-grid (path-based, strongest signal)
- // and the borderless column-alignment fallback. Where ranges overlap, the
- // ruled output wins.
+ // Region-grouped pipeline: split the filtered line list into contiguous
+ // runs sharing a `region_path` (one xy_cut leaf each) and classify each
+ // leaf as its own mini-page. Paragraph / list / code / heading state is
+ // scoped per leaf so a column-wrap can't silently fuse two leaves into one
+ // paragraph, and table detection runs per-leaf so a misfired borderless
+ // table can't consume lines from a different leaf (the dominant Mode 15
+ // cause: a spurious table starting in one column's footnote area was
+ // eating the first lines of the next column, dropping those words from
+ // any block). Cross-leaf merges happen only in `stitch_regions` at the
+ // end, where the rule is explicit and inspectable.
+ let mut region_ranges: Vec<(usize, usize)> = Vec::new();
+ {
+ let mut s = 0;
+ while s < lines.len() {
+ let path = &lines[s].region_path;
+ let mut e = s + 1;
+ while e < lines.len() && lines[e].region_path == *path {
+ e += 1;
+ }
+ region_ranges.push((s, e));
+ s = e;
+ }
+ }
+
+ // Page-level interruptions (HRs from vector graphics + figure refs) need
+ // to interleave with text by y-position. Collect once, then dispatch into
+ // each region's classify call by y-band so they emit in the right slot.
+ // Two interruptions belonging to *no* region (e.g. an HR in a margin band
+ // that no leaf covers) are emitted between regions in y order.
+ let mut all_interruptions: Vec<(f32, Interruption)> = detect_horizontal_rules(page)
+ .into_iter()
+ .map(|y| (y, Interruption::Hr))
+ .collect();
+ if !matches!(image_mode, crate::config::ImageMode::Off) {
+ for r in &page.image_refs {
+ all_interruptions.push((r.bbox.y, Interruption::Figure(r.clone())));
+ }
+ }
+ all_interruptions.sort_by(|a, b| a.0.total_cmp(&b.0));
+
+ // region_boundary_idx[k] = index into `blocks` where region k's first
+ // block lives. Used by `stitch_regions` to know where to test cross-leaf
+ // paragraph continuation. Stored alongside the region's last line so the
+ // stitcher can use the same `continues_paragraph` cross-region rule that
+ // governs intra-region merging — no second source of truth.
+ let mut region_boundaries: Vec<usize> = Vec::new();
+ let mut interrupt_cursor = 0usize;
+ let mut blocks: Vec<Block> = Vec::new();
+
+ let push_interruption = |blocks: &mut Vec<Block>, kind: Interruption| {
+ blocks.push(match kind {
+ Interruption::Hr => Block::HorizontalRule,
+ Interruption::Figure(r) => Block::Figure {
+ id: r.id,
+ bbox: r.bbox,
+ },
+ });
+ };
+
+ for (rstart, rend) in region_ranges {
+ let region_lines = &lines[rstart..rend];
+
+ // Compute this region's y-extent so we can slot in interruptions that
+ // fall above or inside it. xy_cut leaves are rectangular partitions,
+ // so a leaf's y-band is well-defined.
+ let mut y_min = f32::INFINITY;
+ let mut y_max = f32::NEG_INFINITY;
+ for l in region_lines {
+ y_min = y_min.min(l.bbox.y);
+ y_max = y_max.max(l.bbox.y + l.bbox.height);
+ }
+
+ // Emit any interruptions that sit above this region's top edge before
+ // we start emitting the region's own blocks.
+ while interrupt_cursor < all_interruptions.len()
+ && all_interruptions[interrupt_cursor].0 < y_min
+ {
+ let (_, kind) = all_interruptions[interrupt_cursor].clone();
+ push_interruption(&mut blocks, kind);
+ interrupt_cursor += 1;
+ }
+
+ // Hand the region its in-band interruptions (y in [y_min, y_max]) so
+ // the per-line loop can interleave them by y, matching the old
+ // page-level behavior within a leaf.
+ let mut region_interruptions: Vec<(f32, Interruption)> = Vec::new();
+ while interrupt_cursor < all_interruptions.len()
+ && all_interruptions[interrupt_cursor].0 <= y_max
+ {
+ region_interruptions.push(all_interruptions[interrupt_cursor].clone());
+ interrupt_cursor += 1;
+ }
+
+ let region_start = blocks.len();
+ let region_blocks = classify_region(
+ region_lines,
+ region_interruptions,
+ page,
+ heading_map,
+ outline,
+ image_mode,
+ toc_page,
+ debug,
+ );
+ if !region_blocks.is_empty() {
+ region_boundaries.push(region_start);
+ blocks.extend(region_blocks);
+ }
+ }
+
+ // Trailing interruptions below the last region.
+ while interrupt_cursor < all_interruptions.len() {
+ let (_, kind) = all_interruptions[interrupt_cursor].clone();
+ push_interruption(&mut blocks, kind);
+ interrupt_cursor += 1;
+ }
+
+ return stitch_regions(blocks, &region_boundaries);
+}
+
+/// Classify the lines of a single xy_cut leaf into a sequence of blocks. All
+/// per-line state (active paragraph, code run, list run, heading_run) is local
+/// to this call — nothing crosses a leaf boundary except through the explicit
+/// `stitch_regions` post-pass. Page-level signals (`heading_map`,
+/// `struct_nodes`, `outline`, header/footer y-bands) are still consulted
+/// because they're computed once per document.
+fn classify_region(
+ lines: &[ProjectedLine],
+ interruptions: Vec<(f32, Interruption)>,
+ page: &ParsedPage,
+ heading_map: &[(f32, u8)],
+ outline: &[OutlineTarget],
+ image_mode: crate::config::ImageMode,
+ toc_page: bool,
+ debug: bool,
+) -> Vec<Block> {
+ let _ = image_mode; // interruption emission already gated upstream
+ let mut blocks: Vec<Block> = Vec::new();
+ let mut paragraph: Option<ParaAccum> = None;
+ let mut code: Option<Vec<String>> = None;
+ let mut list_base_indent: Option<f32> = None;
+ let mut last_list_item_idx: Option<usize> = None;
+ let mut last_list_line: Option<usize> = None;
+ let mut heading_run: Option<(u8, usize)> = None;
+
+ let flush_paragraph = |blocks: &mut Vec<Block>, p: Option<ParaAccum>| {
+ if let Some(acc) = p
+ && !acc.raw.trim().is_empty()
+ {
+ blocks.push(paragraph_from_accum(acc));
+ }
+ };
+ let flush_code = |blocks: &mut Vec<Block>, c: Option<Vec<String>>| {
+ if let Some(lines) = c
+ && !lines.is_empty()
+ {
+ blocks.push(Block::CodeBlock { lines });
+ }
+ };
+
+ // Per-region table detection. Indices are local to `lines`. Page-level
+ // graphics are still consulted for ruled-table detection because path
+ // objects are page-coordinate; the detector intersects them against the
+ // sub-list's line bboxes anyway.
let ruled_runs = detect_ruled_tables(lines, &page.graphics, page.page_width, page.page_height);
let borderless_runs = detect_tables(lines);
let table_runs = merge_table_runs(ruled_runs, borderless_runs);
- // Suppress HRs that fall inside a detected table's y-range — they're the
- // table's own row dividers, not document-level section breaks. Build the
- // y-extents once before we move table_runs into the iterator.
- // Extend each table's HR-suppression band upward to cover any
- // header/sub-header rows we didn't absorb into the table. The expansion
- // is a few row heights — large enough to catch a 2–3 line bold/italic
- // header sitting just above the table, small enough not to swallow a
- // real section divider belonging to a different block. The downward
- // edge gets a small slack to catch HRs drawn flush with the last row.
const TABLE_HR_SUPPRESS_HEADROOM_ROWS: f32 = 4.0;
let table_y_extents: Vec<(f32, f32)> = table_runs
.iter()
@@ -174,34 +285,17 @@ pub fn classify_page_with_filters(
let mut table_iter = table_runs.into_iter().peekable();
- // Pre-pass: collect document-order "interruptions" — horizontal rules
- // (from vector graphics) and figure injections (raster image refs when
- // image mode is on) — into one y-sorted stream. Emitting them from a single
- // stream keeps HRs and figures correctly interleaved by y; a per-type pass
- // would emit all HRs before all figures regardless of vertical position.
- // Both are suppressed inside table y-extents so a row divider or a
- // cell-spanning raster background doesn't spawn a block mid-table.
let in_table_band = |y: f32| {
table_y_extents
.iter()
.any(|(top, bot)| y >= *top - 2.0 && y <= *bot + 2.0)
};
- let mut interruptions: Vec<(f32, Interruption)> = detect_horizontal_rules(page)
+ let mut region_interruptions: Vec<(f32, Interruption)> = interruptions
.into_iter()
- .filter(|y| !in_table_band(*y))
- .map(|y| (y, Interruption::Hr))
+ .filter(|(y, _)| !in_table_band(*y))
.collect();
- if !matches!(image_mode, crate::config::ImageMode::Off) {
- for r in &page.image_refs {
- if !in_table_band(r.bbox.y) {
- interruptions.push((r.bbox.y, Interruption::Figure(r.clone())));
- }
- }
- }
- // Stable sort by y. HRs were pushed before figures, so an exact-y tie keeps
- // the prior HR-before-figure ordering.
- interruptions.sort_by(|a, b| a.0.total_cmp(&b.0));
- let mut interruptions = interruptions.into_iter().peekable();
+ region_interruptions.sort_by(|a, b| a.0.total_cmp(&b.0));
+ let mut interruptions = region_interruptions.into_iter().peekable();
// Emit any interruptions whose y is at or above `before_y`, flushing the
// active paragraph/code/list state first so each lands as its own block.
@@ -553,6 +647,92 @@ pub fn classify_page_with_filters(
blocks
}
+/// Cross-region merging. Walks the concatenated block stream and, at each
+/// region boundary, tries to fuse the last block of region A with the first
+/// block of region B when they represent one logical unit split across leaves.
+/// This is the *only* place per-region scoping is relaxed — every other
+/// detector now treats a region as a hard wall — so the merge rules here are
+/// deliberately narrow and inspectable.
+///
+/// Currently supports:
+/// - **Paragraph → Paragraph**: same cross-region rule as
+/// `continues_paragraph`'s `region_path != region_path` arm: the previous
+/// paragraph ends mid-sentence (no terminal punctuation) and the next
+/// starts lowercase. Heals the dominant Mode 15 shape (column wrap losing
+/// its tail to a separate block).
+/// - **Hyphen splice**: previous paragraph ends `<letter>-` and next starts
+/// ASCII-lowercase. Already handled in `render_blocks` for the adjacent
+/// case, but doing it here too lets the merged block flow through the
+/// uniform paragraph-rendering path and avoids a `\n\n` slipping in when
+/// the renderer sees an intervening non-paragraph block.
+///
+/// Lists, code blocks, headings, and tables are *not* fused across regions
+/// in v1. A bullet point split across columns is rare and a false merge is
+/// worse than a true split; a table split across leaves indicates a
+/// projection-side issue better fixed there than papered over here.
+fn stitch_regions(blocks: Vec<Block>, region_starts: &[usize]) -> Vec<Block> {
+ if region_starts.len() <= 1 {
+ return blocks;
+ }
+ let boundary_set: std::collections::HashSet<usize> =
+ region_starts.iter().skip(1).copied().collect();
+ let mut out: Vec<Block> = Vec::with_capacity(blocks.len());
+ for (i, block) in blocks.into_iter().enumerate() {
+ if boundary_set.contains(&i)
+ && let Some(prev) = out.last_mut()
+ && let (
+ Block::Paragraph {
+ text: prev_text, ..
+ },
+ Block::Paragraph {
+ text: cur_text,
+ bold: false,
+ italic: false,
+ },
+ ) = (prev, &block)
+ {
+ let prev_trim = prev_text.trim_end();
+ let starts_lower = cur_text
+ .trim_start()
+ .chars()
+ .next()
+ .is_some_and(|c| c.is_lowercase());
+ // Hyphen-splice path: bare `<letter>-` mid-word break across the
+ // leaf boundary. Drop the hyphen, join with no separator.
+ let hyphen_splice = prev_trim.ends_with('-')
+ && prev_trim
+ .chars()
+ .rev()
+ .nth(1)
+ .is_some_and(|c| c.is_ascii_alphabetic())
+ && starts_lower;
+ if hyphen_splice {
+ // Pop the trailing `-` (it may have whitespace after it from
+ // a previous trim, so re-trim).
+ while prev_text.ends_with(|c: char| c.is_whitespace()) {
+ prev_text.pop();
+ }
+ prev_text.pop(); // the '-'
+ prev_text.push_str(cur_text.trim_start());
+ continue;
+ }
+ let ends_open = !prev_trim.ends_with(|c: char| {
+ matches!(
+ c,
+ '.' | '!' | '?' | ':' | ';' | '”' | '"' | ')' | ']' | '。' | '』' | '」'
+ )
+ });
+ if ends_open && starts_lower {
+ prev_text.push(' ');
+ prev_text.push_str(cur_text.trim_start());
+ continue;
+ }
+ }
+ out.push(block);
+ }
+ out
+}
+
#[cfg(test)]
mod tests {
use super::super::blocks::{Block, render_blocks};
diff --git a/crates/liteparse/src/projection.rs b/crates/liteparse/src/projection.rs
index 7d05ed2..ed338ea 100644
--- a/crates/liteparse/src/projection.rs
+++ b/crates/liteparse/src/projection.rs
@@ -3885,32 +3885,138 @@ fn xy_cut_rec(
let mut right: Vec<usize> = Vec::new();
// Column-histogram V-cuts (score = 1.0e9) split between two column
// left-edge clusters. The cut sits at the midpoint between the two
- // peak x's, which is typically far inside the left column — a wide
- // body line whose left half wraps to a second item starting before
- // the column's right edge can have a centroid past the cut, which
- // would mis-route it to the right column. Partition by left edge
- // for these cuts: the item's column membership is determined by
- // which peak its starting x is closer to, not by its midpoint.
- // Density V-cuts (white-space valleys) split between physical
- // text-free space — items truly straddle the cut only when wider
- // than the column, and centroid is the right discriminator there.
+ // peak x's, which is typically far inside the left column. PDFium
+ // routinely emits a single visual line as multiple items (line-start
+ // + mid-line emphasis fragments + late-line clusters); each item has
+ // its own x, so an item-by-item partition by `it.x` will split one
+ // logical line across both sides whenever a continuation fragment
+ // starts past `cut.position`. Verified on `031a888e…page_1` where
+ // a histogram V-cut at x=360.8 inside column 2 routed the body-start
+ // fragments at x=312 LEFT but their same-baseline continuation
+ // "In Section 2 the" at x=474 RIGHT, orphaning the snippet from its
+ // paragraph and producing a stranded block downstream.
+ //
+ // Fix: for histogram V-cuts, first measure how many y-bands have items
+ // on *both* sides of the cut ("straddling rows"). A real column
+ // boundary has every body row straddling (text in left + right column
+ // on the same baseline) — partition by `it.x` so each column gets its
+ // items. An intra-column histogram split (031a888e case: a single line
+ // wrapped into multiple items at different x's) has almost no
+ // straddling rows — partition by row leftmost-x so a wrapped line
+ // stays whole. The threshold (>~25% of rows straddle) cleanly
+ // separates: 0145bc47 (every body row straddles, ~100%) gets per-item
+ // partition; 031a888e (1 of ~45 rows straddles, ~2%) gets per-row.
+ //
+ // Density V-cuts (white-space valleys) still use centroid: they only
+ // fire where a real text-free vertical band exists, so an item that
+ // crosses a density cut is genuinely wider than the column.
let is_histogram_v_cut = cut.axis == CutAxis::Vertical && (cut.score - 1.0e9).abs() < 1.0;
- for &i in &idxs {
- let it = &items[i].item;
- let split_at = match cut.axis {
- CutAxis::Horizontal => it.y + it.height.max(0.0) * 0.5,
- CutAxis::Vertical => {
- if is_histogram_v_cut {
- it.x
+ if is_histogram_v_cut {
+ // Sort by y then x so items on one baseline cluster together.
+ let mut sorted_idxs: Vec<usize> = idxs.clone();
+ sorted_idxs.sort_by(|&a, &b| {
+ items[a]
+ .item
+ .y
+ .total_cmp(&items[b].item.y)
+ .then(items[a].item.x.total_cmp(&items[b].item.x))
+ });
+ let band_tol = (median_h * 0.5).max(2.0);
+
+ // First pass: count rows with items entirely-left, entirely-right,
+ // or straddling the cut. Decides the partition strategy.
+ let (mut rows_total, mut rows_straddle) = (0usize, 0usize);
+ let mut k = 0;
+ while k < sorted_idxs.len() {
+ let band_y = items[sorted_idxs[k]].item.y;
+ let mut j = k;
+ let mut has_left = false;
+ let mut has_right = false;
+ while j < sorted_idxs.len()
+ && (items[sorted_idxs[j]].item.y - band_y).abs() <= band_tol
+ {
+ let ix = items[sorted_idxs[j]].item.x;
+ if ix.is_finite() {
+ if ix < cut.position {
+ has_left = true;
+ } else {
+ has_right = true;
+ }
+ }
+ j += 1;
+ }
+ if has_left || has_right {
+ rows_total += 1;
+ if has_left && has_right {
+ rows_straddle += 1;
+ }
+ }
+ k = j;
+ }
+ // 25% threshold: when a quarter or more of rows have content on
+ // both sides of the cut, it's a real column boundary (every body
+ // row has text in both columns). Below that, the cut sits inside
+ // a single column and the rare straddler is an intra-column
+ // wrapped line that must not be torn apart.
+ const STRADDLE_PARTITION_FRACTION: f32 = 0.25;
+ let partition_by_item = rows_total > 0
+ && (rows_straddle as f32) / (rows_total as f32) >= STRADDLE_PARTITION_FRACTION;
+ if debug_xy {
+ let pad = " ".repeat(depth as usize);
+ eprintln!(
+ "[xy d={depth}]{pad} hist-v straddle={}/{} → partition_by_{}",
+ rows_straddle,
+ rows_total,
+ if partition_by_item { "item" } else { "row" }
+ );
+ }
+
+ if partition_by_item {
+ for &i in &idxs {
+ if items[i].item.x < cut.position {
+ left.push(i);
} else {
- it.x + it.width.max(0.0) * 0.5
+ right.push(i);
}
}
- };
- if split_at < cut.position {
- left.push(i);
} else {
- right.push(i);
+ let mut k = 0;
+ while k < sorted_idxs.len() {
+ let band_y = items[sorted_idxs[k]].item.y;
+ let mut j = k;
+ let mut min_x = f32::INFINITY;
+ while j < sorted_idxs.len()
+ && (items[sorted_idxs[j]].item.y - band_y).abs() <= band_tol
+ {
+ let ix = items[sorted_idxs[j]].item.x;
+ if ix.is_finite() && ix < min_x {
+ min_x = ix;
+ }
+ j += 1;
+ }
+ let side_left = min_x < cut.position;
+ for &i in &sorted_idxs[k..j] {
+ if side_left {
+ left.push(i);
+ } else {
+ right.push(i);
+ }
+ }
+ k = j;
+ }
+ }
+ } else {
+ for &i in &idxs {
+ let it = &items[i].item;
+ let split_at = match cut.axis {
+ CutAxis::Horizontal => it.y + it.height.max(0.0) * 0.5,
+ CutAxis::Vertical => it.x + it.width.max(0.0) * 0.5,
+ };
+ if split_at < cut.position {
+ left.push(i);
+ } else {
+ right.push(i);
+ }
}
}
if debug_xy {