Skip to main content

Crate pdfrum_text

Crate pdfrum_text 

Source
Expand description

§pdfrum-text

Text extraction (ISO 32000-1 §14.8.2): reading order, search, selection geometry, word segmentation and bare-URL detection. It walks the same interpreted page-object graph the renderer walks and never rasterizes, so extracting text from a page costs no pixels and needs no backend.

use pdfrum_text::{FindOptions, TextIndex, TextPage};

let page = TextPage {
    search_text: "Hello, world!".chars().collect(),
    ..TextPage::default()
};
let hit = page.find("world", FindOptions::default()).next().expect("a match");
assert_eq!(hit, TextIndex::new(7)..TextIndex::new(12));

Two index spaces, and mixing them is the bug this crate is shaped to prevent. CharIndex addresses what the page draws — every glyph in drawing order, including generated spaces, hyphens at line breaks and control characters that have geometry but no meaning. TextIndex addresses what search and copy see, with those removed. A hit from TextPage::find is a TextIndex range; TextPage::rects and TextPage::char want a CharIndex; IndexMap is the conversion, and the two are distinct types so the compiler refuses the mistake rather than returning a highlight in the wrong place.

Geometry is per character, not per line. CharBox carries each glyph’s quadrilateral in page space, which is what makes TextPage::rects able to return a selection that follows rotated or skewed text, TextPage::index_at able to answer a click, and TextPage::text_in_rect able to lift a column out of a two-column page.

Reading order is reconstructed, not read off the file: a content stream may draw a page’s text in any order at all, and extract sorts runs into the order a human reads them, right-to-left when ExtractOptions::rtl says so.

Part of pdfrum. #![forbid(unsafe_code)].

MIT OR Apache-2.0

Structs§

CharBox
One extracted character, with its metrics and page geometry.
CharIndex
A position in the character list (TextPage::chars) — the sequence a --txt dump emits.
CharSegment
One run of characters that made it into the text.
ExtractOptions
How extraction behaves.
FindOptions
How a search behaves.
IndexMap
The map between the two index spaces of TextPage.
ObjectIndex
A text object’s position in the page’s flattened object walk.
TextIndex
A position in the search-facing text (TextPage::search_text) — what a search matches and a selection copies.
TextPage
One page’s extracted text (ISO 32000-1 §14.8.2).
TextRun
A text object with everything the heuristics ask of it.
WebLink
One address found in a page’s text.
Word
One word of a page’s text, from TextPage::words.

Enums§

CharType
Where a character came from, which decides how the rest of the pipeline treats it.
Error
What can go wrong querying an extracted page.
Orientation
Which way a line of text runs.

Functions§

extract
Extracts text, layout, and reading order from an interpreted page (ISO 32000-1 §14.8.2).
words
The words a page draws, in content order — the answer Doc.getPageNumWords counts and Doc.getPageNthWord indexes into.