Skip to content

Commit 933e4ad

Browse files
author
vidy
committed
Support font encoder
1 parent 03d4789 commit 933e4ad

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ description = "PDF text extraction"
99

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

12+
13+
[dependencies.pdf]
14+
default-features=false
15+
features = ["cache", "dump"]
16+
git = "https://github.com/pdf-rs/pdf"
17+
1218
[dependencies]
13-
pdf = { git = "https://github.com/pdf-rs/pdf", features = ["cache"] }
14-
pdf_render = { git = "https://github.com/pdf-rs/pdf_render" }
15-
font = { git = "https://github.com/pdf-rs/font" }
19+
pdf_render= { git = "https://github.com/videni/pdf_render_with_vello", branch="vello_wip"}
1620
itertools = "*"
1721
log = "*"
1822
ordered-float = "*"
1923
serde = { version = "*", features = ["derive"] }
2024
unicode-normalization = "0.1.19"
25+
font = { git = "https://github.com/pdf-rs/font", branch = "vello", features=['cff']}
2126

2227
pathfinder_geometry = { git = "https://github.com/servo/pathfinder" }
2328
pathfinder_color = { git = "https://github.com/servo/pathfinder" }

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use std::collections::HashSet;
22

33
use entry::Flow;
44
use pdf::{backend::Backend, object::{Page, Resolve}, PdfError};
5-
use pdf_render::{tracer::{TraceCache, Tracer, DrawItem}, Fill, render_pattern, render_page, FillMode};
5+
use pdf_render::{tracer::{TraceCache, Tracer, DrawItem}, Fill, render_pattern, render_page, FillMode, font::OutlineBuilder};
66

77
mod tree;
88
mod util;
99
mod text;
1010
pub mod entry;
1111

1212
pub fn run<B: Backend>(file: &pdf::file::CachedFile<B>, page: &Page, resolve: &impl Resolve) -> Result<Flow, PdfError> {
13-
let cache = TraceCache::new();
13+
let mut cache = TraceCache::new(OutlineBuilder::default());
1414

1515
let mut clip_paths = vec![];
16-
let mut tracer = Tracer::new(&cache, &mut clip_paths);
16+
let mut tracer = Tracer::new(&mut cache, &mut clip_paths);
1717

1818
render_page(&mut tracer, resolve, &page, Default::default())?;
1919

@@ -68,7 +68,7 @@ pub fn run<B: Backend>(file: &pdf::file::CachedFile<B>, page: &Page, resolve: &i
6868
continue;
6969
}
7070
};
71-
let mut pat_tracer = Tracer::new(&cache, &mut clip_paths);
71+
let mut pat_tracer = Tracer::new(&mut cache, &mut clip_paths);
7272

7373
render_pattern(&mut pat_tracer, &*pattern, resolve)?;
7474
let pat_items = pat_tracer.finish();

src/text.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use font::Encoder;
12
use pathfinder_geometry::vector::Vector2F;
23
use pdf_render::TextSpan;
34
use itertools::{Itertools};
45
use unicode_normalization::UnicodeNormalization;
56
use crate::{util::avg, entry::Word, util::Rect};
67

7-
pub fn concat_text<'a>(out: &mut String, items: impl Iterator<Item=&'a TextSpan> + Clone) -> Vec<Word> {
8+
pub fn concat_text<'a, E: Encoder + 'a>(out: &mut String, items: impl Iterator<Item=&'a TextSpan<E>> + Clone) -> Vec<Word> {
89
let mut words = vec![];
910
let gaps = items.clone()
1011
.flat_map(|s| {

src/tree.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use crate::util::{is_number, avg, CellContent};
1616
use crate::text::{concat_text};
1717
use std::mem::take;
1818
use table::Table;
19+
use font::{Encoder, Glyph};
1920

20-
pub fn build(spans: &[TextSpan], bbox: RectF, lines: &[[f32; 4]]) -> Node {
21+
pub fn build<E: Encoder>(spans: &[TextSpan<E>], bbox: RectF, lines: &[[f32; 4]]) -> Node {
2122
if spans.len() == 0 {
2223
return Node::singleton(&[]);
2324
}
@@ -180,7 +181,7 @@ impl Span {
180181
}
181182
}
182183

183-
pub fn split2(boxes: &mut [(RectF, usize)], spans: &[TextSpan], lines_info: &Lines) -> Node {
184+
pub fn split2<E: Encoder>(boxes: &mut [(RectF, usize)], spans: &[TextSpan<E>], lines_info: &Lines) -> Node {
184185
use std::mem::replace;
185186

186187
#[derive(Debug)]
@@ -383,7 +384,7 @@ pub enum NodeTag {
383384
Complex,
384385
}
385386

386-
pub fn items(mut flow: &mut Flow, spans: &[TextSpan], node: &Node, x_anchor: f32) {
387+
pub fn items<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node: &Node, x_anchor: f32) {
387388
match *node {
388389
Node::Final { ref indices } => {
389390
if indices.len() > 0 {
@@ -534,10 +535,10 @@ pub fn items(mut flow: &mut Flow, spans: &[TextSpan], node: &Node, x_anchor: f32
534535
}
535536

536537

537-
pub fn render(w: &mut String, spans: &[TextSpan], node: &Node, bbox: RectF) {
538+
pub fn render<E: Encoder>(w: &mut String, spans: &[TextSpan<E>], node: &Node, bbox: RectF) {
538539
_render(w, spans, node, bbox, 0)
539540
}
540-
fn _render(w: &mut String, spans: &[TextSpan], node: &Node, bbox: RectF, level: usize) {
541+
fn _render<E: Encoder>(w: &mut String, spans: &[TextSpan<E>], node: &Node, bbox: RectF, level: usize) {
541542
use std::fmt::Write;
542543

543544
match *node {
@@ -596,7 +597,7 @@ fn _render(w: &mut String, spans: &[TextSpan], node: &Node, bbox: RectF, level:
596597
}
597598
}
598599

599-
fn split(boxes: &mut [(RectF, usize)], spans: &[TextSpan], lines: &Lines) -> Node {
600+
fn split<E: Encoder>(boxes: &mut [(RectF, usize)], spans: &[TextSpan<E>], lines: &Lines) -> Node {
600601
let num_boxes = boxes.len();
601602
if num_boxes < 2 {
602603
return Node::singleton(boxes);
@@ -925,13 +926,13 @@ impl TriCount {
925926
}
926927
}
927928
}
928-
fn classify<'a>(spans: impl Iterator<Item=&'a TextSpan>) -> Class {
929+
fn classify<'a, E: Encoder + 'a>(spans: impl Iterator<Item=&'a TextSpan<E>>) -> Class {
929930
use pdf_render::FontEntry;
930931

931932
let mut bold = TriCount::new();
932933
let mut numeric = TriCount::new();
933934
let mut uniform = TriCount::new();
934-
let mut first_font: *const FontEntry = std::ptr::null();
935+
let mut first_font: *const FontEntry<E> = std::ptr::null();
935936

936937
for s in spans {
937938
numeric.add(is_number(&s.text));

0 commit comments

Comments
 (0)