@mlightcad/mtext-parser 1.1.4 → 1.1.6

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__ */ ((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
- const F = {
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 = {
3
3
  c: "Ø",
4
4
  d: "°",
5
5
  p: "±"
6
- }, S = {
6
+ }, F = {
7
7
  l: 1,
8
8
  r: 2,
9
9
  c: 3,
@@ -11,28 +11,80 @@ const F = {
11
11
  d: 5
12
12
  /* DISTRIBUTED */
13
13
  };
14
- function R(s) {
14
+ function v(s) {
15
15
  const [t, e, r] = s;
16
16
  return r << 16 | e << 8 | t;
17
17
  }
18
- function v(s) {
18
+ function O(s) {
19
19
  const t = s & 255, e = s >> 8 & 255, r = s >> 16 & 255;
20
20
  return [t, e, r];
21
21
  }
22
- function O(s) {
22
+ function N(s) {
23
23
  return s.replace(/\r\n|\r|\n/g, "\\P");
24
24
  }
25
25
  function I(s) {
26
26
  return s.replace(/\\P/g, "").replace(/\\~/g, "").includes("\\");
27
27
  }
28
- function N(s, t = !1) {
28
+ function y(s, t = !1) {
29
29
  const e = /* @__PURE__ */ new Set(), r = /\\[fF](.*?)[;|]/g;
30
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 x {
35
+ class R {
36
+ /**
37
+ * Creates a new ContextStack with an initial context.
38
+ * @param initial The initial MTextContext to use as the base of the stack.
39
+ */
40
+ constructor(t) {
41
+ this.stack = [], this.stack.push(t);
42
+ }
43
+ /**
44
+ * Pushes a copy of the given context onto the stack.
45
+ * @param ctx The MTextContext to push (copied).
46
+ */
47
+ push(t) {
48
+ this.stack.push(t);
49
+ }
50
+ /**
51
+ * Pops the top context from the stack and merges its paragraph properties into the new top context.
52
+ * If only one context remains, nothing is popped.
53
+ * @returns The popped MTextContext, or undefined if the stack has only one context.
54
+ */
55
+ pop() {
56
+ if (this.stack.length <= 1) return;
57
+ const t = this.stack.pop(), e = this.stack[this.stack.length - 1];
58
+ return JSON.stringify(e.paragraph) !== JSON.stringify(t.paragraph) && (e.paragraph = { ...t.paragraph }), t;
59
+ }
60
+ /**
61
+ * Returns the current (top) context on the stack.
62
+ */
63
+ get current() {
64
+ return this.stack[this.stack.length - 1];
65
+ }
66
+ /**
67
+ * Returns the current stack depth (number of nested blocks), not counting the root context.
68
+ */
69
+ get depth() {
70
+ return this.stack.length - 1;
71
+ }
72
+ /**
73
+ * Returns the root (bottom) context, which represents the global formatting state.
74
+ * Used for paragraph property application.
75
+ */
76
+ get root() {
77
+ return this.stack[0];
78
+ }
79
+ /**
80
+ * Replaces the current (top) context with the given context.
81
+ * @param ctx The new context to set as the current context.
82
+ */
83
+ setCurrent(t) {
84
+ this.stack[this.stack.length - 1] = t;
85
+ }
86
+ }
87
+ class D {
36
88
  /**
37
89
  * Creates a new MTextParser instance
38
90
  * @param content - The MText content to parse
@@ -40,7 +92,9 @@ class x {
40
92
  * @param yieldPropertyCommands - Whether to yield property change commands
41
93
  */
42
94
  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;
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;
44
98
  }
45
99
  /**
46
100
  * Decode multi-byte character from hex code
@@ -65,13 +119,13 @@ class x {
65
119
  * Push current context onto the stack
66
120
  */
67
121
  pushCtx() {
68
- this.ctxStack.push(this.ctx);
122
+ this.ctxStack.push(this.ctxStack.current);
69
123
  }
70
124
  /**
71
125
  * Pop context from the stack
72
126
  */
73
127
  popCtx() {
74
- this.ctxStack.length > 0 && (this.ctx = this.ctxStack.pop());
128
+ this.ctxStack.pop();
75
129
  }
76
130
  /**
77
131
  * Parse stacking expression (numerator/denominator)
@@ -112,64 +166,64 @@ class x {
112
166
  * @returns Property changes if yieldPropertyCommands is true and changes occurred
113
167
  */
114
168
  parseProperties(t) {
115
- const e = this.ctx.copy();
169
+ const e = this.ctxStack.current.copy(), r = this.ctxStack.current.copy();
116
170
  switch (t) {
117
171
  case "L":
118
- e.underline = !0, this.continueStroke = !0;
172
+ r.underline = !0, this.continueStroke = !0;
119
173
  break;
120
174
  case "l":
121
- e.underline = !1, e.hasAnyStroke || (this.continueStroke = !1);
175
+ r.underline = !1, r.hasAnyStroke || (this.continueStroke = !1);
122
176
  break;
123
177
  case "O":
124
- e.overline = !0, this.continueStroke = !0;
178
+ r.overline = !0, this.continueStroke = !0;
125
179
  break;
126
180
  case "o":
127
- e.overline = !1, e.hasAnyStroke || (this.continueStroke = !1);
181
+ r.overline = !1, r.hasAnyStroke || (this.continueStroke = !1);
128
182
  break;
129
183
  case "K":
130
- e.strikeThrough = !0, this.continueStroke = !0;
184
+ r.strikeThrough = !0, this.continueStroke = !0;
131
185
  break;
132
186
  case "k":
133
- e.strikeThrough = !1, e.hasAnyStroke || (this.continueStroke = !1);
187
+ r.strikeThrough = !1, r.hasAnyStroke || (this.continueStroke = !1);
134
188
  break;
135
189
  case "A":
136
- this.parseAlign(e);
190
+ this.parseAlign(r);
137
191
  break;
138
192
  case "C":
139
- this.parseAciColor(e);
193
+ this.parseAciColor(r);
140
194
  break;
141
195
  case "c":
142
- this.parseRgbColor(e);
196
+ this.parseRgbColor(r);
143
197
  break;
144
198
  case "H":
145
- this.parseHeight(e);
199
+ this.parseHeight(r);
146
200
  break;
147
201
  case "W":
148
- this.parseWidth(e);
202
+ this.parseWidth(r);
149
203
  break;
150
204
  case "Q":
151
- this.parseOblique(e);
205
+ this.parseOblique(r);
152
206
  break;
153
207
  case "T":
154
- this.parseCharTracking(e);
208
+ this.parseCharTracking(r);
155
209
  break;
156
210
  case "p":
157
- this.parseParagraphProperties(e);
211
+ this.parseParagraphProperties(r);
158
212
  break;
159
213
  case "f":
160
214
  case "F":
161
- this.parseFontProperties(e);
215
+ this.parseFontProperties(r);
162
216
  break;
163
217
  default:
164
218
  throw new Error(`Unknown command: ${t}`);
165
219
  }
166
- if (this.continueStroke = e.hasAnyStroke, e.continueStroke = this.continueStroke, this.ctx = e, this.yieldPropertyCommands) {
167
- const r = this.getPropertyChanges(this.lastCtx, e);
168
- if (Object.keys(r).length > 0)
169
- return this.lastCtx = this.ctx.copy(), {
220
+ if (this.continueStroke = r.hasAnyStroke, r.continueStroke = this.continueStroke, this.ctxStack.setCurrent(r), this.yieldPropertyCommands) {
221
+ const a = this.getPropertyChanges(e, r);
222
+ if (Object.keys(a).length > 0)
223
+ return {
170
224
  command: t,
171
- changes: r,
172
- depth: this.ctxStack.length
225
+ changes: a,
226
+ depth: this.ctxStack.depth
173
227
  };
174
228
  }
175
229
  }
@@ -395,7 +449,7 @@ class x {
395
449
  break;
396
450
  case "q": {
397
451
  const o = e.get();
398
- for (h = S[o] || 0; e.peek() === ","; )
452
+ for (h = F[o] || 0; e.peek() === ","; )
399
453
  e.consume(1);
400
454
  break;
401
455
  }
@@ -488,7 +542,7 @@ class x {
488
542
  try {
489
543
  const c = this.parseProperties(o);
490
544
  if (this.yieldPropertyCommands && c)
491
- return this.lastCtx = this.ctx.copy(), [9, c];
545
+ return [9, c];
492
546
  continue;
493
547
  } catch {
494
548
  const c = this.scanner.tail.slice(
@@ -501,7 +555,7 @@ class x {
501
555
  continue;
502
556
  }
503
557
  if (n === "%" && this.scanner.peek(1) === "%") {
504
- const o = this.scanner.peek(2).toLowerCase(), c = F[o];
558
+ const o = this.scanner.peek(2).toLowerCase(), c = E[o];
505
559
  if (c) {
506
560
  this.scanner.consume(3), i += c;
507
561
  continue;
@@ -522,12 +576,13 @@ class x {
522
576
  if (i)
523
577
  return [1, i];
524
578
  if (this.scanner.consume(1), this.yieldPropertyCommands) {
579
+ const o = this.ctxStack.current;
525
580
  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(), [
581
+ const c = this.getPropertyChanges(o, this.ctxStack.current);
582
+ if (Object.keys(c).length > 0)
583
+ return [
529
584
  9,
530
- { command: void 0, changes: o, depth: this.ctxStack.length }
585
+ { command: void 0, changes: c, depth: this.ctxStack.depth }
531
586
  ];
532
587
  } else
533
588
  this.popCtx();
@@ -559,7 +614,7 @@ class x {
559
614
  for (; ; ) {
560
615
  const [i, h] = a();
561
616
  if (i)
562
- yield new b(i, this.ctx, h), r && (yield new b(r, this.ctx, null), r = null);
617
+ yield new k(i, this.ctxStack.current.copy(), h), r && (yield new k(r, this.ctxStack.current.copy(), null), r = null);
563
618
  else
564
619
  break;
565
620
  }
@@ -906,7 +961,7 @@ class d {
906
961
  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;
907
962
  }
908
963
  }
909
- class b {
964
+ class k {
910
965
  /**
911
966
  * Create a new MText token
912
967
  * @param type - The token type
@@ -922,15 +977,15 @@ export {
922
977
  d as MTextContext,
923
978
  _ as MTextLineAlignment,
924
979
  m as MTextParagraphAlignment,
925
- x as MTextParser,
926
- E as MTextStroke,
927
- b as MTextToken,
980
+ D as MTextParser,
981
+ S as MTextStroke,
982
+ k as MTextToken,
928
983
  g as TextScanner,
929
- k as TokenType,
930
- O as escapeDxfLineEndings,
931
- N as getFonts,
984
+ b as TokenType,
985
+ N as escapeDxfLineEndings,
986
+ y as getFonts,
932
987
  I as hasInlineFormattingCodes,
933
- v as int2rgb,
934
- R as rgb2int
988
+ O as int2rgb,
989
+ v as rgb2int
935
990
  };
936
991
  //# sourceMappingURL=parser.es.js.map