@mlightcad/mtext-parser 1.0.6 → 1.0.8

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
@@ -11,28 +11,21 @@ const m = {
11
11
  d: 5
12
12
  /* DISTRIBUTED */
13
13
  };
14
- function v(s) {
14
+ function R(s) {
15
15
  const [e, t, r] = s;
16
16
  return r << 16 | t << 8 | e;
17
17
  }
18
- function R(s) {
18
+ function S(s) {
19
19
  const e = s & 255, t = s >> 8 & 255, r = s >> 16 & 255;
20
20
  return [e, t, r];
21
21
  }
22
- function S(s) {
23
- return s.replace(/\^(.)/g, (e, t) => {
24
- const r = t.charCodeAt(0);
25
- return r === 32 ? "^" : r === 73 ? " " : r === 74 ? `
26
- ` : r === 77 ? "" : "▯";
27
- });
28
- }
29
- function I(s) {
22
+ function v(s) {
30
23
  return s.replace(/\r\n|\r|\n/g, "\\P");
31
24
  }
32
- function N(s) {
25
+ function I(s) {
33
26
  return s.replace(/\\P/g, "").replace(/\\~/g, "").includes("\\");
34
27
  }
35
- class O {
28
+ class N {
36
29
  /**
37
30
  * Creates a new MTextParser instance
38
31
  * @param content - The MText content to parse
@@ -40,7 +33,7 @@ class O {
40
33
  * @param yieldPropertyCommands - Whether to yield property change commands
41
34
  */
42
35
  constructor(e, t, r = !1) {
43
- this.ctxStack = [], this.continueStroke = !1, this.scanner = new p(S(e)), this.ctx = t ?? new g(), this.lastCtx = this.ctx.copy(), this.yieldPropertyCommands = r, this.decoder = new TextDecoder("gbk");
36
+ this.ctxStack = [], this.continueStroke = !1, this.inStackContext = !1, this.scanner = new f(e), this.ctx = t ?? new g(), this.lastCtx = this.ctx.copy(), this.yieldPropertyCommands = r, this.decoder = new TextDecoder("gbk");
44
37
  }
45
38
  /**
46
39
  * Decode multi-byte character from hex code
@@ -52,8 +45,11 @@ class O {
52
45
  const t = new Uint8Array([
53
46
  parseInt(e.substr(0, 2), 16),
54
47
  parseInt(e.substr(2, 2), 16)
55
- ]);
56
- return this.decoder.decode(t);
48
+ ]), a = new TextDecoder("gbk").decode(t);
49
+ if (a !== "▯")
50
+ return a;
51
+ const h = new TextDecoder("big5").decode(t);
52
+ return h !== "▯" ? h : "▯";
57
53
  } catch {
58
54
  return "▯";
59
55
  }
@@ -75,29 +71,33 @@ class O {
75
71
  * @returns Tuple of [TokenType.STACK, [numerator, denominator, type]]
76
72
  */
77
73
  parseStacking() {
78
- const e = new p(this.extractExpression(!0));
79
- let t = "", r = "", n = "";
74
+ const e = new f(this.extractExpression(!0));
75
+ let t = "", r = "", a = "";
80
76
  const i = () => {
81
- let a = e.peek(), o = !1;
82
- return a.charCodeAt(0) < 32 && (a = " "), a === "\\" && (o = !0, e.consume(1), a = e.peek()), e.consume(1), [a, o];
83
- }, c = () => {
84
- let a = "";
77
+ let n = e.peek(), l = !1;
78
+ return n.charCodeAt(0) < 32 && (n = " "), n === "\\" && (l = !0, e.consume(1), n = e.peek()), e.consume(1), [n, l];
79
+ }, h = () => {
80
+ let n = "";
85
81
  for (; e.hasData; ) {
86
- const [o, h] = i();
87
- if (!h && "^/#".includes(o))
88
- return [a, o];
89
- a += o;
82
+ const [l, o] = i();
83
+ if (!o && (l === "/" || l === "#" || l === "^"))
84
+ return [n, l];
85
+ n += l;
90
86
  }
91
- return [a, ""];
92
- }, u = () => {
93
- let a = "";
87
+ return [n, ""];
88
+ }, u = (n) => {
89
+ let l = "", o = n;
94
90
  for (; e.hasData; ) {
95
- const [o, h] = i();
96
- h && o === ";" ? a += ";" : a += o;
91
+ const [c, p] = i();
92
+ if (!(o && c === " ")) {
93
+ if (o = !1, !p && c === ";")
94
+ break;
95
+ l += c;
96
+ }
97
97
  }
98
- return a;
98
+ return l;
99
99
  };
100
- return [t, n] = c(), n && (r = u()), [2, [t, r, n]];
100
+ return [t, a] = h(), a && (r = u(a === "^")), t === "" && r.includes("I/") ? [2, [" ", " ", "/"]] : a === "^" ? [2, [t, r, "^"]] : [2, [t, r, a]];
101
101
  }
102
102
  /**
103
103
  * Parse MText properties
@@ -156,7 +156,7 @@ class O {
156
156
  default:
157
157
  throw new Error(`Unknown command: ${e}`);
158
158
  }
159
- if (t.continueStroke = this.continueStroke, this.ctx = t, this.yieldPropertyCommands) {
159
+ if (this.continueStroke = t.hasAnyStroke, t.continueStroke = this.continueStroke, this.ctx = t, this.yieldPropertyCommands) {
160
160
  const r = this.getPropertyChanges(this.lastCtx, t);
161
161
  if (Object.keys(r).length > 0)
162
162
  return this.lastCtx = this.ctx.copy(), {
@@ -174,8 +174,8 @@ class O {
174
174
  getPropertyChanges(e, t) {
175
175
  const r = {};
176
176
  if (e.underline !== t.underline && (r.underline = t.underline), e.overline !== t.overline && (r.overline = t.overline), e.strikeThrough !== t.strikeThrough && (r.strikeThrough = t.strikeThrough), e.aci !== t.aci && (r.aci = t.aci, r.rgb = t.rgb), e.rgb !== t.rgb && (r.rgb = t.rgb), e.align !== t.align && (r.align = t.align), JSON.stringify(e.fontFace) !== JSON.stringify(t.fontFace) && (r.fontFace = t.fontFace), (e.capHeight.value !== t.capHeight.value || e.capHeight.isRelative !== t.capHeight.isRelative) && (r.capHeight = t.capHeight), (e.widthFactor.value !== t.widthFactor.value || e.widthFactor.isRelative !== t.widthFactor.isRelative) && (r.widthFactor = t.widthFactor), (e.charTrackingFactor.value !== t.charTrackingFactor.value || e.charTrackingFactor.isRelative !== t.charTrackingFactor.isRelative) && (r.charTrackingFactor = t.charTrackingFactor), e.oblique !== t.oblique && (r.oblique = t.oblique), JSON.stringify(e.paragraph) !== JSON.stringify(t.paragraph)) {
177
- const n = {};
178
- e.paragraph.indent !== t.paragraph.indent && (n.indent = t.paragraph.indent), e.paragraph.align !== t.paragraph.align && (n.align = t.paragraph.align), e.paragraph.left !== t.paragraph.left && (n.left = t.paragraph.left), e.paragraph.right !== t.paragraph.right && (n.right = t.paragraph.right), JSON.stringify(e.paragraph.tab_stops) !== JSON.stringify(t.paragraph.tab_stops) && (n.tab_stops = t.paragraph.tab_stops), Object.keys(n).length > 0 && (r.paragraph = n);
177
+ const a = {};
178
+ e.paragraph.indent !== t.paragraph.indent && (a.indent = t.paragraph.indent), e.paragraph.align !== t.paragraph.align && (a.align = t.paragraph.align), e.paragraph.left !== t.paragraph.left && (a.left = t.paragraph.left), e.paragraph.right !== t.paragraph.right && (a.right = t.paragraph.right), JSON.stringify(e.paragraph.tab_stops) !== JSON.stringify(t.paragraph.tab_stops) && (a.tab_stops = t.paragraph.tab_stops), Object.keys(a).length > 0 && (r.paragraph = a);
179
179
  }
180
180
  return r;
181
181
  }
@@ -260,9 +260,9 @@ class O {
260
260
  if (t)
261
261
  if (t.endsWith("x")) {
262
262
  const r = parseFloat(t.slice(0, -1));
263
- e *= Math.abs(r);
263
+ e *= r;
264
264
  } else
265
- e = Math.abs(parseFloat(t));
265
+ e = parseFloat(t);
266
266
  return e;
267
267
  }
268
268
  /**
@@ -292,8 +292,8 @@ class O {
292
292
  parseRgbColor(e) {
293
293
  const t = this.extractIntExpression();
294
294
  if (t) {
295
- const r = parseInt(t) & 16777215, [n, i, c] = R(r);
296
- e.rgb = [c, i, n];
295
+ const r = parseInt(t) & 16777215, [a, i, h] = S(r);
296
+ e.rgb = [h, i, a];
297
297
  }
298
298
  this.consumeOptionalTerminator();
299
299
  }
@@ -305,8 +305,8 @@ class O {
305
305
  extractFloatExpression(e = !1) {
306
306
  const t = e ? /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?x?/ : /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/, r = this.scanner.tail.match(t);
307
307
  if (r) {
308
- const n = r[0];
309
- return this.scanner.consume(n.length), n;
308
+ const a = r[0];
309
+ return this.scanner.consume(a.length), a;
310
310
  }
311
311
  return "";
312
312
  }
@@ -330,10 +330,10 @@ class O {
330
330
  extractExpression(e = !1) {
331
331
  const t = this.scanner.find(";", e);
332
332
  if (t < 0) {
333
- const c = this.scanner.tail;
334
- return this.scanner.consume(c.length), c;
333
+ const h = this.scanner.tail;
334
+ return this.scanner.consume(h.length), h;
335
335
  }
336
- const n = this.scanner.peek(t - this.scanner.currentIndex - 1) === "\\", i = this.scanner.tail.slice(0, t - this.scanner.currentIndex + (n ? 1 : 0));
336
+ const a = this.scanner.peek(t - this.scanner.currentIndex - 1) === "\\", i = this.scanner.tail.slice(0, t - this.scanner.currentIndex + (a ? 1 : 0));
337
337
  return this.scanner.consume(i.length + 1), i;
338
338
  }
339
339
  /**
@@ -344,12 +344,12 @@ class O {
344
344
  const t = this.extractExpression().split("|");
345
345
  if (t.length > 0 && t[0]) {
346
346
  const r = t[0];
347
- let n = "Regular", i = 400;
348
- for (const c of t.slice(1))
349
- c.startsWith("b1") ? i = 700 : c.startsWith("i1") && (n = "Italic");
347
+ let a = "Regular", i = 400;
348
+ for (const h of t.slice(1))
349
+ h.startsWith("b1") ? i = 700 : h.startsWith("i1") && (a = "Italic");
350
350
  e.fontFace = {
351
351
  family: r,
352
- style: n,
352
+ style: a,
353
353
  weight: i
354
354
  };
355
355
  }
@@ -360,56 +360,56 @@ class O {
360
360
  * @param ctx - The context to update
361
361
  */
362
362
  parseParagraphProperties(e) {
363
- const t = new p(this.extractExpression());
364
- let r = e.paragraph.indent, n = e.paragraph.left, i = e.paragraph.right, c = e.paragraph.align, u = [];
365
- const a = () => {
366
- const o = t.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);
367
- if (o) {
368
- const h = parseFloat(o[0]);
369
- for (t.consume(o[0].length); t.peek() === ","; )
363
+ const t = new f(this.extractExpression());
364
+ let r = e.paragraph.indent, a = e.paragraph.left, i = e.paragraph.right, h = e.paragraph.align, u = [];
365
+ const n = () => {
366
+ const l = t.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);
367
+ if (l) {
368
+ const o = parseFloat(l[0]);
369
+ for (t.consume(l[0].length); t.peek() === ","; )
370
370
  t.consume(1);
371
- return h;
371
+ return o;
372
372
  }
373
373
  return 0;
374
374
  };
375
375
  for (; t.hasData; )
376
376
  switch (t.get()) {
377
377
  case "i":
378
- r = a();
378
+ r = n();
379
379
  break;
380
380
  case "l":
381
- n = a();
381
+ a = n();
382
382
  break;
383
383
  case "r":
384
- i = a();
384
+ i = n();
385
385
  break;
386
386
  case "x":
387
387
  break;
388
388
  case "q": {
389
- const h = t.get();
390
- for (c = _[h] || 0; t.peek() === ","; )
389
+ const o = t.get();
390
+ for (h = _[o] || 0; t.peek() === ","; )
391
391
  t.consume(1);
392
392
  break;
393
393
  }
394
394
  case "t":
395
395
  for (u = []; t.hasData; ) {
396
- const h = t.peek();
397
- if (h === "r" || h === "c") {
396
+ const o = t.peek();
397
+ if (o === "r" || o === "c") {
398
398
  t.consume(1);
399
- const l = a();
400
- u.push(h + l.toString());
399
+ const c = n();
400
+ u.push(o + c.toString());
401
401
  } else {
402
- const l = a();
403
- isNaN(l) ? t.consume(1) : u.push(l);
402
+ const c = n();
403
+ isNaN(c) ? t.consume(1) : u.push(c);
404
404
  }
405
405
  }
406
406
  break;
407
407
  }
408
408
  e.paragraph = {
409
409
  indent: r,
410
- left: n,
410
+ left: a,
411
411
  right: i,
412
- align: c,
412
+ align: h,
413
413
  tab_stops: u
414
414
  };
415
415
  }
@@ -425,29 +425,29 @@ class O {
425
425
  */
426
426
  *parse() {
427
427
  let r = null;
428
- const n = () => {
429
- var c;
428
+ const a = () => {
429
+ var h;
430
430
  let i = "";
431
431
  for (; this.scanner.hasData; ) {
432
- let u = !1, a = this.scanner.peek();
433
- const o = this.scanner.currentIndex;
434
- if (a.charCodeAt(0) < 32) {
435
- if (this.scanner.consume(1), a === " ")
432
+ let u = !1, n = this.scanner.peek();
433
+ const l = this.scanner.currentIndex;
434
+ if (n.charCodeAt(0) < 32) {
435
+ if (this.scanner.consume(1), n === " ")
436
436
  return [5, null];
437
- if (a === `
437
+ if (n === `
438
438
  `)
439
439
  return [6, null];
440
- a = " ";
440
+ n = " ";
441
441
  }
442
- if (a === "\\")
442
+ if (n === "\\")
443
443
  if ("\\{}".includes(this.scanner.peek(1)))
444
- u = !0, this.scanner.consume(1), a = this.scanner.peek();
444
+ u = !0, this.scanner.consume(1), n = this.scanner.peek();
445
445
  else {
446
446
  if (i)
447
447
  return [1, i];
448
448
  this.scanner.consume(1);
449
- const h = this.scanner.get();
450
- switch (h) {
449
+ const o = this.scanner.get();
450
+ switch (o) {
451
451
  case "~":
452
452
  return [4, null];
453
453
  case "P":
@@ -456,78 +456,99 @@ class O {
456
456
  return [7, null];
457
457
  case "X":
458
458
  return [8, null];
459
- case "S":
460
- return this.parseStacking();
459
+ case "S": {
460
+ this.inStackContext = !0;
461
+ const c = this.parseStacking();
462
+ return this.inStackContext = !1, c;
463
+ }
461
464
  case "m":
462
465
  case "M":
463
466
  if (this.scanner.peek() === "+") {
464
467
  this.scanner.consume(1);
465
- const l = (c = this.scanner.tail.match(/^[0-9A-Fa-f]{4}/)) == null ? void 0 : c[0];
466
- if (l) {
468
+ const c = (h = this.scanner.tail.match(/^[0-9A-Fa-f]{4}/)) == null ? void 0 : h[0];
469
+ if (c) {
467
470
  this.scanner.consume(4);
468
- const d = this.decodeMultiByteChar(l);
469
- return i ? [1, i] : [1, d];
471
+ const p = this.decodeMultiByteChar(c);
472
+ return i ? [1, i] : [1, p];
470
473
  }
471
474
  this.scanner.consume(-1);
472
475
  }
473
476
  i += "\\M";
474
477
  continue;
475
478
  default:
476
- if (h)
479
+ if (o)
477
480
  try {
478
- const l = this.parseProperties(h);
479
- if (this.yieldPropertyCommands && l)
480
- return this.lastCtx = this.ctx.copy(), [9, l];
481
+ const c = this.parseProperties(o);
482
+ if (this.yieldPropertyCommands && c)
483
+ return this.lastCtx = this.ctx.copy(), [9, c];
481
484
  continue;
482
485
  } catch {
483
- const l = this.scanner.tail.slice(
484
- o,
486
+ const c = this.scanner.tail.slice(
487
+ l,
485
488
  this.scanner.currentIndex
486
489
  );
487
- i += l;
490
+ i += c;
488
491
  }
489
492
  }
490
493
  continue;
491
494
  }
492
- if (a === "%" && this.scanner.peek(1) === "%") {
493
- const h = this.scanner.peek(2).toLowerCase(), l = m[h];
494
- if (l) {
495
- this.scanner.consume(3), i += l;
495
+ if (n === "%" && this.scanner.peek(1) === "%") {
496
+ const o = this.scanner.peek(2).toLowerCase(), c = m[o];
497
+ if (c) {
498
+ this.scanner.consume(3), i += c;
496
499
  continue;
497
500
  } else {
498
501
  this.scanner.consume(3);
499
502
  continue;
500
503
  }
501
504
  }
502
- if (a === " ")
505
+ if (n === " ")
503
506
  return i ? (this.scanner.consume(1), r = 3, [1, i]) : (this.scanner.consume(1), [3, null]);
504
507
  if (!u) {
505
- if (a === "{") {
508
+ if (n === "{") {
506
509
  if (i)
507
510
  return [1, i];
508
511
  this.scanner.consume(1), this.pushCtx();
509
512
  continue;
510
- } else if (a === "}") {
513
+ } else if (n === "}") {
511
514
  if (i)
512
515
  return [1, i];
513
516
  this.scanner.consume(1), this.popCtx();
514
517
  continue;
515
518
  }
516
519
  }
517
- this.scanner.consume(1), a.charCodeAt(0) >= 32 && (i += a);
520
+ if (!this.inStackContext && n === "^") {
521
+ const o = this.scanner.peek(1);
522
+ if (o) {
523
+ const c = o.charCodeAt(0);
524
+ if (this.scanner.consume(2), c === 32)
525
+ i += "^";
526
+ else {
527
+ if (c === 73)
528
+ return i ? [1, i] : [5, null];
529
+ if (c === 74)
530
+ return i ? [1, i] : [6, null];
531
+ if (c === 77)
532
+ continue;
533
+ i += "▯";
534
+ }
535
+ continue;
536
+ }
537
+ }
538
+ this.scanner.consume(1), n.charCodeAt(0) >= 32 && (i += n);
518
539
  }
519
540
  return i ? [1, i] : [0, null];
520
541
  };
521
542
  for (; ; ) {
522
- const [i, c] = n();
543
+ const [i, h] = a();
523
544
  if (i)
524
- yield new f(i, this.ctx, c), r && (yield new f(r, this.ctx, null), r = null);
545
+ yield new d(i, this.ctx, h), r && (yield new d(r, this.ctx, null), r = null);
525
546
  else
526
547
  break;
527
548
  }
528
549
  }
529
550
  }
530
- class p {
551
+ class f {
531
552
  /**
532
553
  * Create a new text scanner
533
554
  * @param text - The text to scan
@@ -764,7 +785,7 @@ class g {
764
785
  return e._stroke = this._stroke, e.continueStroke = this.continueStroke, e._aci = this._aci, e.rgb = this.rgb, e.align = this.align, e.fontFace = { ...this.fontFace }, e._capHeight = { ...this._capHeight }, e._widthFactor = { ...this._widthFactor }, e._charTrackingFactor = { ...this._charTrackingFactor }, e.oblique = this.oblique, e.paragraph = { ...this.paragraph }, e;
765
786
  }
766
787
  }
767
- class f {
788
+ class d {
768
789
  /**
769
790
  * Create a new MText token
770
791
  * @param type - The token type
@@ -779,15 +800,14 @@ export {
779
800
  g as MTextContext,
780
801
  b as MTextLineAlignment,
781
802
  E as MTextParagraphAlignment,
782
- O as MTextParser,
803
+ N as MTextParser,
783
804
  F as MTextStroke,
784
- f as MTextToken,
785
- p as TextScanner,
805
+ d as MTextToken,
806
+ f as TextScanner,
786
807
  k as TokenType,
787
- S as caretDecode,
788
- I as escapeDxfLineEndings,
789
- N as hasInlineFormattingCodes,
790
- R as int2rgb,
791
- v as rgb2int
808
+ v as escapeDxfLineEndings,
809
+ I as hasInlineFormattingCodes,
810
+ S as int2rgb,
811
+ R as rgb2int
792
812
  };
793
813
  //# sourceMappingURL=parser.es.js.map