Skip to content

Commit

Permalink
Merge pull request #5014 from andydotxyz/fix/4998
Browse files Browse the repository at this point in the history
Correctly shape input before truncating
  • Loading branch information
andydotxyz authored Sep 1, 2024
2 parents 7aa9c4e + 93d4d60 commit 5fb3d75
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion widget/hyperlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestHyperlink_Truncate(t *testing.T) {
hyperlink.Truncation = fyne.TextTruncateEllipsis
hyperlink.Refresh()
texts = richTextRenderTexts(&hyperlink.provider)
assert.Equal(t, "TestingWi…", texts[0].Text)
assert.Equal(t, "TestingWit…", texts[0].Text)
}

func TestHyperlink_CreateRendererDoesNotAffectSize(t *testing.T) {
Expand Down
16 changes: 12 additions & 4 deletions widget/richtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,10 +1120,11 @@ func truncateLimit(s string, text *canvas.Text, limit int, ellipsis []rune) (int
RunStart: 0,
RunEnd: len(ellipsis),
Direction: di.DirectionLTR,
Face: face.Fonts.ResolveFace('…'),
Face: face.Fonts.ResolveFace(ellipsis[0]),
Size: float32ToFixed266(text.TextSize),
}
shaper := &shaping.HarfbuzzShaper{}
segmenter := &shaping.Segmenter{}

conf := shaping.WrapConfig{}
conf = conf.WithTruncator(shaper, in)
Expand All @@ -1133,12 +1134,19 @@ func truncateLimit(s string, text *canvas.Text, limit int, ellipsis []rune) (int

in.Text = runes
in.RunEnd = len(runes)
out := shaper.Shape(in)
ins := segmenter.Split(in, face.Fonts)
outs := make([]shaping.Output, len(ins))
for i, in := range ins {
outs[i] = shaper.Shape(in)
}

l.Prepare(conf, runes, shaping.NewSliceIterator([]shaping.Output{out}))
l.Prepare(conf, runes, shaping.NewSliceIterator(outs))
wrapped, done := l.WrapNextLine(limit)

count := wrapped.Line[0].Runes.Count
count := 0
for _, run := range wrapped.Line {
count += run.Runes.Count
}
full := done && count == len(runes)
if !full && len(ellipsis) > 0 {
count--
Expand Down
16 changes: 8 additions & 8 deletions widget/richtext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func TestText_lineBounds(t *testing.T) {
text: "foobar foobar",
trunc: fyne.TextTruncateEllipsis,
want: [][2]int{
{0, 8},
{0, 9},
},
ellipses: 1,
},
Expand Down Expand Up @@ -688,8 +688,8 @@ func TestText_lineBounds(t *testing.T) {
trunc: fyne.TextTruncateEllipsis,
want: [][2]int{
{0, 6},
{7, 15},
{28, 36},
{7, 16},
{28, 37},
},
ellipses: 2,
},
Expand Down Expand Up @@ -757,8 +757,8 @@ func TestText_lineBounds(t *testing.T) {
trunc: fyne.TextTruncateEllipsis,
want: [][2]int{
{0, 6},
{7, 14},
{26, 33},
{7, 15},
{26, 34},
{39, 39},
},
ellipses: 2,
Expand Down Expand Up @@ -909,8 +909,8 @@ func TestText_lineBounds(t *testing.T) {
trunc: fyne.TextTruncateEllipsis,
want: [][2]int{
{0, 6},
{7, 15},
{28, 36},
{7, 16},
{28, 37},
{42, 42},
},
ellipses: 2,
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func TestText_lineBounds_variable_char_width(t *testing.T) {
text: "iiiiiiiiiimmmmmmmmmm",
trunc: fyne.TextTruncateEllipsis,
want: [][2]int{
{0, 8},
{0, 9},
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion widget/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestSelect_ClipValue(t *testing.T) {

r2 := cache.Renderer(text)
assert.Equal(t, 1, len(r2.Objects()))
assert.Equal(t, "some…", r2.Objects()[0].(*canvas.Text).Text)
assert.Equal(t, "some …", r2.Objects()[0].(*canvas.Text).Text)
}

func TestSelect_Disable(t *testing.T) {
Expand Down

0 comments on commit 5fb3d75

Please sign in to comment.