Update Font system

This commit is contained in:
Dragon Fire
2024-05-01 23:14:31 -04:00
parent 3610ee6f70
commit ae59abaf71
8 changed files with 39 additions and 28 deletions
+46
View File
@@ -0,0 +1,46 @@
const { registerFont } = require('canvas');
const weights = {
100: 'thin',
200: 'extraLight',
300: 'light',
400: 'normal',
500: 'medium',
600: 'semiBold',
700: 'bold',
800: 'extraBold',
900: 'heavy',
950: 'extraBlack'
};
const fallbacks = ['Symbola', 'Noto-CJK'];
module.exports = class Font {
constructor(path, filename, metadata) {
this.path = path;
this.name = metadata.name || filename;
this.filename = filename;
this.style = metadata.style === 'regular' ? 'normal' : metadata.style || 'normal';
this.weight = weights[metadata.weight] || metadata.weight || 'normal';
this.type = metadata.type;
this.registered = false;
this.fallbacks = fallbacks.filter(fallback => fallback !== this.filenameNoExt);
}
register() {
if (this.registered) return null;
this.registered = true;
return registerFont(this.path, { family: this.filenameNoExt, style: this.style, weight: this.weight });
}
toCanvasString(size, shouldDoFallbacks = true) {
const shouldFall = shouldDoFallbacks ? `, ${this.fallbacks.join(', ')}` : '';
return `${this.style} ${this.weight} ${size}px ${this.filenameNoExt}${shouldFall}`;
}
get filenameNoExt() {
return this.filename.replace(/(\.(otf|ttf))$/, '');
}
get isFallback() {
return fallbacks.includes(this.filenameNoExt);
}
};
+24
View File
@@ -0,0 +1,24 @@
const { Collection } = require('@discordjs/collection');
const fs = require('fs');
const path = require('path');
const fontFinder = require('font-finder');
const Font = require('./Font');
module.exports = class FontManager extends Collection {
constructor(client, options) {
super(options);
Object.defineProperty(this, 'client', { value: client });
}
async registerFontsIn(filepath) {
const files = fs.readdirSync(filepath);
for (const file of files) {
const metadata = await fontFinder.get(path.join(filepath, file));
const font = new Font(path.join(filepath, file), file, metadata);
this.set(file, font);
font.register();
}
return this;
}
};