@mlightcad/mtext-renderer 0.2.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.
@@ -0,0 +1,128 @@
1
+ import { BaseFont } from './baseFont';
2
+ import { BaseTextShape } from './baseTextShape';
3
+ import { EventManager } from '../common';
4
+ import { FontLoadStatus } from './fontLoader';
5
+ import { FontType } from './font';
6
+
7
+ /**
8
+ * Font mappings configuration.
9
+ * Maps original font names to their replacement font names.
10
+ * - The key is the original font name
11
+ * - The value is the mapped font name
12
+ */
13
+ export type FontMapping = Record<string, string>;
14
+ /**
15
+ * Event arguments for font-related events.
16
+ */
17
+ export interface FontManagerEventArgs {
18
+ /** Name of font which can't be found */
19
+ fontName: string;
20
+ /** The number of characters which use this font. This is only used when the font is not found. */
21
+ count?: number;
22
+ }
23
+ /**
24
+ * Manages font loading, caching, and text rendering.
25
+ * This class is responsible for:
26
+ * - Loading fonts from URLs or cache
27
+ * - Managing font mappings and replacements
28
+ * - Providing text shapes for rendering
29
+ * - Tracking unsupported characters and missed fonts
30
+ */
31
+ export declare class FontManager {
32
+ private static _instance;
33
+ /** THREE.js file loader for loading font files */
34
+ private loader;
35
+ /** Mapping of original font names to their replacements */
36
+ protected fontMapping: FontMapping;
37
+ /** Map of loaded fonts, keyed by font name */
38
+ protected fontMap: Map<string, BaseFont>;
39
+ /** List of font file names that have been loaded */
40
+ protected fileNames: string[];
41
+ /** Record of characters that are not supported by any loaded font */
42
+ unsupportedChars: Record<string, number>;
43
+ /** Record of fonts that were requested but not found */
44
+ missedFonts: Record<string, number>;
45
+ /** Flag to enable/disable font caching */
46
+ enableFontCache: boolean;
47
+ /** Default font to use when a requested font is not found */
48
+ defaultFont: string;
49
+ /** Event managers for font-related events */
50
+ readonly events: {
51
+ /** Event triggered when a font cannot be found */
52
+ fontNotFound: EventManager<FontManagerEventArgs>;
53
+ /** Event triggered when a font is successfully loaded */
54
+ fontLoaded: EventManager<FontManagerEventArgs>;
55
+ };
56
+ private constructor();
57
+ /**
58
+ * Gets the singleton instance of the FontManager
59
+ * @returns The FontManager instance
60
+ */
61
+ static get instance(): FontManager;
62
+ /**
63
+ * Sets the font mapping configuration
64
+ * @param mapping - The font mapping to set
65
+ */
66
+ setFontMapping(mapping: FontMapping): void;
67
+ /**
68
+ * Loads the specified fonts from URLs
69
+ * @param urls - URLs of font files to load. The order represents the priority
70
+ * @returns Promise that resolves to an array of font load statuses
71
+ */
72
+ loadFonts(urls: string | string[]): Promise<FontLoadStatus[]>;
73
+ /**
74
+ * Tries to find the specified font. If not found, uses a replacement font and returns its name.
75
+ * @param fontName - The font name to find
76
+ * @returns The original font name if found, or the replacement font name if not found
77
+ */
78
+ findAndReplaceFont(fontName: string): string;
79
+ /**
80
+ * Gets the text shape for a specific character with the specified font and size
81
+ * @param char - The character to get the shape for
82
+ * @param fontName - The name of the font to use
83
+ * @param size - The size of the character
84
+ * @returns The text shape for the character, or undefined if not found
85
+ */
86
+ getCharShape(char: string, fontName: string, size: number): BaseTextShape | undefined;
87
+ /**
88
+ * Gets the scale factor for a specific font
89
+ * @param fontName - The name of the font
90
+ * @returns The scale factor for the font, or 1 if the font is not found
91
+ */
92
+ getFontScaleFactor(fontName: string): number;
93
+ /**
94
+ * Gets type of the specific font
95
+ * @param fontName - The name of the font
96
+ * @returns The type of the font. If the specified font can't be found, `undefined` is returned
97
+ */
98
+ getFontType(fontName: string): FontType | undefined;
99
+ /**
100
+ * Gets the shape to display when a character is not found
101
+ * @param size - The size of the shape
102
+ * @returns The shape for the not found indicator, or undefined if not available
103
+ */
104
+ getNotFoundTextShape(size: number): BaseTextShape | undefined;
105
+ /**
106
+ * Records a font that was requested but not found
107
+ * @param fontName - The name of the font that was not found
108
+ */
109
+ private recordMissedFonts;
110
+ /**
111
+ * Loads a single font from a URL
112
+ * @param url - The URL of the font file to load
113
+ */
114
+ private loadFont;
115
+ /**
116
+ * Loads all fonts from the cache
117
+ */
118
+ getAllFontsFromCache(): Promise<void>;
119
+ /**
120
+ * Gets a record of all unsupported characters across all loaded fonts
121
+ * @returns A record mapping unsupported characters to their occurrence count
122
+ */
123
+ getUnsupportedChar(): Record<string, number>;
124
+ /**
125
+ * Releases all loaded fonts and clears the font map
126
+ */
127
+ release(): void;
128
+ }
@@ -0,0 +1,6 @@
1
+ export * from './baseFont';
2
+ export * from './baseTextShape';
3
+ export * from './font';
4
+ export * from './fontFactory';
5
+ export * from './fontLoader';
6
+ export * from './fontManager';
@@ -0,0 +1,59 @@
1
+ import { Font, FontData } from 'three/examples/jsm/loaders/FontLoader.js';
2
+ import { BaseFont } from './baseFont';
3
+ import { MeshTextShape } from './meshTextShape';
4
+
5
+ /**
6
+ * Represents the data structure for mesh-based fonts.
7
+ * Extends the base FontData interface with additional properties specific to mesh fonts.
8
+ */
9
+ export interface MeshFontData extends FontData {
10
+ /** Scale factor used to adjust the size of characters when rendering */
11
+ scaleFactor: number;
12
+ }
13
+ /**
14
+ * Represents a mesh-based font (e.g., TTF, OTF, WOFF).
15
+ * This class extends BaseFont to provide specific functionality for mesh fonts,
16
+ * including character shape generation and scale factor management.
17
+ */
18
+ export declare class MeshFont extends BaseFont {
19
+ /** Scale factor used to adjust the size of characters */
20
+ protected scaleFactor?: number;
21
+ /** Three.js font instance used for rendering */
22
+ readonly font: Font;
23
+ /** The type of font (always 'mesh' for this class) */
24
+ readonly type = "mesh";
25
+ /** The parsed font data */
26
+ readonly data: MeshFontData;
27
+ /**
28
+ * Creates a new instance of MeshFont.
29
+ * @param data - Either a MeshFontData object containing font information or an ArrayBuffer containing raw font data
30
+ */
31
+ constructor(data: MeshFontData | ArrayBuffer);
32
+ /**
33
+ * Generates shapes for a text string
34
+ * @param text - The text to generate shapes for
35
+ * @param size - The size of the text
36
+ * @returns Array of shapes representing the text
37
+ */
38
+ generateShapes(text: string, size: number): import('three').Shape[];
39
+ /**
40
+ * Gets the shape data for a specific character at a given size.
41
+ * @param char - The character to get the shape for
42
+ * @param size - The desired size of the character
43
+ * @returns The shape data for the character, or undefined if not found
44
+ */
45
+ getCharShape(char: string, size: number): MeshTextShape | undefined;
46
+ /**
47
+ * Gets the scale factor for this font.
48
+ * This is used to adjust the size of characters when rendering.
49
+ * @returns The scale factor as a number
50
+ */
51
+ getScaleFactor(): number;
52
+ /**
53
+ * Gets the shape to display when a character is not found in the font.
54
+ * Uses "?" as a replacement character.
55
+ * @param size - The desired size of the not found shape
56
+ * @returns The shape data for the not found indicator
57
+ */
58
+ getNotFoundTextShape(size: number): MeshTextShape;
59
+ }
@@ -0,0 +1,11 @@
1
+ import { MeshFontData } from './meshFont';
2
+
3
+ /**
4
+ * Parses a mesh font from raw binary data.
5
+ * This function converts raw font data (e.g., TTF, OTF, WOFF) into a MeshFontData object
6
+ * that can be used by the MeshFont class.
7
+ *
8
+ * @param data - The raw font data as an ArrayBuffer
9
+ * @returns A MeshFontData object containing the parsed font information
10
+ */
11
+ export declare function parseMeshFont(data: ArrayBuffer): MeshFontData;
@@ -0,0 +1,32 @@
1
+ import { BaseTextShape } from './baseTextShape';
2
+ import { MeshFont } from './meshFont';
3
+ import * as THREE from 'three';
4
+ /**
5
+ * Represents a text shape for mesh-based fonts (e.g., TTF, OTF, WOFF).
6
+ * This class extends BaseTextShape to provide specific functionality for mesh fonts,
7
+ * including 3D geometry generation and character width calculation.
8
+ */
9
+ export declare class MeshTextShape extends BaseTextShape {
10
+ /**
11
+ * Flag to indicate whether the character is found in the font.
12
+ * Used to track if the character exists in the font's glyph set.
13
+ */
14
+ isFound: boolean;
15
+ private readonly font;
16
+ private readonly fontSize;
17
+ constructor(char: string, fontSize: number, font: MeshFont);
18
+ /**
19
+ * Converts the text shape to a THREE.js geometry.
20
+ * This is used for 3D rendering of the text.
21
+ * @returns A THREE.js BufferGeometry representing the text shape
22
+ */
23
+ toGeometry(): THREE.BufferGeometry;
24
+ /**
25
+ * Calculates the width of a character in the font.
26
+ * @param char - The character to calculate width for
27
+ * @param fontSize - The size of the font in pixels
28
+ * @param font - The mesh font to use
29
+ * @returns The width of the character in pixels
30
+ */
31
+ private getCharWidth;
32
+ }
@@ -0,0 +1,33 @@
1
+ import { ShxFontData } from '@mlightcad/shx-parser';
2
+ import { BaseFont } from './baseFont';
3
+ import { ShxTextShape } from './shxTextShape';
4
+
5
+ /**
6
+ * ShxFont is a class that extends BaseFont and represents a SHX font.
7
+ * It provides methods to generate shapes for text and retrieve character shapes.
8
+ */
9
+ export declare class ShxFont extends BaseFont {
10
+ /** Internal shx font instance */
11
+ private readonly font;
12
+ readonly type = "shx";
13
+ readonly data: ShxFontData;
14
+ constructor(data: ShxFontData | ArrayBuffer);
15
+ generateShapes(text: string, size: number): ShxTextShape[];
16
+ /**
17
+ * SHX font always has fixed scale factor 1.
18
+ * @returns Always return value 1
19
+ */
20
+ getScaleFactor(): number;
21
+ /**
22
+ * Gets the shape data for a specific character at a given size.
23
+ * @param char - The character to get the shape for
24
+ * @param size - The desired size of the character
25
+ * @returns The shape data for the character, or undefined if not found
26
+ */
27
+ getCharShape(char: string, size: number): ShxTextShape | undefined;
28
+ /**
29
+ * For an unsupported char, use "?" as a replacement.
30
+ */
31
+ getNotFoundTextShape(size: number): ShxTextShape | undefined;
32
+ private getCode;
33
+ }
@@ -0,0 +1,26 @@
1
+ import { Point, ShxShape } from '@mlightcad/shx-parser';
2
+ import { BaseTextShape } from './baseTextShape';
3
+
4
+ import * as THREE from 'three';
5
+ /**
6
+ * Represents a text shape for SHX fonts.
7
+ * This class extends BaseTextShape to provide specific functionality for SHX fonts,
8
+ * including shape data management and dimension calculations.
9
+ */
10
+ export declare class ShxTextShape extends BaseTextShape {
11
+ /** The shape data for this character */
12
+ private readonly shape;
13
+ /**
14
+ * Creates a new instance of ShxTextShape
15
+ * @param char - The character this shape represents
16
+ * @param shape - The shape data for this character
17
+ */
18
+ constructor(char: string, shape: ShxShape);
19
+ protected calcWidth(): number;
20
+ offset(offset: Point): ShxTextShape;
21
+ /**
22
+ * Converts the text shape to a THREE.js geometry
23
+ * @returns A THREE.js BufferGeometry representing the text shape
24
+ */
25
+ toGeometry(): THREE.BufferGeometry<THREE.NormalBufferAttributes>;
26
+ }
@@ -0,0 +1,3 @@
1
+ export * from './common/eventManager';
2
+ export * from './font';
3
+ export * from './renderer';