Skip to content

Use one output byte mapping for glyph lookup and text encoding - #1834

Open
tlq5l wants to merge 2 commits into
foliojs:masterfrom
tlq5l:fix/standard-symbol-zapf-widths
Open

tlq5l wants to merge 2 commits into
foliojs:masterfrom
tlq5l:fix/standard-symbol-zapf-widths

Conversation

@tlq5l

@tlq5l tlq5l commented Oct 30, 2026

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix. Fixes #580.

widthOfString() returns 0 for the letters of Symbol (such as A) and for almost every ZapfDingbats character (such as ! and the euro sign), so wrapping and alignment are wrong with them. AFMFont#characterToGlyph (lib/font/afm.js:178) looks every character up in the Roman WinAnsi name table (A becomes A). These two fonts use their own built-in encoding, marked EncodingScheme FontSpecific (lib/font/data/Symbol.afm:18, lib/font/data/ZapfDingbats.afm:18), where code 65 is Alpha (lib/font/data/Symbol.afm:55), not A. So the lookup misses their glyphs.

The converter (tools/afm-converter.js) now reads the character code of each glyph. For FontSpecific fonts it keeps every glyph with a code from 0 to 255 (tools/afm-converter.js:125-129) and writes a glyphCodes array next to glyphNames (tools/afm-converter.js:173). AFMFont uses that map in characterToGlyph when it is present (lib/font/afm.js:181-183), after the same byte mapping encodeText() uses (outputCode, lib/font/afm.js:37), so one output byte has one width. The Roman fonts' generated files are unchanged. Symbol.js and ZapfDingbats.js were regenerated as tools/README.md describes.

Checklist:

  • Unit Tests
  • Documentation (tools/README.md schema)
  • Update CHANGELOG.md
  • Ready to be merged

Ran live on c39a34d: built the branch and ran d.font(f).widthOfString('ABGDEZHQIKLMNXOPRSTUFCYW') -> Symbol 195.276, ZapfDingbats 228.36, Helvetica 197.352 (Symbol and ZapfDingbats were 0 on master); the ZapfDingbats euro sign and \u0080, both written as byte 80, both measure 4.68 at the default size.

Callers checked: lib/font/afm.js, lib/font/standard.js, tools/afm-converter.js, tests/tools/afm-converter.spec.js, tests/unit/afm.spec.js

AI disclosure: AI-assisted writing and checking, including automated tests and live checks; I take responsibility for it.

tlq5l added 2 commits October 30, 2026 12:49
The built-in encoding lookup used the original character code while
encodeText() maps some characters to a different byte, so U+20AC and
U+0080 both wrote byte 80 but measured differently in ZapfDingbats.

Agent: claude

This branch has not been deployed

No deployments
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.

Buggy widthOfString with "Symbol" & "ZapfDingbats" fonts

1 participant