-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathframe.go
212 lines (180 loc) · 3.64 KB
/
frame.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
package frame
import (
"errors"
"image"
"image/draw"
"github.com/as/font"
"github.com/as/frame/box"
)
const (
FrElastic = 1 << iota
FrUTF8
)
var (
ForceElastic bool
ForceUTF8 bool
)
var (
ErrBadDst = errors.New("bad dst")
)
// Frame is a write-only container for editable text
type Frame struct {
box.Run
p0 int64
p1 int64
b draw.Image
r image.Rectangle
ir *box.Run
Face font.Face
Color
Ticked bool
Scroll func(int)
Drawer
op draw.Op
mintab int
maxtab int
full int
tick draw.Image
tickback draw.Image
tickoff bool
maxlines int
modified bool
pts [][2]image.Point
flags int
}
func New(dst draw.Image, r image.Rectangle, conf *Config) *Frame {
if dst == nil {
return nil
}
if conf == nil {
conf = &Config{}
}
conf.check()
fl := conf.Flag
face := negotiateFace(conf.Face, fl)
mintab, maxtab := tabMinMax(face, fl&FrElastic != 0)
f := &Frame{
Face: face,
Color: conf.Color,
Drawer: conf.Drawer,
Run: box.NewRun(mintab, 5000, face),
op: draw.Src,
mintab: mintab,
maxtab: maxtab,
flags: fl,
}
f.setrects(r, dst)
f.inittick()
run := box.NewRun(mintab, 5000, face)
f.ir = &run
return f
}
func (f *Frame) Config() *Config {
return &Config{
Flag: f.flags,
Color: f.Color,
Face: f.Face,
Drawer: f.Drawer,
}
}
var zc Color
// Flags returns the flags currently set for the frame
func (f *Frame) Flags() int {
return f.flags
}
// Flag sets the flags for the frame. At this time
// only FrElastic is supported.
func (f *Frame) SetFlags(flags int) {
fl := getflag(flags)
f.flags = fl
f.mintab, f.maxtab = tabMinMax(f.Face, f.elastic())
// f.Reset( f.r, f.RGBA(),f.Font)
// f.mintab, f.maxtab = tabMinMax(f.Font, f.elastic())
}
func (f *Frame) elastic() bool {
return f.flags&FrElastic != 0
}
func tabMinMax(ft font.Face, elastic bool) (min, max int) {
mintab := ft.Dx([]byte{' '})
maxtab := mintab * 4
if elastic {
mintab = maxtab
}
return mintab, maxtab
}
func (f *Frame) RGBA() *image.RGBA {
rgba, _ := f.b.(*image.RGBA)
return rgba
}
func (f *Frame) Size() image.Point {
return f.r.Size()
}
// Dirty returns true if the contents of the frame have changes since the last redraw
func (f *Frame) Dirty() bool {
return f.modified
}
// SetDirty alters the frame's internal state
func (f *Frame) SetDirty(dirty bool) {
f.modified = dirty
}
func (f *Frame) SetFont(ft font.Face) {
f.Face = font.Open(ft)
f.Run.Reset(f.Face)
f.Refresh()
}
func (f *Frame) SetOp(op draw.Op) {
f.op = op
}
// Close closes the frame
func (f *Frame) Close() error {
return nil
}
// Reset resets the frame to display on image b with bounds r and font ft.
func (f *Frame) Reset(r image.Rectangle, b *image.RGBA, ft font.Face) {
f.r = r
f.b = b
f.SetFont(ft)
}
// Bounds returns the frame's clipping rectangle
func (f *Frame) Bounds() image.Rectangle {
return f.r.Bounds()
}
// Full returns true if the last line in the frame is full.
func (f *Frame) Full() bool {
if f == nil {
return true
}
return f.full == 1
}
// Maxline returns the max number of wrapped lines fitting on the frame
func (f *Frame) MaxLine() int {
if f == nil {
return 0
}
return f.maxlines
}
// Line returns the number of wrapped lines currently in the frame
func (f *Frame) Line() int {
if f == nil {
return 0
}
return f.Nlines
}
// Len returns the number of bytes currently in the frame
func (f *Frame) Len() int64 {
if f == nil {
return 0
}
return f.Nchars
}
// Dot returns the range of the selected text
func (f *Frame) Dot() (p0, p1 int64) {
return f.p0, f.p1
}
func (f *Frame) setrects(r image.Rectangle, b draw.Image) {
f.b = b
f.r = r
h := f.Face.Dy()
f.r.Max.Y -= f.r.Dy() % h
f.maxlines = f.r.Dy() / h
}