javascript - 如何修复错误:Fontconfig 错误:无法加载默认配置文件
问题描述
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。
解决方案
它应该在 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';
推荐阅读
- java - Spring Boot @GetMapping 到以 .jsp 结尾的 URL
- python - XPath 总是返回一个空列表
- python - 无法通过代理链运行 selenium python 脚本
- sql - 基于同一视图中的列添加其他列并放置值
- android-studio - 是否没有退出接口库的 v7.28:0:0?
- python - 如何从 Pytest 中的另一个测试开始测试?
- typescript - 从 Flow 迁移到 Typescript:TS 中的 $Keys 等价物?
- c# - 收到错误“使用未分配的局部变量”
- .net-core - Azure DevOps 管道“dotnet build”找不到项目
- mysql - Perl DBD::MySQL 执行不支持 limit 关键字后的字符串参数。始终视为数字