AllenDang / giu

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

CalcTextSize Width is 2 times too big #352

Open snhmibby opened 2 years ago

snhmibby commented 2 years ago

If I make a program that sets item spacing and frame padding to 0, sets a monospace font, then calls GetAvailableRegion and CalcTextSize and tries to fill the screen with characters, only half the width of the screen is actually filled. It seems that CalcTextSize returns a width that is 2 times too big? Below is an example program that tries to fill the screen with "."'s, but only succeeds in filling the left half.

package main

import (
    "fmt"

    G "github.com/AllenDang/giu"
    I "github.com/AllenDang/imgui-go"
)

func draw() {
    G.SingleWindow().Layout(
        G.Child().Layout(
            G.Custom(func() {
                I.PushStyleVarVec2(I.StyleVarFramePadding, I.Vec2{X: 0, Y: 0})
                I.PushStyleVarVec2(I.StyleVarItemSpacing, I.Vec2{X: 0, Y: 0})

                w, h := G.GetAvailableRegion()
                cw, ch := G.CalcTextSize(".")
                numline := int(h / ch)
                numchar := int(w / cw)
                fmt.Printf("w=%f, h=%f, cw=%f, ch=%f, numline=%d, numchar=%d\n", w, h, cw, ch, numline, numchar)
                for i := 0; i < numline; i++ {
                    for j := 0; j < numchar; j++ {
                        if j != 0 {
                            I.SameLine()
                        }
                        I.Text(".")
                    }
                }

                I.PopStyleVarV(2)
            }),
        ),
    )
}

func main() {
    //set a monospace font
    G.SetDefaultFont("DejavuSansMono.ttf", 12)
    w := G.NewMasterWindow("Test", 800, 600, 0)
    w.Run(draw)
}
AllenDang commented 2 years ago

@snhmibby Update to newest code and try again.

snhmibby commented 2 years ago

I updated to use the latest code with

go get -u github.com/AllenDang/giu@master

but the above program still only fills half the screen with .'s

snhmibby commented 2 years ago

It's a little bit confusing, if I change the call to giu.CalcTextSize with a call to imgui.CalcTextSize, I can get 2 different cases (both wrong?) depending on the value of wrapwidth argument. If wrapwidth is 0, i get a character width of 13.0 (should be 7.0), if wrapwidth is >0, the height of my single character doubles from 12 to 24.0

gucio321 commented 2 years ago

@snhmibby try giu.CalcTextSizeV(".", true, 0) it gives a result you expect, but idk why :grinning: because a value of hideAfterDoubleHash shouldn't metter here :confused:

snhmibby commented 2 years ago

I wouldn't expect that also since there is no ## in my string. Anyway, I'll use this workaround and leave the issue open! Thanks a lot!

gucio321 commented 2 years ago

@AllenDang is it expected behavior or there is some bug in imgui-go wrapper / Dear ImGui?

AllenDang commented 2 years ago

@snhmibby If you are testing 2 days ago, giu will multiply the size by DPI scaling when CalcTextSize is called. For newest code of giu, the DPI scaling is removed, so you should be able to get consistent value with giu.CalcTextSize and imgui.CalcTextSize.

snhmibby commented 2 years ago

Thanks! I'll try it out later today when i get home from work

snhmibby commented 2 years ago

I'm still getting the wrong text size in the x-direction with the same testprogram as above after updating to github.com/AllenDang/giu v0.5.7-0.20210930062057-7a1a262cf0eb

gucio321 commented 4 months ago

@snhmibby could you check on latest master, this should work on cimgui-go