text: fix zero-width truncated newline rune accounting

This commit fixes another rune accounting issue that only existed when shaping
a solitary newline with zero width while truncating the line.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2023-08-02 10:32:14 -04:00
parent 80da4d6b02
commit 341978dbcd
2 changed files with 31 additions and 13 deletions
+18 -13
View File
@@ -523,22 +523,27 @@ func calculateYOffsets(lines []line) {
// LayoutRunes shapes and wraps the text, and returns the result in Gio's shaped text format.
func (s *shaperImpl) LayoutRunes(params Parameters, txt []rune) document {
hasNewline := len(txt) > 0 && txt[len(txt)-1] == '\n'
justNewline := false
var ls []shaping.Line
var truncated int
if hasNewline {
txt = txt[:len(txt)-1]
if len(txt) == 0 {
// If we only have a newline, shape a space to get line metrics.
txt = []rune{' '}
justNewline = true
}
}
ls, truncated := s.shapeAndWrapText(params, replaceControlCharacters(txt))
if justNewline {
// We shaped a space to get proper line metrics, but we need to drop
// the rune/glyph info since it isn't actually part of the text.
ls[0][0].Glyphs = ls[0][0].Glyphs[:0]
ls[0][0].Runes.Count = 0
ls[0][0].Advance = 0
if hasNewline && len(txt) == 0 {
// If we only have a newline, shape a space to get line metrics.
ls, truncated = s.shapeAndWrapText(params, []rune{' '})
if truncated > 0 {
// Our space was truncated. Since our space didn't exist in any meaningful
// capacity, ensure the truncated count is zeroed out.
truncated = 0
} else {
// We shaped a space to get proper line metrics, but we need to drop
// the rune/glyph info since it isn't actually part of the text.
ls[0][0].Glyphs = ls[0][0].Glyphs[:0]
ls[0][0].Advance = 0
ls[0][0].Runes.Count = 0
}
} else {
ls, truncated = s.shapeAndWrapText(params, replaceControlCharacters(txt))
}
didTruncate := truncated > 0 || (params.forceTruncate && params.MaxLines == len(ls))
+13
View File
@@ -484,6 +484,19 @@ func TestShapeStringRuneAccounting(t *testing.T) {
MaxWidth: 999929,
},
},
{
name: "newline zero-width regression",
input: "\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: 0,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
for _, setup := range []setup{