forked from pdfcpu/pdfcpu
/
types.go
320 lines (251 loc) · 8.2 KB
/
types.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright 2018 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pdfcpu
import (
"encoding/hex"
"fmt"
"io"
"strconv"
"time"
"github.com/hhrutter/pdfcpu/pkg/types"
)
// Supported line delimiters
const (
EolLF = "\x0A"
EolCR = "\x0D"
EolCRLF = "\x0D\x0A"
)
// ReadSeekerCloser is the interface that groups the ReadSeeker and Close interfaces.
type ReadSeekerCloser interface {
io.ReadSeeker
io.Closer
}
// FreeHeadGeneration is the predefined generation number for the head of the free list.
const FreeHeadGeneration = 65535
// ByteSize represents the various terms for storage space.
type ByteSize float64
// Storage space terms.
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
)
func (b ByteSize) String() string {
switch {
case b >= GB:
return fmt.Sprintf("%.2f GB", b/GB)
case b >= MB:
return fmt.Sprintf("%.1f MB", b/MB)
case b >= KB:
return fmt.Sprintf("%.0f KB", b/KB)
}
return fmt.Sprintf("%f Bytes", b)
}
// IntSet is a set of integers.
type IntSet map[int]bool
// StringSet is a set of strings.
type StringSet map[string]bool
// Object defines an interface for all Objects.
type Object interface {
fmt.Stringer
PDFString() string
}
// Boolean represents a PDF boolean object.
type Boolean bool
func (boolean Boolean) String() string {
return fmt.Sprintf("%v", bool(boolean))
}
// PDFString returns a string representation as found in and written to a PDF file.
func (boolean Boolean) PDFString() string {
return boolean.String()
}
// Value returns a bool value for this PDF object.
func (boolean Boolean) Value() bool {
return bool(boolean)
}
///////////////////////////////////////////////////////////////////////////////////
// Float represents a PDF float object.
type Float float64
func (f Float) String() string {
// Use a precision of 2 for logging readability.
return fmt.Sprintf("%.2f", float64(f))
}
// PDFString returns a string representation as found in and written to a PDF file.
func (f Float) PDFString() string {
// The max precision encountered so far has been 11 (fontType3 fontmatrix components).
return strconv.FormatFloat(f.Value(), 'f', 12, 64)
}
// Value returns a float64 value for this PDF object.
func (f Float) Value() float64 {
return float64(f)
}
///////////////////////////////////////////////////////////////////////////////////
// Integer represents a PDF integer object.
type Integer int
func (i Integer) String() string {
return strconv.Itoa(int(i))
}
// PDFString returns a string representation as found in and written to a PDF file.
func (i Integer) PDFString() string {
return i.String()
}
// Value returns an int value for this PDF object.
func (i Integer) Value() int {
return int(i)
}
///////////////////////////////////////////////////////////////////////////////////
// Point represents a user space location.
type Point struct {
X, Y float64
}
// Rectangle represents a rectangular region in userspace.
type Rectangle struct {
*types.Rectangle
}
func (r Rectangle) equals(r2 Rectangle) bool {
return r.LL == r2.LL && r.UR == r2.UR
}
// FitsWithin returns true if rectangle r fits within rectangle r2.
func (r Rectangle) FitsWithin(r2 *Rectangle) bool {
return r.Width() <= r2.Width() && r.Height() <= r2.Height()
}
// ScaledWidth returns the width for given height according to r's aspect ratio.
func (r Rectangle) ScaledWidth(h float64) float64 {
return r.AspectRatio() * h
}
// ScaledHeight returns the height for given width according to r's aspect ratio.
func (r Rectangle) ScaledHeight(w float64) float64 {
return w / r.AspectRatio()
}
// Array returns the PDF representation of a rectangle.
func (r Rectangle) Array() Array {
return NewNumberArray(r.LL.X, r.LL.Y, r.UR.X, r.UR.Y)
}
// CroppedCopy returns a copy of r with applied margin..
func (r Rectangle) CroppedCopy(margin float64) *Rectangle {
return Rect(
r.LL.X+margin,
r.LL.Y+margin,
r.UR.X-margin,
r.UR.Y-margin,
)
}
// Rect returns a new rectangle for given lower left and upper right corners.
func Rect(llx, lly, urx, ury float64) *Rectangle {
return &Rectangle{types.NewRectangle(llx, lly, urx, ury)}
}
// RectForArray returns a new rectangle for given Array.
func RectForArray(a Array) *Rectangle {
return Rect(
a[0].(Float).Value(),
a[1].(Float).Value(),
a[2].(Float).Value(),
a[3].(Float).Value(),
)
}
// RectForDim returns a new rectangle for given dimensions.
func RectForDim(width, height int) *Rectangle {
return Rect(0.0, 0.0, float64(width), float64(height))
}
// RectForFormat returns a new rectangle for given format.
func RectForFormat(f string) *Rectangle {
d := PaperSize[f]
return RectForDim(d.w, d.h)
}
///////////////////////////////////////////////////////////////////////////////////
// Name represents a PDF name object.
type Name string
func (nameObject Name) String() string {
return fmt.Sprintf("%s", string(nameObject))
}
// PDFString returns a string representation as found in and written to a PDF file.
func (nameObject Name) PDFString() string {
s := " "
if len(nameObject) > 0 {
s = string(nameObject)
}
return fmt.Sprintf("/%s", s)
}
// Value returns a string value for this PDF object.
func (nameObject Name) Value() string {
return string(nameObject)
}
///////////////////////////////////////////////////////////////////////////////////
// StringLiteral represents a PDF string literal object.
type StringLiteral string
func (stringliteral StringLiteral) String() string {
return fmt.Sprintf("(%s)", string(stringliteral))
}
// PDFString returns a string representation as found in and written to a PDF file.
func (stringliteral StringLiteral) PDFString() string {
return stringliteral.String()
}
// Value returns a string value for this PDF object.
func (stringliteral StringLiteral) Value() string {
return string(stringliteral)
}
// DateString returns a string representation of t.
func DateString(t time.Time) string {
_, tz := t.Zone()
return fmt.Sprintf("D:%d%02d%02d%02d%02d%02d+%02d'%02d'",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(),
tz/60/60, tz/60%60)
}
///////////////////////////////////////////////////////////////////////////////////
// HexLiteral represents a PDF hex literal object.
type HexLiteral string
func (hexliteral HexLiteral) String() string {
return fmt.Sprintf("<%s>", string(hexliteral))
}
// PDFString returns the string representation as found in and written to a PDF file.
func (hexliteral HexLiteral) PDFString() string {
return hexliteral.String()
}
// Value returns a string value for this PDF object.
func (hexliteral HexLiteral) Value() string {
return string(hexliteral)
}
// Bytes returns the byte representation.
func (hexliteral HexLiteral) Bytes() ([]byte, error) {
b, err := hex.DecodeString(hexliteral.Value())
if err != nil {
return nil, err
}
return b, err
}
///////////////////////////////////////////////////////////////////////////////////
// IndirectRef represents a PDF indirect object.
type IndirectRef struct {
ObjectNumber Integer
GenerationNumber Integer
}
// NewIndirectRef returns a new PDFIndirectRef object.
func NewIndirectRef(objectNumber, generationNumber int) *IndirectRef {
return &IndirectRef{
ObjectNumber: Integer(objectNumber),
GenerationNumber: Integer(generationNumber)}
}
func (ir IndirectRef) String() string {
return fmt.Sprintf("(%s)", ir.PDFString())
}
// PDFString returns a string representation as found in and written to a PDF file.
func (ir IndirectRef) PDFString() string {
return fmt.Sprintf("%d %d R", ir.ObjectNumber, ir.GenerationNumber)
}
// Equals returns true if two indirect References refer to the same object.
func (ir IndirectRef) Equals(indRef IndirectRef) bool {
return ir.ObjectNumber == indRef.ObjectNumber &&
ir.GenerationNumber == indRef.GenerationNumber
}