-
Notifications
You must be signed in to change notification settings - Fork 6
/
draw.go
154 lines (138 loc) · 3.5 KB
/
draw.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
package frame
import (
"image"
"github.com/as/frame/box"
)
// Refresh renders the entire frame, including the underlying
// bitmap. Refresh should not be called after insertion and deletion
// unless the frame's RGBA bitmap was painted over by another
// draw operation.
func (f *Frame) Refresh() {
cols := f.Color
if f.p0 == f.p1 {
ticked := f.Ticked
if ticked {
f.tickat(f.PointOf(f.p0), false)
}
f.drawsel(f.PointOf(0), 0, f.Nchars, cols.Back, cols.Text)
if ticked {
f.tickat(f.PointOf(f.p0), true)
}
return
}
pt := f.PointOf(0)
pt = f.drawsel(pt, 0, f.p0, cols.Back, cols.Text)
pt = f.drawsel(pt, f.p0, f.p1, cols.Hi.Back, cols.Hi.Text)
f.drawsel(pt, f.p1, f.Nchars, cols.Back, cols.Text)
}
// RedrawAt renders the frame's bitmap starting at pt and working downwards.
func (f *Frame) RedrawAt(pt image.Point, text, back image.Image) {
f.redrawRun0(&(f.Run), pt, text, back)
}
// Redraw draws the range [p0:p1] at the given pt.
func (f *Frame) Redraw(pt image.Point, p0, p1 int64, issel bool) {
if f.Ticked {
f.tickat(f.PointOf(f.p0), false)
}
if p0 == p1 {
f.tickat(pt, issel)
return
}
pal := f.Color.Palette
if issel {
pal = f.Color.Hi
}
f.drawsel(pt, p0, p1, pal.Back, pal.Text)
}
// Recolor redraws the range p0:p1 with the given palette
func (f *Frame) Recolor(pt image.Point, p0, p1 int64, cols Palette) {
f.drawsel(pt, p0, p1, cols.Back, cols.Text)
f.modified = true
}
func (f *Frame) redrawRun0(r *box.Run, pt image.Point, text, back image.Image) image.Point {
nb := 0
for ; nb < r.Nbox; nb++ {
b := &r.Box[nb]
pt = f.wrapMax(pt, b)
//if !f.noredraw && b.nrune >= 0 {
if b.Nrune >= 0 {
f.StringBG(f.b, pt, text, image.ZP, f.Face, b.Ptr, back, image.ZP)
}
pt.X += b.Width
}
return pt
}
// widthBox returns the width of box n. If the length of
// alt is different than the box, alt is measured and
// returned instead.
func (f *Frame) widthBox(b *box.Box, alt []byte) int {
if b.Nrune < 0 || len(alt) == b.Len() {
return b.Width
}
return f.Face.Dx(alt)
}
func (f *Frame) drawsel(pt image.Point, p0, p1 int64, back, text image.Image) image.Point {
{
// doubled
p0, p1 := int(p0), int(p1)
q0 := 0
trim := false
var (
nb int
b *box.Box
)
for nb = 0; nb < f.Nbox; nb++ {
b = &f.Box[nb]
l := q0 + b.Len()
if l > p0 {
break
}
q0 = l
}
// Step into box, start coloring it
// How much does this lambda slow things down?
stepFill := func() {
qt := pt
pt = f.wrapMax(pt, b)
if pt.Y > qt.Y {
r := image.Rect(qt.X, qt.Y, f.r.Max.X, pt.Y)
f.Draw(f.b, r, back, qt, f.op)
}
}
for ; nb < f.Nbox && q0 < p1; nb++ {
b = &f.Box[nb]
if q0 >= p0 { // region 0 or 1 or 2
stepFill()
}
ptr := b.Ptr[:b.Len()]
if q0 < p0 {
// region -1: shift p right inside the selection
ptr = ptr[p0-q0:]
q0 = p0
}
trim = false
if q1 := q0 + len(ptr); q1 > p1 {
// region 1: would draw too much, retract the selection
lim := len(ptr) - (q1 - p1)
ptr = ptr[:lim]
trim = true
}
w := f.widthBox(b, ptr)
if b.Nrune > 0 {
f.Draw(f.b, image.Rect(pt.X, pt.Y, min(pt.X+w, f.r.Max.X), pt.Y+f.Face.Dy()), back, pt, f.op)
f.StringBG(f.b, pt, text, image.ZP, f.Face, ptr, back, image.ZP)
} else {
f.Draw(f.b, image.Rect(pt.X, pt.Y, min(pt.X+w, f.r.Max.X), pt.Y+f.Face.Dy()), back, pt, f.op)
}
pt.X += w
if q0 += len(ptr); q0 > p1 {
break
}
}
if p1 > p0 && nb != 0 && nb < f.Nbox && f.Box[nb-1].Len() > 0 && !trim {
b = &f.Box[nb]
stepFill()
}
return pt
}
}