Skip to content

Commit 5fb3d75

Browse files
authored
Merge pull request #5014 from andydotxyz/fix/4998
Correctly shape input before truncating
2 parents 7aa9c4e + 93d4d60 commit 5fb3d75

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

widget/hyperlink_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func TestHyperlink_Truncate(t *testing.T) {
204204
hyperlink.Truncation = fyne.TextTruncateEllipsis
205205
hyperlink.Refresh()
206206
texts = richTextRenderTexts(&hyperlink.provider)
207-
assert.Equal(t, "TestingWi…", texts[0].Text)
207+
assert.Equal(t, "TestingWit…", texts[0].Text)
208208
}
209209

210210
func TestHyperlink_CreateRendererDoesNotAffectSize(t *testing.T) {

widget/richtext.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,11 @@ func truncateLimit(s string, text *canvas.Text, limit int, ellipsis []rune) (int
11201120
RunStart: 0,
11211121
RunEnd: len(ellipsis),
11221122
Direction: di.DirectionLTR,
1123-
Face: face.Fonts.ResolveFace('…'),
1123+
Face: face.Fonts.ResolveFace(ellipsis[0]),
11241124
Size: float32ToFixed266(text.TextSize),
11251125
}
11261126
shaper := &shaping.HarfbuzzShaper{}
1127+
segmenter := &shaping.Segmenter{}
11271128

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

11341135
in.Text = runes
11351136
in.RunEnd = len(runes)
1136-
out := shaper.Shape(in)
1137+
ins := segmenter.Split(in, face.Fonts)
1138+
outs := make([]shaping.Output, len(ins))
1139+
for i, in := range ins {
1140+
outs[i] = shaper.Shape(in)
1141+
}
11371142

1138-
l.Prepare(conf, runes, shaping.NewSliceIterator([]shaping.Output{out}))
1143+
l.Prepare(conf, runes, shaping.NewSliceIterator(outs))
11391144
wrapped, done := l.WrapNextLine(limit)
11401145

1141-
count := wrapped.Line[0].Runes.Count
1146+
count := 0
1147+
for _, run := range wrapped.Line {
1148+
count += run.Runes.Count
1149+
}
11421150
full := done && count == len(runes)
11431151
if !full && len(ellipsis) > 0 {
11441152
count--

widget/richtext_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func TestText_lineBounds(t *testing.T) {
575575
text: "foobar foobar",
576576
trunc: fyne.TextTruncateEllipsis,
577577
want: [][2]int{
578-
{0, 8},
578+
{0, 9},
579579
},
580580
ellipses: 1,
581581
},
@@ -688,8 +688,8 @@ func TestText_lineBounds(t *testing.T) {
688688
trunc: fyne.TextTruncateEllipsis,
689689
want: [][2]int{
690690
{0, 6},
691-
{7, 15},
692-
{28, 36},
691+
{7, 16},
692+
{28, 37},
693693
},
694694
ellipses: 2,
695695
},
@@ -757,8 +757,8 @@ func TestText_lineBounds(t *testing.T) {
757757
trunc: fyne.TextTruncateEllipsis,
758758
want: [][2]int{
759759
{0, 6},
760-
{7, 14},
761-
{26, 33},
760+
{7, 15},
761+
{26, 34},
762762
{39, 39},
763763
},
764764
ellipses: 2,
@@ -909,8 +909,8 @@ func TestText_lineBounds(t *testing.T) {
909909
trunc: fyne.TextTruncateEllipsis,
910910
want: [][2]int{
911911
{0, 6},
912-
{7, 15},
913-
{28, 36},
912+
{7, 16},
913+
{28, 37},
914914
{42, 42},
915915
},
916916
ellipses: 2,
@@ -1041,7 +1041,7 @@ func TestText_lineBounds_variable_char_width(t *testing.T) {
10411041
text: "iiiiiiiiiimmmmmmmmmm",
10421042
trunc: fyne.TextTruncateEllipsis,
10431043
want: [][2]int{
1044-
{0, 8},
1044+
{0, 9},
10451045
},
10461046
},
10471047
{

widget/select_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestSelect_ClipValue(t *testing.T) {
100100

101101
r2 := cache.Renderer(text)
102102
assert.Equal(t, 1, len(r2.Objects()))
103-
assert.Equal(t, "some…", r2.Objects()[0].(*canvas.Text).Text)
103+
assert.Equal(t, "some …", r2.Objects()[0].(*canvas.Text).Text)
104104
}
105105

106106
func TestSelect_Disable(t *testing.T) {

0 commit comments

Comments
 (0)