AllenDang / giu

Cross platform rapid GUI framework for golang based on Dear ImGui.
MIT License
2.16k stars 128 forks source link

StyleSetter: apply SetFontSize for all default fonts (not only first one) #712

Open Geo5 opened 9 months ago

Geo5 commented 9 months ago

What happend?

If one is adding fonts containing e.g. icons or other special charactes with FontAtlas.SetDefaultFont(...) and uses StyleSetter.SetFontSize(...).To(...) afterwards, only text/characters/glyphs contained in the last added default font are shown.

This seems to be, because new default fonts are prepended to the list of default fonts here in SetDefaultFont(...) and the StyleSetter.SetFontSize(...) uses index 0 of the default fonts.

The minimal example does currently not show any icon at all, because of #711 but looks as follows on v0.7.0:

Fonts_v0 7 0_1

I don't have any idea, how this could be fixed, because as far as i understand the magic happens by merging the default fonts in rebuildFontAtlas, which is not done for the extra fonts (which StyleSetter uses to do the styling)

Code example

main.go ```golang package main import ( iconFonts "github.com/juliettef/IconFontCppHeaders" g "github.com/AllenDang/giu" ) var IconFont = iconFonts.IconsForkAwesome func Icon(name string) string { ret, ok := IconFont.Icons[name] if !ok { panic(name + " icon not found") } return ret } func loop() { g.SingleWindow().Layout( g.Style().SetFontSize(30).To( g.Label("Content line"), g.Label("Content line "+Icon("Check")), ), g.Button("Normal button"), g.Button("Normal button"+Icon("Check")), ) } func main() { wnd := g.NewMasterWindow("Multiple fonts and icons", 600, 400, g.MasterWindowFlagsNotResizable) // Change the default font to include icons g.Context.FontAtlas.SetDefaultFont("forkawesome-webfont.ttf", 20) wnd.Run(loop) } ```

To Reproduce

  1. Run my demo

Version

master

OS

Linux

gucio321 commented 4 months ago

so, if index 0 is the only case, fix here should be easy to do. It just needs some extra stuff in style setter to extend fonts field to a slice of fonts and push them all instead of only one font. If I remember correctly I put index 0 there because I was too lazy to apply something more complex :smile:.

Geo5 commented 4 months ago

So you suggest making this https://github.com/AllenDang/giu/blob/304f0b9ed27bd1e4e246308b2326900c3a26f5fd/StyleSetter.go#L16 a slice of fonts and maybe adding a new function like this https://github.com/AllenDang/giu/blob/304f0b9ed27bd1e4e246308b2326900c3a26f5fd/Style.go#L43 which takes a slice of fonts and just calls PushFont for all of them?

gucio321 commented 4 months ago

Yeah and This https://github.com/AllenDang/giu/blob/304f0b9ed27bd1e4e246308b2326900c3a26f5fd/StyleSetter.go#L62 should apply to all default fonts

Also there is no need to add me method - setfont can be modified SetFont(fonrs .. FontInfo)

MisustinIvan commented 1 day ago

I can get to work on this

MisustinIvan commented 1 day ago

if I push multiple fonts, doesn't it just use the font on top of the stack? How does it then render the button with the icon?

gucio321 commented 1 day ago

I suppose that when you push multiple fonts it tries to use this on top of the stack, then it tries the next and so on...

iirc there was something like MergeMode (?) Maybe this is important?

MisustinIvan commented 1 day ago

Is there by chance any way to view the current fonts on the stack?

gucio321 commented 1 day ago

no idea... You can search in cimgui_funcs.go in cimgui-go repo but I'm affraid that no...

MisustinIvan commented 23 hours ago

Well i wrote something, but it doesn't work. If you want to take a look, its here.

MisustinIvan commented 22 hours ago

Bruh I read the read the source code all the way to dear imgui source and have no idea

MisustinIvan commented 21 hours ago

Still have to figure out why does it work withou setting the font size... I will think about it.