-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
201 lines (181 loc) · 4.03 KB
/
builder.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
// Arveto Memo
// BSD 3-Clause License
// Copyright (c) 2020, Arveto Ink, see AUTHOR list file. All rights reserved.
package genpdf
import (
"sort"
)
// Return the length in rune of the string.
func stringLen(s string) (l int) {
for range s {
l++
}
return
}
// Limit the number of rune into a string
func stringLimit(s string, limit int) string {
n := 0 // number of rune
for i := range s { // i is the size in byte
n++
if n > limit {
return s[:i]
}
}
return s
}
// A buffer to split a text into several lines. TabLen and LineLen must be set
// before use. Do not use it in concurence.
type Builder struct {
Lines []string
// The future line is line + space + world
line string
space string // used only if a world comme after.
word string
wordSize int
size int // Len of the line + len of space. All length are in rune.
// Config
TabLen int // width of a tabulation
LineLen int // max line size
PageLen int // The number of line into a page
// Return true if the rune is invalid (For example: the rune desn't have
// a glyph in the font). Can be nil.
RuneCheck func(rune) bool
// The rune to replace unchecked rune
RuneReplace rune
// Runes that do not pass RuneCheck test.
uncheckedRunes map[rune]bool
// Add a string at the begin of each line.
BeginLine func(first bool) string
}
// Add miltiples lines.
func (b *Builder) AddLines(lines ...string) {
for _, l := range lines {
b.AddLine(l)
}
}
// Add one line.
func (b *Builder) AddLine(line string) {
b.CommitLine()
b.WriteString(line)
b.NewLine()
}
// Like WriteString
func (b *Builder) Write(text []byte) (int, error) {
b.WriteString(string(text))
return len(text), nil
}
// Add the text to the buffer. No new line at the end. If it's the last line
// of the document, call Builder.CommitLine()
func (b *Builder) WriteString(text string) {
for _, r := range text {
b.Map(r)
}
}
// Check the rune of map it the add it to the builder.
func (b *Builder) Map(r rune) {
if b.RuneCheck != nil && !b.RuneCheck(r) {
if b.uncheckedRunes == nil {
b.uncheckedRunes = make(map[rune]bool, 1)
}
b.uncheckedRunes[r] = true
b.add(b.RuneReplace)
return
}
b.add(r)
}
func (b *Builder) UncheckedRunes() []rune {
all := make([]rune, 0, len(b.uncheckedRunes))
for r := range b.uncheckedRunes {
all = append(all, r)
}
sort.Slice(all, func(i int, j int) bool { return all[i] < all[j] })
return all
}
func (b *Builder) add(r rune) {
switch r {
case '\t':
if b.wordSize > 0 {
b.commitWorld()
}
n := b.TabLen - b.size%b.TabLen
for i := 0; i < n; i++ {
b.space += string(' ')
}
b.size += n
case ' ':
if b.wordSize > 0 {
b.commitWorld()
}
b.space += string(' ')
b.size++
case '\n':
b.NewLine()
case '\f':
b.CommitLine()
n := b.PageLen - len(b.Lines)%b.PageLen
for i := 0; i < n; i++ {
b.NewLine()
}
default:
b.word += string(r)
b.wordSize++
}
}
// Commit the word and create a new line if not empty
func (b *Builder) CommitLine() {
if b.wordSize > 0 {
b.commitWorld()
if b.size > 0 {
b.NewLine()
}
}
}
// Commit current line and add a new line
func (b *Builder) NewLine() {
if b.wordSize > 0 {
b.commitWorld()
}
b.Lines = append(b.Lines, b.line)
b.line = b.BeginLine(true)
b.space = ""
b.word = ""
b.wordSize = 0
b.size = stringLen(b.line)
}
func (b *Builder) commitWorld() {
if b.size > b.LineLen {
b.Lines = append(b.Lines, b.line)
b.line = b.BeginLine(false)
b.space = ""
b.size = stringLen(b.line)
} else {
b.line += b.space
b.space = ""
}
l := stringLen(b.word)
if l > b.LineLen {
for _, r := range b.word {
b.line += string(r)
b.size++
if b.size > b.LineLen {
b.Lines = append(b.Lines, b.line)
b.line = b.BeginLine(false)
b.word = ""
b.wordSize = 0
b.size = stringLen(b.line)
}
}
} else if b.size+l > b.LineLen {
b.Lines = append(b.Lines, b.line)
b.line = b.BeginLine(false) + b.word
b.space = ""
b.word = ""
b.wordSize = 0
b.size = stringLen(b.line)
} else {
b.line += b.word
b.size += l
b.word = ""
b.wordSize = 0
}
}