forked from pdfcpu/pdfcpu
/
types.go
502 lines (404 loc) · 12.3 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
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 (
"bytes"
"encoding/hex"
"fmt"
"strconv"
"github.com/hamdouni/pdfcpu/pkg/types"
)
// Supported line delimiters
const (
EolLF = "\x0A"
EolCR = "\x0D"
EolCRLF = "\x0D\x0A"
)
// 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
Clone() Object
PDFString() string
}
// Boolean represents a PDF boolean object.
type Boolean bool
// Clone returns a clone of boolean.
func (boolean Boolean) Clone() Object {
return boolean
}
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
// Clone returns a clone of f.
func (f Float) Clone() Object {
return f
}
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 12 (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
// Clone returns a clone of i.
func (i Integer) Clone() Object {
return i
}
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()
}
// Dimensions returns r's dimensions.
func (r Rectangle) Dimensions() Dim {
return Dim{r.Width(), r.Height()}
}
// Translate moves r by dx and dy.
func (r *Rectangle) Translate(dx, dy float64) {
r.LL.Translate(dx, dy)
r.UR.Translate(dx, dy)
}
// 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,
)
}
func (r Rectangle) formatToInches() string {
return fmt.Sprintf("(%3.2f, %3.2f, %3.2f, %3.2f) w=%.2f h=%.2f ar=%.2f",
r.LL.X*userSpaceToInch,
r.LL.Y*userSpaceToInch,
r.UR.X*userSpaceToInch,
r.UR.Y*userSpaceToInch,
r.Width()*userSpaceToInch,
r.Height()*userSpaceToInch,
r.AspectRatio())
}
func (r Rectangle) formatToCentimetres() string {
return fmt.Sprintf("(%3.2f, %3.2f, %3.2f, %3.2f) w=%.2f h=%.2f ar=%.2f",
r.LL.X*userSpaceToCm,
r.LL.Y*userSpaceToCm,
r.UR.X*userSpaceToCm,
r.UR.Y*userSpaceToCm,
r.Width()*userSpaceToCm,
r.Height()*userSpaceToCm,
r.AspectRatio())
}
func (r Rectangle) formatToMillimetres() string {
return fmt.Sprintf("(%3.2f, %3.2f, %3.2f, %3.2f) w=%.2f h=%.2f ar=%.2f",
r.LL.X*userSpaceToMm,
r.LL.Y*userSpaceToMm,
r.UR.X*userSpaceToMm,
r.UR.Y*userSpaceToMm,
r.Width()*userSpaceToMm,
r.Height()*userSpaceToMm,
r.AspectRatio())
}
// Format returns r's details converted into unit.
func (r Rectangle) Format(unit DisplayUnit) string {
switch unit {
case INCHES:
return r.formatToInches()
case CENTIMETRES:
return r.formatToCentimetres()
case MILLIMETRES:
return r.formatToMillimetres()
}
return r.String()
}
// 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 float64) *Rectangle {
return Rect(0.0, 0.0, width, height)
}
// RectForWidthAndHeight returns a new rectangle for given dimensions.
func RectForWidthAndHeight(llx, lly, width, height float64) *Rectangle {
return Rect(llx, lly, llx+width, lly+height)
}
// RectForFormat returns a new rectangle for given format.
func RectForFormat(f string) *Rectangle {
d := PaperSize[f]
return RectForDim(d.Width, d.Height)
}
///////////////////////////////////////////////////////////////////////////////////
// Name represents a PDF name object.
type Name string
// Clone returns a clone of nameObject.
func (nameObject Name) Clone() Object {
return nameObject
}
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 {
s := string(nameObject)
var b bytes.Buffer
for i := 0; i < len(s); {
c := s[i]
if c != '#' {
b.WriteByte(c)
i++
continue
}
// # detected, next 2 chars have to exist.
// This gets checked during parsing.
s1 := s[i+1 : i+3]
b1, _ := hex.DecodeString(s1)
b.WriteByte(b1[0])
i += 3
}
return b.String()
}
///////////////////////////////////////////////////////////////////////////////////
// StringLiteral represents a PDF string literal object.
type StringLiteral string
// Clone returns a clone of stringLiteral.
func (stringliteral StringLiteral) Clone() Object {
return stringliteral
}
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)
}
///////////////////////////////////////////////////////////////////////////////////
// HexLiteral represents a PDF hex literal object.
type HexLiteral string
// NewHexLiteral creates a new HexLiteral for b..
func NewHexLiteral(b []byte) HexLiteral {
return HexLiteral(hex.EncodeToString(b))
}
// Clone returns a clone of hexliteral.
func (hexliteral HexLiteral) Clone() Object {
return hexliteral
}
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)}
}
// Clone returns a clone of ir.
func (ir IndirectRef) Clone() Object {
ir2 := ir
return ir2
}
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
}
/////////////////////////////////////////////////////////////////////////////////////
// DisplayUnit is the metric unit used to output paper sizes.
type DisplayUnit int
// Options for display unit in effect.
const (
POINTS DisplayUnit = iota
INCHES
CENTIMETRES
MILLIMETRES
)
const (
userSpaceToInch = float64(1) / 72
userSpaceToCm = 2.54 / 72
userSpaceToMm = userSpaceToCm * 10
inchToUserSpace = 1 / userSpaceToInch
cmToUserSpace = 1 / userSpaceToCm
mmToUserSpace = 1 / userSpaceToMm
)
func toUserSpace(f float64, unit DisplayUnit) float64 {
switch unit {
case INCHES:
return f * inchToUserSpace
case CENTIMETRES:
return f * cmToUserSpace
case MILLIMETRES:
return f * mmToUserSpace
}
return f
}
// Dim represents the dimensions of a rectangular view medium
// like a PDF page, a sheet of paper or an image grid
// in user space, inches, centimetres or millimetres.
type Dim struct {
Width, Height float64
}
// ToInches converts d to inches.
func (d Dim) ToInches() Dim {
return Dim{d.Width * userSpaceToInch, d.Height * userSpaceToInch}
}
// ToCentimetres converts d to centimetres.
func (d Dim) ToCentimetres() Dim {
return Dim{d.Width * userSpaceToCm, d.Height * userSpaceToCm}
}
// ToMillimetres converts d to centimetres.
func (d Dim) ToMillimetres() Dim {
return Dim{d.Width * userSpaceToMm, d.Height * userSpaceToMm}
}
// AspectRatio returns the relation between width and height.
func (d Dim) AspectRatio() float64 {
return d.Width / d.Height
}
// Landscape returns true if d is in landscape mode.
func (d Dim) Landscape() bool {
return d.AspectRatio() > 1
}
// Portrait returns true if d is in portrait mode.
func (d Dim) Portrait() bool {
return d.AspectRatio() < 1
}
func (d Dim) String() string {
return fmt.Sprintf("%fx%f points", d.Width, d.Height)
}