Skip to content

Commit

Permalink
Handle missing Widths by substituting a font
Browse files Browse the repository at this point in the history
We could do a better job but for now using Helvatica
is better than crashing.

Fixes #94
  • Loading branch information
jrmuizel committed Dec 27, 2024
1 parent 92ce1c8 commit 701728c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,28 @@ impl<'a> PdfSimpleFont<'a> {
i += 1;
}
assert_eq!(first_char + i - 1, last_char);
} else if is_core_font(&base_name) {
} else {
let name = if is_core_font(&base_name) {
&base_name
} else {
println!("no widths and not core font {:?}", base_name);

// This situation is handled differently by different readers
// but basically we try to substitute the best font that we can.

// Poppler/Xpdf:
// this is technically an error -- the Widths entry is required
// for all but the Base-14 fonts -- but certain PDF generators
// apparently don't include widths for Arial and TimesNewRoman

// Pdfium: CFX_FontMapper::FindSubstFont

// mupdf: pdf_load_substitute_font

// We can try to do a better job guessing at a font by looking at the flags
// or the basename but for now we'll just use Helvetica
"Helvetica"
};
for font_metrics in core_fonts::metrics().iter() {
if font_metrics.0 == base_name {
if let Some(ref encoding) = encoding_table {
Expand Down Expand Up @@ -590,8 +611,6 @@ impl<'a> PdfSimpleFont<'a> {
// assert!(maybe_get_obj(doc, font, b"Widths").is_none());
}
}
} else {
panic!("no widths and not core font {:?}", base_name);
}

let missing_width = get::<Option<f64>>(doc, font, b"MissingWidth").unwrap_or(0.);
Expand Down
1 change: 1 addition & 0 deletions tests/docs/missing-widths.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/user-attachments/files/16678923/0000988.pdf

0 comments on commit 701728c

Please sign in to comment.