From a1bd411e433f362f1de077549c9d4dce403a0fbd Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 5 May 2024 02:34:32 -0400 Subject: [PATCH] Fix bug where vertically aligned text would creep downward --- setter.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/setter.go b/setter.go index 2665a97..2682e9e 100644 --- a/setter.go +++ b/setter.go @@ -100,6 +100,7 @@ func (setter *TypeSetter) needAlignedLayout () { setter.alignVertically() } +// should only be called from within setter.needAlignedLayout func (setter *TypeSetter) alignHorizontally () { if len(setter.lines) == 0 { return } @@ -120,6 +121,7 @@ func (setter *TypeSetter) alignHorizontally () { } } +// should only be called from within setter.needAlignedLayout func (setter *TypeSetter) alignVertically () { if setter.height == 0 { return } if len(setter.lines) == 0 { return } @@ -129,20 +131,27 @@ func (setter *TypeSetter) alignVertically () { } // determine how much to shift lines - topOffset := 0 + topOffset := fixed.I(0) contentHeight := setter.layoutBoundsSpace.Dy() if setter.vAlign == AlignMiddle { - topOffset += (setter.height - contentHeight) / 2 + topOffset += fixed.I((setter.height - contentHeight) / 2) } else if setter.vAlign == AlignEnd { - topOffset += setter.height - contentHeight + topOffset += fixed.I(setter.height - contentHeight) + } + + // we may be re-aligning already aligned text. if the text is shifted + // away from the origin, account for that. + if len(setter.lines) > 0 { + topOffset -= setter.lines[0].Y } // shift lines for index := range setter.lines { - setter.lines[index].Y += fixed.I(topOffset) + setter.lines[index].Y += topOffset } } +// should only be called from within setter.alignVertically func (setter *TypeSetter) justifyVertically () { spaceCount := len(setter.lines) - 1 contentHeight := setter.layoutBoundsSpace.Dy()