@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.cjs.js +2 -2
- package/dist/parser.cjs.js.map +1 -1
- package/dist/parser.es.js +105 -50
- 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 +0 -2
- package/package.json +1 -1
package/dist/parser.es.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
const
|
|
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
|
-
},
|
|
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
|
|
14
|
+
function v(s) {
|
|
15
15
|
const [t, e, r] = s;
|
|
16
16
|
return r << 16 | e << 8 | t;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
169
|
+
const e = this.ctxStack.current.copy(), r = this.ctxStack.current.copy();
|
|
116
170
|
switch (t) {
|
|
117
171
|
case "L":
|
|
118
|
-
|
|
172
|
+
r.underline = !0, this.continueStroke = !0;
|
|
119
173
|
break;
|
|
120
174
|
case "l":
|
|
121
|
-
|
|
175
|
+
r.underline = !1, r.hasAnyStroke || (this.continueStroke = !1);
|
|
122
176
|
break;
|
|
123
177
|
case "O":
|
|
124
|
-
|
|
178
|
+
r.overline = !0, this.continueStroke = !0;
|
|
125
179
|
break;
|
|
126
180
|
case "o":
|
|
127
|
-
|
|
181
|
+
r.overline = !1, r.hasAnyStroke || (this.continueStroke = !1);
|
|
128
182
|
break;
|
|
129
183
|
case "K":
|
|
130
|
-
|
|
184
|
+
r.strikeThrough = !0, this.continueStroke = !0;
|
|
131
185
|
break;
|
|
132
186
|
case "k":
|
|
133
|
-
|
|
187
|
+
r.strikeThrough = !1, r.hasAnyStroke || (this.continueStroke = !1);
|
|
134
188
|
break;
|
|
135
189
|
case "A":
|
|
136
|
-
this.parseAlign(
|
|
190
|
+
this.parseAlign(r);
|
|
137
191
|
break;
|
|
138
192
|
case "C":
|
|
139
|
-
this.parseAciColor(
|
|
193
|
+
this.parseAciColor(r);
|
|
140
194
|
break;
|
|
141
195
|
case "c":
|
|
142
|
-
this.parseRgbColor(
|
|
196
|
+
this.parseRgbColor(r);
|
|
143
197
|
break;
|
|
144
198
|
case "H":
|
|
145
|
-
this.parseHeight(
|
|
199
|
+
this.parseHeight(r);
|
|
146
200
|
break;
|
|
147
201
|
case "W":
|
|
148
|
-
this.parseWidth(
|
|
202
|
+
this.parseWidth(r);
|
|
149
203
|
break;
|
|
150
204
|
case "Q":
|
|
151
|
-
this.parseOblique(
|
|
205
|
+
this.parseOblique(r);
|
|
152
206
|
break;
|
|
153
207
|
case "T":
|
|
154
|
-
this.parseCharTracking(
|
|
208
|
+
this.parseCharTracking(r);
|
|
155
209
|
break;
|
|
156
210
|
case "p":
|
|
157
|
-
this.parseParagraphProperties(
|
|
211
|
+
this.parseParagraphProperties(r);
|
|
158
212
|
break;
|
|
159
213
|
case "f":
|
|
160
214
|
case "F":
|
|
161
|
-
this.parseFontProperties(
|
|
215
|
+
this.parseFontProperties(r);
|
|
162
216
|
break;
|
|
163
217
|
default:
|
|
164
218
|
throw new Error(`Unknown command: ${t}`);
|
|
165
219
|
}
|
|
166
|
-
if (this.continueStroke =
|
|
167
|
-
const
|
|
168
|
-
if (Object.keys(
|
|
169
|
-
return
|
|
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:
|
|
172
|
-
depth: this.ctxStack.
|
|
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 =
|
|
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
|
|
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 =
|
|
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
|
|
527
|
-
if (Object.keys(
|
|
528
|
-
return
|
|
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:
|
|
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
|
|
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
|
|
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
|
-
|
|
926
|
-
|
|
927
|
-
|
|
980
|
+
D as MTextParser,
|
|
981
|
+
S as MTextStroke,
|
|
982
|
+
k as MTextToken,
|
|
928
983
|
g as TextScanner,
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
984
|
+
b as TokenType,
|
|
985
|
+
N as escapeDxfLineEndings,
|
|
986
|
+
y as getFonts,
|
|
932
987
|
I as hasInlineFormattingCodes,
|
|
933
|
-
|
|
934
|
-
|
|
988
|
+
O as int2rgb,
|
|
989
|
+
v as rgb2int
|
|
935
990
|
};
|
|
936
991
|
//# sourceMappingURL=parser.es.js.map
|