forked from pdfcpu/pdfcpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlineTree.go
170 lines (134 loc) · 4.18 KB
/
outlineTree.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
/*
Copyright 2018 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validate
import (
pdf "github.com/hhrutter/pdfcpu/pkg/pdfcpu"
"github.com/pkg/errors"
)
func validateOutlineItemDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
dictName := "outlineItemDict"
// Title, required, text string
_, err := validateStringEntry(xRefTable, d, dictName, "Title", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
// Parent, required, dict indRef
ir, err := validateIndRefEntry(xRefTable, d, dictName, "Parent", REQUIRED, pdf.V10)
if err != nil {
return err
}
_, err = xRefTable.DereferenceDict(*ir)
if err != nil {
return err
}
// Count, optional, int
_, err = validateIntegerEntry(xRefTable, d, dictName, "Count", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// SE, optional, dict indRef, since V1.3
ir, err = validateIndRefEntry(xRefTable, d, dictName, "SE", OPTIONAL, pdf.V13)
if err != nil {
return err
}
if ir != nil {
_, err = xRefTable.DereferenceDict(*ir)
if err != nil {
return err
}
}
// C, optional, array of 3 numbers, since V1.4
_, err = validateNumberArrayEntry(xRefTable, d, dictName, "C", OPTIONAL, pdf.V14, func(a pdf.Array) bool { return len(a) == 3 })
if err != nil {
return err
}
// F, optional integer, since V1.4
_, err = validateIntegerEntry(xRefTable, d, dictName, "F", OPTIONAL, pdf.V14, nil)
if err != nil {
return err
}
// Optional A or Dest, since V1.1
return validateActionOrDestination(xRefTable, d, dictName, pdf.V11)
}
func validateOutlineTree(xRefTable *pdf.XRefTable, first, last *pdf.IndirectRef) error {
var (
d pdf.Dict
objNumber int
err error
)
// Process linked list of outline items.
for ir := first; ir != nil; ir = d.IndirectRefEntry("Next") {
objNumber = ir.ObjectNumber.Value()
// outline item dict
d, err = xRefTable.DereferenceDict(*ir)
if err != nil {
return err
}
if d == nil {
return errors.Errorf("validateOutlineTree: object #%d is nil.", objNumber)
}
err = validateOutlineItemDict(xRefTable, d)
if err != nil {
return err
}
firstChild := d.IndirectRefEntry("First")
lastChild := d.IndirectRefEntry("Last")
if firstChild == nil && lastChild == nil {
// Leaf
continue
}
if firstChild != nil && lastChild != nil {
// Recurse into subtree.
err = validateOutlineTree(xRefTable, firstChild, lastChild)
if err != nil {
return err
}
continue
}
return errors.New("validateOutlineTree: corrupted, needs both first and last or neither for a leaf")
}
// Relaxed validation
if objNumber != last.ObjectNumber.Value() && xRefTable.ValidationMode == pdf.ValidationStrict {
return errors.Errorf("validateOutlineTree: corrupted child list %d <> %d\n", objNumber, last.ObjectNumber)
}
return nil
}
func validateOutlines(xRefTable *pdf.XRefTable, rootDict pdf.Dict, required bool, sinceVersion pdf.Version) error {
// => 12.3.3 Document Outline
ir, err := validateIndRefEntry(xRefTable, rootDict, "rootDict", "Outlines", OPTIONAL, sinceVersion)
if err != nil || ir == nil {
return err
}
d, err := xRefTable.DereferenceDict(*ir)
if err != nil || d == nil {
return err
}
// Type, optional, name
_, err = validateNameEntry(xRefTable, d, "outlineDict", "Type", OPTIONAL, pdf.V10, func(s string) bool { return s == "Outlines" })
if err != nil {
return err
}
first := d.IndirectRefEntry("First")
last := d.IndirectRefEntry("Last")
if first == nil {
if last != nil {
return errors.New("validateOutlines: corrupted, root needs both first and last")
}
// leaf
return nil
}
if last == nil {
return errors.New("validateOutlines: corrupted, root needs both first and last")
}
return validateOutlineTree(xRefTable, first, last)
}