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

Support font encoder #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ description = "PDF text extraction"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]
pdf = { git = "https://github.com/pdf-rs/pdf", features = ["cache"] }
pdf_render = { git = "https://github.com/pdf-rs/pdf_render" }
font = { git = "https://github.com/pdf-rs/font" }
pdf_render= { git = "https://github.com/videni/pdf_render_with_vello.git", branch="vello_wip"}
# pdf_render= { path = "../pdf_render/render"}
pdf = { git = "https://github.com/pdf-rs/pdf", features = ["cache", "dump"], default-features = false, rev = "9002322822a3773d3d265dee81d855b40f5e0d0a"}

itertools = "*"
log = "*"
ordered-float = "*"
serde = { version = "*", features = ["derive"] }
unicode-normalization = "0.1.19"
font = { git = "https://github.com/videni/font", branch = "master", features=['cff']}

pathfinder_geometry = { git = "https://github.com/servo/pathfinder" }
pathfinder_color = { git = "https://github.com/servo/pathfinder" }
Expand Down
36 changes: 28 additions & 8 deletions examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ fn main() {
let file = FileOptions::cached().open(&input).expect("can't read PDF");
let resolver = file.resolver();

for (page_nr, page) in file.pages().enumerate() {
let page = page.expect("can't read page");
let flow = pdf_text::run(&file, &page, &resolver).expect("can't render page");
println!("# page {}", page_nr + 1);
// for (page_nr, page) in file.pages().enumerate() {
let page: pdf::object::PageRc = file.get_page(0).unwrap();
let flow = pdf_text::run(&file, &page, &resolver, Default::default(), false).expect("can't render page");
for run in flow.runs {
for line in run.lines {
println!("{}", line.words.iter().map(|w| &w.text).format(" "));
for line in &run.lines {
println!("{:?}", line.rect);
for word in &line.words {
println!("{}, {:?}", word.text.as_str(), word.rect);
dbg!(&word.chars);

let text = &word.text;
let mut offset = 0;
let mut chars = word.chars.iter().peekable();
let mut texts = vec![];

while let Some(_) = chars.next() {
// Get text for current char
let s = if let Some(next) = chars.peek() {
let s = &text[offset..next.offset];
offset = next.offset;
s
} else {
&text[offset..]
};

texts.push(s);
}
}
}
println!();
}
}
// }
}
81 changes: 81 additions & 0 deletions src/classify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::sync::Arc;

use font::Encoder;
use pdf_render::TextSpan;

use crate::util::is_number;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Class {
Number,
Header,
Paragraph,
Mixed,
}

pub fn classify<'a, E: Encoder + 'a>(spans: impl Iterator<Item=&'a TextSpan<E>>) -> Class {
use pdf_render::FontEntry;

let mut bold = TriCount::new();
let mut numeric = TriCount::new();
let mut uniform = TriCount::new();
let mut first_font: *const FontEntry<E> = std::ptr::null();

for s in spans {
numeric.add(is_number(&s.text));
if let Some(ref font) = s.font {
bold.add(font.name.contains("Bold"));
let font_ptr = Arc::as_ptr(font);
if first_font.is_null() {
first_font = font_ptr;
} else {
uniform.add(font_ptr == first_font);
}
}
}
uniform.add(true);

match (numeric.count(), bold.count(), uniform.count()) {
(Tri::True, _, Tri::True) => Class::Number,
(_, Tri::True, Tri::True) => Class::Header,
(_, Tri::False, Tri::True) => Class::Paragraph,
(_, Tri::False, _) => Class::Paragraph,
(_, Tri::Maybe(_), _) => Class::Paragraph,
_ => Class::Mixed
}
}

pub enum Tri {
False,
True,
Maybe(f32),
Unknown,
}

#[derive(Debug)]
pub struct TriCount {
tru: usize,
fal: usize,
}
impl TriCount {
fn new() -> Self {
TriCount {
tru: 0,
fal: 0
}
}
fn add(&mut self, b: bool) {
match b {
false => self.fal += 1,
true => self.tru += 1,
}
}
fn count(&self) -> Tri {
match (self.fal, self.tru) {
(0, 0) => Tri::Unknown,
(0, _) => Tri::True,
(_, 0) => Tri::False,
(f, t) => Tri::Maybe(t as f32 / (t + f) as f32)
}
}
}
52 changes: 0 additions & 52 deletions src/entry.rs

This file was deleted.

Loading