textmode.js 0.1.6-beta.5 → 0.1.6-beta.6

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.
Files changed (29) hide show
  1. package/dist/textmode.esm.js +1845 -0
  2. package/dist/textmode.esm.min.js +1844 -0
  3. package/dist/textmode.umd.js +26 -0
  4. package/dist/textmode.umd.min.js +25 -0
  5. package/dist/types/ColorPalette.d.ts +1 -5
  6. package/dist/types/Textmode.d.ts +0 -10
  7. package/dist/types/export/image/ImageDataExtractor.d.ts +1 -1
  8. package/dist/types/export/image/ImageExporter.d.ts +1 -1
  9. package/dist/types/export/image/ImageFileHandler.d.ts +1 -7
  10. package/dist/types/export/svg/SVGContentGenerator.d.ts +1 -1
  11. package/dist/types/export/svg/SVGDataExtractor.d.ts +1 -1
  12. package/dist/types/export/svg/SVGPathGenerator.d.ts +0 -5
  13. package/dist/types/export/txt/TXTDataExtractor.d.ts +1 -1
  14. package/dist/types/index.d.ts +0 -1
  15. package/dist/types/textmode/Canvas.d.ts +0 -11
  16. package/dist/types/textmode/ConversionPipeline.d.ts +12 -9
  17. package/dist/types/textmode/Grid.d.ts +1 -6
  18. package/dist/types/textmode/Textmodifier.d.ts +5 -25
  19. package/dist/types/textmode/converters/FeatureConverter.d.ts +24 -0
  20. package/dist/types/textmode/mixins/ConversionMixin.d.ts +63 -0
  21. package/dist/types/textmode/mixins/ExportMixin.d.ts +112 -1
  22. package/dist/types/textmode/mixins/FontMixin.d.ts +1 -1
  23. package/dist/types/textmode/mixins/RenderingMixin.d.ts +304 -1
  24. package/dist/types/textmode/mixins/{MixinBase.d.ts → TextmodifierMixin.d.ts} +4 -4
  25. package/dist/types/textmode/mixins/index.d.ts +2 -1
  26. package/package.json +16 -2
  27. package/dist/textmode.js +0 -26
  28. package/dist/textmode.min.js +0 -25
  29. package/dist/types/types/index.d.ts +0 -4
@@ -1,6 +1,6 @@
1
1
  import type { Framebuffer } from "../rendering/webgl/Framebuffer";
2
2
  import type { GLRenderer } from "../rendering/webgl/Renderer";
3
- import { TextmodeConverter } from "./converters";
3
+ import { TextmodeBrightnessConverter, TextmodeConverter } from "./converters";
4
4
  import type { TextmodeFont } from "./font";
5
5
  import type { TextmodeGrid } from "./Grid";
6
6
  /**
@@ -15,6 +15,10 @@ export declare class TextmodeConversionPipeline {
15
15
  private font;
16
16
  private grid;
17
17
  private converters;
18
+ /** Pre-configured brightness converter for easy access */
19
+ readonly brightness: TextmodeBrightnessConverter;
20
+ /** Pre-configured custom converter for easy access */
21
+ readonly custom: TextmodeConverter;
18
22
  private _resultFramebuffer;
19
23
  private _asciiShader;
20
24
  private _characterFramebuffer;
@@ -37,23 +41,22 @@ export declare class TextmodeConversionPipeline {
37
41
  */
38
42
  render(sourceFramebuffer: Framebuffer): void;
39
43
  /**
40
- * Get a specific converter by name.
41
- * @param name The name of the converter to retrieve.
42
- * @returns The requested `TextmodeConverter` instance.
44
+ * Adds a new converter to the pipeline.
45
+ * @param converter The converter instance to add.
46
+ * @returns The added {@link TextmodeConverter} instance.
43
47
  */
44
- get(name: string): TextmodeConverter | void;
45
48
  /**
46
49
  * Adds a new converter to the pipeline.
47
50
  * @param name A unique name for the converter.
48
51
  * @param type The type of converter to add. Can be either "brightness" or "custom".
49
52
  * @returns The newly created {@link TextmodeConverter} instance or `void` if the addition failed.
50
53
  */
51
- add(name: string, type: "brightness" | "custom"): TextmodeConverter | void;
54
+ add(type: "brightness" | "custom"): TextmodeConverter | void;
52
55
  /**
53
- * Removes a converter from the pipeline by name or instance.
54
- * @param nameOrInstance The unique name of the converter or the converter instance to remove.
56
+ * Removes a converter from the pipeline.
57
+ * @param converter The converter instance to remove.
55
58
  */
56
- remove(nameOrInstance: string | TextmodeConverter): void;
59
+ remove(converter: TextmodeConverter): void;
57
60
  /**
58
61
  * Returns the framebuffer containing the textmode conversion result.
59
62
  */
@@ -38,7 +38,7 @@ export declare class TextmodeGrid {
38
38
  /**
39
39
  * Reset the total grid width & height, and the offset to the outer canvas.
40
40
  */
41
- private _resizeGrid;
41
+ private _updateDimensions;
42
42
  /**
43
43
  * Re-assign the grid cell dimensions and `reset()` the grid.
44
44
  * @param newCellWidth The new cell width.
@@ -77,11 +77,6 @@ export declare class TextmodeGrid {
77
77
  get cellWidth(): number;
78
78
  /** Returns the height of each cell in the grid. */
79
79
  get cellHeight(): number;
80
- /**
81
- * Dispose of this TextmodeGrid and clean up references.
82
- * This method is idempotent and safe to call multiple times.
83
- */
84
- dispose(): void;
85
80
  /** Returns the number of columns in the grid. */
86
81
  get cols(): number;
87
82
  /** Returns the number of rows in the grid. */
@@ -3,11 +3,11 @@ import { TextmodeFont } from './font';
3
3
  import { TextmodeGrid } from './Grid';
4
4
  import { TextmodeCanvas } from './Canvas';
5
5
  import { TextmodeConversionPipeline } from './ConversionPipeline';
6
- import type { TextmodeConverter } from './converters';
7
- import { type TextmodifierCore } from './mixins';
6
+ import { type TextmodifierContext } from './mixins';
8
7
  import type { RenderingCapabilities } from './mixins/RenderingMixin';
9
8
  import type { ExportCapabilities } from './mixins/ExportMixin';
10
9
  import type { FontCapabilities } from './mixins/FontMixin';
10
+ import type { ConversionCapabilities } from './mixins/ConversionMixin';
11
11
  /**
12
12
  * Supported capture sources for textmode rendering
13
13
  */
@@ -40,14 +40,14 @@ export type TextmodeOptions = {
40
40
  /**
41
41
  * Base class for mixin application
42
42
  */
43
- declare class TextmodifierBase implements TextmodifierCore {
43
+ declare class TextmodifierCore implements TextmodifierContext {
44
44
  _renderer: GLRenderer;
45
45
  _font: TextmodeFont;
46
46
  _pipeline: TextmodeConversionPipeline;
47
47
  textmodeCanvas: TextmodeCanvas;
48
48
  _grid: TextmodeGrid;
49
49
  }
50
- declare const Textmodifier_base: typeof TextmodifierBase;
50
+ declare const Textmodifier_base: typeof TextmodifierCore;
51
51
  /**
52
52
  * Manages textmode rendering on a canvas or video element.
53
53
  *
@@ -359,26 +359,6 @@ export declare class Textmodifier extends Textmodifier_base {
359
359
  * @param height The new height of the canvas.
360
360
  */
361
361
  resizeCanvas(width: number, height: number): void;
362
- /**
363
- * @inheritDoc TextmodeConversionPipeline.get
364
- *
365
- * @example
366
- * ```javascript
367
- * // Fetch a canvas element to apply textmode rendering to
368
- * const canvas = document.querySelector('canvas#myCanvas');
369
- *
370
- * // Create a Textmodifier instance
371
- * const textmodifier = await textmode.create(canvas);
372
- *
373
- * // Get the pre-defined brightness converter from the pipeline
374
- * const brightnessConverter = textmodifier.converter('brightness');
375
- *
376
- * // Update properties of the brightness converter
377
- * brightnessConverter.invert(true);
378
- * brightnessConverter.characters(" .,;:*");
379
- * ```
380
- */
381
- converter(name: string): TextmodeConverter | void;
382
362
  /**
383
363
  * Completely destroy this Textmodifier instance and free all associated resources.
384
364
  *
@@ -427,6 +407,6 @@ export declare class Textmodifier extends Textmodifier_base {
427
407
  /** Check if the instance has been disposed/destroyed. */
428
408
  get isDisposed(): boolean;
429
409
  }
430
- export interface Textmodifier extends RenderingCapabilities, ExportCapabilities, FontCapabilities {
410
+ export interface Textmodifier extends RenderingCapabilities, ExportCapabilities, FontCapabilities, ConversionCapabilities {
431
411
  }
432
412
  export {};
@@ -73,6 +73,30 @@ export declare abstract class TextmodeFeatureConverter extends TextmodeConverter
73
73
  * @param flip If `true`, characters are flipped vertically. If `false`, no flip is applied.
74
74
  */
75
75
  flipVertically(flip: boolean | number): void;
76
+ /**
77
+ * Helper method to parse color values from either hex string or RGB(A) parameters.
78
+ * @param r Red component (0-255) or hex string.
79
+ * @param methodName The name of the calling method for error reporting.
80
+ * @param g Green component (0-255).
81
+ * @param b Blue component (0-255).
82
+ * @param a Alpha component (0-255).
83
+ * @returns Normalized RGBA array [r, g, b, a] (0-1 range) or null if invalid.
84
+ */
85
+ private parseColor;
86
+ /**
87
+ * Helper method to set color mode with validation.
88
+ * @param mode The color mode to set.
89
+ * @param optionKey The option key to set in _options.
90
+ * @param errorMessage The error message to show if validation fails.
91
+ */
92
+ private setColorMode;
93
+ /**
94
+ * Helper method to set boolean options with validation.
95
+ * @param value The boolean or number value to set.
96
+ * @param optionKey The option key to set in _options.
97
+ * @param displayName The display name for error messages.
98
+ */
99
+ private setBooleanOption;
76
100
  /**
77
101
  * Parses a hex color string and returns RGBA values.
78
102
  * @param hex Hex color string (e.g., '#FF0000', '#F00', 'FF0000', 'F00').
@@ -0,0 +1,63 @@
1
+ import type { Mixin } from './TextmodifierMixin';
2
+ import type { TextmodeConverter } from '../converters';
3
+ /**
4
+ * Interface for conversion pipeline capabilities that will be mixed into Textmodifier
5
+ */
6
+ export interface ConversionCapabilities {
7
+ /**
8
+ * Adds a new converter to the pipeline.
9
+ * @param name A unique name for the converter.
10
+ * @param type The type of converter to add. Can be either "brightness" or "custom".
11
+ * @returns The newly created {@link TextmodeConverter} instance or `void` if the addition failed.
12
+ *
13
+ * @example
14
+ * ```javascript
15
+ * // Fetch a canvas element to apply textmode rendering to
16
+ * const canvas = document.querySelector('canvas#myCanvas');
17
+ *
18
+ * // Create a Textmodifier instance
19
+ * const textmodifier = await textmode.create(canvas);
20
+ *
21
+ * // Add a new brightness converter
22
+ * const myConverter = textmodifier.addConverter('my-brightness', 'brightness');
23
+ *
24
+ * // Configure the new converter
25
+ * myConverter.characters("▓▒░ ");
26
+ * myConverter.enabled(true);
27
+ *
28
+ * // Add a custom converter
29
+ * const customConverter = textmodifier.addConverter('my-custom', 'custom');
30
+ * ```
31
+ */
32
+ addConverter(name: string, type: 'brightness' | 'custom'): TextmodeConverter | void;
33
+ /**
34
+ * Removes a converter from the pipeline by name or instance.
35
+ * @param nameOrInstance The unique name of the converter or the converter instance to remove.
36
+ *
37
+ * @example
38
+ * ```javascript
39
+ * // Fetch a canvas element to apply textmode rendering to
40
+ * const canvas = document.querySelector('canvas#myCanvas');
41
+ *
42
+ * // Create a Textmodifier instance
43
+ * const textmodifier = await textmode.create(canvas);
44
+ *
45
+ * // Add a converter
46
+ * const myConverter = textmodifier.addConverter('temp-converter', 'brightness');
47
+ *
48
+ * // Remove by name
49
+ * textmodifier.removeConverter('temp-converter');
50
+ *
51
+ * // Or remove by instance
52
+ * const anotherConverter = textmodifier.addConverter('another', 'custom');
53
+ * textmodifier.removeConverter(anotherConverter);
54
+ * ```
55
+ */
56
+ removeConverter(nameOrInstance: string | TextmodeConverter): void;
57
+ }
58
+ /**
59
+ * Mixin that adds conversion pipeline capabilities to a class by delegating to TextmodeConversionPipeline
60
+ * @param Base The base class to extend
61
+ * @returns Extended class with conversion capabilities
62
+ */
63
+ export declare const ConversionMixin: Mixin<ConversionCapabilities>;
@@ -1,4 +1,4 @@
1
- import type { Mixin } from './MixinBase';
1
+ import type { Mixin } from './TextmodifierMixin';
2
2
  import { type SVGExportOptions } from '../../export/svg';
3
3
  import { type TXTExportOptions } from '../../export/txt';
4
4
  import { type ImageExportOptions, type ImageFormat } from '../../export/image';
@@ -10,22 +10,111 @@ export interface ExportCapabilities {
10
10
  * Generate the current textmode rendering as a text string.
11
11
  * @param options Options for text generation *(excluding filename)*
12
12
  * @returns Textmode grid content as a string.
13
+ *
14
+ * @example
15
+ * ```javascript
16
+ * // Fetch a canvas element to apply textmode rendering to
17
+ * const canvas = document.querySelector('canvas#myCanvas');
18
+ *
19
+ * // Create a Textmodifier instance
20
+ * const textmodifier = await textmode.create(canvas, {renderMode: 'manual'});
21
+ *
22
+ * // Render a single frame
23
+ * textmodifier.render();
24
+ *
25
+ * // Get the current rendering as a text string
26
+ * const textString = textmodifier.toString({
27
+ * preserveTrailingSpaces: false,
28
+ * lineEnding: 'lf'
29
+ * });
30
+ *
31
+ * // Print to console or use otherwise
32
+ * console.log(textString);
33
+ *
34
+ * ////////
35
+ *
36
+ * // Example with video element
37
+ * const video = document.querySelector('video#myVideo');
38
+ * const videoTextmodifier = await textmode.create(video);
39
+ *
40
+ * // The textmode overlay will automatically update as the video plays
41
+ * video.play();
42
+ *
43
+ * // Get current frame as ASCII
44
+ * const videoFrame = videoTextmodifier.toString();
45
+ * ```
13
46
  */
14
47
  toString(options?: Omit<TXTExportOptions, 'filename'>): string;
15
48
  /**
16
49
  * Export the current textmode rendering to a TXT file.
17
50
  * @param options Options for TXT export
51
+ *
52
+ * @example
53
+ * ```javascript
54
+ * // Fetch a canvas element to apply textmode rendering to
55
+ * const canvas = document.querySelector('canvas#myCanvas');
56
+ *
57
+ * // Create a Textmodifier instance
58
+ * const textmodifier = await textmode.create(canvas, {renderMode: 'manual'});
59
+ *
60
+ * // Render a single frame
61
+ * textmodifier.render();
62
+ *
63
+ * // Export the current rendering to a TXT file
64
+ * textmodifier.saveStrings({
65
+ * filename: 'my_textmode_rendering',
66
+ * preserveTrailingSpaces: false
67
+ * });
68
+ * ```
18
69
  */
19
70
  saveStrings(options?: TXTExportOptions): void;
20
71
  /**
21
72
  * Generate the current textmode rendering as an SVG string.
22
73
  * @param options Options for SVG generation *(excluding filename)*
23
74
  * @returns SVG content as a string.
75
+ *
76
+ * @example
77
+ * ```javascript
78
+ * // Fetch a canvas element to apply textmode rendering to
79
+ * const canvas = document.querySelector('canvas#myCanvas');
80
+ *
81
+ * // Create a Textmodifier instance
82
+ * const textmodifier = await textmode.create(canvas, {renderMode: 'manual'});
83
+ *
84
+ * // Render a single frame
85
+ * textmodifier.render();
86
+ *
87
+ * // Get the current rendering as an SVG string
88
+ * const svgString = textmodifier.toSVG({
89
+ * includeBackgroundRectangles: true,
90
+ * drawMode: 'fill'
91
+ * });
92
+ *
93
+ * // Print to console or use otherwise
94
+ * console.log(svgString);
95
+ * ```
24
96
  */
25
97
  toSVG(options?: Omit<SVGExportOptions, 'filename'>): string;
26
98
  /**
27
99
  * Export the current textmode rendering to an SVG file.
28
100
  * @param options Options for SVG export
101
+ *
102
+ * @example
103
+ * ```javascript
104
+ * // Fetch a canvas element to apply textmode rendering to
105
+ * const canvas = document.querySelector('canvas#myCanvas');
106
+ *
107
+ * // Create a Textmodifier instance
108
+ * const textmodifier = await textmode.create(canvas, {renderMode: 'manual'});
109
+ *
110
+ * // Render a single frame
111
+ * textmodifier.render();
112
+ *
113
+ * // Export the current rendering to an SVG file
114
+ * textmodifier.saveSVG({
115
+ * filename: 'my_textmode_rendering',
116
+ * });
117
+ * ```
29
118
  */
30
119
  saveSVG(options?: SVGExportOptions): void;
31
120
  /**
@@ -33,6 +122,28 @@ export interface ExportCapabilities {
33
122
  * @param filename The filename (without extension) to save the image as
34
123
  * @param format The image format ('png', 'jpg', or 'webp')
35
124
  * @param options Additional options for image export
125
+ *
126
+ * @example
127
+ * ```javascript
128
+ * // Fetch a canvas element to apply textmode rendering to
129
+ * const canvas = document.querySelector('canvas#myCanvas');
130
+ *
131
+ * // Create a Textmodifier instance
132
+ * const textmodifier = await textmode.create(canvas, {renderMode: 'manual'});
133
+ *
134
+ * // Render a single frame
135
+ * textmodifier.render();
136
+ *
137
+ * // Export the current rendering to a PNG file
138
+ * textmodifier.saveCanvas('my_textmode_rendering', 'png');
139
+ *
140
+ * // Export with custom options
141
+ * textmodifier.saveCanvas('my_textmode_rendering', 'jpg', {
142
+ * quality: 0.8,
143
+ * scale: 2.0,
144
+ * backgroundColor: 'white'
145
+ * });
146
+ * ```
36
147
  */
37
148
  saveCanvas(filename: string, format?: ImageFormat, options?: Omit<ImageExportOptions, 'filename' | 'format'>): Promise<void>;
38
149
  }
@@ -1,4 +1,4 @@
1
- import type { Mixin } from './MixinBase';
1
+ import type { Mixin } from './TextmodifierMixin';
2
2
  /**
3
3
  * Interface for font capabilities that will be mixed into Textmodifier
4
4
  */