Skip to main content

PageText

Struct PageText 

Source
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

Source

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?
examples/extract_text.rs (line 16)
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}
Source

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?
examples/extract_text.rs (line 18)
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}
Source

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.

Source

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.)

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more