fix(sorting): retain positive extents when shrinking thin boxes

AF
30m ago
+95-6

Summary

With the default shrink factor, a valid one-pixel-wide or one-pixel-high box is truncated to zero width or height. XY-cut then has no projection for that box and can omit its text element from the sorted page. Fractional page coordinates can also lose their positive extents if they are converted to integers before shrinking.

Preserve the original coordinates until shrinking, then keep each strictly positive scaled dimension at least one pixel wide/high on the integer projection grid. The top-left anchor, normal-size integer boxes, originally zero dimensions and an explicit zero shrink factor retain their existing behavior.

Release note

Recovering previously omitted text can change reading order and sequence-derived element IDs. Consumers that persist IDs or compare ordered outputs should account for this corrected output.

Validation

  • Added public sort_page_elements coverage for fractional narrow width, narrow height and both dimensions, plus originally zero-width/height controls, in both XY-cut directions. The expanded regression file gives 6 failed, 32 passed before the fractional-coordinate fix and 38 passed after it.
  • The complete thin-box regression, sorting and XY-cut test modules pass: 63 passed. They check returned text, element identity, unchanged input coordinates, integer and fractional boxes, and zero-factor controls.
  • Verified with Python 3.12.14, NumPy 2.4.4, Numba 0.65.0, pytest 9.0.3, unstructured-inference 1.6.12 and OpenCV 4.13.0.92.
  • Ruff 0.15.10 lint/format and git diff --check pass.
  • Merged main 0bf5f787; the version guard passes with the unchanged 0.27.38-dev version.
  • The full project suite and partition benchmark were not run locally. Upstream repository CI has now passed on c35f0f9. CodeQL and Security still require maintainer approval. Partition Benchmark stops before measurement because the Tesseract PPA has no Ubuntu 26.04 resolute Release file; the current main benchmark fails at the same installation step.

Checklist

  • Added regression tests for retained text and unchanged sorting inputs.
  • Preserved the existing zero-size/zero-factor behavior.
  • Apply the bug label.