@mlightcad/mtext-renderer 0.8.8 → 0.8.9
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/lib/cache/fontCacheManager.d.ts +71 -0
- package/lib/cache/index.d.ts +1 -0
- package/lib/cache/schema.d.ts +27 -0
- package/lib/common/eventManager.d.ts +26 -0
- package/lib/common/index.d.ts +2 -0
- package/lib/common/utils.d.ts +11 -0
- package/lib/font/baseFont.d.ts +81 -0
- package/lib/font/baseTextShape.d.ts +17 -0
- package/lib/font/charGeometryCache.d.ts +43 -0
- package/lib/font/defaultFontLoader.d.ts +50 -0
- package/lib/font/font.d.ts +25 -0
- package/lib/font/fontFactory.d.ts +31 -0
- package/lib/font/fontLoader.d.ts +58 -0
- package/lib/font/fontManager.d.ts +192 -0
- package/lib/font/index.d.ts +7 -0
- package/lib/font/meshFont.d.ts +138 -0
- package/lib/font/meshFontParser.d.ts +10 -0
- package/lib/font/meshTextShape.d.ts +33 -0
- package/lib/font/normalComputationToggle.d.ts +57 -0
- package/lib/font/shxFont.d.ts +62 -0
- package/lib/font/shxTextShape.d.ts +29 -0
- package/lib/font/threeFont.d.ts +50 -0
- package/lib/index.d.ts +4 -0
- package/lib/renderer/defaultStyleManager.d.ts +18 -0
- package/lib/renderer/index.d.ts +3 -0
- package/lib/renderer/mtext.d.ts +135 -0
- package/lib/renderer/mtextProcessor.d.ts +232 -0
- package/lib/renderer/styleManager.d.ts +20 -0
- package/lib/renderer/types.d.ts +114 -0
- package/lib/worker/baseRenderer.d.ts +91 -0
- package/lib/worker/index.d.ts +5 -0
- package/lib/worker/mainThreadRenderer.d.ts +49 -0
- package/lib/worker/mtextWorker.d.ts +1 -0
- package/lib/worker/unifiedRenderer.d.ts +69 -0
- package/lib/worker/webWorkerRenderer.d.ts +164 -0
- package/package.json +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { FontData } from '../font';
|
|
2
|
+
/**
|
|
3
|
+
* A simple font cache interface that provides map-like operations for font data
|
|
4
|
+
*/
|
|
5
|
+
export declare class FontCacheManager {
|
|
6
|
+
private static readonly DATABASE_NAME;
|
|
7
|
+
private static readonly DATABASE_VERSION;
|
|
8
|
+
private static _instance;
|
|
9
|
+
private db;
|
|
10
|
+
private isClosing;
|
|
11
|
+
private constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Returns the singleton instance of the FontCacheManager
|
|
14
|
+
*/
|
|
15
|
+
static get instance(): FontCacheManager;
|
|
16
|
+
/**
|
|
17
|
+
* Sets a font in the cache
|
|
18
|
+
* @param fileName The font file name (key)
|
|
19
|
+
* @param fontData The font data to store
|
|
20
|
+
*/
|
|
21
|
+
set(fileName: string, fontData: FontData): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Gets a font from the cache
|
|
24
|
+
* @param fileName The font file name (key)
|
|
25
|
+
* @returns The font data if found, undefined otherwise
|
|
26
|
+
*/
|
|
27
|
+
get(fileName: string): Promise<FontData | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Deletes a font from the cache
|
|
30
|
+
* @param fileName The font file name (key)
|
|
31
|
+
*/
|
|
32
|
+
delete(fileName: string): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Gets all fonts from the cache
|
|
35
|
+
* @returns An array of all font data in the cache
|
|
36
|
+
*/
|
|
37
|
+
getAll(): Promise<FontData[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Clears all fonts from the cache
|
|
40
|
+
*/
|
|
41
|
+
clear(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if a font exists in the cache
|
|
44
|
+
* @param fileName The font file name (key)
|
|
45
|
+
*/
|
|
46
|
+
has(fileName: string): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Closes the database connection and cleans up resources.
|
|
49
|
+
* After calling this, any further operations will require reopening the database.
|
|
50
|
+
*/
|
|
51
|
+
close(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Destroys the database instance and deletes all data.
|
|
54
|
+
* Use with caution as this operation cannot be undone.
|
|
55
|
+
*/
|
|
56
|
+
destroy(): Promise<void>;
|
|
57
|
+
private getDatabase;
|
|
58
|
+
/**
|
|
59
|
+
* Applies all schema versions that are greater than the old version and less than or equal to the new version
|
|
60
|
+
* @param db The database instance
|
|
61
|
+
* @param oldVersion The old version of the database
|
|
62
|
+
* @param newVersion The new version of the database
|
|
63
|
+
*/
|
|
64
|
+
private handleUpgrade;
|
|
65
|
+
/**
|
|
66
|
+
* Applies a single schema version's changes to the database
|
|
67
|
+
* @param db The database instance
|
|
68
|
+
* @param schema The schema version to apply
|
|
69
|
+
*/
|
|
70
|
+
private applySchemaVersion;
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fontCacheManager';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DBSchema } from 'idb';
|
|
2
|
+
import { FontData } from '../font/font';
|
|
3
|
+
/**
|
|
4
|
+
* Store name constant
|
|
5
|
+
*/
|
|
6
|
+
export declare const DB_STORES: {
|
|
7
|
+
readonly fonts: "fonts";
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Database schema interface for the font cache
|
|
11
|
+
*/
|
|
12
|
+
export interface DbFontCacheSchema extends DBSchema {
|
|
13
|
+
[DB_STORES.fonts]: {
|
|
14
|
+
key: string;
|
|
15
|
+
value: FontData;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Database schema versions
|
|
20
|
+
*/
|
|
21
|
+
export declare const dbSchema: {
|
|
22
|
+
version: number;
|
|
23
|
+
stores: {
|
|
24
|
+
name: "fonts";
|
|
25
|
+
keyPath: string;
|
|
26
|
+
}[];
|
|
27
|
+
}[];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The class representing one event manager
|
|
3
|
+
*/
|
|
4
|
+
export declare class EventManager<T = unknown> {
|
|
5
|
+
private listeners;
|
|
6
|
+
/**
|
|
7
|
+
* Add the event listener
|
|
8
|
+
* @param listener Input listener to be added
|
|
9
|
+
*/
|
|
10
|
+
addEventListener(listener: (payload: T) => void): void;
|
|
11
|
+
/**
|
|
12
|
+
* Remove the listener
|
|
13
|
+
* @param listener Input listener to be removed
|
|
14
|
+
*/
|
|
15
|
+
removeEventListener(listener: (payload: T) => void): void;
|
|
16
|
+
/**
|
|
17
|
+
* Remove all listeners bound to the target and add one new listener
|
|
18
|
+
* @param listener Input listener to be added
|
|
19
|
+
*/
|
|
20
|
+
replaceEventListener(listener: (payload: T) => void): void;
|
|
21
|
+
/**
|
|
22
|
+
* Notify all listeners
|
|
23
|
+
* @param payload Input payload passed to listener
|
|
24
|
+
*/
|
|
25
|
+
dispatch(payload?: T, ...args: unknown[]): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const getExtension: (url: string) => string;
|
|
2
|
+
export declare const getFileName: (url: string) => string | undefined;
|
|
3
|
+
export declare const getFileNameWithoutExtension: (url: string) => string;
|
|
4
|
+
/**
|
|
5
|
+
* AutoCAD files sometimes use an indexed color value between 1 and 255 inclusive.
|
|
6
|
+
* Each value corresponds to a color. index 1 is red, that is 16711680 or 0xFF0000.
|
|
7
|
+
* index 0 and 256, while included in this array, are actually reserved for inheritance
|
|
8
|
+
* values in AutoCAD so they should not be used for index color lookups.
|
|
9
|
+
*/
|
|
10
|
+
export declare const AUTOCAD_COLOR_INDEX: number[];
|
|
11
|
+
export declare const getColorByIndex: (index: number) => number;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BaseTextShape } from './baseTextShape';
|
|
2
|
+
import { CharGeometryCache } from './charGeometryCache';
|
|
3
|
+
import { FontData, FontType } from './font';
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for font implementations.
|
|
6
|
+
* Provides common functionality and interface for font handling.
|
|
7
|
+
* This class defines the core interface that all font types must implement.
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class BaseFont {
|
|
10
|
+
/** The type of font (shx or mesh) */
|
|
11
|
+
abstract readonly type: FontType;
|
|
12
|
+
/**
|
|
13
|
+
* The parsed font data. Different types of fonts have different data structures.
|
|
14
|
+
* This data is used to render characters and calculate metrics.
|
|
15
|
+
*/
|
|
16
|
+
abstract readonly data: unknown;
|
|
17
|
+
/**
|
|
18
|
+
* Font names. One font may have multiple names.
|
|
19
|
+
*/
|
|
20
|
+
names: Set<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Encoding used by character code. Please refer to the following link for encoding name.
|
|
23
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
|
|
24
|
+
*/
|
|
25
|
+
encoding?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Caching of font character geometries to improve text rendering performance.
|
|
28
|
+
*/
|
|
29
|
+
cache: CharGeometryCache;
|
|
30
|
+
constructor(fontData: FontData);
|
|
31
|
+
/**
|
|
32
|
+
* Return true if this font contains glyph of the specified character. Otherwise, return false.
|
|
33
|
+
* @param char - The character to check
|
|
34
|
+
* @returns True if this font contains glyph of the specified character. Otherwise, return false.
|
|
35
|
+
*/
|
|
36
|
+
abstract hasChar(char: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Return true if this font contains glyph of the specified character code. Otherwise, return false.
|
|
39
|
+
* @param code - The character code to check
|
|
40
|
+
* @returns True if this font contains glyph of the specified character code. Otherwise, return false.
|
|
41
|
+
*/
|
|
42
|
+
abstract hasCode(code: number): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Record of characters that are not supported by this font.
|
|
45
|
+
* Maps character strings to their occurrence count.
|
|
46
|
+
* Used for tracking and reporting unsupported characters.
|
|
47
|
+
*/
|
|
48
|
+
unsupportedChars: Record<string, number>;
|
|
49
|
+
/**
|
|
50
|
+
* Gets the shape data for a specific character at a given size.
|
|
51
|
+
* @param char - The character to get the shape for
|
|
52
|
+
* @param size - The desired size of the character
|
|
53
|
+
* @returns The shape data for the character, or undefined if not found
|
|
54
|
+
*/
|
|
55
|
+
abstract getCharShape(char: string, size: number): BaseTextShape | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the shape data for a specific character code at a given size.
|
|
58
|
+
* @param code - The character code to get the shape for
|
|
59
|
+
* @param size - The desired size of the character
|
|
60
|
+
* @returns The shape data for the character code, or undefined if not found
|
|
61
|
+
*/
|
|
62
|
+
abstract getCodeShape(code: number, size: number): BaseTextShape | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Gets the scale factor for this font.
|
|
65
|
+
* This is used to adjust the size of characters when rendering.
|
|
66
|
+
* @returns The scale factor as a number
|
|
67
|
+
*/
|
|
68
|
+
abstract getScaleFactor(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Gets the shape to display when a character is not found in the font.
|
|
71
|
+
* @param size - The desired size of the not found shape
|
|
72
|
+
* @returns The shape data for the not found indicator, or undefined if not available
|
|
73
|
+
*/
|
|
74
|
+
abstract getNotFoundTextShape(size: number): BaseTextShape | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Records an unsupported character in the font.
|
|
77
|
+
* Increments the count for the given character in unsupportedChars.
|
|
78
|
+
* @param char - The unsupported character to record
|
|
79
|
+
*/
|
|
80
|
+
protected addUnsupportedChar(char: string): void;
|
|
81
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for text shape implementations.
|
|
4
|
+
* Provides common functionality and interface for text shape handling.
|
|
5
|
+
* This class defines the core interface that all text shape types must implement.
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class BaseTextShape extends THREE.Shape {
|
|
8
|
+
/**
|
|
9
|
+
* Width used to render this character
|
|
10
|
+
*/
|
|
11
|
+
width: number;
|
|
12
|
+
/**
|
|
13
|
+
* Converts the text shape to a THREE.js geometry
|
|
14
|
+
* @returns A THREE.js BufferGeometry representing the text shape
|
|
15
|
+
*/
|
|
16
|
+
abstract toGeometry(): THREE.BufferGeometry;
|
|
17
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
/**
|
|
3
|
+
* Manages caching of font character geometries to improve text rendering performance.
|
|
4
|
+
*/
|
|
5
|
+
export declare class CharGeometryCache {
|
|
6
|
+
private cache;
|
|
7
|
+
constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Returns true if the geometry of the specified character code exists in the cache.
|
|
10
|
+
* Otherwise, returns false.
|
|
11
|
+
* @param code One character code.
|
|
12
|
+
* @param size The font size.
|
|
13
|
+
* @returns True if the geometry of the specified character code exists in the cache.
|
|
14
|
+
* Otherwise, returns false.
|
|
15
|
+
*/
|
|
16
|
+
hasGeometry(code: number, size: number): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Get the geometry for a single character from cache if available.
|
|
19
|
+
* The cache key includes both character codeand size.
|
|
20
|
+
* @param code The character code to get geometry from cache.
|
|
21
|
+
* @param size The font size.
|
|
22
|
+
* @returns The geometry for a single character from cache if avaiable.
|
|
23
|
+
* Return undefined if the character not found in cache.
|
|
24
|
+
*/
|
|
25
|
+
getGeometry(code: number, size: number): THREE.BufferGeometry | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Set the geometry to cache for a single character.
|
|
28
|
+
* @param char The character to set geometry for.
|
|
29
|
+
* @param size The font size.
|
|
30
|
+
* @param geometry The geometry to set.
|
|
31
|
+
*/
|
|
32
|
+
setGeometry(code: number, size: number, geometry: THREE.BufferGeometry): void;
|
|
33
|
+
/**
|
|
34
|
+
* Dispose all cached geometries.
|
|
35
|
+
*/
|
|
36
|
+
dispose(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Generates cache key by character and font size.
|
|
39
|
+
* @param char One character code.
|
|
40
|
+
* @param size The font size.
|
|
41
|
+
*/
|
|
42
|
+
private generateKey;
|
|
43
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { FontInfo, FontLoader, FontLoadStatus } from './fontLoader';
|
|
2
|
+
/**
|
|
3
|
+
* Default implementation of the FontLoader interface.
|
|
4
|
+
* This class provides font loading functionality using [this font repository](https://mlightcad.gitlab.io/cad-data/fonts/).
|
|
5
|
+
* It loads font metadata from a JSON file and provides access to available fonts.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DefaultFontLoader implements FontLoader {
|
|
8
|
+
/** List of available fonts in the system */
|
|
9
|
+
private _avaiableFonts;
|
|
10
|
+
private _baseUrl;
|
|
11
|
+
private _avaiableFontMap;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new instance of DefaultFontLoader
|
|
14
|
+
*/
|
|
15
|
+
constructor();
|
|
16
|
+
/**
|
|
17
|
+
* Base URL to load fonts
|
|
18
|
+
*/
|
|
19
|
+
get baseUrl(): string;
|
|
20
|
+
set baseUrl(value: string);
|
|
21
|
+
/**
|
|
22
|
+
* Gets the list of available fonts
|
|
23
|
+
* @returns Array of FontInfo objects describing available fonts
|
|
24
|
+
*/
|
|
25
|
+
get avaiableFonts(): FontInfo[];
|
|
26
|
+
/**
|
|
27
|
+
* Triggered when font url changed
|
|
28
|
+
* @param url - New font url value
|
|
29
|
+
*/
|
|
30
|
+
onFontUrlChanged(url: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves information about all available fonts in the system.
|
|
33
|
+
* Loads font metadata from a CDN if not already loaded.
|
|
34
|
+
* @returns Promise that resolves to an array of FontInfo objects
|
|
35
|
+
* @throws {Error} If font metadata cannot be loaded from the CDN
|
|
36
|
+
*/
|
|
37
|
+
getAvailableFonts(): Promise<FontInfo[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Loads the specified fonts into the system. If one font is already loaded,
|
|
40
|
+
* the font will not be loaded again. If no font names are provided, just loads
|
|
41
|
+
* all available fonts information (not fonts).
|
|
42
|
+
* @param fontNames - Array of font names to load
|
|
43
|
+
* @returns Promise that resolves to an array of FontLoadStatus objects
|
|
44
|
+
*/
|
|
45
|
+
load(fontNames: string[]): Promise<FontLoadStatus[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Build one font map. The key is font name. The value is font info.
|
|
48
|
+
*/
|
|
49
|
+
private buildFontMap;
|
|
50
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the type of font supported by the system
|
|
3
|
+
* - 'shx': SHX font format commonly used in CAD systems
|
|
4
|
+
* - 'mesh': Mesh-based font format (e.g., TTF, OTF, WOFF)
|
|
5
|
+
*/
|
|
6
|
+
export type FontType = 'shx' | 'mesh';
|
|
7
|
+
/**
|
|
8
|
+
* Represents font data stored in the cache database.
|
|
9
|
+
* This interface defines the structure of font data that is stored and retrieved from the cache.
|
|
10
|
+
*/
|
|
11
|
+
export interface FontData {
|
|
12
|
+
/** The font name */
|
|
13
|
+
name: string;
|
|
14
|
+
/** The alias names of the font */
|
|
15
|
+
alias: string[];
|
|
16
|
+
/** The type of font (shx or mesh) */
|
|
17
|
+
type: FontType;
|
|
18
|
+
/**
|
|
19
|
+
* Encoding used by character code. Please refer to the following link for encoding name.
|
|
20
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
|
|
21
|
+
*/
|
|
22
|
+
encoding?: string;
|
|
23
|
+
/** The parsed font data. Different types of fonts have different data structures. */
|
|
24
|
+
data: unknown;
|
|
25
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BaseFont } from './baseFont';
|
|
2
|
+
import { FontData } from './font';
|
|
3
|
+
/**
|
|
4
|
+
* A singleton factory class for creating font instances.
|
|
5
|
+
* This factory can create both ShxFont and MeshFont instances based on the provided font data.
|
|
6
|
+
* It handles the creation of appropriate font objects based on the font type and data format.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const fontFactory = FontFactory.getInstance();
|
|
11
|
+
* const font = fontFactory.createFont(fontData);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class FontFactory {
|
|
15
|
+
private static _instance;
|
|
16
|
+
private constructor();
|
|
17
|
+
/**
|
|
18
|
+
* Gets the singleton instance of the FontFactory
|
|
19
|
+
* @returns The FontFactory instance
|
|
20
|
+
*/
|
|
21
|
+
static get instance(): FontFactory;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a font instance based on the provided font data.
|
|
24
|
+
* The type of font created (ShxFont or MeshFont) is determined by the font type.
|
|
25
|
+
*
|
|
26
|
+
* @param data - The font data to create the font instance from
|
|
27
|
+
* @returns A new instance of either ShxFont or MeshFont
|
|
28
|
+
* @throws {Error} If the font data type is not supported
|
|
29
|
+
*/
|
|
30
|
+
createFont(data: FontData): BaseFont;
|
|
31
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents information about a font in the system
|
|
3
|
+
*/
|
|
4
|
+
export interface FontInfo {
|
|
5
|
+
/** Array of font names/aliases */
|
|
6
|
+
name: string[];
|
|
7
|
+
/** Font file name */
|
|
8
|
+
file: string;
|
|
9
|
+
/** Type of the font - either mesh or shx format */
|
|
10
|
+
type: 'mesh' | 'shx';
|
|
11
|
+
/** URL where the font can be accessed */
|
|
12
|
+
url: string;
|
|
13
|
+
/**
|
|
14
|
+
* Encoding used by character code. Please refer to the following link for encoding name.
|
|
15
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
|
|
16
|
+
*/
|
|
17
|
+
encoding?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Represents the status of a font loading operation
|
|
21
|
+
*/
|
|
22
|
+
export interface FontLoadStatus {
|
|
23
|
+
/** Name of the font that was loaded */
|
|
24
|
+
fontName: string;
|
|
25
|
+
/** URL from which the font was loaded */
|
|
26
|
+
url: string;
|
|
27
|
+
/**
|
|
28
|
+
* The status to load font
|
|
29
|
+
* - Success
|
|
30
|
+
* - Font not found in font repository
|
|
31
|
+
* - Failed to load font from font repository
|
|
32
|
+
*/
|
|
33
|
+
status: 'Success' | 'NotFound' | 'FailedToLoad';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Interface that defines font loading functionality.
|
|
37
|
+
* Applications should implement this interface to provide font loading capabilities.
|
|
38
|
+
* This interface abstracts the font loading process, allowing different implementations
|
|
39
|
+
* for different font sources or loading strategies.
|
|
40
|
+
*/
|
|
41
|
+
export interface FontLoader {
|
|
42
|
+
/**
|
|
43
|
+
* Loads the specified fonts into the system
|
|
44
|
+
* @param fontNames - Array of font names to load
|
|
45
|
+
* @returns Promise that resolves to an array of FontLoadStatus objects indicating the load status of each font
|
|
46
|
+
*/
|
|
47
|
+
load(fontNames: string[]): Promise<FontLoadStatus[]>;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves information about all available fonts in the system
|
|
50
|
+
* @returns Promise that resolves to an array of FontInfo objects containing details about available fonts
|
|
51
|
+
*/
|
|
52
|
+
getAvailableFonts(): Promise<FontInfo[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Base URL to load fonts
|
|
55
|
+
*/
|
|
56
|
+
get baseUrl(): string;
|
|
57
|
+
set baseUrl(value: string);
|
|
58
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { EventManager } from '../common';
|
|
2
|
+
import { BaseFont } from './baseFont';
|
|
3
|
+
import { BaseTextShape } from './baseTextShape';
|
|
4
|
+
import { FontType } from './font';
|
|
5
|
+
import { FontInfo, FontLoader, FontLoadStatus } from './fontLoader';
|
|
6
|
+
/**
|
|
7
|
+
* Font mappings configuration.
|
|
8
|
+
* Maps original font names to their replacement font names.
|
|
9
|
+
* - The key is the original font name
|
|
10
|
+
* - The value is the mapped font name
|
|
11
|
+
*/
|
|
12
|
+
export type FontMapping = Record<string, string>;
|
|
13
|
+
/**
|
|
14
|
+
* Event arguments for font-related events.
|
|
15
|
+
*/
|
|
16
|
+
export interface FontManagerEventArgs {
|
|
17
|
+
/** Name of font which can't be found */
|
|
18
|
+
fontName: string;
|
|
19
|
+
/** The number of characters which use this font. This is only used when the font is not found. */
|
|
20
|
+
count?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Manages font loading, caching, and text rendering.
|
|
24
|
+
* This class is responsible for:
|
|
25
|
+
* - Loading fonts from URLs or cache
|
|
26
|
+
* - Managing font mappings and replacements
|
|
27
|
+
* - Providing text shapes for rendering
|
|
28
|
+
* - Tracking unsupported characters and missed fonts
|
|
29
|
+
*/
|
|
30
|
+
export declare class FontManager {
|
|
31
|
+
private static _instance;
|
|
32
|
+
/** THREE.js file loader for loading font files */
|
|
33
|
+
private loader;
|
|
34
|
+
/** Font loader which can be swapped by users */
|
|
35
|
+
private fontLoader;
|
|
36
|
+
/** Mapping of original font names to their replacements */
|
|
37
|
+
protected fontMapping: FontMapping;
|
|
38
|
+
/** Map of loaded fonts, keyed by font name */
|
|
39
|
+
protected loadedFontMap: Map<string, BaseFont>;
|
|
40
|
+
/** List of font file names that have been loaded */
|
|
41
|
+
protected fileNames: string[];
|
|
42
|
+
/** Record of characters that are not supported by any loaded font */
|
|
43
|
+
unsupportedChars: Record<string, number>;
|
|
44
|
+
/** Record of fonts that were requested but not found */
|
|
45
|
+
missedFonts: Record<string, number>;
|
|
46
|
+
/** Flag to enable/disable font caching */
|
|
47
|
+
enableFontCache: boolean;
|
|
48
|
+
/** Default font to use when a requested font is not found */
|
|
49
|
+
defaultFont: string;
|
|
50
|
+
/** Event managers for font-related events */
|
|
51
|
+
readonly events: {
|
|
52
|
+
/** Event triggered when a font cannot be found */
|
|
53
|
+
fontNotFound: EventManager<FontManagerEventArgs>;
|
|
54
|
+
/** Event triggered when a font is successfully loaded */
|
|
55
|
+
fontLoaded: EventManager<FontManagerEventArgs>;
|
|
56
|
+
};
|
|
57
|
+
private constructor();
|
|
58
|
+
/**
|
|
59
|
+
* Gets the singleton instance of the FontManager
|
|
60
|
+
* @returns The FontManager instance
|
|
61
|
+
*/
|
|
62
|
+
static get instance(): FontManager;
|
|
63
|
+
/**
|
|
64
|
+
* Base URL to load fonts
|
|
65
|
+
*/
|
|
66
|
+
get baseUrl(): string;
|
|
67
|
+
set baseUrl(value: string);
|
|
68
|
+
/**
|
|
69
|
+
* Sets the font mapping configuration
|
|
70
|
+
* @param mapping - The font mapping to set
|
|
71
|
+
*/
|
|
72
|
+
setFontMapping(mapping: FontMapping): void;
|
|
73
|
+
/**
|
|
74
|
+
* Sets the font loader
|
|
75
|
+
* @param fontLoader - The font loader to set
|
|
76
|
+
*/
|
|
77
|
+
setFontLoader(fontLoader: FontLoader): void;
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves information about all available fonts in the system.
|
|
80
|
+
* Loads font metadata from a CDN if not already loaded.
|
|
81
|
+
* @returns Promise that resolves to an array of FontInfo objects
|
|
82
|
+
* @throws {Error} If font metadata cannot be loaded from the CDN
|
|
83
|
+
*/
|
|
84
|
+
getAvailableFonts(): Promise<FontInfo[]>;
|
|
85
|
+
/**
|
|
86
|
+
* Return true if the default font was loaded.
|
|
87
|
+
* @returns True if the default font was loaded. False otherwise.
|
|
88
|
+
*/
|
|
89
|
+
isDefaultFontLoaded(): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Loads the default font
|
|
92
|
+
* @returns Promise that resolves to the font load statuses
|
|
93
|
+
*/
|
|
94
|
+
loadDefaultFont(): Promise<FontLoadStatus[]>;
|
|
95
|
+
/**
|
|
96
|
+
* Loads the specified fonts from font names
|
|
97
|
+
* @param names - Font names to load.
|
|
98
|
+
* @returns Promise that resolves to an array of font load statuses
|
|
99
|
+
*/
|
|
100
|
+
loadFontsByNames(names: string | string[]): Promise<FontLoadStatus[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Loads the specified fonts from URLs
|
|
103
|
+
* @param urls - URLs of font files to load.
|
|
104
|
+
* @returns Promise that resolves to an array of font load statuses
|
|
105
|
+
*/
|
|
106
|
+
loadFonts(fonts: FontInfo | FontInfo[]): Promise<FontLoadStatus[]>;
|
|
107
|
+
/**
|
|
108
|
+
* Tries to find the specified font. If not found, uses a replacement font and returns its name.
|
|
109
|
+
* @param fontName - The font name to find
|
|
110
|
+
* @returns The original font name if found, or the replacement font name if not found
|
|
111
|
+
*/
|
|
112
|
+
findAndReplaceFont(fontName: string): string;
|
|
113
|
+
/**
|
|
114
|
+
* Gets font by font name. Return undefined if not found.
|
|
115
|
+
* @param fontName - The font name to find
|
|
116
|
+
* @param recordMissedFonts - Record the font name to property `missedFonts` in this class
|
|
117
|
+
* if the specified font name not found.
|
|
118
|
+
* @returns The font with the specified font name, or undefined if not found
|
|
119
|
+
*/
|
|
120
|
+
getFontByName(fontName: string, recordMissedFonts?: boolean): BaseFont | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* Gets the first font which contains the specified character.
|
|
123
|
+
* @param char - The character to get the shape for
|
|
124
|
+
* @returns The text shape for the character, or undefined if not found
|
|
125
|
+
*/
|
|
126
|
+
getFontByChar(char: string): BaseFont | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Gets the text shape for a specific character with the specified font and size
|
|
129
|
+
* @param char - The character to get the shape for
|
|
130
|
+
* @param fontName - The name of the font to use
|
|
131
|
+
* @param size - The size of the character
|
|
132
|
+
* @returns The text shape for the character, or undefined if not found
|
|
133
|
+
*/
|
|
134
|
+
getCharShape(char: string, fontName: string, size: number): BaseTextShape | undefined;
|
|
135
|
+
/**
|
|
136
|
+
* Gets the scale factor for a specific font
|
|
137
|
+
* @param fontName - The name of the font
|
|
138
|
+
* @returns The scale factor for the font, or 1 if the font is not found
|
|
139
|
+
*/
|
|
140
|
+
getFontScaleFactor(fontName: string): number;
|
|
141
|
+
/**
|
|
142
|
+
* Gets type of the specific font
|
|
143
|
+
* @param fontName - The name of the font
|
|
144
|
+
* @returns The type of the font. If the specified font can't be found, `undefined` is returned
|
|
145
|
+
*/
|
|
146
|
+
getFontType(fontName: string): FontType | undefined;
|
|
147
|
+
/**
|
|
148
|
+
* Gets the shape to display when a character is not found
|
|
149
|
+
* @param size - The size of the shape
|
|
150
|
+
* @returns The shape for the not found indicator, or undefined if not available
|
|
151
|
+
*/
|
|
152
|
+
getNotFoundTextShape(size: number): BaseTextShape | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* Checks if a font is already loaded in the system
|
|
155
|
+
* @param fontName - The name of the font to check
|
|
156
|
+
* @returns True if the font is loaded, false otherwise
|
|
157
|
+
*/
|
|
158
|
+
isFontLoaded(fontName: string): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Records a font that was requested but not found
|
|
161
|
+
* @param fontName - The name of the font that was not found
|
|
162
|
+
*/
|
|
163
|
+
private recordMissedFonts;
|
|
164
|
+
/**
|
|
165
|
+
* Loads a single font
|
|
166
|
+
* @param fontInfo - The matadata of the font to be loaded
|
|
167
|
+
*/
|
|
168
|
+
private loadFont;
|
|
169
|
+
private fontInfoToFontData;
|
|
170
|
+
/**
|
|
171
|
+
* Loads all fonts from the cache
|
|
172
|
+
*/
|
|
173
|
+
getAllFontsFromCache(): Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Gets a record of all unsupported characters across all loaded fonts
|
|
176
|
+
* @returns A record mapping unsupported characters to their occurrence count
|
|
177
|
+
*/
|
|
178
|
+
getUnsupportedChar(): Record<string, number>;
|
|
179
|
+
/**
|
|
180
|
+
* Releases loaded fonts from memory.
|
|
181
|
+
*
|
|
182
|
+
* - If no argument is provided, all loaded fonts are released and the font map is cleared.
|
|
183
|
+
* - If a font name is provided, only that specific font is released from the font map.
|
|
184
|
+
*
|
|
185
|
+
* This is useful for freeing up memory, especially when working with large font files (e.g., Chinese mesh fonts).
|
|
186
|
+
* Notes: Based on testing, one Chinese mesh font file may take 40M memory.
|
|
187
|
+
*
|
|
188
|
+
* @param fontToRelease - (Optional) The name of the font to release. If omitted, all fonts are released.
|
|
189
|
+
* @returns `true` if the operation succeeded (all fonts released or the specified font was found and deleted), `false` if the specified font was not found.
|
|
190
|
+
*/
|
|
191
|
+
release(fontToRelease?: string): boolean;
|
|
192
|
+
}
|