admin管理员组

文章数量:1122832

I'm using node-canvas to generate an image with custom fonts. I've included a .ttf file and registered it using registerFont, but the font doesn't apply correctly to the text in the final image. Here's a simplified version of my code:

const { createCanvas, registerFont } = require('canvas');
const path = require('path');

const fontPath = path.join(__dirname, 'font.ttf');
registerFont(fontPath, { family: 'CustomFont' });

const canvas = createCanvas(800, 600);
const ctx = canvas.getContext('2d');
ctx.font = '30px "CustomFont", sans-serif';
ctx.fillStyle = 'black';
ctx.fillText('Hello World', 50, 50);

When I run this, the text renders, but the custom font is not applied. I'm suspecting the issue might be with the family name provided in registerFont.

How can I confirm the correct family name for the font I'm using? I've tried the following without success:

  1. Checking the font file name (e.g., font.ttf) and using it as the family name.
  2. Changing the font style in the ctx.font property.

Are there any reliable ways to extract the family name from the .ttf file or confirm what node-canvas recognizes? Any other debugging tips would be appreciated!

本文标签: javascriptFont Not Applying in nodecanvasStack Overflow