@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.
@@ -49,22 +49,39 @@ export interface MTextFormatOptions {
49
49
  collectCharBoxes?: boolean;
50
50
  }
51
51
  /**
52
- * This class represents lines of texts.
52
+ * Converts parsed MText tokens into positioned THREE.js geometry.
53
+ *
54
+ * Owns line breaking, inline/paragraph formatting, stack fractions, alignment,
55
+ * and per-character bounding boxes used for picking.
53
56
  */
54
57
  export declare class MTextProcessor {
58
+ /** Active text style (font, height, oblique, big font, etc.). */
55
59
  private _style;
60
+ /** Layer/block color resolution settings for ByLayer and ByBlock colors. */
56
61
  private _colorSettings;
62
+ /** Factory for mesh and line materials used when flushing geometry batches. */
57
63
  private _styleManager;
64
+ /** Resolves font files, glyph shapes, and font-format-specific metrics. */
58
65
  private _fontManager;
66
+ /** Layout and formatting options supplied at construction time. */
59
67
  private _options;
68
+ /** Accumulated vertical extent of completed lines, excluding the active line. */
60
69
  private _totalHeight;
70
+ /** Horizontal pen position within the current visual line. */
61
71
  private _hOffset;
72
+ /** Maximum logical pen advance seen across all processed visual lines. */
62
73
  private _maxLineAdvance;
74
+ /** Vertical baseline position of the current visual line. */
63
75
  private _vOffset;
76
+ /** Number of visual lines processed so far (1-based while rendering). */
64
77
  private _lineCount;
78
+ /** THREE.js objects created for the current visual line before alignment. */
65
79
  private _currentLineObjects;
80
+ /** Saved {@link RenderContext} snapshots for nested `{}` formatting groups. */
66
81
  private _contextStack;
82
+ /** Active inline formatting and font metric state. */
67
83
  private _currentContext;
84
+ /** Largest font size encountered on the current visual line. */
68
85
  private _maxFontSize;
69
86
  /**
70
87
  * The current horizontal alignment for the paragraph.
@@ -76,25 +93,45 @@ export declare class MTextProcessor {
76
93
  * so it persists until explicitly changed by another paragraph alignment command.
77
94
  */
78
95
  private _currentHorizontalAlignment;
96
+ /** Whether the most recently appended char box targeted mesh or line geometry. */
79
97
  private _lastCharBoxTarget;
98
+ /** True once a visible glyph has been placed on the current visual line. */
80
99
  private _lineHasRenderableChar;
100
+ /**
101
+ * Font size recorded when advancing from an empty line; used to correct
102
+ * vertical position when the first glyph on the next line uses a different height.
103
+ */
81
104
  private _pendingEmptyLineFontSizeAdjust?;
105
+ /** Vertical layout metadata for each completed visual line. */
82
106
  private _lineLayouts;
107
+ /** Character indices where automatic visual line breaks occurred. */
83
108
  private _lineBreakIndices;
109
+ /** Running count of logical characters emitted into flushed geometry groups. */
84
110
  private _processedCharCount;
111
+ /** Line glyph entries collected for batch geometry merge within a style segment. */
112
+ private _lineBatchEntries;
113
+ /** Current paragraph first-line indent in drawing units. */
85
114
  private _currentIndent;
115
+ /** Current paragraph left margin in drawing units. */
86
116
  private _currentLeftMargin;
117
+ /** Current paragraph right margin in drawing units. */
87
118
  private _currentRightMargin;
88
119
  /**
89
120
  * Construct one instance of this class and initialize some properties with default values.
90
121
  * @param style Input text style
122
+ * @param colorSettings Layer/block color resolution settings
91
123
  * @param styleManager Input text style manager instance
92
124
  * @param fontManager Input font manager instance
93
125
  * @param options Input formating options
94
126
  */
95
127
  constructor(style: TextStyle, colorSettings: ColorSettings, styleManager: StyleManager, fontManager: FontManager, options: MTextFormatOptions);
128
+ /**
129
+ * Font manager used to resolve glyphs, shapes, and font metrics.
130
+ */
96
131
  get fontManager(): FontManager;
132
+ /** Style manager used to create mesh and line materials. */
97
133
  get styleManager(): StyleManager;
134
+ /** Active CAD text style for the entity being rendered. */
98
135
  get textStyle(): TextStyle;
99
136
  /**
100
137
  * Total height of all lines of text
@@ -180,24 +217,31 @@ export declare class MTextProcessor {
180
217
  * All of THREE.js objects in current line. It contains objects in all of sections of this line.
181
218
  */
182
219
  get currentLineObjects(): THREE.Object3D<THREE.Object3DEventMap>[];
220
+ /** Per-line vertical layout records accumulated during rendering. */
183
221
  get lineLayouts(): LineLayout[];
184
222
  /**
185
223
  * The horizental offset of current character in this line
186
224
  */
187
225
  get hOffset(): number;
226
+ /** @param value New horizontal pen position within the current line. */
188
227
  set hOffset(value: number);
189
228
  /**
190
229
  * The vertical offset of current character in this line
191
230
  */
192
231
  get vOffset(): number;
232
+ /** @param value New vertical baseline position for the current line. */
193
233
  set vOffset(value: number);
234
+ /** Current paragraph first-line indent in drawing units. */
194
235
  get currentIndent(): number;
236
+ /** Current paragraph left margin in drawing units. */
195
237
  get currentLeftMargin(): number;
238
+ /** Current paragraph right margin in drawing units. */
196
239
  get currentRightMargin(): number;
240
+ /** Usable line width after subtracting left and right paragraph margins. */
197
241
  get maxLineWidth(): number;
198
242
  /**
199
243
  * Process text format information
200
- * @param item Input mtext inline codes
244
+ * @param item Inline formatting command or restore snapshot from the parser.
201
245
  */
202
246
  processFormat(item: ChangedProperties): void;
203
247
  /**
@@ -214,6 +258,8 @@ export declare class MTextProcessor {
214
258
  * - width/height/tracking factors
215
259
  * - paragraph alignment and margins
216
260
  * - underline/overline/strike-through flags
261
+ *
262
+ * @param changes Full property snapshot to apply to the current render context.
217
263
  */
218
264
  private applyPropertyChanges;
219
265
  /**
@@ -272,15 +318,29 @@ export declare class MTextProcessor {
272
318
  * and starting a new line with indent applied.
273
319
  * @param geometries Current text geometries to process
274
320
  * @param lineGeometries Current line geometries to process
321
+ * @param meshCharBoxes Mesh char boxes to flush with the current line
322
+ * @param lineCharBoxes Line char boxes to flush with the current line
275
323
  * @param group The group to add processed geometries to
276
324
  */
277
325
  private startNewParagraph;
278
326
  /**
279
327
  * Renders one SHX shape glyph for AutoCAD SHAPE entities.
280
328
  *
281
- * The glyph is resolved by {@link shapeName} first, then by {@link shapeNumber}.
329
+ * @param shapeName Optional SHX shape name from the SHAPE entity.
330
+ * @param shapeNumber Optional SHX shape number from the SHAPE entity.
331
+ * @returns A single styled THREE.js object for the resolved shape, or `undefined` when lookup fails.
282
332
  */
283
333
  processShapeGlyph(shapeName?: string, shapeNumber?: number): THREE.Object3D | undefined;
334
+ /**
335
+ * Resolves a SHX shape glyph by name and/or numeric code for SHAPE entities.
336
+ *
337
+ * Name lookup is attempted first; numeric lookup is used when the name is absent
338
+ * or does not match. Unlike MText, failed SHAPE lookups do not fall back to `?`.
339
+ *
340
+ * @param shapeName Optional SHX shape name.
341
+ * @param shapeNumber Optional SHX shape number.
342
+ * @returns Resolved shape and display label, or `undefined` when not found.
343
+ */
284
344
  private resolveShapeGlyph;
285
345
  /**
286
346
  * Builds geometry for one glyph and appends it to the output buffers.
@@ -288,47 +348,235 @@ export declare class MTextProcessor {
288
348
  * @returns Horizontal advance width after width factor and oblique skew.
289
349
  */
290
350
  private buildShapeGeometry;
351
+ /**
352
+ * Builds the world transform for one glyph (width factor, oblique, bold, translate).
353
+ *
354
+ * @param charX Horizontal glyph origin in drawing space.
355
+ * @param charY Vertical glyph origin in drawing space.
356
+ * @param charHeight Layout font height used for oblique advance calculation.
357
+ * @returns Composite transform matrix and extra horizontal advance introduced by oblique skew.
358
+ */
359
+ private buildCharTransformMatrix;
360
+ /**
361
+ * Appends one glyph's geometry to the active batch buffers and optional char boxes.
362
+ *
363
+ * Mesh fonts (`ShapeGeometry`) are transformed immediately; line fonts are queued in
364
+ * {@link _lineBatchEntries} for later merge via {@link TextGeometryBuilder.mergeLineGeometries}.
365
+ *
366
+ * @param shape Source text shape for the glyph.
367
+ * @param label Character label stored on geometry and char boxes.
368
+ * @param canonical Untransformed glyph geometry from the font.
369
+ * @param matrix World transform to apply to the glyph.
370
+ * @param geometries Accumulator for mesh (`ShapeGeometry`) primitives.
371
+ * @param meshCharBoxes Accumulator for mesh-glyph picking boxes.
372
+ * @param lineCharBoxes Accumulator for line-glyph picking boxes.
373
+ */
374
+ private appendCharGeometry;
375
+ /**
376
+ * Creates a two-point line decoration geometry (underline, overline, strike-through).
377
+ *
378
+ * @param lineGeometries Accumulator for line-based decoration geometry.
379
+ * @param vertices Six floats: start `(x,y,z)` and end `(x,y,z)` in drawing space.
380
+ */
381
+ private pushDecorationLine;
291
382
  /**
292
383
  * Render the specified texts
293
- * @param item Input texts to render
384
+ * @param tokens Parsed MText token stream from the parser.
385
+ * @returns A {@link THREE.Group} containing merged line/mesh objects and layout metadata.
294
386
  */
295
387
  processText(tokens: Generator<MTextToken>): THREE.Group<THREE.Object3DEventMap>;
388
+ /**
389
+ * Flushes pending geometry and char boxes into a styled THREE.js object on `group`.
390
+ *
391
+ * When only char boxes exist (spaces, empty lines), creates a marker object with
392
+ * layout metadata and no visible geometry.
393
+ *
394
+ * @param geometries Pending mesh geometries for the current style segment.
395
+ * @param lineGeometries Pending line geometries and decorations for the current style segment.
396
+ * @param meshCharBoxes Mesh-glyph char boxes collected since the last flush.
397
+ * @param lineCharBoxes Line-glyph char boxes collected since the last flush.
398
+ * @param group Parent group receiving the created object.
399
+ * @param charBoxType Char-box classification stored on the flushed object.
400
+ */
296
401
  private processGeometries;
402
+ /**
403
+ * Lays out and renders one parser word token, breaking to a new visual line when needed.
404
+ *
405
+ * @param word Character sequence for a single word token.
406
+ * @param geometries Mesh geometry accumulator for the active style segment.
407
+ * @param lineGeometries Line geometry accumulator for the active style segment.
408
+ * @param meshCharBoxes Mesh char-box accumulator.
409
+ * @param lineCharBoxes Line char-box accumulator.
410
+ */
297
411
  private processWord;
412
+ /**
413
+ * Renders an MText stack token (`\S...;`) as fraction, superscript, subscript, or tolerance layout.
414
+ *
415
+ * @param stackData Parser stack payload: `[numerator, denominator, divider]`.
416
+ * @param geometries Mesh geometry accumulator.
417
+ * @param lineGeometries Line geometry accumulator.
418
+ * @param meshCharBoxes Mesh char-box accumulator.
419
+ * @param lineCharBoxes Line char-box accumulator.
420
+ */
298
421
  private processStack;
422
+ /**
423
+ * Records a zero-height char box for a stack fraction divider in logical char order.
424
+ *
425
+ * @param startX Horizontal start of the fraction bar in drawing space.
426
+ * @param currentVOffset Vertical baseline of the stack relative to the main line.
427
+ * @param width Horizontal extent of the fraction bar.
428
+ * @param meshCharBoxes Mesh char-box accumulator.
429
+ * @param lineCharBoxes Line char-box accumulator.
430
+ */
299
431
  private recordStackDivider;
300
432
  /**
301
433
  * Convert a legacy top-anchored vOffset (used by stack/sub/sup logic) into
302
434
  * the current baseline-anchored coordinate system.
435
+ *
436
+ * @param legacyTopAlignedVOffset Vertical position in the legacy top-anchored system.
437
+ * @param fontSize Font size associated with the legacy offset.
438
+ * @returns Baseline-anchored vertical offset for the current layout model.
303
439
  */
304
440
  private convertTopAlignedVOffset;
441
+ /**
442
+ * Advances the pen by one space width and optionally records a space char box.
443
+ *
444
+ * @param meshCharBoxes Mesh char-box accumulator.
445
+ * @param lineCharBoxes Line char-box accumulator.
446
+ */
305
447
  private processBlank;
448
+ /**
449
+ * Records the character index where a visual line break occurs.
450
+ *
451
+ * @param meshCharBoxes Optional pending mesh char boxes included in the break index.
452
+ * @param lineCharBoxes Optional pending line char boxes included in the break index.
453
+ */
306
454
  private recordVisualLineBreak;
455
+ /**
456
+ * Appends vertical layout metadata for the current visual line to {@link _lineLayouts}.
457
+ */
307
458
  private recordCurrentLineLayout;
459
+ /**
460
+ * Renders a percent-control-code symbol (`%%...`) or its literal fallback character.
461
+ *
462
+ * @param data Parser payload describing the percent symbol kind and lookup data.
463
+ * @param geometries Mesh geometry accumulator.
464
+ * @param lineGeometries Line geometry accumulator.
465
+ * @param meshCharBoxes Mesh char-box accumulator.
466
+ * @param lineCharBoxes Line char-box accumulator.
467
+ */
308
468
  private processPercentSymbol;
469
+ /**
470
+ * Renders one character glyph, including decorations and line-break handling.
471
+ *
472
+ * Missing glyphs are treated as spaces. When the pen exceeds {@link maxLineWidth},
473
+ * a visual line break is recorded before placement.
474
+ *
475
+ * @param char Character to render.
476
+ * @param geometries Mesh geometry accumulator.
477
+ * @param lineGeometries Line geometry accumulator.
478
+ * @param meshCharBoxes Mesh char-box accumulator.
479
+ * @param lineCharBoxes Line char-box accumulator.
480
+ * @param shapeOverride Optional pre-resolved shape (used by percent symbols and stacks).
481
+ */
309
482
  private processChar;
483
+ /**
484
+ * Applies horizontal alignment to the final visual line after all tokens are processed.
485
+ */
310
486
  private processLastLine;
487
+ /**
488
+ * Initializes line metric state from the current font and default text height.
489
+ */
311
490
  private initLineParams;
491
+ /**
492
+ * Switches the active font family and recomputes derived line metrics.
493
+ *
494
+ * @param fontName Font family name from an inline `\f` command or text style.
495
+ */
312
496
  private changeFont;
313
497
  /**
314
- * Calcuate font size, line space, line height and other parameters.
498
+ * Recalculates font scale factor and drawing-space font size from defaults and scale factors.
499
+ *
500
+ * @param newFontHeight Optional absolute font height override in drawing units.
315
501
  */
316
502
  private calcuateLineParams;
317
503
  /**
318
- * Get text shape of the specified character
319
- * @param char Input one character
320
- * @returns Return the text shape of the specified character
504
+ * Returns whether a shape contributes visible stroke or mesh geometry for rendering.
505
+ *
506
+ * @param shape Candidate text shape.
507
+ * @param char Character being resolved (spaces may still be renderable when width > 0).
508
+ * @returns `true` when the shape should be drawn instead of replaced with the not-found glyph.
321
509
  */
322
510
  private shapeHasStrokeGeometry;
511
+ /**
512
+ * Resolves the best available shape for a percent-control-code symbol.
513
+ *
514
+ * Tries symbol-font lookup codes first, then falls back to the primary font glyph.
515
+ *
516
+ * @param data Parser percent-symbol payload.
517
+ * @returns A renderable text shape for the symbol.
518
+ */
323
519
  private resolvePercentSymbolShape;
520
+ /**
521
+ * Resolves the text shape for one character using primary font, big font, defaults, and symbol fonts.
522
+ *
523
+ * Updates {@link _maxFontSize} when the active font size exceeds the current line maximum.
524
+ * Falls back to the not-found glyph when no renderable geometry is available.
525
+ *
526
+ * @param char Character to resolve.
527
+ * @returns Renderable text shape and the font that supplied it.
528
+ */
529
+ private resolveCharShape;
530
+ private canProbeFontOwnership;
531
+ private fontHasChar;
532
+ /**
533
+ * Returns the renderable shape for one character, discarding font provenance metadata.
534
+ *
535
+ * @param char Character to resolve.
536
+ * @returns Renderable text shape, or `undefined` when resolution fails entirely.
537
+ */
324
538
  private getCharShape;
539
+ /**
540
+ * Finalizes the current visual line and starts the next one.
541
+ *
542
+ * Records layout metadata, resets horizontal pen state, updates vertical offset,
543
+ * and reapplies paragraph alignment to objects on the completed line.
544
+ *
545
+ * @param collectBreakIndex When `true`, records a char index for automatic wrapping.
546
+ */
325
547
  private advanceToNextLine;
548
+ /**
549
+ * Updates {@link _maxLineAdvance} from the current line's horizontal pen position.
550
+ */
326
551
  private captureCurrentLineAdvance;
552
+ /**
553
+ * Counts logical characters represented by pending char boxes for break-index accounting.
554
+ *
555
+ * Stack char boxes collapse numerator, divider, and denominator into one logical token.
556
+ *
557
+ * @param meshCharBoxes Pending mesh char boxes.
558
+ * @param lineCharBoxes Pending line char boxes.
559
+ * @param charBoxType Classification of the geometry group being flushed.
560
+ * @returns Number of logical characters contributed by the pending boxes.
561
+ */
327
562
  private countFinalCharBoxes;
563
+ /**
564
+ * Corrects vertical offset when the first glyph on a line uses a different font size
565
+ * than the empty line that preceded it.
566
+ */
328
567
  private applyPendingEmptyLineYAdjust;
568
+ /**
569
+ * Chooses whether subsequent char boxes should attach to mesh or line geometry owners.
570
+ *
571
+ * @param meshCharBoxes Mesh char boxes already collected on the current line.
572
+ * @param lineCharBoxes Line char boxes already collected on the current line.
573
+ * @returns Target geometry family for the next char box.
574
+ */
329
575
  private resolveCharBoxTarget;
330
576
  /**
331
- * Apply translation on the specified buffer geometries according to text alignment setting
577
+ * Applies translation on the specified buffer geometries according to text alignment setting.
578
+ *
579
+ * Translates both geometry vertices and attached char boxes on the current visual line.
332
580
  */
333
581
  private processAlignment;
334
582
  /**
@@ -337,22 +585,45 @@ export declare class MTextProcessor {
337
585
  * AutoCAD uses each font's own space glyph metrics (SHX pen advance or TrueType
338
586
  * horizontal advance). A fixed fraction of text height (e.g. 50% for SHX) is only
339
587
  * a rough fallback when the font has no space definition.
588
+ *
589
+ * @param font Font family name.
590
+ * @param fontSize Font size in drawing units.
591
+ * @returns Horizontal advance width for one space character.
340
592
  */
341
593
  private calculateBlankWidthForFont;
342
594
  /**
343
- * Merges line-based geometries for LineSegments. SHX glyphs use indexed
344
- * BufferGeometry while fraction/divider decorations omit an index; normalize
345
- * to non-indexed form so mergeGeometries accepts the batch.
595
+ * Converts pending mesh and line geometries into a styled THREE.js object.
596
+ *
597
+ * @param geometries Mesh (`ShapeGeometry`) primitives for the current style segment.
598
+ * @param lineGeometries Line primitives and decorations for the current style segment.
599
+ * @param meshCharBoxes Mesh char boxes to attach to the created object.
600
+ * @param lineCharBoxes Line char boxes to attach to the created object.
601
+ * @param charBoxType Char-box classification stored on the created object.
602
+ * @returns A mesh, line segments object, or small group containing both.
346
603
  */
347
- private mergeLineGeometries;
604
+ private toThreeObject;
348
605
  /**
349
- * Convert the text shape geometries to three.js object
350
- * @param geometries Input text shape geometries
351
- * @returns Return three.js object created from the specified text shape geometries
606
+ * Multiplies the active font-size scale factor and recomputes line metrics.
607
+ *
608
+ * @param value Relative scale multiplier (for example `0.7` for superscript).
352
609
  */
353
- private toThreeObject;
354
610
  private changeFontSizeScaleFactor;
611
+ /**
612
+ * Sets an absolute font height override and recomputes line metrics.
613
+ *
614
+ * @param value Font height in drawing units.
615
+ */
355
616
  private changeFontHeight;
617
+ /**
618
+ * Resolves the initial text color from entity color settings.
619
+ *
620
+ * @returns Base color as `0xRRGGBB`.
621
+ */
356
622
  private resolveBaseColor;
623
+ /**
624
+ * Builds color settings for material creation from the current render context.
625
+ *
626
+ * @returns Color settings including the active inline color snapshot.
627
+ */
357
628
  private getMaterialColorSettings;
358
629
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-renderer",
3
- "version": "0.11.6",
3
+ "version": "0.11.8",
4
4
  "description": "AutoCAD MText renderer based on Three.js",
5
5
  "license": "MIT",
6
6
  "author": "MLight Lee <mlight.lee@outlook.com>",
@@ -44,7 +44,7 @@
44
44
  ],
45
45
  "dependencies": {
46
46
  "@mlightcad/mtext-parser": "^1.5.0",
47
- "@mlightcad/shx-parser": "^1.3.5",
47
+ "@mlightcad/shx-parser": "^1.4.4",
48
48
  "iconv-lite": "^0.7.0",
49
49
  "idb": "^8.0.3",
50
50
  "opentype.js": "^2.0.0"