fschutt / printpdf

An easy-to-use library for writing PDF in Rust
https://docs.rs/printpdf/
MIT License
777 stars 95 forks source link

Custom fonts have really large text boxes when selecting #108

Open cyphar opened 2 years ago

cyphar commented 2 years ago

When creating a PDF using printpdf, I've noticed that with custom fonts (specifically Roboto Slab and B612 Mono), when you try to select text in a PDF viewer the selection will be much taller than the text and will overlap adjacent lines (rendering them invisible). If I use the built-in courier fonts with exactly the same text settings, the problem goes away. Here's a screenshot of the issue in Evince:

2021-12-23-094214_561x794_scrot

This is most notable when opening the PDF in Evince. In Firefox and Chrome the selected text is the right height. You can get a copy of an example PDF with this problem here.

fschutt commented 2 years ago

The "problem" with those fonts is that the unitsPerEm field is set to 2000 instead of the default 1000 and Evince is the only PDF viewer that doesn't respect that.

image

I can work around it by dividing the ascender / descender by 2.0, but then the text appears "spaced out", because the glyph widths still expect 2000 units per em:

image

I also need to debug why the ascender / descender doesn't use the sTypoMetrics field as it should. The fix is to scale the ascender / descender AND the glyph widths to 1000em and also to submit a bug report to Evince.

seijikun commented 1 year ago

Same problem with Okular. I think both depend on Poppler for pdf parsing/rendering, so if that's really a viewer bug, it might be located there. I opened a bug for Okular: https://bugs.kde.org/show_bug.cgi?id=470091