/
outlines.go
300 lines (256 loc) · 7.57 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
/*
* 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/xwc1125/gopdf/common"
"github.com/xwc1125/gopdf/core"
)
// PdfOutlineTreeNode contains common fields used by the outline and outline
// item objects.
type PdfOutlineTreeNode struct {
context interface{} // Allow accessing outer structure.
First *PdfOutlineTreeNode
Last *PdfOutlineTreeNode
}
// PdfOutline represents a PDF outline dictionary (Table 152 - p. 376).
type PdfOutline struct {
PdfOutlineTreeNode
Parent *PdfOutlineTreeNode
Count *int64
primitive *core.PdfIndirectObject
}
// PdfOutlineItem represents an outline item dictionary (Table 153 - pp. 376 - 377).
type PdfOutlineItem struct {
PdfOutlineTreeNode
Title *core.PdfObjectString
Parent *PdfOutlineTreeNode
Prev *PdfOutlineTreeNode
Next *PdfOutlineTreeNode
Count *int64
Dest core.PdfObject
A core.PdfObject
SE core.PdfObject
C core.PdfObject
F core.PdfObject
primitive *core.PdfIndirectObject
}
// NewPdfOutline returns an initialized PdfOutline.
func NewPdfOutline() *PdfOutline {
outline := &PdfOutline{
primitive: core.MakeIndirectObject(core.MakeDict()),
}
outline.context = outline
return outline
}
// NewPdfOutlineTree returns an initialized PdfOutline tree.
func NewPdfOutlineTree() *PdfOutline {
outlineTree := NewPdfOutline()
outlineTree.context = &outlineTree
return outlineTree
}
// NewPdfOutlineItem returns an initialized PdfOutlineItem.
func NewPdfOutlineItem() *PdfOutlineItem {
outlineItem := &PdfOutlineItem{
primitive: core.MakeIndirectObject(core.MakeDict()),
}
outlineItem.context = outlineItem
return outlineItem
}
// NewOutlineBookmark returns an initialized PdfOutlineItem for a given bookmark title and page.
func NewOutlineBookmark(title string, page *core.PdfIndirectObject) *PdfOutlineItem {
bookmark := PdfOutlineItem{}
bookmark.context = &bookmark
bookmark.Title = core.MakeString(title)
destArray := core.MakeArray()
destArray.Append(page)
destArray.Append(core.MakeName("Fit"))
bookmark.Dest = destArray
return &bookmark
}
// Does not traverse the tree.
func newPdfOutlineFromIndirectObject(container *core.PdfIndirectObject) (*PdfOutline, error) {
dict, isDict := container.PdfObject.(*core.PdfObjectDictionary)
if !isDict {
return nil, fmt.Errorf("outline object not a dictionary")
}
outline := NewPdfOutline()
if obj := dict.Get("Type"); obj != nil {
typeVal, ok := obj.(*core.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 := core.GetNumberAsInt64(obj)
if err != nil {
return nil, err
}
outline.Count = &count
}
return outline, nil
}
// Does not traverse the tree.
func (r *PdfReader) newPdfOutlineItemFromIndirectObject(container *core.PdfIndirectObject) (*PdfOutlineItem, error) {
dict, isDict := container.PdfObject.(*core.PdfObjectDictionary)
if !isDict {
return nil, fmt.Errorf("outline object not a dictionary")
}
item := NewPdfOutlineItem()
// Title (required).
obj := dict.Get("Title")
if obj == nil {
return nil, fmt.Errorf("missing Title from Outline Item (required)")
}
title, ok := core.GetString(obj)
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.(*core.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 = core.ResolveReference(obj)
if !r.isLazy {
err := r.traverseObjectData(item.Dest)
if err != nil {
return nil, err
}
}
}
if obj := dict.Get("A"); obj != nil {
item.A = core.ResolveReference(obj)
if !r.isLazy {
err := r.traverseObjectData(item.A)
if err != nil {
return nil, err
}
}
}
if obj := dict.Get("SE"); obj != nil {
// TODO: To add structure element support.
// Currently not supporting structure elements.
item.SE = nil
}
if obj := dict.Get("C"); obj != nil {
item.C = core.ResolveReference(obj)
}
if obj := dict.Get("F"); obj != nil {
item.F = core.ResolveReference(obj)
}
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
}
// GetContainingPdfObject returns the container of the outline tree node (indirect object).
func (n *PdfOutlineTreeNode) GetContainingPdfObject() core.PdfObject {
return n.getOuter().GetContainingPdfObject()
}
// ToPdfObject returns the PDF representation of the outline tree node.
func (n *PdfOutlineTreeNode) ToPdfObject() core.PdfObject {
return n.getOuter().ToPdfObject()
}
// GetContainingPdfObject returns the container of the outline (indirect object).
func (o *PdfOutline) GetContainingPdfObject() core.PdfObject {
return o.primitive
}
// ToPdfObject recursively builds the Outline tree PDF object.
func (o *PdfOutline) ToPdfObject() core.PdfObject {
container := o.primitive
dict := container.PdfObject.(*core.PdfObjectDictionary)
dict.Set("Type", core.MakeName("Outlines"))
if o.First != nil {
dict.Set("First", o.First.ToPdfObject())
}
if o.Last != nil {
dict.Set("Last", o.Last.getOuter().GetContainingPdfObject())
//PdfObjectConverterCache[o.Last.getOuter()]
}
if o.Parent != nil {
dict.Set("Parent", o.Parent.getOuter().GetContainingPdfObject())
}
if o.Count != nil {
dict.Set("Count", core.MakeInteger(*o.Count))
}
return container
}
// GetContainingPdfObject returns the container of the outline item (indirect object).
func (oi *PdfOutlineItem) GetContainingPdfObject() core.PdfObject {
return oi.primitive
}
// ToPdfObject recursively builds the Outline tree PDF object.
func (oi *PdfOutlineItem) ToPdfObject() core.PdfObject {
container := oi.primitive
dict := container.PdfObject.(*core.PdfObjectDictionary)
dict.Set("Title", oi.Title)
if oi.A != nil {
dict.Set("A", oi.A)
}
if obj := dict.Get("SE"); obj != nil {
// TODO: Currently not supporting structure element hierarchy.
// Remove it.
dict.Remove("SE")
// delete(*dict, "SE")
}
/*
if oi.SE != nil {
(*dict)["SE"] = oi.SE
}
*/
if oi.C != nil {
dict.Set("C", oi.C)
}
if oi.Dest != nil {
dict.Set("Dest", oi.Dest)
}
if oi.F != nil {
dict.Set("F", oi.F)
}
if oi.Count != nil {
dict.Set("Count", core.MakeInteger(*oi.Count))
}
if oi.Next != nil {
dict.Set("Next", oi.Next.ToPdfObject())
}
if oi.First != nil {
dict.Set("First", oi.First.ToPdfObject())
}
if oi.Prev != nil {
dict.Set("Prev", oi.Prev.getOuter().GetContainingPdfObject())
//PdfObjectConverterCache[oi.Prev.getOuter()]
}
if oi.Last != nil {
dict.Set("Last", oi.Last.getOuter().GetContainingPdfObject())
// PdfObjectConverterCache[oi.Last.getOuter()]
}
if oi.Parent != nil {
dict.Set("Parent", oi.Parent.getOuter().GetContainingPdfObject())
//PdfObjectConverterCache[oi.Parent.getOuter()]
}
return container
}