Skip to content

Fix Wide Character's width caculation in TextGrid#6143

Merged
andydotxyz merged 9 commits into
fyne-io:developfrom
typenoob:fix-wcwidth
Mar 30, 2026
Merged

Fix Wide Character's width caculation in TextGrid#6143
andydotxyz merged 9 commits into
fyne-io:developfrom
typenoob:fix-wcwidth

Conversation

@typenoob

@typenoob typenoob commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Description:

Simply use https://github.com/mattn/go-runewidth to caculate wide char's widith in TextGrid.

Fixes #6099

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

@andydotxyz

Copy link
Copy Markdown
Member

Thanks for the fix. I don't think go.mod is up to date though, as the new dependency is listed as indirect.
Also you'll have to use an older version (the .17) to get a version compatible with our current Go version support.

@typenoob

typenoob commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

Sorry, I'm not very experienced in go package management. It has been fixed now.

@andydotxyz

Copy link
Copy Markdown
Member

Sorry, I'm not very experienced in go package management. It has been fixed now.

In general "go mod tidy" fixes it - but in the situation of libraries requiring higher Go versions it does get more complicated.

@andydotxyz

Copy link
Copy Markdown
Member

Sorry for the additional step, but TextGrid was just updated to not draw the underline as a separate object, so this is now conflicting.
Please push a new commit that merges develop and resolves the conflict.

@typenoob

typenoob commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Done @andydotxyz

But there's one more thing I'm concerned. The width of textGrid returned by Size() is also wrong in Wide Char case.

package main

import (
	"fmt"

	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	w := a.NewWindow("Hello World")

	l := widget.NewLabel("一二三")
	t := widget.NewTextGridFromString("一二三")
	w.SetContent(container.NewHBox(
		l, t,
	))
	fmt.Printf("Label width is correct as %.1f, but textgrid width is wrong as %.1f",
		l.Size().Width, t.Size().Width)
	// Label width is correct as 58.0, but textgrid width is wrong as 24.0
	w.ShowAndRun()
}

And I can't figure out a clean way to fix, because currently all cells in the TextGrid are constrained to have the same size.

type textGridContent struct {
	BaseWidget
	text *TextGrid

	rows     int
	cellSize fyne.Size

	visible []fyne.CanvasObject
}

I'd like to make a seperate PR, what do you think?

@andydotxyz

Copy link
Copy Markdown
Member

For performance all cells are the same size as you say.
I think you'll need to look at a "stride" approach - so if runeWidth == 2 then the item is painted but the cell after it is empty, before continuing as normal with an offset 1 cell later. (see how tab works for a similar algorithm).

@coveralls

coveralls commented Mar 9, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 60.808% (+0.01%) from 60.798%
when pulling cad6121 on typenoob:fix-wcwidth
into 465174c on fyne-io:develop.

@typenoob

Copy link
Copy Markdown
Contributor Author

I think it's ready now.

@andydotxyz andydotxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this

@andydotxyz andydotxyz merged commit 33a4ea1 into fyne-io:develop Mar 30, 2026
11 checks passed
andydotxyz pushed a commit that referenced this pull request May 7, 2026
* use ”stride“ approach for wide character handling
* Add a test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants