Files
xiao/structures/Font.js
T
2021-02-20 11:01:19 -05:00

44 lines
1.1 KiB
JavaScript

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 variants = {
'Noto-CJK.otf': 'Noto Sans',
'Noto-Emoji.ttf': 'Noto Sans'
};
module.exports = class Font {
constructor(path, filename, metadata) {
this.path = path;
this.name = variants[filename] || 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;
}
register() {
if (this.registered) return null;
this.registered = true;
return registerFont(this.path, { family: this.filenameNoExt, style: this.style, weight: this.weight });
}
toCanvasString(size) {
return `${this.style} ${this.weight} ${size}px ${this.filenameNoExt}`;
}
get filenameNoExt() {
return this.filename.replace(/(\.(otf|ttf))$/, '');
}
};