@mlightcad/mtext-renderer 0.11.6 → 0.11.8

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,63 @@
1
+ /**
2
+ * Callback invoked when an entry is removed from the cache, either because it
3
+ * was evicted to make room for a new entry, replaced by a different value, or
4
+ * cleared explicitly.
5
+ *
6
+ * @typeParam K - The cache key type.
7
+ * @typeParam V - The cache value type.
8
+ */
9
+ export type LRUCacheEvictHandler<K, V> = (key: K, value: V) => void;
10
+ /**
11
+ * Simple least-recently-used cache with a fixed maximum size.
12
+ *
13
+ * Entries are ordered by access time: {@link get} and {@link set} move a key to
14
+ * the most-recently-used position. When the cache is full, the oldest entry is
15
+ * evicted before inserting a new one.
16
+ *
17
+ * @typeParam K - The cache key type.
18
+ * @typeParam V - The cache value type.
19
+ */
20
+ export declare class LRUCache<K, V> {
21
+ private readonly maxSize;
22
+ private readonly onEvict?;
23
+ private readonly map;
24
+ /**
25
+ * Creates an LRU cache with the given capacity and optional eviction handler.
26
+ *
27
+ * @param maxSize - Maximum number of entries to retain. Defaults to 4096.
28
+ * @param onEvict - Optional callback invoked for each evicted or replaced value.
29
+ */
30
+ constructor(maxSize?: number, onEvict?: LRUCacheEvictHandler<K, V>);
31
+ /**
32
+ * Returns the value for `key` and marks it as most recently used.
33
+ *
34
+ * @param key - The cache key to look up.
35
+ * @returns The cached value, or `undefined` if the key is not present.
36
+ */
37
+ get(key: K): V | undefined;
38
+ /**
39
+ * Stores `value` under `key` and marks it as most recently used.
40
+ *
41
+ * If the key already exists, its previous value is passed to {@link onEvict}
42
+ * when the new value differs. If the cache is at capacity, the least recently
43
+ * used entry is evicted before the new entry is inserted.
44
+ *
45
+ * @param key - The cache key to set.
46
+ * @param value - The value to store.
47
+ */
48
+ set(key: K, value: V): void;
49
+ /**
50
+ * Returns whether `key` exists in the cache without updating its recency.
51
+ *
52
+ * @param key - The cache key to test.
53
+ * @returns True if the key is present; otherwise, false.
54
+ */
55
+ has(key: K): boolean;
56
+ /**
57
+ * Removes all entries from the cache.
58
+ *
59
+ * If an {@link onEvict} handler was provided, it is invoked once per entry
60
+ * before the internal map is cleared.
61
+ */
62
+ clear(): void;
63
+ }
@@ -14,4 +14,8 @@ export declare abstract class BaseTextShape extends THREE.Shape {
14
14
  * @returns A THREE.js BufferGeometry representing the text shape
15
15
  */
16
16
  abstract toGeometry(): THREE.BufferGeometry;
17
+ /**
18
+ * Whether the shape has drawable stroke or mesh geometry (not advance-only).
19
+ */
20
+ hasStrokeGeometry(): boolean;
17
21
  }
@@ -1,10 +1,11 @@
1
1
  import * as THREE from 'three';
2
2
  /**
3
3
  * Manages caching of font character geometries to improve text rendering performance.
4
+ * Uses an LRU policy so memory stays bounded when many (code, size) pairs are loaded.
4
5
  */
5
6
  export declare class CharGeometryCache {
6
- private cache;
7
- constructor();
7
+ private readonly cache;
8
+ constructor(maxSize?: number);
8
9
  /**
9
10
  * Returns true if the geometry of the specified character code exists in the cache.
10
11
  * Otherwise, returns false.
@@ -22,6 +22,8 @@ export declare class MeshTextShape extends BaseTextShape {
22
22
  * @returns A THREE.js BufferGeometry representing the text shape
23
23
  */
24
24
  toGeometry(): THREE.BufferGeometry;
25
+ /** @inheritdoc */
26
+ hasStrokeGeometry(): boolean;
25
27
  /**
26
28
  * Calculates the width of a character in the font.
27
29
  * @param char - The character to calculate width for
@@ -1,7 +1,9 @@
1
- import { ShxFontData } from '@mlightcad/shx-parser';
1
+ import { ShxFontData, ShxFontMetrics } from '@mlightcad/shx-parser';
2
2
  import { BaseFont } from './baseFont';
3
3
  import { FontData } from './font';
4
4
  import { ShxTextShape } from './shxTextShape';
5
+ /** Scaled SHX font layout metrics (cap height, cell width, etc.) from `@mlightcad/shx-parser`. */
6
+ export type { ShxFontMetrics } from '@mlightcad/shx-parser';
5
7
  /**
6
8
  * ShxFont is a class that extends BaseFont and represents a SHX font.
7
9
  * It provides methods to generate shapes for text and retrieve character shapes.
@@ -9,68 +11,92 @@ import { ShxTextShape } from './shxTextShape';
9
11
  export declare class ShxFont extends BaseFont {
10
12
  /** Internal shx font instance */
11
13
  private readonly font;
14
+ /** The type of font; always `'shx'`. */
12
15
  readonly type = "shx";
16
+ /** Parsed SHX font data used for glyph lookup and layout metrics. */
13
17
  readonly data: ShxFontData;
18
+ /** Cached layout-ready {@link ShxTextShape} instances keyed by code and size. */
19
+ private readonly layoutShapeCache;
20
+ /** Cached BIGFONT character encodings keyed by input character. */
21
+ private readonly codeCache;
22
+ /**
23
+ * Creates a new SHX font wrapper.
24
+ * @param fontData - Font metadata and binary SHX data used to initialize the font.
25
+ */
14
26
  constructor(fontData: FontData);
15
27
  /**
16
- * Return true if this font contains glyph of the specified character. Otherwise, return false.
17
- * @param char - The character to check
18
- * @returns True if this font contains glyph of the specified character. Otherwise, return false.
28
+ * Returns whether the font contains a glyph for the given character.
29
+ * @param char - The character to look up.
30
+ * @returns True if the font contains the character; otherwise, false.
19
31
  */
20
32
  hasChar(char: string): boolean;
21
33
  /**
22
- * Return true if this font contains glyph of the specified character code. Otherwise, return false.
23
- * @param code - The character code to check
24
- * @returns True if this font contains glyph of the specified character code. Otherwise, return false.
34
+ * Returns whether the font contains a glyph for the given character code.
35
+ * @param code - The character code to look up.
36
+ * @returns True if the font contains the code point; otherwise, false.
25
37
  */
26
38
  hasCode(code: number): boolean;
27
39
  /**
28
- * Horizontal advance for the space character (ASCII 32) at the given size.
29
- * Uses the SHX glyph pen advance when defined; otherwise falls back to half the
30
- * text height (common for AutoCAD SHX fonts).
40
+ * Computes the horizontal advance for a space at the requested size.
41
+ * @param size - The requested font size.
42
+ * @returns The width of the space advance.
31
43
  */
32
44
  getSpaceAdvance(size: number): number;
45
+ /**
46
+ * Converts a text string into a list of SHX text shapes.
47
+ * @param text - The text to convert.
48
+ * @param size - The requested font size.
49
+ * @returns An array of generated SHX text shapes.
50
+ */
33
51
  generateShapes(text: string, size: number): ShxTextShape[];
34
52
  /**
35
- * SHX font always has fixed scale factor 1.
36
- * @returns Always return value 1
53
+ * Returns the scale factor used by the SHX font implementation.
54
+ * @returns Always returns 1 for SHX fonts.
37
55
  */
38
56
  getScaleFactor(): number;
57
+ /**
58
+ * Gets the scaled layout metrics for the font at the requested size.
59
+ * @param size - The requested font size.
60
+ * @returns The SHX font metrics for the given size.
61
+ */
62
+ getFontMetrics(size: number): ShxFontMetrics;
39
63
  /**
40
64
  * Gets the shape data for a specific character at a given size.
41
- * If the font type is BIGFONT, please use getCodeShape to get the shape data
42
- * because the character code for BIGFONT isn't unicode.
43
- * @param char - The character to get the shape for
44
- * @param size - The desired size of the character
45
- * @returns The shape data for the character, or undefined if not found
65
+ * @param char - The character to look up.
66
+ * @param size - The requested font size.
67
+ * @returns The shape data for the character, or undefined if not found.
46
68
  */
47
69
  getCharShape(char: string, size: number): ShxTextShape | undefined;
48
70
  /**
49
71
  * Gets the shape data for a specific character code at a given size.
50
- * The passed code must the code stored in font instead of unicode.
51
- * - Unicode shx font uses unicode as character code.
52
- * - Bigfont uses a custom encoding for double-byte characters.
53
- * @param code - The character code to get the shape for
54
- * @param size - The desired size of the character
55
- * @returns The shape data for the character code, or undefined if not found
72
+ * @param code - The character code to look up.
73
+ * @param size - The requested font size.
74
+ * @returns The shape data for the code, or undefined if not found.
56
75
  */
57
76
  getCodeShape(code: number, size: number): ShxTextShape | undefined;
58
77
  /**
59
- * Gets the shape data for a named SHX shape at a given size.
60
- *
61
- * Shape names are matched case-insensitively via the underlying SHX parser.
78
+ * Gets the shape data for a named SHX shape at the requested size.
79
+ * @param name - The SHX shape name to look up.
80
+ * @param size - The requested font size.
81
+ * @returns The matching shape, or undefined if unavailable.
62
82
  */
63
83
  getShapeByName(name: string, size: number): ShxTextShape | undefined;
64
- /** True when the SHX glyph has drawable strokes or a non-zero pen advance. */
84
+ /**
85
+ * Checks whether a parsed SHX shape contains renderable strokes.
86
+ * @param shape - The shape to inspect.
87
+ * @returns True when the shape has at least one renderable segment.
88
+ */
65
89
  private static hasRenderableStrokes;
66
90
  /**
67
- * For an unsupported char, use "?" as a replacement.
91
+ * Gets the fallback text shape used for missing characters.
92
+ * @param size - The requested font size.
93
+ * @returns The fallback shape, or undefined if it cannot be built.
68
94
  */
69
95
  getNotFoundTextShape(size: number): ShxTextShape | undefined;
70
96
  /**
71
- * Gets encoded code of the specified character according to font character encoding
72
- * @param char - The character to get its code
73
- * @returns Returns encoded code of the specified character
97
+ * Resolves the internal SHX character code for a given Unicode character.
98
+ * @param char - The input character.
99
+ * @returns The internal SHX code used for lookup.
74
100
  */
75
101
  private getCode;
76
102
  }
@@ -13,27 +13,32 @@ export declare class ShxTextShape extends BaseTextShape {
13
13
  private readonly shape;
14
14
  private readonly font;
15
15
  private readonly fontSize;
16
+ /** Lazily built geometry for this layout-ready shape instance. */
17
+ private geometry?;
16
18
  /**
17
- * Creates a new instance of ShxTextShape
18
- * @param code - The character code this shape represents
19
- * @param shape - The shape data for this character
19
+ * Creates a new SHX text shape wrapper.
20
+ * @param code - The character code represented by this shape.
21
+ * @param fontSize - The font size used to resolve the shape advance width.
22
+ * @param shape - The parsed SHX shape data.
23
+ * @param font - The SHX font instance that owns the shape.
20
24
  */
21
25
  constructor(code: number, fontSize: number, shape: ShxShape, font: ShxFont);
22
26
  /**
23
- * Resolves horizontal advance for SHX glyphs.
24
- *
25
- * - Unicode / shapes fonts: prefer the pen-down end X (lastPoint), which matches
26
- * AutoCAD's advance for single-byte SHX.
27
- * - Big fonts (CJK): advance is the scaled font cell width (content.width /
28
- * content.height * fontSize). Glyph bbox or lastPoint can be narrower than the
29
- * cell; using only bbox caused successive Han characters to overlap visually.
27
+ * Computes the width of the shape from its bounding box.
28
+ * @returns The width of the shape's bounding box.
30
29
  */
31
- private resolveAdvanceWidth;
32
30
  protected calcWidth(): number;
31
+ /**
32
+ * Returns a translated copy of this shape.
33
+ * @param offset - The offset to apply to the shape.
34
+ * @returns A new shape shifted by the given offset.
35
+ */
33
36
  offset(offset: Point): ShxTextShape;
34
37
  /**
35
- * Converts the text shape to a THREE.js geometry
36
- * @returns A THREE.js BufferGeometry representing the text shape
38
+ * Converts the text shape to a THREE.js geometry.
39
+ * @returns A BufferGeometry representing the text shape.
37
40
  */
38
41
  toGeometry(): THREE.BufferGeometry<THREE.NormalBufferAttributes>;
42
+ /** @inheritdoc */
43
+ hasStrokeGeometry(): boolean;
39
44
  }
@@ -0,0 +1,47 @@
1
+ import * as THREE from 'three';
2
+ /**
3
+ * One cached glyph geometry plus the world transform to apply at merge time.
4
+ */
5
+ export interface TransformedLineGeometryEntry {
6
+ /** The source line-segment geometry for a single glyph. */
7
+ geometry: THREE.BufferGeometry;
8
+ /** The 4×4 transform matrix applied to each vertex before merging. */
9
+ matrix: THREE.Matrix4;
10
+ }
11
+ /**
12
+ * Builds a single line-segment {@link THREE.BufferGeometry} from many transformed glyph sources.
13
+ * Avoids per-character geometry allocation before merge.
14
+ */
15
+ export declare class TextGeometryBuilder {
16
+ /**
17
+ * Merges indexed or non-indexed line geometries into one non-indexed {@link THREE.BufferGeometry}
18
+ * suitable for {@link THREE.LineSegments}.
19
+ * @param entries Glyph geometries paired with their world transforms.
20
+ * @returns A single non-indexed line geometry containing all transformed segments.
21
+ */
22
+ static mergeLineGeometries(entries: TransformedLineGeometryEntry[]): THREE.BufferGeometry;
23
+ /**
24
+ * Counts how many line segments a geometry represents.
25
+ * Indexed geometries use index pairs; non-indexed geometries use consecutive position pairs.
26
+ * @param geometry The line geometry to inspect.
27
+ * @returns The number of line segments (each segment is two vertices).
28
+ */
29
+ private static countLineSegments;
30
+ /**
31
+ * Applies a transform matrix to a single line geometry and returns a new non-indexed copy.
32
+ * @param geometry The source line geometry.
33
+ * @param matrix The transform applied to every vertex.
34
+ * @returns A new non-indexed line geometry with transformed positions.
35
+ */
36
+ private static applyMatrixToLineGeometry;
37
+ /**
38
+ * Writes transformed line-segment vertices from a source geometry into a flat position buffer.
39
+ * Supports both indexed and non-indexed source geometries.
40
+ * @param geometry The source line geometry.
41
+ * @param matrix The transform applied to each vertex before writing.
42
+ * @param output The destination `Float32Array` (xyz per vertex).
43
+ * @param outputOffset The index in `output` at which writing begins.
44
+ * @returns The next write offset after all segments have been written.
45
+ */
46
+ private static writeTransformedLineSegments;
47
+ }