Skip to content

Say that no font is set instead of failing on null in documents created with font: null - #1867

Open
alphaNumericEntity wants to merge 1 commit into
foliojs:masterfrom
alphaNumericEntity:no-font-error
Open

alphaNumericEntity wants to merge 1 commit into
foliojs:masterfrom
alphaNumericEntity:no-font-error

Conversation

@alphaNumericEntity

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix, fixes #1647.

The docs suggest new PDFDocument({ font: null }) for a document that shouldn't load Helvetica, for example in the browser build where standard fonts have to be registered first. Until a font is selected, though, almost everything text-related fails with a TypeError that doesn't say what's wrong:

const doc = new PDFDocument({ font: null });
doc.moveDown(); // TypeError: Cannot read properties of null (reading 'lineHeight')

The same happens with moveUp(), currentLineHeight(), widthOfString(), heightOfString(), boundsOfString(), text() (wrapped or not), list() and table(). That's the follow-up in #1647.

Changes:

  • A _requireFont() helper in the fonts mixin throws No font is set. Select one with doc.font(), or pass the font option when creating the document. It's called where the current font is first read: currentLineHeight(), widthOfString(), list() and _fragment() (for text() with lineBreak: false, which doesn't go through the line wrapper). Every other path reaches one of these first. Each call sits where the font was already read, so only the error message changes; nothing that worked before throws now.
  • getting_started.md, under "Setting default font", now shows the cleaner answer to the original request: new PDFDocument({ font: fontData }) with a Uint8Array, an ArrayBuffer or (in Node) a path starts the document with an embedded font and never loads a standard font. It also says what font: null requires. The browser section links to it.

I also checked the original Deno report. With Deno 2.9.6, a deno compiled binary runs new PDFDocument() fine with both 0.17.0 and 0.20.2, so the failure that remains is the font: null one above.

Not changed: on a font: null document, a table whose cells set their own font still fails, with Not a supported font format or standard PDF font., because the table code restores the previous font with doc.font(null, ...) after measuring (lib/table/normalize.js:177, and the same pattern in size.js and render.js). It fails the same way on master. I left it alone because changing what restoring "no font" means seemed like a separate decision.

Checklist:

  • Unit Tests
  • Documentation
  • Update CHANGELOG.md
  • Ready to be merged

yarn test (664 passed), yarn lint, yarn prettier and yarn test:package all pass. The new tests fail on master with the TypeError.

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.

Support embedded-only fonts - avoid loading defaults during initialization

1 participant