glfw icon indicating copy to clipboard operation
glfw copied to clipboard

How to convert points to pixels for a non-fullscreen window ?

Open mulle-kybernetik-tv opened this issue 5 years ago • 4 comments

To convert font sizes from the somewhat more familiar points to pixels, I should be using the formula pixels = points * PPI / 72 AFAIK. But how do I get the PPI (pixel per inch/DPI) for my window ?

It would seem, I need to get the monitor first, on which my non-fullscreen window resides. And then get the monitors physical size and current resolution. The function glfwGetWindowMonitor will return NULL for windowed mode windows, though.

OK, so I might have to fallback to glfwGetPrimaryMonitor. The physical size I could get with glfwGetMonitorPhysicalSize. But the video mode I get from glfwGetVideoMode isn't expressed as pixels but as screen coordinates, which seems possibly scaled already ? This may still be what I want, but I am not sure.

So what I have is this, converting mm to inch:

monitor = glfwGetPrimaryMonitor();
glfwGetMonitorPhysicalSize( monitor, &w, &h);
mode = (GLFWvidmode *) glfwGetVideoMode( monitor);
_primaryMonitorPPI = mode->height / ((float) h * 0.03937007874);

Ideally of course I would just like to ask the window its PPI :)

mulle-kybernetik-tv avatar Jul 22 '19 23:07 mulle-kybernetik-tv

~~I agree it would be nice to just ask glfw for the dpi of a window. Windows' API for this actually looks sensible (assuming it works as documented): https://docs.microsoft.com/en-gb/windows/win32/api/winuser/nf-winuser-getdpiforwindow~~

Clearly I don't know this part of glfw as well as I thought. Please see https://www.glfw.org/docs/latest/group__window.html#gaf5d31de9c19c4f994facea64d2b3106c Not sure how to get the system's default DPI though...

tombsar avatar Jul 23 '19 00:07 tombsar

glfwGetWindowContentScale gives you something different. I get 1.1 for example, which is what I put in to tweak my fonts to appear larger (on Linux). PPI is not really derivable by it IMO.

mulle-kybernetik-tv avatar Jul 23 '19 10:07 mulle-kybernetik-tv

@mulle-kybernetik-tv Did you ever resolve how to best get the DPI you want? I have the exact same question.

dantamont avatar Sep 18 '22 04:09 dantamont

Nope. It's gonna bite be some time, but I am running with the naive code I outlined above for now.

mulle-kybernetik-tv avatar Sep 18 '22 23:09 mulle-kybernetik-tv