-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindexof.go
48 lines (44 loc) · 812 Bytes
/
indexof.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
package frame
import (
"image"
)
// IndexOf returns the chracter index under the
// point pt.
func (f *Frame) IndexOf(pt image.Point) (p int64) {
pt.X += 1
return f.indexOf(pt)
}
func (f *Frame) indexOf(pt image.Point) (p int64) {
pt = f.grid(pt)
qt := f.r.Min
bn := 0
for ; bn < f.Nbox && qt.Y < pt.Y; bn++ {
b := &f.Box[bn]
qt = f.wrapMax(qt, b)
if qt.Y >= pt.Y {
break
}
qt = f.advance(qt, b)
p += int64(b.Len())
}
for ; bn < f.Nbox && qt.X <= pt.X; bn++ {
b := &f.Box[bn]
qt = f.wrapMax(qt, b)
if qt.Y > pt.Y {
break
}
if qt.X+b.Width > pt.X {
if b.Nrune < 0 {
qt = f.advance(qt, b)
} else {
left := pt.X - qt.X
p += int64(f.Face.Fits(b.Ptr, left))
qt.X += left
}
} else {
p += int64(b.Len())
qt = f.advance(qt, b)
}
}
return p
}