pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Fix character spacing in fonts using ligatures

Open phipla opened this issue 2 years ago • 2 comments

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

Screenshot 2022-10-14 at 12 03 01

After

Screenshot 2022-10-14 at 12 04 14

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.

phipla avatar Oct 14 '22 10:10 phipla

Please merge this PR.

handihow avatar Feb 13 '23 09:02 handihow

Same issue happened in Gujrati & Hindi Fonts.

pratik2575 avatar May 08 '23 06:05 pratik2575