forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlines_obj.go
218 lines (187 loc) · 4.42 KB
/
outlines_obj.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
package gopdf
import (
"fmt"
"io"
)
// OutlinesObj : outlines dictionary
type OutlinesObj struct { //impl IObj
getRoot func() *GoPdf
index int
first int
last int
count int
lastObj *OutlineObj
}
func (o *OutlinesObj) init(funcGetRoot func() *GoPdf) {
o.getRoot = funcGetRoot
o.first = -1
o.last = -1
}
func (o *OutlinesObj) getType() string {
return "Outlines"
}
func (o *OutlinesObj) write(w io.Writer, objID int) error {
content := "<<\n"
content += fmt.Sprintf("\t/Type /%s\n", o.getType())
content += fmt.Sprintf("\t/Count %d\n", o.count)
if o.first >= 0 {
content += fmt.Sprintf("\t/First %d 0 R\n", o.first)
}
if o.last >= 0 {
content += fmt.Sprintf("\t/Last %d 0 R\n", o.last)
}
content += ">>\n"
if _, err := io.WriteString(w, content); err != nil {
return err
}
return nil
}
func (o *OutlinesObj) SetIndexObjOutlines(index int) {
o.index = index
}
func (o *OutlinesObj) AddOutline(dest int, title string) {
oo := &OutlineObj{title: title, dest: dest, parent: o.index, prev: o.last, next: -1}
o.last = o.getRoot().addObj(oo) + 1
if o.first <= 0 {
o.first = o.last
}
if o.lastObj != nil {
o.lastObj.next = o.last
}
o.lastObj = oo
o.count++
}
// AddOutlinesWithPosition add outlines with position
func (o *OutlinesObj) AddOutlinesWithPosition(dest int, title string, y float64) *OutlineObj {
oo := &OutlineObj{title: title, dest: dest, parent: o.index, prev: o.last, next: -1, height: y}
o.last = o.getRoot().addObj(oo) + 1
if o.first <= 0 {
o.first = o.last
}
if o.lastObj != nil {
o.lastObj.next = o.last
}
o.lastObj = oo
o.count++
oo.index = o.last
return oo
}
func (o *OutlinesObj) Count() int {
return o.count
}
// OutlineObj include attribute of outline
type OutlineObj struct { //impl IObj
title string
index int
dest int
parent int
prev int
next int
first int
last int
height float64
}
func (o *OutlineObj) init(funcGetRoot func() *GoPdf) {
}
func (o *OutlineObj) SetFirst(first int) {
o.first = first
}
func (o *OutlineObj) SetLast(last int) {
o.last = last
}
func (o *OutlineObj) SetPrev(prev int) {
o.prev = prev
}
func (o *OutlineObj) SetNext(next int) {
o.next = next
}
func (o *OutlineObj) SetParent(parent int) {
o.parent = parent
}
func (o *OutlineObj) GetIndex() int {
return o.index
}
func (o *OutlineObj) getType() string {
return "Outline"
}
//func (o *OutlineObj) write(w io.Writer, objID int) error {
// io.WriteString(w, "<<\n")
// fmt.Fprintf(w, " /Parent %d 0 R\n", o.parent)
// if o.prev >= 0 {
// fmt.Fprintf(w, " /Prev %d 0 R\n", o.prev)
// }
// if o.next >= 0 {
// fmt.Fprintf(w, " /Next %d 0 R\n", o.next)
// }
// fmt.Fprintf(w, " /Dest [ %d 0 R /XYZ null null null ]\n", o.dest)
// fmt.Fprintf(w, " /Title <FEFF%s>\n", encodeUtf8(o.title))
// io.WriteString(w, ">>\n")
// return nil
//}
func (o *OutlineObj) write(w io.Writer, objID int) error {
io.WriteString(w, "<<\n")
fmt.Fprintf(w, " /Parent %d 0 R\n", o.parent)
if o.prev >= 0 {
fmt.Fprintf(w, " /Prev %d 0 R\n", o.prev)
}
if o.next >= 0 {
fmt.Fprintf(w, " /Next %d 0 R\n", o.next)
}
if o.first > 0 {
fmt.Fprintf(w, " /First %d 0 R\n", o.first)
}
if o.last > 0 {
fmt.Fprintf(w, " /Last %d 0 R\n", o.last)
}
fmt.Fprintf(w, " /Dest [ %d 0 R /XYZ 90 %f 0 ]\n", o.dest, o.height)
fmt.Fprintf(w, " /Title <FEFF%s>\n", encodeUtf8(o.title))
io.WriteString(w, ">>\n")
return nil
}
// OutlineNode is a node of outline
type OutlineNode struct {
Obj *OutlineObj
Children []*OutlineNode
}
// OutlineNodes are all nodes of outline
type OutlineNodes []*OutlineNode
// Parse parse outline nodes
func (objs OutlineNodes) Parse() {
for i, obj := range objs {
if i == 0 {
obj.Obj.SetPrev(-1)
} else {
obj.Obj.SetNext(objs[i-1].Obj.GetIndex())
}
if i == len(objs)-1 {
obj.Obj.SetNext(-1)
} else {
obj.Obj.SetNext(objs[i+1].Obj.GetIndex())
}
obj.Parse()
}
}
// Parse parse outline
func (obj OutlineNode) Parse() {
if obj.Children == nil || len(obj.Children) == 0 {
return
}
for i, children := range obj.Children {
if i == 0 {
obj.Obj.SetFirst(children.Obj.GetIndex())
children.Obj.SetPrev(-1)
}
if i == len(obj.Children)-1 {
obj.Obj.SetLast(children.Obj.GetIndex())
children.Obj.SetNext(-1)
}
if i != 0 {
children.Obj.SetPrev(obj.Children[i-1].Obj.GetIndex())
}
if i != len(obj.Children)-1 {
children.Obj.SetNext(obj.Children[i+1].Obj.GetIndex())
}
children.Obj.SetParent(obj.Obj.GetIndex())
children.Parse()
}
}