mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-11 11:21:16 +02:00
Update Font system
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user