forked from pdfcpu/pdfcpu
/
dict.go
516 lines (400 loc) · 9.96 KB
/
dict.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
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 pdfcpu
import (
"fmt"
"sort"
"strings"
"github.com/hhrutter/pdfcpu/pkg/log"
"github.com/pkg/errors"
)
// Dict represents a PDF dict object.
type Dict map[string]Object
// NewDict returns a new PDFDict object.
func NewDict() Dict {
return map[string]Object{}
}
// Len returns the length of this PDFDict.
func (d Dict) Len() int {
return len(d)
}
// Insert adds a new entry to this PDFDict.
func (d Dict) Insert(key string, value Object) (ok bool) {
if _, found := d.Find(key); found {
return false
}
d[key] = value
return true
}
// InsertInt adds a new int entry to this PDFDict.
func (d Dict) InsertInt(key string, value int) {
d.Insert(key, Integer(value))
}
// InsertFloat adds a new float entry to this PDFDict.
func (d Dict) InsertFloat(key string, value float32) {
d.Insert(key, Float(value))
}
// InsertString adds a new string entry to this PDFDict.
func (d Dict) InsertString(key, value string) {
d.Insert(key, StringLiteral(value))
}
// InsertName adds a new name entry to this PDFDict.
func (d Dict) InsertName(key, value string) {
d.Insert(key, Name(value))
}
// Update modifies an existing entry of this PDFDict.
func (d Dict) Update(key string, value Object) {
if value != nil {
d[key] = value
}
}
// Find returns the Object for given key and PDFDict.
func (d Dict) Find(key string) (value Object, found bool) {
value, found = d[key]
return
}
// Delete deletes the Object for given key.
func (d Dict) Delete(key string) (value Object) {
value, found := d.Find(key)
if !found {
return nil
}
delete(d, key)
return
}
// Entry returns the value for given key.
func (d Dict) Entry(dictName, key string, required bool) (Object, error) {
obj, found := d.Find(key)
if !found || obj == nil {
if required {
return nil, errors.Errorf("dict=%s required entry=%s missing", dictName, key)
}
//log.Trace.Printf("dict=%s entry %s is nil\n", dictName, key)
return nil, nil
}
return obj, nil
}
// BooleanEntry expects and returns a BooleanEntry for given key.
func (d Dict) BooleanEntry(key string) *bool {
value, found := d.Find(key)
if !found {
return nil
}
bb, ok := value.(Boolean)
if ok {
b := bb.Value()
return &b
}
return nil
}
// StringEntry expects and returns a StringLiteral entry for given key.
// Unused.
func (d Dict) StringEntry(key string) *string {
value, found := d.Find(key)
if !found {
return nil
}
pdfStr, ok := value.(StringLiteral)
if ok {
s := string(pdfStr)
return &s
}
return nil
}
// NameEntry expects and returns a Name entry for given key.
func (d Dict) NameEntry(key string) *string {
value, found := d.Find(key)
if !found {
return nil
}
Name, ok := value.(Name)
if ok {
s := string(Name)
return &s
}
return nil
}
// IntEntry expects and returns a Integer entry for given key.
func (d Dict) IntEntry(key string) *int {
value, found := d.Find(key)
if !found {
return nil
}
pdfInt, ok := value.(Integer)
if ok {
i := int(pdfInt)
return &i
}
return nil
}
// Int64Entry expects and returns a Integer entry representing an int64 value for given key.
func (d Dict) Int64Entry(key string) *int64 {
value, found := d.Find(key)
if !found {
return nil
}
pdfInt, ok := value.(Integer)
if ok {
i := int64(pdfInt)
return &i
}
return nil
}
// IndirectRefEntry returns an indirectRefEntry for given key for this dictionary.
func (d Dict) IndirectRefEntry(key string) *IndirectRef {
value, found := d.Find(key)
if !found {
return nil
}
pdfIndRef, ok := value.(IndirectRef)
if ok {
return &pdfIndRef
}
// return err?
return nil
}
// DictEntry expects and returns a PDFDict entry for given key.
func (d Dict) DictEntry(key string) Dict {
value, found := d.Find(key)
if !found {
return nil
}
// TODO resolve indirect ref.
d, ok := value.(Dict)
if ok {
return d
}
return nil
}
// StreamDictEntry expects and returns a StreamDict entry for given key.
// unused.
func (d Dict) StreamDictEntry(key string) *StreamDict {
value, found := d.Find(key)
if !found {
return nil
}
sd, ok := value.(StreamDict)
if ok {
return &sd
}
return nil
}
// ArrayEntry expects and returns a Array entry for given key.
func (d Dict) ArrayEntry(key string) Array {
value, found := d.Find(key)
if !found {
return nil
}
a, ok := value.(Array)
if ok {
return a
}
return nil
}
// StringLiteralEntry returns a StringLiteral object for given key.
func (d Dict) StringLiteralEntry(key string) *StringLiteral {
value, found := d.Find(key)
if !found {
return nil
}
s, ok := value.(StringLiteral)
if ok {
return &s
}
return nil
}
// HexLiteralEntry returns a HexLiteral object for given key.
func (d Dict) HexLiteralEntry(key string) *HexLiteral {
value, found := d.Find(key)
if !found {
return nil
}
s, ok := value.(HexLiteral)
if ok {
return &s
}
return nil
}
// Length returns a *int64 for entry with key "Length".
// Stream length may be referring to an indirect object.
func (d Dict) Length() (*int64, *int) {
val := d.Int64Entry("Length")
if val != nil {
return val, nil
}
indirectRef := d.IndirectRefEntry("Length")
if indirectRef == nil {
return nil, nil
}
intVal := indirectRef.ObjectNumber.Value()
return nil, &intVal
}
// Type returns the value of the name entry for key "Type".
func (d Dict) Type() *string {
return d.NameEntry("Type")
}
// Subtype returns the value of the name entry for key "Subtype".
func (d Dict) Subtype() *string {
return d.NameEntry("Subtype")
}
// Size returns the value of the int entry for key "Size"
func (d Dict) Size() *int {
return d.IntEntry("Size")
}
// IsObjStm returns true if given PDFDict is an object stream.
func (d Dict) IsObjStm() bool {
return d.Type() != nil && *d.Type() == "ObjStm"
}
// W returns a *Array for key "W".
func (d Dict) W() Array {
return d.ArrayEntry("W")
}
// Prev returns the previous offset.
func (d Dict) Prev() *int64 {
return d.Int64Entry("Prev")
}
// Index returns a *Array for key "Index".
func (d Dict) Index() Array {
return d.ArrayEntry("Index")
}
// N returns a *int for key "N".
func (d Dict) N() *int {
return d.IntEntry("N")
}
// First returns a *int for key "First".
func (d Dict) First() *int {
return d.IntEntry("First")
}
// IsLinearizationParmDict returns true if this dict has an int entry for key "Linearized".
func (d Dict) IsLinearizationParmDict() bool {
return d.IntEntry("Linearized") != nil
}
// IncrementBy increments the integer value for given key by i.
func (d *Dict) IncrementBy(key string, i int) error {
v := d.IntEntry(key)
if v == nil {
return errors.Errorf("IncrementBy: unknown key: %s", key)
}
*v += i
d.Update(key, Integer(*v))
return nil
}
// Increment increments the integer value for given key.
func (d *Dict) Increment(key string) error {
return d.IncrementBy(key, 1)
}
func (d Dict) indentedString(level int) string {
logstr := []string{"<<\n"}
tabstr := strings.Repeat("\t", level)
var keys []string
for k := range d {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := d[k]
if subdict, ok := v.(Dict); ok {
dictStr := subdict.indentedString(level + 1)
logstr = append(logstr, fmt.Sprintf("%s<%s, %s>\n", tabstr, k, dictStr))
continue
}
if a, ok := v.(Array); ok {
arrStr := a.indentedString(level + 1)
logstr = append(logstr, fmt.Sprintf("%s<%s, %s>\n", tabstr, k, arrStr))
continue
}
logstr = append(logstr, fmt.Sprintf("%s<%s, %v>\n", tabstr, k, v))
}
logstr = append(logstr, fmt.Sprintf("%s%s", strings.Repeat("\t", level-1), ">>"))
return strings.Join(logstr, "")
}
// PDFString returns a string representation as found in and written to a PDF file.
func (d Dict) PDFString() string {
logstr := []string{} //make([]string, 20)
logstr = append(logstr, "<<")
var keys []string
for k := range d {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := d[k]
if v == nil {
logstr = append(logstr, fmt.Sprintf("/%s null", k))
continue
}
d, ok := v.(Dict)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s%s", k, d.PDFString()))
continue
}
a, ok := v.(Array)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s%s", k, a.PDFString()))
continue
}
ir, ok := v.(IndirectRef)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s %s", k, ir.PDFString()))
continue
}
n, ok := v.(Name)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s%s", k, n.PDFString()))
continue
}
i, ok := v.(Integer)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s %s", k, i.PDFString()))
continue
}
f, ok := v.(Float)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s %s", k, f.PDFString()))
continue
}
b, ok := v.(Boolean)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s %s", k, b.PDFString()))
continue
}
sl, ok := v.(StringLiteral)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s%s", k, sl.PDFString()))
continue
}
hl, ok := v.(HexLiteral)
if ok {
logstr = append(logstr, fmt.Sprintf("/%s%s", k, hl.PDFString()))
continue
}
log.Info.Fatalf("PDFDict.PDFString(): entry of unknown object type: %T %[1]v\n", v)
}
logstr = append(logstr, ">>")
return strings.Join(logstr, "")
}
func (d Dict) String() string {
return d.indentedString(1)
}
// StringEntryBytes returns the byte slice representing the string value for key.
func (d Dict) StringEntryBytes(key string) ([]byte, error) {
s := d.StringLiteralEntry(key)
if s != nil {
return Unescape(s.Value())
}
h := d.HexLiteralEntry(key)
if h != nil {
return h.Bytes()
}
return nil, nil
}