-
Notifications
You must be signed in to change notification settings - Fork 39
/
rtf-document.js
73 lines (71 loc) · 1.85 KB
/
rtf-document.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
'use strict'
const RTFGroup = require('./rtf-group.js')
const RTFParagraph = require('./rtf-paragraph.js')
class RTFDocument extends RTFGroup {
constructor () {
super()
this.charset = 'ASCII'
this.ignorable = false
this.marginLeft = 1800
this.marginRight = 1800
this.marginBottom = 1440
this.marginTop = 1440
this.style = {
font: 0,
fontSize: 24,
bold: false,
italic: false,
underline: false,
strikethrough: false,
foreground: null,
background: null,
firstLineIndent: 0,
indent: 0,
align: 'left',
valign: 'normal'
}
}
get (name) {
return this[name]
}
getFont (num) {
return this.fonts[num]
}
getColor (num) {
return this.colors[num]
}
getStyle (name) {
if (!name) return this.style
return this.style[name]
}
addContent (node) {
if (node instanceof RTFParagraph) {
while (this.content.length && !(this.content[this.content.length - 1] instanceof RTFParagraph)) {
node.content.unshift(this.content.pop())
}
super.addContent(node)
if (node.content.length) {
const initialStyle = node.content[0].style
const style = {}
style.font = this.getFont(initialStyle.font)
style.foreground = this.getColor(initialStyle.foreground)
style.background = this.getColor(initialStyle.background)
for (let prop of Object.keys(initialStyle)) {
if (initialStyle[prop] == null) continue
let match = true
for (let span of node.content) {
if (initialStyle[prop] !== span.style[prop]) {
match = false
break
}
}
if (match) style[prop] = initialStyle[prop]
}
node.style = style
}
} else {
super.addContent(node)
}
}
}
module.exports = RTFDocument