【问题标题】:How to fix error: Fontconfig error: Cannot load default config file如何修复错误:Fontconfig 错误:无法加载默认配置文件
【发布时间】:2021-07-12 02:24:47
【问题描述】:
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 制作一个不和谐的机器人。我想制作一个画布欢迎信息,然后当它制作完成时,它可以正常工作,除了画布上的文字。原来是这样的:

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

【问题讨论】:

  • 你能发布一个代码 sn-p 以便我可以告诉你其中有什么问题..?我认为 fontPath './restPath' 的问题
  • 好的@LakshmanKambam
  • @LakshmanKambam 做到了!

标签: javascript node.js discord.js


【解决方案1】:

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

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

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'。否则它不起作用。

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

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

ctx.font = '12px Roboto Medium';

【讨论】:

  • 你摇滚!你解决了问题不知道如何感谢你!
  • 很高兴知道它有帮助。不要忘记投票。
  • 我还不能,因为我没有 15 声望。("
  • 哦,我没注意到。非易失性!别担心......你很快就会到达那里。 :)
猜你喜欢
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
相关资源
最近更新 更多