Skip to content

fix: handle postscriptName as Uint8Array in EmbeddedFont - #1753

Open
pratik-desgn wants to merge 1 commit into
foliojs:masterfrom
pratik-desgn:fix-postscript-name-uint8array
Open

fix: handle postscriptName as Uint8Array in EmbeddedFont#1753
pratik-desgn wants to merge 1 commit into
foliojs:masterfrom
pratik-desgn:fix-postscript-name-uint8array

Conversation

@pratik-desgn

Copy link
Copy Markdown

Summary

Fixes #1687. fontkit's readString() returns a raw Uint8Array instead of a string when the runtime's TextDecoder doesn't support the font's encoding (e.g. x-mac-roman, utf-16be). This happens on Hermes (React Native), which only supports utf-8/utf-16le. EmbeddedFont assumed postscriptName is always a string and called .replaceAll(' ', '_') directly on it, so embed() crashed with TypeError: replaceAll is not a function on any font whose postscript name needed that fallback decoding path.

Fix

Added postscriptNameToString() (handles string, Uint8Array, and nullish input) and used it everywhere postscriptName was read: both where it's stored as this.name in the constructor, and where it's used to build the embedded font's BaseFont/FontName tag in embed().

Test plan

  • Added a regression test that simulates the Hermes scenario by overriding a real font's postscriptName getter to return a Uint8Array, then calls embed() and asserts it doesn't throw and produces the same BaseFont tag as the string path. Confirmed it fails with the exact reported error on the old code, and passes with the fix.
  • Full test suite passes (377/377, unit + visual).
  • eslint clean.

fontkit's readString() returns a raw Uint8Array instead of a string
when the runtime's TextDecoder doesn't support the font's encoding
(e.g. x-mac-roman, utf-16be). This happens on Hermes (React Native),
which only supports utf-8/utf-16le, and crashed embed() with
"replaceAll is not a function" since postscriptName was assumed to
always be a string.

Added postscriptNameToString() to normalize string/Uint8Array/nullish
postscriptName values before use, applied both where it's stored as
this.name and where it's used to build the embedded font's BaseFont
tag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EmbeddedFont#embed crashes when postscriptName is a Uint8Array (Hermes / React Native)

1 participant