pdf-lib
pdf-lib copied to clipboard
Fix character spacing in fonts using ligatures
What?
Fixes glyph spacing being wrong when fonts use ligatures. This problem was mentioned in issue #440, #871, #1010.
Why?
The suggested workaround was to disable font ligatures, but ligatures are a desirable feature of many fonts, especially on some non-latin scripts, or on fonts that simulate or are inspired by cursive handwriting.
How?
The problem is in PDF-LIB: the code gets the glyph metric this way:
- get all codepoints
- for each codepoint, get the glyph for this codepoint
- extract the metrics for this glyph.
When fonts have ligatures, there might be several glyphs for a single code point, therefore the glyph metric code does not work and misses some glyphs.
This fix just gets the metrics for all glyphs in the font and does not use glyphForCodePoint
Testing?
The problem is pretty easy to spot on the rendered PDF when using a font using ligatures, like Lobster. Integration test 1, which tests font rendering, was used to check that the issue was fixed.
New Dependencies?
None
Screenshots
Before

After

Suggested Reading?
Yes
Anything Else?
No
Checklist
- [x] I read CONTRIBUTING.md.
- [x] I read MAINTAINERSHIP.md#pull-requests.
- [x] I added/updated unit tests for my changes.
- [x] I added/updated integration tests for my changes.
- [x] I ran the integration tests.
- [x] I tested my changes in Node, Deno, and the browser.
- [x] I viewed documents produced with my changes in Adobe Acrobat, Foxit Reader, Firefox, and Chrome.
- [x] I added/updated doc comments for any new/modified public APIs.
- [x] My changes work for both new and existing PDF files.
- [x] I ran the linter on my changes.
Please merge this PR.
Same issue happened in Gujrati & Hindi Fonts.