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

No support for Chinese ttf font #2660

Closed
nyfooo9 opened this issue Nov 23, 2021 · 22 comments
Closed

No support for Chinese ttf font #2660

nyfooo9 opened this issue Nov 23, 2021 · 22 comments
Labels
information-needed Further information is requested unverified A bug that has been reported but not verified

Comments

@nyfooo9
Copy link

nyfooo9 commented Nov 23, 2021

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 nyfooo9 added the unverified A bug that has been reported but not verified label Nov 23, 2021
@nyfooo9
Copy link
Author

nyfooo9 commented Nov 23, 2021

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/v2@v2.1.1/widget/richtext.go:222 +0x465
fyne.io/fyne/v2/widget.(*Entry).TypedRune(0xc00273a480, 0xc000006240)
D:/gowork/pkg/mod/fyne.io/fyne/v2@v2.1.1/widget/entry.go:743 +0x165
fyne.io/fyne/v2/internal/driver/glfw.(*window).charInput.func1()
D:/gowork/pkg/mod/fyne.io/fyne/v2@v2.1.1/internal/driver/glfw/window.go:1226 +0x3d
fyne.io/fyne/v2/internal/driver/common.(*Window).RunEventQueue(0xc00037c200)
D:/gowork/pkg/mod/fyne.io/fyne/v2@v2.1.1/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/v2@v2.1.1/internal/driver/glfw/window.go:1395 +0x127

@andydotxyz
Copy link
Member

andydotxyz commented Nov 24, 2021

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 andydotxyz added the information-needed Further information is requested label Dec 7, 2021
@evepupil
Copy link

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

@andydotxyz
Copy link
Member

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.

@jorzion
Copy link
Contributor

jorzion commented Jan 7, 2022

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/v2@v2.1.1/widget/richtext.go:222 +0x465 fyne.io/fyne/v2/widget.(*Entry).TypedRune(0xc00273a480, 0xc000006240) D:/gowork/pkg/mod/fyne.io/fyne/v2@v2.1.1/widget/entry.go:743 +0x165 fyne.io/fyne/v2/internal/driver/glfw.(*window).charInput.func1() D:/gowork/pkg/mod/fyne.io/fyne/v2@v2.1.1/internal/driver/glfw/window.go:1226 +0x3d fyne.io/fyne/v2/internal/driver/common.(*Window).RunEventQueue(0xc00037c200) D:/gowork/pkg/mod/fyne.io/fyne/v2@v2.1.1/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/v2@v2.1.1/internal/driver/glfw/window.go:1395 +0x127

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

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

@Jacalz
Copy link
Member

Jacalz commented Jan 7, 2022

可以支持中文的,我在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.

@CMA2401PT
Copy link

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
Copy link

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. 然后中文就能正常显示了

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

@bestravis
Copy link

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

@andydotxyz
Copy link
Member

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

@bestravis
Copy link

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

@nononocat
Copy link

nononocat commented Aug 23, 2022

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

vski5 added a commit to vski5/SingleMarkDown that referenced this issue Oct 12, 2022
@imparting
Copy link

imparting commented Nov 3, 2022

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

Set FYNE_FONT before app.New()

app创建之前设置FYNE_FONT

@hongguozb
Copy link

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/v2@v2.2.4/widget/richtext.go:222 +0x248
fyne.io/fyne/v2/widget.(*Entry).TypedRune(0x1400009c1e0, 0x6c34)
/Users/go/pkg/mod/fyne.io/fyne/v2@v2.2.4/widget/entry.go:738 +0xdc
fyne.io/fyne/v2/internal/driver/glfw.(*window).processCharInput.func1()
/Users/go/pkg/mod/fyne.io/fyne/v2@v2.2.4/internal/driver/glfw/window.go:782 +0x2c
fyne.io/fyne/v2/internal/driver/common.(*Window).RunEventQueue(0x0?)
/Users/go/pkg/mod/fyne.io/fyne/v2@v2.2.4/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/v2@v2.2.4/internal/driver/glfw/window.go:942 +0x144

@andydotxyz
Copy link
Member

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

I think you are looking at #2777

@lvhejin
Copy link

lvhejin commented Jul 5, 2023

.\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)

@andydotxyz
Copy link
Member

Don't mix v2 with older imports

@Jacalz Jacalz changed the title no support Chinese ttf; No support for Chinese ttf font Jul 5, 2023
@Jacalz
Copy link
Member

Jacalz commented Jul 5, 2023

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

@lengzhao
Copy link

lengzhao commented Jul 16, 2023

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

@andydotxyz
Copy link
Member

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

@TajangSec
Copy link

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

no,i use latest version

@andydotxyz
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
information-needed Further information is requested unverified A bug that has been reported but not verified
Projects
None yet
Development

No branches or pull requests