-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.go
More file actions
172 lines (148 loc) · 4.19 KB
/
Copy pathobjects.go
File metadata and controls
172 lines (148 loc) · 4.19 KB
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
// Copyright (c) 2026 the go-pdfkit/pdfkit authors. All rights reserved.
// Use of this source code is governed by a BSD-3-Clause license that can be
// found in the LICENSE file at the root of this repository.
package pdfkit
import (
"bytes"
"strconv"
)
// pdfValue is any object that can be serialised into PDF syntax. The concrete
// implementations model the handful of COS object types pdfkit emits: names,
// numbers, strings, arrays, dictionaries, streams and indirect references.
type pdfValue interface {
encodePDF(*bytes.Buffer)
}
// ftoa formats a float in PDF's plain decimal notation: no exponent (PDF has no
// scientific form) and no redundant trailing zeros. Whole values render without
// a fractional part, so 100.0 becomes "100".
func ftoa(v float64) string {
return strconv.FormatFloat(v, 'f', -1, 64)
}
// pdfName is a PDF name object such as /Type. It is written with a leading
// slash and the characters that are illegal in a name escaped as #xx.
type pdfName string
func (n pdfName) encodePDF(b *bytes.Buffer) {
b.WriteByte('/')
for i := 0; i < len(n); i++ {
c := n[i]
if c < '!' || c > '~' || c == '#' || c == '/' || c == '%' ||
c == '(' || c == ')' || c == '<' || c == '>' || c == '[' ||
c == ']' || c == '{' || c == '}' {
b.WriteByte('#')
const hex = "0123456789ABCDEF"
b.WriteByte(hex[c>>4])
b.WriteByte(hex[c&0xf])
continue
}
b.WriteByte(c)
}
}
// pdfInt is a PDF integer object.
type pdfInt int64
func (n pdfInt) encodePDF(b *bytes.Buffer) {
b.WriteString(strconv.FormatInt(int64(n), 10))
}
// pdfReal is a PDF real (floating-point) object.
type pdfReal float64
func (n pdfReal) encodePDF(b *bytes.Buffer) {
b.WriteString(ftoa(float64(n)))
}
// pdfString is a PDF literal string, written in parentheses with the reserved
// bytes escaped. It carries arbitrary bytes (used for /Producer, dates, ...).
type pdfString []byte
func (s pdfString) encodePDF(b *bytes.Buffer) {
b.WriteByte('(')
for _, c := range s {
switch c {
case '(', ')', '\\':
b.WriteByte('\\')
b.WriteByte(c)
case '\n':
b.WriteString(`\n`)
case '\r':
b.WriteString(`\r`)
case '\t':
b.WriteString(`\t`)
default:
b.WriteByte(c)
}
}
b.WriteByte(')')
}
// pdfHexString is a PDF hexadecimal string, written between angle brackets. It
// is used for the trailer /ID and other binary payloads.
type pdfHexString []byte
func (s pdfHexString) encodePDF(b *bytes.Buffer) {
const hex = "0123456789ABCDEF"
b.WriteByte('<')
for _, c := range s {
b.WriteByte(hex[c>>4])
b.WriteByte(hex[c&0xf])
}
b.WriteByte('>')
}
// pdfArray is a PDF array object.
type pdfArray []pdfValue
func (a pdfArray) encodePDF(b *bytes.Buffer) {
b.WriteByte('[')
for i, v := range a {
if i > 0 {
b.WriteByte(' ')
}
v.encodePDF(b)
}
b.WriteByte(']')
}
// pdfDict is a PDF dictionary. Keys are held in insertion order so the encoded
// bytes are deterministic (map iteration order is not).
type pdfDict struct {
keys []string
vals []pdfValue
}
// newDict returns an empty dictionary ready for set.
func newDict() *pdfDict { return &pdfDict{} }
// set adds or replaces the entry for key. Replacing keeps the original slot so
// ordering stays stable across edits.
func (d *pdfDict) set(key string, v pdfValue) *pdfDict {
for i, k := range d.keys {
if k == key {
d.vals[i] = v
return d
}
}
d.keys = append(d.keys, key)
d.vals = append(d.vals, v)
return d
}
func (d *pdfDict) encodePDF(b *bytes.Buffer) {
b.WriteString("<<")
for i, k := range d.keys {
if i > 0 {
b.WriteByte(' ')
}
pdfName(k).encodePDF(b)
b.WriteByte(' ')
d.vals[i].encodePDF(b)
}
b.WriteString(">>")
}
// pdfStream is an indirect stream object: a dictionary followed by raw bytes.
// The /Length entry is filled in from the data when the object is written.
type pdfStream struct {
dict *pdfDict
data []byte
}
func (s *pdfStream) encodePDF(b *bytes.Buffer) {
s.dict.set("Length", pdfInt(len(s.data)))
s.dict.encodePDF(b)
b.WriteString("\nstream\n")
b.Write(s.data)
b.WriteString("\nendstream")
}
// objRef is an indirect reference to object number n (generation 0), written as
// "n 0 R".
type objRef int
func (r objRef) encodePDF(b *bytes.Buffer) {
b.WriteString(strconv.Itoa(int(r)))
b.WriteString(" 0 R")
}