@mlightcad/mtext-input-box 0.2.8 → 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,20 @@ 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;
233
+ /**
234
+ * Editor vertical bounds for the selection box / bounding overlay.
235
+ * Renderer `layout.lines` strips are often taller than actual glyphs (leading
236
+ * above the first row, uniform line-height slots, etc.). When a line has
237
+ * glyph boxes, use those for that row; keep the strip only for empty lines.
238
+ */
239
+ private computeEditorVerticalBounds;
224
240
  private extractBoxesFromRenderedObject;
225
241
  private normalizeRenderedTopAlignment;
226
242
  private toLocalBox;
@@ -296,6 +312,11 @@ export declare class MTextInputBox {
296
312
  private toScriptFromStyle;
297
313
  private createDefaultParseContext;
298
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;
299
320
  private mapRibbonParagraphAlignment;
300
321
  private mapRibbonAttachmentCode;
301
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,135 @@ 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
+ };
1404
+ }
1405
+ /**
1406
+ * Editor vertical bounds for the selection box / bounding overlay.
1407
+ * Renderer `layout.lines` strips are often taller than actual glyphs (leading
1408
+ * above the first row, uniform line-height slots, etc.). When a line has
1409
+ * glyph boxes, use those for that row; keep the strip only for empty lines.
1410
+ */
1411
+ computeEditorVerticalBounds(lineLayouts, charBoxes, objectLocal) {
1412
+ const fallback = () => ({
1413
+ minY: objectLocal.y - objectLocal.height / 2,
1414
+ maxY: objectLocal.y + objectLocal.height / 2
1415
+ });
1416
+ const overlapY = (a0, a1, b0, b1) => a0 < b1 + 1e-4 && a1 > b0 - 1e-4;
1417
+ if (lineLayouts.length === 0 && charBoxes.length === 0) {
1418
+ return fallback();
1419
+ }
1420
+ if (lineLayouts.length === 0) {
1421
+ let minY = Number.POSITIVE_INFINITY;
1422
+ let maxY = Number.NEGATIVE_INFINITY;
1423
+ for (const b of charBoxes) {
1424
+ minY = Math.min(minY, b.y - b.height / 2);
1425
+ maxY = Math.max(maxY, b.y + b.height / 2);
1426
+ }
1427
+ return Number.isFinite(minY) && Number.isFinite(maxY) && maxY >= minY
1428
+ ? { minY, maxY }
1429
+ : fallback();
1430
+ }
1431
+ if (charBoxes.length === 0) {
1432
+ let minY = Number.POSITIVE_INFINITY;
1433
+ let maxY = Number.NEGATIVE_INFINITY;
1434
+ for (const line of lineLayouts) {
1435
+ const lo = line.y - line.height / 2;
1436
+ const hi = line.y + line.height / 2;
1437
+ minY = Math.min(minY, lo);
1438
+ maxY = Math.max(maxY, hi);
1439
+ }
1440
+ return Number.isFinite(minY) && Number.isFinite(maxY) && maxY >= minY
1441
+ ? { minY, maxY }
1442
+ : fallback();
1443
+ }
1444
+ let minY = Number.POSITIVE_INFINITY;
1445
+ let maxY = Number.NEGATIVE_INFINITY;
1446
+ for (const line of lineLayouts) {
1447
+ const lo = line.y - line.height / 2;
1448
+ const hi = line.y + line.height / 2;
1449
+ const onLine = charBoxes.filter((b) => {
1450
+ const c0 = b.y - b.height / 2;
1451
+ const c1 = b.y + b.height / 2;
1452
+ return overlapY(c0, c1, lo, hi);
1453
+ });
1454
+ if (onLine.length === 0) {
1455
+ minY = Math.min(minY, lo);
1456
+ maxY = Math.max(maxY, hi);
1457
+ }
1458
+ else {
1459
+ for (const b of onLine) {
1460
+ minY = Math.min(minY, b.y - b.height / 2);
1461
+ maxY = Math.max(maxY, b.y + b.height / 2);
1462
+ }
1463
+ }
1464
+ }
1465
+ for (const b of charBoxes) {
1466
+ const c0 = b.y - b.height / 2;
1467
+ const c1 = b.y + b.height / 2;
1468
+ const overlapsAnyLine = lineLayouts.some((line) => {
1469
+ const lo = line.y - line.height / 2;
1470
+ const hi = line.y + line.height / 2;
1471
+ return overlapY(c0, c1, lo, hi);
1472
+ });
1473
+ if (!overlapsAnyLine) {
1474
+ minY = Math.min(minY, c0);
1475
+ maxY = Math.max(maxY, c1);
1476
+ }
1477
+ }
1478
+ if (!Number.isFinite(minY) || !Number.isFinite(maxY) || maxY < minY) {
1479
+ return fallback();
1480
+ }
1481
+ return { minY, maxY };
1301
1482
  }
1302
1483
  extractBoxesFromRenderedObject(object) {
1303
1484
  const layout = object.createLayoutData();
@@ -1325,25 +1506,19 @@ export class MTextInputBox {
1325
1506
  .filter((value) => Number.isInteger(value))
1326
1507
  .filter((value) => value >= 0 && value <= charBoxes.length);
1327
1508
  const local = this.toLocalBox(object.box);
1328
- let containerTop = local.y - local.height / 2;
1329
- let containerBottom = local.y + local.height / 2;
1330
- if (lineLayouts.length > 0) {
1331
- for (const line of lineLayouts) {
1332
- const top = line.y - line.height / 2;
1333
- const bottom = line.y + line.height / 2;
1334
- containerTop = Math.min(containerTop, top);
1335
- containerBottom = Math.max(containerBottom, bottom);
1336
- }
1337
- }
1509
+ const { minY: containerTop, maxY: containerBottom } = this.computeEditorVerticalBounds(lineLayouts, charBoxes, local);
1338
1510
  const containerBox = {
1339
- x: local.x,
1511
+ x: attachmentColumnMinLocal(this.width, this.editorAttachmentPoint),
1340
1512
  y: containerTop,
1341
- width: local.width,
1513
+ width: Math.max(1, this.width),
1342
1514
  height: Math.max(0, containerBottom - containerTop)
1343
1515
  };
1344
- containerBox.x = 0;
1345
- containerBox.width = this.width;
1346
- containerBox.height = Math.max(containerBox.height, this.getFallbackLineAdvance());
1516
+ const minHeight = this.getFallbackLineAdvance();
1517
+ if (containerBox.height < minHeight) {
1518
+ const delta = minHeight - containerBox.height;
1519
+ containerBox.y -= delta;
1520
+ containerBox.height = minHeight;
1521
+ }
1347
1522
  return {
1348
1523
  containerBox,
1349
1524
  charBoxes,
@@ -1412,7 +1587,7 @@ export class MTextInputBox {
1412
1587
  }
1413
1588
  return {
1414
1589
  containerBox: {
1415
- x: 0,
1590
+ x: attachmentColumnMinLocal(this.width, this.editorAttachmentPoint),
1416
1591
  y: minY,
1417
1592
  width: this.width,
1418
1593
  height: Math.max(1, -minY)
@@ -2399,6 +2574,40 @@ export class MTextInputBox {
2399
2574
  }
2400
2575
  return new MTextDocument(normalizedAst);
2401
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
+ }
2402
2611
  mapRibbonParagraphAlignment(alignment) {
2403
2612
  switch (alignment) {
2404
2613
  case 'default':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-input-box",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "@mlightcad/text-box-cursor": "0.1.1"
18
18
  },
19
19
  "peerDependencies": {
20
- "@mlightcad/mtext-renderer": "^0.10.13",
20
+ "@mlightcad/mtext-renderer": "^0.10.14",
21
21
  "three": "^0.172.0"
22
22
  },
23
23
  "devDependencies": {
@@ -28,6 +28,7 @@
28
28
  "@typescript-eslint/parser": "^8.41.0",
29
29
  "eslint": "^9.34.0",
30
30
  "eslint-config-prettier": "^10.1.8",
31
+ "rollup-plugin-peer-deps-external": "^2.2.4",
31
32
  "typescript": "^5.9.2",
32
33
  "typescript-eslint": "^8.41.0",
33
34
  "vitest": "^3.2.4"
@@ -35,6 +36,6 @@
35
36
  "scripts": {
36
37
  "build": "tsc -p tsconfig.build.json",
37
38
  "lint": "eslint src tests",
38
- "test": "vitest run --config ./vitest.config.ts"
39
+ "test": "vitest run"
39
40
  }
40
41
  }