forked from unidoc/unidoc
/
outlines.go
312 lines (266 loc) · 7.21 KB
/
outlines.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
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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package model
import (
"fmt"
"github.com/unidoc/unidoc/common"
. "github.com/unidoc/unidoc/pdf/core"
)
type PdfOutlineTreeNode struct {
context interface{} // Allow accessing outer structure.
First *PdfOutlineTreeNode
Last *PdfOutlineTreeNode
}
// PDF outline dictionary (Table 152 - p. 376).
type PdfOutline struct {
PdfOutlineTreeNode
Parent *PdfOutlineTreeNode
Count *int64
primitive *PdfIndirectObject
}
// Pdf outline item dictionary (Table 153 - pp. 376 - 377).
type PdfOutlineItem struct {
PdfOutlineTreeNode
Title *PdfObjectString
Parent *PdfOutlineTreeNode
Prev *PdfOutlineTreeNode
Next *PdfOutlineTreeNode
Count *int64
Dest PdfObject
A PdfObject
SE PdfObject
C PdfObject
F PdfObject
primitive *PdfIndirectObject
}
func NewPdfOutline() *PdfOutline {
outline := &PdfOutline{}
container := &PdfIndirectObject{}
container.PdfObject = MakeDict()
outline.primitive = container
return outline
}
func NewPdfOutlineTree() *PdfOutline {
outlineTree := NewPdfOutline()
outlineTree.context = &outlineTree
return outlineTree
}
func NewPdfOutlineItem() *PdfOutlineItem {
outlineItem := &PdfOutlineItem{}
container := &PdfIndirectObject{}
container.PdfObject = MakeDict()
outlineItem.primitive = container
return outlineItem
}
func NewOutlineBookmark(title string, page *PdfIndirectObject) *PdfOutlineItem {
bookmark := PdfOutlineItem{}
bookmark.context = &bookmark
bookmark.Title = MakeString(title)
destArray := PdfObjectArray{}
destArray = append(destArray, page)
destArray = append(destArray, MakeName("Fit"))
bookmark.Dest = &destArray
return &bookmark
}
// Does not traverse the tree.
func newPdfOutlineFromIndirectObject(container *PdfIndirectObject) (*PdfOutline, error) {
dict, isDict := container.PdfObject.(*PdfObjectDictionary)
if !isDict {
return nil, fmt.Errorf("Outline object not a dictionary")
}
outline := PdfOutline{}
outline.primitive = container
outline.context = &outline
if obj := dict.Get("Type"); obj != nil {
typeVal, ok := obj.(*PdfObjectName)
if ok {
if *typeVal != "Outlines" {
common.Log.Debug("ERROR Type != Outlines (%s)", *typeVal)
// Should be "Outlines" if there, but some files have other types
// Log as an error but do not quit.
// Might be a good idea to log this kind of deviation from the standard separately.
}
}
}
if obj := dict.Get("Count"); obj != nil {
// This should always be an integer, but in a few cases has been a float.
count, err := getNumberAsInt64(obj)
if err != nil {
return nil, err
}
outline.Count = &count
}
return &outline, nil
}
// Does not traverse the tree.
func (this *PdfReader) newPdfOutlineItemFromIndirectObject(container *PdfIndirectObject) (*PdfOutlineItem, error) {
dict, isDict := container.PdfObject.(*PdfObjectDictionary)
if !isDict {
return nil, fmt.Errorf("Outline object not a dictionary")
}
item := PdfOutlineItem{}
item.primitive = container
item.context = &item
// Title (required).
obj := dict.Get("Title")
if obj == nil {
return nil, fmt.Errorf("Missing Title from Outline Item (required)")
}
obj, err := this.traceToObject(obj)
if err != nil {
return nil, err
}
title, ok := TraceToDirectObject(obj).(*PdfObjectString)
if !ok {
return nil, fmt.Errorf("Title not a string (%T)", obj)
}
item.Title = title
// Count (optional).
if obj := dict.Get("Count"); obj != nil {
countVal, ok := obj.(*PdfObjectInteger)
if !ok {
return nil, fmt.Errorf("Count not an integer (%T)", obj)
}
count := int64(*countVal)
item.Count = &count
}
// Other keys.
if obj := dict.Get("Dest"); obj != nil {
item.Dest, err = this.traceToObject(obj)
if err != nil {
return nil, err
}
err := this.traverseObjectData(item.Dest)
if err != nil {
return nil, err
}
}
if obj := dict.Get("A"); obj != nil {
item.A, err = this.traceToObject(obj)
if err != nil {
return nil, err
}
err := this.traverseObjectData(item.A)
if err != nil {
return nil, err
}
}
if obj := dict.Get("SE"); obj != nil {
// XXX: To add structure element support.
// Currently not supporting structure elements.
item.SE = nil
/*
item.SE, err = this.traceToObject(obj)
if err != nil {
return nil, err
}
*/
}
if obj := dict.Get("C"); obj != nil {
item.C, err = this.traceToObject(obj)
if err != nil {
return nil, err
}
}
if obj := dict.Get("F"); obj != nil {
item.F, err = this.traceToObject(obj)
if err != nil {
return nil, err
}
}
return &item, nil
}
// Get the outer object of the tree node (Outline or OutlineItem).
func (n *PdfOutlineTreeNode) getOuter() PdfModel {
if outline, isOutline := n.context.(*PdfOutline); isOutline {
return outline
}
if outlineItem, isOutlineItem := n.context.(*PdfOutlineItem); isOutlineItem {
return outlineItem
}
common.Log.Debug("ERROR Invalid outline tree node item") // Should never happen.
return nil
}
func (this *PdfOutlineTreeNode) GetContainingPdfObject() PdfObject {
return this.getOuter().GetContainingPdfObject()
}
func (this *PdfOutlineTreeNode) ToPdfObject() PdfObject {
return this.getOuter().ToPdfObject()
}
func (this *PdfOutline) GetContainingPdfObject() PdfObject {
return this.primitive
}
// Recursively build the Outline tree PDF object.
func (this *PdfOutline) ToPdfObject() PdfObject {
container := this.primitive
dict := container.PdfObject.(*PdfObjectDictionary)
dict.Set("Type", MakeName("Outlines"))
if this.First != nil {
dict.Set("First", this.First.ToPdfObject())
}
if this.Last != nil {
dict.Set("Last", this.Last.getOuter().GetContainingPdfObject())
//PdfObjectConverterCache[this.Last.getOuter()]
}
if this.Parent != nil {
dict.Set("Parent", this.Parent.getOuter().GetContainingPdfObject())
}
return container
}
func (this *PdfOutlineItem) GetContainingPdfObject() PdfObject {
return this.primitive
}
// Outline item.
// Recursively build the Outline tree PDF object.
func (this *PdfOutlineItem) ToPdfObject() PdfObject {
container := this.primitive
dict := container.PdfObject.(*PdfObjectDictionary)
dict.Set("Title", this.Title)
if this.A != nil {
dict.Set("A", this.A)
}
if obj := dict.Get("SE"); obj != nil {
// XXX: Currently not supporting structure element hierarchy.
// Remove it.
dict.Remove("SE")
// delete(*dict, "SE")
}
/*
if this.SE != nil {
(*dict)["SE"] = this.SE
}
*/
if this.C != nil {
dict.Set("C", this.C)
}
if this.Dest != nil {
dict.Set("Dest", this.Dest)
}
if this.F != nil {
dict.Set("F", this.F)
}
if this.Count != nil {
dict.Set("Count", MakeInteger(*this.Count))
}
if this.Next != nil {
dict.Set("Next", this.Next.ToPdfObject())
}
if this.First != nil {
dict.Set("First", this.First.ToPdfObject())
}
if this.Prev != nil {
dict.Set("Prev", this.Prev.getOuter().GetContainingPdfObject())
//PdfObjectConverterCache[this.Prev.getOuter()]
}
if this.Last != nil {
dict.Set("Last", this.Last.getOuter().GetContainingPdfObject())
// PdfObjectConverterCache[this.Last.getOuter()]
}
if this.Parent != nil {
dict.Set("Parent", this.Parent.getOuter().GetContainingPdfObject())
//PdfObjectConverterCache[this.Parent.getOuter()]
}
return container
}