Skip to content

Fonts: the ToUnicode CMap uses the platform newline, so the same document saves to different bytes on Windows and Linux #630

Closed

Description

@Tim81

What happens

TrueTypeFontEmbedder builds the ToUnicode CMap with StringBuilder.AppendLine (src/VellumPdf.Kernel/Fonts/TrueTypeFontEmbedder.cs, lines 519-566 on main; the same code is in v2.3.3). AppendLine ends each line with Environment.NewLine, so the CMap text uses CR LF when the document is saved on Windows and LF when it is saved on Linux or macOS.

The same document therefore produces different bytes depending on the operating system that saves it. Measured while fixing a test in #629: for DejaVu Sans drawing four BMP characters, the ToUnicode stream is 398 bytes inflated (255 compressed) on Windows and 380 bytes inflated (248 compressed) under Ubuntu 24.04. Every other stream in that file was byte-equal after inflating. The decoded CMap text differs only in CR LF against LF.

Both forms are valid: a CMap is PostScript text, and an end-of-line marker may be CR LF or LF. What breaks is reproducibility: a document built with a pinned Timestamp and document ID is not byte-identical across platforms, and any test that pins the bytes of a file with an embedded font has to normalise line endings first (#629 does this for one test).

Scope of the search

git grep -nE "AppendLine\(|Environment\.NewLine|WriteLine\(" origin/main -- 'src/*.cs' finds these 15 AppendLine calls in TrueTypeFontEmbedder.cs and, outside it, only console output in VellumPdf.Cli. No other writer of PDF bytes in src/ uses the platform newline.

Proposed fix

Write the CMap lines with an explicit \n, as the rest of the writer does. That changes the bytes Windows writes for every document with an embedded TrueType or OpenType font (CR LF becomes LF in the ToUnicode stream), so it is a deliberate output change to list in the CHANGELOG. Pin it with a test that inflates the ToUnicode stream and asserts it contains no CR byte, and drop the line-ending normalisation from the #629 test once the fix lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/fontsbug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions