TypeSetter properly adds a null char onto the end of its text

This commit is contained in:
Sasha Koshka 2023-02-16 02:22:32 -05:00
parent 367aee4570
commit 7235c86e22
2 changed files with 34 additions and 6 deletions

View File

@ -37,7 +37,8 @@ func (setter *TypeSetter) needLayout () {
horizontalExtent := fixed.Int26_6(0)
horizontalExtentSpace := fixed.Int26_6(0)
lastLine := LineLayout { }
metrics := setter.face.Metrics()
remaining := setter.text
y := fixed.Int26_6(0)
@ -59,8 +60,17 @@ func (setter *TypeSetter) needLayout () {
horizontalExtentSpace = lineWidthSpace
}
setter.lines = append(setter.lines, line)
lastLine = line
}
// add a null onto the end because the very end of the text should have
// a valid layout position
lastWord := &lastLine.Words[len(lastLine.Words) - 1]
lastWord.Runes = append (lastWord.Runes, RuneLayout {
X: lastWord.Width + lastWord.SpaceAfter,
Rune: 0,
})
// set all line widths to horizontalExtent if we don't have a specified
// maximum width
if setter.maxWidth == 0 {
@ -195,10 +205,11 @@ func (setter *TypeSetter) For (iterator RuneIterator) {
for _, line := range setter.lines {
for _, word := range line.Words {
for _, char := range word.Runes {
iterator (index, char.Rune, fixed.Point26_6 {
keepGoing := iterator (index, char.Rune, fixed.Point26_6 {
X: word.X + char.X,
Y: line.Y,
})
if !keepGoing { return }
index ++
}}
if line.BreakAfter { index ++ }

View File

@ -1,7 +1,9 @@
package textdraw
import "image"
import "testing"
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
import "git.tebibyte.media/sashakoshka/tomo/defaultfont"
func TestSetterLength (test *testing.T) {
@ -15,10 +17,10 @@ func TestSetterLength (test *testing.T) {
length ++
return true
})
if length != len(text) - 1 {
if length != len(text) {
test.Fatalf (
`setter rune count: %d, expected: %d`,
length, len(text) - 1)
length, len(text))
}
// case 2
@ -28,10 +30,10 @@ func TestSetterLength (test *testing.T) {
length ++
return true
})
if length != len(text) - 1 {
if length != len(text) {
test.Fatalf (
`setter rune count: %d, expected: %d`,
length, len(text) - 1)
length, len(text))
}
}
@ -77,4 +79,19 @@ func testLargeRecHeight (test *testing.T, width int) {
}
}
func TestSetterIndex (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)
}
}
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."