Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Replace the canvas package with @napi-rs/canvas #19015

Merged
merged 2 commits into from
Nov 10, 2024

Conversation

Snuffleupagus
Copy link
Collaborator

The @napi-rs/canvas package has fewer dependencies, which should hopefully make installing and using it easier for pdfjs-dist end-users. (Over the years we've seen, repeatedly, that canvas can be difficult to install successfully.)
Furthermore, this package includes more functionality (such as Path2D) which reduces the overall number of dependencies in the PDF.js project.

One point to note is that @napi-rs/canvas is a fair bit newer than canvas, and has a lot fewer users, however looking at the commit history it does seem to be actively maintained.

Note that I've successfully tested the Node.js examples, in particular the pdf2png one, with this patch applied and things appear to work fine.

Please see:

@Snuffleupagus
Copy link
Collaborator Author

Before moving forward with this, the following points should probably be agreed upon:

  • Do we want to replace the canvas dependency?
  • Do we think that @napi-rs/canvas is a reasonable choice?
  • Test this sufficiently on "all" platforms (my testing has been on Windows).

@Snuffleupagus
Copy link
Collaborator Author

/botio test

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_test from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.193.163.58:8877/daf83370e7d6cfc/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_test from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/7c80a5eca86a72b/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/7c80a5eca86a72b/output.txt

Total script time: 30.85 mins

  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 15

Image differences available at: http://54.241.84.105:8877/7c80a5eca86a72b/reftest-analyzer.html#web=eq.log

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/daf83370e7d6cfc/output.txt

Total script time: 51.54 mins

  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: Passed

@calixteman
Copy link
Contributor

calixteman commented Nov 9, 2024

I tested the patch on mac with the example and it works fine.

The `@napi-rs/canvas` package has fewer dependencies, which should *hopefully* make installing and using it easier for `pdfjs-dist` end-users. (Over the years we've seen, repeatedly, that `canvas` can be difficult to install successfully.)
Furthermore, this package includes more functionality (such as `Path2D`) which reduces the overall number of dependencies in the PDF.js project.

One point to note is that `@napi-rs/canvas` is a fair bit newer than `canvas`, and has a lot fewer users, however looking at the commit history it does seem to be actively maintained.

Note that I've successfully tested the [Node.js examples](https://github.com/mozilla/pdf.js/tree/master/examples/node), in particular the `pdf2png` one, with this patch applied and things appear to work fine.

Please see:
 - https://www.npmjs.com/package/@napi-rs/canvas
 - https://github.com/Brooooooklyn/canvas
Given that `ImageData` has been supported for many years in all browsers, see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/ImageData#browser_compatibility), we have a `typeof` check that's only necessary in Node.js environments.
Since the `@napi-rs/canvas` package provides that functionality, we can thus add an `ImageData` polyfill which allows us to ever so slightly simplify the code.
@timvandermeij
Copy link
Contributor

timvandermeij commented Nov 10, 2024

Do we want to replace the canvas dependency?

I think we should, primarily given the number of installation issues we've seen and the lack of active development, both visible at https://github.com/Automattic/node-canvas too.

Do we think that @napi-rs/canvas is a reasonable choice?

I think so. It's under active development, it has a good amount of support/stars from the community, it's written in a memory-safe language (Rust) and it has no other dependencies which should make it easier to install for end users. Doing so also doesn't require many changes as shown in this patch, and it reduces the number of dependencies ever so slightly. The only drawbacks I can imagine are if features that we rely on are missing, and that it's a relatively new project which means that the future could be a bit more uncertain compared to more "settled" packages (but we can always switch later on, so I don't see that as a blocker here).

@Snuffleupagus Snuffleupagus marked this pull request as ready for review November 10, 2024 17:47
Copy link
Contributor

@calixteman calixteman left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants