-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructure-components.go
271 lines (216 loc) · 5.27 KB
/
structure-components.go
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
package rtfconverter
import (
"fmt"
"strings"
"strconv"
)
type rtfElement interface {
setParent(p *rtfGroup)
GetParent() (*rtfGroup)
Dump(level int)
}
/**
* RTF Groups
*/
type rtfGroup struct {
children []rtfElement
parent *rtfGroup
}
func (r *rtfGroup) addChild(c rtfElement) {
c.setParent(r)
r.children = append(r.children, c)
}
func (r *rtfGroup) GetChildren() []rtfElement{
return r.children
}
func (r *rtfGroup) setParent(p *rtfGroup) {
r.parent = p
}
func (r *rtfGroup) GetParent()(*rtfGroup) {
return r.parent
}
func (r *rtfGroup) IsDestination() bool {
return r.CheckChildAtIndex(0, "*")
}
func (r *rtfGroup) IsRtfGroup() bool {
return r.CheckChildAtIndex(0, "rtf")
}
/**
* check if the group define the font table (first child must be fonttbl)
*/
func (r *rtfGroup) IsFontTable() bool {
return r.CheckChildAtIndex(0, "fonttbl")
}
/**
* check if the group define the stylesheet
*/
func (r *rtfGroup) IsStylesheet() bool {
return r.CheckChildAtIndex(0, "stylesheet")
}
/**
* check if the group define the listtables
*/
func (r *rtfGroup) IsListtables() bool {
return r.CheckChildAtIndex(0, "listtables")
}
/**
* check if info group - document info are
*/
func (r *rtfGroup) IsInfo() bool {
return r.CheckChildAtIndex(0, "info")
}
/**
* check if the group define the files table
*/
func (r *rtfGroup) IsFilesTable() bool {
return (r.IsDestination() && r.CheckChildAtIndex(1, "filetbl"))
}
/**
* check if the group define the revtbl
*/
func (r *rtfGroup) IsTrackChanges() bool {
return (r.IsDestination() && r.CheckChildAtIndex(1, "revtbl")) || r.CheckChildAtIndex(0, "revtbl")
}
/**
* check if the group define the font table (first child must be colortbl)
*/
func (r *rtfGroup) IsColorTable() bool {
return r.CheckChildAtIndex(0, "colortbl")
}
/**
* check if is a fontinfo entry from font table group
* <fontnum><fontfamily><fcharset>?<fprq>?<panose>?<nontaggedname>?<fontemb>?<codepage>? <fontname><fontaltname>? ';'
* eq:
* {\f0\fswiss\fcharset0 Arial;}
*/
func (r *rtfGroup) IsFontInfo() bool {
return r.CheckChildAtIndex(0, "f")
}
/**
* check if the group is an alternative name
* {\*\falt xxxx}
*/
func (r *rtfGroup) IsFontAlternative() bool {
return r.IsDestination() && r.CheckChildAtIndex(1, "falt")
}
func (r *rtfGroup) CheckChildAtIndex(idx int, checkWord string) (bool) {
if idx < len(r.children) {
child := r.children[idx]
// First child not a control symbol?
switch child.(type) {
case *rtfControlSymbol:
return child.(*rtfControlSymbol).symbol == checkWord
case *rtfControlWord:
return child.(*rtfControlWord).word == checkWord
}
}
return false;
}
func (r *rtfGroup) Dump(level int) {
fmt.Printf("%sGroup (Children: %d)\r\n", strings.Repeat(" ", level), len(r.children));
if len(r.children) > 0 {
for _, child := range(r.children) {
child.Dump(level+1);
}
}
}
/**
* RTF Word Control
* word property will saved the control word without \
* parameter may contains negative numbers
*
* eg: \rtf1
* word: rtf
* parameter: 1
*
*/
type rtfControlWord struct {
word string
parameter string
parent *rtfGroup
}
func (r *rtfControlWord) setParent(p *rtfGroup) {
r.parent = p
}
func (r *rtfControlWord) GetParent()(*rtfGroup) {
return r.parent
}
func (r *rtfControlWord) GetWord()(string) {
return r.word
}
/**
* parameter should be an integer; I return it as a string so I can make the difference between 0 and empty
* @param {[type]} r *rtfControlWord) GetParameter() (string [description]
* @return {[type]} [description]
*/
func (r *rtfControlWord) GetParameter() (string) {
return r.parameter
}
/**
* for control words the default parameter is 1 and is integer
*/
func (r *rtfControlWord) GetIntParameter() (int) {
if (r.parameter == "") {
return 1
}
p,err := strconv.Atoi(r.parameter)
if err == nil {
return p
}
return 1
}
func (r *rtfControlWord) Dump(level int) {
fmt.Printf("%sControl Word (Word: \\%s%v)\r\n", strings.Repeat(" ", level), r.word, r.parameter);
}
/**
* symbols control
* parameter is used only for \'HH control symbol where HH are hexadecimal digits
* in the symbol property the value will be saved without the \
* eg: \'HH =>
* symbol: '
* parameter: HH
*/
type rtfControlSymbol struct {
symbol string
parameter string
parent *rtfGroup
}
func (r *rtfControlSymbol) GetSymbol()(string) {
return r.symbol
}
func (r *rtfControlSymbol) setParent(p *rtfGroup) {
r.parent = p
}
func (r *rtfControlSymbol) GetParent()(*rtfGroup) {
return r.parent
}
/**
* usually the parameter is empty, except when the control symbol is \'
* @param {[type]} r *rtfControlSymbol) GetParameter() (string [description]
* @return {[type]} [description]
*/
func (r *rtfControlSymbol) GetParameter() (string) {
return r.parameter
}
func (r *rtfControlSymbol) Dump(level int) {
fmt.Printf("%sControl Symbol (Symbol: \\%s%s)\r\n", strings.Repeat(" ", level), r.symbol, r.parameter);
}
/**
* Text
*/
type rtfText struct {
content []byte
parent *rtfGroup
}
func (r *rtfText) setParent(p *rtfGroup) {
r.parent = p
}
func (r *rtfText) GetParent()(*rtfGroup) {
return r.parent
}
func (r *rtfText) GetContent()([]byte) {
return r.content
}
func (r *rtfText) Dump(level int) {
fmt.Printf("%sControl Text: %s)\r\n", strings.Repeat(" ", level), r.content);
}