/
textbox.go
267 lines (231 loc) · 5.63 KB
/
textbox.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
package textbox
import (
"bytes"
"errors"
"unicode/utf8"
)
// Point is a 2d coordinate
type Point struct {
X, Y int
}
// Rect defines a rectangle in a 2d-plane by its position and size
type Rect struct {
Origin Point
Width int
Height int
}
// Textbox is a canvas of runes of a defined dimension
type Textbox struct {
canvas [][]rune
pixels []rune
width int
height int
cursor int
}
// NewTextbox returns a textbox with dimentions widht and height
func NewTextbox(width, height int) *Textbox {
tb := new(Textbox)
tb.width = int(abs(int64(width)))
tb.height = int(abs(int64(height)))
tb.canvas = make([][]rune, height)
tb.pixels = make([]rune, width*height)
index := 0
for i := range tb.canvas {
tb.canvas[i] = tb.pixels[index : index+width]
index += width
}
return tb
}
// FromStrings creates a new textbox exactly large enough to contain strs
func FromStrings(strs ...string) *Textbox {
width, height := 0, len(strs)
for _, s := range strs {
if len(s) > width {
width = len(s)
}
}
tb := NewTextbox(width, height)
for i, s := range strs {
copy(tb.canvas[i], []rune(s))
}
return tb
}
// Runes returns the contents of the textbox as a slice of runes
func (tb *Textbox) Runes() []rune {
runes := make([]rune, len(tb.pixels))
copy(runes, tb.pixels)
return runes
}
// String returns the contents of the textbox as a string
func (tb *Textbox) String() string {
buf := new(bytes.Buffer)
for i := range tb.canvas {
buf.WriteString(string(tb.canvas[i]))
buf.WriteByte('\n')
}
return buf.String()
}
// Bytes returns the contense of the textbox as a []byte
func (tb *Textbox) Bytes() []byte {
return []byte(string(tb.pixels))
}
// Size returns the textbox dimensions
func (tb *Textbox) Size() (width, height int) {
return tb.width, tb.height
}
// Cursor returns the current cursor location
func (tb *Textbox) Cursor() (x, y int) {
return tb.cursor % len(tb.canvas[0]), tb.cursor / len(tb.canvas[0])
}
// SetCursor moves the cursor to the x, y location
func (tb *Textbox) SetCursor(x, y int) error {
if x < 0 || x > tb.width ||
y < 0 || y > tb.height {
return errors.New("location out of bounds")
}
tb.cursor = (y * len(tb.canvas[0])) + x
return nil
}
func (tb *Textbox) Write(b []byte) (int, error) {
return tb.WriteRunes(bytes.Runes(b))
}
// WriteRunes writes r into the textbox at the cursor location
func (tb *Textbox) WriteRunes(r []rune) (int, error) {
if tb.cursor >= len(tb.pixels) {
return 0, errors.New("textbox full")
}
n := copy(tb.pixels[tb.cursor:], r)
tb.cursor += n
return n, nil
}
// WriteString writes s into the textbox at the cursor location
func (tb *Textbox) WriteString(s string) (int, error) {
return tb.WriteRunes([]rune(s))
}
// Draw tbox into tb at the offset given by p
func (tb *Textbox) Draw(tbox *Textbox, p Point, transparents []rune) (int, error) {
count := 0
// check for complete out if bounds
if tbox.width+p.X <= 0 || tbox.height+p.Y <= 0 {
return count, nil
}
// set crop point on foreground textbox
cropPoint := Point{0, 0}
if p.X < 0 {
cropPoint.X = int(abs(int64(p.X)))
p.X = 0
}
if p.Y < 0 {
cropPoint.Y = int(abs(int64(p.Y)))
p.Y = 0
}
// draw foreground textbox (tb) into background textbox (t)
xIndex := p.X
if transparents == nil {
// without transparency
for i := range tbox.canvas[cropPoint.Y:] {
yIndex := p.Y + i
if yIndex >= tb.height {
break
}
count += copy(tb.canvas[yIndex][xIndex:], tbox.canvas[cropPoint.Y+i][cropPoint.X:])
}
} else {
// with transparency
invisible := make(map[rune]bool)
for _, r := range transparents {
invisible[r] = true
}
for i := range tbox.canvas[cropPoint.Y:] {
yIndex := p.Y + i
if yIndex >= tb.height {
break
}
for j := range tbox.canvas[i][cropPoint.X:] {
if xIndex+j >= tb.width {
break
}
if !invisible[tbox.canvas[cropPoint.Y+i][cropPoint.X+j]] {
tb.canvas[yIndex][xIndex+j] = tbox.canvas[cropPoint.Y+i][cropPoint.X+j]
}
count++
}
}
}
// set cursor
tb.SetCursor(
min(xIndex+tbox.width-cropPoint.X-1, tb.width-1),
min(p.Y+tbox.height-cropPoint.Y-1, tb.height-1))
tb.cursor++
return count, nil
}
// Tile fills tb by drawing adjacent copies of tbox into it
func (tb *Textbox) Tile(tbox *Textbox, transparents []rune) (int, error) {
count := 0
var err error
p := Point{0, 0}
for {
start := p
n, err := tb.Draw(tbox, p, transparents)
count += n
if err != nil || tb.cursor >= len(tb.pixels) {
break
}
p.X, p.Y = tb.Cursor()
if p.X != 0 {
p.Y = start.Y
}
}
return count, err
}
// Fill every rune in textbox with 'u'
func (tb *Textbox) Fill(u rune) error {
if !utf8.ValidRune(u) {
return errors.New("invalid rune")
}
for i := range tb.pixels {
tb.pixels[i] = u
}
return nil
}
// Replace all 'a' runes in textbox with 'b'
func (tb *Textbox) Replace(a, b rune) (int, error) {
if !utf8.ValidRune(a) {
return 0, errors.New("invalid rune")
}
count := 0
for i := range tb.pixels {
if tb.pixels[i] == a {
tb.pixels[i] = b
count++
}
}
return count, nil
}
// Crop returns a new textbox with the contense the crop region
func (tb *Textbox) Crop(r Rect) (*Textbox, error) {
// Check if point is in bounds
err := tb.SetCursor(r.Origin.X, r.Origin.Y)
if err != nil {
return nil, err
}
// crop intersection
cropWidth := min(tb.width-r.Origin.X, r.Width)
cropHeight := min(tb.height-r.Origin.Y, r.Height)
crop := NewTextbox(cropWidth, cropHeight)
for i := range crop.canvas {
copy(crop.canvas[i], tb.canvas[r.Origin.Y+i][r.Origin.X:cropWidth+r.Origin.X])
}
return crop, err
}
// Helper functions
func abs(n int64) int64 {
y := n >> 63
return (n ^ y) - y
}
func min(a, b int) int {
if a < b {
return a
}
return b
}