@mlightcad/mtext-parser 1.1.1 → 1.1.3
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/parser.cjs.js +2 -2
- package/dist/parser.cjs.js.map +1 -1
- package/dist/parser.es.js +76 -66
- package/dist/parser.es.js.map +1 -1
- package/dist/parser.umd.js +2 -2
- package/dist/parser.umd.js.map +1 -1
- package/dist/types/parser.d.ts +20 -3
- package/package.json +1 -1
package/dist/parser.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var k = /* @__PURE__ */ ((
|
|
1
|
+
var k = /* @__PURE__ */ ((s) => (s[s.NONE = 0] = "NONE", s[s.WORD = 1] = "WORD", s[s.STACK = 2] = "STACK", s[s.SPACE = 3] = "SPACE", s[s.NBSP = 4] = "NBSP", s[s.TABULATOR = 5] = "TABULATOR", s[s.NEW_PARAGRAPH = 6] = "NEW_PARAGRAPH", s[s.NEW_COLUMN = 7] = "NEW_COLUMN", s[s.WRAP_AT_DIMLINE = 8] = "WRAP_AT_DIMLINE", s[s.PROPERTIES_CHANGED = 9] = "PROPERTIES_CHANGED", s))(k || {}), _ = /* @__PURE__ */ ((s) => (s[s.BOTTOM = 0] = "BOTTOM", s[s.MIDDLE = 1] = "MIDDLE", s[s.TOP = 2] = "TOP", s))(_ || {}), m = /* @__PURE__ */ ((s) => (s[s.DEFAULT = 0] = "DEFAULT", s[s.LEFT = 1] = "LEFT", s[s.RIGHT = 2] = "RIGHT", s[s.CENTER = 3] = "CENTER", s[s.JUSTIFIED = 4] = "JUSTIFIED", s[s.DISTRIBUTED = 5] = "DISTRIBUTED", s))(m || {}), E = /* @__PURE__ */ ((s) => (s[s.NONE = 0] = "NONE", s[s.UNDERLINE = 1] = "UNDERLINE", s[s.OVERLINE = 2] = "OVERLINE", s[s.STRIKE_THROUGH = 4] = "STRIKE_THROUGH", s))(E || {});
|
|
2
2
|
const F = {
|
|
3
3
|
c: "Ø",
|
|
4
4
|
d: "°",
|
|
@@ -11,36 +11,36 @@ const F = {
|
|
|
11
11
|
d: 5
|
|
12
12
|
/* DISTRIBUTED */
|
|
13
13
|
};
|
|
14
|
-
function R(
|
|
15
|
-
const [t, e,
|
|
16
|
-
return
|
|
14
|
+
function R(s) {
|
|
15
|
+
const [t, e, r] = s;
|
|
16
|
+
return r << 16 | e << 8 | t;
|
|
17
17
|
}
|
|
18
|
-
function v(
|
|
19
|
-
const t =
|
|
20
|
-
return [t, e,
|
|
18
|
+
function v(s) {
|
|
19
|
+
const t = s & 255, e = s >> 8 & 255, r = s >> 16 & 255;
|
|
20
|
+
return [t, e, r];
|
|
21
21
|
}
|
|
22
|
-
function
|
|
23
|
-
return
|
|
22
|
+
function O(s) {
|
|
23
|
+
return s.replace(/\r\n|\r|\n/g, "\\P");
|
|
24
24
|
}
|
|
25
|
-
function
|
|
26
|
-
return
|
|
25
|
+
function I(s) {
|
|
26
|
+
return s.replace(/\\P/g, "").replace(/\\~/g, "").includes("\\");
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
const e = /* @__PURE__ */ new Set(),
|
|
30
|
-
return [...
|
|
28
|
+
function N(s, t = !1) {
|
|
29
|
+
const e = /* @__PURE__ */ new Set(), r = /\\[fF](.*?)[;|]/g;
|
|
30
|
+
return [...s.matchAll(r)].forEach((a) => {
|
|
31
31
|
let i = a[1].toLowerCase();
|
|
32
32
|
t && (i = i.replace(/\.(ttf|otf|woff|shx)$/, "")), e.add(i);
|
|
33
33
|
}), e;
|
|
34
34
|
}
|
|
35
|
-
class
|
|
35
|
+
class x {
|
|
36
36
|
/**
|
|
37
37
|
* Creates a new MTextParser instance
|
|
38
38
|
* @param content - The MText content to parse
|
|
39
39
|
* @param ctx - Optional initial MText context
|
|
40
40
|
* @param yieldPropertyCommands - Whether to yield property change commands
|
|
41
41
|
*/
|
|
42
|
-
constructor(t, e,
|
|
43
|
-
this.ctxStack = [], this.continueStroke = !1, this.inStackContext = !1, this.scanner = new g(t), this.ctx = e ?? new d(), this.lastCtx = this.ctx.copy(), this.yieldPropertyCommands =
|
|
42
|
+
constructor(t, e, r = !1) {
|
|
43
|
+
this.ctxStack = [], this.continueStroke = !1, this.inStackContext = !1, this.scanner = new g(t), this.ctx = e ?? new d(), this.lastCtx = this.ctx.copy(), this.yieldPropertyCommands = r;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Decode multi-byte character from hex code
|
|
@@ -79,7 +79,7 @@ class D {
|
|
|
79
79
|
*/
|
|
80
80
|
parseStacking() {
|
|
81
81
|
const t = new g(this.extractExpression(!0));
|
|
82
|
-
let e = "",
|
|
82
|
+
let e = "", r = "", a = "";
|
|
83
83
|
const i = () => {
|
|
84
84
|
let n = t.peek(), l = !1;
|
|
85
85
|
return n.charCodeAt(0) < 32 && (n = " "), n === "\\" && (l = !0, t.consume(1), n = t.peek()), t.consume(1), [n, l];
|
|
@@ -104,7 +104,7 @@ class D {
|
|
|
104
104
|
}
|
|
105
105
|
return l;
|
|
106
106
|
};
|
|
107
|
-
return [e, a] = h(), a && (
|
|
107
|
+
return [e, a] = h(), a && (r = u(a === "^")), e === "" && r.includes("I/") ? [2, [" ", " ", "/"]] : a === "^" ? [2, [e, r, "^"]] : [2, [e, r, a]];
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
110
|
* Parse MText properties
|
|
@@ -164,11 +164,12 @@ class D {
|
|
|
164
164
|
throw new Error(`Unknown command: ${t}`);
|
|
165
165
|
}
|
|
166
166
|
if (this.continueStroke = e.hasAnyStroke, e.continueStroke = this.continueStroke, this.ctx = e, this.yieldPropertyCommands) {
|
|
167
|
-
const
|
|
168
|
-
if (Object.keys(
|
|
167
|
+
const r = this.getPropertyChanges(this.lastCtx, e);
|
|
168
|
+
if (Object.keys(r).length > 0)
|
|
169
169
|
return this.lastCtx = this.ctx.copy(), {
|
|
170
170
|
command: t,
|
|
171
|
-
changes:
|
|
171
|
+
changes: r,
|
|
172
|
+
depth: this.ctxStack.length
|
|
172
173
|
};
|
|
173
174
|
}
|
|
174
175
|
}
|
|
@@ -179,12 +180,12 @@ class D {
|
|
|
179
180
|
* @returns Object containing changed properties
|
|
180
181
|
*/
|
|
181
182
|
getPropertyChanges(t, e) {
|
|
182
|
-
const
|
|
183
|
-
if (t.underline !== e.underline && (
|
|
183
|
+
const r = {};
|
|
184
|
+
if (t.underline !== e.underline && (r.underline = e.underline), t.overline !== e.overline && (r.overline = e.overline), t.strikeThrough !== e.strikeThrough && (r.strikeThrough = e.strikeThrough), t.color.aci !== e.color.aci && (r.aci = e.color.aci), t.color.rgbValue !== e.color.rgbValue && (r.rgb = e.color.rgb), t.align !== e.align && (r.align = e.align), JSON.stringify(t.fontFace) !== JSON.stringify(e.fontFace) && (r.fontFace = e.fontFace), (t.capHeight.value !== e.capHeight.value || t.capHeight.isRelative !== e.capHeight.isRelative) && (r.capHeight = e.capHeight), (t.widthFactor.value !== e.widthFactor.value || t.widthFactor.isRelative !== e.widthFactor.isRelative) && (r.widthFactor = e.widthFactor), (t.charTrackingFactor.value !== e.charTrackingFactor.value || t.charTrackingFactor.isRelative !== e.charTrackingFactor.isRelative) && (r.charTrackingFactor = e.charTrackingFactor), t.oblique !== e.oblique && (r.oblique = e.oblique), JSON.stringify(t.paragraph) !== JSON.stringify(e.paragraph)) {
|
|
184
185
|
const a = {};
|
|
185
|
-
t.paragraph.indent !== e.paragraph.indent && (a.indent = e.paragraph.indent), t.paragraph.align !== e.paragraph.align && (a.align = e.paragraph.align), t.paragraph.left !== e.paragraph.left && (a.left = e.paragraph.left), t.paragraph.right !== e.paragraph.right && (a.right = e.paragraph.right), JSON.stringify(t.paragraph.tab_stops) !== JSON.stringify(e.paragraph.tab_stops) && (a.tab_stops = e.paragraph.tab_stops), Object.keys(a).length > 0 && (
|
|
186
|
+
t.paragraph.indent !== e.paragraph.indent && (a.indent = e.paragraph.indent), t.paragraph.align !== e.paragraph.align && (a.align = e.paragraph.align), t.paragraph.left !== e.paragraph.left && (a.left = e.paragraph.left), t.paragraph.right !== e.paragraph.right && (a.right = e.paragraph.right), JSON.stringify(t.paragraph.tab_stops) !== JSON.stringify(e.paragraph.tab_stops) && (a.tab_stops = e.paragraph.tab_stops), Object.keys(a).length > 0 && (r.paragraph = a);
|
|
186
187
|
}
|
|
187
|
-
return
|
|
188
|
+
return r;
|
|
188
189
|
}
|
|
189
190
|
/**
|
|
190
191
|
* Parse alignment property
|
|
@@ -266,8 +267,8 @@ class D {
|
|
|
266
267
|
const e = this.extractFloatExpression(!0);
|
|
267
268
|
if (e)
|
|
268
269
|
if (e.endsWith("x")) {
|
|
269
|
-
const
|
|
270
|
-
t *=
|
|
270
|
+
const r = parseFloat(e.slice(0, -1));
|
|
271
|
+
t *= r;
|
|
271
272
|
} else
|
|
272
273
|
t = parseFloat(e);
|
|
273
274
|
return t;
|
|
@@ -287,8 +288,8 @@ class D {
|
|
|
287
288
|
parseAciColor(t) {
|
|
288
289
|
const e = this.extractIntExpression();
|
|
289
290
|
if (e) {
|
|
290
|
-
const
|
|
291
|
-
|
|
291
|
+
const r = parseInt(e);
|
|
292
|
+
r < 257 && (t.color.aci = r);
|
|
292
293
|
}
|
|
293
294
|
this.consumeOptionalTerminator();
|
|
294
295
|
}
|
|
@@ -299,8 +300,8 @@ class D {
|
|
|
299
300
|
parseRgbColor(t) {
|
|
300
301
|
const e = this.extractIntExpression();
|
|
301
302
|
if (e) {
|
|
302
|
-
const
|
|
303
|
-
t.color.rgbValue =
|
|
303
|
+
const r = parseInt(e) & 16777215;
|
|
304
|
+
t.color.rgbValue = r;
|
|
304
305
|
}
|
|
305
306
|
this.consumeOptionalTerminator();
|
|
306
307
|
}
|
|
@@ -310,9 +311,9 @@ class D {
|
|
|
310
311
|
* @returns Extracted expression
|
|
311
312
|
*/
|
|
312
313
|
extractFloatExpression(t = !1) {
|
|
313
|
-
const e = t ? /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?x?/ : /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/,
|
|
314
|
-
if (
|
|
315
|
-
const a =
|
|
314
|
+
const e = t ? /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?x?/ : /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/, r = this.scanner.tail.match(e);
|
|
315
|
+
if (r) {
|
|
316
|
+
const a = r[0];
|
|
316
317
|
return this.scanner.consume(a.length), a;
|
|
317
318
|
}
|
|
318
319
|
return "";
|
|
@@ -350,12 +351,12 @@ class D {
|
|
|
350
351
|
parseFontProperties(t) {
|
|
351
352
|
const e = this.extractExpression().split("|");
|
|
352
353
|
if (e.length > 0 && e[0]) {
|
|
353
|
-
const
|
|
354
|
+
const r = e[0];
|
|
354
355
|
let a = "Regular", i = 400;
|
|
355
356
|
for (const h of e.slice(1))
|
|
356
|
-
h.startsWith("b1") ? i = 700 : h.startsWith("i1") && (a = "
|
|
357
|
+
h.startsWith("b1") ? i = 700 : h === "i" || h.startsWith("i1") ? a = "Italic" : (h === "i0" || h.startsWith("i0")) && (a = "Regular");
|
|
357
358
|
t.fontFace = {
|
|
358
|
-
family:
|
|
359
|
+
family: r,
|
|
359
360
|
style: a,
|
|
360
361
|
weight: i
|
|
361
362
|
};
|
|
@@ -368,7 +369,7 @@ class D {
|
|
|
368
369
|
*/
|
|
369
370
|
parseParagraphProperties(t) {
|
|
370
371
|
const e = new g(this.extractExpression());
|
|
371
|
-
let
|
|
372
|
+
let r = t.paragraph.indent, a = t.paragraph.left, i = t.paragraph.right, h = t.paragraph.align, u = [];
|
|
372
373
|
const n = () => {
|
|
373
374
|
const l = e.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);
|
|
374
375
|
if (l) {
|
|
@@ -382,7 +383,7 @@ class D {
|
|
|
382
383
|
for (; e.hasData; )
|
|
383
384
|
switch (e.get()) {
|
|
384
385
|
case "i":
|
|
385
|
-
|
|
386
|
+
r = n();
|
|
386
387
|
break;
|
|
387
388
|
case "l":
|
|
388
389
|
a = n();
|
|
@@ -413,7 +414,7 @@ class D {
|
|
|
413
414
|
break;
|
|
414
415
|
}
|
|
415
416
|
t.paragraph = {
|
|
416
|
-
indent:
|
|
417
|
+
indent: r,
|
|
417
418
|
left: a,
|
|
418
419
|
right: i,
|
|
419
420
|
align: h,
|
|
@@ -431,7 +432,7 @@ class D {
|
|
|
431
432
|
* @yields MTextToken objects
|
|
432
433
|
*/
|
|
433
434
|
*parse() {
|
|
434
|
-
let
|
|
435
|
+
let r = null;
|
|
435
436
|
const a = () => {
|
|
436
437
|
var h;
|
|
437
438
|
let i = "";
|
|
@@ -510,7 +511,7 @@ class D {
|
|
|
510
511
|
}
|
|
511
512
|
}
|
|
512
513
|
if (n === " ")
|
|
513
|
-
return i ? (this.scanner.consume(1),
|
|
514
|
+
return i ? (this.scanner.consume(1), r = 3, [1, i]) : (this.scanner.consume(1), [3, null]);
|
|
514
515
|
if (!u) {
|
|
515
516
|
if (n === "{") {
|
|
516
517
|
if (i)
|
|
@@ -520,7 +521,16 @@ class D {
|
|
|
520
521
|
} else if (n === "}") {
|
|
521
522
|
if (i)
|
|
522
523
|
return [1, i];
|
|
523
|
-
this.scanner.consume(1), this.
|
|
524
|
+
if (this.scanner.consume(1), this.yieldPropertyCommands) {
|
|
525
|
+
this.popCtx();
|
|
526
|
+
const o = this.getPropertyChanges(this.lastCtx, this.ctx);
|
|
527
|
+
if (Object.keys(o).length > 0)
|
|
528
|
+
return this.lastCtx = this.ctx.copy(), [
|
|
529
|
+
9,
|
|
530
|
+
{ command: void 0, changes: o, depth: this.ctxStack.length }
|
|
531
|
+
];
|
|
532
|
+
} else
|
|
533
|
+
this.popCtx();
|
|
524
534
|
continue;
|
|
525
535
|
}
|
|
526
536
|
}
|
|
@@ -549,7 +559,7 @@ class D {
|
|
|
549
559
|
for (; ; ) {
|
|
550
560
|
const [i, h] = a();
|
|
551
561
|
if (i)
|
|
552
|
-
yield new b(i, this.ctx, h),
|
|
562
|
+
yield new b(i, this.ctx, h), r && (yield new b(r, this.ctx, null), r = null);
|
|
553
563
|
else
|
|
554
564
|
break;
|
|
555
565
|
}
|
|
@@ -614,21 +624,21 @@ class g {
|
|
|
614
624
|
* @returns Index of the character, or -1 if not found
|
|
615
625
|
*/
|
|
616
626
|
find(t, e = !1) {
|
|
617
|
-
let
|
|
618
|
-
for (;
|
|
619
|
-
if (e && this.text[
|
|
620
|
-
if (
|
|
621
|
-
if (this.text[
|
|
622
|
-
return
|
|
623
|
-
|
|
627
|
+
let r = this._index;
|
|
628
|
+
for (; r < this.textLen; ) {
|
|
629
|
+
if (e && this.text[r] === "\\") {
|
|
630
|
+
if (r + 1 < this.textLen) {
|
|
631
|
+
if (this.text[r + 1] === t)
|
|
632
|
+
return r + 1;
|
|
633
|
+
r += 2;
|
|
624
634
|
continue;
|
|
625
635
|
}
|
|
626
|
-
|
|
636
|
+
r++;
|
|
627
637
|
continue;
|
|
628
638
|
}
|
|
629
|
-
if (this.text[
|
|
630
|
-
return
|
|
631
|
-
|
|
639
|
+
if (this.text[r] === t)
|
|
640
|
+
return r;
|
|
641
|
+
r++;
|
|
632
642
|
}
|
|
633
643
|
return -1;
|
|
634
644
|
}
|
|
@@ -690,8 +700,8 @@ class f {
|
|
|
690
700
|
*/
|
|
691
701
|
get rgb() {
|
|
692
702
|
if (this._rgbValue === null) return null;
|
|
693
|
-
const t = this._rgbValue >> 16 & 255, e = this._rgbValue >> 8 & 255,
|
|
694
|
-
return [t, e,
|
|
703
|
+
const t = this._rgbValue >> 16 & 255, e = this._rgbValue >> 8 & 255, r = this._rgbValue & 255;
|
|
704
|
+
return [t, e, r];
|
|
695
705
|
}
|
|
696
706
|
/**
|
|
697
707
|
* Set the RGB color. Setting this disables ACI color.
|
|
@@ -699,8 +709,8 @@ class f {
|
|
|
699
709
|
*/
|
|
700
710
|
set rgb(t) {
|
|
701
711
|
if (t) {
|
|
702
|
-
const [e,
|
|
703
|
-
this._rgbValue = (e & 255) << 16 | (
|
|
712
|
+
const [e, r, a] = t;
|
|
713
|
+
this._rgbValue = (e & 255) << 16 | (r & 255) << 8 | a & 255, this._aci = null;
|
|
704
714
|
} else
|
|
705
715
|
this._rgbValue = null;
|
|
706
716
|
}
|
|
@@ -903,8 +913,8 @@ class b {
|
|
|
903
913
|
* @param ctx - The text context at this token
|
|
904
914
|
* @param data - Optional token data
|
|
905
915
|
*/
|
|
906
|
-
constructor(t, e,
|
|
907
|
-
this.type = t, this.ctx = e, this.data =
|
|
916
|
+
constructor(t, e, r) {
|
|
917
|
+
this.type = t, this.ctx = e, this.data = r;
|
|
908
918
|
}
|
|
909
919
|
}
|
|
910
920
|
export {
|
|
@@ -912,14 +922,14 @@ export {
|
|
|
912
922
|
d as MTextContext,
|
|
913
923
|
_ as MTextLineAlignment,
|
|
914
924
|
m as MTextParagraphAlignment,
|
|
915
|
-
|
|
925
|
+
x as MTextParser,
|
|
916
926
|
E as MTextStroke,
|
|
917
927
|
b as MTextToken,
|
|
918
928
|
g as TextScanner,
|
|
919
929
|
k as TokenType,
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
930
|
+
O as escapeDxfLineEndings,
|
|
931
|
+
N as getFonts,
|
|
932
|
+
I as hasInlineFormattingCodes,
|
|
923
933
|
v as int2rgb,
|
|
924
934
|
R as rgb2int
|
|
925
935
|
};
|