/
combine_duplicate_direct_objects.go
70 lines (60 loc) · 1.93 KB
/
combine_duplicate_direct_objects.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package optimize
import (
"crypto/md5"
"github.com/xwc1125/gopdf/core"
)
// CombineDuplicateDirectObjects combines duplicated direct objects by its data hash.
// It implements interface model.Optimizer.
type CombineDuplicateDirectObjects struct {
}
// Optimize optimizes PDF objects to decrease PDF size.
func (dup *CombineDuplicateDirectObjects) Optimize(objects []core.PdfObject) (optimizedObjects []core.PdfObject, err error) {
updateObjectNumbers(objects)
dictsByHash := make(map[string][]*core.PdfObjectDictionary)
var processDict func(pDict *core.PdfObjectDictionary)
processDict = func(pDict *core.PdfObjectDictionary) {
for _, key := range pDict.Keys() {
obj := pDict.Get(key)
if dict, isDictObj := obj.(*core.PdfObjectDictionary); isDictObj {
hasher := md5.New()
hasher.Write([]byte(dict.WriteString()))
hash := string(hasher.Sum(nil))
dictsByHash[hash] = append(dictsByHash[hash], dict)
processDict(dict)
}
}
}
for _, obj := range objects {
ind, isIndirectObj := obj.(*core.PdfIndirectObject)
if !isIndirectObj {
continue
}
if dict, isDictObj := ind.PdfObject.(*core.PdfObjectDictionary); isDictObj {
processDict(dict)
}
}
indirects := make([]core.PdfObject, 0, len(dictsByHash))
replaceTable := make(map[core.PdfObject]core.PdfObject)
for _, dicts := range dictsByHash {
if len(dicts) < 2 {
continue
}
dict := core.MakeDict()
dict.Merge(dicts[0])
ind := core.MakeIndirectObject(dict)
indirects = append(indirects, ind)
for i := 0; i < len(dicts); i++ {
dict := dicts[i]
replaceTable[dict] = ind
}
}
optimizedObjects = make([]core.PdfObject, len(objects))
copy(optimizedObjects, objects)
optimizedObjects = append(indirects, optimizedObjects...)
replaceObjectsInPlace(optimizedObjects, replaceTable)
return optimizedObjects, nil
}