@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
167 lines (166 loc) • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FontsManager = void 0;
const Font_1 = require("../helpers/Font");
const LazyUtil_1 = require("../../utils/LazyUtil");
const Fonts_1 = require("../../helpers/Fonts");
const canvas_1 = require("@napi-rs/canvas");
class FontsManager {
map;
debug;
constructor(debug = false) {
this.debug = debug;
this.map = new Map();
this.loadFonts(Fonts_1.Fonts);
}
/**
* Replace base fonts with custom fonts by special file.
* Use this method before loading fonts by `FontManager`.
* The file should be generated by the following instructions:
* @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
* @param fontList {IFonts[]} - The `fonts` to set
*/
loadFonts(fontList) {
for (const fontFamily in fontList) {
if (fontList.hasOwnProperty(fontFamily)) {
for (const weight in fontList[fontFamily]) {
if (fontList[fontFamily].hasOwnProperty(weight)) {
this.add(new Font_1.Font()
.setFamily(fontFamily)
.setWeight(Number(weight))
// @ts-ignore
.setBase64(fontList[fontFamily][weight]));
}
}
}
}
return this;
}
/**
* Add a font to the map
* @param fonts {Font[]} - The `font` to add to the map
*/
add(...fonts) {
if (this.debug)
LazyUtil_1.LazyLog.log('info', `Adding fonts...\nlength: ${fonts.length}`);
for (const font of fonts) {
if (this.debug)
LazyUtil_1.LazyLog.log('none', `Data:`, font.toJSON());
if (!font.family)
throw new LazyUtil_1.LazyError("Family must be provided");
if (!font.weight)
throw new LazyUtil_1.LazyError("Weight must be provided");
if (!font.path && !font.base64)
throw new LazyUtil_1.LazyError("Path or base64 must be provided");
if (this.map.has(`${font.family}_${font.weight}`))
throw new LazyUtil_1.LazyError("Font already exists");
this.map.set(`${font.family}_${font.weight}`, font);
if (font.path)
canvas_1.GlobalFonts.registerFromPath(font.path, font.family);
if (font.base64)
canvas_1.GlobalFonts.register(font.base64, font.family);
}
return this;
}
/**
* Remove a font from the map
* @param array {Array<{ family: string, weight: string }>} - The `family` and `weight` of the font to remove
*/
remove(...array) {
for (const font of array) {
this.map.delete(`${font.family}_${font.weight}`);
}
return this;
}
/**
* ClearLayer all fonts from the map
*/
clear() {
this.map.clear();
return this;
}
/**
* Get a font from the map
* @param family {string} - The `family` of the font to get
* @param weight {string} - The `weight` of the font to get
*/
get(family, weight) {
if (weight)
return this.map.get(`${family}_${weight}`);
return Array.from(this.map.values()).filter(font => font.family === family);
}
/**
* Check if a font exists in the map
* @param family {string} - The `family` of the font to check
* @param weight {string} - The `weight` of the font to check
*/
has(family, weight) {
if (weight)
return this.map.has(`${family}_${weight}`);
return Array.from(this.map.values()).some(font => font.family === family);
}
/**
* Get the size of the map
*/
size() {
return this.map.size;
}
/**
* Get the values of the map
*/
values() {
return this.map.values();
}
/**
* Get the keys of the map
*/
keys() {
return this.map.keys();
}
/**
* Get the entries of the map
*/
entries() {
return this.map.entries();
}
/**
* Iterate over the map
* @param callbackfn {Function} - The function to execute on each font
* @param thisArg {any} - The `this` context to use
*/
forEach(callbackfn, thisArg) {
this.map.forEach(callbackfn, thisArg);
return this;
}
/**
* Convert the map to a JSON object
*/
toJSON() {
return Object.fromEntries(this.map);
}
/**
* Convert the map from a JSON object
* @param json {object} - The JSON object to convert
*/
fromJSON(json) {
this.map = new Map(Object.entries(json));
return this;
}
/**
* Convert the map to an array
*/
toArray() {
return Array.from(this.map.values());
}
/**
* Convert an array to the map
* @param array {Font[]} - The `array` to convert
*/
fromArray(array) {
for (const font of array) {
this.map.set(`${font.family}_${font.weight}`, font);
}
return this;
}
}
exports.FontsManager = FontsManager;