From 7235c86e225f14fa2085828b5919b9e8d99d402d Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 16 Feb 2023 02:22:32 -0500 Subject: [PATCH] TypeSetter properly adds a null char onto the end of its text --- textdraw/setter.go | 15 +++++++++++++-- textdraw/setter_test.go | 25 +++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/textdraw/setter.go b/textdraw/setter.go index 74cead7..3d59764 100644 --- a/textdraw/setter.go +++ b/textdraw/setter.go @@ -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 ++ } diff --git a/textdraw/setter_test.go b/textdraw/setter_test.go index c971d20..e9df405 100644 --- a/textdraw/setter_test.go +++ b/textdraw/setter_test.go @@ -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."