Automattic / Automattic/node-canvas

registerFont function is not working

Open
#2,255 9 comments 0 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
I am facing an issue while trying to use a custom font. The font works perfectly fine when it is installed on my local machine (Windows), but I am unable to use it when importing it using the `registerFont` function.

## Code
```js
const { SlashCommandBuilder, AttachmentBuilder } = require("discord.js");
const { createCanvas, registerFont } = require("canvas");
const path = require("node:path");

module.exports = {
data: new SlashCommandBuilder()
.setName("test")
.setDescription("Generate an image"),
async execute(interaction) {
await interaction.deferReply();

const customFontPath = path.join(
__dirname,
"../../../assets/fonts/Impress.ttf"
);

registerFont(customFontPath, { family: "Impress BT" });

const canvas = createCanvas(500, 500);
const ctx = canvas.getContext("2d");

ctx.font = '30px "Impress BT"';
ctx.fillText("Test", 10, 50);

const attachment = new AttachmentBuilder(canvas.createPNGStream(), {
name: `image.png`,
});

await interaction.editReply({
files: [attachment],
});
},
};
```

This is the image I get when the font is installed on my system: ![image](https://github.com/Automattic/node-canvas/assets/21266356/7e9fcea6-6707-4106-bee5-485a3b8f2aca)

This is the image I get when the font is not installed on my system: ![image](https://github.com/Automattic/node-canvas/assets/21266356/7e590f41-f143-4ce0-a0a5-3e1d3c808038)

- The custom font I'm using: https://www.dafontfree.net/impress-bt-regular/f129654.htm
- I have checked that the font file is accessible and located at the correct path. I have no error in the console.
- I have tried different approaches, including using different font file formats (TTF, OTF) and using different family names.
- It does the same with other custom fonts that aren't installed on my system. :(

## Your Environment
* canvas@2.11.2
* Node 18.16.0 on Windows 10

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.