@mlightcad/mtext-input-box 0.2.9 → 0.2.10

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.
@@ -1,5 +1,5 @@
1
1
  import type { Box, CursorStyle, SelectionStyle } from '@mlightcad/text-box-cursor';
2
- import type { ColorSettings, MTextColor, MTextParagraphAlignment, TextStyle } from '@mlightcad/mtext-renderer';
2
+ import type { ColorSettings, MTextAttachmentPoint, MTextColor, MTextParagraphAlignment, TextStyle } from '@mlightcad/mtext-renderer';
3
3
  import type * as THREE from 'three';
4
4
  /** Vertical script style for a character run. */
5
5
  export type CharScript = 'normal' | 'superscript' | 'subscript';
@@ -176,6 +176,10 @@ export interface MTextInputBoxOptions {
176
176
  width: number;
177
177
  /** Optional world-space origin of the editor container. */
178
178
  position?: THREE.Vector3;
179
+ /**
180
+ * Initial MTEXT attachment (DXF group 71). Defaults to top-left when omitted.
181
+ */
182
+ initialAttachmentPoint?: MTextAttachmentPoint;
179
183
  /**
180
184
  * Default text style passed to `@mlightcad/mtext-renderer`.
181
185
  *
@@ -1,5 +1,5 @@
1
1
  import { type DebugVisibility } from '@mlightcad/text-box-cursor';
2
- import { type TextStyle } from '@mlightcad/mtext-renderer';
2
+ import { MTextAttachmentPoint, type TextStyle } from '@mlightcad/mtext-renderer';
3
3
  import * as THREE from 'three';
4
4
  import { defaultCharFormat, sameFormat } from './format';
5
5
  import type { CharFormat, CursorLayoutData, CursorDirection, EditorState, MTextInputBoxEvent, MTextInputBoxOptions, MTextToolbarTheme, SelectionDirection } from './types';
@@ -157,6 +157,8 @@ export declare class MTextInputBox {
157
157
  setAttachmentPoint(attachmentPoint: string): void;
158
158
  /** Returns the current attachment as a two-letter ribbon / DXF-style code. */
159
159
  getAttachmentPointCode(): string;
160
+ /** Returns the current attachment as an {@link MTextAttachmentPoint} value (DXF 71). */
161
+ getMTextAttachmentPoint(): MTextAttachmentPoint;
160
162
  /** Toggles selected alphabetic text between upper and lower case. */
161
163
  toggleCase(): void;
162
164
  /** Returns renderer default text style defined by `textStyle`. */
@@ -221,6 +223,13 @@ export declare class MTextInputBox {
221
223
  private ensureFontsLoaded;
222
224
  private collectFontNamesForText;
223
225
  private updateCursorData;
226
+ /**
227
+ * Extends {@link layoutContainer} vertically so empty logical rows (no glyphs)
228
+ * remain inside the chrome bounds. Intentionally ignores non-empty rows: their
229
+ * `LineInfo` may still use inflated renderer line strips, while
230
+ * {@link computeEditorVerticalBounds} already tightened using glyph boxes.
231
+ */
232
+ private unionLayoutContainerWithCursorLines;
224
233
  /**
225
234
  * Editor vertical bounds for the selection box / bounding overlay.
226
235
  * Renderer `layout.lines` strips are often taller than actual glyphs (leading
@@ -303,6 +312,11 @@ export declare class MTextInputBox {
303
312
  private toScriptFromStyle;
304
313
  private createDefaultParseContext;
305
314
  private createNormalizedDocument;
315
+ /**
316
+ * World-space point on the layout bounds that matches the given MTEXT
317
+ * attachment (DXF 71), using the same box convention as {@link updateBoundingBoxGeometry}.
318
+ */
319
+ private computeAttachmentAnchorOnBounds;
306
320
  private mapRibbonParagraphAlignment;
307
321
  private mapRibbonAttachmentCode;
308
322
  private attachmentPointToRibbonCode;
@@ -5,6 +5,25 @@ import { DEFAULT_FONT_FAMILY, defaultCharFormat, sameFormat } from './format';
5
5
  import { hasNodeStyle, MTextDocument } from '../model';
6
6
  import { EditorUiAdapter } from '../controller';
7
7
  import { MTextToolbar } from '../ui/toolbar';
8
+ /**
9
+ * Local X of the MTEXT column's left edge relative to the insertion point (DXF 71),
10
+ * matching {@link MText.calculateAnchorPoint} / `mtext-renderer` frame semantics.
11
+ * Uses numeric codes so unit tests can partially mock `@mlightcad/mtext-renderer`.
12
+ */
13
+ function attachmentColumnMinLocal(width, attachment) {
14
+ const w = Math.max(1, width);
15
+ const a = attachment;
16
+ // Left column: 1 TL, 4 ML, 7 BL, 10 baseline-left
17
+ if (a === undefined || a === 1 || a === 4 || a === 7 || a === 10)
18
+ return 0;
19
+ // Center column: 2 TC, 5 MC, 8 BC, 11 baseline-center
20
+ if (a === 2 || a === 5 || a === 8 || a === 11)
21
+ return -w / 2;
22
+ // Right column: 3 TR, 6 MR, 9 BR, 12 baseline-right
23
+ if (a === 3 || a === 6 || a === 9 || a === 12)
24
+ return -w;
25
+ return 0;
26
+ }
8
27
  /**
9
28
  * Three.js based MText editor component with core text editing interaction.
10
29
  */
@@ -245,6 +264,9 @@ export class MTextInputBox {
245
264
  this.colorSettings = options.colorSettings;
246
265
  this.width = Math.max(1, options.width);
247
266
  this.position = options.position?.clone() ?? new THREE.Vector3(0, 0, 0);
267
+ if (options.initialAttachmentPoint !== undefined) {
268
+ this.editorAttachmentPoint = options.initialAttachmentPoint;
269
+ }
248
270
  this.enableWordWrap = options.enableWordWrap ?? true;
249
271
  const textStyle = options.textStyle ?? MTextInputBox.DEFAULT_TEXT_STYLE;
250
272
  const fixedTextHeight = Math.max(1, Number.isFinite(textStyle.fixedTextHeight)
@@ -644,6 +666,32 @@ export class MTextInputBox {
644
666
  const next = this.mapRibbonAttachmentCode(attachmentPoint);
645
667
  if (next === undefined || next === this.editorAttachmentPoint)
646
668
  return;
669
+ // Keep the on-screen text frame fixed: DXF insertion point is the selected
670
+ // attachment location on the bounding box, so changing attachment must move
671
+ // `position` by the delta between the old and new anchor on the same box.
672
+ if (this.rendererReady && Number.isFinite(this.layoutContainer.width)) {
673
+ const left = this.position.x + this.layoutContainer.x;
674
+ const right = left + this.layoutContainer.width;
675
+ const bottom = this.position.y + this.layoutContainer.y;
676
+ const top = bottom + this.layoutContainer.height;
677
+ if (Number.isFinite(left) &&
678
+ Number.isFinite(right) &&
679
+ Number.isFinite(bottom) &&
680
+ Number.isFinite(top) &&
681
+ right >= left - 1e-9 &&
682
+ top >= bottom - 1e-9) {
683
+ const oldAnchor = this.computeAttachmentAnchorOnBounds(left, right, bottom, top, this.editorAttachmentPoint);
684
+ const newAnchor = this.computeAttachmentAnchorOnBounds(left, right, bottom, top, next);
685
+ this.position.x += newAnchor.x - oldAnchor.x;
686
+ this.position.y += newAnchor.y - oldAnchor.y;
687
+ this.cursorRenderer.setViewTransform({
688
+ x: this.position.x,
689
+ y: this.position.y,
690
+ scaleX: 1,
691
+ scaleY: 1
692
+ });
693
+ }
694
+ }
647
695
  this.editorAttachmentPoint = next;
648
696
  this.relayout();
649
697
  this.emit('change');
@@ -652,6 +700,10 @@ export class MTextInputBox {
652
700
  getAttachmentPointCode() {
653
701
  return this.attachmentPointToRibbonCode(this.editorAttachmentPoint);
654
702
  }
703
+ /** Returns the current attachment as an {@link MTextAttachmentPoint} value (DXF 71). */
704
+ getMTextAttachmentPoint() {
705
+ return this.editorAttachmentPoint;
706
+ }
655
707
  /** Toggles selected alphabetic text between upper and lower case. */
656
708
  toggleCase() {
657
709
  const selection = this.getSelectionRange();
@@ -1298,6 +1350,57 @@ export class MTextInputBox {
1298
1350
  this.cursorLogic.clearSelection();
1299
1351
  }
1300
1352
  this.syncStateFromCursor();
1353
+ // Renderer-driven vertical bounds can omit a trailing empty row when a single
1354
+ // inflated line strip overlaps all glyphs (we then keep only glyph extents).
1355
+ // Cursor geometry for empty rows still carries those rows (break fallbacks), so
1356
+ // union only empty lines — non-empty rows are already covered by glyph bounds,
1357
+ // and their LineInfo can still reflect oversized strips (reintroducing leading).
1358
+ const expanded = this.unionLayoutContainerWithCursorLines(this.layoutContainer);
1359
+ if (Math.abs(expanded.y - this.layoutContainer.y) > 1e-6 ||
1360
+ Math.abs(expanded.height - this.layoutContainer.height) > 1e-6) {
1361
+ this.layoutContainer = expanded;
1362
+ this.latestCursorLayoutData.containerBox = { ...this.layoutContainer };
1363
+ this.cursorLogic.updateData(this.layoutContainer, charBoxes, lineBreakIndices, lineLayouts);
1364
+ this.cursorLogic.moveTo(nextIndex, pendingLineHint);
1365
+ if (this.selectionStart !== this.selectionEnd) {
1366
+ this.cursorLogic.setSelection(this.selectionStart, this.selectionEnd);
1367
+ }
1368
+ else {
1369
+ this.cursorLogic.clearSelection();
1370
+ }
1371
+ this.syncStateFromCursor();
1372
+ }
1373
+ }
1374
+ /**
1375
+ * Extends {@link layoutContainer} vertically so empty logical rows (no glyphs)
1376
+ * remain inside the chrome bounds. Intentionally ignores non-empty rows: their
1377
+ * `LineInfo` may still use inflated renderer line strips, while
1378
+ * {@link computeEditorVerticalBounds} already tightened using glyph boxes.
1379
+ */
1380
+ unionLayoutContainerWithCursorLines(container) {
1381
+ const lines = this.cursorLogic.getLines();
1382
+ if (lines.length === 0)
1383
+ return container;
1384
+ let low = container.y;
1385
+ let high = container.y + container.height;
1386
+ for (const line of lines) {
1387
+ if (line.charCount !== 0)
1388
+ continue;
1389
+ const lineLo = line.y - line.height / 2;
1390
+ const lineHi = line.y + line.height / 2;
1391
+ if (Number.isFinite(lineLo))
1392
+ low = Math.min(low, lineLo);
1393
+ if (Number.isFinite(lineHi))
1394
+ high = Math.max(high, lineHi);
1395
+ }
1396
+ if (!Number.isFinite(low) || !Number.isFinite(high) || high < low) {
1397
+ return container;
1398
+ }
1399
+ return {
1400
+ ...container,
1401
+ y: low,
1402
+ height: high - low
1403
+ };
1301
1404
  }
1302
1405
  /**
1303
1406
  * Editor vertical bounds for the selection box / bounding overlay.
@@ -1405,13 +1508,11 @@ export class MTextInputBox {
1405
1508
  const local = this.toLocalBox(object.box);
1406
1509
  const { minY: containerTop, maxY: containerBottom } = this.computeEditorVerticalBounds(lineLayouts, charBoxes, local);
1407
1510
  const containerBox = {
1408
- x: local.x,
1511
+ x: attachmentColumnMinLocal(this.width, this.editorAttachmentPoint),
1409
1512
  y: containerTop,
1410
- width: local.width,
1513
+ width: Math.max(1, this.width),
1411
1514
  height: Math.max(0, containerBottom - containerTop)
1412
1515
  };
1413
- containerBox.x = 0;
1414
- containerBox.width = this.width;
1415
1516
  const minHeight = this.getFallbackLineAdvance();
1416
1517
  if (containerBox.height < minHeight) {
1417
1518
  const delta = minHeight - containerBox.height;
@@ -1486,7 +1587,7 @@ export class MTextInputBox {
1486
1587
  }
1487
1588
  return {
1488
1589
  containerBox: {
1489
- x: 0,
1590
+ x: attachmentColumnMinLocal(this.width, this.editorAttachmentPoint),
1490
1591
  y: minY,
1491
1592
  width: this.width,
1492
1593
  height: Math.max(1, -minY)
@@ -2473,6 +2574,40 @@ export class MTextInputBox {
2473
2574
  }
2474
2575
  return new MTextDocument(normalizedAst);
2475
2576
  }
2577
+ /**
2578
+ * World-space point on the layout bounds that matches the given MTEXT
2579
+ * attachment (DXF 71), using the same box convention as {@link updateBoundingBoxGeometry}.
2580
+ */
2581
+ computeAttachmentAnchorOnBounds(left, right, bottom, top, point) {
2582
+ const midX = (left + right) * 0.5;
2583
+ const midY = (bottom + top) * 0.5;
2584
+ const z = this.position.z;
2585
+ switch (point) {
2586
+ case MTextAttachmentPoint.TopLeft:
2587
+ return new THREE.Vector3(left, top, z);
2588
+ case MTextAttachmentPoint.TopCenter:
2589
+ return new THREE.Vector3(midX, top, z);
2590
+ case MTextAttachmentPoint.TopRight:
2591
+ return new THREE.Vector3(right, top, z);
2592
+ case MTextAttachmentPoint.MiddleLeft:
2593
+ return new THREE.Vector3(left, midY, z);
2594
+ case MTextAttachmentPoint.MiddleCenter:
2595
+ return new THREE.Vector3(midX, midY, z);
2596
+ case MTextAttachmentPoint.MiddleRight:
2597
+ return new THREE.Vector3(right, midY, z);
2598
+ case MTextAttachmentPoint.BottomLeft:
2599
+ case MTextAttachmentPoint.BaselineLeft:
2600
+ return new THREE.Vector3(left, bottom, z);
2601
+ case MTextAttachmentPoint.BottomCenter:
2602
+ case MTextAttachmentPoint.BaselineCenter:
2603
+ return new THREE.Vector3(midX, bottom, z);
2604
+ case MTextAttachmentPoint.BottomRight:
2605
+ case MTextAttachmentPoint.BaselineRight:
2606
+ return new THREE.Vector3(right, bottom, z);
2607
+ default:
2608
+ return new THREE.Vector3(left, top, z);
2609
+ }
2610
+ }
2476
2611
  mapRibbonParagraphAlignment(alignment) {
2477
2612
  switch (alignment) {
2478
2613
  case 'default':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-input-box",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",