@mlightcad/mtext-input-box 0.2.7 → 0.2.9
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.
- package/dist/viewer/format.js +11 -2
- package/dist/viewer/types.d.ts +17 -1
- package/dist/viewer/viewer.d.ts +49 -0
- package/dist/viewer/viewer.js +307 -28
- package/package.json +5 -3
package/dist/viewer/format.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MTextParagraphAlignment } from '@mlightcad/mtext-renderer';
|
|
1
2
|
export const DEFAULT_FONT_FAMILY = 'simkai';
|
|
2
3
|
/** Returns default character format. */
|
|
3
4
|
export function defaultCharFormat() {
|
|
@@ -11,7 +12,11 @@ export function defaultCharFormat() {
|
|
|
11
12
|
strike: false,
|
|
12
13
|
script: 'normal',
|
|
13
14
|
aci: null,
|
|
14
|
-
rgb: 0xffffff
|
|
15
|
+
rgb: 0xffffff,
|
|
16
|
+
obliqueAngle: 0,
|
|
17
|
+
widthFactor: 1,
|
|
18
|
+
tracking: 1,
|
|
19
|
+
paragraphAlignment: MTextParagraphAlignment.DEFAULT
|
|
15
20
|
};
|
|
16
21
|
}
|
|
17
22
|
/** Compares two formats. */
|
|
@@ -25,5 +30,9 @@ export function sameFormat(a, b) {
|
|
|
25
30
|
a.strike === b.strike &&
|
|
26
31
|
a.script === b.script &&
|
|
27
32
|
a.aci === b.aci &&
|
|
28
|
-
a.rgb === b.rgb
|
|
33
|
+
a.rgb === b.rgb &&
|
|
34
|
+
a.obliqueAngle === b.obliqueAngle &&
|
|
35
|
+
a.widthFactor === b.widthFactor &&
|
|
36
|
+
a.tracking === b.tracking &&
|
|
37
|
+
a.paragraphAlignment === b.paragraphAlignment);
|
|
29
38
|
}
|
package/dist/viewer/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Box, CursorStyle, SelectionStyle } from '@mlightcad/text-box-cursor';
|
|
2
|
-
import type { ColorSettings, MTextColor, TextStyle } from '@mlightcad/mtext-renderer';
|
|
2
|
+
import type { ColorSettings, 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';
|
|
@@ -25,6 +25,22 @@ export interface CharFormat {
|
|
|
25
25
|
aci: number | null;
|
|
26
26
|
/** Explicit RGB color packed as `0xRRGGBB`. */
|
|
27
27
|
rgb: number | null;
|
|
28
|
+
/**
|
|
29
|
+
* Character slant in degrees (AutoCAD oblique; negative leans the other way).
|
|
30
|
+
* Serialized to MTEXT as `\Qangle;`.
|
|
31
|
+
*/
|
|
32
|
+
obliqueAngle: number;
|
|
33
|
+
/** Horizontal scale of characters (width factor). Serialized as `\Wvalue;`. */
|
|
34
|
+
widthFactor: number;
|
|
35
|
+
/**
|
|
36
|
+
* Inter-character spacing factor (MTEXT `\T` tracking). `1` is the default spacing.
|
|
37
|
+
*/
|
|
38
|
+
tracking: number;
|
|
39
|
+
/**
|
|
40
|
+
* Paragraph horizontal alignment for the active paragraph (cursor or selection).
|
|
41
|
+
* Uses {@link MTextParagraphAlignment} from the shared MTEXT stack (parser / renderer).
|
|
42
|
+
*/
|
|
43
|
+
paragraphAlignment: MTextParagraphAlignment;
|
|
28
44
|
}
|
|
29
45
|
/** Internal editor state snapshot used for updates and history-like operations. */
|
|
30
46
|
export interface EditorState {
|
package/dist/viewer/viewer.d.ts
CHANGED
|
@@ -25,6 +25,18 @@ export declare class MTextInputBox {
|
|
|
25
25
|
private mtextInsertionOffset;
|
|
26
26
|
private enableWordWrap;
|
|
27
27
|
private mtextString;
|
|
28
|
+
/**
|
|
29
|
+
* Optional line spacing factor forwarded to {@link MTextData.lineSpaceFactor}.
|
|
30
|
+
* When `null`, the renderer default (same as {@link MTextInputBox.DEFAULT_LINE_SPACE_FACTOR}) is used.
|
|
31
|
+
*/
|
|
32
|
+
private editorLineSpaceFactor;
|
|
33
|
+
/**
|
|
34
|
+
* MTEXT attachment (DXF group 71) for the editor box relative to
|
|
35
|
+
* {@link position}. Drives {@link createMTextData} and contextual ribbon
|
|
36
|
+
* "Justify" / attachment controls.
|
|
37
|
+
*/
|
|
38
|
+
private editorAttachmentPoint;
|
|
39
|
+
private static readonly DEFAULT_LINE_SPACE_FACTOR;
|
|
28
40
|
private cursorIndex;
|
|
29
41
|
private selectionStart;
|
|
30
42
|
private selectionEnd;
|
|
@@ -132,6 +144,19 @@ export declare class MTextInputBox {
|
|
|
132
144
|
setCurrentFormat(format: Partial<CharFormat>): void;
|
|
133
145
|
/** Returns current insertion format. */
|
|
134
146
|
getCurrentFormat(): CharFormat;
|
|
147
|
+
/**
|
|
148
|
+
* Sets horizontal alignment for every paragraph that overlaps the current
|
|
149
|
+
* selection or cursor, using the same ids as the contextual ribbon
|
|
150
|
+
* (`default`, `left`, `center`, `right`, `justified`, `distributed`).
|
|
151
|
+
*/
|
|
152
|
+
setParagraphAlignment(alignment: string): void;
|
|
153
|
+
/**
|
|
154
|
+
* Sets the MTEXT attachment point using contextual ribbon codes (`TL`, `MC`,
|
|
155
|
+
* `BR`, etc.), matching AutoCAD nine-point attachment.
|
|
156
|
+
*/
|
|
157
|
+
setAttachmentPoint(attachmentPoint: string): void;
|
|
158
|
+
/** Returns the current attachment as a two-letter ribbon / DXF-style code. */
|
|
159
|
+
getAttachmentPointCode(): string;
|
|
135
160
|
/** Toggles selected alphabetic text between upper and lower case. */
|
|
136
161
|
toggleCase(): void;
|
|
137
162
|
/** Returns renderer default text style defined by `textStyle`. */
|
|
@@ -196,6 +221,13 @@ export declare class MTextInputBox {
|
|
|
196
221
|
private ensureFontsLoaded;
|
|
197
222
|
private collectFontNamesForText;
|
|
198
223
|
private updateCursorData;
|
|
224
|
+
/**
|
|
225
|
+
* Editor vertical bounds for the selection box / bounding overlay.
|
|
226
|
+
* Renderer `layout.lines` strips are often taller than actual glyphs (leading
|
|
227
|
+
* above the first row, uniform line-height slots, etc.). When a line has
|
|
228
|
+
* glyph boxes, use those for that row; keep the strip only for empty lines.
|
|
229
|
+
*/
|
|
230
|
+
private computeEditorVerticalBounds;
|
|
199
231
|
private extractBoxesFromRenderedObject;
|
|
200
232
|
private normalizeRenderedTopAlignment;
|
|
201
233
|
private toLocalBox;
|
|
@@ -213,6 +245,20 @@ export declare class MTextInputBox {
|
|
|
213
245
|
private resolveBaseFormatColor;
|
|
214
246
|
private createBaseFormatFromTextStyle;
|
|
215
247
|
private createMTextData;
|
|
248
|
+
/**
|
|
249
|
+
* Line spacing factor used for layout and returned when the host commits the editor.
|
|
250
|
+
* Matches the ribbon / `AcEdMTextEditor` default when no override is set.
|
|
251
|
+
*/
|
|
252
|
+
getLineSpacingFactor(): number;
|
|
253
|
+
/**
|
|
254
|
+
* Applies paragraph line spacing from ribbon-style multiples (`1` = default /
|
|
255
|
+
* single, `2` = default plus one text-line height of extra gap, etc.).
|
|
256
|
+
* Values in `(0, 1)` are still treated as raw renderer `lineSpaceFactor`
|
|
257
|
+
* for callers that set CAD factors directly.
|
|
258
|
+
*/
|
|
259
|
+
setLineSpacingFactor(factor: number): void;
|
|
260
|
+
/** Clears the explicit line spacing override and uses the renderer default. */
|
|
261
|
+
clearLineSpacing(): void;
|
|
216
262
|
private getFallbackLineAdvance;
|
|
217
263
|
private createTextStyle;
|
|
218
264
|
private resolveRenderColorSettings;
|
|
@@ -257,6 +303,9 @@ export declare class MTextInputBox {
|
|
|
257
303
|
private toScriptFromStyle;
|
|
258
304
|
private createDefaultParseContext;
|
|
259
305
|
private createNormalizedDocument;
|
|
306
|
+
private mapRibbonParagraphAlignment;
|
|
307
|
+
private mapRibbonAttachmentCode;
|
|
308
|
+
private attachmentPointToRibbonCode;
|
|
260
309
|
private mergeCharFormat;
|
|
261
310
|
private cloneDocumentStyle;
|
|
262
311
|
private toDocumentStyle;
|
package/dist/viewer/viewer.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { CursorRenderer, TextBoxCursor, ThreeJsRendererAdapter } from '@mlightcad/text-box-cursor';
|
|
2
|
-
import { getColorByIndex,
|
|
2
|
+
import { getColorByIndex, LINE_SPACING_SCALE_FACTOR, MText, MTextAttachmentPoint, MTextColor, MTextContext, MTextFlowDirection, MTextLineAlignment, MTextParagraphAlignment, UnifiedRenderer } from '@mlightcad/mtext-renderer';
|
|
3
3
|
import * as THREE from 'three';
|
|
4
|
-
import { MTextContext, MTextLineAlignment } from '@mlightcad/mtext-renderer';
|
|
5
4
|
import { DEFAULT_FONT_FAMILY, defaultCharFormat, sameFormat } from './format';
|
|
6
|
-
import { MTextDocument } from '../model';
|
|
5
|
+
import { hasNodeStyle, MTextDocument } from '../model';
|
|
7
6
|
import { EditorUiAdapter } from '../controller';
|
|
8
7
|
import { MTextToolbar } from '../ui/toolbar';
|
|
9
8
|
/**
|
|
@@ -37,6 +36,18 @@ export class MTextInputBox {
|
|
|
37
36
|
mtextInsertionOffset = new THREE.Vector3(0, 0, 0);
|
|
38
37
|
enableWordWrap;
|
|
39
38
|
mtextString = '';
|
|
39
|
+
/**
|
|
40
|
+
* Optional line spacing factor forwarded to {@link MTextData.lineSpaceFactor}.
|
|
41
|
+
* When `null`, the renderer default (same as {@link MTextInputBox.DEFAULT_LINE_SPACE_FACTOR}) is used.
|
|
42
|
+
*/
|
|
43
|
+
editorLineSpaceFactor = null;
|
|
44
|
+
/**
|
|
45
|
+
* MTEXT attachment (DXF group 71) for the editor box relative to
|
|
46
|
+
* {@link position}. Drives {@link createMTextData} and contextual ribbon
|
|
47
|
+
* "Justify" / attachment controls.
|
|
48
|
+
*/
|
|
49
|
+
editorAttachmentPoint = MTextAttachmentPoint.TopLeft;
|
|
50
|
+
static DEFAULT_LINE_SPACE_FACTOR = 0.3;
|
|
40
51
|
cursorIndex = 0;
|
|
41
52
|
selectionStart = 0;
|
|
42
53
|
selectionEnd = 0;
|
|
@@ -545,7 +556,6 @@ export class MTextInputBox {
|
|
|
545
556
|
if (this.latestCursorLayoutData.lineLayouts) {
|
|
546
557
|
snapshot.lineLayouts = this.latestCursorLayoutData.lineLayouts.map((line) => ({ ...line }));
|
|
547
558
|
}
|
|
548
|
-
console.log(snapshot);
|
|
549
559
|
return snapshot;
|
|
550
560
|
}
|
|
551
561
|
/** Sets current insertion format. */
|
|
@@ -560,7 +570,7 @@ export class MTextInputBox {
|
|
|
560
570
|
for (const node of nodesToUpdate) {
|
|
561
571
|
const nodeFormat = this.toCharFormat(node.style);
|
|
562
572
|
const mergedNodeFormat = this.mergeCharFormat(nodeFormat, format);
|
|
563
|
-
node.style = this.toDocumentStyle(mergedNodeFormat);
|
|
573
|
+
node.style = this.toDocumentStyle(mergedNodeFormat, node.style);
|
|
564
574
|
}
|
|
565
575
|
this.syncUiStateFromDocument();
|
|
566
576
|
this.relayout();
|
|
@@ -572,6 +582,76 @@ export class MTextInputBox {
|
|
|
572
582
|
getCurrentFormat() {
|
|
573
583
|
return { ...this.currentFormat };
|
|
574
584
|
}
|
|
585
|
+
/**
|
|
586
|
+
* Sets horizontal alignment for every paragraph that overlaps the current
|
|
587
|
+
* selection or cursor, using the same ids as the contextual ribbon
|
|
588
|
+
* (`default`, `left`, `center`, `right`, `justified`, `distributed`).
|
|
589
|
+
*/
|
|
590
|
+
setParagraphAlignment(alignment) {
|
|
591
|
+
const align = this.mapRibbonParagraphAlignment(alignment);
|
|
592
|
+
if (align === undefined)
|
|
593
|
+
return;
|
|
594
|
+
this.commitHistoryEdit(() => {
|
|
595
|
+
this.syncDocumentFromUiState();
|
|
596
|
+
const selection = this.getSelectionRange();
|
|
597
|
+
const docStart = this.toDocumentIndexFromLogicalIndex(Math.min(selection.start, selection.end), true);
|
|
598
|
+
const docEnd = this.toDocumentIndexFromLogicalIndex(Math.max(selection.start, selection.end), false);
|
|
599
|
+
const nodes = this.document.ast.nodes;
|
|
600
|
+
if (nodes.length === 0)
|
|
601
|
+
return;
|
|
602
|
+
const normalizeDocIndex = (idx) => {
|
|
603
|
+
const clamped = Math.max(0, Math.min(idx, nodes.length - 1));
|
|
604
|
+
if (nodes[clamped]?.type === 'paragraphBreak') {
|
|
605
|
+
return Math.min(clamped + 1, nodes.length);
|
|
606
|
+
}
|
|
607
|
+
return Math.min(idx, nodes.length - 1);
|
|
608
|
+
};
|
|
609
|
+
const findParagraphStart = (docIdx) => {
|
|
610
|
+
const i = normalizeDocIndex(docIdx);
|
|
611
|
+
for (let j = i; j >= 0; j--) {
|
|
612
|
+
if (nodes[j]?.type === 'paragraphBreak')
|
|
613
|
+
return j + 1;
|
|
614
|
+
}
|
|
615
|
+
return 0;
|
|
616
|
+
};
|
|
617
|
+
const findParagraphEndExclusive = (docIdx) => {
|
|
618
|
+
const i = Math.min(Math.max(0, docIdx), nodes.length - 1);
|
|
619
|
+
for (let j = i; j < nodes.length; j++) {
|
|
620
|
+
if (nodes[j]?.type === 'paragraphBreak')
|
|
621
|
+
return j;
|
|
622
|
+
}
|
|
623
|
+
return nodes.length;
|
|
624
|
+
};
|
|
625
|
+
const rangeStart = findParagraphStart(Math.min(docStart, docEnd));
|
|
626
|
+
const rangeEnd = findParagraphEndExclusive(Math.max(docStart, docEnd));
|
|
627
|
+
if (rangeStart >= rangeEnd)
|
|
628
|
+
return;
|
|
629
|
+
for (let i = rangeStart; i < rangeEnd; i++) {
|
|
630
|
+
const node = nodes[i];
|
|
631
|
+
if (node && hasNodeStyle(node)) {
|
|
632
|
+
node.style.paragraph.align = align;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
this.syncUiStateFromDocument();
|
|
636
|
+
this.relayout();
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Sets the MTEXT attachment point using contextual ribbon codes (`TL`, `MC`,
|
|
641
|
+
* `BR`, etc.), matching AutoCAD nine-point attachment.
|
|
642
|
+
*/
|
|
643
|
+
setAttachmentPoint(attachmentPoint) {
|
|
644
|
+
const next = this.mapRibbonAttachmentCode(attachmentPoint);
|
|
645
|
+
if (next === undefined || next === this.editorAttachmentPoint)
|
|
646
|
+
return;
|
|
647
|
+
this.editorAttachmentPoint = next;
|
|
648
|
+
this.relayout();
|
|
649
|
+
this.emit('change');
|
|
650
|
+
}
|
|
651
|
+
/** Returns the current attachment as a two-letter ribbon / DXF-style code. */
|
|
652
|
+
getAttachmentPointCode() {
|
|
653
|
+
return this.attachmentPointToRibbonCode(this.editorAttachmentPoint);
|
|
654
|
+
}
|
|
575
655
|
/** Toggles selected alphabetic text between upper and lower case. */
|
|
576
656
|
toggleCase() {
|
|
577
657
|
const selection = this.getSelectionRange();
|
|
@@ -1219,6 +1299,84 @@ export class MTextInputBox {
|
|
|
1219
1299
|
}
|
|
1220
1300
|
this.syncStateFromCursor();
|
|
1221
1301
|
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Editor vertical bounds for the selection box / bounding overlay.
|
|
1304
|
+
* Renderer `layout.lines` strips are often taller than actual glyphs (leading
|
|
1305
|
+
* above the first row, uniform line-height slots, etc.). When a line has
|
|
1306
|
+
* glyph boxes, use those for that row; keep the strip only for empty lines.
|
|
1307
|
+
*/
|
|
1308
|
+
computeEditorVerticalBounds(lineLayouts, charBoxes, objectLocal) {
|
|
1309
|
+
const fallback = () => ({
|
|
1310
|
+
minY: objectLocal.y - objectLocal.height / 2,
|
|
1311
|
+
maxY: objectLocal.y + objectLocal.height / 2
|
|
1312
|
+
});
|
|
1313
|
+
const overlapY = (a0, a1, b0, b1) => a0 < b1 + 1e-4 && a1 > b0 - 1e-4;
|
|
1314
|
+
if (lineLayouts.length === 0 && charBoxes.length === 0) {
|
|
1315
|
+
return fallback();
|
|
1316
|
+
}
|
|
1317
|
+
if (lineLayouts.length === 0) {
|
|
1318
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
1319
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
1320
|
+
for (const b of charBoxes) {
|
|
1321
|
+
minY = Math.min(minY, b.y - b.height / 2);
|
|
1322
|
+
maxY = Math.max(maxY, b.y + b.height / 2);
|
|
1323
|
+
}
|
|
1324
|
+
return Number.isFinite(minY) && Number.isFinite(maxY) && maxY >= minY
|
|
1325
|
+
? { minY, maxY }
|
|
1326
|
+
: fallback();
|
|
1327
|
+
}
|
|
1328
|
+
if (charBoxes.length === 0) {
|
|
1329
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
1330
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
1331
|
+
for (const line of lineLayouts) {
|
|
1332
|
+
const lo = line.y - line.height / 2;
|
|
1333
|
+
const hi = line.y + line.height / 2;
|
|
1334
|
+
minY = Math.min(minY, lo);
|
|
1335
|
+
maxY = Math.max(maxY, hi);
|
|
1336
|
+
}
|
|
1337
|
+
return Number.isFinite(minY) && Number.isFinite(maxY) && maxY >= minY
|
|
1338
|
+
? { minY, maxY }
|
|
1339
|
+
: fallback();
|
|
1340
|
+
}
|
|
1341
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
1342
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
1343
|
+
for (const line of lineLayouts) {
|
|
1344
|
+
const lo = line.y - line.height / 2;
|
|
1345
|
+
const hi = line.y + line.height / 2;
|
|
1346
|
+
const onLine = charBoxes.filter((b) => {
|
|
1347
|
+
const c0 = b.y - b.height / 2;
|
|
1348
|
+
const c1 = b.y + b.height / 2;
|
|
1349
|
+
return overlapY(c0, c1, lo, hi);
|
|
1350
|
+
});
|
|
1351
|
+
if (onLine.length === 0) {
|
|
1352
|
+
minY = Math.min(minY, lo);
|
|
1353
|
+
maxY = Math.max(maxY, hi);
|
|
1354
|
+
}
|
|
1355
|
+
else {
|
|
1356
|
+
for (const b of onLine) {
|
|
1357
|
+
minY = Math.min(minY, b.y - b.height / 2);
|
|
1358
|
+
maxY = Math.max(maxY, b.y + b.height / 2);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
for (const b of charBoxes) {
|
|
1363
|
+
const c0 = b.y - b.height / 2;
|
|
1364
|
+
const c1 = b.y + b.height / 2;
|
|
1365
|
+
const overlapsAnyLine = lineLayouts.some((line) => {
|
|
1366
|
+
const lo = line.y - line.height / 2;
|
|
1367
|
+
const hi = line.y + line.height / 2;
|
|
1368
|
+
return overlapY(c0, c1, lo, hi);
|
|
1369
|
+
});
|
|
1370
|
+
if (!overlapsAnyLine) {
|
|
1371
|
+
minY = Math.min(minY, c0);
|
|
1372
|
+
maxY = Math.max(maxY, c1);
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
if (!Number.isFinite(minY) || !Number.isFinite(maxY) || maxY < minY) {
|
|
1376
|
+
return fallback();
|
|
1377
|
+
}
|
|
1378
|
+
return { minY, maxY };
|
|
1379
|
+
}
|
|
1222
1380
|
extractBoxesFromRenderedObject(object) {
|
|
1223
1381
|
const layout = object.createLayoutData();
|
|
1224
1382
|
const charBoxes = [];
|
|
@@ -1245,16 +1403,7 @@ export class MTextInputBox {
|
|
|
1245
1403
|
.filter((value) => Number.isInteger(value))
|
|
1246
1404
|
.filter((value) => value >= 0 && value <= charBoxes.length);
|
|
1247
1405
|
const local = this.toLocalBox(object.box);
|
|
1248
|
-
|
|
1249
|
-
let containerBottom = local.y + local.height / 2;
|
|
1250
|
-
if (lineLayouts.length > 0) {
|
|
1251
|
-
for (const line of lineLayouts) {
|
|
1252
|
-
const top = line.y - line.height / 2;
|
|
1253
|
-
const bottom = line.y + line.height / 2;
|
|
1254
|
-
containerTop = Math.min(containerTop, top);
|
|
1255
|
-
containerBottom = Math.max(containerBottom, bottom);
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1406
|
+
const { minY: containerTop, maxY: containerBottom } = this.computeEditorVerticalBounds(lineLayouts, charBoxes, local);
|
|
1258
1407
|
const containerBox = {
|
|
1259
1408
|
x: local.x,
|
|
1260
1409
|
y: containerTop,
|
|
@@ -1263,7 +1412,12 @@ export class MTextInputBox {
|
|
|
1263
1412
|
};
|
|
1264
1413
|
containerBox.x = 0;
|
|
1265
1414
|
containerBox.width = this.width;
|
|
1266
|
-
|
|
1415
|
+
const minHeight = this.getFallbackLineAdvance();
|
|
1416
|
+
if (containerBox.height < minHeight) {
|
|
1417
|
+
const delta = minHeight - containerBox.height;
|
|
1418
|
+
containerBox.y -= delta;
|
|
1419
|
+
containerBox.height = minHeight;
|
|
1420
|
+
}
|
|
1267
1421
|
return {
|
|
1268
1422
|
containerBox,
|
|
1269
1423
|
charBoxes,
|
|
@@ -1486,10 +1640,15 @@ export class MTextInputBox {
|
|
|
1486
1640
|
const color = this.resolveBaseFormatColor(colorSettings);
|
|
1487
1641
|
base.aci = color.aci;
|
|
1488
1642
|
base.rgb = color.rgb;
|
|
1643
|
+
const wf = style.widthFactor;
|
|
1644
|
+
base.widthFactor = typeof wf === 'number' && Number.isFinite(wf) ? Math.max(0.01, wf) : 1;
|
|
1645
|
+
const obl = style.obliqueAngle;
|
|
1646
|
+
base.obliqueAngle = typeof obl === 'number' && Number.isFinite(obl) ? obl : 0;
|
|
1647
|
+
base.tracking = 1;
|
|
1489
1648
|
return base;
|
|
1490
1649
|
}
|
|
1491
1650
|
createMTextData() {
|
|
1492
|
-
|
|
1651
|
+
const data = {
|
|
1493
1652
|
text: this.mtextString,
|
|
1494
1653
|
height: Math.max(1, this.defaultTextStyle.fixedTextHeight),
|
|
1495
1654
|
width: this.width,
|
|
@@ -1498,11 +1657,43 @@ export class MTextInputBox {
|
|
|
1498
1657
|
y: this.position.y,
|
|
1499
1658
|
z: this.position.z
|
|
1500
1659
|
},
|
|
1501
|
-
attachmentPoint:
|
|
1660
|
+
attachmentPoint: this.editorAttachmentPoint,
|
|
1502
1661
|
drawingDirection: MTextFlowDirection.LEFT_TO_RIGHT,
|
|
1503
1662
|
widthFactor: 1,
|
|
1504
1663
|
collectCharBoxes: true
|
|
1505
1664
|
};
|
|
1665
|
+
if (this.editorLineSpaceFactor != null && Number.isFinite(this.editorLineSpaceFactor)) {
|
|
1666
|
+
data.lineSpaceFactor = this.editorLineSpaceFactor;
|
|
1667
|
+
}
|
|
1668
|
+
return data;
|
|
1669
|
+
}
|
|
1670
|
+
/**
|
|
1671
|
+
* Line spacing factor used for layout and returned when the host commits the editor.
|
|
1672
|
+
* Matches the ribbon / `AcEdMTextEditor` default when no override is set.
|
|
1673
|
+
*/
|
|
1674
|
+
getLineSpacingFactor() {
|
|
1675
|
+
return this.editorLineSpaceFactor ?? MTextInputBox.DEFAULT_LINE_SPACE_FACTOR;
|
|
1676
|
+
}
|
|
1677
|
+
/**
|
|
1678
|
+
* Applies paragraph line spacing from ribbon-style multiples (`1` = default /
|
|
1679
|
+
* single, `2` = default plus one text-line height of extra gap, etc.).
|
|
1680
|
+
* Values in `(0, 1)` are still treated as raw renderer `lineSpaceFactor`
|
|
1681
|
+
* for callers that set CAD factors directly.
|
|
1682
|
+
*/
|
|
1683
|
+
setLineSpacingFactor(factor) {
|
|
1684
|
+
if (!Number.isFinite(factor) || factor <= 0)
|
|
1685
|
+
return;
|
|
1686
|
+
const defaultF = MTextInputBox.DEFAULT_LINE_SPACE_FACTOR;
|
|
1687
|
+
const rendererFactor = factor >= 1 ? Math.max(0.01, defaultF + (factor - 1) / LINE_SPACING_SCALE_FACTOR) : factor;
|
|
1688
|
+
this.editorLineSpaceFactor = rendererFactor;
|
|
1689
|
+
this.relayout();
|
|
1690
|
+
this.emit('change');
|
|
1691
|
+
}
|
|
1692
|
+
/** Clears the explicit line spacing override and uses the renderer default. */
|
|
1693
|
+
clearLineSpacing() {
|
|
1694
|
+
this.editorLineSpaceFactor = null;
|
|
1695
|
+
this.relayout();
|
|
1696
|
+
this.emit('change');
|
|
1506
1697
|
}
|
|
1507
1698
|
getFallbackLineAdvance() {
|
|
1508
1699
|
return Math.max(1, this.currentFormat.fontSize * MTextInputBox.FALLBACK_LINE_ADVANCE_RATIO);
|
|
@@ -2181,6 +2372,11 @@ export class MTextInputBox {
|
|
|
2181
2372
|
const reducedThreshold = Math.max(1, baseSize) * 0.9;
|
|
2182
2373
|
const isReducedScriptCapHeight = capHeight <= reducedThreshold;
|
|
2183
2374
|
const logicalFontSize = script === 'normal' ? capHeight : isReducedScriptCapHeight ? capHeight / 0.7 : capHeight;
|
|
2375
|
+
const wf = style.widthFactor?.value;
|
|
2376
|
+
const widthFactor = typeof wf === 'number' && Number.isFinite(wf) ? Math.max(0.01, wf) : 1;
|
|
2377
|
+
const tr = style.charTrackingFactor?.value;
|
|
2378
|
+
const tracking = typeof tr === 'number' && Number.isFinite(tr) ? Math.max(0.01, tr) : 1;
|
|
2379
|
+
const oblique = typeof style.oblique === 'number' && Number.isFinite(style.oblique) ? style.oblique : 0;
|
|
2184
2380
|
return {
|
|
2185
2381
|
fontFamily: style.fontFace.family || defaultFontFamily,
|
|
2186
2382
|
fontSize: Math.max(1, logicalFontSize || baseSize),
|
|
@@ -2191,7 +2387,11 @@ export class MTextInputBox {
|
|
|
2191
2387
|
strike: style.strikeThrough,
|
|
2192
2388
|
script,
|
|
2193
2389
|
aci: resolvedAci,
|
|
2194
|
-
rgb: resolvedColor
|
|
2390
|
+
rgb: resolvedColor,
|
|
2391
|
+
obliqueAngle: oblique,
|
|
2392
|
+
widthFactor,
|
|
2393
|
+
tracking,
|
|
2394
|
+
paragraphAlignment: style.paragraph.align
|
|
2195
2395
|
};
|
|
2196
2396
|
}
|
|
2197
2397
|
toScriptFromStyle(style, baseSize, capHeight) {
|
|
@@ -2273,6 +2473,75 @@ export class MTextInputBox {
|
|
|
2273
2473
|
}
|
|
2274
2474
|
return new MTextDocument(normalizedAst);
|
|
2275
2475
|
}
|
|
2476
|
+
mapRibbonParagraphAlignment(alignment) {
|
|
2477
|
+
switch (alignment) {
|
|
2478
|
+
case 'default':
|
|
2479
|
+
return MTextParagraphAlignment.DEFAULT;
|
|
2480
|
+
case 'left':
|
|
2481
|
+
return MTextParagraphAlignment.LEFT;
|
|
2482
|
+
case 'center':
|
|
2483
|
+
return MTextParagraphAlignment.CENTER;
|
|
2484
|
+
case 'right':
|
|
2485
|
+
return MTextParagraphAlignment.RIGHT;
|
|
2486
|
+
case 'justified':
|
|
2487
|
+
return MTextParagraphAlignment.JUSTIFIED;
|
|
2488
|
+
case 'distributed':
|
|
2489
|
+
return MTextParagraphAlignment.DISTRIBUTED;
|
|
2490
|
+
default:
|
|
2491
|
+
return undefined;
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
mapRibbonAttachmentCode(code) {
|
|
2495
|
+
switch (code.trim().toUpperCase()) {
|
|
2496
|
+
case 'TL':
|
|
2497
|
+
return MTextAttachmentPoint.TopLeft;
|
|
2498
|
+
case 'TC':
|
|
2499
|
+
return MTextAttachmentPoint.TopCenter;
|
|
2500
|
+
case 'TR':
|
|
2501
|
+
return MTextAttachmentPoint.TopRight;
|
|
2502
|
+
case 'ML':
|
|
2503
|
+
return MTextAttachmentPoint.MiddleLeft;
|
|
2504
|
+
case 'MC':
|
|
2505
|
+
return MTextAttachmentPoint.MiddleCenter;
|
|
2506
|
+
case 'MR':
|
|
2507
|
+
return MTextAttachmentPoint.MiddleRight;
|
|
2508
|
+
case 'BL':
|
|
2509
|
+
return MTextAttachmentPoint.BottomLeft;
|
|
2510
|
+
case 'BC':
|
|
2511
|
+
return MTextAttachmentPoint.BottomCenter;
|
|
2512
|
+
case 'BR':
|
|
2513
|
+
return MTextAttachmentPoint.BottomRight;
|
|
2514
|
+
default:
|
|
2515
|
+
return undefined;
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
attachmentPointToRibbonCode(point) {
|
|
2519
|
+
switch (point) {
|
|
2520
|
+
case MTextAttachmentPoint.TopLeft:
|
|
2521
|
+
return 'TL';
|
|
2522
|
+
case MTextAttachmentPoint.TopCenter:
|
|
2523
|
+
return 'TC';
|
|
2524
|
+
case MTextAttachmentPoint.TopRight:
|
|
2525
|
+
return 'TR';
|
|
2526
|
+
case MTextAttachmentPoint.MiddleLeft:
|
|
2527
|
+
return 'ML';
|
|
2528
|
+
case MTextAttachmentPoint.MiddleCenter:
|
|
2529
|
+
return 'MC';
|
|
2530
|
+
case MTextAttachmentPoint.MiddleRight:
|
|
2531
|
+
return 'MR';
|
|
2532
|
+
case MTextAttachmentPoint.BottomLeft:
|
|
2533
|
+
case MTextAttachmentPoint.BaselineLeft:
|
|
2534
|
+
return 'BL';
|
|
2535
|
+
case MTextAttachmentPoint.BottomCenter:
|
|
2536
|
+
case MTextAttachmentPoint.BaselineCenter:
|
|
2537
|
+
return 'BC';
|
|
2538
|
+
case MTextAttachmentPoint.BottomRight:
|
|
2539
|
+
case MTextAttachmentPoint.BaselineRight:
|
|
2540
|
+
return 'BR';
|
|
2541
|
+
default:
|
|
2542
|
+
return 'TL';
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2276
2545
|
mergeCharFormat(base, patch) {
|
|
2277
2546
|
const next = { ...base, ...patch };
|
|
2278
2547
|
if (patch.aci !== undefined) {
|
|
@@ -2295,6 +2564,9 @@ export class MTextInputBox {
|
|
|
2295
2564
|
next.aci = null;
|
|
2296
2565
|
}
|
|
2297
2566
|
}
|
|
2567
|
+
if (patch.paragraphAlignment !== undefined) {
|
|
2568
|
+
next.paragraphAlignment = patch.paragraphAlignment;
|
|
2569
|
+
}
|
|
2298
2570
|
return next;
|
|
2299
2571
|
}
|
|
2300
2572
|
cloneDocumentStyle(style) {
|
|
@@ -2320,7 +2592,7 @@ export class MTextInputBox {
|
|
|
2320
2592
|
}
|
|
2321
2593
|
};
|
|
2322
2594
|
}
|
|
2323
|
-
toDocumentStyle(format) {
|
|
2595
|
+
toDocumentStyle(format, existing) {
|
|
2324
2596
|
const explicitAci = this.isExplicitAci(format.aci) ? format.aci : null;
|
|
2325
2597
|
const rgbColor = explicitAci !== null
|
|
2326
2598
|
? null
|
|
@@ -2332,6 +2604,7 @@ export class MTextInputBox {
|
|
|
2332
2604
|
: format.script === 'subscript'
|
|
2333
2605
|
? MTextLineAlignment.BOTTOM
|
|
2334
2606
|
: MTextLineAlignment.MIDDLE;
|
|
2607
|
+
const paragraphAlign = format.paragraphAlignment;
|
|
2335
2608
|
return {
|
|
2336
2609
|
underline: format.underline,
|
|
2337
2610
|
overline: format.overline,
|
|
@@ -2346,15 +2619,21 @@ export class MTextInputBox {
|
|
|
2346
2619
|
weight: format.bold ? 700 : 400
|
|
2347
2620
|
},
|
|
2348
2621
|
capHeight: { value: capHeight, isRelative: false },
|
|
2349
|
-
widthFactor: {
|
|
2350
|
-
|
|
2351
|
-
|
|
2622
|
+
widthFactor: {
|
|
2623
|
+
value: Math.max(0.01, format.widthFactor),
|
|
2624
|
+
isRelative: false
|
|
2625
|
+
},
|
|
2626
|
+
charTrackingFactor: {
|
|
2627
|
+
value: Math.max(0.01, format.tracking),
|
|
2628
|
+
isRelative: false
|
|
2629
|
+
},
|
|
2630
|
+
oblique: format.obliqueAngle,
|
|
2352
2631
|
paragraph: {
|
|
2353
|
-
indent: 0,
|
|
2354
|
-
left: 0,
|
|
2355
|
-
right: 0,
|
|
2356
|
-
align:
|
|
2357
|
-
tabs: []
|
|
2632
|
+
indent: existing?.paragraph.indent ?? 0,
|
|
2633
|
+
left: existing?.paragraph.left ?? 0,
|
|
2634
|
+
right: existing?.paragraph.right ?? 0,
|
|
2635
|
+
align: paragraphAlign,
|
|
2636
|
+
tabs: existing?.paragraph.tabs ? [...existing.paragraph.tabs] : []
|
|
2358
2637
|
}
|
|
2359
2638
|
};
|
|
2360
2639
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mlightcad/mtext-input-box",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -17,16 +17,18 @@
|
|
|
17
17
|
"@mlightcad/text-box-cursor": "0.1.1"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@mlightcad/mtext-renderer": "^0.10.
|
|
20
|
+
"@mlightcad/mtext-renderer": "^0.10.14",
|
|
21
21
|
"three": "^0.172.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
+
"@eslint/js": "^9.34.0",
|
|
24
25
|
"@types/node": "^24.4.0",
|
|
25
26
|
"@types/three": "^0.172.0",
|
|
26
27
|
"@typescript-eslint/eslint-plugin": "^8.41.0",
|
|
27
28
|
"@typescript-eslint/parser": "^8.41.0",
|
|
28
29
|
"eslint": "^9.34.0",
|
|
29
30
|
"eslint-config-prettier": "^10.1.8",
|
|
31
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
30
32
|
"typescript": "^5.9.2",
|
|
31
33
|
"typescript-eslint": "^8.41.0",
|
|
32
34
|
"vitest": "^3.2.4"
|
|
@@ -34,6 +36,6 @@
|
|
|
34
36
|
"scripts": {
|
|
35
37
|
"build": "tsc -p tsconfig.build.json",
|
|
36
38
|
"lint": "eslint src tests",
|
|
37
|
-
"test": "vitest run
|
|
39
|
+
"test": "vitest run"
|
|
38
40
|
}
|
|
39
41
|
}
|