Skip to content

feat: accept pre-parsed fontkit Font instances in doc.font() - #1776

Merged
blikblum merged 1 commit into
masterfrom
diegomura/pdfkit-font-object-support
Aug 23, 2026
Merged

feat: accept pre-parsed fontkit Font instances in doc.font()#1776
blikblum merged 1 commit into
masterfrom
diegomura/pdfkit-font-object-support

Conversation

@diegomura

@diegomura diegomura commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

What

PDFFontFactory.open now accepts an already-parsed fontkit Font instance (duck-typed via src.layout) in addition to string paths, Uint8Array, and ArrayBuffer. Unsupported objects still hit the existing "Not a supported font format" error.

Why

react-pdf maintains a fork of pdfkit (@react-pdf/pdfkit) and is converging back onto upstream. Nearly all fork deltas have been upstreamed already; this is one of the last functional gaps. react-pdf does its own text layout with fontkit and passes the parsed Font instance to doc.font() so pdfkit doesn't re-parse bytes it already has in memory.

The existing cache machinery in lib/mixins/fonts.js (null cacheKey for non-string sources, isEqualFont dedup) already handles these sources — no changes needed there; tests cover that path.

Tests

  • doc.font(fontInstance) embeds the font and renders text
  • The same instance passed twice reuses the cached EmbeddedFont
  • Two different instances don't collide
  • registerFont('MyFont', fontInstance) + doc.font('MyFont') works
  • Plain objects still throw the existing error (pre-existing test)

@diegomura
diegomura force-pushed the diegomura/pdfkit-font-object-support branch from c1e4003 to 7a7fd23 Compare August 23, 2026 11:42
@diegomura
diegomura force-pushed the diegomura/pdfkit-font-object-support branch from 7a7fd23 to 9733b3a Compare August 23, 2026 11:46
@diegomura diegomura changed the title Accept pre-parsed fontkit Font instances in doc.font() feat: accept pre-parsed fontkit Font instances in doc.font() Aug 23, 2026
@diegomura
diegomura requested review from blikblum and devongovett and a lite review from Copilot August 23, 2026 11:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends PDFKit’s font-loading pipeline so doc.font() / registerFont() can accept an already-parsed fontkit Font instance (in addition to file paths and raw byte sources), avoiding redundant parsing when callers already use fontkit for layout (e.g., react-pdf).

Changes:

  • Update PDFFontFactory.open() to accept pre-parsed fontkit Font instances (duck-typed via layout()).
  • Add unit tests covering embedding, caching behavior, non-collision, and registerFont() with parsed instances.
  • Document the new supported input type and note it in the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/unit/font.spec.js Adds tests for passing parsed fontkit Font instances into doc.font() and registerFont().
lib/font_factory.js Accepts pre-parsed fontkit Font objects in PDFFontFactory.open().
docs/text.md Documents support for fontkit Font instances as font() inputs.
CHANGELOG.md Notes the new support in the Unreleased section.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/font_factory.js
Comment thread tests/unit/font.spec.js
Comment on lines +75 to +78
const pdf = collectPdf(doc);
expect(missingObjects(pdf)).toHaveLength(0);
});
});
Comment thread docs/text.md
@diegomura
diegomura force-pushed the diegomura/pdfkit-font-object-support branch from 8bd4daa to 9733b3a Compare August 23, 2026 12:35
@blikblum
blikblum merged commit f0863a6 into master Aug 23, 2026
1 of 7 checks passed
@blikblum
blikblum deleted the diegomura/pdfkit-font-object-support branch August 23, 2026 13:29
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.

3 participants