pub struct PageText { /* private fields */ }Expand description
A page’s extracted text: the runs in draw order, plus a whitespace-stripped, lowercased index for robust quote matching.
Implementations§
Source§impl PageText
impl PageText
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the page has any extractable text (false for pure scans with no OCR layer — the host should then fall back to area markup).
Examples found in repository?
7fn main() {
8 let mut args = std::env::args().skip(1);
9 let path = args.next().expect("usage: extract_text <file.pdf> [page]");
10 let page: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(0);
11
12 let bytes = Arc::new(std::fs::read(&path).expect("read pdf"));
13 let doc = gpui_pdf::parse(bytes).expect("parse pdf");
14
15 match gpui_pdf::extract_page_text(&doc, page) {
16 Some(pt) if !pt.is_empty() => {
17 println!("--- page {page} text ---");
18 println!("{}", pt.text());
19 }
20 Some(_) => eprintln!("page {page}: no extractable text (scanned image?)"),
21 None => eprintln!("no page {page}"),
22 }
23}Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
A readable reconstruction of the page text (spaces inserted on horizontal
gaps, newlines on baseline changes). For search / display — locate uses the
whitespace-insensitive index instead.
Examples found in repository?
7fn main() {
8 let mut args = std::env::args().skip(1);
9 let path = args.next().expect("usage: extract_text <file.pdf> [page]");
10 let page: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(0);
11
12 let bytes = Arc::new(std::fs::read(&path).expect("read pdf"));
13 let doc = gpui_pdf::parse(bytes).expect("parse pdf");
14
15 match gpui_pdf::extract_page_text(&doc, page) {
16 Some(pt) if !pt.is_empty() => {
17 println!("--- page {page} text ---");
18 println!("{}", pt.text());
19 }
20 Some(_) => eprintln!("page {page}: no extractable text (scanned image?)"),
21 None => eprintln!("no page {page}"),
22 }
23}Sourcepub fn locate(&self, needle: &str, occurrence: usize) -> Vec<NormRect>
pub fn locate(&self, needle: &str, occurrence: usize) -> Vec<NormRect>
Locate the occurrence-th (0-based) case- and whitespace-insensitive match of
needle on the page and return one normalized rect per line it spans (so a
wrapped quote highlights as multiple line boxes). Empty if not found.
Sourcepub fn find_matches(&self, needle: &str) -> Vec<Vec<NormRect>>
pub fn find_matches(&self, needle: &str) -> Vec<Vec<NormRect>>
Every (non-overlapping) case- and whitespace-insensitive match of needle on
the page, each as one normalized rect per line it spans (for find-in-PDF).
Matches are returned in reading order. (search feature.)
Sourcepub fn select(&self, from: NormPoint, to: NormPoint) -> Option<Selection>
pub fn select(&self, from: NormPoint, to: NormPoint) -> Option<Selection>
Resolve a drag from from to to into a Selection: the run range between
the nearest glyphs (draw order ≈ reading order), its one-line quote, the
occurrence index, and the rects to draw. None if there’s no text or the
selection is empty.
Auto Trait Implementations§
impl Freeze for PageText
impl RefUnwindSafe for PageText
impl Send for PageText
impl Sync for PageText
impl Unpin for PageText
impl UnsafeUnpin for PageText
impl UnwindSafe for PageText
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more