-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext.go
72 lines (65 loc) · 1.54 KB
/
text.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
package debugui
import (
"image"
"iter"
"strings"
"unicode"
"github.com/rivo/uniseg"
)
func removeSpaceAtLineTail(str string) string {
return strings.TrimRightFunc(str, unicode.IsSpace)
}
func lines(text string, width int) iter.Seq[string] {
return func(yield func(string) bool) {
var line string
var word string
state := -1
for len(text) > 0 {
cluster, nextText, boundaries, nextState := uniseg.StepString(text, state)
switch m := boundaries & uniseg.MaskLine; m {
default:
word += cluster
case uniseg.LineCanBreak, uniseg.LineMustBreak:
if line == "" {
line += word + cluster
} else {
if l := removeSpaceAtLineTail(line + word + cluster); textWidth(l) > width {
if !yield(removeSpaceAtLineTail(line)) {
return
}
line = word + cluster
} else {
line += word + cluster
}
}
word = ""
if m == uniseg.LineMustBreak {
if !yield(removeSpaceAtLineTail(line)) {
return
}
line = ""
}
}
state = nextState
text = nextText
}
line += word
if len(line) > 0 {
if !yield(removeSpaceAtLineTail(line)) {
return
}
}
}
}
// Text creates a text label.
func (c *Context) Text(text string) {
c.GridCell(func(bounds image.Rectangle) {
for line := range lines(text, bounds.Dx()-c.style().padding) {
_, _ = c.widget(emptyWidgetID, 0, nil, nil, func(bounds image.Rectangle) {
c.drawWidgetText(line, bounds, colorText, 0)
})
}
})
}