UNPKG

@nmmty/lazycanvas

Version:

A simple way to interact with @napi-rs/canvas in an advanced way!

191 lines (190 loc) 6.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FontsManager = void 0; const helpers_1 = require("../helpers"); const LazyUtil_1 = require("../../utils/LazyUtil"); const Fonts_1 = require("../../helpers/Fonts"); const canvas_1 = require("@napi-rs/canvas"); /** * Class representing a manager for handling fonts. */ class FontsManager { /** * A map storing fonts with their family and weight as the key. */ map; /** * Whether debugging is enabled. */ debug; /** * Constructs a new FontsManager instance. * @param {Object} [opts] - Optional settings for the FontsManager. * @param {boolean} [opts.debug] - Whether debugging is enabled. */ constructor(opts) { this.map = new Map(); this.debug = opts?.debug || false; this.loadFonts(Fonts_1.Fonts); } /** * Loads fonts into the manager from a given font list. * @param {IFonts} [fontList] - The fonts to load into the manager. * @returns {this} The current instance for chaining. */ loadFonts(fontList) { this.add(...Object.entries(fontList).map(([fontFamily, fontWeights]) => { return Object.entries(fontWeights).map(([weight, base64]) => { return new helpers_1.Font() .setFamily(fontFamily) .setWeight(Number(weight)) .setBase64(base64); }); }).flat()); return this; } /** * 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 in [MD file](https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md). * @param {Font[]} [fonts] - The fonts to add to the manager. * @returns {this} The current instance for chaining. * @throws {LazyError} If required font properties are missing or the font already exists. */ 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; } /** * Removes fonts from the manager. * @param {Array<{ family: string, weight: string }>} [array] - The family and weight of the fonts to remove. * @returns {this} The current instance for chaining. */ remove(...array) { for (const font of array) { this.map.delete(`${font.family}_${font.weight}`); } return this; } /** * Clears all fonts from the manager. * @returns {this} The current instance for chaining. */ clear() { this.map.clear(); return this; } /** * Retrieves a font or fonts from the manager. * @param {string} [family] - The family of the font to retrieve. * @param {string} [weight] - The weight of the font to retrieve (optional). * @returns {Font | Font[] | undefined} The retrieved font(s) or undefined if not found. */ get(family, weight) { if (weight) return this.map.get(`${family}_${weight}`); return Array.from(this.map.values()).filter(font => font.family === family); } /** * Checks if a font exists in the manager. * @param {string} [family] - The family of the font to check. * @param {string} [weight] - The weight of the font to check (optional). * @returns {boolean} True if the font exists, false otherwise. */ has(family, weight) { if (weight) return this.map.has(`${family}_${weight}`); return Array.from(this.map.values()).some(font => font.family === family); } /** * Retrieves the number of fonts in the manager. * @returns {number} The size of the font map. */ size() { return this.map.size; } /** * Retrieves the values (fonts) from the manager. * @returns {IterableIterator<Font>} An iterator for the font values. */ values() { return this.map.values(); } /** * Retrieves the keys (family and weight) from the manager. * @returns {IterableIterator<string>} An iterator for the font keys. */ keys() { return this.map.keys(); } /** * Retrieves the entries (key-value pairs) from the manager. * @returns {IterableIterator<[string, Font]>} An iterator for the font entries. */ entries() { return this.map.entries(); } /** * Iterates over the fonts in the manager. * @param {Function} [callbackfn] - The function to execute on each font. * @param {any} [thisArg] - The `this` context to use (optional). * @returns {this} The current instance for chaining. */ forEach(callbackfn, thisArg) { this.map.forEach(callbackfn, thisArg); return this; } /** * Converts the font map to a JSON object. * @returns {object} The JSON representation of the font map. */ toJSON() { return Object.fromEntries(this.map); } /** * Populates the font map from a JSON object. * @param {object} [json] - The JSON object to populate the map from. * @returns {this} The current instance for chaining. */ fromJSON(json) { this.map = new Map(Object.entries(json)); return this; } /** * Converts the font map to an array. * @returns {Font[]} An array of fonts. */ toArray() { return Array.from(this.map.values()); } /** * Populates the font map from an array of fonts. * @param {Font[]} [array] - The array of fonts to populate the map from. * @returns {this} The current instance for chaining. */ fromArray(array) { for (const font of array) { this.map.set(`${font.family}_${font.weight}`, font); } return this; } } exports.FontsManager = FontsManager;