@nmmty/lazycanvas 0.3.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/Filters.d.ts +10 -10
- package/dist/helpers/Fonts.d.ts +1 -1
- package/dist/helpers/FontsList.d.ts +1 -1
- package/dist/helpers/FontsList.js +19 -19
- package/dist/index.d.ts +11 -20
- package/dist/index.js +40 -45
- package/dist/structures/LazyCanvas.d.ts +126 -17
- package/dist/structures/LazyCanvas.js +101 -33
- package/dist/structures/components/BaseLayer.d.ts +188 -38
- package/dist/structures/components/BaseLayer.js +88 -41
- package/dist/structures/components/BezierLayer.d.ts +108 -21
- package/dist/structures/components/BezierLayer.js +73 -24
- package/dist/structures/components/ClearLayer.d.ts +142 -0
- package/dist/structures/components/ClearLayer.js +152 -0
- package/dist/structures/components/Group.d.ts +86 -18
- package/dist/structures/components/Group.js +69 -29
- package/dist/structures/components/ImageLayer.d.ts +85 -12
- package/dist/structures/components/ImageLayer.js +55 -42
- package/dist/structures/components/LineLayer.d.ts +111 -18
- package/dist/structures/components/LineLayer.js +58 -21
- package/dist/structures/components/MorphLayer.d.ts +109 -21
- package/dist/structures/components/MorphLayer.js +55 -27
- package/dist/structures/components/Path2DLayer.d.ts +191 -0
- package/dist/structures/components/Path2DLayer.js +318 -0
- package/dist/structures/components/QuadraticLayer.d.ts +108 -22
- package/dist/structures/components/QuadraticLayer.js +65 -30
- package/dist/structures/components/TextLayer.d.ts +201 -40
- package/dist/structures/components/TextLayer.js +101 -49
- package/dist/structures/components/index.d.ts +10 -0
- package/dist/structures/components/index.js +26 -0
- package/dist/structures/helpers/Exporter.d.ts +52 -0
- package/dist/structures/helpers/Exporter.js +168 -0
- package/dist/structures/helpers/Font.d.ts +64 -10
- package/dist/structures/helpers/Font.js +38 -11
- package/dist/structures/helpers/Gradient.d.ts +96 -9
- package/dist/structures/helpers/Gradient.js +48 -17
- package/dist/structures/helpers/Link.d.ts +52 -8
- package/dist/structures/helpers/Link.js +42 -11
- package/dist/structures/helpers/Pattern.d.ts +52 -7
- package/dist/structures/helpers/Pattern.js +45 -40
- package/dist/structures/helpers/index.d.ts +6 -0
- package/dist/structures/helpers/index.js +22 -0
- package/dist/structures/helpers/readers/JSONReader.d.ts +49 -0
- package/dist/structures/helpers/readers/JSONReader.js +172 -0
- package/dist/structures/helpers/readers/SVGReader.d.ts +20 -0
- package/dist/structures/helpers/readers/SVGReader.js +577 -0
- package/dist/structures/helpers/readers/YAMLReader.d.ts +0 -0
- package/dist/structures/helpers/readers/YAMLReader.js +1 -0
- package/dist/structures/managers/AnimationManager.d.ts +120 -0
- package/dist/structures/managers/AnimationManager.js +99 -0
- package/dist/structures/managers/FontsManager.d.ts +76 -32
- package/dist/structures/managers/FontsManager.js +70 -45
- package/dist/structures/managers/LayersManager.d.ts +84 -32
- package/dist/structures/managers/LayersManager.js +67 -29
- package/dist/structures/managers/RenderManager.d.ts +63 -6
- package/dist/structures/managers/RenderManager.js +162 -33
- package/dist/types/LazyCanvas.d.ts +2 -0
- package/dist/types/components/ClearLayer.d.ts +19 -0
- package/dist/types/enum.d.ts +20 -7
- package/dist/types/enum.js +24 -10
- package/dist/types/index.d.ts +2 -17
- package/dist/types/index.js +17 -0
- package/dist/types/managers/AnimationManager.d.ts +14 -0
- package/dist/types/types.d.ts +7 -3
- package/dist/utils/LazyUtil.js +2 -2
- package/dist/utils/utils.d.ts +10 -9
- package/dist/utils/utils.js +159 -164
- package/package.json +9 -5
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { AnyColorSpace } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing the animation manager.
|
|
4
|
+
*/
|
|
5
|
+
export interface IAnimationManager {
|
|
6
|
+
/**
|
|
7
|
+
* The options for the animation manager.
|
|
8
|
+
*/
|
|
9
|
+
options: IAnimationOptions;
|
|
10
|
+
/**
|
|
11
|
+
* Whether debugging is enabled.
|
|
12
|
+
*/
|
|
13
|
+
debug: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface representing the animation options.
|
|
17
|
+
*/
|
|
18
|
+
export interface IAnimationOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The frame rate of the animation.
|
|
21
|
+
*/
|
|
22
|
+
frameRate: number;
|
|
23
|
+
/**
|
|
24
|
+
* The maximum number of colors in the animation.
|
|
25
|
+
*/
|
|
26
|
+
maxColors: number;
|
|
27
|
+
/**
|
|
28
|
+
* The color space used in the animation.
|
|
29
|
+
*/
|
|
30
|
+
colorSpace: AnyColorSpace;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the animation should loop.
|
|
33
|
+
*/
|
|
34
|
+
loop: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the animation should have transparency.
|
|
37
|
+
*/
|
|
38
|
+
transparency: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Utility options for the animation.
|
|
41
|
+
*/
|
|
42
|
+
utils: {
|
|
43
|
+
/**
|
|
44
|
+
* Whether to clear the content of previous frames.
|
|
45
|
+
*/
|
|
46
|
+
clear: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Buffer-related options.
|
|
49
|
+
*/
|
|
50
|
+
buffer: {
|
|
51
|
+
/**
|
|
52
|
+
* The size of the frame buffer.
|
|
53
|
+
*/
|
|
54
|
+
size: number;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Class representing the animation manager, which handles animation settings and options.
|
|
60
|
+
*/
|
|
61
|
+
export declare class AnimationManager implements IAnimationManager {
|
|
62
|
+
/**
|
|
63
|
+
* The options for the animation manager.
|
|
64
|
+
*/
|
|
65
|
+
options: IAnimationOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Whether debugging is enabled.
|
|
68
|
+
*/
|
|
69
|
+
debug: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Constructs a new AnimationManager instance.
|
|
72
|
+
* @param opts {Object} - Optional settings for the animation manager.
|
|
73
|
+
* @param opts.debug {boolean} - Whether debugging is enabled.
|
|
74
|
+
* @param opts.settings {Object} - Additional settings for the animation manager.
|
|
75
|
+
* @param opts.settings.options {IAnimationOptions} - The animation options.
|
|
76
|
+
*/
|
|
77
|
+
constructor(opts?: {
|
|
78
|
+
debug?: boolean;
|
|
79
|
+
settings?: {
|
|
80
|
+
options?: IAnimationOptions;
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
/**
|
|
84
|
+
* Sets the frame rate of the animation.
|
|
85
|
+
* @param frameRate {number} - The frame rate of the animation (default is 30).
|
|
86
|
+
* @returns {this} The current instance for chaining.
|
|
87
|
+
*/
|
|
88
|
+
setFrameRate(frameRate: number): this;
|
|
89
|
+
/**
|
|
90
|
+
* Sets whether the animation should loop.
|
|
91
|
+
* @param loop {boolean} - Whether the animation should loop (default is true).
|
|
92
|
+
* @returns {this} The current instance for chaining.
|
|
93
|
+
*/
|
|
94
|
+
setLoop(loop: boolean): this;
|
|
95
|
+
/**
|
|
96
|
+
* Sets whether the animation should have transparency.
|
|
97
|
+
* @param transparency {boolean} - Whether the animation should have transparency (default is true).
|
|
98
|
+
* @returns {this} The current instance for chaining.
|
|
99
|
+
*/
|
|
100
|
+
setTransparent(transparency: boolean): this;
|
|
101
|
+
/**
|
|
102
|
+
* Sets the RGB format of the animation.
|
|
103
|
+
* @param rgb {ColorSpace} - The RGB format of the animation (default is RGB565).
|
|
104
|
+
* @returns {this} The current instance for chaining.
|
|
105
|
+
*/
|
|
106
|
+
setRGBFormat(rgb: AnyColorSpace): this;
|
|
107
|
+
/**
|
|
108
|
+
* Sets the maximum number of colors in the animation.
|
|
109
|
+
* @param maxColors {number} - The maximum number of colors (default is 256).
|
|
110
|
+
* @returns {this} The current instance for chaining.
|
|
111
|
+
*/
|
|
112
|
+
setMaxColors(maxColors: number): this;
|
|
113
|
+
/**
|
|
114
|
+
* Sets whether the content of previous frames will be cleared.
|
|
115
|
+
* @param clear {boolean} - Whether to clear the content of previous frames (default is true).
|
|
116
|
+
* @param bufferSize {number} - The size of the frame buffer (default is 0).
|
|
117
|
+
* @returns {this} The current instance for chaining.
|
|
118
|
+
*/
|
|
119
|
+
setClear(clear: boolean, bufferSize?: number): this;
|
|
120
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnimationManager = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
/**
|
|
6
|
+
* Class representing the animation manager, which handles animation settings and options.
|
|
7
|
+
*/
|
|
8
|
+
class AnimationManager {
|
|
9
|
+
/**
|
|
10
|
+
* The options for the animation manager.
|
|
11
|
+
*/
|
|
12
|
+
options;
|
|
13
|
+
/**
|
|
14
|
+
* Whether debugging is enabled.
|
|
15
|
+
*/
|
|
16
|
+
debug;
|
|
17
|
+
/**
|
|
18
|
+
* Constructs a new AnimationManager instance.
|
|
19
|
+
* @param opts {Object} - Optional settings for the animation manager.
|
|
20
|
+
* @param opts.debug {boolean} - Whether debugging is enabled.
|
|
21
|
+
* @param opts.settings {Object} - Additional settings for the animation manager.
|
|
22
|
+
* @param opts.settings.options {IAnimationOptions} - The animation options.
|
|
23
|
+
*/
|
|
24
|
+
constructor(opts) {
|
|
25
|
+
this.options = opts?.settings?.options || {
|
|
26
|
+
frameRate: 30,
|
|
27
|
+
maxColors: 256,
|
|
28
|
+
colorSpace: types_1.ColorSpace.RGB565,
|
|
29
|
+
loop: true,
|
|
30
|
+
transparency: true,
|
|
31
|
+
utils: {
|
|
32
|
+
clear: true,
|
|
33
|
+
buffer: {
|
|
34
|
+
size: 0
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
this.debug = opts?.debug || false;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Sets the frame rate of the animation.
|
|
42
|
+
* @param frameRate {number} - The frame rate of the animation (default is 30).
|
|
43
|
+
* @returns {this} The current instance for chaining.
|
|
44
|
+
*/
|
|
45
|
+
setFrameRate(frameRate) {
|
|
46
|
+
this.options.frameRate = frameRate;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Sets whether the animation should loop.
|
|
51
|
+
* @param loop {boolean} - Whether the animation should loop (default is true).
|
|
52
|
+
* @returns {this} The current instance for chaining.
|
|
53
|
+
*/
|
|
54
|
+
setLoop(loop) {
|
|
55
|
+
this.options.loop = loop;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sets whether the animation should have transparency.
|
|
60
|
+
* @param transparency {boolean} - Whether the animation should have transparency (default is true).
|
|
61
|
+
* @returns {this} The current instance for chaining.
|
|
62
|
+
*/
|
|
63
|
+
setTransparent(transparency) {
|
|
64
|
+
this.options.transparency = transparency;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Sets the RGB format of the animation.
|
|
69
|
+
* @param rgb {ColorSpace} - The RGB format of the animation (default is RGB565).
|
|
70
|
+
* @returns {this} The current instance for chaining.
|
|
71
|
+
*/
|
|
72
|
+
setRGBFormat(rgb) {
|
|
73
|
+
this.options.colorSpace = rgb;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Sets the maximum number of colors in the animation.
|
|
78
|
+
* @param maxColors {number} - The maximum number of colors (default is 256).
|
|
79
|
+
* @returns {this} The current instance for chaining.
|
|
80
|
+
*/
|
|
81
|
+
setMaxColors(maxColors) {
|
|
82
|
+
this.options.maxColors = maxColors;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sets whether the content of previous frames will be cleared.
|
|
87
|
+
* @param clear {boolean} - Whether to clear the content of previous frames (default is true).
|
|
88
|
+
* @param bufferSize {number} - The size of the frame buffer (default is 0).
|
|
89
|
+
* @returns {this} The current instance for chaining.
|
|
90
|
+
*/
|
|
91
|
+
setClear(clear, bufferSize) {
|
|
92
|
+
this.options.utils.clear = clear;
|
|
93
|
+
if (bufferSize) {
|
|
94
|
+
this.options.utils.buffer.size = bufferSize;
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.AnimationManager = AnimationManager;
|
|
@@ -1,84 +1,128 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Font, IFonts } from "../helpers";
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing the FontsManager.
|
|
4
|
+
*/
|
|
5
|
+
export interface IFontsManager {
|
|
6
|
+
/**
|
|
7
|
+
* A map storing fonts with their family and weight as the key.
|
|
8
|
+
*/
|
|
9
|
+
map: Map<string, Font>;
|
|
10
|
+
/**
|
|
11
|
+
* Whether debugging is enabled.
|
|
12
|
+
*/
|
|
13
|
+
debug: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Class representing a manager for handling fonts.
|
|
17
|
+
*/
|
|
3
18
|
export declare class FontsManager implements IFontsManager {
|
|
19
|
+
/**
|
|
20
|
+
* A map storing fonts with their family and weight as the key.
|
|
21
|
+
*/
|
|
4
22
|
map: Map<string, Font>;
|
|
23
|
+
/**
|
|
24
|
+
* Whether debugging is enabled.
|
|
25
|
+
*/
|
|
5
26
|
debug: boolean;
|
|
6
|
-
constructor(debug?: boolean);
|
|
7
27
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
28
|
+
* Constructs a new FontsManager instance.
|
|
29
|
+
* @param opts {Object} - Optional settings for the FontsManager.
|
|
30
|
+
* @param opts.debug {boolean} - Whether debugging is enabled.
|
|
31
|
+
*/
|
|
32
|
+
constructor(opts?: {
|
|
33
|
+
debug?: boolean;
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Loads fonts into the manager from a given font list.
|
|
37
|
+
* @param fontList {IFonts} - The fonts to load into the manager.
|
|
38
|
+
* @returns {this} The current instance for chaining.
|
|
13
39
|
*/
|
|
14
40
|
loadFonts(fontList: IFonts): this;
|
|
15
41
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
42
|
+
* Replace base fonts with custom fonts by special file.
|
|
43
|
+
* Use this method before loading fonts by `FontManager`.
|
|
44
|
+
* The file should be generated by the following instructions in MD file.
|
|
45
|
+
* @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
|
|
46
|
+
* @param fonts {Font[]} - The fonts to add to the manager.
|
|
47
|
+
* @returns {this} The current instance for chaining.
|
|
48
|
+
* @throws {LazyError} If required font properties are missing or the font already exists.
|
|
18
49
|
*/
|
|
19
50
|
add(...fonts: Font[]): this;
|
|
20
51
|
/**
|
|
21
|
-
*
|
|
22
|
-
* @param array {Array<{ family: string, weight: string }>} - The
|
|
52
|
+
* Removes fonts from the manager.
|
|
53
|
+
* @param array {Array<{ family: string, weight: string }>} - The family and weight of the fonts to remove.
|
|
54
|
+
* @returns {this} The current instance for chaining.
|
|
23
55
|
*/
|
|
24
56
|
remove(...array: Array<{
|
|
25
57
|
family: string;
|
|
26
58
|
weight: string;
|
|
27
59
|
}>): this;
|
|
28
60
|
/**
|
|
29
|
-
*
|
|
61
|
+
* Clears all fonts from the manager.
|
|
62
|
+
* @returns {this} The current instance for chaining.
|
|
30
63
|
*/
|
|
31
64
|
clear(): this;
|
|
32
65
|
/**
|
|
33
|
-
*
|
|
34
|
-
* @param family {string} - The
|
|
35
|
-
* @param weight {string} - The
|
|
66
|
+
* Retrieves a font or fonts from the manager.
|
|
67
|
+
* @param family {string} - The family of the font to retrieve.
|
|
68
|
+
* @param weight {string} - The weight of the font to retrieve (optional).
|
|
69
|
+
* @returns {Font | Font[] | undefined} The retrieved font(s) or undefined if not found.
|
|
36
70
|
*/
|
|
37
71
|
get(family: string, weight?: string): Font | Font[] | undefined;
|
|
38
72
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @param family {string} - The
|
|
41
|
-
* @param weight {string} - The
|
|
73
|
+
* Checks if a font exists in the manager.
|
|
74
|
+
* @param family {string} - The family of the font to check.
|
|
75
|
+
* @param weight {string} - The weight of the font to check (optional).
|
|
76
|
+
* @returns {boolean} True if the font exists, false otherwise.
|
|
42
77
|
*/
|
|
43
78
|
has(family: string, weight?: string): boolean;
|
|
44
79
|
/**
|
|
45
|
-
*
|
|
80
|
+
* Retrieves the number of fonts in the manager.
|
|
81
|
+
* @returns {number} The size of the font map.
|
|
46
82
|
*/
|
|
47
83
|
size(): number;
|
|
48
84
|
/**
|
|
49
|
-
*
|
|
85
|
+
* Retrieves the values (fonts) from the manager.
|
|
86
|
+
* @returns {IterableIterator<Font>} An iterator for the font values.
|
|
50
87
|
*/
|
|
51
88
|
values(): IterableIterator<Font>;
|
|
52
89
|
/**
|
|
53
|
-
*
|
|
90
|
+
* Retrieves the keys (family and weight) from the manager.
|
|
91
|
+
* @returns {IterableIterator<string>} An iterator for the font keys.
|
|
54
92
|
*/
|
|
55
93
|
keys(): IterableIterator<string>;
|
|
56
94
|
/**
|
|
57
|
-
*
|
|
95
|
+
* Retrieves the entries (key-value pairs) from the manager.
|
|
96
|
+
* @returns {IterableIterator<[string, Font]>} An iterator for the font entries.
|
|
58
97
|
*/
|
|
59
98
|
entries(): IterableIterator<[string, Font]>;
|
|
60
99
|
/**
|
|
61
|
-
*
|
|
62
|
-
* @param callbackfn {Function} - The function to execute on each font
|
|
63
|
-
* @param thisArg {any} - The `this` context to use
|
|
100
|
+
* Iterates over the fonts in the manager.
|
|
101
|
+
* @param callbackfn {Function} - The function to execute on each font.
|
|
102
|
+
* @param thisArg {any} - The `this` context to use (optional).
|
|
103
|
+
* @returns {this} The current instance for chaining.
|
|
64
104
|
*/
|
|
65
105
|
forEach(callbackfn: (value: Font, key: string, map: Map<string, Font>) => void, thisArg?: any): this;
|
|
66
106
|
/**
|
|
67
|
-
*
|
|
107
|
+
* Converts the font map to a JSON object.
|
|
108
|
+
* @returns {object} The JSON representation of the font map.
|
|
68
109
|
*/
|
|
69
110
|
toJSON(): object;
|
|
70
111
|
/**
|
|
71
|
-
*
|
|
72
|
-
* @param json {object} - The JSON object to
|
|
112
|
+
* Populates the font map from a JSON object.
|
|
113
|
+
* @param json {object} - The JSON object to populate the map from.
|
|
114
|
+
* @returns {this} The current instance for chaining.
|
|
73
115
|
*/
|
|
74
116
|
fromJSON(json: object): this;
|
|
75
117
|
/**
|
|
76
|
-
*
|
|
118
|
+
* Converts the font map to an array.
|
|
119
|
+
* @returns {Font[]} An array of fonts.
|
|
77
120
|
*/
|
|
78
121
|
toArray(): Font[];
|
|
79
122
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @param array {Font[]} - The
|
|
123
|
+
* Populates the font map from an array of fonts.
|
|
124
|
+
* @param array {Font[]} - The array of fonts to populate the map from.
|
|
125
|
+
* @returns {this} The current instance for chaining.
|
|
82
126
|
*/
|
|
83
127
|
fromArray(array: Font[]): this;
|
|
84
128
|
}
|
|
@@ -1,44 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FontsManager = void 0;
|
|
4
|
-
const
|
|
4
|
+
const helpers_1 = require("../helpers");
|
|
5
5
|
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
6
6
|
const Fonts_1 = require("../../helpers/Fonts");
|
|
7
7
|
const canvas_1 = require("@napi-rs/canvas");
|
|
8
|
+
/**
|
|
9
|
+
* Class representing a manager for handling fonts.
|
|
10
|
+
*/
|
|
8
11
|
class FontsManager {
|
|
12
|
+
/**
|
|
13
|
+
* A map storing fonts with their family and weight as the key.
|
|
14
|
+
*/
|
|
9
15
|
map;
|
|
16
|
+
/**
|
|
17
|
+
* Whether debugging is enabled.
|
|
18
|
+
*/
|
|
10
19
|
debug;
|
|
11
|
-
|
|
12
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a new FontsManager instance.
|
|
22
|
+
* @param opts {Object} - Optional settings for the FontsManager.
|
|
23
|
+
* @param opts.debug {boolean} - Whether debugging is enabled.
|
|
24
|
+
*/
|
|
25
|
+
constructor(opts) {
|
|
13
26
|
this.map = new Map();
|
|
27
|
+
this.debug = opts?.debug || false;
|
|
14
28
|
this.loadFonts(Fonts_1.Fonts);
|
|
15
29
|
}
|
|
16
30
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
|
|
21
|
-
* @param fontList {IFonts[]} - The `fonts` to set
|
|
31
|
+
* Loads fonts into the manager from a given font list.
|
|
32
|
+
* @param fontList {IFonts} - The fonts to load into the manager.
|
|
33
|
+
* @returns {this} The current instance for chaining.
|
|
22
34
|
*/
|
|
23
35
|
loadFonts(fontList) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.setBase64(fontList[fontFamily][weight]));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
36
|
+
this.add(...Object.entries(fontList).map(([fontFamily, fontWeights]) => {
|
|
37
|
+
return Object.entries(fontWeights).map(([weight, base64]) => {
|
|
38
|
+
return new helpers_1.Font()
|
|
39
|
+
.setFamily(fontFamily)
|
|
40
|
+
.setWeight(Number(weight))
|
|
41
|
+
.setBase64(base64);
|
|
42
|
+
});
|
|
43
|
+
}).flat());
|
|
37
44
|
return this;
|
|
38
45
|
}
|
|
39
46
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
47
|
+
* Replace base fonts with custom fonts by special file.
|
|
48
|
+
* Use this method before loading fonts by `FontManager`.
|
|
49
|
+
* The file should be generated by the following instructions in MD file.
|
|
50
|
+
* @see https://github.com/NMMTY/LazyCanvas/blob/main/scripts/FontsGenerate.md
|
|
51
|
+
* @param fonts {Font[]} - The fonts to add to the manager.
|
|
52
|
+
* @returns {this} The current instance for chaining.
|
|
53
|
+
* @throws {LazyError} If required font properties are missing or the font already exists.
|
|
42
54
|
*/
|
|
43
55
|
add(...fonts) {
|
|
44
56
|
if (this.debug)
|
|
@@ -63,8 +75,9 @@ class FontsManager {
|
|
|
63
75
|
return this;
|
|
64
76
|
}
|
|
65
77
|
/**
|
|
66
|
-
*
|
|
67
|
-
* @param array {Array<{ family: string, weight: string }>} - The
|
|
78
|
+
* Removes fonts from the manager.
|
|
79
|
+
* @param array {Array<{ family: string, weight: string }>} - The family and weight of the fonts to remove.
|
|
80
|
+
* @returns {this} The current instance for chaining.
|
|
68
81
|
*/
|
|
69
82
|
remove(...array) {
|
|
70
83
|
for (const font of array) {
|
|
@@ -73,16 +86,18 @@ class FontsManager {
|
|
|
73
86
|
return this;
|
|
74
87
|
}
|
|
75
88
|
/**
|
|
76
|
-
*
|
|
89
|
+
* Clears all fonts from the manager.
|
|
90
|
+
* @returns {this} The current instance for chaining.
|
|
77
91
|
*/
|
|
78
92
|
clear() {
|
|
79
93
|
this.map.clear();
|
|
80
94
|
return this;
|
|
81
95
|
}
|
|
82
96
|
/**
|
|
83
|
-
*
|
|
84
|
-
* @param family {string} - The
|
|
85
|
-
* @param weight {string} - The
|
|
97
|
+
* Retrieves a font or fonts from the manager.
|
|
98
|
+
* @param family {string} - The family of the font to retrieve.
|
|
99
|
+
* @param weight {string} - The weight of the font to retrieve (optional).
|
|
100
|
+
* @returns {Font | Font[] | undefined} The retrieved font(s) or undefined if not found.
|
|
86
101
|
*/
|
|
87
102
|
get(family, weight) {
|
|
88
103
|
if (weight)
|
|
@@ -90,9 +105,10 @@ class FontsManager {
|
|
|
90
105
|
return Array.from(this.map.values()).filter(font => font.family === family);
|
|
91
106
|
}
|
|
92
107
|
/**
|
|
93
|
-
*
|
|
94
|
-
* @param family {string} - The
|
|
95
|
-
* @param weight {string} - The
|
|
108
|
+
* Checks if a font exists in the manager.
|
|
109
|
+
* @param family {string} - The family of the font to check.
|
|
110
|
+
* @param weight {string} - The weight of the font to check (optional).
|
|
111
|
+
* @returns {boolean} True if the font exists, false otherwise.
|
|
96
112
|
*/
|
|
97
113
|
has(family, weight) {
|
|
98
114
|
if (weight)
|
|
@@ -100,61 +116,70 @@ class FontsManager {
|
|
|
100
116
|
return Array.from(this.map.values()).some(font => font.family === family);
|
|
101
117
|
}
|
|
102
118
|
/**
|
|
103
|
-
*
|
|
119
|
+
* Retrieves the number of fonts in the manager.
|
|
120
|
+
* @returns {number} The size of the font map.
|
|
104
121
|
*/
|
|
105
122
|
size() {
|
|
106
123
|
return this.map.size;
|
|
107
124
|
}
|
|
108
125
|
/**
|
|
109
|
-
*
|
|
126
|
+
* Retrieves the values (fonts) from the manager.
|
|
127
|
+
* @returns {IterableIterator<Font>} An iterator for the font values.
|
|
110
128
|
*/
|
|
111
129
|
values() {
|
|
112
130
|
return this.map.values();
|
|
113
131
|
}
|
|
114
132
|
/**
|
|
115
|
-
*
|
|
133
|
+
* Retrieves the keys (family and weight) from the manager.
|
|
134
|
+
* @returns {IterableIterator<string>} An iterator for the font keys.
|
|
116
135
|
*/
|
|
117
136
|
keys() {
|
|
118
137
|
return this.map.keys();
|
|
119
138
|
}
|
|
120
139
|
/**
|
|
121
|
-
*
|
|
140
|
+
* Retrieves the entries (key-value pairs) from the manager.
|
|
141
|
+
* @returns {IterableIterator<[string, Font]>} An iterator for the font entries.
|
|
122
142
|
*/
|
|
123
143
|
entries() {
|
|
124
144
|
return this.map.entries();
|
|
125
145
|
}
|
|
126
146
|
/**
|
|
127
|
-
*
|
|
128
|
-
* @param callbackfn {Function} - The function to execute on each font
|
|
129
|
-
* @param thisArg {any} - The `this` context to use
|
|
147
|
+
* Iterates over the fonts in the manager.
|
|
148
|
+
* @param callbackfn {Function} - The function to execute on each font.
|
|
149
|
+
* @param thisArg {any} - The `this` context to use (optional).
|
|
150
|
+
* @returns {this} The current instance for chaining.
|
|
130
151
|
*/
|
|
131
152
|
forEach(callbackfn, thisArg) {
|
|
132
153
|
this.map.forEach(callbackfn, thisArg);
|
|
133
154
|
return this;
|
|
134
155
|
}
|
|
135
156
|
/**
|
|
136
|
-
*
|
|
157
|
+
* Converts the font map to a JSON object.
|
|
158
|
+
* @returns {object} The JSON representation of the font map.
|
|
137
159
|
*/
|
|
138
160
|
toJSON() {
|
|
139
161
|
return Object.fromEntries(this.map);
|
|
140
162
|
}
|
|
141
163
|
/**
|
|
142
|
-
*
|
|
143
|
-
* @param json {object} - The JSON object to
|
|
164
|
+
* Populates the font map from a JSON object.
|
|
165
|
+
* @param json {object} - The JSON object to populate the map from.
|
|
166
|
+
* @returns {this} The current instance for chaining.
|
|
144
167
|
*/
|
|
145
168
|
fromJSON(json) {
|
|
146
169
|
this.map = new Map(Object.entries(json));
|
|
147
170
|
return this;
|
|
148
171
|
}
|
|
149
172
|
/**
|
|
150
|
-
*
|
|
173
|
+
* Converts the font map to an array.
|
|
174
|
+
* @returns {Font[]} An array of fonts.
|
|
151
175
|
*/
|
|
152
176
|
toArray() {
|
|
153
177
|
return Array.from(this.map.values());
|
|
154
178
|
}
|
|
155
179
|
/**
|
|
156
|
-
*
|
|
157
|
-
* @param array {Font[]} - The
|
|
180
|
+
* Populates the font map from an array of fonts.
|
|
181
|
+
* @param array {Font[]} - The array of fonts to populate the map from.
|
|
182
|
+
* @returns {this} The current instance for chaining.
|
|
158
183
|
*/
|
|
159
184
|
fromArray(array) {
|
|
160
185
|
for (const font of array) {
|