Fixed TypeSetter.AtPosition

This commit is contained in:
Sasha Koshka 2023-08-06 03:37:08 -04:00
parent c207eadfb8
commit 6407baed13
2 changed files with 19 additions and 15 deletions

View File

@ -155,6 +155,17 @@ func DoLine (text []rune, face font.Face, wrap bool, maxWidth fixed.Int26_6) (li
return
}
// Length returns the amount of runes within the line, including the trailing
// line break if it exists.
func (line *LineLayout) Length () int {
lineSize := 0
for _, word := range line.Words {
lineSize += len(word.Runes)
}
if line.BreakAfter { lineSize ++ }
return lineSize
}
// Align aligns the text in the line according to the specified alignment
// method.
func (line *LineLayout) Align (align Align) {

View File

@ -30,7 +30,6 @@ func (setter *TypeSetter) needLayout () {
setter.layoutClean = true
setter.alignClean = false
// we need to have a font and some text to do anything
setter.lines = nil
setter.layoutBounds = image.Rectangle { }
setter.layoutBoundsSpace = image.Rectangle { }
@ -238,27 +237,21 @@ func (setter *TypeSetter) AtPosition (position fixed.Point26_6) (index int) {
// find the first line who's bottom bound is greater than position.Y. if
// we haven't found it, then dont set the line variable (defaults to the
// last line)
metrics := setter.face.Metrics()
line := setter.lines[len(setter.lines) - 1]
lineSize := 0
for _, curLine := range setter.lines {
for _, curWord := range curLine.Words {
lineSize += len(curWord.Runes)
}
if curLine.BreakAfter { lineSize ++ }
index += lineSize
metrics := setter.face.Metrics()
lastLine := setter.lines[len(setter.lines) - 1]
for _, curLine := range setter.lines {
if curLine.Y + metrics.Descent > position.Y {
line = curLine
lastLine = curLine
break
}
index += curLine.Length()
}
index -= lineSize
if line.Words == nil { return }
if lastLine.Words == nil { return }
// find the first rune who's right bound is greater than position.X.
for _, curWord := range line.Words {
for _, curWord := range lastLine.Words {
for _, curChar := range curWord.Runes {
x := curWord.X + curChar.X + curChar.Width
if x > position.X { goto foundRune }