TypeSetter can now do vertical text alignment yay

This commit is contained in:
Sasha Koshka 2023-09-15 15:54:46 -04:00
parent 92cb318972
commit 8d9e0e1340
2 changed files with 50 additions and 13 deletions

View File

@ -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

View File

@ -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 }