AllenDang / giu

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

[bug] AddFont not working depending on scale factor on windows #759

Open 0xkalle opened 4 months ago

0xkalle commented 4 months ago

What happend?

When adding font in different sizes with AddFont to the FontAtlas, it is working fine on 100% Scale. On other Scales it is not working or partly working. e.g. on 125% no custom fonts work while on 150% some work.

Beside the example below I also made as simple as possible example repo: https://github.com/0xkalle/giu-font-test

I also tried to run with master. But it is crashing (even the hello world example crashes)

Code example

main.go ```golang package main import ( g "github.com/AllenDang/giu" ) var ( datafont_16 *g.FontInfo datafont_24 *g.FontInfo datafont_32 *g.FontInfo datafont_48 *g.FontInfo datafont_64 *g.FontInfo datafont_72 *g.FontInfo datafont_96 *g.FontInfo ) func loop() { g.SingleWindow().Layout( g.Label("0verlay Text 96").Font(datafont_96), g.Label("0verlay Text 72").Font(datafont_72), g.Label("0verlay Text 64").Font(datafont_64), g.Label("0verlay Text 48").Font(datafont_48), g.Label("0verlay Text 32").Font(datafont_32), g.Label("0verlay Text 24").Font(datafont_24), g.Label("0verlay Text 16").Font(datafont_16), ) } func main() { wnd := g.NewMasterWindow("Font test", 600, 400, 0) datafont_16 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 16) datafont_24 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 24) datafont_32 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 32) datafont_48 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 48) datafont_64 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 64) datafont_72 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 72) datafont_96 = g.Context.FontAtlas.AddFont("robotomono-m.ttf", 96) wnd.Run(loop) } ```

To Reproduce

  1. Set Scale in windows to 100 / 125 / 150 %
  2. Run my demo
  3. see the fonts in window.

Results are: 100% 100

125% 125

150%

150

Version

(latest)

OS

windows 10

wxblue commented 4 months ago

Use original key for extraFontMap in FontAtlasProsessor.go

        // Add extra fonts
    for _, fontInfo := range a.extraFonts {
        // Scale font size with DPI scale factor
        key := fontInfo.String()
        if runtime.GOOS == windows {
            fontInfo.size *= Context.GetPlatform().GetContentScale()
        }

        // Store imgui.Font for PushFont
        var f imgui.Font
        if len(fontInfo.fontByte) == 0 {
            f = fonts.AddFontFromFileTTFV(fontInfo.fontPath, fontInfo.size, imgui.DefaultFontConfig, ranges.Data())
        } else {
            f = fonts.AddFontFromMemoryTTFV(fontInfo.fontByte, fontInfo.size, imgui.DefaultFontConfig, ranges.Data())
        }

        a.extraFontMap[key] = &f
    }
0xkalle commented 4 months ago

I'll try that later today. Thanks. :)

0xkalle commented 4 months ago

Works. :) Is it worth it to make a PR for this or should it be fixed in the upcoming c-imgui version if still needed?

gucio321 commented 4 months ago

idk, could you check if this issue still exists on master?

0xkalle commented 4 months ago

I'll check. Last time I couldn't get it to run with master. I'll try again and report back. :)

0xkalle commented 4 months ago

Still persists on master as well.

It also leads to this odd window that keeps getting bigger like mentioned in #155 . But I can see the fonts in the beginning and on 125% scaling they are not the fonts they should be and not in the right size.

Here is a screen recording:

https://github.com/AllenDang/giu/assets/31923808/a76bb72c-dddf-4c90-864d-4134d23c2ee3