@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,3 @@
1
+ export * from './mtext';
2
+ export * from './styleManager';
3
+ export * from './types';
@@ -0,0 +1,90 @@
1
+ import { FontManager } from '../font';
2
+ import { ColorSettings, MTextData, TextStyle } from './types';
3
+ import { StyleManager } from './styleManager';
4
+ import * as THREE from 'three';
5
+ /**
6
+ * Represents an AutoCAD MText object in Three.js.
7
+ * This class extends THREE.Object3D to provide MText rendering capabilities,
8
+ * including text layout, alignment, and transformation.
9
+ */
10
+ export declare class MText extends THREE.Object3D {
11
+ /** The text style configuration for this MText object */
12
+ private _style;
13
+ /** The style manager instance for handling text styles */
14
+ private _styleManager;
15
+ /** The font manager instance for handling font operations */
16
+ private _fontManager;
17
+ /** Color settings used to decided font color */
18
+ private _colorSettings;
19
+ /** The bounding box of the entire MText object */
20
+ private _box;
21
+ /** Array of bounding boxes for individual text elements */
22
+ private _boxes;
23
+ /**
24
+ * Creates a new instance of MText.
25
+ * @param text - The MText data containing text content and properties
26
+ * @param style - The text style configuration
27
+ * @param styleManager - The style manager instance
28
+ * @param fontManager - The font manager instance
29
+ * @param colorSettings - Color settings used to decided font color
30
+ */
31
+ constructor(text: MTextData, style: TextStyle, styleManager: StyleManager, fontManager: FontManager, colorSettings?: ColorSettings);
32
+ /**
33
+ * Gets the font manager instance associated with this MText object.
34
+ * @returns The FontManager instance
35
+ */
36
+ get fontManager(): FontManager;
37
+ /**
38
+ * Gets the style manager instance associated with this MText object.
39
+ * @returns The StyleManager instance
40
+ */
41
+ get styleManager(): StyleManager;
42
+ /**
43
+ * Gets the text style configuration for this MText object.
44
+ * @returns The TextStyle configuration
45
+ */
46
+ get textStyle(): TextStyle;
47
+ /**
48
+ * Gets or sets the bounding box of this MText object.
49
+ * The bounding box is calculated without considering the transformation matrix.
50
+ * To get the bounding box with transformation, call `applyMatrix4` on this box.
51
+ */
52
+ get box(): THREE.Box3;
53
+ set box(box: THREE.Box3);
54
+ /**
55
+ * Calculates intersections between a ray and this MText object.
56
+ * Overrides the base THREE.Object3D raycast method to use the text's bounding boxes.
57
+ * @param raycaster - The raycaster to use for intersection testing
58
+ * @param intersects - Array to store intersection results
59
+ */
60
+ raycast(raycaster: THREE.Raycaster, intersects: THREE.Intersection[]): void;
61
+ /**
62
+ * Loads and processes MText data to create a Three.js object.
63
+ * @param mtextData - The MText data to process
64
+ * @param style - The text style configuration
65
+ * @returns The created Three.js object, or undefined if creation fails
66
+ */
67
+ private loadMText;
68
+ /**
69
+ * Creates a group of text elements from MText data.
70
+ * @param mtextData - The MText data to process
71
+ * @param style - The text style configuration
72
+ * @returns An object containing the created Three.js object and its height
73
+ */
74
+ private createMTextGroup;
75
+ /**
76
+ * Calculates the anchor point for text positioning based on alignment and flow direction.
77
+ * @param width - The width of the text
78
+ * @param height - The height of the text
79
+ * @param attachmentPoint - The attachment point for text alignment
80
+ * @param flowDirection - The text flow direction
81
+ * @returns The calculated anchor point coordinates
82
+ */
83
+ private calculateAnchorPoint;
84
+ /**
85
+ * Recursively calculates bounding boxes for an object and its children.
86
+ * @param object - The Three.js object to process
87
+ * @param boxes - Array to store the calculated bounding boxes
88
+ */
89
+ private getBoxes;
90
+ }
@@ -0,0 +1,225 @@
1
+ import { ChangedProperties, MTextParagraphAlignment, MTextToken } from '@mlightcad/mtext-parser';
2
+ import { FontManager } from '../font';
3
+ import { StyleManager } from './styleManager';
4
+ import { MTextFlowDirection, TextStyle } from './types';
5
+
6
+ import * as THREE from 'three';
7
+ export interface MTextFormatOptions {
8
+ /**
9
+ * Font size.
10
+ */
11
+ fontSize: number;
12
+ /**
13
+ * Scale factor of character width.
14
+ */
15
+ widthFactor: number;
16
+ /**
17
+ * The line space factor.
18
+ */
19
+ lineSpaceFactor: number;
20
+ /**
21
+ * The horizontal alignment.
22
+ */
23
+ horizontalAlignment: MTextParagraphAlignment;
24
+ /**
25
+ * The maximum width of one line of text string.
26
+ */
27
+ maxWidth: number;
28
+ /**
29
+ * The direction that the text string follows from its start to its finish.
30
+ */
31
+ flowDirection: MTextFlowDirection;
32
+ /**
33
+ * The color of the current block which is used when the text color is by block.
34
+ */
35
+ byBlockColor: number;
36
+ /**
37
+ * The color of the current layer which is used when the text color is by layer.
38
+ */
39
+ byLayerColor: number;
40
+ /**
41
+ * Whether to remove font name extension.
42
+ */
43
+ removeFontExtension: boolean;
44
+ }
45
+ /**
46
+ * This class represents lines of texts.
47
+ */
48
+ export declare class MTextProcessor {
49
+ private _style;
50
+ private _styleManager;
51
+ private _fontManager;
52
+ private _options;
53
+ private _totalHeight;
54
+ private _hOffset;
55
+ private _vOffset;
56
+ private _lineCount;
57
+ private _currentLineObjects;
58
+ private _currentHorizontalAlignment;
59
+ private _currentWidthFactor;
60
+ private _currentWordSpace;
61
+ private _currentBlankWidth;
62
+ private _currentFont;
63
+ private _currentFontScaleFactor;
64
+ private _currentFontSize;
65
+ private _currentFontSizeScaleFactor;
66
+ private _currentColor;
67
+ private _currentMaxFontSize;
68
+ private _currentUnderline;
69
+ private _currentOverline;
70
+ private _currentStrikeThrough;
71
+ private _currentObliqueAngle;
72
+ /**
73
+ * Construct one instance of this class and initialize some properties with default values.
74
+ * @param style Input text style
75
+ * @param styleManager Input text style manager instance
76
+ * @param fontManager Input font manager instance
77
+ * @param options Input formating options
78
+ */
79
+ constructor(style: TextStyle, styleManager: StyleManager, fontManager: FontManager, options: MTextFormatOptions);
80
+ get fontManager(): FontManager;
81
+ get styleManager(): StyleManager;
82
+ get textStyle(): TextStyle;
83
+ /**
84
+ * Total height of all lines of text
85
+ */
86
+ get totalHeight(): number;
87
+ /**
88
+ * The maximum width of one text line
89
+ */
90
+ get maxWidth(): number;
91
+ /**
92
+ * The direction that the text string follows from its start to its finish.
93
+ */
94
+ get flowDirection(): MTextFlowDirection;
95
+ /**
96
+ * The default horizontal alignment of one text line
97
+ */
98
+ get defaultHorizontalAlignment(): MTextParagraphAlignment;
99
+ /**
100
+ * The default scale factor of character width
101
+ */
102
+ get defaultWidthFactor(): number;
103
+ /**
104
+ * The default font size of texts
105
+ */
106
+ get defaultFontSize(): number;
107
+ /**
108
+ * The default line space factor
109
+ */
110
+ get defaultLineSpaceFactor(): number;
111
+ /**
112
+ * Font name of current character
113
+ */
114
+ get currentFont(): string;
115
+ /**
116
+ * The current horizontal alignment of one text line
117
+ */
118
+ get currentHorizontalAlignment(): MTextParagraphAlignment;
119
+ /**
120
+ * Scale factor of current font height. The scale factor isn't set by users in text editor.
121
+ * It is one scale factor binded to font. For ttf or woff fonts, it is calculated by certain
122
+ * algorithm. For shx font, it is alway equal to 1.
123
+ */
124
+ get currentFontScaleFactor(): number;
125
+ /**
126
+ * Font size of current character
127
+ */
128
+ get currentFontSize(): number;
129
+ /**
130
+ * Font size scale factor of current character
131
+ */
132
+ get currentFontSizeScaleFactor(): number;
133
+ /**
134
+ * The height of current line of texts
135
+ */
136
+ get currentLineHeight(): number;
137
+ /**
138
+ * The maximum font size in current line. Characters in one line may have different font and font
139
+ * size. So we need to store the maximum font size in current line in order to calculate the height
140
+ * of current line.
141
+ */
142
+ get currentMaxFontSize(): number;
143
+ /**
144
+ * The current space setting between two characters
145
+ */
146
+ get currentWordSpace(): number;
147
+ /**
148
+ * The current scale factor of character width
149
+ */
150
+ get currentWidthFactor(): number;
151
+ /**
152
+ * The current text color
153
+ */
154
+ get currentColor(): number;
155
+ /**
156
+ * All of THREE.js objects in current line. It contains objects in all of sections of this line.
157
+ */
158
+ get currentLineObjects(): THREE.Object3D<THREE.Object3DEventMap>[];
159
+ /**
160
+ * The horizental offset of current character in this line
161
+ */
162
+ get hOffset(): number;
163
+ set hOffset(value: number);
164
+ /**
165
+ * The vertical offset of current character in this line
166
+ */
167
+ get vOffset(): number;
168
+ set vOffset(value: number);
169
+ /**
170
+ * Oblique angle of current character
171
+ */
172
+ get currentObliqueAngle(): number;
173
+ /**
174
+ * Process text format information
175
+ * @param item Input mtext inline codes
176
+ */
177
+ processFormat(item: ChangedProperties): void;
178
+ /**
179
+ * Render the specified texts
180
+ * @param item Input texts to render
181
+ */
182
+ processText(tokens: Generator<MTextToken>): THREE.Group<THREE.Object3DEventMap>;
183
+ private processGeometries;
184
+ private processWord;
185
+ private processStack;
186
+ private processBlank;
187
+ private processChar;
188
+ processLastLine(): void;
189
+ private initLineParams;
190
+ private changeFontHeight;
191
+ private changeFont;
192
+ private changeFontSizeScaleFactor;
193
+ /**
194
+ * Calcuate font size, line space, line height and other parameters.
195
+ */
196
+ private calcuateLineParams;
197
+ /**
198
+ * Get text shape of the specified character
199
+ * @param char Input one character
200
+ * @returns Return the text shape of the specified character
201
+ */
202
+ private getCharShape;
203
+ private startNewLine;
204
+ /**
205
+ * Apply translation on the specified buffer geometries according to text alignment setting
206
+ */
207
+ private processAlignment;
208
+ /**
209
+ * In AutoCAD, the width of a regular space character (ASCII 32, the space key on the keyboard) in MText
210
+ * depends on the current font and text height, and is not a fixed value.
211
+ * Specifically:
212
+ * - Space width ≈ Text height × space width ratio defined by the font
213
+ * - For common TrueType fonts (like Arial), the space width is typically about 1/4 to 1/3 of the text height.
214
+ * For example, if the text height is 10 (units), the space width would be approximately 2.5 to 3.3 units.
215
+ * - For SHX fonts (AutoCAD's built-in vector fonts, such as txt.shx), the space width is often half the text height.
216
+ * So if the text height is 10, the space width is typically 5 units.
217
+ */
218
+ private calculateBlankWidth;
219
+ /**
220
+ * Convert the text shape geometries to three.js object
221
+ * @param geometries Input text shape geometries
222
+ * @returns Return three.js object created from the specified text shape geometries
223
+ */
224
+ private toThreeObject;
225
+ }
@@ -0,0 +1,11 @@
1
+ import * as THREE from 'three';
2
+ /**
3
+ * Class to manage basic text style
4
+ */
5
+ export declare class StyleManager {
6
+ private lineBasicMaterials;
7
+ private meshBasicMaterials;
8
+ unsupportedTextStyles: Record<string, number>;
9
+ getMeshBasicMaterial(color: number): THREE.Material;
10
+ getLineBasicMaterial(color: number): THREE.Material;
11
+ }
@@ -0,0 +1,62 @@
1
+ export interface Point3d {
2
+ x: number;
3
+ y: number;
4
+ z: number;
5
+ }
6
+ export interface Point2d {
7
+ x: number;
8
+ y: number;
9
+ }
10
+ export declare enum MTextFlowDirection {
11
+ LEFT_TO_RIGHT = 1,
12
+ RIGHT_TO_LEFT = 2,
13
+ TOP_TO_BOTTOM = 3,
14
+ BOTTOM_TO_TOP = 4,
15
+ BY_STYLE = 5
16
+ }
17
+ export declare enum MTextAttachmentPoint {
18
+ TopLeft = 1,
19
+ TopCenter = 2,
20
+ TopRight = 3,
21
+ MiddleLeft = 4,
22
+ MiddleCenter = 5,
23
+ MiddleRight = 6,
24
+ BottomLeft = 7,
25
+ BottomCenter = 8,
26
+ BottomRight = 9
27
+ }
28
+ export interface MTextData {
29
+ text: string;
30
+ height: number;
31
+ width: number;
32
+ position: Point3d;
33
+ rotation?: number;
34
+ directionVector?: Point3d;
35
+ attachmentPoint?: MTextAttachmentPoint;
36
+ drawingDirection?: MTextFlowDirection;
37
+ lineSpaceFactor?: number;
38
+ widthFactor?: number;
39
+ }
40
+ /**
41
+ * Text style
42
+ */
43
+ export interface TextStyle {
44
+ name: string;
45
+ standardFlag: number;
46
+ fixedTextHeight: number;
47
+ widthFactor: number;
48
+ obliqueAngle: number;
49
+ textGenerationFlag: number;
50
+ lastHeight: number;
51
+ font: string;
52
+ bigFont: string;
53
+ extendedFont?: string;
54
+ color: number;
55
+ }
56
+ /**
57
+ * Color settings used to decided font color
58
+ */
59
+ export interface ColorSettings {
60
+ byLayerColor: number;
61
+ byBlockColor: number;
62
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@mlightcad/mtext-renderer",
3
+ "version": "0.2.0",
4
+ "description": "AutoCAD MText renderer based on Three.js",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "package.json"
12
+ ],
13
+ "keywords": [
14
+ "autocad",
15
+ "mtext",
16
+ "three.js",
17
+ "renderer",
18
+ "3d",
19
+ "text"
20
+ ],
21
+ "author": "",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "@mlightcad/mtext-parser": "^1.1.1",
25
+ "@mlightcad/shx-parser": "^1.0.4",
26
+ "opentype.js": "^1.3.4",
27
+ "idb": "^8.0.3"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^20.10.6",
31
+ "@types/opentype.js": "^1.3.8",
32
+ "@types/three": "^0.172.0",
33
+ "@typescript-eslint/eslint-plugin": "^8.32.1",
34
+ "@typescript-eslint/parser": "^8.32.1",
35
+ "cp-cli": "^2.0.0",
36
+ "eslint": "^9.26.0",
37
+ "eslint-config-prettier": "^10.1.5",
38
+ "eslint-plugin-prettier": "^5.4.0",
39
+ "prettier": "^3.1.1",
40
+ "typescript": "~5.3.3",
41
+ "vite": "^5.0.0",
42
+ "vite-plugin-dts": "^3.7.0"
43
+ },
44
+ "peerDependencies": {
45
+ "three": "^0.172.0"
46
+ },
47
+ "scripts": {
48
+ "build": "vite build",
49
+ "build:example": "vite build --config examples/vite.config.ts",
50
+ "example": "npm run build:example && vite --config examples/vite.config.ts",
51
+ "lint": "eslint \"src/**/*.{ts,tsx,js,jsx}\" \"examples/**/*.{ts,tsx,js,jsx}\"",
52
+ "lint:fix": "eslint \"src/**/*.{ts,tsx,js,jsx}\" \"examples/**/*.{ts,tsx,js,jsx}\" --fix",
53
+ "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\" \"examples/**/*.{ts,tsx,js,jsx}\""
54
+ }
55
+ }