Add AtPosition to TypeSetter

This commit is contained in:
Sasha Koshka 2024-09-20 00:38:23 -04:00
parent 76bd2837d3
commit f386684454

View File

@ -89,6 +89,34 @@ type TypeSetter struct {
layoutBoundsSpace fixed.Rectangle26_6
}
// AtPosition returns the index of the rune at the specified position.
func (this *TypeSetter) AtPosition (position fixed.Point26_6) int {
metrics := this.face.Metrics()
lastValidIndex := 0
index := 0
for _, tok := range this.tokens {
pos := tok.position
yValid :=
position.Y >= pos.Y - metrics.Ascent &&
position.Y <= pos.Y + metrics.Descent
if !yValid { index += len(tok.runes); continue }
for _, runl := range tok.runes {
x := pos.X + runl.x
xValid := position.X >= pos.X + runl.x
if xValid {
lastValidIndex = index
} else if x > position.X {
return lastValidIndex
}
index ++
}
}
index ++
return lastValidIndex
}
// Runes returns an iterator for all runes in the TypeSetter, and their positions.
func (this *TypeSetter) Runes () RuneIter {
this.needFlow()