@mlightcad/mtext-parser 1.1.6 → 1.1.7

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.es.js CHANGED
@@ -1,9 +1,9 @@
1
- var b = /* @__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))(b || {}), _ = /* @__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 || {}), S = /* @__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))(S || {});
2
- const E = {
1
+ var _ = /* @__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))(_ || {}), m = /* @__PURE__ */ ((s) => (s[s.BOTTOM = 0] = "BOTTOM", s[s.MIDDLE = 1] = "MIDDLE", s[s.TOP = 2] = "TOP", s))(m || {}), S = /* @__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))(S || {}), 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
+ const F = {
3
3
  c: "Ø",
4
4
  d: "°",
5
5
  p: "±"
6
- }, F = {
6
+ }, R = {
7
7
  l: 1,
8
8
  r: 2,
9
9
  c: 3,
@@ -11,11 +11,11 @@ const E = {
11
11
  d: 5
12
12
  /* DISTRIBUTED */
13
13
  };
14
- function v(s) {
14
+ function O(s) {
15
15
  const [t, e, r] = s;
16
16
  return r << 16 | e << 8 | t;
17
17
  }
18
- function O(s) {
18
+ function y(s) {
19
19
  const t = s & 255, e = s >> 8 & 255, r = s >> 16 & 255;
20
20
  return [t, e, r];
21
21
  }
@@ -25,14 +25,14 @@ function N(s) {
25
25
  function I(s) {
26
26
  return s.replace(/\\P/g, "").replace(/\\~/g, "").includes("\\");
27
27
  }
28
- function y(s, t = !1) {
28
+ function x(s, t = !1) {
29
29
  const e = /* @__PURE__ */ new Set(), r = /\\[fF](.*?)[;|]/g;
30
30
  return [...s.matchAll(r)].forEach((a) => {
31
- let i = a[1].toLowerCase();
32
- t && (i = i.replace(/\.(ttf|otf|woff|shx)$/, "")), e.add(i);
31
+ let l = a[1].toLowerCase();
32
+ t && (l = l.replace(/\.(ttf|otf|woff|shx)$/, "")), e.add(l);
33
33
  }), e;
34
34
  }
35
- class R {
35
+ class v {
36
36
  /**
37
37
  * Creates a new ContextStack with an initial context.
38
38
  * @param initial The initial MTextContext to use as the base of the stack.
@@ -89,12 +89,12 @@ class D {
89
89
  * Creates a new MTextParser instance
90
90
  * @param content - The MText content to parse
91
91
  * @param ctx - Optional initial MText context
92
- * @param yieldPropertyCommands - Whether to yield property change commands
92
+ * @param options - Parser options
93
93
  */
94
- constructor(t, e, r = !1) {
94
+ constructor(t, e, r = {}) {
95
95
  this.continueStroke = !1, this.inStackContext = !1, this.scanner = new g(t);
96
- const a = e ?? new d();
97
- this.ctxStack = new R(a), this.yieldPropertyCommands = r;
96
+ const a = e ?? new k();
97
+ this.ctxStack = new v(a), this.yieldPropertyCommands = r.yieldPropertyCommands ?? !1, this.resetParagraphParameters = r.resetParagraphParameters ?? !1;
98
98
  }
99
99
  /**
100
100
  * Decode multi-byte character from hex code
@@ -109,8 +109,8 @@ class D {
109
109
  ]), a = new TextDecoder("gbk").decode(e);
110
110
  if (a !== "▯")
111
111
  return a;
112
- const h = new TextDecoder("big5").decode(e);
113
- return h !== "▯" ? h : "▯";
112
+ const i = new TextDecoder("big5").decode(e);
113
+ return i !== "▯" ? i : "▯";
114
114
  } catch {
115
115
  return "▯";
116
116
  }
@@ -134,31 +134,31 @@ class D {
134
134
  parseStacking() {
135
135
  const t = new g(this.extractExpression(!0));
136
136
  let e = "", r = "", a = "";
137
- const i = () => {
138
- let n = t.peek(), l = !1;
139
- return n.charCodeAt(0) < 32 && (n = " "), n === "\\" && (l = !0, t.consume(1), n = t.peek()), t.consume(1), [n, l];
140
- }, h = () => {
141
- let n = "";
137
+ const l = () => {
138
+ let c = t.peek(), n = !1;
139
+ return c.charCodeAt(0) < 32 && (c = " "), c === "\\" && (n = !0, t.consume(1), c = t.peek()), t.consume(1), [c, n];
140
+ }, i = () => {
141
+ let c = "";
142
142
  for (; t.hasData; ) {
143
- const [l, o] = i();
144
- if (!o && (l === "/" || l === "#" || l === "^"))
145
- return [n, l];
146
- n += l;
143
+ const [n, p] = l();
144
+ if (!p && (n === "/" || n === "#" || n === "^"))
145
+ return [c, n];
146
+ c += n;
147
147
  }
148
- return [n, ""];
149
- }, u = (n) => {
150
- let l = "", o = n;
148
+ return [c, ""];
149
+ }, u = (c) => {
150
+ let n = "", p = c;
151
151
  for (; t.hasData; ) {
152
- const [c, p] = i();
153
- if (!(o && c === " ")) {
154
- if (o = !1, !p && c === ";")
152
+ const [h, o] = l();
153
+ if (!(p && h === " ")) {
154
+ if (p = !1, !o && h === ";")
155
155
  break;
156
- l += c;
156
+ n += h;
157
157
  }
158
158
  }
159
- return l;
159
+ return n;
160
160
  };
161
- return [e, a] = h(), a && (r = u(a === "^")), e === "" && r.includes("I/") ? [2, [" ", " ", "/"]] : a === "^" ? [2, [e, r, "^"]] : [2, [e, r, a]];
161
+ return [e, a] = i(), a && (r = u(a === "^")), e === "" && r.includes("I/") ? [2, [" ", " ", "/"]] : a === "^" ? [2, [e, r, "^"]] : [2, [e, r, a]];
162
162
  }
163
163
  /**
164
164
  * Parse MText properties
@@ -392,11 +392,11 @@ class D {
392
392
  extractExpression(t = !1) {
393
393
  const e = this.scanner.find(";", t);
394
394
  if (e < 0) {
395
- const h = this.scanner.tail;
396
- return this.scanner.consume(h.length), h;
395
+ const i = this.scanner.tail;
396
+ return this.scanner.consume(i.length), i;
397
397
  }
398
- const a = this.scanner.peek(e - this.scanner.currentIndex - 1) === "\\", i = this.scanner.tail.slice(0, e - this.scanner.currentIndex + (a ? 1 : 0));
399
- return this.scanner.consume(i.length + 1), i;
398
+ const a = this.scanner.peek(e - this.scanner.currentIndex - 1) === "\\", l = this.scanner.tail.slice(0, e - this.scanner.currentIndex + (a ? 1 : 0));
399
+ return this.scanner.consume(l.length + 1), l;
400
400
  }
401
401
  /**
402
402
  * Parse font properties
@@ -406,13 +406,13 @@ class D {
406
406
  const e = this.extractExpression().split("|");
407
407
  if (e.length > 0 && e[0]) {
408
408
  const r = e[0];
409
- let a = "Regular", i = 400;
410
- for (const h of e.slice(1))
411
- h.startsWith("b1") ? i = 700 : h === "i" || h.startsWith("i1") ? a = "Italic" : (h === "i0" || h.startsWith("i0")) && (a = "Regular");
409
+ let a = "Regular", l = 400;
410
+ for (const i of e.slice(1))
411
+ i.startsWith("b1") ? l = 700 : i === "i" || i.startsWith("i1") ? a = "Italic" : (i === "i0" || i.startsWith("i0")) && (a = "Regular");
412
412
  t.fontFace = {
413
413
  family: r,
414
414
  style: a,
415
- weight: i
415
+ weight: l
416
416
  };
417
417
  }
418
418
  }
@@ -423,46 +423,46 @@ class D {
423
423
  */
424
424
  parseParagraphProperties(t) {
425
425
  const e = new g(this.extractExpression());
426
- let r = t.paragraph.indent, a = t.paragraph.left, i = t.paragraph.right, h = t.paragraph.align, u = [];
427
- const n = () => {
428
- const l = e.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);
429
- if (l) {
430
- const o = parseFloat(l[0]);
431
- for (e.consume(l[0].length); e.peek() === ","; )
426
+ let r = t.paragraph.indent, a = t.paragraph.left, l = t.paragraph.right, i = t.paragraph.align, u = [];
427
+ const c = () => {
428
+ const n = e.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);
429
+ if (n) {
430
+ const p = parseFloat(n[0]);
431
+ for (e.consume(n[0].length); e.peek() === ","; )
432
432
  e.consume(1);
433
- return o;
433
+ return p;
434
434
  }
435
435
  return 0;
436
436
  };
437
437
  for (; e.hasData; )
438
438
  switch (e.get()) {
439
439
  case "i":
440
- r = n();
440
+ r = c();
441
441
  break;
442
442
  case "l":
443
- a = n();
443
+ a = c();
444
444
  break;
445
445
  case "r":
446
- i = n();
446
+ l = c();
447
447
  break;
448
448
  case "x":
449
449
  break;
450
450
  case "q": {
451
- const o = e.get();
452
- for (h = F[o] || 0; e.peek() === ","; )
451
+ const p = e.get();
452
+ for (i = R[p] || 0; e.peek() === ","; )
453
453
  e.consume(1);
454
454
  break;
455
455
  }
456
456
  case "t":
457
457
  for (u = []; e.hasData; ) {
458
- const o = e.peek();
459
- if (o === "r" || o === "c") {
458
+ const p = e.peek();
459
+ if (p === "r" || p === "c") {
460
460
  e.consume(1);
461
- const c = n();
462
- u.push(o + c.toString());
461
+ const h = c();
462
+ u.push(p + h.toString());
463
463
  } else {
464
- const c = n();
465
- isNaN(c) ? e.consume(1) : u.push(c);
464
+ const h = c();
465
+ isNaN(h) ? e.consume(1) : u.push(h);
466
466
  }
467
467
  }
468
468
  break;
@@ -470,8 +470,8 @@ class D {
470
470
  t.paragraph = {
471
471
  indent: r,
472
472
  left: a,
473
- right: i,
474
- align: h,
473
+ right: l,
474
+ align: i,
475
475
  tab_stops: u
476
476
  };
477
477
  }
@@ -487,12 +487,24 @@ class D {
487
487
  */
488
488
  *parse() {
489
489
  let r = null;
490
- const a = () => {
491
- var h;
490
+ function a(i) {
491
+ const u = { ...i.paragraph };
492
+ i.paragraph = {
493
+ indent: 0,
494
+ left: 0,
495
+ right: 0,
496
+ align: 0,
497
+ tab_stops: []
498
+ };
499
+ const c = {};
500
+ return u.indent !== 0 && (c.indent = 0), u.left !== 0 && (c.left = 0), u.right !== 0 && (c.right = 0), u.align !== 0 && (c.align = 0), JSON.stringify(u.tab_stops) !== JSON.stringify([]) && (c.tab_stops = []), c;
501
+ }
502
+ const l = () => {
503
+ var u;
492
504
  let i = "";
493
505
  for (; this.scanner.hasData; ) {
494
- let u = !1, n = this.scanner.peek();
495
- const l = this.scanner.currentIndex;
506
+ let c = !1, n = this.scanner.peek();
507
+ const p = this.scanner.currentIndex;
496
508
  if (n.charCodeAt(0) < 32) {
497
509
  if (this.scanner.consume(1), n === " ")
498
510
  return [5, null];
@@ -503,13 +515,13 @@ class D {
503
515
  }
504
516
  if (n === "\\")
505
517
  if ("\\{}".includes(this.scanner.peek(1)))
506
- u = !0, this.scanner.consume(1), n = this.scanner.peek();
518
+ c = !0, this.scanner.consume(1), n = this.scanner.peek();
507
519
  else {
508
520
  if (i)
509
521
  return [1, i];
510
522
  this.scanner.consume(1);
511
- const o = this.scanner.get();
512
- switch (o) {
523
+ const h = this.scanner.get();
524
+ switch (h) {
513
525
  case "~":
514
526
  return [4, null];
515
527
  case "P":
@@ -520,44 +532,44 @@ class D {
520
532
  return [8, null];
521
533
  case "S": {
522
534
  this.inStackContext = !0;
523
- const c = this.parseStacking();
524
- return this.inStackContext = !1, c;
535
+ const o = this.parseStacking();
536
+ return this.inStackContext = !1, o;
525
537
  }
526
538
  case "m":
527
539
  case "M":
528
540
  if (this.scanner.peek() === "+") {
529
541
  this.scanner.consume(1);
530
- const c = (h = this.scanner.tail.match(/^[0-9A-Fa-f]{4}/)) == null ? void 0 : h[0];
531
- if (c) {
542
+ const o = (u = this.scanner.tail.match(/^[0-9A-Fa-f]{4}/)) == null ? void 0 : u[0];
543
+ if (o) {
532
544
  this.scanner.consume(4);
533
- const p = this.decodeMultiByteChar(c);
534
- return i ? [1, i] : [1, p];
545
+ const b = this.decodeMultiByteChar(o);
546
+ return i ? [1, i] : [1, b];
535
547
  }
536
548
  this.scanner.consume(-1);
537
549
  }
538
550
  i += "\\M";
539
551
  continue;
540
552
  default:
541
- if (o)
553
+ if (h)
542
554
  try {
543
- const c = this.parseProperties(o);
544
- if (this.yieldPropertyCommands && c)
545
- return [9, c];
555
+ const o = this.parseProperties(h);
556
+ if (this.yieldPropertyCommands && o)
557
+ return [9, o];
546
558
  continue;
547
559
  } catch {
548
- const c = this.scanner.tail.slice(
549
- l,
560
+ const o = this.scanner.tail.slice(
561
+ p,
550
562
  this.scanner.currentIndex
551
563
  );
552
- i += c;
564
+ i += o;
553
565
  }
554
566
  }
555
567
  continue;
556
568
  }
557
569
  if (n === "%" && this.scanner.peek(1) === "%") {
558
- const o = this.scanner.peek(2).toLowerCase(), c = E[o];
559
- if (c) {
560
- this.scanner.consume(3), i += c;
570
+ const h = this.scanner.peek(2).toLowerCase(), o = F[h];
571
+ if (o) {
572
+ this.scanner.consume(3), i += o;
561
573
  continue;
562
574
  } else {
563
575
  this.scanner.consume(3);
@@ -566,7 +578,7 @@ class D {
566
578
  }
567
579
  if (n === " ")
568
580
  return i ? (this.scanner.consume(1), r = 3, [1, i]) : (this.scanner.consume(1), [3, null]);
569
- if (!u) {
581
+ if (!c) {
570
582
  if (n === "{") {
571
583
  if (i)
572
584
  return [1, i];
@@ -576,13 +588,13 @@ class D {
576
588
  if (i)
577
589
  return [1, i];
578
590
  if (this.scanner.consume(1), this.yieldPropertyCommands) {
579
- const o = this.ctxStack.current;
591
+ const h = this.ctxStack.current;
580
592
  this.popCtx();
581
- const c = this.getPropertyChanges(o, this.ctxStack.current);
582
- if (Object.keys(c).length > 0)
593
+ const o = this.getPropertyChanges(h, this.ctxStack.current);
594
+ if (Object.keys(o).length > 0)
583
595
  return [
584
596
  9,
585
- { command: void 0, changes: c, depth: this.ctxStack.depth }
597
+ { command: void 0, changes: o, depth: this.ctxStack.depth }
586
598
  ];
587
599
  } else
588
600
  this.popCtx();
@@ -590,17 +602,17 @@ class D {
590
602
  }
591
603
  }
592
604
  if (!this.inStackContext && n === "^") {
593
- const o = this.scanner.peek(1);
594
- if (o) {
595
- const c = o.charCodeAt(0);
596
- if (this.scanner.consume(2), c === 32)
605
+ const h = this.scanner.peek(1);
606
+ if (h) {
607
+ const o = h.charCodeAt(0);
608
+ if (this.scanner.consume(2), o === 32)
597
609
  i += "^";
598
610
  else {
599
- if (c === 73)
611
+ if (o === 73)
600
612
  return i ? [1, i] : [5, null];
601
- if (c === 74)
613
+ if (o === 74)
602
614
  return i ? [1, i] : [6, null];
603
- if (c === 77)
615
+ if (o === 77)
604
616
  continue;
605
617
  i += "▯";
606
618
  }
@@ -612,10 +624,22 @@ class D {
612
624
  return i ? [1, i] : [0, null];
613
625
  };
614
626
  for (; ; ) {
615
- const [i, h] = a();
616
- if (i)
617
- yield new k(i, this.ctxStack.current.copy(), h), r && (yield new k(r, this.ctxStack.current.copy(), null), r = null);
618
- else
627
+ const [i, u] = l.call(this);
628
+ if (i) {
629
+ if (yield new f(i, this.ctxStack.current.copy(), u), i === 6 && this.resetParagraphParameters) {
630
+ const c = this.ctxStack.current, n = a(c);
631
+ this.yieldPropertyCommands && Object.keys(n).length > 0 && (yield new f(
632
+ 9,
633
+ c.copy(),
634
+ {
635
+ command: void 0,
636
+ changes: { paragraph: n },
637
+ depth: this.ctxStack.depth
638
+ }
639
+ ));
640
+ }
641
+ r && (yield new f(r, this.ctxStack.current.copy(), null), r = null);
642
+ } else
619
643
  break;
620
644
  }
621
645
  }
@@ -720,7 +744,7 @@ class g {
720
744
  return t;
721
745
  }
722
746
  }
723
- class f {
747
+ class d {
724
748
  // Store as 0xRRGGBB or null
725
749
  /**
726
750
  * Create a new MTextColor instance.
@@ -796,7 +820,7 @@ class f {
796
820
  * @returns A new MTextColor instance with the same color state.
797
821
  */
798
822
  copy() {
799
- const t = new f();
823
+ const t = new d();
800
824
  return t._aci = this._aci, t._rgbValue = this._rgbValue, t;
801
825
  }
802
826
  /**
@@ -815,9 +839,9 @@ class f {
815
839
  return this._aci === t._aci && this._rgbValue === t._rgbValue;
816
840
  }
817
841
  }
818
- class d {
842
+ class k {
819
843
  constructor() {
820
- this._stroke = 0, this.continueStroke = !1, this.color = new f(), this.align = 0, this.fontFace = { family: "", style: "Regular", weight: 400 }, this._capHeight = { value: 1, isRelative: !1 }, this._widthFactor = { value: 1, isRelative: !1 }, this._charTrackingFactor = { value: 1, isRelative: !1 }, this.oblique = 0, this.paragraph = {
844
+ this._stroke = 0, this.continueStroke = !1, this.color = new d(), this.align = 0, this.fontFace = { family: "", style: "Regular", weight: 400 }, this._capHeight = { value: 1, isRelative: !1 }, this._widthFactor = { value: 1, isRelative: !1 }, this._charTrackingFactor = { value: 1, isRelative: !1 }, this.oblique = 0, this.paragraph = {
821
845
  indent: 0,
822
846
  left: 0,
823
847
  right: 0,
@@ -957,11 +981,11 @@ class d {
957
981
  * @returns A new context with the same properties
958
982
  */
959
983
  copy() {
960
- const t = new d();
984
+ const t = new k();
961
985
  return t._stroke = this._stroke, t.continueStroke = this.continueStroke, t.color = this.color.copy(), t.align = this.align, t.fontFace = { ...this.fontFace }, t._capHeight = { ...this._capHeight }, t._widthFactor = { ...this._widthFactor }, t._charTrackingFactor = { ...this._charTrackingFactor }, t.oblique = this.oblique, t.paragraph = { ...this.paragraph }, t;
962
986
  }
963
987
  }
964
- class k {
988
+ class f {
965
989
  /**
966
990
  * Create a new MText token
967
991
  * @param type - The token type
@@ -973,19 +997,19 @@ class k {
973
997
  }
974
998
  }
975
999
  export {
976
- f as MTextColor,
977
- d as MTextContext,
978
- _ as MTextLineAlignment,
979
- m as MTextParagraphAlignment,
1000
+ d as MTextColor,
1001
+ k as MTextContext,
1002
+ m as MTextLineAlignment,
1003
+ S as MTextParagraphAlignment,
980
1004
  D as MTextParser,
981
- S as MTextStroke,
982
- k as MTextToken,
1005
+ E as MTextStroke,
1006
+ f as MTextToken,
983
1007
  g as TextScanner,
984
- b as TokenType,
1008
+ _ as TokenType,
985
1009
  N as escapeDxfLineEndings,
986
- y as getFonts,
1010
+ x as getFonts,
987
1011
  I as hasInlineFormattingCodes,
988
- O as int2rgb,
989
- v as rgb2int
1012
+ y as int2rgb,
1013
+ O as rgb2int
990
1014
  };
991
1015
  //# sourceMappingURL=parser.es.js.map