-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
159 lines (159 loc) · 5.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// https://adndevblog.typepad.com/autocad/2017/09/dissecting-mtext-format-codes.html
// https://www.cadforum.cz/cadforum_en/text-formatting-codes-in-mtext-objects-tip8640
const dxfTextControlCodeSymbolMap = {
d: '°',
c: '⌀',
p: '±',
};
export const parseDxfMTextContent = (s, options) => {
s = s.replace(/%%(.)/g, (_, c) => dxfTextControlCodeSymbolMap[c] || c);
const encoding = options?.encoding;
let decoder = encoding instanceof TextDecoder ? encoding : undefined;
let currentText = '';
let c;
const contents = [];
const pushContent = (content) => {
if (currentText) {
contents.push(currentText);
currentText = '';
}
contents.push(content);
};
for (let i = 0; i < s.length; i++) {
switch (c = s[i]) {
default:
currentText += c;
break;
case '\\': {
switch (c = s[++i]) {
default:
currentText += c;
break;
case 'P':
currentText += '\n';
break;
case 'f':
case 'F': {
let f = '';
while (c = s[++i]) {
if (c === ';') {
pushContent({ f });
break;
}
if (c === '|') {
const content = { f };
const end = s.indexOf(';', ++i);
for (const element of s.slice(i, end).split('|')) {
content[element[0]] = +element.slice(1);
}
i = end;
pushContent(content);
break;
}
f += c === '\\' ? s[++i] : c;
}
break;
}
case 'S': {
let upper = '';
let op;
let lower = '';
while (c = s[++i]) {
if (c === ';') {
op && pushContent({ S: [upper, op, lower] });
break;
}
if (c === '\\') {
op ? lower += s[++i] : upper += s[++i];
}
else if (op) {
lower += c;
}
else if (c === '^' || c === '/' || c === '#') {
op = c;
}
else {
upper += c;
}
}
break;
}
case 'H':
case 'W':
const start = ++i;
const [, value, unit] = s.slice(start, i = s.indexOf(';', i)).match(/^(\d*(?:\.\d+)?)(\D*)$/);
pushContent({ [c]: [+value, unit] });
break;
case 'Q':
case 'A':
case 'C':
case 'T': {
const start = ++i;
pushContent({ [c]: +s.slice(start, i = s.indexOf(';', i)) });
break;
}
case 'L':
case 'O':
case 'K':
pushContent({ [c]: 1 });
break;
case 'l':
case 'o':
case 'k':
pushContent({ [c.toUpperCase()]: 0 });
break;
case 'U':
case 'u':
if (s[i + 1] === '+') {
currentText += String.fromCodePoint(parseInt(s.substr(i + 2, 4), 16));
i += 5;
}
else {
currentText += c;
}
break;
case 'M':
case 'm':
if (encoding) {
if (s[i + 1] === '+' && s[i + 2] === '1') {
currentText += (decoder = decoder || new TextDecoder(encoding)).decode(new Uint8Array([
parseInt(s.substr(i + 3, 2), 16),
parseInt(s.substr(i + 5, 2), 16),
]));
i += 6;
}
else {
currentText += c;
}
}
else {
currentText += '\\' + c;
}
break;
}
break;
}
case '{': {
let depth = 1;
const start = i;
while (c = s[++i]) {
if (c === '{') {
depth++;
}
else if (c === '}') {
if (--depth === 0) {
pushContent(parseDxfMTextContent(s.slice(start + 1, i)));
break;
}
}
else if (c === '\\') {
i++;
}
}
break;
}
}
}
currentText && contents.push(currentText);
return contents;
};