最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fix error: Fontconfig error: Cannot load default config file - Stack Overflow

programmeradmin3浏览0评论
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(),
  'wele-image.png'
 );

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

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

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.

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(),
  'wele-image.png'
 );

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

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

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.

Share Improve this question edited Apr 24, 2021 at 15:01 Lioness100 8,4126 gold badges20 silver badges50 bronze badges asked Apr 17, 2021 at 7:25 Hassan_BHHassan_BH 1131 gold badge2 silver badges11 bronze badges 3
  • Can you post a code snippet so i can tell you what's wrong in it..? i think the issue with fontPath './restPath' – Lakshman Kambam Commented Apr 17, 2021 at 7:31
  • Ok @LakshmanKambam – Hassan_BH Commented Apr 17, 2021 at 9:50
  • @LakshmanKambam DID! – Hassan_BH Commented Apr 17, 2021 at 9:53
Add a ment  | 

2 Answers 2

Reset to default 13

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

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/icsans.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)

Example:

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

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

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

ctx.font = '12px Roboto Medium';

I had the error in a CentOS instance, installing this worked for me.

yum install fontconfig
发布评论

评论列表(0)

  1. 暂无评论