如何修复错误:Fontconfig 错误:无法加载默认配置文件

2022-01-10 00:00:00 node.js javascript discord.js
client.on('guildMemberAdd', async (member) => {
 const channel = member.guild.channels.cache.find(
  (channel) => channel.name === 'general'
 );

 if (!channel) return;

 const canvas = Canvas.createCanvas(700, 250);

 const ctx = canvas.getContext('2d');

 const background = await Canvas.loadImage('./wallpaper.jpg');
 ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

 ctx.strokeStyle = '#ffffff';
 ctx.strokeRect(0, 0, canvas.width, canvas.height);

 // Select the font size and type from one of the natively available fonts
 ctx.font = '60px ArialCE.ttf';
 // Select the style that will be used to fill the text in
 ctx.fillStyle = '#ffffff';
 // Actually fill the text with a solid color
 ctx.fillText(member.displayName, canvas.width / 2.5, canvas.height / 1.8);

 ctx.beginPath();
 // Start the arc to form a circle
 ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
 // Put the pen down
 ctx.closePath();
 // Clip off the region you drew on
 ctx.clip();

 const avatar = await Canvas.loadImage(
  member.user.displayAvatarURL({ format: 'jpg' })
 );
 // Move the image downwards vertically and constrain its height to 200, so it's a square
 ctx.drawImage(avatar, 25, 25, 200, 200);

 const attachment = new Discord.MessageAttachment(
  canvas.toBuffer(),
  'welcome-image.png'
 );

 channel.send(`Welcome ${member.toString()} to the server!`, attachment);
});

我一直在使用 discord.js 制作一个不和谐的机器人.我想制作一个画布欢迎信息,然后当它制作完成时,它可以正常工作,除了画布上的文字.原来是这样的:

I have been making a discord bot using discord.js. I wanted to make a canvas welcome message then when made it, it was working and all, except the words on the canvas. It was all like this:

我在谷歌上搜索了很多,但找不到任何解决方案.我得到的错误是(Fontconfig 错误:无法加载默认配置文件).我知道这意味着我的系统中没有字体,但是如何添加它们.我正在使用 repl.it.

and I searched a lot on google but couldn't find any solution. The error I get is (Fontconfig error: Cannot load default config file). I know it means that there are no fonts in my system but how to add them. I'm using repl.it.

推荐答案

它应该在 node-canvas 2.0+ 中工作.签出新的 Canvas.registerFont 方法:https://github.com/Automattic/node-canvas/#registerfont

It should work in node-canvas 2.0+. checkout new Canvas.registerFont method: https://github.com/Automattic/node-canvas/#registerfont

要使用未安装为系统字体的字体文件,请使用 registerFont() 将字体注册到 Canvas.这必须在创建画布之前完成.

To use a font file that is not installed as a system font, use registerFont() to register the font with Canvas. This must be done before the Canvas is created.

const { registerFont, createCanvas } = require('canvas')
registerFont('./fontFolder/comicsans.ttf', { family: 'Comic Sans' })

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

ctx.font = '12px "Comic Sans"'
ctx.fillText('Everyone hates this font :(', 250, 10)

例子:

TTF 文件将具有不同的字体名称.所以这里只有 'Roboto' .否则它不起作用.

TTF file will have a different font name. so only 'Roboto' here. otherwise it doesn't work.

registerFont('./fonts/Roboto-Medium.ttf', {family: 'Roboto'})

不要将 fontName 定义为 'Medium 28px Roboto'.它不会工作.

Don't define fontName as 'Medium 28px Roboto'. it won't work.

ctx.font = '12px Roboto Medium';

相关文章