diff --git a/layout.go b/layout.go index 6a14459..9816493 100644 --- a/layout.go +++ b/layout.go @@ -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) { diff --git a/setter.go b/setter.go index d92f67c..718fa5a 100644 --- a/setter.go +++ b/setter.go @@ -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 }