Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly shape input before truncating #5014

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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