mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
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:
+14
-9
@@ -523,22 +523,27 @@ func calculateYOffsets(lines []line) {
|
|||||||
// LayoutRunes shapes and wraps the text, and returns the result in Gio's shaped text format.
|
// 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 {
|
func (s *shaperImpl) LayoutRunes(params Parameters, txt []rune) document {
|
||||||
hasNewline := len(txt) > 0 && txt[len(txt)-1] == '\n'
|
hasNewline := len(txt) > 0 && txt[len(txt)-1] == '\n'
|
||||||
justNewline := false
|
var ls []shaping.Line
|
||||||
|
var truncated int
|
||||||
if hasNewline {
|
if hasNewline {
|
||||||
txt = txt[:len(txt)-1]
|
txt = txt[:len(txt)-1]
|
||||||
if len(txt) == 0 {
|
}
|
||||||
|
if hasNewline && len(txt) == 0 {
|
||||||
// If we only have a newline, shape a space to get line metrics.
|
// If we only have a newline, shape a space to get line metrics.
|
||||||
txt = []rune{' '}
|
ls, truncated = s.shapeAndWrapText(params, []rune{' '})
|
||||||
justNewline = true
|
if truncated > 0 {
|
||||||
}
|
// Our space was truncated. Since our space didn't exist in any meaningful
|
||||||
}
|
// capacity, ensure the truncated count is zeroed out.
|
||||||
ls, truncated := s.shapeAndWrapText(params, replaceControlCharacters(txt))
|
truncated = 0
|
||||||
if justNewline {
|
} else {
|
||||||
// We shaped a space to get proper line metrics, but we need to drop
|
// 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.
|
// 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].Glyphs = ls[0][0].Glyphs[:0]
|
||||||
ls[0][0].Runes.Count = 0
|
|
||||||
ls[0][0].Advance = 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))
|
didTruncate := truncated > 0 || (params.forceTruncate && params.MaxLines == len(ls))
|
||||||
|
|||||||
@@ -484,6 +484,19 @@ func TestShapeStringRuneAccounting(t *testing.T) {
|
|||||||
MaxWidth: 999929,
|
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) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
for _, setup := range []setup{
|
for _, setup := range []setup{
|
||||||
|
|||||||
Reference in New Issue
Block a user