diff --git a/layout.go b/layout.go index c763508..6df7c82 100644 --- a/layout.go +++ b/layout.go @@ -201,8 +201,8 @@ func (line *LineLayout) justify () { trueContentWidth += word.Width } - spaceCount := fixed.Int26_6(len(line.Words) - 1) - spacePerWord := (line.Width - trueContentWidth) / spaceCount + spaceCount := len(line.Words) - 1 + spacePerWord := (line.Width - trueContentWidth) / fixed.Int26_6(spaceCount) x := fixed.Int26_6(0) for index, word := range line.Words { line.Words[index].X = x diff --git a/setter.go b/setter.go index a501543..2665a97 100644 --- a/setter.go +++ b/setter.go @@ -83,17 +83,10 @@ func (setter *TypeSetter) needLayout () { setter.layoutBoundsSpace.Max.X = horizontalExtentSpace.Round() y -= metrics.Height - if setter.height == 0 { - setter.layoutBounds.Min.Y = -metrics.Ascent.Round() - setter.layoutBounds.Max.Y = - y.Round() + - metrics.Descent.Round() - } else { - setter.layoutBounds.Min.Y = -metrics.Ascent.Round() - setter.layoutBounds.Max.Y = - setter.height - - metrics.Ascent.Round() - } + setter.layoutBounds.Min.Y = -metrics.Ascent.Round() + setter.layoutBounds.Max.Y = + y.Round() + + metrics.Descent.Round() setter.layoutBoundsSpace.Min.Y = setter.layoutBounds.Min.Y setter.layoutBoundsSpace.Max.Y = setter.layoutBounds.Max.Y } @@ -103,6 +96,13 @@ func (setter *TypeSetter) needAlignedLayout () { setter.needLayout() setter.alignClean = true + setter.alignHorizontally() + setter.alignVertically() +} + +func (setter *TypeSetter) alignHorizontally () { + if len(setter.lines) == 0 { return } + for index := range setter.lines { align := setter.hAlign @@ -120,6 +120,43 @@ func (setter *TypeSetter) needAlignedLayout () { } } +func (setter *TypeSetter) alignVertically () { + if setter.height == 0 { return } + if len(setter.lines) == 0 { return } + if setter.vAlign == AlignEven { + setter.justifyVertically() + return + } + + // determine how much to shift lines + topOffset := 0 + contentHeight := setter.layoutBoundsSpace.Dy() + if setter.vAlign == AlignMiddle { + topOffset += (setter.height - contentHeight) / 2 + } else if setter.vAlign == AlignEnd { + topOffset += setter.height - contentHeight + } + + // shift lines + for index := range setter.lines { + setter.lines[index].Y += fixed.I(topOffset) + } +} + +func (setter *TypeSetter) justifyVertically () { + spaceCount := len(setter.lines) - 1 + contentHeight := setter.layoutBoundsSpace.Dy() + spacePerLine := + fixed.Int26_6(setter.height - contentHeight) / + fixed.Int26_6(spaceCount) + + y := fixed.Int26_6(0) + for index := range setter.lines { + setter.lines[index].Y = y + y += spacePerLine + setter.LineHeight() + } +} + // SetWrap sets whether or not the text wraps around and forms new lines. func (setter *TypeSetter) SetWrap (wrap bool) { if setter.wrap == wrap { return }