fix: handle postscriptName as Uint8Array in EmbeddedFont - #1753
Open
pratik-desgn wants to merge 1 commit into
Open
fix: handle postscriptName as Uint8Array in EmbeddedFont#1753pratik-desgn wants to merge 1 commit into
pratik-desgn wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1687.
fontkit'sreadString()returns a rawUint8Arrayinstead of a string when the runtime'sTextDecoderdoesn't support the font's encoding (e.g.x-mac-roman,utf-16be). This happens on Hermes (React Native), which only supportsutf-8/utf-16le.EmbeddedFontassumedpostscriptNameis always a string and called.replaceAll(' ', '_')directly on it, soembed()crashed withTypeError: replaceAll is not a functionon any font whose postscript name needed that fallback decoding path.Fix
Added
postscriptNameToString()(handlesstring,Uint8Array, and nullish input) and used it everywherepostscriptNamewas read: both where it's stored asthis.namein the constructor, and where it's used to build the embedded font'sBaseFont/FontNametag inembed().Test plan
postscriptNamegetter to return aUint8Array, then callsembed()and asserts it doesn't throw and produces the sameBaseFonttag as the string path. Confirmed it fails with the exact reported error on the old code, and passes with the fix.eslintclean.