-
Notifications
You must be signed in to change notification settings - Fork 0
/
lb.go
244 lines (218 loc) · 5.88 KB
/
lb.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
package lb
// Direction is the data-type for word direction.
type Direction int
// List of word direction values.
const (
LeftToRight Direction = iota
RightToLeft
)
// Box is a solid text element (typically, a word).
type Box interface {
// Direction returns the writing direction of the box.
Direction() Direction
// Width returns the extent of the box within the text.
Width() float32
}
// Line is a series of boxes that fit in a line of text.
type Line struct {
// Boxes is the slice of boxes within the line.
Boxes []Box
// GlueWidth is the extent of the glues between the boxes.
GlueWidth float32
}
// Options contains several options passed to the line-breaking algorithm.
type Options struct {
// TextWidth is the extent of a line of text.
TextWidth float32
// TextDirection is the writing direction of the text.
TextDirection Direction
// GlueWidth is the normal extent of the glues between the boxes.
GlueWidth float32
// GlueShrink is the extent that a glue can shrink to.
GlueShrink float32
// GlueExpand is the extent that a glue can expand to.
GlueExpand float32
}
// Greedy is a greedy but fast line-breaking algorithm that tries to fit as much
// boxes as possible in each and every line.
func Greedy(para []Box, opt *Options) (lines []*Line) {
var (
minGlueWidth = opt.GlueWidth - opt.GlueShrink
line = &Line{GlueWidth: opt.GlueWidth}
lineWidth = float32(0)
boxesWidth = float32(0)
numGlues = 0
)
for _, box := range para {
boxWidth := box.Width()
if len(line.Boxes) == 0 {
line.Boxes = append(line.Boxes, box)
lineWidth += boxWidth
boxesWidth += boxWidth
} else if lineWidth+minGlueWidth+boxWidth <= opt.TextWidth {
line.Boxes = append(line.Boxes, box)
lineWidth += minGlueWidth + boxWidth
boxesWidth += boxWidth
numGlues++
} else {
if numGlues > 0 && boxesWidth < opt.TextWidth {
line.GlueWidth = (opt.TextWidth - boxesWidth) / float32(numGlues)
}
bidi(line, opt.TextDirection)
lines = append(lines, line)
line = &Line{GlueWidth: opt.GlueWidth, Boxes: []Box{box}}
lineWidth = boxWidth
boxesWidth = boxWidth
numGlues = 0
}
}
if len(line.Boxes) > 0 {
lines = append(lines, line)
}
return lines
}
// KnuthPlass is a relatively slow line-breaking algorithm but gives the best
// result aesthetically.
func KnuthPlass(para []Box, opt *Options) (lines []*Line) {
cache := &kpCache{
Ends: make(map[int][]int),
Badnesses: make(map[int]float32),
}
ends, _ := knuthPlass(para, opt, cache, 0)
i := 0
for _, end := range ends {
var (
line = &Line{GlueWidth: opt.GlueWidth}
boxesWidth = float32(0)
)
for i < end {
line.Boxes = append(line.Boxes, para[i])
boxesWidth += para[i].Width()
i++
}
if numGlues := len(line.Boxes) - 1; end < len(para) && numGlues > 0 {
line.GlueWidth = (opt.TextWidth - boxesWidth) / float32(numGlues)
}
bidi(line, opt.TextDirection)
lines = append(lines, line)
}
return lines
}
type kpCache struct {
Ends map[int][]int
Badnesses map[int]float32
}
func knuthPlass(para []Box, opt *Options, cache *kpCache, start int) (ends []int, badness float32) {
if ends, ok := cache.Ends[start]; ok {
return ends, cache.Badnesses[start]
}
badness = float32(10_000)
for _, bp := range breakRange(para, opt, start) {
var (
nextBadness float32
nextEnds []int
)
if bp.Index < len(para) {
nextEnds, nextBadness = knuthPlass(para, opt, cache, bp.Index)
}
if b := bp.Badness + nextBadness; b < badness {
badness = bp.Badness + nextBadness
ends = append([]int{bp.Index}, nextEnds...)
}
}
cache.Ends[start] = ends
cache.Badnesses[start] = badness
return ends, badness
}
type breakPoint struct {
Index int
Badness float32
}
func breakRange(para []Box, opt *Options, start int) (bps []breakPoint) {
var (
minGlueWidth = opt.GlueWidth - opt.GlueShrink
maxGlueWidth = opt.GlueWidth + opt.GlueExpand
numGlues = 0
first, last int
minLineWidth float32
maxLineWidth float32
boxesWidth float32
)
for first = start; first < len(para); first++ {
boxWidth := para[first].Width()
if first == start {
minLineWidth = boxWidth
maxLineWidth = boxWidth
boxesWidth = boxWidth
} else if maxLineWidth+maxGlueWidth+boxWidth <= opt.TextWidth {
minLineWidth += minGlueWidth + boxWidth
maxLineWidth += maxGlueWidth + boxWidth
boxesWidth += boxWidth
numGlues++
} else {
badness := float32(0)
if numGlues > 0 {
glueWidth := (opt.TextWidth - boxesWidth) / float32(numGlues)
diff := opt.GlueWidth - glueWidth
badness = diff * diff
}
bps = append(bps, breakPoint{Index: first, Badness: badness})
break
}
}
if len(bps) == 0 {
bps = append(bps, breakPoint{Index: first})
return
}
for last = first; last < len(para); last++ {
boxWidth := para[last].Width()
if minLineWidth+minGlueWidth+boxWidth > opt.TextWidth {
break
}
minLineWidth += minGlueWidth + boxWidth
boxesWidth += boxWidth
numGlues++
badness := float32(0)
if numGlues > 0 && last != len(para)-1 {
glueWidth := (opt.TextWidth - boxesWidth) / float32(numGlues)
diff := opt.GlueWidth - glueWidth
badness = diff * diff
}
bps = append(bps, breakPoint{Index: last + 1, Badness: badness})
}
return bps
}
func bidi(line *Line, textDirection Direction) {
var (
result []Box
stack boxStack
box Box
)
result = make([]Box, 0, len(line.Boxes))
for _, b := range line.Boxes {
if b.Direction() != textDirection {
stack = pushBox(stack, b)
} else {
for !stack.Empty() {
stack, box = popBox(stack)
result = append(result, box)
}
result = append(result, b)
}
}
for !stack.Empty() {
stack, box = popBox(stack)
result = append(result, box)
}
line.Boxes = result
}
type boxStack []Box
func (s boxStack) Empty() bool {
return len(s) == 0
}
func pushBox(s boxStack, b Box) boxStack {
return append(s, b)
}
func popBox(s boxStack) (boxStack, Box) {
return s[:len(s)-1], s[len(s)-1]
}