inkyblackness / imgui-go

Go wrapper library for "Dear ImGui" (https://github.com/ocornut/imgui)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add fontawesome (AddFontFromFileTTFV)

Wolf65 opened this issue · comments

Hey. help solve the problem when trying to connect the fontawawsome font through the AddFontFromFileTTFV function instead of the "?" icons.
Безымянный
font const

ICON_MIN_FA = 0xf000
ICON_MAX_FA = 0xf941
....
ICON_FA_ANGLE_DOWN = '\uf107'
ICON_FA_ANGLE_LEFT = '\uf104'
if imgui.ButtonV(string(ICON_FA_ANGLE_LEFT), app.buttonSizeBigSquare) {

}
imgui.SameLine()
if imgui.ButtonV(fmt.Sprintf("%c", ICON_FA_ANGLE_LEFT), app.buttonSizeBigSquare) {

}
imgui.SameLine()
if imgui.ButtonV(fmt.Sprintf("%#U", ICON_FA_ANGLE_LEFT), app.buttonSizeBigSquare) {

}
imgui.SameLine()
if imgui.ButtonV(fmt.Sprintf("%+q", ICON_FA_ANGLE_LEFT), app.buttonSizeBigSquare) {

}
imgui.SameLine()
if imgui.ButtonV(fmt.Sprintf("%v", ICON_FA_ANGLE_LEFT), app.buttonSizeBigSquare) {

}
imgui.SameLine()
if imgui.ButtonV(fmt.Sprintf("%s", ICON_FA_ANGLE_LEFT), app.buttonSizeBigSquare) {

}

code add font

fonts := imgui.CurrentIO().Fonts()

fonts.AddFontDefault()

fileFA := "font/fa-regular-400.ttf"
fontConfigFA := imgui.NewFontConfig()
//fontConfigFA.SetGlyphMinAdvanceX(float32(ICON_MIN_FA))
//fontConfigFA.SetGlyphMaxAdvanceX(float32(ICON_MAX_FA))
fontConfigFA.SetMergeMode(true)
fontConfigFA.SetPixelSnapH(true)
var builderFA imgui.GlyphRangesBuilder
builderFA.Add(rune(ICON_MIN_FA), rune(ICON_MAX_FA))
//builderFA.Add(ICON_FA_ANGLE_LEFT, ICON_FA_ANGLE_DOWN)

rangesFA := builderFA.Build()


fonts.AddFontFromFileTTFV(fileFA, 16, fontConfigFA, rangesFA.GlyphRanges)
image := fonts .TextureDataAlpha8()
	
var lastTexture int32
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &lastTexture)
gl.GenTextures(1, &renderer.fontTexture)
gl.BindTexture(gl.TEXTURE_2D, renderer.fontTexture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(image.Width), int32(image.Height),
	0, gl.RED, gl.UNSIGNED_BYTE, image.Pixels)

fonts .SetTextureID(imgui.TextureID(renderer.fontTexture))

gl.BindTexture(gl.TEXTURE_2D, uint32(lastTexture))

What could be the problem and how it can be solved.

Hello @Wolf65 !
From a first inspection the code looks fine, and you seem to have tried out various approaches already.
Does anything change when you remove the fonts.AddFontDefault(), or put it after the AddFontFromFile() ?

If I comment on fonts.AddFontDefault () and fontConfigFA.SetMergeMode (true) then
Безымянный

if first fontawawsome and then standard (log clear)
code

fonts := imgui.CurrentIO().Fonts()

	fileFA := "font/fa-regular-400.ttf"
	fontConfigFA := imgui.NewFontConfig()
	//fontConfigFA.SetGlyphMinAdvanceX(float32(ICON_MIN_FA))
	//fontConfigFA.SetGlyphMaxAdvanceX(float32(ICON_MAX_FA))
	//fontConfigFA.SetMergeMode(true)
	fontConfigFA.SetPixelSnapH(true)
	var builderFA imgui.GlyphRangesBuilder
	builderFA.Add(rune(ICON_MIN_FA), rune(ICON_MAX_FA))
	//builderFA.Add(ICON_FA_ANGLE_LEFT, ICON_FA_ANGLE_DOWN)

	rangesFA := builderFA.Build()


	fonts.AddFontFromFileTTFV(fileFA, 16, fontConfigFA, rangesFA.GlyphRanges)

	confDefFont := imgui.NewFontConfig()
	confDefFont.SetMergeMode(true)
	fonts.AddFontDefaultV(confDefFont)
        image := fonts .TextureDataAlpha8()

result

Безымянный

I have the suspicion that adding the extra font does not work. Do you receive a non-nil return value from AddFontFromFileTTFV?

Addendum: If you receive something, draw an image based on the created font texture (the demo window does this as an example). This to see whether and how the created texture looks like.

Now there is no way to check what the function returns. But if I add a new font from jetbrains in the same way, everything is fine and it displays correctly

The signature of the function is AddFontFromFileTTFV(filename string, sizePixels float32, config FontConfig, glyphRange GlyphRanges) Font - so, the returned type is a simple uintptr, which you can print. If it is 0, then the call failed.
Trying this with another font to get it work gives even more an indication that the first one won't be read by Dear ImGui...

Hi all,

I have the same issue - i've added FontAwesome and it's displayed in the Dear ImGui demo window correctly as texture (in Configuration, Styles, Fonts), but not as glyphs - they are all empty as seen in the screenshot.
Screenshot 2020-01-20 at 9 12 08
AddFontFromFileTTFV does return big number as result, so it obviously works fine. I'm trying to resolve this as well and will do PR if i manage to.

I do have FontAwesome (same font files even) added in C++ app using Dear ImGui and it works fine.

Maybe it’s in ranger since in c ++ version range was passed as "[start, end, 0]" in go "[start, end]"

After some more testing it seems like in my case everything was working fine, except that i've set SetGlyphMinAdvanceX and SetGlyphMaxAdvanceX on the font config which was wrong - after i've removed them the icons showed just fine.

My code is :

fileFA := "../fonts/fontawesome-webfont.ttf"
fontConfigFA := imgui.NewFontConfig()
fontConfigFA.SetMergeMode(true)
fontConfigFA.SetPixelSnapH(true)
var builderFA imgui.GlyphRangesBuilder
builderFA.Add(rune(fonts.FA_ICON_MIN), rune(fonts.FA_ICON_MAX))
rangesFA := builderFA.Build()
context.fontFA = fontAtlas.AddFontFromFileTTFV(fileFA, 14, fontConfigFA, rangesFA.GlyphRanges)
fontConfigFA.Delete()

После еще одного тестирования кажется, что в моем случае все работало нормально, за исключением того, что я установил SetGlyphMinAdvanceX и SetGlyphMaxAdvanceX в конфигурации шрифтов, что было неправильно - после того, как я удалил их, значки показались очень хорошо.

Мой код:

fileFA := "../fonts/fontawesome-webfont.ttf"
fontConfigFA := imgui.NewFontConfig()
fontConfigFA.SetMergeMode(true)
fontConfigFA.SetPixelSnapH(true)
var builderFA imgui.GlyphRangesBuilder
builderFA.Add(rune(fonts.FA_ICON_MIN), rune(fonts.FA_ICON_MAX))
rangesFA := builderFA.Build()
context.fontFA = fontAtlas.AddFontFromFileTTFV(fileFA, 14, fontConfigFA, rangesFA.GlyphRanges)
fontConfigFA.Delete()

all the same "?"

Where can I get this font from? And/or, do you have a complete example reproducing the issue?

I'm using it in my app Kuplung-Go, code starts at L417 with those fonts.

Not sure if it's an issue though, my only problem was that i specified SetGlyphMinAdvanceX and SetGlyphMaxAdvanceX, which might've filtered the glyphs a bit more necessary.

P.S. I'm not very knowledgable in Go, so i might be wrong in several occasions :)

everything turned out to be much easier in the free version of the font, not all glyphs are in the "style (regular, solid, brands)" and if you connect all the files with all styles, everything works
Безымянный

Great - so, everything solved? :)

Yes, everything works as I wanted. Thanks for the help.