Automattic / Automattic/node-canvas

Sometimes registerFont() does not work correctly in multiple processes(implemented by cluster)

Open
#2,019 16 comments 3 reactions 0 assignees View on GitHub
Text & Fonts
Dominant language
JavaScript
Stars
10.7k
Forks
1.2k
Avg merge
4d 8h
Merged PRs (30d)
1

Description

**Issue:**
Sometimes the text can be rendered correctly but sometimes it seems using another default font(same always), it works on one process but doesn't work on another process because I'm using cluster. Since there is a license for the font, I can't upload it here. And I think it should not be relevant to the font as sometimes it really works. It is not happening every time.

**Note: The important is that it works fine if fork one process only!!! And it worked well on Canvas 1.6.11 as well before upgraded to 2.9.1**

**Code:**
let cluster = require('cluster');
if(cluster.isPrimary) {
for (let i = 0; i < 8; i++) {
cluster.fork();
}
} else {
const {createCanvas, registerFont} = require('canvas');
const font_path = '/data/fonts/NaiveBold.ttf';
let font_name = 'NaiveBold';
registerFont(font_path, {family: font_name});
let canvas = createCanvas(300, 300);
let ctx = canvas.getContext('2d');
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
ctx.fillStyle = '#000000;
ctx.font = '20px ' + font_name;
ctx.fillText('This is a test text.', 0, 0);
// Try to save the text into a file to see the issue
// And from here it will start a web server by express
}

Text images generated:
Correct font used:
![correct-font](https://user-images.githubusercontent.com/96717845/162553847-c6ab9ed6-0e2a-4ed8-90ca-6d9f7e6be56f.jpg)
Incorrect font used(always same font):
![default-font](https://user-images.githubusercontent.com/96717845/162553854-a58fd70f-c6e1-4091-8e9c-70851e0d750f.jpg)

**Environment:**
Nodejs 16.14.0 on Ubontu
Canvas 2.9.1
Fontconfig 2.13.1
Freetype 2.10.4
Cairo 1.16.0
Pango 1.46.0

Are there any ideas? Thank you in advance.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.