-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
216 lines (196 loc) · 4.88 KB
/
read.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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package pagetree
import (
"maps"
"seehuhn.de/go/pdf"
)
// FindPages returns a list of all pages in the document.
// The returned list contains the references to the page dictionaries.
func FindPages(r pdf.Getter) ([]pdf.Reference, error) {
meta := r.GetMeta()
catalog := meta.Catalog
if catalog.Pages == 0 {
return nil, errInvalidPageTree
}
var res []pdf.Reference
todo := []pdf.Reference{catalog.Pages}
seen := map[pdf.Reference]bool{
catalog.Pages: true,
}
for len(todo) > 0 {
k := len(todo) - 1
ref := todo[k]
todo = todo[:k]
node, err := pdf.GetDict(r, ref)
if err != nil {
return nil, err
}
tp, err := pdf.GetName(r, node["Type"])
if err != nil {
return nil, err
}
switch tp {
case "Page":
res = append(res, ref)
case "Pages":
kids, err := pdf.GetArray(r, node["Kids"])
if err != nil {
return nil, err
}
for i := len(kids) - 1; i >= 0; i-- {
kid := kids[i]
if kidRef, ok := kid.(pdf.Reference); ok && !seen[kidRef] {
todo = append(todo, kidRef)
seen[kidRef] = true
} else {
res = append(res, 0)
}
}
}
}
return res, nil
}
type Iterator struct {
Err error
r pdf.Getter
}
func NewIterator(r pdf.Getter) *Iterator {
return &Iterator{r: r}
}
// All returns a function which iterates over all pages in the document.
// The arguments are the reference to the page dictionary and the page
// dictionary itself.
//
// The function must return true if the iteration should continue, or false if it
// should stop.
//
// TODO(voss): change this to iterate over (page number, page dictionary) pairs?
func (i *Iterator) All() func(yield func(pdf.Reference, pdf.Dict) bool) bool {
yield := func(yield func(pdf.Reference, pdf.Dict) bool) bool {
if i.Err != nil {
return false
}
r := i.r
meta := r.GetMeta()
root := meta.Catalog.Pages
if root == 0 {
return true
}
type frame struct {
todo []pdf.Reference
inherited pdf.Dict
}
var stack []*frame
todo := []pdf.Reference{root}
inherited := pdf.Dict{}
inheritable := getInheritable(meta.Version)
seen := map[pdf.Reference]bool{
root: true,
}
for len(todo) > 0 || len(stack) > 0 {
if len(todo) == 0 {
k := len(stack) - 1
frame := stack[k]
stack = stack[:k]
todo = frame.todo
inherited = frame.inherited
}
k := len(todo) - 1
ref := todo[k]
todo = todo[:k]
node, err := pdf.GetDict(r, ref)
if err != nil {
if pdf.IsMalformed(err) {
continue
}
i.Err = err
return false
}
tp, err := pdf.GetName(r, node["Type"])
if err != nil {
if pdf.IsMalformed(err) {
continue
}
i.Err = err
return false
}
switch tp {
case "Page":
for _, name := range inheritable {
_, isPresent := node[name]
if val, canInherit := inherited[name]; !isPresent && canInherit {
node[name] = val
}
}
delete(node, "Parent")
cont := yield(ref, node)
if !cont {
return false
}
case "Pages":
kids, err := pdf.GetArray(r, node["Kids"])
if err != nil {
if pdf.IsMalformed(err) {
continue
}
i.Err = err
return false
}
hasInheritables := false
for _, name := range inheritable {
if _, isPresent := node[name]; isPresent {
hasInheritables = true
break
}
}
if hasInheritables {
if len(todo) > 0 {
stack = append(stack, &frame{
todo: todo,
inherited: maps.Clone(inherited),
})
todo = nil
}
for _, name := range inheritable {
if tmp, ok := node[name]; ok {
inherited[name] = tmp
}
}
}
for i := len(kids) - 1; i >= 0; i-- {
kid := kids[i]
if kidRef, ok := kid.(pdf.Reference); ok && !seen[kidRef] {
todo = append(todo, kidRef)
seen[kidRef] = true
}
}
}
}
return true
}
return yield
}
func getInheritable(v pdf.Version) []pdf.Name {
if v < pdf.V1_3 {
return inheritedOld
}
return inheritableNew
}
var (
inheritableNew = []pdf.Name{"Resources", "MediaBox", "CropBox", "Rotate"} // Since PDF 1.3
inheritedOld = []pdf.Name{"Resources", "MediaBox", "CropBox", "Rotate", "AA"} // Before PDF 1.3
)