Files
pdfcraft/crates/render/src/text.rs
T
Hector Villarreal aef22858a9 Select a table by rows when a drag crosses its columns (#3669)
## What this changes

Fixes #3850.

Partly addresses #3682: drags within one column of its two-column page now select that column's text (the tester's harness measured 3/9 → 7/9 on `cols.pdf`). Drags from one column into the next still follow reading order, as on `main`; they need column-correct reading order (#3676).

Selecting text in a table selected the wrong things. A table reads column by column, so a plain drag along one row, or down and across a few rows, took the rest of the first column and the top of the next ones between its ends. On some rows the highlight jumped to the column headers and half the table. Copy gave the cells out of order. Preview selects these by rows, and this makes PdfCraft do the same.

Rebased on `main` after the module split (#4889): the row selection is in `crates/render/src/text.rs`, the padded-cell join and barcode rows in `text/order.rs` and `text/layout.rs`, the drag's pointer end in `crates/ui-egui/src/canvas/selection.rs` and `canvas.rs`.

- **Visual rows** (`PageText::visual_between`, used by `Selection::rects` and `Selection::text`). The drag selects by rows when reading order between its ends would not follow them: the rest of the first row, the rows between, and the start of the last. Copy gives the cells tab-separated, as Column select does. Rows are used when either of these holds:
  - Reading order strays above or below the rows dragged over.
  - The drag ends in another column. That means across a gutter that no text of those rows crosses (checked a few lines above and below as well, so a justified line's wide word gap does not count), or with the pointer past the end of both lines.
- **Limits on the rows.** Rows never take lines left of everywhere the drag has been, and they stay in the column when the drag does. A drag in the right column of side-by-side text that ends on a hanging list marker does not pull in the left column. Running text keeps reading order whenever it covers the same glyphs, and so does a column of running text beside a sidebar. A drag down one article or table column keeps reading order too. A line's extent for these limits is its ink: spaces padding it out don't count.
- **Pointer position.** `Span::Run` records where the drag's pointer ended (`head_x`). It can lie well past the nearest glyph, as in the empty end of a table row. Select all, a clicked word, line or page, and four-click page selection keep reading order.
- **Hit testing.** `PageText::nearest` still takes the glyph whose em box holds the point first (#3693). Failing that, it prefers the row level with the pointer, so past the end of a short row a longer row just below no longer captures the drag. Between rows, the old distance rule still applies.
- **Padded cells.** Report generators pad each cell of a row with trailing spaces. When a cell's padding stops short of the next cell (more than the 3-height continuation gap), the next cell on its baseline now joins the row, within 8 line heights (`PADDED_FIELD`, `PADDED_REACH`). Without this, the row's last cells were read after the whole table, in selection, copy and `text_extract` alike.
- **Barcodes drawn with a font** (new since the last review). Mexican CFDI invoices and receipts print their QR or Aztec code with a barcode font: rows of glyphs stacked flush, 5 pt from the seal's text and on nearly the same baselines. Each code row joined the seal line beside it, so the seal read interleaved with the code, some seal lines came after the whole code, and a drag down the seal jumped through it. Rows of at least 8 glyphs, 4 or more stacked flush with one glyph count and the same edges (`barcode_rows`), now join no other text and read after the page's text. A point beside the code is not taken by the code row level with it.

## How you verified it

- [x] `cargo fmt --all --check`
- [x] `cargo clippy --workspace --all-targets --locked -- -D warnings`
- [x] tests pass on the crates I changed, and on the crates that use text extraction:

  | Crate or suite | Passed |
  |---|---|
  | `pdfcraft-render` (lib + integration) | 476 + 64 |
  | `pdfcraft-ui-egui --lib` | 696 |
  | `pdfcraft-ui-egui --test it` (`column_select`, `text_select`, `drag_selects`, `selection_works`) | 33 |
  | `pdfcraft-automation` | 359 |
  | `pdfcraft-engine` | 442 |
  | `pdfcraft-export` | 43 |

- [x] `cargo xtask layers`, `cargo xtask assets`, `cargo check -p pdfcraft-render --target wasm32-unknown-unknown`, `cargo xtask parity`

**Tests.** I reverted each fix and watched these fail:

- `column_select.rs::a_plain_drag_along_one_table_row_selects_only_that_row` (UI, select tool and highlighter). Before the fix it got `"Apple\nPear\nQty\n12"` instead of `"Apple\t12"`.
- `column_select.rs::a_drag_ending_between_the_columns_selects_the_rows` (UI). Without `head_x` it got `"Name\nApple\nPear"`.
- `column_select.rs::a_drag_down_text_beside_a_barcode_selects_only_the_text` (UI). Without the barcode rows it got `"ABCDEFGHIJKL one two three\nFGHIJKLMNOPQ\n…"`.
- `text/tests.rs::a_barcode_drawn_with_a_font_reads_apart_from_the_text_beside_it`
- `text/tests.rs::a_point_beside_a_barcode_takes_the_text_not_the_code_row`
- `text/tests.rs::a_point_level_with_a_row_takes_that_rows_nearest_glyph`
- `text/tests.rs::a_drag_in_the_right_column_takes_nothing_from_the_left`

Also new in `text/tests.rs`: `a_drag_along_one_table_row_selects_that_row`, `a_drag_over_table_rows_selects_those_rows`, `a_drag_that_reading_order_already_covers_keeps_it`, `a_pointer_ending_beside_the_column_takes_the_rows`, `a_cell_padded_with_spaces_joins_the_next_cell_of_its_row`, `a_justified_gap_is_not_a_gutter`, and the running-text, sidebar, heading and vertical-text cases from earlier review rounds. In `column_select.rs`: double-click drags, select all and four-click page selection on uneven columns.

All fixtures are synthetic.

**Corpus.** Before the rebase, text extraction of the pinned pdf.js corpus was byte-identical with and without this PR. The barcode change leaves it identical too: the plain text of the first 60 pages of each of its 989 PDFs (1,525 pages) is the same with and without it.

**Real-world files** (private, not attached):

- **Text extraction:** on a personal folder of PDFs (bank and hotel reports, payslips, policies, contracts, slide decks), the only files that differed from `main` before the rebase were bank statements whose padded rows now keep their amounts. The barcode change alters one page across 105 files: a statement's CFDI page, whose seal now reads in order.
- **Simulated drags:** about 53,700 random drags across those files. Selection takes at most about 145 µs per frame.
- **Two-column contract:** on a bilingual two-column contract, 2,649 drags kept inside one column leak into the other column less often than before (1,224 vs 1,591). In no file does the new code leak a two-column drag where the old one did not, except across table rows, where rows running across the page is intended.

## Never-crash

- [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests
- [x] No indexing with input-derived positions in the new code (`get`). Allocations are bounded by the page's glyphs, and `side_by_side` compares at most `SIDE_BY_SIDE_LINES` lines. Barcode rows are found with one hash map over the page's rows. NaN boxes rank last in `nearest` (`total_cmp`) and are never barcode rows. No recursion.
- [x] No `unsafe`
- [ ] Any crash this fixes has a small synthetic regression test (n/a, no crash)

## Provenance

- [x] No asset from an Adobe product, and no Adobe icon recreated, traced or imitated
- [x] No font files committed here
- [x] No GPL/AGPL code copied in
- [x] Behaviour comes from black-box observation (Preview's selection on the same files), not from reading another implementation's source
- [x] No new assets

## Security

- [x] `--root` confinement untouched
- [x] MCP and the UI control channel untouched
- [x] No new outbound network access

## Parity and reach

- [x] `parity/acrobat-features.toml`: `view.text-select` gains the new evidence and a note. Selection across pages is still missing, as before.
- [x] Reachable headlessly: the padded-cell join and barcode rows change `text_extract` too. Visual-row selection is a pointer interaction, like the rest of plain drag selection.
- [x] `ROADMAP.md` progress log line

## Anything a reviewer should look at first

1. **A deliberate behaviour change.** `alt_drag_selects_only_the_text_inside_the_rectangle` asserted that a plain drag from "Name" to "12" selects in reading order: `"Name\nApple\nPear\nQty\n12"`. It now selects the two rows dragged over, `"Name\tQty\nApple\t12"`, and I changed the assertion. Reading order itself is unchanged (`a_column_selection_takes_one_table_column` still asserts it), and so is a drag down one column.
2. **The gutter test is a heuristic.** "Another column" means a vertical strip at least one line height wide that no text in the dragged rows, or 4 line heights around them, crosses. That catches table columns and side-by-side text, and ignores justified word gaps in practice. Thin columns with ragged edges may still fool it.
3. **The padded-cell join changes reading order.** It applies only to a segment whose box runs on at least 2 line heights of trailing spaces past its last visible glyph, and only towards a cell on the same baseline within 8 line heights.
4. **Barcode rows change reading order.** A stack of 4+ flush rows of 8+ glyphs, each with the same glyph count, height and edges, reads after the page's text. Text set solid still has lines of different lengths and counts; a hex dump set solid in a monospaced font would also match, and would then read last.
5. **Overlap with #2431** (narrow article gutters, #2387). The two PRs touch different code paths and should rebase cleanly.

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **New Features**
  * Text drags across table-like layouts now select cells by visual row, with copied cells separated by tabs. Multi-click and Shift-click selections can also follow visual rows where applicable; select-all and other non-row selections retain reading order.
  * Text selection and pointer placement work more accurately around columns, gutters, and barcode-like text, reducing unintended selection of nearby content. Barcode text is kept separate from adjacent text.
* **Documentation**
  * Updated feature coverage and progress notes to reflect text-selection behavior and regression testing.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-15 12:11:23 +01:00

789 lines
40 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Text extraction (bootstrap for `pdfcraft-text`, architecture §8).
//!
//! A hayro `Device` that ignores paint and records every glyph: its Unicode value (ToUnicode →
//! encoding → glyph name fallbacks, handled by hayro) and its box in *page view space*, i.e.
//! points with the origin at the top-left of the displayed page, rotation applied, y down. The UI
//! maps view space to the screen with a single scale.
//!
//! On top of the glyph list: line grouping (baseline clustering), word gaps, plain-text output,
//! case-insensitive search and reading-order selection.
use hayro::hayro_interpret::font::Glyph;
use hayro::hayro_interpret::hayro_syntax::Pdf;
use hayro::hayro_interpret::{
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, InterpreterWarning, Paint, PathDrawMode,
SoftMask, interpret_page,
};
use kurbo::{Affine, BezPath, Rect, Shape};
use std::collections::{BTreeMap, HashMap, HashSet};
/// Semantic exclusions for statistics. Untagged text is retained; no layout guess is made.
#[derive(Clone, Debug, Default)]
pub struct TextFilter {
pub tables: bool,
pub code: bool,
/// Exclude Figure-marked text and invisible text layers, including OCR.
pub images: bool,
/// Exclude `/ActualText` that marks no glyph, whose words are drawn as outlines or pictures:
/// a page's own text, for deciding whether it still needs reading from its pixels.
pub painted: bool,
/// Page-local MCIDs whose structure ancestors match the selected exclusions.
pub mcids: HashSet<i32>,
}
impl TextFilter {
fn excludes(&self, tag: &[u8], mcid: Option<i32>) -> bool {
(self.tables && matches!(tag, b"Table" | b"TR" | b"TH" | b"TD" | b"THead" | b"TBody" | b"TFoot"))
|| (self.code && tag == b"Code")
|| (self.images && tag == b"Figure")
|| mcid.is_some_and(|id| self.mcids.contains(&id))
}
}
mod search;
/// Whether `(x, y)` lies in or on the convex quadrilateral `quad`, whichever way it winds. A
/// quad with a NaN corner, or no area, holds nothing.
fn quad_contains(quad: &[[f32; 2]; 4], x: f32, y: f32) -> bool {
let (mut positive, mut negative) = (false, false);
for (k, a) in quad.iter().enumerate() {
let b = quad[(k + 1) % 4];
let cross = (b[0] - a[0]) * (y - a[1]) - (b[1] - a[1]) * (x - a[0]);
positive |= cross > 0.0;
negative |= cross < 0.0;
if cross.is_nan() {
return false;
}
}
positive != negative
}
/// One glyph on the page.
#[derive(Clone, Debug, PartialEq)]
pub struct TextGlyph {
/// The glyph's Unicode text: one character, or several for a ligature.
pub text: String,
/// Box in page view space (points, y down): [x0, y0, x1, y1]. The bounding box of `quad`.
pub rect: [f32; 4],
/// The glyph's em box in page view space, as it is drawn (rotated, skewed or mirrored): the
/// start and end of its edge towards the next line, then the end and start of the opposite
/// edge. `quad[0]` → `quad[1]` runs in the writing direction.
pub quad: [[f32; 2]; 4],
/// Unit vector of the writing direction in page view space (`[1, 0]`: left to right).
pub direction: [f32; 2],
}
/// `s` folded for case-insensitive search, one character for one character.
///
/// The dotted and dotless i of Turkish and Azerbaijani (`İ` U+0130, `ı` U+0131) and the Latin
/// `I`/`i` all fold to `i`. `str::to_lowercase` follows no locale: it turns `İ` into `i` plus a
/// combining dot (two characters) and leaves `ı` alone, so neither `İSTANBUL` nor `IRMAK` could
/// be found by typing `istanbul` or `ırmak`. Search can't know the document's language, so the
/// four are treated as one letter; this only adds matches where an `ı` or `İ` is involved.
/// Greek sigma's normal and word-final forms (`σ`/`ς`) both fold to `σ`, as does `Σ`.
pub fn fold_case(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'I' | 'i' | '\u{130}' | '\u{131}' => out.push('i'),
'Σ' | 'σ' | 'ς' => out.push('σ'),
_ => out.extend(c.to_lowercase()),
}
}
out
}
/// Byte ranges in `text` where `needle` occurs, with the find bar's rules ([`PageText::find_opts`]):
/// case folded unless `case_sensitive`, any run of whitespace matching one space, and with
/// `whole_words` only where no letter or digit adjoins the match. At most `limit` ranges, in order
/// and not overlapping. Find and replace matches a paragraph's text with it, so what the find bar
/// highlights is what gets replaced.
pub fn find_in_text(text: &str, needle: &str, case_sensitive: bool, whole_words: bool, limit: usize) -> Vec<std::ops::Range<usize>> {
search::find_in_text(text, needle, case_sensitive, whole_words, limit)
}
/// The text of one page, in content order, with line structure.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct PageText {
pub glyphs: Vec<TextGlyph>,
/// For each glyph: its line index (lines are in reading order, top to bottom).
pub line_of: Vec<u32>,
/// `true` when a word break precedes the glyph on the same line.
pub space_before: Vec<bool>,
/// The page's content was cut at the renderer's limits (#623), so this text is incomplete:
/// whatever the cut part drew is missing, and a search over it can miss.
pub truncated: bool,
}
impl PageText {
pub fn is_empty(&self) -> bool {
self.glyphs.is_empty()
}
/// Text of glyphs `range` with spaces and line breaks reconstructed.
pub fn text_of(&self, range: std::ops::Range<usize>) -> String {
let mut s = String::new();
let mut prev_line = None;
for i in range.clone() {
let Some(g) = self.glyphs.get(i) else { break };
if i != range.start {
if prev_line != Some(self.line_of[i]) {
s.push('\n');
} else if self.space_before[i] {
s.push(' ');
}
}
s.push_str(&g.text);
prev_line = Some(self.line_of[i]);
}
s
}
pub fn plain_text(&self) -> String {
self.text_of(0..self.glyphs.len())
}
/// Case-insensitive search. Returns glyph ranges of every match (words may span line breaks,
/// which count as a single space, or as nothing where the line runs on between two Chinese or
/// Japanese characters or ends in a soft hyphen: there a query with a space across the break
/// doesn't match, and one without does; a ligature matches its letters, and a word divided by a
/// hyphen at a line end matches whole as well as with the hyphen).
pub fn find(&self, needle: &str) -> Vec<std::ops::Range<usize>> {
self.find_opts(needle, false, false)
}
/// Search with Acrobat's find options: case-sensitive, whole words only.
pub fn find_opts(&self, needle: &str, case_sensitive: bool, whole_words: bool) -> Vec<std::ops::Range<usize>> {
self.find_opts_limited(needle, case_sensitive, whole_words, usize::MAX)
}
/// Search with Acrobat's find options, returning at most `limit` glyph ranges.
pub fn find_opts_limited(&self, needle: &str, case_sensitive: bool, whole_words: bool, limit: usize) -> Vec<std::ops::Range<usize>> {
search::find(self, needle, case_sensitive, whole_words, limit)
}
/// Glyph ranges of the matches a character matcher finds in the page text (words and lines
/// separated by one space, original case). Used for pattern search (Search & Redact).
pub fn find_with(&self, matcher: impl Fn(&[char]) -> Vec<std::ops::Range<usize>>) -> Vec<std::ops::Range<usize>> {
let mut chars: Vec<char> = Vec::new();
// `None` is a word or line separator inserted between glyphs. It is not part of either
// glyph, so a match that starts or ends on it must not take the neighbouring glyph.
let mut owner: Vec<Option<usize>> = Vec::new();
for (i, g) in self.glyphs.iter().enumerate() {
let gap = self.space_before.get(i).copied().unwrap_or(false) || self.line_of.get(i) != self.line_of.get(i.saturating_sub(1));
if i > 0 && gap && chars.last().is_some_and(|c| *c != ' ') {
chars.push(' ');
owner.push(None);
}
for c in g.text.chars() {
chars.push(if c.is_whitespace() { ' ' } else { c });
owner.push(Some(i));
}
}
matcher(&chars)
.into_iter()
.filter_map(|r| {
if r.start >= r.end || r.end > chars.len() {
return None;
}
let glyphs = owner.get(r.start..r.end)?;
let start = glyphs.iter().find_map(|g| *g)?;
let end = glyphs.iter().rev().find_map(|g| *g)?.checked_add(1)?;
(start < end).then_some(start..end)
})
.collect()
}
/// Index of the first glyph whose box holds a view-space point: the text under the pointer.
pub fn glyph_at(&self, x: f32, y: f32) -> Option<usize> {
self.glyphs.iter().position(|g| x >= g.rect[0] && x <= g.rect[2] && y >= g.rect[1] && y <= g.rect[3])
}
/// Index of the glyph nearest to a view-space point (for selection anchors).
///
/// A glyph whose drawn em box (`quad`) holds the point wins. Skewed or turned text has bounding
/// boxes wider than the glyphs, so the box of the letter before overlaps this one and would
/// otherwise be found first (#3693). Failing that, the nearest bounding box wins, vertical
/// distance weighing more, and a tie goes to the glyph whose centre is closer. A point level
/// with some text prefers the nearest glyph of that row, so past the end of a short table cell
/// a longer cell in the next row does not win; but a point in the leading right beside another
/// line, parted by a gutter (see [`Self::gutter_between`]) from the row level with it, takes
/// that line: between two lines of one column, it is not taken by another column's line that
/// different baselines put level. Nor is a point beside a barcode drawn with a font taken by
/// the code's row level with it.
pub fn nearest(&self, x: f32, y: f32) -> Option<usize> {
let centre_distance = |g: &TextGlyph| {
let (cx, cy) = (g.quad.iter().map(|q| q[0]).sum::<f32>() / 4.0, g.quad.iter().map(|q| q[1]).sum::<f32>() / 4.0);
(cx - x).powi(2) + (cy - y).powi(2)
};
let inside = self
.glyphs
.iter()
.enumerate()
.filter(|(_, g)| quad_contains(&g.quad, x, y))
.min_by(|a, b| centre_distance(a.1).total_cmp(&centre_distance(b.1)))
.map(|(i, _)| i);
if inside.is_some() {
return inside;
}
// (glyph, distance, centre distance). `total_cmp` ranks NaN last, so a glyph with a broken
// box never wins over a real one.
let mut on_row: Option<(usize, f32, f32)> = None;
let mut any: Option<(usize, f32, f32)> = None;
let closer = |best: Option<(usize, f32, f32)>, d: f32, c: f32| best.is_none_or(|(_, b, bc)| d.total_cmp(&b).then(c.total_cmp(&bc)).is_lt());
for (i, g) in self.glyphs.iter().enumerate() {
let dx = if x < g.rect[0] {
g.rect[0] - x
} else if x > g.rect[2] {
x - g.rect[2]
} else {
0.0
};
let dy = if y < g.rect[1] {
g.rect[1] - y
} else if y > g.rect[3] {
y - g.rect[3]
} else {
0.0
};
let c = centre_distance(g);
if dy == 0.0 && closer(on_row, dx, c) {
on_row = Some((i, dx, c));
}
// Prefer the same line: vertical distance weighs more.
let d = dx + dy * 3.0;
if closer(any, d, c) {
any = Some((i, d, c));
}
}
match (on_row, any) {
// The point sits in the leading right beside another line (within half its height), and
// a gutter parts it from the row level with it: that row is another column's.
(Some((r, _, _)), Some((i, _, _))) if r != i => {
let (Some(g), Some(n)) = (self.glyphs.get(r), self.glyphs.get(i)) else { return Some(r) };
let beside = (n.rect[1] - y).max(y - n.rect[3]) <= (n.rect[3] - n.rect[1]) * 0.5;
let h = g.rect[3] - g.rect[1];
// From the far end of the row's line, so its column's shorter lines count too.
let toward_right = x > g.rect[2];
let from = self.line_span(r).map_or(if toward_right { g.rect[2] } else { g.rect[0] }, |(l0, l1)| if toward_right { l0 } else { l1 });
let reach = h * GUTTER_ROWS;
// A barcode drawn with a font has a row at every height beside it; the point is only
// on one of them when it is over the code.
let off_barcode = self.line_span(r).is_some_and(|(r0, r1)| !(r0..=r1).contains(&x))
&& self.line_of.get(r).is_some_and(|l| self.barcode_lines().contains(l));
Some(if off_barcode || (beside && self.gutter_between(from, x, y - reach, y + reach, h)) { i } else { r })
}
(on_row, any) => on_row.or(any).map(|(i, _, _)| i),
}
}
/// The word around glyph `i` as its first and last glyph (double-click): it stops at word
/// gaps, blank glyphs and line ends.
pub fn word_at(&self, i: usize) -> Option<(usize, usize)> {
self.glyphs.get(i)?;
let blank = |k: usize| self.glyphs.get(k).is_none_or(|g| g.text.trim().is_empty());
// Glyph `k` (≥ 1) follows glyph `k - 1` with no word gap or line break between them.
let joined = |k: usize| !self.space_before.get(k).copied().unwrap_or(true) && self.line_of.get(k - 1) == self.line_of.get(k);
let mut first = i;
while first > 0 && joined(first) && !blank(first - 1) {
first -= 1;
}
let mut last = i;
while joined(last + 1) && !blank(last + 1) {
last += 1;
}
Some((first, last))
}
/// The line around glyph `i` as its first and last glyph (triple-click). A line's glyphs are
/// consecutive.
pub fn line_at(&self, i: usize) -> Option<(usize, usize)> {
self.glyphs.get(i)?;
let line = self.line_of.get(i)?;
let same = |k: usize| self.line_of.get(k) == Some(line);
let mut first = i;
while first > 0 && same(first - 1) {
first -= 1;
}
let mut last = i;
while same(last + 1) {
last += 1;
}
Some((first, last))
}
/// Merge the boxes of `range` into one rectangle per line (for highlighting).
pub fn line_rects(&self, range: std::ops::Range<usize>) -> Vec<[f32; 4]> {
self.merged_rects(range)
}
/// One rectangle per run of consecutive glyphs on one line, for glyph indices in ascending
/// order (a reading-order range, or [`Self::glyphs_in`]). A run stops at a skipped glyph, so
/// the rectangle never covers text that is not selected.
pub fn glyph_rects(&self, glyphs: &[usize]) -> Vec<[f32; 4]> {
self.merged_rects(glyphs.iter().copied())
}
fn merged_rects(&self, glyphs: impl IntoIterator<Item = usize>) -> Vec<[f32; 4]> {
let mut out: Vec<(u32, usize, [f32; 4])> = Vec::new();
for i in glyphs {
let (Some(g), Some(&line)) = (self.glyphs.get(i), self.line_of.get(i)) else { break };
match out.last_mut() {
Some((l, last, r)) if *l == line && last.checked_add(1) == Some(i) => {
*last = i;
union(r, &g.rect);
}
_ => out.push((line, i, g.rect)),
}
}
out.into_iter().map(|(_, _, r)| r).collect()
}
/// The anchor and head for [`Self::visual_between`] that take two units (words or lines, each
/// as its first and last glyph in reading order) whole, from `unit` to `to`: the earlier
/// unit's left end and the later one's right end, earlier meaning on a higher row, or on the
/// same row and to the left. Reading order may have them the other way round (a drag from a
/// right-hand cell down into a left-hand one), and a right-to-left word reads from its right
/// end, so the ends are taken by position.
pub fn row_ends(&self, unit: (usize, usize), to: (usize, usize)) -> (usize, usize) {
// A unit's outer glyphs: its leftmost and rightmost, whatever its reading order.
let outer = |(i, j): (usize, usize)| match (self.glyphs.get(i), self.glyphs.get(j)) {
(Some(g), Some(h)) if h.rect[0] < g.rect[0] => (j, i),
_ => (i, j),
};
let ((unit_left, unit_right), (to_left, to_right)) = (outer(unit), outer(to));
let later = match (self.glyphs.get(unit_left), self.glyphs.get(to_left)) {
(Some(a), Some(b)) => {
let dy = (b.rect[1] + b.rect[3] - a.rect[1] - a.rect[3]) / 2.0;
let h = (a.rect[3] - a.rect[1]).min(b.rect[3] - b.rect[1]);
if dy.abs() > h / 2.0 { dy > 0.0 } else { b.rect[0] > a.rect[0] }
}
_ => to.0 > unit.0,
};
if later { (unit_left, to_right) } else { (unit_right, to_left) }
}
/// What a drag from glyph `anchor` to glyph `head` selects by visual rows, as Preview selects
/// it, where reading order between them would not follow the rows: on one row, the row's
/// glyphs from one end to the other, as [`Self::glyphs_in`] takes them; over several, the rest
/// of the top row from the upper end, the rows between and the bottom row up to the lower end.
///
/// A table reads column by column, so a drag across its columns would take the first column
/// down to the drag's end and the next ones from the top, and a row whose cells came apart
/// reads its last cells after the table. Rows are used when reading order strays above or
/// below the rows the drag covers, or when the drag ends in another column than it started
/// in: the two glyphs' lines don't overlap side by side, or the pointer, at `head_x` (view
/// space) when known, ended past the end of both of them. `None` otherwise
/// (running text, a drag down one article or table column): the reading-order range is the
/// answer.
pub fn visual_between(&self, anchor: usize, head: usize, head_x: Option<f32>) -> Option<Vec<usize>> {
let (a, h) = (self.glyphs.get(anchor)?, self.glyphs.get(head)?);
if self.line_of.get(anchor)? == self.line_of.get(head)? {
return None;
}
// Rows mean nothing in vertical writing or turned text: two vertical columns at one
// height are not a row. Right-to-left rows are still rows.
let horizontal = |g: &TextGlyph| g.direction[1].abs() <= WORD_GAP;
if !horizontal(a) || !horizontal(h) {
return None;
}
let centre = |g: &TextGlyph| (g.rect[1] + g.rect[3]) / 2.0;
let (upper, lower) = if centre(a) <= centre(h) { (a, h) } else { (h, a) };
let (top, bottom) = (upper.rect[1].min(lower.rect[1]), upper.rect[3].max(lower.rect[3]));
let range = self.glyphs.get(anchor.min(head)..=anchor.max(head))?;
let strays = !range.iter().all(|g| (top..=bottom).contains(&centre(g)));
// One row: each one's centre lies within the other's height.
let within = |y: f32, g: &TextGlyph| (g.rect[1]..=g.rect[3]).contains(&y);
let one_row = within(centre(upper), lower) && within(centre(lower), upper);
let spans = self.line_span(anchor).zip(self.line_span(head));
// From the anchor to where the pointer ended (or the head glyph), across a gutter no text
// of these rows crosses: another column. Word gaps in a column, however wide a justified
// line spreads them, are crossed by the other rows' text.
// From the far end of the anchor's line, so its column's other lines count wherever along
// the line the drag started.
let to_x = head_x.unwrap_or((h.rect[0] + h.rect[2]) / 2.0);
let a_x = (a.rect[0] + a.rect[2]) / 2.0;
let from = spans.map_or(a_x, |((a0, a1), _)| if to_x >= a_x { a0 } else { a1 });
let strip = self.gutter_strip(from, to_x, top, bottom, a.rect[3] - a.rect[1]);
let other_column = strip.is_some();
if !strays && (one_row || !other_column) {
return None;
}
// From one column of running text into the next, reading order carries the text on (the
// rest of one column, then the start of the next); rows would interleave the columns. One
// row shows too little of the columns, so a drag along one looks at the rows around it.
let lh = a.rect[3] - a.rect[1];
let (near_top, near_bottom) = if one_row { (top - lh * GUTTER_ROWS, bottom + lh * GUTTER_ROWS) } else { (top, bottom) };
let strip = if one_row { self.gutter_strip(from, to_x, near_top, near_bottom, lh) } else { strip };
if let Some(strip) = strip
&& self.running_text_beside(strip, from.min(to_x), from.max(to_x), near_top, near_bottom)
{
return None;
}
let picked = if one_row {
self.glyphs_in([a.rect[0].min(h.rect[0]), top, a.rect[2].max(h.rect[2]), bottom])
} else {
// Rows run left to right from the upper end; other writing keeps reading order.
let ltr = |g: &TextGlyph| g.direction[0] > 0.0 && g.direction[1].abs() <= WORD_GAP && !g.text.chars().any(is_rtl);
if !ltr(upper) || !ltr(lower) {
return None;
}
// The rows take only lines reaching into the columns the drag went through: none left
// of everywhere it has been (the column beside a right-hand one), and, when it stayed
// in one column (reading order only strayed, as when a justified line's last word is
// read late), none right of it either. Across columns, rows run on to their end.
let ((a0, a1), (b0, b1)) = spans?;
let pointer = head_x.filter(|x| x.is_finite());
let left = a0.min(b0).min(pointer.unwrap_or(f32::INFINITY));
let right = if other_column { f32::INFINITY } else { a1.max(b1).max(pointer.unwrap_or(f32::NEG_INFINITY)) };
let mut lines: BTreeMap<u32, (f32, f32)> = BTreeMap::new();
for (g, &l) in self.glyphs.iter().zip(&self.line_of) {
if (top..=bottom).contains(&centre(g)) && !g.text.trim().is_empty() {
let span = lines.entry(l).or_insert((f32::INFINITY, f32::NEG_INFINITY));
*span = (span.0.min(g.rect[0]), span.1.max(g.rect[2]));
}
}
let in_column = |l: u32| lines.get(&l).is_some_and(|&(l0, l1)| l0.max(left) < l1.min(right));
// Across columns, nearly every row of a table has cells side by side; a column of
// running text beside a sidebar or a figure caption has many rows with nothing beside
// them. Rows would interleave the two, so reading order is kept. The whole rows of the
// band within the columns, not just the glyphs picked: the bottom row is cut off at the
// pointer and may show a single cell, and text left of the drag is no part of the rows.
if other_column {
let band: Vec<usize> = (0..self.glyphs.len())
.filter(|&i| self.glyphs.get(i).is_some_and(|g| (top..=bottom).contains(&centre(g))))
.filter(|&i| self.line_of.get(i).is_some_and(|&l| in_column(l)))
.collect();
let (multi, rows) = self.cell_rows(&band);
if (multi as f32) < rows as f32 * ROW_CELLS {
return None;
}
}
let wanted = |(g, &l): (&TextGlyph, &u32)| {
let (cx, cy) = ((g.rect[0] + g.rect[2]) / 2.0, centre(g));
let on_top_row = cy <= upper.rect[3];
let on_bottom_row = cy >= lower.rect[1];
(top..=bottom).contains(&cy) && (!on_top_row || cx >= upper.rect[0]) && (!on_bottom_row || cx <= lower.rect[2]) && in_column(l)
};
self.glyphs.iter().zip(&self.line_of).enumerate().filter(|(_, gl)| wanted(*gl)).map(|(i, _)| i).collect()
};
// The same glyphs as reading order, in running text: its text is the better copy. In a
// table (pieces side by side on a row) the rows give the copy its row-by-row order.
let (lo, hi) = (anchor.min(head), anchor.max(head));
let same = picked.first() == Some(&lo) && picked.last() == Some(&hi) && picked.len() == hi - lo + 1;
if same && !self.side_by_side(&picked) {
return None;
}
(!picked.is_empty()).then_some(picked)
}
/// Whether two of the lines `glyphs` fall on sit side by side on one row, as table cells do.
/// Only the first [`SIDE_BY_SIDE_LINES`] lines are compared.
fn side_by_side(&self, glyphs: &[usize]) -> bool {
let mut lines: Vec<(u32, [f32; 4])> = Vec::new();
for &i in glyphs {
let (Some(g), Some(&l)) = (self.glyphs.get(i), self.line_of.get(i)) else { continue };
if let Some((last, bb)) = lines.last_mut()
&& *last == l
{
union(bb, &g.rect);
} else if lines.len() >= SIDE_BY_SIDE_LINES {
break;
} else {
lines.push((l, g.rect));
}
}
lines.iter().enumerate().any(|(k, (_, a))| {
lines.iter().skip(k + 1).any(|(_, b)| {
let (ca, cb) = ((a[1] + a[3]) / 2.0, (b[1] + b[3]) / 2.0);
(b[1]..=b[3]).contains(&ca) && (a[1]..=a[3]).contains(&cb)
})
})
}
/// The visual rows of the visible glyphs `glyphs` (by vertical position, whichever lines they
/// belong to), as how many have two or more cells (a gap of [`ROW_CELL_GAP`] line heights between
/// glyphs) and how many there are. A statement row written as one long line with wide gaps
/// counts as cells, as a table row of separate lines does.
fn cell_rows(&self, glyphs: &[usize]) -> (usize, usize) {
let mut ink: Vec<(f32, f32, f32, f32)> = glyphs
.iter()
.filter_map(|&i| self.glyphs.get(i))
.filter(|g| !g.text.trim().is_empty())
.map(|g| ((g.rect[1] + g.rect[3]) / 2.0, g.rect[0], g.rect[2], g.rect[3] - g.rect[1]))
.collect();
ink.sort_by(|a, b| a.0.total_cmp(&b.0));
let (mut rows, mut multi) = (0usize, 0usize);
let mut start = 0;
while start < ink.len() {
// A row runs while the centres stay within 0.6 line heights of the row's first glyph.
let (cy0, h0) = (ink[start].0, ink[start].3.max(0.1));
let end = start + ink[start..].iter().take_while(|g| (g.0 - cy0).abs() <= h0 * 0.6).count().max(1);
let mut row: Vec<(f32, f32)> = ink[start..end].iter().map(|g| (g.1, g.2)).collect();
row.sort_by(|a, b| a.0.total_cmp(&b.0));
let mut reach = f32::NEG_INFINITY;
let gap = row.iter().any(|&(x0, x1)| {
let wide = reach.is_finite() && x0 - reach > h0 * ROW_CELL_GAP;
reach = reach.max(x1);
wide
});
rows += 1;
multi += usize::from(gap);
start = end;
}
(multi, rows)
}
/// Whether a column gutter lies between `x0` and `x1` (either order) in the rows whose
/// centres lie between `top` and `bottom`: a vertical strip at least `width` wide that no
/// glyph of those rows crosses, with at least two lines of text on each side of it between
/// `x0` and `x1`, or beyond either when the strip reaches it (columns' baselines need not
/// line up). The gap a justified line leaves between two words has only that line beyond it.
fn gutter_between(&self, x0: f32, x1: f32, top: f32, bottom: f32, width: f32) -> bool {
self.gutter_strip(x0, x1, top, bottom, width).is_some()
}
/// [`Self::gutter_between`]'s gutter, as its left and right edges.
fn gutter_strip(&self, x0: f32, x1: f32, top: f32, bottom: f32, width: f32) -> Option<(f32, f32)> {
let (lo, hi) = (x0.min(x1), x0.max(x1));
if !(hi - lo >= width && width > 0.0) {
return None;
}
let centre = |g: &TextGlyph| (g.rect[1] + g.rect[3]) / 2.0;
// The rows' glyphs as (centre, left, right), top to bottom.
let mut rows: Vec<(f32, f32, f32)> =
self.glyphs.iter().filter(|g| (top..=bottom).contains(&centre(g))).map(|g| (centre(g), g.rect[0], g.rect[2])).collect();
rows.sort_by(|a, b| a.0.total_cmp(&b.0));
// Two lines of text on each side of the strip g0..g1: glyphs whose centres lie within half
// a width of the previous one's (rows is sorted by centre) are on its line.
let lines_beside = |side: &dyn Fn(f32, f32) -> bool| {
let (mut lines, mut last) = (0usize, f32::NEG_INFINITY);
for &(cy, _, _) in rows.iter().filter(|&&(_, a, b)| side(a, b)) {
if cy - last > width * 0.5 {
lines += 1;
}
last = cy;
}
lines
};
// Text within lo..hi counts, so a column beyond the span parts nothing inside it; but a
// strip running on to either end, where the point is out in a gap, is a gutter if a
// column lies beyond it.
let parted = |g0: f32, g1: f32| {
let left = if g0 <= lo { f32::NEG_INFINITY } else { lo };
let right = if g1 >= hi { f32::INFINITY } else { hi };
lines_beside(&|_, b| (left..=g0).contains(&b)) >= 2 && lines_beside(&|a, _| (g1..=right).contains(&a)) >= 2
};
let mut spans: Vec<(f32, f32)> = rows.iter().filter(|&&(_, a, b)| b > lo && a < hi).map(|&(_, a, b)| (a, b)).collect();
spans.sort_by(|a, b| a.0.total_cmp(&b.0));
let mut covered = lo;
for (s0, s1) in spans {
if s0 - covered >= width && parted(covered, s0) {
return Some((covered, s0));
}
covered = covered.max(s1);
}
(hi - covered >= width && parted(covered, hi)).then_some((covered, hi))
}
/// Whether the lines between `top` and `bottom` on each side of the gutter `g0..g1` are
/// running text: one column a side (no two lines side by side), most of its lines (at least
/// half: paragraphs' first and last lines fall short) starting and ending at about the same
/// place, at least half as wide as the gutter and [`RUNNING_TEXT_EMS`] line heights wide.
/// Table cells, terms and form labels are short or ragged beside a wide gap, or sit several
/// to a row. Only the lines ending after `lo` on the left, and starting before `hi`
/// (or right after the gutter) on the right, count.
fn running_text_beside(&self, (g0, g1): (f32, f32), lo: f32, hi: f32, top: f32, bottom: f32) -> bool {
let mut lines: BTreeMap<u32, [f32; 4]> = BTreeMap::new();
for (g, &l) in self.glyphs.iter().zip(&self.line_of) {
if (top..=bottom).contains(&((g.rect[1] + g.rect[3]) / 2.0)) && !g.text.trim().is_empty() {
lines.entry(l).and_modify(|r| union(r, &g.rect)).or_insert(g.rect);
}
}
let gutter = g1 - g0;
let column = |side: Vec<[f32; 4]>| {
let n = side.len();
if n < 2 {
return false;
}
let (x0, x1) = side.iter().fold((f32::INFINITY, f32::NEG_INFINITY), |(a, b), r| (a.min(r[0]), b.max(r[2])));
let w = x1 - x0;
let one_a_row = side.iter().enumerate().all(|(k, a)| {
side.iter().skip(k + 1).all(|b| {
let (ca, cb) = ((a[1] + a[3]) / 2.0, (b[1] + b[3]) / 2.0);
!((b[1]..=b[3]).contains(&ca) || (a[1]..=a[3]).contains(&cb))
})
});
let starts = side.iter().filter(|r| r[0] <= x0 + w * RUNNING_TEXT_RAG).count();
let ends = side.iter().filter(|r| r[2] >= x1 - w * RUNNING_TEXT_RAG).count();
let tallest = side.iter().map(|r| r[3] - r[1]).fold(0.0f32, f32::max);
one_a_row && w >= gutter * 0.5 && w >= tallest * RUNNING_TEXT_EMS && starts * 2 >= n && ends * 2 >= n
};
let reach = hi.max(g1 + gutter);
let left: Vec<[f32; 4]> = lines.values().filter(|r| r[2] <= g0 && r[2] >= lo).copied().collect();
let right: Vec<[f32; 4]> = lines.values().filter(|r| r[0] >= g1 && r[0] <= reach).copied().collect();
left.len() <= SIDE_BY_SIDE_LINES && right.len() <= SIDE_BY_SIDE_LINES && column(left) && column(right)
}
/// The lines that are rows of a barcode drawn with a font (a QR or Aztec code), as the layout
/// finds them: such rows read after all other text.
fn barcode_lines(&self) -> HashSet<u32> {
let mut lines: Vec<(u32, usize, [f32; 4])> = Vec::new();
for (g, &l) in self.glyphs.iter().zip(&self.line_of) {
match lines.last_mut() {
Some((id, n, bb)) if *id == l => {
*n += 1;
union(bb, &g.rect);
}
_ => lines.push((l, 1, g.rect)),
}
}
let rows: Vec<(usize, [f32; 4])> = lines.iter().map(|&(_, n, bb)| (n, bb)).collect();
lines.iter().zip(barcode_rows(&rows)).filter(|(_, barcode)| *barcode).map(|((l, _, _), _)| *l).collect()
}
/// The left and right edges of the ink of the line glyph `i` is on: spaces padding it out (a
/// heading indented with spaces past a barcode beside it) are not where its text is. A line of
/// spaces only spans its spaces.
fn line_span(&self, i: usize) -> Option<(f32, f32)> {
let (first, last) = self.line_at(i)?;
let line = self.glyphs.get(first..=last)?;
let span = |inked: bool| {
line.iter()
.filter(|g| !inked || !g.text.trim().is_empty())
.fold((f32::INFINITY, f32::NEG_INFINITY), |(x0, x1), g| (x0.min(g.rect[0]), x1.max(g.rect[2])))
};
Some(Some(span(true)).filter(|(x0, x1)| x0 <= x1).unwrap_or_else(|| span(false)))
}
/// The glyphs whose centre lies inside `rect` (view space, corners in either order), in
/// reading order: what a column selection covers (Alt/Option-drag, Acrobat's column select).
/// A glyph only partly inside counts when its centre is, so a rectangle drawn a little into
/// the next column does not pick up the edge of its text.
pub fn glyphs_in(&self, rect: [f32; 4]) -> Vec<usize> {
let (x0, x1) = (rect[0].min(rect[2]), rect[0].max(rect[2]));
let (y0, y1) = (rect[1].min(rect[3]), rect[1].max(rect[3]));
let inside = |g: &TextGlyph| {
let (cx, cy) = ((g.rect[0] + g.rect[2]) / 2.0, (g.rect[1] + g.rect[3]) / 2.0);
(x0..=x1).contains(&cx) && (y0..=y1).contains(&cy)
};
self.glyphs.iter().enumerate().filter(|(_, g)| inside(g)).map(|(i, _)| i).collect()
}
/// The text of a column selection ([`Self::glyphs_in`]) as it looks on the page: one row per
/// visual line, top to bottom. Pieces of different lines that sit side by side (table cells,
/// newspaper columns) share a row and are separated by a tab, so a pasted table lands in
/// spreadsheet cells. Rows of right-to-left text put their pieces right to left. When any
/// selected glyph does not run horizontally (CJK vertical writing, turned text, a page shown
/// turned by `/Rotate`) rows mean nothing, and the lines come out one per row in reading order
/// instead.
pub fn column_text(&self, glyphs: &[usize]) -> String {
// The selected glyphs of each line, with their box: (glyphs, bbox). A line is cut where a gap wider than one run
// of text can hold separates two glyphs (`arrange` joins runs on one baseline into a line, as table cells are), so
// cells keep their tab.
let mut lines: Vec<(Vec<usize>, [f32; 4])> = Vec::new();
let mut line_ids: Vec<u32> = Vec::new();
let mut previous: Option<&TextGlyph> = None;
for &i in glyphs {
let (Some(g), Some(&line)) = (self.glyphs.get(i), self.line_of.get(i)) else { continue };
let apart = previous.is_some_and(|p| {
let h = (p.rect[3] - p.rect[1]).max(g.rect[3] - g.rect[1]);
g.rect[0] - p.rect[2] >= CELL_GAP * h
});
previous = Some(g);
match (lines.last_mut(), line_ids.last()) {
(Some((members, bb)), Some(&l)) if l == line && !apart => {
members.push(i);
union(bb, &g.rect);
}
_ => {
lines.push((vec![i], g.rect));
line_ids.push(line);
}
}
}
// Within the layout's own tolerance for reading a glyph as unturned.
let vertical = glyphs.iter().any(|&i| self.glyphs.get(i).is_some_and(|g| g.direction[1].abs() > WORD_GAP));
let piece = |members: &[usize]| {
let mut s = String::new();
let mut prev: Option<usize> = None;
for &i in members {
let Some(g) = self.glyphs.get(i) else { continue };
let gap = self.space_before.get(i).copied().unwrap_or(false) || prev.and_then(|p| p.checked_add(1)) != Some(i);
if prev.is_some() && gap {
s.push(' ');
}
s.push_str(&g.text);
prev = Some(i);
}
s
};
if vertical {
return lines.iter().map(|(members, _)| piece(members)).collect::<Vec<_>>().join("\n");
}
// Rows: a line joins the row whose first line its vertical centre falls within.
let mut order: Vec<usize> = (0..lines.len()).collect();
order.sort_by(|a, b| {
let cy = |k: usize| lines.get(k).map_or(0.0, |(_, bb)| (bb[1] + bb[3]) / 2.0);
cy(*a).total_cmp(&cy(*b))
});
let mut rows: Vec<(f32, f32, Vec<usize>)> = Vec::new();
for k in order {
let Some((_, bb)) = lines.get(k) else { continue };
let cy = (bb[1] + bb[3]) / 2.0;
match rows.last_mut() {
Some((top, bottom, members)) if cy >= *top && cy <= *bottom => members.push(k),
_ => rows.push((bb[1], bb[3], vec![k])),
}
}
let rtl = |members: &[usize]| members.iter().any(|&i| self.glyphs.get(i).is_some_and(|g| g.text.chars().any(is_rtl)));
let mut out = Vec::with_capacity(rows.len());
for (_, _, mut members) in rows {
let right_to_left = members.iter().all(|&k| lines.get(k).is_some_and(|(m, _)| rtl(m)));
members.sort_by(|a, b| {
let x = |k: usize| lines.get(k).map_or(0.0, |(_, bb)| bb[0]);
if right_to_left { x(*b).total_cmp(&x(*a)) } else { x(*a).total_cmp(&x(*b)) }
});
let cells: Vec<String> = members.iter().filter_map(|&k| lines.get(k)).map(|(m, _)| piece(m)).collect();
out.push(cells.join("\t"));
}
out.join("\n")
}
}
/// Line heights above and below a point (or a one-row drag) looked over for a gutter.
const GUTTER_ROWS: f32 = 4.0;
/// The share of a row selection's visual rows that must have cells side by side for the rows to be
/// a table's: a column of running text beside a sidebar has such cells on only some of its rows.
const ROW_CELLS: f32 = 0.75;
/// A gap of this many line heights between two glyphs of a row parts two cells (a word gap is
/// about a third of a line height).
const ROW_CELL_GAP: f32 = 2.0;
/// Running text is at least this many line heights wide; terms, labels and narrow cells are not.
const RUNNING_TEXT_EMS: f32 = 8.0;
/// The share of a column's width its running text's lines may fall short of its edges by.
const RUNNING_TEXT_RAG: f32 = 0.2;
/// Lines a selection compares pairwise to tell a table from running text.
const SIDE_BY_SIDE_LINES: usize = 256;
use hayro::hayro_interpret::TransformExt;
mod device;
mod layout;
mod order;
use device::{CELL_GAP, WORD_GAP};
#[cfg(test)]
use device::{MAX_REDRAW_TEXT, REDRAW_TEXT_BYTES, Replaced, TextDevice, ligature_letters};
pub(crate) use device::{extract_page, extract_page_filtered};
pub use layout::layout;
#[cfg(test)]
use layout::{from_upright, to_upright};
use layout::{is_rtl, union};
use order::barcode_rows;
#[cfg(test)]
use order::{Run, Scratch, Seg, collide, restarts_run};
#[cfg(test)]
mod tests;