forked from pdfcpu/pdfcpu
/
validateFunction.go
222 lines (160 loc) · 5.25 KB
/
validateFunction.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
package pdfcpu
import (
"github.com/pkg/errors"
)
// see 7.10 Functions
func validateExponentialInterpolationFunctionDict(xRefTable *XRefTable, dict *PDFDict) error {
dictName := "exponentialInterpolationFunctionDict"
// Version check
err := xRefTable.ValidateVersion(dictName, V13)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "Domain", REQUIRED, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "Range", OPTIONAL, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "C0", OPTIONAL, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "C1", OPTIONAL, V13, nil)
if err != nil {
return err
}
_, err = validateNumberEntry(xRefTable, dict, dictName, "N", REQUIRED, V13, nil)
return err
}
func validateStitchingFunctionDict(xRefTable *XRefTable, dict *PDFDict) error {
dictName := "stitchingFunctionDict"
// Version check
err := xRefTable.ValidateVersion(dictName, V13)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "Domain", REQUIRED, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "Range", OPTIONAL, V13, nil)
if err != nil {
return err
}
_, err = validateFunctionArrayEntry(xRefTable, dict, dictName, "Functions", REQUIRED, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "Bounds", REQUIRED, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, dict, dictName, "Encode", REQUIRED, V13, nil)
return err
}
func validateSampledFunctionStreamDict(xRefTable *XRefTable, dict *PDFStreamDict) error {
dictName := "sampledFunctionStreamDict"
// Version check
err := xRefTable.ValidateVersion(dictName, V12)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, &dict.PDFDict, dictName, "Domain", REQUIRED, V12, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, &dict.PDFDict, dictName, "Range", REQUIRED, V12, nil)
if err != nil {
return err
}
_, err = validateIntegerArrayEntry(xRefTable, &dict.PDFDict, dictName, "Size", REQUIRED, V12, nil)
if err != nil {
return err
}
validate := func(i int) bool { return intMemberOf(i, []int{1, 2, 4, 8, 12, 16, 24, 32}) }
_, err = validateIntegerEntry(xRefTable, &dict.PDFDict, dictName, "BitsPerSample", REQUIRED, V12, validate)
if err != nil {
return err
}
_, err = validateIntegerEntry(xRefTable, &dict.PDFDict, dictName, "Order", OPTIONAL, V12, func(i int) bool { return i == 1 || i == 3 })
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, &dict.PDFDict, dictName, "Encode", OPTIONAL, V12, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, &dict.PDFDict, dictName, "Decode", OPTIONAL, V12, nil)
return err
}
func validatePostScriptCalculatorFunctionStreamDict(xRefTable *XRefTable, dict *PDFStreamDict) error {
dictName := "postScriptCalculatorFunctionStreamDict"
// Version check
err := xRefTable.ValidateVersion(dictName, V13)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, &dict.PDFDict, dictName, "Domain", REQUIRED, V13, nil)
if err != nil {
return err
}
_, err = validateNumberArrayEntry(xRefTable, &dict.PDFDict, dictName, "Range", REQUIRED, V13, nil)
return err
}
func processFunctionDict(xRefTable *XRefTable, dict *PDFDict) error {
funcType, err := validateIntegerEntry(xRefTable, dict, "functionDict", "FunctionType", REQUIRED, V10, func(i int) bool { return i == 2 || i == 3 })
if err != nil {
return err
}
switch *funcType {
case 2:
err = validateExponentialInterpolationFunctionDict(xRefTable, dict)
case 3:
err = validateStitchingFunctionDict(xRefTable, dict)
}
return err
}
func processFunctionStreamDict(xRefTable *XRefTable, sd *PDFStreamDict) error {
funcType, err := validateIntegerEntry(xRefTable, &sd.PDFDict, "functionDict", "FunctionType", REQUIRED, V10, func(i int) bool { return i == 0 || i == 4 })
if err != nil {
return err
}
switch *funcType {
case 0:
err = validateSampledFunctionStreamDict(xRefTable, sd)
case 4:
err = validatePostScriptCalculatorFunctionStreamDict(xRefTable, sd)
}
return err
}
func processFunction(xRefTable *XRefTable, obj PDFObject) (err error) {
// Function dict: dict or stream dict with required entry "FunctionType" (integer):
// 0: Sampled function (stream dict)
// 2: Exponential interpolation function (dict)
// 3: Stitching function (dict)
// 4: PostScript calculator function (stream dict), since V1.3
switch obj := obj.(type) {
case PDFDict:
// process function 2,3
err = processFunctionDict(xRefTable, &obj)
case PDFStreamDict:
// process function 0,4
err = processFunctionStreamDict(xRefTable, &obj)
default:
return errors.New("processFunction: obj must be dict or stream dict")
}
return err
}
func validateFunction(xRefTable *XRefTable, obj PDFObject) error {
obj, err := xRefTable.Dereference(obj)
if err != nil {
return err
}
if obj == nil {
return errors.New("writeFunction: missing object")
}
return processFunction(xRefTable, obj)
}