-
Notifications
You must be signed in to change notification settings - Fork 39
/
rtf-interpreter.js
422 lines (402 loc) · 10.8 KB
/
rtf-interpreter.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
'use strict'
const assert = require('assert')
const util = require('util')
const Writable = require('readable-stream').Writable
const RTFGroup = require('./rtf-group.js')
const RTFParagraph = require('./rtf-paragraph.js')
const RTFSpan = require('./rtf-span.js')
const iconv = require('iconv-lite')
const availableCP = [
437, 737, 775, 850, 852, 853, 855, 857, 858, 860, 861, 863, 865, 866,
869, 932, 936, 949, 950, 1125, 1250, 1251, 1252, 1253, 1254, 1257 ]
const codeToCP = {
0: 'ASCII',
2: 'SYMBOL',
77: 'MacRoman',
128: 'SHIFT_JIS',
129: 'CP949', // Hangul
130: 'JOHAB',
134: 'CP936', // GB2312 simplified chinese
136: 'BIG5',
161: 'CP1253', // greek
162: 'CP1254', // turkish
163: 'CP1258', // vietnamese
177: 'CP862', // hebrew
178: 'CP1256', // arabic
186: 'CP1257', // baltic
204: 'CP1251', // russian
222: 'CP874', // thai
238: 'CP238', // eastern european
254: 'CP437' // PC-437
}
class RTFInterpreter extends Writable {
constructor (document) {
super({objectMode: true})
this.doc = document
this.parserState = this.parseTop
this.groupStack = []
this.group = null
this.once('prefinish', () => this.finisher())
this.hexStore = []
}
_write (cmd, encoding, done) {
const method = 'cmd$' + cmd.type.replace(/-(.)/g, (_, char) => char.toUpperCase())
if (this[method]) {
this[method](cmd)
} else {
process.emit('error', `Unknown RTF command ${cmd.type}, tried ${method}`)
}
done()
}
finisher () {
while (this.groupStack.length) this.cmd$groupEnd()
const initialStyle = this.doc.content.length ? this.doc.content[0].style : []
for (let prop of Object.keys(this.doc.style)) {
let match = true
for (let para of this.doc.content) {
if (initialStyle[prop] !== para.style[prop]) {
match = false
break
}
}
if (match) this.doc.style[prop] = initialStyle[prop]
}
}
flushHexStore () {
if (this.hexStore.length > 0) {
let hexstr = this.hexStore.map(cmd => cmd.value).join('')
this.group.addContent(new RTFSpan({
value: iconv.decode(
Buffer.from(hexstr, 'hex'), this.group.get('charset'))
}))
this.hexStore.splice(0)
}
}
cmd$groupStart () {
this.flushHexStore()
if (this.group) this.groupStack.push(this.group)
this.group = new RTFGroup(this.group || this.doc)
}
cmd$ignorable () {
this.flushHexStore()
this.group.ignorable = true
}
cmd$endParagraph () {
this.flushHexStore()
this.group.addContent(new RTFParagraph())
}
cmd$groupEnd () {
this.flushHexStore()
const endingGroup = this.group
this.group = this.groupStack.pop()
const doc = this.group || this.doc
if (endingGroup instanceof FontTable) {
doc.fonts = endingGroup.table
} else if (endingGroup instanceof ColorTable) {
doc.colors = endingGroup.table
} else if (endingGroup !== this.doc && !endingGroup.get('ignorable')) {
for (const item of endingGroup.content) {
doc.addContent(item)
}
process.emit('debug', 'GROUP END', endingGroup.type, endingGroup.get('ignorable'))
}
}
cmd$text (cmd) {
this.flushHexStore()
if (!this.group) { // an RTF fragment, missing the {\rtf1 header
this.group = this.doc
}
this.group.addContent(new RTFSpan(cmd))
}
cmd$controlWord (cmd) {
this.flushHexStore()
if (!this.group.type) this.group.type = cmd.value
const method = 'ctrl$' + cmd.value.replace(/-(.)/g, (_, char) => char.toUpperCase())
if (this[method]) {
this[method](cmd.param)
} else {
if (!this.group.get('ignorable')) process.emit('debug', method, cmd.param)
}
}
cmd$hexchar (cmd) {
this.hexStore.push(cmd)
}
cmd$error (cmd) {
this.emit('error', new Error('Error: ' + cmd.value + (cmd.row && cmd.col ? ' at line ' + cmd.row + ':' + cmd.col : '') + '.'))
}
ctrl$rtf () {
this.group = this.doc
}
// new line
ctrl$line () {
this.group.addContent(new RTFSpan({ value: '\n' }))
}
// tab
ctrl$tab () {
this.group.addContent(new RTFSpan({ value: '\t' }))
}
// alignment
ctrl$qc () {
this.group.style.align = 'center'
}
ctrl$qj () {
this.group.style.align = 'justify'
}
ctrl$ql () {
this.group.style.align = 'left'
}
ctrl$qr () {
this.group.style.align = 'right'
}
// text direction
ctrl$rtlch () {
this.group.style.dir = 'rtl'
}
ctrl$ltrch () {
this.group.style.dir = 'ltr'
}
// general style
ctrl$par () {
this.group.addContent(new RTFParagraph())
}
ctrl$pard () {
this.group.resetStyle()
}
ctrl$plain () {
this.group.style.fontSize = this.doc.getStyle('fontSize')
this.group.style.bold = this.doc.getStyle('bold')
this.group.style.italic = this.doc.getStyle('italic')
this.group.style.underline = this.doc.getStyle('underline')
}
ctrl$b (set) {
this.group.style.bold = set !== 0
}
ctrl$i (set) {
this.group.style.italic = set !== 0
}
ctrl$u (num) {
var charBuf = Buffer.alloc ? Buffer.alloc(2) : new Buffer(2)
// RTF, for reasons, represents unicode characters as signed integers
// thus managing to match literally no one.
charBuf.writeInt16LE(num, 0)
this.group.addContent(new RTFSpan({value: iconv.decode(charBuf, 'ucs2')}))
}
ctrl$super () {
this.group.style.valign = 'super'
}
ctrl$sub () {
this.group.style.valign = 'sub'
}
ctrl$nosupersub () {
this.group.style.valign = 'normal'
}
ctrl$strike (set) {
this.group.style.strikethrough = set !== 0
}
ctrl$ul (set) {
this.group.style.underline = set !== 0
}
ctrl$ulnone (set) {
this.group.style.underline = false
}
ctrl$fi (value) {
this.group.style.firstLineIndent = value
}
ctrl$cufi (value) {
this.group.style.firstLineIndent = value * 100
}
ctrl$li (value) {
this.group.style.indent = value
}
ctrl$lin (value) {
this.group.style.indent = value
}
ctrl$culi (value) {
this.group.style.indent = value * 100
}
// encodings
ctrl$ansi () {
this.group.charset = 'ASCII'
}
ctrl$mac () {
this.group.charset = 'MacRoman'
}
ctrl$pc () {
this.group.charset = 'CP437'
}
ctrl$pca () {
this.group.charset = 'CP850'
}
ctrl$ansicpg (codepage) {
if (availableCP.indexOf(codepage) === -1) {
this.emit('error', new Error('Codepage ' + codepage + ' is not available.'))
} else {
this.group.charset = 'CP' + codepage
}
}
// fonts
ctrl$fonttbl () {
this.group = new FontTable(this.group.parent)
}
ctrl$f (num) {
if (this.group instanceof FontTable) {
this.group.currentFont = this.group.table[num] = new Font()
} else if (this.group.parent instanceof FontTable) {
this.group.parent.currentFont = this.group.parent.table[num] = new Font()
} else {
this.group.style.font = num
let fontCharset = this.group.getFont(num).charset
fontCharset = fontCharset && fontCharset !== 'ASCII' ? fontCharset : this.group.charset// default font charset
this.group.charset = fontCharset
}
}
ctrl$fnil () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'nil'
}
}
ctrl$froman () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'roman'
}
}
ctrl$fswiss () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'swiss'
}
}
ctrl$fmodern () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'modern'
}
}
ctrl$fscript () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'script'
}
}
ctrl$fdecor () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'decor'
}
}
ctrl$ftech () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'tech'
}
}
ctrl$fbidi () {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').family = 'bidi'
}
}
ctrl$fcharset (code) {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
let charset = null
if (code === 1) {
charset = this.group.get('charset')
} else {
charset = codeToCP[code]
}
if (charset == null) {
return this.emit('error', new Error('Unsupported charset code #' + code))
}
this.group.get('currentFont').charset = charset
}
}
ctrl$fprq (pitch) {
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) {
this.group.get('currentFont').pitch = pitch
}
}
// colors
ctrl$colortbl () {
this.group = new ColorTable(this.group.parent)
}
ctrl$red (value) {
if (this.group instanceof ColorTable) {
this.group.red = value
}
}
ctrl$blue (value) {
if (this.group instanceof ColorTable) {
this.group.blue = value
}
}
ctrl$green (value) {
if (this.group instanceof ColorTable) {
this.group.green = value
}
}
ctrl$cf (value) {
this.group.style.foreground = value
}
ctrl$cb (value) {
this.group.style.background = value
}
ctrl$fs (value) {
this.group.style.fontSize = value
}
// margins
ctrl$margl (value) {
this.doc.marginLeft = value
}
ctrl$margr (value) {
this.doc.marginRight = value
}
ctrl$margt (value) {
this.doc.marginTop = value
}
ctrl$margb (value) {
this.doc.marginBottom = value
}
// unsupported (and we need to ignore content)
ctrl$stylesheet (value) {
this.group.ignorable = true
}
ctrl$info (value) {
this.group.ignorable = true
}
ctrl$mmathPr (value) {
this.group.ignorable = true
}
}
class FontTable extends RTFGroup {
constructor (parent) {
super(parent)
this.table = []
this.currentFont = {family: 'roman', charset: 'ASCII', name: 'Serif'}
}
addContent (text) {
this.currentFont.name += text.value.replace(/;\s*$/, '')
}
}
class Font {
constructor () {
this.family = null
this.charset = null
this.name = ''
this.pitch = 0
}
}
class ColorTable extends RTFGroup {
constructor (parent) {
super(parent)
this.table = []
this.red = 0
this.blue = 0
this.green = 0
}
addContent (text) {
assert(text.value === ';', 'got: ' + util.inspect(text))
this.table.push({
red: this.red,
blue: this.blue,
green: this.green
})
this.red = 0
this.blue = 0
this.green = 0
}
}
module.exports = RTFInterpreter