Automattic / node-canvas

Node canvas is a Cairo backed Canvas implementation for NodeJS.
10.1k stars 1.16k forks source link

It should be possible to use the same font rendering backend between OSes #2270

Open slalomzacharyd opened 1 year ago

slalomzacharyd commented 1 year ago

If you try to use the library for unit testing, it generates slightly different text between OSs. It would be great to be able to use this library to generate images for use in testing where consistency is important.

Steps to Reproduce

  1. Render some text using a font like Noto Sans or Roboto on Mac OS and output it to a PNG.
  2. Render the same text using the same font using Ubuntu and output it to a PNG.
  3. Notice the text is rendered differently.
Screenshot 2023-07-14 at 15 34 43
chearon commented 1 year ago

Browsers aren't consistent with each other or even with themselves across OSes. Which browser and OS should we choose to emulate? Even if there were an answer I doubt Cairo can be made to do that.

slalomzacharyd commented 1 year ago

There are holes you can poke into my request, but to do that I need to make sure what I am requesting is clear so the holes make sense.

A look at Cairo

Through MacPorts, it looks like you can compile with different backends for Cairo. (You might even be able to compile in multiple backends.) By default Cairo will try to use the native backend (GDI for Window/CoreText for macOS/FC for linux) which may render slightly differently from each other

Screenshot 2023-07-15 at 13 18 00

Notice specifically that Cairo on macOS supports X11 as a backend. Theoretically, if on macOS I was using the X11 backend I would get the same output as my Linux box. It is possible with pango cairo to choose a backend using the env variable PANGOCAIRO_BACKEND.

Screenshot 2023-07-15 at 13 21 09

It also appears that cairo on brew also compiles in freetype and fontconfig.

The Question

So my question is: since node-canvas is not likely to be used in a browser, why can't we use an ENV variable to choose a backend similar to Pango Cairo? You would think that consistent rendering for a library like this might be important for some apps.

The possible counter arguments

slalomzacharyd commented 1 year ago

Please don't try to bring the request off the rails by asking about emulation or browsers. Neither of those are in the scope of this issue.