diff --git a/layout.go b/layout.go index 6657947..1875602 100644 --- a/layout.go +++ b/layout.go @@ -114,17 +114,12 @@ type LineLayout struct { // is crossed. The word which would have crossed over the limit will not be // processed. func DoLine (text []rune, face font.Face, wrap bool, width fixed.Int26_6) (line LineLayout, remaining []rune) { - // TODO: remove all the positioning logic from here to avoid redundancy - // with LineLayout.Align, because that should get called anyways 100% of - // the time to account for tab stops remaining = text x := fixed.Int26_6(0) - lastWord := WordLayout { } isFirstWord := true for { // process one word word, remainingFromWord := DoWord(remaining, face) - word.X = x x += word.Width // if we have gone over the preferred width, stop processing @@ -138,7 +133,6 @@ func DoLine (text []rune, face font.Face, wrap bool, width fixed.Int26_6) (line // if the word actually has contents, add it if word.Runes != nil { - lastWord = word line.Words = append(line.Words, word) } @@ -154,9 +148,12 @@ func DoLine (text []rune, face font.Face, wrap bool, width fixed.Int26_6) (line } // set the width of the line's content. - line.ContentWidth = lastWord.X + lastWord.Width line.Width = width - line.SpaceAfter = lastWord.SpaceAfter + if len(line.Words) > 0 { + lastWord := line.Words[len(line.Words) - 1] + line.ContentWidth = x - lastWord.SpaceAfter + line.SpaceAfter = lastWord.SpaceAfter + } return } @@ -192,16 +189,16 @@ func (line *LineLayout) Align (align Align, tabWidth fixed.Int26_6) { line.Words[index].X += leftOffset } } - } // assume line has content > 0 func (line *LineLayout) contract (tabWidth fixed.Int26_6) { x := fixed.Int26_6(0) - for _, word := range line.Words { + for index, word := range line.Words { word.X = x x += word.Width x += word.SpaceAfter + line.Words[index] = word } lastWord := line.Words[len(line.Words) - 1] line.ContentWidth = lastWord.X + lastWord.Width @@ -210,7 +207,7 @@ func (line *LineLayout) contract (tabWidth fixed.Int26_6) { // assume line has content > 0 func (line *LineLayout) justify (tabWidth fixed.Int26_6) { - if len(line.Words) < 2 { + if len(line.Words) <= 1 { line.Align(AlignStart, tabWidth) return }