forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_obj.go
67 lines (55 loc) · 1.24 KB
/
raw_obj.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
package gopdf
import (
"compress/zlib"
"fmt"
"io"
)
type RawObj struct {
Data []byte
BoundingBox [4]float64
getRoot func() *GoPdf
}
func (i *RawObj) init(getRoot func() *GoPdf) {
i.getRoot = getRoot
}
func (i *RawObj) getType() string {
return "XObject"
}
func (i *RawObj) write(w io.Writer, objId int) error {
buf := GetBuffer()
defer PutBuffer(buf)
compressed, err := zlib.NewWriterLevel(buf, i.getRoot().compressLevel)
if err != nil {
return err
}
_, err = compressed.Write(i.Data)
if err != nil {
return err
}
err = compressed.Close()
if err != nil {
return err
}
content := "<<\n"
content += fmt.Sprintf("\t/Type /%s\n", i.getType())
content += "\t/Subtype /Form\n"
content += "\t/Matrix [1 0 0 1 0 0]\n"
content += fmt.Sprintf("\t/BBox [%.3f %.3f %.3f %.3f]\n", i.BoundingBox[0], i.BoundingBox[1], i.BoundingBox[2], i.BoundingBox[3])
content += fmt.Sprintf("\t/Length %d\n", buf.Len())
content += "\t/Filter /FlateDecode\n" // must come after /Length
content += ">>\n"
content += "stream\n"
_, err = io.WriteString(w, content)
if err != nil {
return err
}
_, err = buf.WriteTo(w)
if err != nil {
return err
}
_, err = io.WriteString(w, "endstream\n")
if err != nil {
return err
}
return nil
}