mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-06 14:20:51 +02:00
Add global client font manager
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
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'
|
||||
};
|
||||
|
||||
module.exports = class Font {
|
||||
constructor(path, filename, metadata) {
|
||||
this.path = path;
|
||||
this.name = metadata.name || 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.name, style: this.style, weight: this.weight });
|
||||
}
|
||||
|
||||
toCanvasString(size) {
|
||||
return `${this.style} ${this.weight} ${size}px ${this.name}`;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user