giu icon indicating copy to clipboard operation
giu copied to clipboard

[bug] g.Style().SetFontSize is not working on windows

Open Suraj-Yadav opened this issue 2 years ago • 9 comments

What happend?

I was running examples/setstyle but all the text are of same size. I expected large label to be bigger. image

This is happening for latest commit as well as v0.6.2.

I looked into the code and i think i found the reason for this. (Code from https://github.com/AllenDang/giu/blob/master/FontAtlasProsessor.go#L300C1-L316C3)

	// Add extra fonts
	for _, fontInfo := range a.extraFonts {
		// Scale font size with DPI scale factor
		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[fontInfo.String()] = &f
	}

Here for Windows we change the size in fontInfo struct and the key for extraFontMap is generated from this updated fontInfo struct. But the struct in extraFonts still stores the older size, which when used for PushFont never finds the data in extraFontMap (here).

Code example

main.go
package main

import (
	"image/color"

	g "github.com/AllenDang/giu"
)

func loop() {
	g.SingleWindow().Layout(
		g.Style().
			SetColor(g.StyleColorText, color.RGBA{0x36, 0x74, 0xD5, 255}).
			To(
				g.Label("I'm a styled label"),
			),
		g.Style().
			SetColor(g.StyleColorBorder, color.RGBA{0x36, 0x74, 0xD5, 255}).
			SetStyle(g.StyleVarFramePadding, 10, 10).
			To(
				g.Button("I'm a styled button"),
			),
		g.Button("I'm a normal button"),
		g.Style().
			SetFontSize(60).To(
			g.Label("large label"),
		),
	)
}

func main() {
	wnd := g.NewMasterWindow("Set Style", 400, 200, g.MasterWindowFlagsNotResizable)
	wnd.Run(loop)
}

To Reproduce

  1. Run example from latest commit go run github.com/AllenDang/giu/examples/setstyle
  2. Run example from v0.6.2 go run github.com/AllenDang/giu/examples/[email protected]

Both show the same output. image

Version

master

OS

Windows 10

Suraj-Yadav avatar Aug 27 '23 06:08 Suraj-Yadav

Any updates on this issue?

It seems that it is still broken...

I didn't get it... at first, when I played with the FontAtlas stuff, it rendered the fonts ok.. After some adjustments, it completely stopped working.. It seemed to work only when g.MasterWindowFlagsNotResizable is passed..

wilsonmfaria avatar Oct 03 '23 15:10 wilsonmfaria

Well.. after some research I managed to make it work on Windows...

Basically you need to create a "yourprogram.manifest" file with this info:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="Any Cool Name" type="win32"/>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
        </dependentAssembly>
    </dependency>
    <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings>
            <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
            <gdiScaling xmlns="http://schemas.microsoft.com/SMI/2017/WindowsSettings">true</gdiScaling>
        </windowsSettings>
    </application>
</assembly>

Use a tool like this one available for Go: https://github.com/akavel/rsrc to generate a .syso file like so:

  1. go get github.com/akavel/rsrc
  2. go install github.com/akavel/rsrc
  3. rsrc -manifest yourprogram.manifest -o rsrc.syso -ico .\icon.ico

After generating the .syso file, you can simply go run . or go build -ldflags "-s -w -H=windowsgui -extldflags=-static" .

These are the magic lines that made the fonts work as intended..

<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
<gdiScaling xmlns="http://schemas.microsoft.com/SMI/2017/WindowsSettings">true</gdiScaling>

imagem_2023-10-05_115708112

wilsonmfaria avatar Oct 05 '23 14:10 wilsonmfaria

@wilsonmfaria that is a nice workaround.

The one thing happening here is, that g.Context.GetPlatform().GetContentScale() will always return 1 with that. Therefore it is hard to position and size a window correctly by code.

Does somebody have an idea, how I could get the "physical/real" resolution of a display by code?

My problem is, that all methods e.g. w32.GetSystemMetrics(w32.SM_CXSCREEN) or w32.GetDeviceCaps(hDc, w32.HORZRES) return only the resolution after the scale is applied. This in my case means 4096 instead of 5120 = 1.25 * 4096. That leads to me setting a window to small because g.NewMasterWindow("my window", x, y, 0) expects xand yto be the size without scale applied.

Would be awesome if somebody has an idea, as without wilsonmfarias workarround the textwill all be in default font.

0xkalle avatar Feb 09 '24 08:02 0xkalle

As i can't check it right now but i suggest to try the latest master which uses cimgui-go instead of imgui-go. Maybe this works there...

gucio321 avatar Feb 09 '24 10:02 gucio321

I think there is a different underlying issue with the font atlas. I threw together a simple example. Will post it in a second. (Also tried the master with it, but crashes.) One moment :)

0xkalle avatar Feb 09 '24 10:02 0xkalle

Added my findings in a new issue, as I think it is not related to the SetFontSize but to AddFont or the Font Atlas. Issue is here: https://github.com/AllenDang/giu/issues/759

0xkalle avatar Feb 09 '24 10:02 0xkalle

Please check on v0.8.1 and confirm it still doesn't work

gucio321 avatar Jul 07 '24 21:07 gucio321

@gucio321 I switched to Fedora, so I don't have a windows system to test this now.

I tried to run it inside Virtual Box on Windows 11, the issue is still there.

Suraj-Yadav avatar Jul 08 '24 08:07 Suraj-Yadav

@gucio321 I switched to Fedora, so I don't have a windows system to test this now.

Nice to hear 😁

I tried to run it inside Virtual Box on Windows 11, the issue is still there.

Ok, got it

gucio321 avatar Jul 08 '24 09:07 gucio321