Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Suraj-Yadav opened this issue Aug 27, 2023 · 6 comments
Open

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

Suraj-Yadav opened this issue Aug 27, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@Suraj-Yadav
Copy link

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 Suraj-Yadav added the bug Something isn't working label Aug 27, 2023
@wilsonmfaria
Copy link

wilsonmfaria commented Oct 3, 2023

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

wilsonmfaria commented Oct 5, 2023

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

@0xkalle
Copy link

0xkalle commented Feb 9, 2024

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

@gucio321
Copy link
Collaborator

gucio321 commented Feb 9, 2024

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

@0xkalle
Copy link

0xkalle commented Feb 9, 2024

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

0xkalle commented Feb 9, 2024

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: #759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants