-
Notifications
You must be signed in to change notification settings - Fork 1
/
trees.go
921 lines (826 loc) · 21.9 KB
/
trees.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
package model
import (
"fmt"
"sort"
"strings"
)
// generic code for limits
type numTree interface {
nums() []int
kids() []numTree
Limits() [2]int
}
func limitsNum(n numTree) [2]int {
kids := n.kids()
if len(kids) == 0 { // leaf node
nums := n.nums()
if len(nums) == 0 {
return [2]int{}
}
sort.Ints(nums)
return [2]int{nums[0], nums[len(nums)-1]}
}
limits := kids[0].Limits()
for _, kid := range kids[1:] {
kidLimits := kid.Limits()
if kidLimits[0] < limits[0] {
limits[0] = kidLimits[0]
}
if kidLimits[1] > limits[1] {
limits[1] = kidLimits[1]
}
}
return limits
}
type nameTree interface {
names() []string
kids() []nameTree
Limits() [2]string
}
func limitsName(n nameTree) [2]string {
kids := n.kids()
if len(kids) == 0 { // leaf node
names := n.names()
if len(names) == 0 {
return [2]string{}
}
sort.Strings(names)
return [2]string{names[0], names[len(names)-1]}
}
limits := kids[0].Limits()
for _, kid := range kids[1:] {
kidLimits := kid.Limits()
if kidLimits[0] < limits[0] {
limits[0] = kidLimits[0]
}
if kidLimits[1] > limits[1] {
limits[1] = kidLimits[1]
}
}
return limits
}
// NameToDest associate an explicit destination
// to a name.
type NameToDest struct {
Name DestinationString
Destination DestinationExplicit
}
func (n NameToDest) clone(cache cloneCache) NameToDest {
out := n
if n.Destination != nil {
out.Destination = n.Destination.clone(cache).(DestinationExplicit)
}
return out
}
// DestTree links a serie of arbitrary name
// to explicit destination, enabling `NamedDestination`
// to reference them.
// In may be found as a tree in PDF files, but
// a linear representation may be enough for many use cases.
type DestTree struct {
Kids []DestTree
// Names must be sorted by .Name fields
Names []NameToDest
}
// IsEmpty returns true if the tree is empty
// and should not be written in the PDF file.
func (d DestTree) IsEmpty() bool {
return len(d.Kids) == 0 && len(d.Names) == 0
}
func (d DestTree) names() []string {
out := make([]string, len(d.Names))
for i, k := range d.Names {
out[i] = string(k.Name)
}
return out
}
func (d DestTree) kids() []nameTree {
out := make([]nameTree, len(d.Kids))
for i, k := range d.Kids {
out[i] = k
}
return out
}
// Limits specify the (lexically) least and greatest keys included in the Names array of
// a leaf node or in the Names arrays of any leaf nodes that are descendants of an
// intermediate node.
func (d DestTree) Limits() [2]string {
return limitsName(d)
}
// LookupTable walks the name tree and
// accumulates the result into one map
func (d DestTree) LookupTable() map[DestinationString]DestinationExplicit {
out := make(map[DestinationString]DestinationExplicit)
for _, v := range d.Names {
out[v.Name] = v.Destination
}
for _, kid := range d.Kids {
for name, dest := range kid.LookupTable() {
out[name] = dest
}
}
return out
}
func (p DestTree) pdfString(pdf pdfWriter, ref Reference) string {
b := newBuffer()
limits := p.Limits()
b.line("<</Limits [%s %s]",
pdf.EncodeString(limits[0], ByteString, ref), pdf.EncodeString(limits[1], ByteString, ref))
if len(p.Kids) != 0 {
b.fmt("/Kids [")
for _, kid := range p.Kids {
kidRef := pdf.CreateObject()
pdf.WriteObject(kid.pdfString(pdf, kidRef), kidRef)
b.fmt("%s ", kidRef)
}
b.line("]")
}
if len(p.Names) != 0 {
b.fmt("/Names [ ")
for _, name := range p.Names {
b.fmt("%s %s ", pdf.EncodeString(string(name.Name), ByteString, ref),
name.Destination.pdfDestination(pdf, ref))
}
b.line("]")
}
b.fmt(">>")
return b.String()
}
// cache.pages must have been filled
func (d DestTree) clone(cache cloneCache) DestTree {
out := d
if d.Kids != nil { // preserve reflect.DeepEqual
out.Kids = make([]DestTree, len(d.Kids))
}
for i, k := range d.Kids {
out.Kids[i] = k.clone(cache)
}
if d.Names != nil { // preserve reflect.DeepEqual
out.Names = make([]NameToDest, len(d.Names))
}
for i, k := range d.Names {
out.Names[i] = k.clone(cache)
}
return out
}
// ----------------------------------------------------------------------
// NameToAppearance associate an appearance stream
// to a name.
type NameToAppearance struct {
Name string
Appearance *XObjectForm
}
func (n NameToAppearance) clone(cache cloneCache) NameToAppearance {
out := n
if n.Appearance != nil {
out.Appearance = cache.checkOrClone(n.Appearance).(*XObjectForm)
}
return out
}
// AppearanceTree links a serie of arbitrary name
// to appearance streams
type AppearanceTree struct {
Kids []AppearanceTree
Names []NameToAppearance
}
// IsEmpty returns true if the tree is empty
// and should not be written in the PDF file.
func (d AppearanceTree) IsEmpty() bool {
return len(d.Kids) == 0 && len(d.Names) == 0
}
func (d AppearanceTree) names() []string {
out := make([]string, len(d.Names))
for i, k := range d.Names {
out[i] = string(k.Name)
}
return out
}
func (d AppearanceTree) kids() []nameTree {
out := make([]nameTree, len(d.Kids))
for i, k := range d.Kids {
out[i] = k
}
return out
}
// Limits specify the (lexically) least and greatest keys included in the Names array of
// a leaf node or in the Names arrays of any leaf nodes that are descendants of an
// intermediate node.
func (d AppearanceTree) Limits() [2]string {
return limitsName(d)
}
// LookupTable walks the name tree and
// accumulates the result into one map
func (d AppearanceTree) LookupTable() map[string]*XObjectForm {
out := make(map[string]*XObjectForm)
for _, v := range d.Names {
out[v.Name] = v.Appearance
}
for _, kid := range d.Kids {
for name, dest := range kid.LookupTable() {
out[name] = dest
}
}
return out
}
func (p AppearanceTree) pdfString(pdf pdfWriter, ref Reference) string {
b := newBuffer()
limits := p.Limits()
b.line("<</Limits [%s %s]",
pdf.EncodeString(limits[0], ByteString, ref), pdf.EncodeString(limits[1], ByteString, ref))
if len(p.Kids) != 0 {
b.fmt("/Kids [")
for _, kid := range p.Kids {
kidRef := pdf.CreateObject()
pdf.WriteObject(kid.pdfString(pdf, kidRef), kidRef)
b.fmt("%s ", kidRef)
}
b.line("]")
}
if len(p.Names) != 0 {
b.fmt("/Names [ ")
for _, name := range p.Names {
b.fmt("%s %s ", pdf.EncodeString(string(name.Name), ByteString, ref),
pdf.addItem(name.Appearance))
}
b.line("]")
}
b.fmt(">>")
return b.String()
}
func (d AppearanceTree) clone(cache cloneCache) AppearanceTree {
out := d
if d.Kids != nil { // preserve reflect.DeepEqual
out.Kids = make([]AppearanceTree, len(d.Kids))
for i, k := range d.Kids {
out.Kids[i] = k.clone(cache)
}
}
if d.Names != nil { // preserve reflect.DeepEqual
out.Names = make([]NameToAppearance, len(d.Names))
for i, k := range d.Names {
out.Names[i] = k.clone(cache)
}
}
return out
}
// ----------------------------------------------------------------------
type NameToFile struct {
Name string
FileSpec *FileSpec // indirect object
}
func (f NameToFile) clone(cache cloneCache) NameToFile {
out := f
out.FileSpec = cache.checkOrClone(f.FileSpec).(*FileSpec)
return out
}
// EmbeddedFileTree is written as a Name Tree in PDF,
// but, since it generally won't be big, is
// represented here as a flat list.
// It must be sorted by .Name field.
type EmbeddedFileTree []NameToFile
func (d EmbeddedFileTree) names() []string {
out := make([]string, len(d))
for i, k := range d {
out[i] = k.Name
}
return out
}
func (d EmbeddedFileTree) kids() []nameTree {
return nil
}
func (efs EmbeddedFileTree) Limits() [2]string {
return limitsName(efs)
}
func (p EmbeddedFileTree) pdfString(pdf pdfWriter, ref Reference) string {
lims := p.Limits()
chunks := make([]string, len(p))
for i, f := range p {
fsRef := pdf.addItem(f.FileSpec)
chunks[i] = fmt.Sprintf("%s %s",
pdf.EncodeString(f.Name, ByteString, ref), fsRef)
}
return fmt.Sprintf("<</Limits [%s %s] /Names [%s]>>",
pdf.EncodeString(lims[0], ByteString, ref), pdf.EncodeString(lims[1], ByteString, ref),
strings.Join(chunks, " "))
}
func (p EmbeddedFileTree) clone(cache cloneCache) EmbeddedFileTree {
if p == nil { // preserve reflect.DeepEqual
return p
}
out := make(EmbeddedFileTree, len(p))
for i, f := range p {
out[i] = f.clone(cache)
}
return out
}
// -----------------------------------------------------------------------
// PageLabel defines the labelling characteristics for the pages
// in a range.
type PageLabel struct {
S Name
P string // optional
St int // optionnal default to 1
}
func (p PageLabel) pdfString(st PDFWritter, ref Reference) string {
b := newBuffer()
b.fmt("<</S %s", p.S)
if p.P != "" {
b.fmt(" /P %s", st.EncodeString(p.P, TextString, ref))
}
if p.St != 1 {
b.fmt(" /St %d", p.St)
}
b.fmt(">>")
return b.String()
}
type NumToPageLabel struct {
Num int
PageLabel PageLabel // rather a direct object
}
// return two elements, to be included in an array
func (n NumToPageLabel) pdfString(pdf pdfWriter, ref Reference) string {
return fmt.Sprintf("%d %s", n.Num, n.PageLabel.pdfString(pdf, ref))
}
type PageLabelsTree struct {
Kids []PageLabelsTree
Nums []NumToPageLabel
}
func (d PageLabelsTree) nums() []int {
out := make([]int, len(d.Nums))
for i, k := range d.Nums {
out[i] = k.Num
}
return out
}
func (d PageLabelsTree) kids() []numTree {
out := make([]numTree, len(d.Kids))
for i, k := range d.Kids {
out[i] = k
}
return out
}
// Limits specify the (numerically) least and greatest keys included in
// the Nums array of a leaf node or in the Nums arrays of any leaf nodes that are
// descendants of an intermediate node.
func (d PageLabelsTree) Limits() [2]int {
return limitsNum(d)
}
// LookupTable walks the number tree and
// accumulates the result into one map
func (d PageLabelsTree) LookupTable() map[int]PageLabel {
out := make(map[int]PageLabel)
for _, v := range d.Nums {
out[v.Num] = v.PageLabel
}
for _, kid := range d.Kids {
for name, dest := range kid.LookupTable() {
out[name] = dest
}
}
return out
}
func (p PageLabelsTree) pdfString(pdf pdfWriter, ref Reference, isRoot bool) string {
b := newBuffer()
b.fmt("<<")
if !isRoot {
limits := p.Limits()
b.fmt("/Limits [%d %d] ", limits[0], limits[1])
}
if len(p.Kids) != 0 {
b.fmt("/Kids [")
for _, kid := range p.Kids {
kidRef := pdf.CreateObject()
pdf.WriteObject(kid.pdfString(pdf, kidRef, false), kidRef)
b.fmt("%s ", kidRef)
}
b.line("]")
}
if len(p.Nums) != 0 {
b.fmt("/Nums [ ")
for _, num := range p.Nums {
b.fmt("%s ", num.pdfString(pdf, ref))
}
b.line("]")
}
b.fmt(">>")
return b.String()
}
func (p PageLabelsTree) Clone() PageLabelsTree {
out := p
out.Nums = append([]NumToPageLabel(nil), p.Nums...)
if p.Kids != nil {
out.Kids = make([]PageLabelsTree, len(p.Kids))
for i, k := range p.Kids {
out.Kids[i] = k.Clone()
}
}
return out
}
// ------------------------------------------------------------
type NameToStructureElement struct {
Name string
Structure *StructureElement
}
// return two elements, to be included in an array
func (n NameToStructureElement) pdfString(pdf pdfWriter, ref Reference) string {
return fmt.Sprintf("%s %s",
pdf.EncodeString(n.Name, ByteString, ref), pdf.structure[n.Structure])
}
func (n NameToStructureElement) clone(cache cloneCache) NameToStructureElement {
out := n
out.Structure = cache.structure[n.Structure]
return out
}
type IDTree struct {
Kids []IDTree
Names []NameToStructureElement
}
// NewIDTree builds a valid IDTree from the given maping.
// The tree should be good enough for most use cases,
// but you may also build you own.
func NewIDTree(ids map[string]*StructureElement) IDTree {
// keys must be sorted
allKeys := make([]string, 0, len(ids))
for k := range ids {
allKeys = append(allKeys, k)
}
sort.Strings(allKeys)
const maxKidLength, maxKeysLength = 20, 50
// walk takes a sorted list of keys
// and build an IDTree, by splitting if if necessary
var walk func(keys []string) IDTree
walk = func(keys []string) IDTree {
var node IDTree
if len(keys) <= maxKeysLength {
// all names fit into one leaf object
node.Names = make([]NameToStructureElement, len(keys))
for i, n := range keys {
node.Names[i] = NameToStructureElement{Name: n, Structure: ids[n]}
}
return node
}
// too many names: we split the list into subtrees
sizeChunk := len(keys) / (maxKidLength - 1) // so that we have at most maxKidLength
for _, chunk := range splitStrings(keys, sizeChunk) {
node.Kids = append(node.Kids, walk(chunk))
}
return node
}
return walk(allKeys)
}
func splitStrings(names []string, sizeChunk int) [][]string {
out := make([][]string, 0, 1+len(names)/sizeChunk)
for i := 0; i < len(names); i += sizeChunk {
sliceEnd := i + sizeChunk
if i+sizeChunk > len(names) {
sliceEnd = len(names)
}
out = append(out, names[i:sliceEnd])
}
return out
}
func splitInts(nums []int, sizeChunk int) [][]int {
out := make([][]int, 0, 1+len(nums)/sizeChunk)
for i := 0; i < len(nums); i += sizeChunk {
sliceEnd := i + sizeChunk
if i+sizeChunk > len(nums) {
sliceEnd = len(nums)
}
out = append(out, nums[i:sliceEnd])
}
return out
}
// LookupTable walks the tree and accumulate the names into one map.
func (id IDTree) LookupTable() map[string]*StructureElement {
out := make(map[string]*StructureElement, len(id.Names))
for _, name := range id.Names {
out[name.Name] = name.Structure
}
for _, kid := range id.Kids {
for name, s := range kid.LookupTable() { // merge
out[name] = s
}
}
return out
}
func (d IDTree) names() []string {
out := make([]string, len(d.Names))
for i, k := range d.Names {
out[i] = k.Name
}
return out
}
func (d IDTree) kids() []nameTree {
out := make([]nameTree, len(d.Kids))
for i, k := range d.Kids {
out[i] = k
}
return out
}
// Limits specify the (lexically) least and greatest keys included in the Names array of
// a leaf node or in the Names arrays of any leaf nodes that are descendants of an
// intermediate node.
func (d IDTree) Limits() [2]string {
return limitsName(d)
}
// requires that the structure tree is already clone
func (d IDTree) clone(cache cloneCache) IDTree {
out := d
if d.Kids != nil { // preserve reflect.DeepEqual
out.Kids = make([]IDTree, len(d.Kids))
for i, k := range d.Kids {
out.Kids[i] = k.clone(cache)
}
}
if d.Names != nil { // preserve reflect.DeepEqual
out.Names = make([]NameToStructureElement, len(d.Names))
for i, k := range d.Names {
out.Names[i] = k.clone(cache)
}
}
return out
}
// requires the structure to have been written
func (d IDTree) pdfString(pdf pdfWriter, ref Reference) string {
b := newBuffer()
limits := d.Limits()
b.line("<</Limits [%s %s]",
pdf.EncodeString(limits[0], ByteString, ref),
pdf.EncodeString(limits[1], ByteString, ref))
if len(d.Kids) != 0 {
b.fmt("/Kids [")
for _, kid := range d.Kids {
kidRef := pdf.CreateObject()
pdf.WriteObject(kid.pdfString(pdf, kidRef), kidRef)
b.fmt("%s ", kidRef)
}
b.line("]")
}
if len(d.Names) != 0 {
b.fmt("/Names [ ")
for _, num := range d.Names {
b.fmt("%s ", num.pdfString(pdf, ref))
}
b.line("]")
}
b.fmt(">>")
return b.String()
}
// NumToParent store the values of `ParentTree`.
// For an object that is a content item in its own right, the value shall be
// a *StructureElement, that contains it as a content item.
// For a page object or content stream containing marked-content
// sequences that are content items, the value shall be []*StructureElement,
// parent elements of those marked-content sequences.
type NumToParent struct {
Num int
// either Parent or Parents must be non nil
Parent *StructureElement
Parents []*StructureElement
}
func (n NumToParent) pdfString(pdf pdfWriter) string {
var parent string
if n.Parent != nil {
parent = pdf.structure[n.Parent].String()
} else {
refs := make([]Reference, len(n.Parents))
for i, p := range n.Parents {
refs[i] = pdf.structure[p]
}
parent = writeRefArray(refs)
}
return fmt.Sprintf("%d %s", n.Num, parent)
}
func (n NumToParent) clone(cache cloneCache) NumToParent {
out := n
out.Parent = cache.structure[n.Parent]
if n.Parents != nil {
out.Parents = make([]*StructureElement, len(n.Parents))
for i, p := range n.Parents {
out.Parents[i] = cache.structure[p]
}
}
return out
}
// ParentTree associate to a StructParent or StructParents entry
// the corresponding *StructureElement(s)
type ParentTree struct {
Kids []ParentTree
Nums []NumToParent
}
// NewParentTree builds a valid ParentTree from the given maping.
// The tree should be good enough for most use cases,
// but you may also build you own.
// Note that the field `Num` in the `parents` values are ignored:
// the key in the map is used instead.
func NewParentTree(parents map[int]NumToParent) ParentTree {
// keys must be sorted
allKeys := make([]int, 0, len(parents))
for k := range parents {
allKeys = append(allKeys, k)
}
sort.Ints(allKeys)
const maxKidLength, maxKeysLength = 20, 50
// walk takes a sorted list of keys
// and build an ParentTree, by splitting if if necessary
var walk func(keys []int) ParentTree
walk = func(keys []int) ParentTree {
var node ParentTree
if len(keys) <= maxKeysLength {
// all keys fit into one leaf object
node.Nums = make([]NumToParent, len(keys))
for i, n := range keys {
out := parents[n]
out.Num = n
node.Nums[i] = out
}
return node
}
// too many keys: we split the list into subtrees
sizeChunk := len(keys) / (maxKidLength - 1) // so that we have at most maxKidLength
for _, chunk := range splitInts(keys, sizeChunk) {
node.Kids = append(node.Kids, walk(chunk))
}
return node
}
return walk(allKeys)
}
// LookupTable walks the tree and accumulate the parents into one map.
func (id ParentTree) LookupTable() map[int]NumToParent {
out := make(map[int]NumToParent, len(id.Nums))
for _, num := range id.Nums {
out[num.Num] = num
}
for _, kid := range id.Kids {
for num, s := range kid.LookupTable() { // merge
out[num] = s
}
}
return out
}
func (d ParentTree) nums() []int {
out := make([]int, len(d.Nums))
for i, k := range d.Nums {
out[i] = k.Num
}
return out
}
func (d ParentTree) kids() []numTree {
out := make([]numTree, len(d.Kids))
for i, k := range d.Kids {
out[i] = k
}
return out
}
func (d ParentTree) Limits() [2]int {
return limitsNum(d)
}
// structure elements must have been cloned
func (d ParentTree) clone(cache cloneCache) ParentTree {
out := d
if d.Nums != nil { // preserve nil
out.Nums = make([]NumToParent, len(d.Nums))
for i, n := range d.Nums {
out.Nums[i] = n.clone(cache)
}
}
if d.Kids != nil { // preserve nil
out.Kids = make([]ParentTree, len(d.Kids))
for i, k := range d.Kids {
out.Kids[i] = k.clone(cache)
}
}
return out
}
func (d ParentTree) pdfString(pdf pdfWriter) string {
b := newBuffer()
limits := d.Limits()
b.line("<</Limits [%d %d]", limits[0], limits[1])
if len(d.Kids) != 0 {
b.fmt("/Kids [")
for _, kid := range d.Kids {
kidRef := pdf.addObject(kid.pdfString(pdf))
b.fmt("%s ", kidRef)
}
b.line("]")
}
if len(d.Nums) != 0 {
b.fmt("/Nums [ ")
for _, num := range d.Nums {
b.fmt("%s ", num.pdfString(pdf))
}
b.line("]")
}
b.fmt(">>")
return b.String()
}
// ----------------- Pages and Templates name entry -----------------
type NameToPage struct {
Name string
Page *PageObject
}
// return two elements, to be included in an array
// pages can be either already written (visible) or not (invisible)
func (n NameToPage) pdfString(pdf pdfWriter, ref Reference) string {
pageRef, ok := pdf.pages[n.Page]
if !ok { // template
pageRef = pdf.addObject(n.Page.pdfString(pdf))
}
return fmt.Sprintf("%s %s", pdf.EncodeString(n.Name, ByteString, ref), pageRef)
}
func (n NameToPage) clone(cache cloneCache) NameToPage {
out := n
if cached := cache.pages[n.Page]; cached != nil {
out.Page = cached.(*PageObject)
} else {
out.Page = n.Page.clone(cache).(*PageObject)
}
return out
}
// TemplateTree is both the Templates and the Pages entry
// of the name dictionary
type TemplateTree struct {
Kids []TemplateTree
Names []NameToPage
}
// IsEmpty returns true if the tree is empty
// and should not be written in the PDF file.
func (d TemplateTree) IsEmpty() bool {
return len(d.Kids) == 0 && len(d.Names) == 0
}
func (d TemplateTree) names() []string {
out := make([]string, len(d.Names))
for i, k := range d.Names {
out[i] = k.Name
}
return out
}
func (d TemplateTree) kids() []nameTree {
out := make([]nameTree, len(d.Kids))
for i, k := range d.Kids {
out[i] = k
}
return out
}
// Limits specify the (lexically) least and greatest keys included in the Names array of
// a leaf node or in the Names arrays of any leaf nodes that are descendants of an
// intermediate node.
func (d TemplateTree) Limits() [2]string {
return limitsName(d)
}
// requires that the structure tree is already clone
func (d TemplateTree) clone(cache cloneCache) TemplateTree {
out := d
if d.Kids != nil { // preserve reflect.DeepEqual
out.Kids = make([]TemplateTree, len(d.Kids))
for i, k := range d.Kids {
out.Kids[i] = k.clone(cache)
}
}
if d.Names != nil { // preserve reflect.DeepEqual
out.Names = make([]NameToPage, len(d.Names))
for i, k := range d.Names {
out.Names[i] = k.clone(cache)
}
}
return out
}
func (d TemplateTree) pdfString(pdf pdfWriter, ref Reference) string {
b := newBuffer()
limits := d.Limits()
b.line("<</Limits [%s %s]",
pdf.EncodeString(limits[0], ByteString, ref),
pdf.EncodeString(limits[1], ByteString, ref))
if len(d.Kids) != 0 {
b.fmt("/Kids [")
for _, kid := range d.Kids {
kidRef := pdf.CreateObject()
pdf.WriteObject(kid.pdfString(pdf, kidRef), kidRef)
b.fmt("%s ", kidRef)
}
b.line("]")
}
if len(d.Names) != 0 {
b.fmt("/Names [ ")
for _, num := range d.Names {
b.fmt("%s ", num.pdfString(pdf, ref))
}
b.line("]")
}
b.fmt(">>")
return b.String()
}
// LookupTable walks the name tree and
// accumulates the result into one map
func (d TemplateTree) LookupTable() map[string]*PageObject {
out := make(map[string]*PageObject)
for _, v := range d.Names {
out[v.Name] = v.Page
}
for _, kid := range d.Kids {
for name, dest := range kid.LookupTable() {
out[name] = dest
}
}
return out
}