fyne icon indicating copy to clipboard operation
fyne copied to clipboard

no support Chinese ttf;

Open nyfooo9 opened this issue 3 years ago • 12 comments

Describe the bug:

To Reproduce:

Steps to reproduce the behaviour:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Screenshots:

Example code:

Device (please complete the following information):

  • OS:
  • Version:
  • Go version:
  • Fyne version:

nyfooo9 avatar Nov 23 '21 15:11 nyfooo9

Add this init in main.go //func init() { // fontPaths := findfont.List() // for _, path := range fontPaths { // fmt.Println(path) // //楷体:simkai.ttf // //黑体:simhei.ttf // if strings.Contains(path, "simkai.ttf") { // os.Setenv("FYNE_FONT", path) // break // } // } // fmt.Println("=============") //}

dump accured: C:\Windows\Fonts\seguisli.ttf C:\Windows\Fonts\seguisym.ttf C:\Windows\Fonts\simfang.ttf C:\Windows\Fonts\simhei.ttf C:\Windows\Fonts\simkai.ttf

2021/11/23 23:05:53 Lifecycle: Started 2021/11/23 23:05:54 Lifecycle: Entered Foreground panic: runtime error: slice bounds out of range [2:1]

goroutine 53 [running]: fyne.io/fyne/v2/widget.(*RichText).insertAt(0xc0003726e0, 0x2, 0xc002307f38, 0x3) D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/widget/richtext.go:222 +0x465 fyne.io/fyne/v2/widget.(*Entry).TypedRune(0xc00273a480, 0xc000006240) D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/widget/entry.go:743 +0x165 fyne.io/fyne/v2/internal/driver/glfw.(*window).charInput.func1() D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/glfw/window.go:1226 +0x3d fyne.io/fyne/v2/internal/driver/common.(*Window).RunEventQueue(0xc00037c200) D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/common/window.go:58 +0x6d created by fyne.io/fyne/v2/internal/driver/glfw.(*gLDriver).createWindow.func1 D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/glfw/window.go:1395 +0x127

nyfooo9 avatar Nov 23 '21 15:11 nyfooo9

Can you please fill in the bug report with version information requested?

Additionally the stack shows RichText but that is not in your demonstration code - can you expand the example to replicate the issue?

andydotxyz avatar Nov 24 '21 13:11 andydotxyz

func InitFont() { fontPaths := findfont.List() for _, path := range fontPaths { if strings.Contains(path, "simhei.ttf") { os.Setenv("FYNE_FONT", path) break } } fmt.Println("=====字体初始化成功========") }

evepupil avatar Dec 20 '21 06:12 evepupil

This is not code that replicates the error. We would need the Fyne code (including the Entry widget) and the keystrokes you used to demonstrate the problem.

andydotxyz avatar Dec 20 '21 17:12 andydotxyz

Add this init in main.go //func init() { // fontPaths := findfont.List() // for _, path := range fontPaths { // fmt.Println(path) // //楷体:simkai.ttf // //黑体:simhei.ttf // if strings.Contains(path, "simkai.ttf") { // os.Setenv("FYNE_FONT", path) // break // } // } // fmt.Println("=============") //}

dump accured:

C:\Windows\Fonts\seguisli.ttf C:\Windows\Fonts\seguisym.ttf C:\Windows\Fonts\simfang.ttf C:\Windows\Fonts\simhei.ttf C:\Windows\Fonts\simkai.ttf 2021/11/23 23:05:53 Lifecycle: Started 2021/11/23 23:05:54 Lifecycle: Entered Foreground panic: runtime error: slice bounds out of range [2:1]

goroutine 53 [running]: fyne.io/fyne/v2/widget.(*RichText).insertAt(0xc0003726e0, 0x2, 0xc002307f38, 0x3) D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/widget/richtext.go:222 +0x465 fyne.io/fyne/v2/widget.(*Entry).TypedRune(0xc00273a480, 0xc000006240) D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/widget/entry.go:743 +0x165 fyne.io/fyne/v2/internal/driver/glfw.(*window).charInput.func1() D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/glfw/window.go:1226 +0x3d fyne.io/fyne/v2/internal/driver/common.(*Window).RunEventQueue(0xc00037c200) D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/common/window.go:58 +0x6d created by fyne.io/fyne/v2/internal/driver/glfw.(*gLDriver).createWindow.func1 D:/gowork/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/glfw/window.go:1395 +0x127

可以支持中文的,我在Deepin(Linux)和Windows都能正常显示和输入中文。

如果实在读取系统字体有问题,可以使用主题的方式,把字体编译成主题资源,然后使用自己的中文主题来显示界面。

jorzion avatar Jan 07 '22 12:01 jorzion

可以支持中文的,我在Deepin(Linux)和Windows都能正常显示和输入中文。

如果实在读取系统字体有问题,可以使用主题的方式,把字体编译成主题资源,然后使用自己的中文主题来显示界面。

Chinese can be supported, and I can display and enter Chinese normally in Both Deepin (Linux) and Windows.

If you really have problems reading system fonts, you can use the theme method, compile the fonts into theme resources, and then use your own Chinese theme to display the interface.

I suspect that this is what you mean? I ran it though a translation service. I'd prefer if we could stick to English for conversations here to make sure that everyone can understand.

Jacalz avatar Jan 07 '22 13:01 Jacalz

because the default font in Fyne dose not support Chinese, but you can do something like this, it works well for me:

  1. Define a custom theme: (many part are copied from Fyne)
type myTheme struct {
	regular, bold, italic, boldItalic, monospace fyne.Resource
}

func (t *myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
	return theme.DefaultTheme().Color(name, variant)
}

func (t *myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
	return theme.DefaultTheme().Icon(name)
}

func (m *myTheme) Font(style fyne.TextStyle) fyne.Resource {
	if style.Monospace {
		return m.monospace
	}
	if style.Bold {
		if style.Italic {
			return m.boldItalic
		}
		return m.bold
	}
	if style.Italic {
		return m.italic
	}
	return m.regular
}

func (m *myTheme) Size(name fyne.ThemeSizeName) float32 {
	return theme.DefaultTheme().Size(name)
}

func (t *myTheme) SetFonts(regularFontPath string, monoFontPath string) {
	t.regular = theme.TextFont()
	t.bold = theme.TextBoldFont()
	t.italic = theme.TextItalicFont()
	t.boldItalic = theme.TextBoldItalicFont()
	t.monospace = theme.TextMonospaceFont()

	if regularFontPath != "" {
		t.regular = loadCustomFont(regularFontPath, "Regular", t.regular)
		t.bold = loadCustomFont(regularFontPath, "Bold", t.bold)
		t.italic = loadCustomFont(regularFontPath, "Italic", t.italic)
		t.boldItalic = loadCustomFont(regularFontPath, "BoldItalic", t.boldItalic)
	}
	if monoFontPath != "" {
		t.monospace = loadCustomFont(monoFontPath, "Regular", t.monospace)
	} else {
		t.monospace = t.regular
	}
}

func loadCustomFont(env, variant string, fallback fyne.Resource) fyne.Resource {
	variantPath := strings.Replace(env, "Regular", variant, -1)

	res, err := fyne.LoadResourceFromPath(variantPath)
	if err != nil {
		fyne.LogError("Error loading specified font", err)
		return fallback
	}

	return res
}
  1. set font in theme:
t := &myTheme{}
t.SetFonts("./assets/font/Consolas-with-Yahei Regular Nerd Font.ttf", "")
// note that under "./assets/font", there are actually 4 files:
// Consolas-with-Yahei Bold Nerd Font.ttf
// Consolas-with-Yahei BoldItalic Nerd Font.ttf
// Consolas-with-Yahei Italic Nerd Font.ttf
// Consolas-with-Yahei Regular Nerd Font.ttf
  1. finally set theme:
app.Settings().SetTheme(t)
  1. and it works

in chinese:

因为Fyne的默认字体并不支持中文,你可以像我这么干,很顺利的显示出来了:

  1. 先自定义一个主题(这里面很多代码都是直接从 Fyne 中拷贝过来的)
type myTheme struct {
	regular, bold, italic, boldItalic, monospace fyne.Resource
}

func (t *myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
	return theme.DefaultTheme().Color(name, variant)
}

func (t *myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
	return theme.DefaultTheme().Icon(name)
}

func (m *myTheme) Font(style fyne.TextStyle) fyne.Resource {
	if style.Monospace {
		return m.monospace
	}
	if style.Bold {
		if style.Italic {
			return m.boldItalic
		}
		return m.bold
	}
	if style.Italic {
		return m.italic
	}
	return m.regular
}

func (m *myTheme) Size(name fyne.ThemeSizeName) float32 {
	return theme.DefaultTheme().Size(name)
}

func (t *myTheme) SetFonts(regularFontPath string, monoFontPath string) {
	t.regular = theme.TextFont()
	t.bold = theme.TextBoldFont()
	t.italic = theme.TextItalicFont()
	t.boldItalic = theme.TextBoldItalicFont()
	t.monospace = theme.TextMonospaceFont()

	if regularFontPath != "" {
		t.regular = loadCustomFont(regularFontPath, "Regular", t.regular)
		t.bold = loadCustomFont(regularFontPath, "Bold", t.bold)
		t.italic = loadCustomFont(regularFontPath, "Italic", t.italic)
		t.boldItalic = loadCustomFont(regularFontPath, "BoldItalic", t.boldItalic)
	}
	if monoFontPath != "" {
		t.monospace = loadCustomFont(monoFontPath, "Regular", t.monospace)
	} else {
		t.monospace = t.regular
	}
}

func loadCustomFont(env, variant string, fallback fyne.Resource) fyne.Resource {
	variantPath := strings.Replace(env, "Regular", variant, -1)

	res, err := fyne.LoadResourceFromPath(variantPath)
	if err != nil {
		fyne.LogError("Error loading specified font", err)
		return fallback
	}

	return res
}
  1. 设置主题中的字体:
t := &myTheme{}
t.SetFonts("./assets/font/Consolas-with-Yahei Regular Nerd Font.ttf", "")
// 注意"./assets/font"目录下有4个文件:
// Consolas-with-Yahei Bold Nerd Font.ttf
// Consolas-with-Yahei BoldItalic Nerd Font.ttf
// Consolas-with-Yahei Italic Nerd Font.ttf
// Consolas-with-Yahei Regular Nerd Font.ttf
  1. 最后设置主题:
app.Settings().SetTheme(t)
  1. 然后中文就能正常显示了

CMA2401PT avatar Feb 21 '22 08:02 CMA2401PT

because the default font in Fyne dose not support Chinese, but you can do something like this, it works well for me:

  1. Define a custom theme: (many part are copied from Fyne)
type myTheme struct {
	regular, bold, italic, boldItalic, monospace fyne.Resource
}

func (t *myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
	return theme.DefaultTheme().Color(name, variant)
}

func (t *myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
	return theme.DefaultTheme().Icon(name)
}

func (m *myTheme) Font(style fyne.TextStyle) fyne.Resource {
	if style.Monospace {
		return m.monospace
	}
	if style.Bold {
		if style.Italic {
			return m.boldItalic
		}
		return m.bold
	}
	if style.Italic {
		return m.italic
	}
	return m.regular
}

func (m *myTheme) Size(name fyne.ThemeSizeName) float32 {
	return theme.DefaultTheme().Size(name)
}

func (t *myTheme) SetFonts(regularFontPath string, monoFontPath string) {
	t.regular = theme.TextFont()
	t.bold = theme.TextBoldFont()
	t.italic = theme.TextItalicFont()
	t.boldItalic = theme.TextBoldItalicFont()
	t.monospace = theme.TextMonospaceFont()

	if regularFontPath != "" {
		t.regular = loadCustomFont(regularFontPath, "Regular", t.regular)
		t.bold = loadCustomFont(regularFontPath, "Bold", t.bold)
		t.italic = loadCustomFont(regularFontPath, "Italic", t.italic)
		t.boldItalic = loadCustomFont(regularFontPath, "BoldItalic", t.boldItalic)
	}
	if monoFontPath != "" {
		t.monospace = loadCustomFont(monoFontPath, "Regular", t.monospace)
	} else {
		t.monospace = t.regular
	}
}

func loadCustomFont(env, variant string, fallback fyne.Resource) fyne.Resource {
	variantPath := strings.Replace(env, "Regular", variant, -1)

	res, err := fyne.LoadResourceFromPath(variantPath)
	if err != nil {
		fyne.LogError("Error loading specified font", err)
		return fallback
	}

	return res
}
  1. set font in theme:
t := &myTheme{}
t.SetFonts("./assets/font/Consolas-with-Yahei Regular Nerd Font.ttf", "")
// note that under "./assets/font", there are actually 4 files:
// Consolas-with-Yahei Bold Nerd Font.ttf
// Consolas-with-Yahei BoldItalic Nerd Font.ttf
// Consolas-with-Yahei Italic Nerd Font.ttf
// Consolas-with-Yahei Regular Nerd Font.ttf
  1. finally set theme:
app.Settings().SetTheme(t)
  1. and it works

in chinese:

因为Fyne的默认字体并不支持中文,你可以像我这么干,很顺利的显示出来了:

  1. 先自定义一个主题(这里面很多代码都是直接从 Fyne 中拷贝过来的)
type myTheme struct {
	regular, bold, italic, boldItalic, monospace fyne.Resource
}

func (t *myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
	return theme.DefaultTheme().Color(name, variant)
}

func (t *myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
	return theme.DefaultTheme().Icon(name)
}

func (m *myTheme) Font(style fyne.TextStyle) fyne.Resource {
	if style.Monospace {
		return m.monospace
	}
	if style.Bold {
		if style.Italic {
			return m.boldItalic
		}
		return m.bold
	}
	if style.Italic {
		return m.italic
	}
	return m.regular
}

func (m *myTheme) Size(name fyne.ThemeSizeName) float32 {
	return theme.DefaultTheme().Size(name)
}

func (t *myTheme) SetFonts(regularFontPath string, monoFontPath string) {
	t.regular = theme.TextFont()
	t.bold = theme.TextBoldFont()
	t.italic = theme.TextItalicFont()
	t.boldItalic = theme.TextBoldItalicFont()
	t.monospace = theme.TextMonospaceFont()

	if regularFontPath != "" {
		t.regular = loadCustomFont(regularFontPath, "Regular", t.regular)
		t.bold = loadCustomFont(regularFontPath, "Bold", t.bold)
		t.italic = loadCustomFont(regularFontPath, "Italic", t.italic)
		t.boldItalic = loadCustomFont(regularFontPath, "BoldItalic", t.boldItalic)
	}
	if monoFontPath != "" {
		t.monospace = loadCustomFont(monoFontPath, "Regular", t.monospace)
	} else {
		t.monospace = t.regular
	}
}

func loadCustomFont(env, variant string, fallback fyne.Resource) fyne.Resource {
	variantPath := strings.Replace(env, "Regular", variant, -1)

	res, err := fyne.LoadResourceFromPath(variantPath)
	if err != nil {
		fyne.LogError("Error loading specified font", err)
		return fallback
	}

	return res
}
  1. 设置主题中的字体:
t := &myTheme{}
t.SetFonts("./assets/font/Consolas-with-Yahei Regular Nerd Font.ttf", "")
// 注意"./assets/font"目录下有4个文件:
// Consolas-with-Yahei Bold Nerd Font.ttf
// Consolas-with-Yahei BoldItalic Nerd Font.ttf
// Consolas-with-Yahei Italic Nerd Font.ttf
// Consolas-with-Yahei Regular Nerd Font.ttf
  1. 最后设置主题:
app.Settings().SetTheme(t)
  1. 然后中文就能正常显示了

感谢,我的按照这个方法设置好了。

dmzlingyin avatar Apr 01 '22 13:04 dmzlingyin

yeah,its work. the exe application, cannot load the assets file. have anyone can show me how to use go-bindata or else?

bestravis avatar Apr 25 '22 09:04 bestravis

Using relative paths does not work once you distribute applications. Information about our recommended bundling is at https://developer.fyne.io/extend/bundle

andydotxyz avatar Apr 25 '22 11:04 andydotxyz

Thanks, Im use the os. Setenv("FYNE_FONT", path) , it works ! bundle assets ways it works too. But the latter is not compatible with development building both environments. have any tutorial about this ? :D

bestravis avatar Apr 26 '22 07:04 bestravis

Just setting the FYNE_FONT environment variables as your font file path will solve this problem. e.g: In Windows

set FYNE_FONT=C:\my_project\font\msyh.ttf

nononocat avatar Aug 23 '22 02:08 nononocat

1. os.Setenv("FYNE_FONT", font_file_path);
...
2. app.New()

Set FYNE_FONT before app.New()

app创建之前设置FYNE_FONT

imparting avatar Nov 03 '22 01:11 imparting

when I input Chinese characters ,occur 2 problem

1、in entry lost some Chinese characters which type in typewriting place 2、if input lots of characters, mey be more then 20 panic occur below:

TSM AdjustCapsLockLEDForKeyTransitionHandling - _ISSetPhysicalKeyboardCapsLockLED Inhibit panic: runtime error: slice bounds out of range [38:37]

goroutine 52 [running]: fyne.io/fyne/v2/widget.(*RichText).insertAt(0x140000da000, 0x26, {0x14000f25f20, 0x3}) /Users/go/pkg/mod/fyne.io/fyne/[email protected]/widget/richtext.go:222 +0x248 fyne.io/fyne/v2/widget.(*Entry).TypedRune(0x1400009c1e0, 0x6c34) /Users/go/pkg/mod/fyne.io/fyne/[email protected]/widget/entry.go:738 +0xdc fyne.io/fyne/v2/internal/driver/glfw.(*window).processCharInput.func1() /Users/go/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/glfw/window.go:782 +0x2c fyne.io/fyne/v2/internal/driver/common.(*Window).RunEventQueue(0x0?) /Users/go/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/common/window.go:35 +0x3c created by fyne.io/fyne/v2/internal/driver/glfw.(*gLDriver).createWindow.func1 /Userså/go/pkg/mod/fyne.io/fyne/[email protected]/internal/driver/glfw/window.go:942 +0x144

hongguozb avatar Nov 20 '22 13:11 hongguozb

1、in entry lost some Chinese characters which type in typewriting place

I think you are looking at #2777

andydotxyz avatar Nov 20 '22 18:11 andydotxyz

.\main.go:25:26: cannot use t (variable of type *myTheme) as "fyne.io/fyne".Theme value in argument to app.Settings().SetTheme: *myTheme does not implement "fyne.io/fyne".Theme (missing method BackgroundColor)

lvhejin avatar Jul 05 '23 17:07 lvhejin

Don't mix v2 with older imports

andydotxyz avatar Jul 05 '23 17:07 andydotxyz

And please keep questions related to the issues you write them on. Questions should go in into discussions or on Gophers slack

Jacalz avatar Jul 05 '23 20:07 Jacalz

import (_ "github.com/lengzhao/font/autoload") This will automatically load the fonts in the system without additional settings (automatically obtain the system language, find all system fonts, and configure the first matching font):https://github.com/lengzhao/font

lengzhao avatar Jul 16 '23 02:07 lengzhao

We moved to a new font loader, I believe this is fixed.

andydotxyz avatar Aug 28 '23 14:08 andydotxyz

We moved to a new font loader, I believe this is fixed.

no,i use latest version

TajangSec avatar Nov 18 '23 15:11 TajangSec

Please expand on your assertion that this issue is not resolved. A new crash dump for example.

andydotxyz avatar Nov 18 '23 16:11 andydotxyz