-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwriteutils.go
133 lines (115 loc) · 2.98 KB
/
writeutils.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
package model
import (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"time"
)
// FmtFloat returns a PDF compatible float representation of `f`.
func FmtFloat(f Fl) string {
// avoid to represent 0 as -0
if f == 0 {
return "0"
}
// Round rounds f with 12 digits precision
n := math.Pow10(5)
f_ := math.Round(float64(f)*n) / n
return strconv.FormatFloat(f_, 'f', -1, 32)
}
func writeMaybeFloat(f MaybeFloat) string {
if f == nil {
return "null"
}
return FmtFloat(Fl(f.(ObjFloat)))
}
func writeIntArray(as []int) string {
b := make([]string, len(as))
for i, a := range as {
b[i] = strconv.Itoa(a)
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
func writeFloatArray(as []Fl) string {
b := make([]string, len(as))
for i, a := range as {
b[i] = FmtFloat(a)
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
func writeRefArray(as []Reference) string {
b := make([]string, len(as))
for i, ref := range as {
b[i] = ref.String()
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
func writePointArray(rs [][2]Fl) string {
b := make([]string, len(rs))
for i, a := range rs {
b[i] = fmt.Sprintf("%s %s ", FmtFloat(a[0]), FmtFloat(a[1]))
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
func writeRangeArray(rs []Range) string {
b := make([]string, len(rs))
for i, a := range rs {
b[i] = fmt.Sprintf("%s %s", FmtFloat(a[0]), FmtFloat(a[1]))
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
func writePointsArray(rs [][2]Fl) string {
b := make([]string, len(rs))
for i, a := range rs {
b[i] = fmt.Sprintf("%s %s ", FmtFloat(a[0]), FmtFloat(a[1]))
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
func writeNameArray(rs []Name) string {
b := make([]string, len(rs))
for i, a := range rs {
b[i] = a.String()
}
return fmt.Sprintf("[%s]", strings.Join(b, " "))
}
// DateTimeString returns a valid PDF string representation of `t`.
// Note that the string is not encoded (or crypted).
func DateTimeString(t time.Time) string {
_, tz := t.Zone()
tzm := tz / 60
sign := "+"
if tzm < 0 {
sign = "-"
tzm = -tzm
}
return fmt.Sprintf("D:%d%02d%02d%02d%02d%02d%s%02d'%02d'",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(),
sign, tzm/60, tzm%60)
}
func (pdf pdfWriter) dateString(t time.Time, context Reference) string {
return pdf.EncodeString(DateTimeString(t), TextString, context)
}
func writeStringsArray(ar []string, pdf PDFWritter, mode PDFStringEncoding, context Reference) string {
chunks := make([]string, len(ar))
for i, val := range ar {
chunks[i] = pdf.EncodeString(val, mode, context)
}
return fmt.Sprintf("[%s]", strings.Join(chunks, " "))
}
// helper to shorten the writting of formatted strings
type buffer struct {
*bytes.Buffer
}
func newBuffer() buffer {
return buffer{Buffer: &bytes.Buffer{}}
}
func (b buffer) fmt(format string, arg ...interface{}) {
fmt.Fprintf(b.Buffer, format, arg...)
}
// add a formatted line
func (b buffer) line(format string, arg ...interface{}) {
b.fmt(format, arg...)
b.WriteByte('\n')
}