From 8a22afe95acd916ebd6e9d5a55b7a1bc686c1267 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 6 Sep 2024 01:00:28 -0400 Subject: [PATCH] Restructure alignment process --- layout.go | 50 ++++++++++++++++++++++++++++++++++---------------- setter.go | 2 +- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/layout.go b/layout.go index d5f45f0..6657947 100644 --- a/layout.go +++ b/layout.go @@ -114,6 +114,9 @@ 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 { } @@ -170,30 +173,45 @@ func (line *LineLayout) Length () int { // Align aligns the text in the line according to the specified alignment // method. -func (line *LineLayout) Align (align Align) { +func (line *LineLayout) Align (align Align, tabWidth fixed.Int26_6) { if len(line.Words) == 0 { return } if align == AlignEven { - line.justify() - return + line.justify(tabWidth) + } else { + line.contract(tabWidth) + + var leftOffset fixed.Int26_6 + if align == AlignMiddle { + leftOffset = (line.Width - line.ContentWidth) / 2 + } else if align == AlignEnd { + leftOffset = line.Width - line.ContentWidth + } + + for index := range line.Words { + line.Words[index].X += leftOffset + } } - leftOffset := -line.Words[0].X - - if align == AlignMiddle { - leftOffset += (line.Width - line.ContentWidth) / 2 - } else if align == AlignEnd { - leftOffset += line.Width - line.ContentWidth - } - - for index := range line.Words { - line.Words[index].X += leftOffset - } } -func (line *LineLayout) justify () { +// assume line has content > 0 +func (line *LineLayout) contract (tabWidth fixed.Int26_6) { + x := fixed.Int26_6(0) + for _, word := range line.Words { + word.X = x + x += word.Width + x += word.SpaceAfter + } + lastWord := line.Words[len(line.Words) - 1] + line.ContentWidth = lastWord.X + lastWord.Width + line.SpaceAfter = lastWord.SpaceAfter +} + +// assume line has content > 0 +func (line *LineLayout) justify (tabWidth fixed.Int26_6) { if len(line.Words) < 2 { - line.Align(AlignStart) + line.Align(AlignStart, tabWidth) return } diff --git a/setter.go b/setter.go index f487694..6176a4f 100644 --- a/setter.go +++ b/setter.go @@ -118,7 +118,7 @@ func (setter *TypeSetter) alignHorizontally () { } // align line - setter.lines[index].Align(align) + setter.lines[index].Align(align, setter.tabWidth) } }