giu icon indicating copy to clipboard operation
giu copied to clipboard

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

Open Geo5 opened this issue 2 years ago • 12 comments

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

Geo5 avatar Oct 08 '23 01:10 Geo5

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:.

gucio321 avatar Feb 17 '24 14:02 gucio321

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?

Geo5 avatar Feb 20 '24 20:02 Geo5

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)

gucio321 avatar Feb 21 '24 07:02 gucio321

I can get to work on this

MisustinIvan avatar Jul 10 '24 11:07 MisustinIvan

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?

MisustinIvan avatar Jul 10 '24 12:07 MisustinIvan

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?

gucio321 avatar Jul 10 '24 12:07 gucio321

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

MisustinIvan avatar Jul 10 '24 14:07 MisustinIvan

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

gucio321 avatar Jul 10 '24 15:07 gucio321

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

MisustinIvan avatar Jul 10 '24 15:07 MisustinIvan

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

MisustinIvan avatar Jul 10 '24 17:07 MisustinIvan

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

MisustinIvan avatar Jul 10 '24 17:07 MisustinIvan

Bruh I dont think there is a way to acomplish this without a major rewrite of a lot of things, which would in turn break a ton of other stuff. If you want icons in your font, just use a nerd font. They have all the icons from awesome and a lot more.

MisustinIvan avatar Jul 12 '24 08:07 MisustinIvan