Skip to content

[Bug]: Text layer improperly applies word spacing, leading to misaligned text selection & highlights #20319

Closed

Description

@rossj

Attach (recommended) or Link to PDF file

bad word spacing - decompressed.pdf

It seems that text selection / highlighting improperly applies "word spacing," e.g. after a non-0x20 CID that is just mapped to U+0020 via a ToUnicode CMap. This causes a discrepancy in the actual rendered text and a glyph's highlight / selection box.

In more detail:

Section "9.3.3 Word spacing" of the PDF spec says: "Word spacing shall be applied to every occurrence of the single-byte character code 32 in a string when using a simple font (including Type 3) or a composite font that defines code 32 as a single-byte code. It shall not apply to occurrences of the byte value 32 in multiple-byte codes."

This behavior appears to be followed by PDF.js's text rendering / layout operations, but not by the text highlight / selection layer.

See attached sample file and screenshots, which shows a Type0 font and a 3-character Tj op ("A<SPACE>A"), with a set word spacing of 50 units. The word spacing is not applied in the rendering, but is applied in the highlight boxes.

Web browser and its version

Firefox 143.0.1

Operating system and its version

Windows 11

PDF.js version

Latest / demo

Is the bug present in the latest PDF.js version?

Yes

Is a browser extension

No

Steps to reproduce the problem

  1. Open the attached sample PDF with Pdf.js
  2. Attempt to select the "A A" text line
  3. Observe that the left "A" highlights normally, but that the 2nd "A" does not select until you move the cursor a few character widths further right than expected.
Image

What is the expected behavior?

It is expected that the highlight box for the right "A" is also aligned with the visual "A"

What went wrong?

The layout calculation for the hidden text layer incorrectly added the word spacing after the 0x0003 CID, which is only mapped to U+0020 via the ToUnicode CMap.

Link to a viewer

No response

Additional context

Note that as of now, PDF.js's misaligned highlight does seem to match Acrobat's behavior. I have attempted to file this same issue with Adobe: https://acrobat.uservoice.com/forums/590923-acrobat-for-windows-and-mac/suggestions/50527812-text-selection-highlight-improperly-applying-wor

Activity

  1. rossj commented on Sep 29, 2025

    @rossj
    ContributorAuthor

    Commenting to add an additional sample file. This one demonstrates the inverse word spacing issue - the word spacing is correctly added in the rendering canvas but is not applied in the text layer. This sample file has code 0x20 mapped to \A in a Type3 font, which should still invoke word spacing despite not being a space.

    bad word spacing - single byte font with 0x20 = A.pdf

    Image
  2. self-assigned this
    on Oct 3, 2025
  3. added a commit that references this issue on Oct 3, 2025
    4d15bfe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions