Skip to content

Bugfix: Handle TypedArrays and Buffers in deep-merge/clone#1744

Open
ykw9263 wants to merge 13 commits into
foliojs:masterfrom
ykw9263:bugfix/table-deep-merged-typedarray
Open

Bugfix: Handle TypedArrays and Buffers in deep-merge/clone#1744
ykw9263 wants to merge 13 commits into
foliojs:masterfrom
ykw9263:bugfix/table-deep-merged-typedarray

Conversation

@ykw9263

@ykw9263 ykw9263 commented Jun 25, 2026

Copy link
Copy Markdown

What kind of change does this PR introduce?

This fixes #1743 where providing font buffers crashes table generation.

In lib\table\utils.js, deepMerge and deepClone() copy properties key by key including Buffer and Uint8Array without inheriting the class. This causes the merged font buffer failing to be recognised by PDFFontFactory which relies on instanceof to determine the type of font sources.

This PR changes:

  • deepMerge() and deepClone() now clone TypedArrays and Buffers using .slice() and Buffer.from(buffer) respectively.

  • Table style merging is modified to copy fonts by reference.

Additional unit tests (fonts in tables, deep-merging buffers) are added for the changes.

Checklist:

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

TODO:

@ykw9263 ykw9263 marked this pull request as draft June 25, 2026 17:33
Comment thread lib/table/utils.js Outdated
Comment thread lib/table/utils.js Outdated
@ykw9263 ykw9263 marked this pull request as ready for review June 27, 2026 19:25

@blikblum blikblum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While the font handling is not overhauled this seems a good workaround

Comment thread lib/table/utils.js Outdated
Comment thread lib/table/utils.js Outdated
ykw9263 and others added 3 commits June 28, 2026 13:49
- remove unnecessary isObject check
- rename binary data check function

Co-authored-by: Luiz Américo <camara_luiz@yahoo.com.br>
- font family name is not allowed for ttf fonts with no Variations
Comment thread lib/table/style.js
Comment on lines +126 to +132
// extract fonts
const { font: defaultFont, ...restDefaultStyle } = defaultColStyle || {};
const { font: font, ...restStyle } = colStyle || {};
const mergedFont = {
...defaultFont,
...font,
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that deepMerge was modified to handle binary data, why not use it?

@ykw9263 ykw9263 Jun 28, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Just found a bug where merging fonts with font collections would cause problem

  doc.table({
    rowStyles: [{
        font: { src: SOME_TTC_FONT, family: FONT_FAMILY },
    }]
  })
  .row([
    { text: 'Hello World', font: { src: SOME_TTF_FONT } }
  ]);
// throws `Variations require a font with the fvar, gvar and glyf, or CFF2 tables`

I think font src should be binded with its family and not be merged with other family names across styles

@blikblum

Copy link
Copy Markdown
Member

There is a minor changelog conflict

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.

PDFKit crashes when providing fonts as Buffer in table

2 participants