/
text_tools.go
278 lines (262 loc) · 6.2 KB
/
text_tools.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
// Copyright (c) 2016, redstarcoder
package widgets
import (
"errors"
"log"
"strings"
"time"
"github.com/go-gl/gl/v2.1/gl"
"github.com/golang/freetype/truetype"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dbase"
"github.com/llgcode/draw2d/draw2dgl"
"golang.org/x/image/math/fixed"
)
// FIXME Do away with this function
// BUG(x) loadCurrentFont should take a GraphicContext interface
func loadCurrentFont(gc *draw2dgl.GraphicContext) (*truetype.Font, error) {
font := draw2d.GetFont(gc.GetFontData())
if font == nil {
font = draw2d.GetFont(draw2dbase.DefaultFontData)
}
if font == nil {
return nil, errors.New("No font set, and no default font available.")
}
gc.SetFont(font)
gc.SetFontSize(gc.GetFontSize())
return font, nil
}
func fUnitsToFloat64(x fixed.Int26_6) float64 {
scaled := x << 2
return float64(scaled/256) + float64(scaled%256)/256.0
}
// fillStringAtWidth draws the text at the specified point (x, y), stopping before width is exceeded
func fillStringAtWidth(_gc draw2d.GraphicContext, text string, x, y, width float64) float64 {
gc := _gc.(*draw2dgl.GraphicContext)
f, err := loadCurrentFont(gc)
if err != nil {
log.Println(err)
return 0.0
}
startx := x
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
for _, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
if x+glyph.Width-startx > width {
break
}
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
return x - startx
}
// fillStringAtWidthCursor draws the text at the specified point (x, y), stopping before width is exceeded.
func fillStringAtWidthCursor(_gc draw2d.GraphicContext, c *Cursor, x, y, width float64) {
gc := _gc.(*draw2dgl.GraphicContext)
var last_i int = 1
f, err := loadCurrentFont(gc)
if err != nil {
log.Println(err)
return
}
startx := x
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
cx := x
cindex := c.i - c.iOffset
text := c.text[c.iOffset:]
for i, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
if i == cindex {
cx = x + 1
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
if x+glyph.Width-startx > width {
last_i = i
break
}
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
if c.drawCursor {
if cindex == len(text) {
cx = x + 1
}
gl.LineWidth(2)
gc.MoveTo(cx, y-gc.GetFontSize()-1)
gc.LineTo(cx, y)
gc.Stroke()
gl.LineWidth(1)
}
if last_i > 1 {
c.iEdge = last_i + c.iOffset
} else {
if c.iOffset > 0 {
c.iEdge = len(c.text)
} else {
c.iEdge = len(c.text) + 100
}
}
}
// TODO calculate iEdge & iOffset using something like MoveToX
type Cursor struct {
text string // text is the text stored in the field
textLines []string // textLines is the text stored in the field, stored as lines
i, iOffset, iEdge int // i is the position of the text cursor
iY, maxLines int // iY is the y position of i, maxLines is the max visible lines
lastBlink time.Time
drawCursor bool
}
func (c *Cursor) GenLines(_gc draw2d.GraphicContext, width float64) {
gc := _gc.(*draw2dgl.GraphicContext)
f, err := loadCurrentFont(gc)
if err != nil {
log.Println(err)
return
}
x := float64(3)
ii := 0
lastLine := 0
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
c.textLines = make([]string, 0, 127)
for i, r := range c.text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
if r == '\n' || x+glyph.Width > width {
c.textLines = append(c.textLines, string(c.text[lastLine:lastLine+ii]))
ii = 0
x = 3
if r != '\n' {
lastLine = i
} else {
lastLine = i + 1
}
prev, hasPrev = truetype.Index(0), false
}
if r != '\n' {
ii++
x += glyph.Width
prev, hasPrev = index, true
}
}
if ii != 0 {
c.textLines = append(c.textLines, string(c.text[lastLine:lastLine+ii]))
}
}
func (c *Cursor) Blink() (redraw bool) {
if c.i > c.iEdge {
c.iOffset += c.i - c.iEdge
c.iEdge = c.i
redraw = true
}
if now := time.Now(); now.Sub(c.lastBlink) >= time.Millisecond*667 {
c.lastBlink = now
c.drawCursor = c.drawCursor == false
redraw = true
}
return
}
func (c *Cursor) Insert(s string) {
c.text = strings.Join([]string{c.text[:c.i], s, c.text[c.i:]}, "")
c.MoveRight()
}
// GenLines must be called after
func (c *Cursor) InsertLine(s string) {
c.text = strings.Join(c.textLines, "\n") + "\n" + s
}
func (c *Cursor) Backspace() bool {
if c.i == 0 {
return false
}
c.text = strings.Join([]string{c.text[:c.i-1], c.text[c.i:]}, "")
if c.iOffset > 0 && len(c.text)+1 == c.iEdge {
c.i--
c.iEdge--
c.iOffset--
c.drawCursor = true
} else {
c.MoveLeft()
}
return true
}
// TODO Make MoveLeft and MoveRight take a parameter
func (c *Cursor) MoveLeft() bool {
if c.i > 0 {
c.i--
if c.i < c.iOffset {
c.iEdge--
c.iOffset--
}
c.drawCursor = true
return true
}
return false
}
func (c *Cursor) MoveRight() bool {
if c.i < len(c.text) {
c.i++
if c.i > c.iEdge {
c.iEdge++
c.iOffset++
}
c.drawCursor = true
return true
}
return false
}
func (c *Cursor) MoveTo(i int) bool {
if i > c.i {
for c.i < i {
if !c.MoveRight() {
break
}
}
return true
} else if i < c.i {
for c.i > i {
if !c.MoveLeft() {
break
}
}
return true
}
return false
}
func (c *Cursor) MoveToX(_gc draw2d.GraphicContext, x, mx, width float64) {
gc := _gc.(*draw2dgl.GraphicContext)
f, err := loadCurrentFont(gc)
if err != nil {
log.Println(err)
return
}
prev, hasPrev := truetype.Index(0), false
width += x
fontName := gc.GetFontName()
text := c.text[c.iOffset:]
for i, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
if x+glyph.Width > mx || x+glyph.Width > width {
c.i = i + c.iOffset
return
}
x += glyph.Width
prev, hasPrev = index, true
}
c.i = len(text) + c.iOffset
}