Font Command

This commit is contained in:
Dragon Fire
2021-02-20 11:26:43 -05:00
parent 7f448a7712
commit 4a2c294947
5 changed files with 103 additions and 2 deletions
+49
View File
@@ -0,0 +1,49 @@
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();
}
};
+1 -1
View File
@@ -67,7 +67,7 @@ module.exports = class TypingTestCommand extends Command {
generateImage(sentence) {
const canvasPre = createCanvas(1, 1);
const ctxPre = canvasPre.getContext('2d');
ctxPre.font = '75px Noto';
ctxPre.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(75);
const len = ctxPre.measureText(sentence);
const canvas = createCanvas(100 + len.width, 200);
const ctx = canvas.getContext('2d');
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "129.7.3",
"version": "129.8.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+4
View File
@@ -40,4 +40,8 @@ module.exports = class Font {
get filenameNoExt() {
return this.filename.replace(/(\.(otf|ttf))$/, '');
}
get isVariant() {
return variants[this.filename];
}
};
+48
View File
@@ -0,0 +1,48 @@
const { ArgumentType, util: { disambiguation } } = require('discord.js-commando');
const { escapeMarkdown } = require('discord.js');
module.exports = class FontArgumentType extends ArgumentType {
constructor(client) {
super(client, 'font');
}
validate(value) {
const choice = value.toLowerCase();
let found = this.client.fonts.filter(font => {
if (font.isVariant) return false;
if (font.name.toLowerCase().includes(choice)) return true;
if (font.filenameNoExt.toLowerCase().includes(choice)) return true;
return false;
});
if (found.size === 0) return false;
if (found.size === 1) return true;
const foundExact = found.filter(font => {
if (font.name.toLowerCase() === choice) return true;
if (font.filenameNoExt.toLowerCase() === choice) return true;
return false;
});
if (foundExact.size === 1) return true;
if (foundExact.size > 0) found = foundExact;
return found.size <= 15 ?
`${disambiguation(found.map(font => escapeMarkdown(font.name)), 'fonts', null)}\n` :
'Multiple fonts found. Please be more specific.';
}
parse(value) {
const choice = value.toLowerCase();
let found = this.client.fonts.filter(font => {
if (font.name.toLowerCase().includes(choice)) return true;
if (font.filenameNoExt.toLowerCase().includes(choice)) return true;
return true;
});
if (found.size === 0) return null;
if (found.size === 1) return found.first();
const foundExact = found.filter(font => {
if (font.name.toLowerCase() === choice) return true;
if (font.filenameNoExt.toLowerCase() === choice) return true;
return true;
});
if (foundExact.size === 1) return foundExact.first();
return null;
}
};