@mlightcad/mtext-parser 1.1.1 → 1.1.2

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 k = /* @__PURE__ */ ((r) => (r[r.NONE = 0] = "NONE", r[r.WORD = 1] = "WORD", r[r.STACK = 2] = "STACK", r[r.SPACE = 3] = "SPACE", r[r.NBSP = 4] = "NBSP", r[r.TABULATOR = 5] = "TABULATOR", r[r.NEW_PARAGRAPH = 6] = "NEW_PARAGRAPH", r[r.NEW_COLUMN = 7] = "NEW_COLUMN", r[r.WRAP_AT_DIMLINE = 8] = "WRAP_AT_DIMLINE", r[r.PROPERTIES_CHANGED = 9] = "PROPERTIES_CHANGED", r))(k || {}), _ = /* @__PURE__ */ ((r) => (r[r.BOTTOM = 0] = "BOTTOM", r[r.MIDDLE = 1] = "MIDDLE", r[r.TOP = 2] = "TOP", r))(_ || {}), m = /* @__PURE__ */ ((r) => (r[r.DEFAULT = 0] = "DEFAULT", r[r.LEFT = 1] = "LEFT", r[r.RIGHT = 2] = "RIGHT", r[r.CENTER = 3] = "CENTER", r[r.JUSTIFIED = 4] = "JUSTIFIED", r[r.DISTRIBUTED = 5] = "DISTRIBUTED", r))(m || {}), E = /* @__PURE__ */ ((r) => (r[r.NONE = 0] = "NONE", r[r.UNDERLINE = 1] = "UNDERLINE", r[r.OVERLINE = 2] = "OVERLINE", r[r.STRIKE_THROUGH = 4] = "STRIKE_THROUGH", r))(E || {});
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: "°",
5
5
  p: "±"
6
- }, S = {
6
+ }, R = {
7
7
  l: 1,
8
8
  r: 2,
9
9
  c: 3,
@@ -11,36 +11,36 @@ const F = {
11
11
  d: 5
12
12
  /* DISTRIBUTED */
13
13
  };
14
- function R(r) {
15
- const [t, e, s] = r;
16
- return s << 16 | e << 8 | t;
14
+ function S(s) {
15
+ const [t, e, r] = s;
16
+ return r << 16 | e << 8 | t;
17
17
  }
18
- function v(r) {
19
- const t = r & 255, e = r >> 8 & 255, s = r >> 16 & 255;
20
- return [t, e, s];
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 I(r) {
23
- return r.replace(/\r\n|\r|\n/g, "\\P");
22
+ function O(s) {
23
+ return s.replace(/\r\n|\r|\n/g, "\\P");
24
24
  }
25
- function N(r) {
26
- return r.replace(/\\P/g, "").replace(/\\~/g, "").includes("\\");
25
+ function I(s) {
26
+ return s.replace(/\\P/g, "").replace(/\\~/g, "").includes("\\");
27
27
  }
28
- function O(r, t = !1) {
29
- const e = /* @__PURE__ */ new Set(), s = /\\[fF](.*?)[;|]/g;
30
- return [...r.matchAll(s)].forEach((a) => {
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 D {
35
+ class y {
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, s = !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 = s;
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 = "", s = "", a = "";
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 && (s = u(a === "^")), e === "" && s.includes("I/") ? [2, [" ", " ", "/"]] : a === "^" ? [2, [e, s, "^"]] : [2, [e, s, 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,11 @@ 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 s = this.getPropertyChanges(this.lastCtx, e);
168
- if (Object.keys(s).length > 0)
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: s
171
+ changes: r
172
172
  };
173
173
  }
174
174
  }
@@ -179,12 +179,12 @@ class D {
179
179
  * @returns Object containing changed properties
180
180
  */
181
181
  getPropertyChanges(t, e) {
182
- const s = {};
183
- if (t.underline !== e.underline && (s.underline = e.underline), t.overline !== e.overline && (s.overline = e.overline), t.strikeThrough !== e.strikeThrough && (s.strikeThrough = e.strikeThrough), t.color.aci !== e.color.aci && (s.aci = e.color.aci), t.color.rgbValue !== e.color.rgbValue && (s.rgb = e.color.rgb), t.align !== e.align && (s.align = e.align), JSON.stringify(t.fontFace) !== JSON.stringify(e.fontFace) && (s.fontFace = e.fontFace), (t.capHeight.value !== e.capHeight.value || t.capHeight.isRelative !== e.capHeight.isRelative) && (s.capHeight = e.capHeight), (t.widthFactor.value !== e.widthFactor.value || t.widthFactor.isRelative !== e.widthFactor.isRelative) && (s.widthFactor = e.widthFactor), (t.charTrackingFactor.value !== e.charTrackingFactor.value || t.charTrackingFactor.isRelative !== e.charTrackingFactor.isRelative) && (s.charTrackingFactor = e.charTrackingFactor), t.oblique !== e.oblique && (s.oblique = e.oblique), JSON.stringify(t.paragraph) !== JSON.stringify(e.paragraph)) {
182
+ const r = {};
183
+ 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
184
  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 && (s.paragraph = 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 && (r.paragraph = a);
186
186
  }
187
- return s;
187
+ return r;
188
188
  }
189
189
  /**
190
190
  * Parse alignment property
@@ -266,8 +266,8 @@ class D {
266
266
  const e = this.extractFloatExpression(!0);
267
267
  if (e)
268
268
  if (e.endsWith("x")) {
269
- const s = parseFloat(e.slice(0, -1));
270
- t *= s;
269
+ const r = parseFloat(e.slice(0, -1));
270
+ t *= r;
271
271
  } else
272
272
  t = parseFloat(e);
273
273
  return t;
@@ -287,8 +287,8 @@ class D {
287
287
  parseAciColor(t) {
288
288
  const e = this.extractIntExpression();
289
289
  if (e) {
290
- const s = parseInt(e);
291
- s < 257 && (t.color.aci = s);
290
+ const r = parseInt(e);
291
+ r < 257 && (t.color.aci = r);
292
292
  }
293
293
  this.consumeOptionalTerminator();
294
294
  }
@@ -299,8 +299,8 @@ class D {
299
299
  parseRgbColor(t) {
300
300
  const e = this.extractIntExpression();
301
301
  if (e) {
302
- const s = parseInt(e) & 16777215;
303
- t.color.rgbValue = s;
302
+ const r = parseInt(e) & 16777215;
303
+ t.color.rgbValue = r;
304
304
  }
305
305
  this.consumeOptionalTerminator();
306
306
  }
@@ -310,9 +310,9 @@ class D {
310
310
  * @returns Extracted expression
311
311
  */
312
312
  extractFloatExpression(t = !1) {
313
- const e = t ? /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?x?/ : /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/, s = this.scanner.tail.match(e);
314
- if (s) {
315
- const a = s[0];
313
+ const e = t ? /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?x?/ : /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/, r = this.scanner.tail.match(e);
314
+ if (r) {
315
+ const a = r[0];
316
316
  return this.scanner.consume(a.length), a;
317
317
  }
318
318
  return "";
@@ -350,12 +350,12 @@ class D {
350
350
  parseFontProperties(t) {
351
351
  const e = this.extractExpression().split("|");
352
352
  if (e.length > 0 && e[0]) {
353
- const s = e[0];
353
+ const r = e[0];
354
354
  let a = "Regular", i = 400;
355
355
  for (const h of e.slice(1))
356
- h.startsWith("b1") ? i = 700 : h.startsWith("i1") && (a = "Italic");
356
+ h.startsWith("b1") ? i = 700 : h === "i" || h.startsWith("i1") ? a = "Italic" : (h === "i0" || h.startsWith("i0")) && (a = "Regular");
357
357
  t.fontFace = {
358
- family: s,
358
+ family: r,
359
359
  style: a,
360
360
  weight: i
361
361
  };
@@ -368,7 +368,7 @@ class D {
368
368
  */
369
369
  parseParagraphProperties(t) {
370
370
  const e = new g(this.extractExpression());
371
- let s = t.paragraph.indent, a = t.paragraph.left, i = t.paragraph.right, h = t.paragraph.align, u = [];
371
+ let r = t.paragraph.indent, a = t.paragraph.left, i = t.paragraph.right, h = t.paragraph.align, u = [];
372
372
  const n = () => {
373
373
  const l = e.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);
374
374
  if (l) {
@@ -382,7 +382,7 @@ class D {
382
382
  for (; e.hasData; )
383
383
  switch (e.get()) {
384
384
  case "i":
385
- s = n();
385
+ r = n();
386
386
  break;
387
387
  case "l":
388
388
  a = n();
@@ -394,7 +394,7 @@ class D {
394
394
  break;
395
395
  case "q": {
396
396
  const o = e.get();
397
- for (h = S[o] || 0; e.peek() === ","; )
397
+ for (h = R[o] || 0; e.peek() === ","; )
398
398
  e.consume(1);
399
399
  break;
400
400
  }
@@ -413,7 +413,7 @@ class D {
413
413
  break;
414
414
  }
415
415
  t.paragraph = {
416
- indent: s,
416
+ indent: r,
417
417
  left: a,
418
418
  right: i,
419
419
  align: h,
@@ -431,7 +431,7 @@ class D {
431
431
  * @yields MTextToken objects
432
432
  */
433
433
  *parse() {
434
- let s = null;
434
+ let r = null;
435
435
  const a = () => {
436
436
  var h;
437
437
  let i = "";
@@ -510,7 +510,7 @@ class D {
510
510
  }
511
511
  }
512
512
  if (n === " ")
513
- return i ? (this.scanner.consume(1), s = 3, [1, i]) : (this.scanner.consume(1), [3, null]);
513
+ return i ? (this.scanner.consume(1), r = 3, [1, i]) : (this.scanner.consume(1), [3, null]);
514
514
  if (!u) {
515
515
  if (n === "{") {
516
516
  if (i)
@@ -520,7 +520,13 @@ class D {
520
520
  } else if (n === "}") {
521
521
  if (i)
522
522
  return [1, i];
523
- this.scanner.consume(1), this.popCtx();
523
+ if (this.scanner.consume(1), this.yieldPropertyCommands) {
524
+ this.popCtx();
525
+ const o = this.getPropertyChanges(this.lastCtx, this.ctx);
526
+ if (Object.keys(o).length > 0)
527
+ return this.lastCtx = this.ctx.copy(), [9, { command: void 0, changes: o }];
528
+ } else
529
+ this.popCtx();
524
530
  continue;
525
531
  }
526
532
  }
@@ -549,7 +555,7 @@ class D {
549
555
  for (; ; ) {
550
556
  const [i, h] = a();
551
557
  if (i)
552
- yield new b(i, this.ctx, h), s && (yield new b(s, this.ctx, null), s = null);
558
+ yield new b(i, this.ctx, h), r && (yield new b(r, this.ctx, null), r = null);
553
559
  else
554
560
  break;
555
561
  }
@@ -614,21 +620,21 @@ class g {
614
620
  * @returns Index of the character, or -1 if not found
615
621
  */
616
622
  find(t, e = !1) {
617
- let s = this._index;
618
- for (; s < this.textLen; ) {
619
- if (e && this.text[s] === "\\") {
620
- if (s + 1 < this.textLen) {
621
- if (this.text[s + 1] === t)
622
- return s + 1;
623
- s += 2;
623
+ let r = this._index;
624
+ for (; r < this.textLen; ) {
625
+ if (e && this.text[r] === "\\") {
626
+ if (r + 1 < this.textLen) {
627
+ if (this.text[r + 1] === t)
628
+ return r + 1;
629
+ r += 2;
624
630
  continue;
625
631
  }
626
- s++;
632
+ r++;
627
633
  continue;
628
634
  }
629
- if (this.text[s] === t)
630
- return s;
631
- s++;
635
+ if (this.text[r] === t)
636
+ return r;
637
+ r++;
632
638
  }
633
639
  return -1;
634
640
  }
@@ -690,8 +696,8 @@ class f {
690
696
  */
691
697
  get rgb() {
692
698
  if (this._rgbValue === null) return null;
693
- const t = this._rgbValue >> 16 & 255, e = this._rgbValue >> 8 & 255, s = this._rgbValue & 255;
694
- return [t, e, s];
699
+ const t = this._rgbValue >> 16 & 255, e = this._rgbValue >> 8 & 255, r = this._rgbValue & 255;
700
+ return [t, e, r];
695
701
  }
696
702
  /**
697
703
  * Set the RGB color. Setting this disables ACI color.
@@ -699,8 +705,8 @@ class f {
699
705
  */
700
706
  set rgb(t) {
701
707
  if (t) {
702
- const [e, s, a] = t;
703
- this._rgbValue = (e & 255) << 16 | (s & 255) << 8 | a & 255, this._aci = null;
708
+ const [e, r, a] = t;
709
+ this._rgbValue = (e & 255) << 16 | (r & 255) << 8 | a & 255, this._aci = null;
704
710
  } else
705
711
  this._rgbValue = null;
706
712
  }
@@ -903,8 +909,8 @@ class b {
903
909
  * @param ctx - The text context at this token
904
910
  * @param data - Optional token data
905
911
  */
906
- constructor(t, e, s) {
907
- this.type = t, this.ctx = e, this.data = s;
912
+ constructor(t, e, r) {
913
+ this.type = t, this.ctx = e, this.data = r;
908
914
  }
909
915
  }
910
916
  export {
@@ -912,15 +918,15 @@ export {
912
918
  d as MTextContext,
913
919
  _ as MTextLineAlignment,
914
920
  m as MTextParagraphAlignment,
915
- D as MTextParser,
921
+ y as MTextParser,
916
922
  E as MTextStroke,
917
923
  b as MTextToken,
918
924
  g as TextScanner,
919
925
  k as TokenType,
920
- I as escapeDxfLineEndings,
921
- O as getFonts,
922
- N as hasInlineFormattingCodes,
926
+ O as escapeDxfLineEndings,
927
+ N as getFonts,
928
+ I as hasInlineFormattingCodes,
923
929
  v as int2rgb,
924
- R as rgb2int
930
+ S as rgb2int
925
931
  };
926
932
  //# sourceMappingURL=parser.es.js.map