-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter_to.go
174 lines (138 loc) · 3.27 KB
/
writer_to.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
package pdf
import (
"io"
)
// WriteTo serializes the Boolean according to the rules in
// §7.3.2
func (b Boolean) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
if b {
buf.WriteString("true")
} else {
buf.WriteString("false")
}
return buf.WriteTo(w)
}
// WriteTo serializes the Integer according to the rules in
// §7.3.3
func (i Integer) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.Printf("%d", int(i))
return buf.WriteTo(w)
}
// WriteTo serializes the Real according to the rules in
// §7.3.3
func (r Real) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.Printf("%v", float64(r))
return buf.WriteTo(w)
}
// WriteTo serializes the String according to the rules in
// §7.3.4
func (s String) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.WriteByte('(')
for _, b := range []byte(s) {
switch b {
case '(':
buf.WriteString("\\(")
case ')':
buf.WriteString("\\)")
default:
buf.WriteByte(b)
}
}
buf.WriteByte(')')
return buf.WriteTo(w)
}
// WriteTo serializes the Name according to the rules in
// §7.3.5
func (n Name) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.Printf("/%s", n)
return buf.WriteTo(w)
}
// WriteTo serializes the Array according to the rules in
// §7.3.6
func (a Array) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.WriteByte('[')
for _, obj := range a {
n, err := obj.writeTo(buf)
if err != nil {
return n, err
}
buf.WriteByte(' ')
}
if len(a) != 0 {
buf.Truncate(buf.Len() - 1)
}
buf.WriteByte(']')
return buf.WriteTo(w)
}
// WriteTo serializes the Dictionary according to the rules in
// §7.3.6
func (d Dictionary) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.WriteString("<<")
for name, obj := range d {
n, err := name.writeTo(buf)
if err != nil {
return n, err
}
buf.WriteByte(' ')
n, err = obj.writeTo(buf)
if err != nil {
return n, err
}
}
buf.WriteString(">>")
return buf.WriteTo(w)
}
// WriteTo serializes the Stream according to the rules in
// §7.3.8
func (s Stream) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
// update the dictionary
if s.Dictionary == nil {
s.Dictionary = Dictionary{}
}
s.Dictionary[Name("Length")] = Integer(len(s.Stream))
n, err := s.Dictionary.writeTo(buf)
if err != nil {
return n, err
}
buf.Printf("\nstream\n")
nint, err := buf.Write(s.Stream)
if err != nil {
return int64(nint), err
}
buf.Printf("\nendstream")
return buf.WriteTo(w)
}
// WriteTo serializes Null according to the rules in
// §7.3.9
func (null Null) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.WriteString("null")
return buf.WriteTo(w)
}
// WriteTo serializes the ObjectReference according to the rules in
// §7.3.10
func (objref ObjectReference) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.Printf("%d %d R", objref.ObjectNumber, objref.GenerationNumber)
return buf.WriteTo(w)
}
// WriteTo serializes the IndirectObject according to the rules in
// §7.3.10
func (inobj IndirectObject) writeTo(w io.Writer) (int64, error) {
buf := &buffer{}
buf.Printf("%d %d obj\n", inobj.ObjectNumber, inobj.GenerationNumber)
n, err := inobj.Object.writeTo(buf)
if err != nil {
return n, err
}
buf.Printf("\nendobj")
return buf.WriteTo(w)
}