Improved accuracy of type setter
This commit is contained in:
parent
a0e7bf1373
commit
bd55b6c17d
@ -127,7 +127,7 @@ func DoLine (text []rune, face font.Face, maxWidth fixed.Int26_6) (line LineLayo
|
|||||||
}
|
}
|
||||||
lastRune = word.LastRune()
|
lastRune = word.LastRune()
|
||||||
word.X = x
|
word.X = x
|
||||||
x += word.Width
|
x += word.Width + word.SpaceAfter
|
||||||
|
|
||||||
// if we have gone over the maximum width, stop processing
|
// if we have gone over the maximum width, stop processing
|
||||||
// words (if maxWidth is even specified)
|
// words (if maxWidth is even specified)
|
||||||
|
@ -18,6 +18,10 @@ func TestDoWord (test *testing.T) {
|
|||||||
if len(word.Runes) != 4 {
|
if len(word.Runes) != 4 {
|
||||||
test.Fatalf(`wrong rune length %d`, len(word.Runes))
|
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' {
|
if word.FirstRune() != 'T' {
|
||||||
test.Fatalf(`wrong first rune %s`, string(word.FirstRune()))
|
test.Fatalf(`wrong first rune %s`, string(word.FirstRune()))
|
||||||
|
@ -37,11 +37,12 @@ func (setter *TypeSetter) needLayout () {
|
|||||||
|
|
||||||
horizontalExtent := fixed.Int26_6(0)
|
horizontalExtent := fixed.Int26_6(0)
|
||||||
horizontalExtentSpace := fixed.Int26_6(0)
|
horizontalExtentSpace := fixed.Int26_6(0)
|
||||||
metrics := setter.face.Metrics()
|
|
||||||
remaining := setter.text
|
metrics := setter.face.Metrics()
|
||||||
y := fixed.Int26_6(0)
|
remaining := setter.text
|
||||||
maxY := fixed.I(setter.maxHeight) + metrics.Height
|
y := fixed.Int26_6(0)
|
||||||
for len(remaining) > 0 && y < maxY {
|
maxY := fixed.I(setter.maxHeight) + metrics.Height
|
||||||
|
for len(remaining) > 0 && (y < maxY || setter.maxHeight == 0) {
|
||||||
// process one line
|
// process one line
|
||||||
line, remainingFromLine := DoLine (
|
line, remainingFromLine := DoLine (
|
||||||
remaining, setter.face, fixed.I(setter.maxWidth))
|
remaining, setter.face, fixed.I(setter.maxWidth))
|
||||||
@ -73,6 +74,7 @@ func (setter *TypeSetter) needLayout () {
|
|||||||
setter.layoutBoundsSpace.Max.X = setter.maxWidth
|
setter.layoutBoundsSpace.Max.X = setter.maxWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
y -= metrics.Height
|
||||||
if setter.maxHeight == 0 {
|
if setter.maxHeight == 0 {
|
||||||
setter.layoutBounds.Min.Y = -metrics.Ascent.Round()
|
setter.layoutBounds.Min.Y = -metrics.Ascent.Round()
|
||||||
setter.layoutBounds.Max.Y =
|
setter.layoutBounds.Max.Y =
|
||||||
|
44
textdraw/setter_test.go
Normal file
44
textdraw/setter_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user