Improved accuracy of type setter

This commit is contained in:
Sasha Koshka 2023-02-15 20:16:49 -05:00
parent a0e7bf1373
commit bd55b6c17d
4 changed files with 56 additions and 6 deletions

View File

@ -127,7 +127,7 @@ func DoLine (text []rune, face font.Face, maxWidth fixed.Int26_6) (line LineLayo
}
lastRune = word.LastRune()
word.X = x
x += word.Width
x += word.Width + word.SpaceAfter
// if we have gone over the maximum width, stop processing
// words (if maxWidth is even specified)

View File

@ -18,6 +18,10 @@ func TestDoWord (test *testing.T) {
if len(word.Runes) != 4 {
test.Fatalf(`wrong rune length %d`, len(word.Runes))
}
if word.SpaceAfter != fixed.I(7) {
test.Fatalf(`wrong space after %d`, word.SpaceAfter.Round())
}
if word.FirstRune() != 'T' {
test.Fatalf(`wrong first rune %s`, string(word.FirstRune()))

View File

@ -37,11 +37,12 @@ func (setter *TypeSetter) needLayout () {
horizontalExtent := fixed.Int26_6(0)
horizontalExtentSpace := fixed.Int26_6(0)
metrics := setter.face.Metrics()
remaining := setter.text
y := fixed.Int26_6(0)
maxY := fixed.I(setter.maxHeight) + metrics.Height
for len(remaining) > 0 && y < maxY {
metrics := setter.face.Metrics()
remaining := setter.text
y := fixed.Int26_6(0)
maxY := fixed.I(setter.maxHeight) + metrics.Height
for len(remaining) > 0 && (y < maxY || setter.maxHeight == 0) {
// process one line
line, remainingFromLine := DoLine (
remaining, setter.face, fixed.I(setter.maxWidth))
@ -73,6 +74,7 @@ func (setter *TypeSetter) needLayout () {
setter.layoutBoundsSpace.Max.X = setter.maxWidth
}
y -= metrics.Height
if setter.maxHeight == 0 {
setter.layoutBounds.Min.Y = -metrics.Ascent.Round()
setter.layoutBounds.Max.Y =

44
textdraw/setter_test.go Normal file
View File

@ -0,0 +1,44 @@
package textdraw
import "testing"
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/sashakoshka/tomo/defaultfont"
func TestSetterLength (test *testing.T) {
text := []rune("The quick brown fox\njumped over the lazy dog.")
setter := TypeSetter { }
setter.SetText(text)
setter.SetFace(defaultfont.FaceRegular)
length := 0
setter.For (func (i int, r rune, p fixed.Point26_6) bool {
length ++
return true
})
if length != len(text) - 1 {
test.Fatalf (
`setter rune count: %d, expected: %d`,
length, len(text) - 1)
}
}
func TestSetterBounds (test *testing.T) {
text := []rune("The quick brown fox\njumped over the lazy dog.")
setter := TypeSetter { }
setter.SetText(text)
setter.SetFace(defaultfont.FaceRegular)
bounds := setter.LayoutBounds()
expectDy := 13 * 2
if expectDy != bounds.Dy() {
test.Fatalf (
`setter bounds Dy: %d, expected: %d`,
bounds.Dy(), expectDy)
}
expectDx := 7 * 25
if expectDx != bounds.Dx() {
test.Fatalf (
`setter bounds Dx: %d, expected: %d`,
bounds.Dx(), expectDx)
}
}