This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/textdraw/setter_test.go

149 lines
5.1 KiB
Go
Raw Normal View History

2023-02-15 18:16:49 -07:00
package textdraw
import "image"
2023-02-15 18:16:49 -07:00
import "testing"
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
2023-02-15 18:16:49 -07:00
import "git.tebibyte.media/sashakoshka/tomo/defaultfont"
func TestSetterLength (test *testing.T) {
2023-02-15 23:55:00 -07:00
// case 1
2023-02-15 18:16:49 -07:00
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) {
2023-02-15 18:16:49 -07:00
test.Fatalf (
`setter rune count: %d, expected: %d`,
length, len(text))
2023-02-15 18:16:49 -07:00
}
2023-02-15 23:55:00 -07:00
// case 2
setter.SetMaxWidth(10)
length = 0
setter.For (func (i int, r rune, p fixed.Point26_6) bool {
length ++
return true
})
if length != len(text) {
2023-02-15 23:55:00 -07:00
test.Fatalf (
`setter rune count: %d, expected: %d`,
length, len(text))
2023-02-15 23:55:00 -07:00
}
2023-02-15 18:16:49 -07:00
}
func TestSetterBounds (test *testing.T) {
setter := TypeSetter { }
2023-02-15 23:55:00 -07:00
setter.SetText([]rune("The quick brown fox\njumped over the lazy dog."))
2023-02-15 18:16:49 -07:00
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)
}
2023-02-15 23:55:00 -07:00
testLargeRecHeight(test, 256)
testLargeRecHeight(test, 100)
testLargeRecHeight(test, 20)
testLargeRecHeight(test, 400)
2023-02-15 18:16:49 -07:00
}
2023-02-15 23:55:00 -07:00
func testLargeRecHeight (test *testing.T, width int) {
setter := TypeSetter { }
setter.SetText([]rune(lipsum))
setter.SetFace(defaultfont.FaceRegular)
setter.SetMaxWidth(width)
recHeight := setter.ReccomendedHeightFor(width)
bounds := setter.LayoutBounds()
if recHeight != bounds.Dy() {
test.Fatalf (
`setter bounds mismatch rec. height: %d, Dy: %d ` +
`for width: %d`,
recHeight, bounds.Dy(), width)
}
}
2023-02-16 10:35:31 -07:00
func TestSetterPosition (test *testing.T) {
setter := TypeSetter { }
setter.SetText([]rune("The quick brown fox\njumped over the lazy dog."))
setter.SetFace(defaultfont.FaceRegular)
index := 20
pos := fixedutil.RoundPt(setter.PositionAt(index))
expect := image.Pt(0, 13)
if pos != expect {
test.Fatalf (
`setter pos at %d: (%d, %d), expected: (%d, %d)`,
index, pos.X, pos.Y, expect.X, expect.Y)
}
}
2023-02-16 10:35:31 -07:00
func TestSetterIndex (test *testing.T) {
setter := TypeSetter { }
setter.SetText([]rune("The quick brown fox\njumped over the lazy dog."))
setter.SetFace(defaultfont.FaceRegular)
pos := fixed.P(3, 8)
index := setter.AtPosition(pos)
expect := 20
if index != expect {
test.Fatalf (
`setter index at (%d, %d): %d, expected: %d`,
pos.X.Round(), pos.Y.Round(), index, expect)
}
pos = fixed.P(-59, 230)
index = setter.AtPosition(pos)
2023-02-16 12:39:51 -07:00
expect = 20
if index != expect {
test.Fatalf (
`setter index at (%d, %d): %d, expected: %d`,
pos.X.Round(), pos.Y.Round(), index, expect)
}
pos = fixed.P(-500, -500)
index = setter.AtPosition(pos)
expect = 0
if index != expect {
test.Fatalf (
`setter index at (%d, %d): %d, expected: %d`,
pos.X.Round(), pos.Y.Round(), index, expect)
}
pos = fixed.P(500, -500)
index = setter.AtPosition(pos)
2023-02-16 10:35:31 -07:00
expect = 19
if index != expect {
test.Fatalf (
`setter index at (%d, %d): %d, expected: %d`,
pos.X.Round(), pos.Y.Round(), index, expect)
}
pos = fixed.P(500, 500)
index = setter.AtPosition(pos)
2023-02-16 12:39:51 -07:00
expect = setter.Length()
2023-02-16 10:35:31 -07:00
if index != expect {
test.Fatalf (
`setter index at (%d, %d): %d, expected: %d`,
pos.X.Round(), pos.Y.Round(), index, expect)
}
}
2023-02-15 23:55:00 -07:00
const lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Fermentum et sollicitudin ac orci phasellus egestas tellus rutrum. Aliquam vestibulum morbi blandit cursus risus at ultrices mi. Gravida dictum fusce ut placerat. Cursus metus aliquam eleifend mi in nulla posuere. Sit amet nulla facilisi morbi tempus iaculis urna id. Amet volutpat consequat mauris nunc congue nisi vitae. Varius duis at consectetur lorem donec massa sapien faucibus et. Vitae elementum curabitur vitae nunc sed velit dignissim. In hac habitasse platea dictumst quisque sagittis purus. Enim nulla aliquet porttitor lacus luctus accumsan tortor. Lectus magna fringilla urna porttitor rhoncus dolor purus non.\n\nNon pulvinar neque laoreet suspendisse. Viverra adipiscing at in tellus integer. Vulputate dignissim suspendisse in est ante. Purus in mollis nunc sed id semper. In est ante in nibh mauris cursus. Risus pretium quam vulputate dignissim suspendisse in est. Blandit aliquam etiam erat velit scelerisque in dictum. Lectus quam id leo in. Odio tempor orci dapibus ultrices in iaculis. Pharetra sit amet aliquam id. Elit ut aliquam purus sit. Egestas dui id ornare arcu odio ut sem nulla pharetra. Massa tempor nec feugiat nisl pretium fusce id. Dui accumsan sit amet nulla facilisi morbi. A lacus vestibulum sed arcu non odio euismod. Nam libero justo laoreet sit amet cursus. Mattis rhoncus urna neque viverra justo nec. Mauris augue neque gravida in fermentum et sollicitudin ac. Vulputate mi sit amet mauris. Ut sem nulla pharetra diam sit amet."