text: ensure truncated consecutive newlines are handled

This commit ensures that multiple newlines in a row still produce expected
results when occuring within a truncated string. The problem was that we usually
wrap text that is truncated in a way that forces the truncator symbol to appear
at the end *unless* we know we're on the final paragraph of the input text. This
is the right behavior for text that will be displayed, but when shaping a paragraph
containing nothing but a newline, we do not want the truncator symbol in our line.
I simply had to disable the forced truncation contextually to make it work.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2023-08-03 15:57:34 -04:00
parent c1d975cced
commit 2c90e001a0
2 changed files with 29 additions and 0 deletions
+1
View File
@@ -530,6 +530,7 @@ func (s *shaperImpl) LayoutRunes(params Parameters, txt []rune) document {
}
truncatedNewline := false
if hasNewline && len(txt) == 0 {
params.forceTruncate = false
// If we only have a newline, shape a space to get line metrics.
ls, truncated = s.shapeAndWrapText(params, []rune{' '})
if truncated > 0 {
+28
View File
@@ -163,6 +163,8 @@ func TestShapingNewlineHandling(t *testing.T) {
{textInput: "a\nb", expectedLines: 2, expectedGlyphs: 3},
{textInput: "", expectedLines: 1, expectedGlyphs: 1},
{textInput: "\n", expectedLines: 1, expectedGlyphs: 2},
{textInput: "\n\n", expectedLines: 2, expectedGlyphs: 3},
{textInput: "\n\n\n", expectedLines: 3, expectedGlyphs: 4},
} {
t.Run(fmt.Sprintf("%q", tc.textInput), func(t *testing.T) {
ltrFace, _ := opentype.Parse(goregular.TTF)
@@ -497,6 +499,32 @@ func TestShapeStringRuneAccounting(t *testing.T) {
MaxWidth: 0,
},
},
{
name: "double newline regression",
input: "\n\n",
params: Parameters{
Font: font.Font{Typeface: "Go", Style: font.Regular, Weight: font.Normal},
Alignment: Start,
PxPerEm: 768,
MaxLines: 1,
Truncator: "\u200b",
WrapPolicy: WrapHeuristically,
MaxWidth: 1000,
},
},
{
name: "triple newline regression",
input: "\n\n\n",
params: Parameters{
Font: font.Font{Typeface: "Go", Style: font.Regular, Weight: font.Normal},
Alignment: Start,
PxPerEm: 768,
MaxLines: 1,
Truncator: "\u200b",
WrapPolicy: WrapHeuristically,
MaxWidth: 1000,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
for _, setup := range []setup{