Files
xiao/commands/edit-image/font.js
T
Dragon Fire 4a2c294947 Font Command
2021-02-20 11:26:43 -05:00

50 lines
1.5 KiB
JavaScript

const Command = require('../../structures/Command');
const { createCanvas } = require('canvas');
const { wrapText } = require('../../util/Canvas');
module.exports = class FontCommand extends Command {
constructor(client) {
super(client, {
name: 'font',
aliases: ['font-test'],
group: 'edit-image',
memberName: 'font',
description: 'Types text in a specific font.',
args: [
{
key: 'font',
prompt: 'What font do you want to use? Only fonts used in other commands are available.',
type: 'font'
},
{
key: 'text',
prompt: 'What text do you want to type?',
type: 'string'
}
]
});
}
run(msg, { font, text }) {
return msg.say({ files: [{ attachment: this.generateImage(font, text), name: `${font.filenameNoExt}.png` }] });
}
generateImage(font, text) {
const canvasPre = createCanvas(1, 1);
const ctxPre = canvasPre.getContext('2d');
ctxPre.font = this.client.fonts.get(font.filename).toCanvasString(75);
const len = ctx.measureText(text);
const lines = wrapText(ctxPre, text, 450);
const canvas = createCanvas(Math.min(len, 500), 50 + (75 * lines.length));
const ctx = canvas.getContext('2d');
ctx.font = this.client.fonts.get(font.filename).toCanvasString(75);
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'black';
ctx.fillText(lines.join('\n'), canvas.width / 2, canvas.height / 2);
return canvas.toBuffer();
}
};