-
-
Notifications
You must be signed in to change notification settings - Fork 456
/
streamdict.go
176 lines (130 loc) · 4.48 KB
/
streamdict.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
package types
import (
"fmt"
"github.com/hhrutter/pdfcpu/log"
"github.com/pkg/errors"
)
// PDFFilter represents a PDF stream filter object.
type PDFFilter struct {
Name string
DecodeParms *PDFDict
}
// PDFStreamDict represents a PDF stream dict object.
type PDFStreamDict struct {
PDFDict
StreamOffset int64
StreamLength *int64
StreamLengthObjNr *int
FilterPipeline []PDFFilter
Raw []byte // Encoded
Content []byte // Decoded
IsPageContent bool
}
// NewPDFStreamDict creates a new PDFStreamDict for given PDFDict, stream offset and length.
func NewPDFStreamDict(pdfDict PDFDict, streamOffset int64, streamLength *int64, streamLengthObjNr *int,
filterPipeline []PDFFilter) PDFStreamDict {
return PDFStreamDict{pdfDict, streamOffset, streamLength, streamLengthObjNr, filterPipeline, nil, nil, false}
}
// HasSoleFilterNamed returns true if there is exactly one filter defined for a stream dict.
func (streamDict PDFStreamDict) HasSoleFilterNamed(filterName string) bool {
fpl := streamDict.FilterPipeline
if fpl == nil {
return false
}
if len(fpl) != 1 {
return false
}
soleFilter := fpl[0]
return soleFilter.Name == filterName
}
// PDFObjectStreamDict represents a object stream dictionary.
type PDFObjectStreamDict struct {
PDFStreamDict
Prolog []byte
ObjCount int
FirstObjOffset int
ObjArray PDFArray
}
// NewPDFObjectStreamDict creates a new PDFObjectStreamDict object.
func NewPDFObjectStreamDict() *PDFObjectStreamDict {
streamDict := PDFStreamDict{PDFDict: NewPDFDict()}
streamDict.Insert("Type", PDFName("ObjStm"))
streamDict.Insert("Filter", PDFName("FlateDecode"))
streamDict.FilterPipeline = []PDFFilter{{Name: "FlateDecode", DecodeParms: nil}}
return &PDFObjectStreamDict{PDFStreamDict: streamDict}
}
// IndexedObject returns the object at given index from a PDFObjectStreamDict.
func (oStreamDict *PDFObjectStreamDict) IndexedObject(index int) (PDFObject, error) {
if oStreamDict.ObjArray == nil {
return nil, errors.Errorf("IndexedObject(%d): object not available", index)
}
return oStreamDict.ObjArray[index], nil
}
// AddObject adds another object to this object stream.
// Relies on decoded content!
func (oStreamDict *PDFObjectStreamDict) AddObject(objNumber int, entry *XRefTableEntry) error {
offset := len(oStreamDict.Content)
s := ""
if oStreamDict.ObjCount > 0 {
s = " "
}
s = s + fmt.Sprintf("%d %d", objNumber, offset)
oStreamDict.Prolog = append(oStreamDict.Prolog, []byte(s)...)
var pdfString string
switch obj := entry.Object.(type) {
case PDFDict:
pdfString = obj.PDFString()
case PDFArray:
pdfString = obj.PDFString()
case PDFInteger:
pdfString = obj.PDFString()
case PDFFloat:
pdfString = obj.PDFString()
case PDFStringLiteral:
pdfString = obj.PDFString()
case PDFHexLiteral:
pdfString = obj.PDFString()
case PDFBoolean:
pdfString = obj.PDFString()
case PDFName:
pdfString = obj.PDFString()
default:
return errors.Errorf("AddObject: undefined PDF object #%d\n", objNumber)
}
oStreamDict.Content = append(oStreamDict.Content, []byte(pdfString)...)
oStreamDict.ObjCount++
log.Debug.Printf("AddObject end : ObjCount:%d prolog = <%s> Content = <%s>\n", oStreamDict.ObjCount, oStreamDict.Prolog, oStreamDict.Content)
return nil
}
// Finalize prepares the final content of the objectstream.
func (oStreamDict *PDFObjectStreamDict) Finalize() {
oStreamDict.Content = append(oStreamDict.Prolog, oStreamDict.Content...)
oStreamDict.FirstObjOffset = len(oStreamDict.Prolog)
log.Debug.Printf("Finalize : firstObjOffset:%d Content = <%s>\n", oStreamDict.FirstObjOffset, oStreamDict.Content)
}
// PDFXRefStreamDict represents a cross reference stream dictionary.
type PDFXRefStreamDict struct {
PDFStreamDict
Size int
Objects []int
W [3]int
PreviousOffset *int64
}
// NewPDFXRefStreamDict creates a new PDFXRefStreamDict object.
func NewPDFXRefStreamDict(ctx *PDFContext) *PDFXRefStreamDict {
streamDict := PDFStreamDict{PDFDict: NewPDFDict()}
streamDict.Insert("Type", PDFName("XRef"))
streamDict.Insert("Filter", PDFName("FlateDecode"))
streamDict.FilterPipeline = []PDFFilter{{Name: "FlateDecode", DecodeParms: nil}}
streamDict.Insert("Root", *ctx.Root)
if ctx.Info != nil {
streamDict.Insert("Info", *ctx.Info)
}
if ctx.ID != nil {
streamDict.Insert("ID", *ctx.ID)
}
if ctx.Encrypt != nil && ctx.EncKey != nil {
streamDict.Insert("Encrypt", *ctx.Encrypt)
}
return &PDFXRefStreamDict{PDFStreamDict: streamDict}
}