inkyblackness/imgui-go

在golang中 怎么支持中文显示呢

Closed this issue · 10 comments

在golang中 怎么支持中文显示呢

这个是 添加字体部分代码
如果这样设置 程序就会大量报错,无法正常运行
cio := imgui.CurrentIO()
var a float32
a = 13.0
cio.Fonts().AddFontFromFileTTF(./MSYH.TTF, a)

Hello there and thank you for your message!
I believe you want to know how to display Chinese characters with imgui. In case of different character-sets (or glyph-sets), you need to provide the sets you intend to use:

fonts.AddFontFromFileTTFV("C:\\windows\\fonts\\msjhl.ttc", 15.0, imgui.DefaultFontConfig, fonts.GlyphRangesChineseFull())

image

Is this what you want?

您好,谢谢您的留言!
我相信您想知道如何使用imgui显示汉字。如果使用不同的字符集(或字形集),则需要提供打算使用的字符集:

fonts.AddFontFromFileTTFV("C:\\windows\\fonts\\msjhl.ttc", 15.0, imgui.DefaultFontConfig, fonts.GlyphRangesChineseFull())

图片

这是你想要的吗?

感谢您的帮助 根据您的帮助 我这里已经解决了这个问题
但是 我发现这样设置 一次只能支持一种语言 如果要支持多种语言 应该怎么设置呢

To support multiple ranges, you have two options:

  • Register multiple fonts and select the font you need for a particular range for a widget, or
  • For fonts supporting multiple glyph ranges, use GlyphRangesBuilder, which provides GlyphRangesBuilder.AddExisting(GlyphRanges) that can be used to add multiple ranges. FontAtlas provides a few standard ranges, such as FontAtlas.GlyphRangesCyrillic() or FontAtlas.GlyphRangesThai(). When done, you can use GlyphRangesBuilder.Build() to retrieve the combined GlyphRanges instance to use for AddFontFromFileTTFV()

要支持多个范围,您有两个选择:

  • 注册多种字体并为小部件的特定范围选择所需的字体,或者
  • 对于支持多个字形范围的字体,请使用GlyphRangesBuilder,该字体GlyphRangesBuilder.AddExisting(GlyphRanges)可用于添加多个范围。FontAtlas提供一些标准范围,例如FontAtlas.GlyphRangesCyrillic()FontAtlas.GlyphRangesThai()。完成后,您可以使用GlyphRangesBuilder.Build()检索组合的GlyphRanges实例以用于AddFontFromFileTTFV()

思路我应该已经了解了 但写起来 总是失败

To support multiple ranges, you have two options:

  • Register multiple fonts and select the font you need for a particular range for a widget, or
  • For fonts supporting multiple glyph ranges, use GlyphRangesBuilder, which provides GlyphRangesBuilder.AddExisting(GlyphRanges) that can be used to add multiple ranges. FontAtlas provides a few standard ranges, such as FontAtlas.GlyphRangesCyrillic() or FontAtlas.GlyphRangesThai(). When done, you can use GlyphRangesBuilder.Build() to retrieve the combined GlyphRanges instance to use for AddFontFromFileTTFV()

感谢您的回复 我基本已经理解这种方法了 只是 在写代码的过程中 总是出现问题 . 如果您方便的话能否写一个简单的例子,我好作为参考!

You would use The GlyphRangesBuilder like so:

	var builder imgui.GlyphRangesBuilder
	builder.AddExisting(fonts.GlyphRangesDefault())
	builder.AddExisting(fonts.GlyphRangesKorean())
	builder.AddExisting(fonts.GlyphRangesChineseFull())
	ranges := builder.Build()
	fonts.AddFontFromFileTTFV("P:\\temp\\lihei.ttf", 15.0, imgui.DefaultFontConfig, ranges.GlyphRanges)
	image := fonts.TextureDataAlpha8()
	ranges.Free()

The lihei.ttf font is a font that holds both Chinese and Japanese characters. Available here for example.

If you need to use multiple font files, then it becomes more complex, as you need to use imgui.PushFont() to select the font you want to use for the next interface element.
Rough example:

	fontA := fonts.AddFontFromFileTTFV("P:\\temp\\lihei.ttf", 15.0, imgui.DefaultFontConfig, ranges.GlyphRanges)
	fontB := fonts.AddFontFromFileTTFV("P:\\temp\\DroidSansThai-Bx5l.ttf", 15.0, imgui.DefaultFontConfig, fonts.GlyphRangesThai())
	...
	imgui.PushFont(fontB)
	imgui.Text("ภาษาไทย") // will use the Thai font
	imgui.PopFont()
	imgui.Text("测试조선말") // will use the "large" font containing several languages (implicitly the first font added)

I hope this helps.
Personally I have not done any of these more complex scenarios. If you need more help, please head over to Dear ImGui.

You would use The GlyphRangesBuilder like so:

	var builder imgui.GlyphRangesBuilder
	builder.AddExisting(fonts.GlyphRangesDefault())
	builder.AddExisting(fonts.GlyphRangesKorean())
	builder.AddExisting(fonts.GlyphRangesChineseFull())
	ranges := builder.Build()
	fonts.AddFontFromFileTTFV("P:\\temp\\lihei.ttf", 15.0, imgui.DefaultFontConfig, ranges.GlyphRanges)
	image := fonts.TextureDataAlpha8()
	ranges.Free()

The lihei.ttf font is a font that holds both Chinese and Japanese characters. Available here for example.

If you need to use multiple font files, then it becomes more complex, as you need to use imgui.PushFont() to select the font you want to use for the next interface element.
Rough example:

	fontA := fonts.AddFontFromFileTTFV("P:\\temp\\lihei.ttf", 15.0, imgui.DefaultFontConfig, ranges.GlyphRanges)
	fontB := fonts.AddFontFromFileTTFV("P:\\temp\\DroidSansThai-Bx5l.ttf", 15.0, imgui.DefaultFontConfig, fonts.GlyphRangesThai())
	...
	imgui.PushFont(fontB)
	imgui.Text("ภาษาไทย") // will use the Thai font
	imgui.PopFont()
	imgui.Text("测试조선말") // will use the "large" font containing several languages (implicitly the first font added)

I hope this helps.
Personally I have not done any of these more complex scenarios. If you need more help, please head over to Dear ImGui.

感谢您的帮助 我的问题已经全部解决,由于对于各库不太熟悉在调试过程中发现 在 C.CSting() 应该是这个 或者是别的地方. 因为fonts.AddFontFromFileTTFV("P:\temp\lihei.ttf", 15.0, imgui.DefaultFontConfig, ranges.GlyphRanges) 如果 ttf 路径中有中文 或者有可能是 宽字节 不受支持 会造成 无法正常读入 ttf文件 ,如果可能的话 希望修复一下,当然也可能是 imGui 这个库 自身并不支持.

Great that you can work with it now!

As for using a file path with unicode characters: This would be an issue in the ImGui library, yes. In imgui.cpp there's ImFileOpen(), which has a block using MultiByteToWideChar. This block is guarded by some preprocessor checks. When compiling with Go, it would probably have either __GNUC__ or __CYGWIN__ set, and it defaults to fopen(), which does not support UTF8 (under MS Windows).

I'm not entirely sure why this would be disabled even for a Windows platform. Yet, you could help yourself by implementing own functions for handling files (The define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS is for that). This would need some indirection to Go-world, which could handle the file, and return the data. A little tricky, but doable. If you have something, I'd be happy to accept a pull request.

I'm closing this issue here now. All the best for your projects!

Hello there and thank you for your message! I believe you want to know how to display Chinese characters with imgui. In case of different character-sets (or glyph-sets), you need to provide the sets you intend to use:

fonts.AddFontFromFileTTFV("C:\\windows\\fonts\\msjhl.ttc", 15.0, imgui.DefaultFontConfig, fonts.GlyphRangesChineseFull())

image

Is this what you want?

fonts 怎么来的呢?

@aprchen if you are asking where the variable fonts came from in the above sample, it is the result of imgui.CurrentIO().Fonts() .