-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct.go
298 lines (279 loc) · 7.83 KB
/
struct.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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2021 Jochen Voss <[email protected]>
//
// 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 pdf
import (
"fmt"
"reflect"
"strings"
"golang.org/x/text/language"
)
// AsDict creates a PDF Dict object, encoding the fields of a Go struct.
//
// If the argument is nil, the result is nil.
//
// This is the converse of [DecodeDict].
func AsDict(s interface{}) Dict {
if s == nil {
return nil
}
v := reflect.Indirect(reflect.ValueOf(s))
if v.Kind() != reflect.Struct {
return nil
}
vt := v.Type()
res := make(Dict)
fieldLoop:
for i := 0; i < vt.NumField(); i++ {
fVal := v.Field(i)
fInfo := vt.Field(i)
optional := false
for _, t := range strings.Split(fInfo.Tag.Get("pdf"), ",") {
switch t {
case "":
// pass
case "optional":
optional = true
case "extra":
for key, val := range fVal.Interface().(map[string]string) {
res[Name(key)] = TextString(val)
}
continue fieldLoop
default:
assign := strings.SplitN(t, "=", 2)
if len(assign) != 2 {
continue
}
res[Name(assign[0])] = Name(assign[1])
}
}
key := Name(fInfo.Name)
switch {
case optional && fVal.IsZero():
continue
case fInfo.Type == textStringType:
res[key] = fVal.Interface().(TextString)
case fInfo.Type == dateType:
res[key] = Date(fVal.Interface().(Date))
case fInfo.Type == languageType:
tag := fVal.Interface().(language.Tag)
if !tag.IsRoot() {
res[key] = TextString(tag.String())
}
case fInfo.Type == versionType:
version := fVal.Interface().(Version)
versionString, err := version.ToString()
if err == nil { // ignore invalid and unknown versions
res[key] = Name(versionString)
}
case fVal.Kind() == reflect.Bool:
res[key] = Boolean(fVal.Bool())
case fInfo.Type == referenceType:
ref := fVal.Interface().(Reference)
if ref != 0 {
res[key] = ref
}
default:
if fVal.CanInterface() {
val := fVal.Interface()
if obj, ok := val.(Object); ok {
res[key] = obj
} else {
panic(fmt.Sprintf("unsupported field type %T", val))
}
}
}
}
return res
}
// DecodeDict initialises a struct using the data from a PDF dictionary.
// The argument dst must be a pointer to a struct, or the function will panic.
//
// Go struct tags can be used to control the decoding process. The following
// tags are supported:
//
// - "optional": the field is optional and may be omitted from the PDF
// dictionary. Omitted fields default to the Go zero value for the
// field type.
// - "allowstring": the field is a Name, but the PDF dictionary may contain
// a String instead. If a String is found, it will be converted to a Name.
// - "extra": the field is a map[string]string which contains all
// entries in the PDF dictionary which are not otherwise decoded.
//
// TODO(voss): remove the "allowstring" tag and always convert strings to names,
// where needed.
//
// This function is the converse of [AsDict].
func DecodeDict(r Getter, dst interface{}, src Dict) error {
v := reflect.Indirect(reflect.ValueOf(dst))
vt := v.Type()
// To allow parsing malformed PDF files, we don't abort on error. Instead,
// we fill all struct fields we can and then return the first error
// encountered.
var firstErr error
seen := map[string]bool{}
extra := -1
fieldLoop:
for i := 0; i < vt.NumField(); i++ {
fVal := v.Field(i)
if !fVal.CanSet() {
continue
}
fInfo := vt.Field(i)
seen[fInfo.Name] = true
fVal.Set(reflect.Zero(fInfo.Type)) // zero all fields
// read the struct tags
optional := false
allowstring := false
for _, t := range strings.Split(fInfo.Tag.Get("pdf"), ",") {
switch t {
case "optional":
optional = true
case "allowstring":
allowstring = true
case "extra":
extra = i
continue fieldLoop
}
}
// get and fix up the value from the Dict
dictVal := src[Name(fInfo.Name)]
if fInfo.Type != objectType && fInfo.Type != referenceType {
// Follow references to indirect objects where needed.
// As a side effect, this calls .AsPDF() on the object.
obj, err := Resolve(r, dictVal)
if err != nil {
if firstErr == nil {
firstErr = err
}
continue
}
dictVal = obj
}
if dictVal == nil {
if !optional && firstErr == nil {
firstErr = fmt.Errorf("required Dict entry /%s not found",
fInfo.Name)
}
continue
}
if allowstring && fInfo.Type == nameType {
if s, ok := dictVal.(String); ok {
dictVal = Name(s)
}
}
// finally, assign the value to the field
switch {
case fInfo.Type.Kind() == reflect.Bool:
fVal.SetBool(dictVal == Boolean(true))
case fInfo.Type == textStringType:
if v, ok := dictVal.(asTextStringer); ok {
s := v.AsTextString()
fVal.Set(reflect.ValueOf(s))
} else if firstErr == nil {
firstErr = fmt.Errorf("/%s: expected TextString but got %T",
fInfo.Name, dictVal)
}
case fInfo.Type == dateType:
if v, ok := dictVal.(asDater); ok {
s, err := v.AsDate()
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("/%s: %s", fInfo.Name, err)
}
} else {
fVal.Set(reflect.ValueOf(s))
}
} else if firstErr == nil {
firstErr = fmt.Errorf("/%s: expected pdf.String but got %T",
fInfo.Name, dictVal)
}
case fInfo.Type == languageType:
tagString, err := GetTextString(r, dictVal)
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("/%s: %s", fInfo.Name, err)
}
} else {
tag, err := language.Parse(string(tagString))
if err == nil {
fVal.Set(reflect.ValueOf(tag))
} else if tagString != "" && firstErr == nil {
firstErr = fmt.Errorf("/%s: %s: %s",
fInfo.Name, tagString, err)
}
}
case fInfo.Type == versionType:
var vString string
switch x := dictVal.(type) {
case Name:
vString = string(x)
case String:
vString = string(x)
case Real:
vString = fmt.Sprintf("%.1f", x)
default:
if firstErr == nil {
firstErr = fmt.Errorf("/%s: expected pdf.Name but got %T",
fInfo.Name, dictVal)
}
}
version, err := ParseVersion(vString)
if err == nil {
fVal.Set(reflect.ValueOf(version))
} else if firstErr == nil {
firstErr = fmt.Errorf("/%s: %s: %s", fInfo.Name, vString, err)
}
case reflect.TypeOf(dictVal).AssignableTo(fInfo.Type):
fVal.Set(reflect.ValueOf(dictVal))
default:
if firstErr == nil {
firstErr = fmt.Errorf("/%s: expected %T but got %T",
fInfo.Name, fVal.Interface(), dictVal)
}
}
}
if extra >= 0 {
extraDict := make(map[string]string)
for keyName, valObj := range src {
key := string(keyName)
if seen[key] {
continue
}
if valObj, ok := valObj.(asTextStringer); ok {
s := valObj.AsTextString()
if len(s) > 0 {
extraDict[key] = string(s)
}
}
}
if len(extraDict) > 0 {
v.Field(extra).Set(reflect.ValueOf(extraDict))
}
}
if firstErr != nil {
return &MalformedFileError{Err: firstErr}
}
return nil
}
var (
nameType = reflect.TypeFor[Name]()
objectType = reflect.TypeFor[Object]()
referenceType = reflect.TypeFor[Reference]()
textStringType = reflect.TypeFor[TextString]()
dateType = reflect.TypeFor[Date]()
languageType = reflect.TypeFor[language.Tag]()
versionType = reflect.TypeFor[Version]()
)