Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an optional word delimiter. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,7 @@ pub struct PlainTextOutput<W: ConvertToFmt> {
last_y: f64,
first_char: bool,
flip_ctm: Transform,
word_delimiter: Option<String>,
}

impl<W: ConvertToFmt> PlainTextOutput<W> {
Expand All @@ -1909,8 +1910,16 @@ impl<W: ConvertToFmt> PlainTextOutput<W> {
first_char: false,
last_y: 0.,
flip_ctm: Transform2D::identity(),
word_delimiter: None,
}
}

/// Can be useful when trying to extract table like pdf text, with a bit a
/// luck some of the output can be interpreted as CSV.
pub fn word_delimiter(&mut self, delim: Option<String>) -> &mut Self {
self.word_delimiter = delim;
self
}
}

/* There are some structural hints that PDFs can use to signal word and line endings:
Expand Down Expand Up @@ -1957,7 +1966,15 @@ impl<W: ConvertToFmt> OutputDev for PlainTextOutput<W> {
self.first_char = true;
Ok(())
}
fn end_word(&mut self) -> Result<(), OutputError> {Ok(())}

fn end_word(&mut self) -> Result<(), OutputError> {
use std::fmt::Write;
if let Some(s) = &self.word_delimiter {
write!(self.writer, "{}", s)?;
}
Ok(())
}

fn end_line(&mut self) -> Result<(), OutputError>{
//write!(self.file, "\n");
Ok(())
Expand Down