mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.3k stars 243 forks source link

Bad scaling when using Swing Terminal Emulator on Windows #611

Closed gonza2323 closed 1 month ago

gonza2323 commented 2 months ago

Hi,

When using the Swing Terminal Emulator on Windows (created with `defaultTerminalFactory().createTerminalEmulator()'), with scaling set to anything but 100%, the terminal's content is scaled very badly, to a point where it's hard to read the text.

From what I could find online, this is caused by Swing declaring that it's hdpi aware, despite it not being able to properly scale its contents.

There's two workarounds I've found, but neither properly fixes the issue.

  1. Overriding the scaling behavior so it's performed by "System" or "System (enhanced)", in the java.exe's or javaw.exe's compatibility settings. This makes Windows do the scaling instead of letting the app do it. It gives much better results, but it has to be set by the application user, and affects all java apps, so it's not ideal.

  2. Using System.setProperty("sun.java2d.uiScale", "1") in the application, or calling java with this parameter when running the app. This overrides Windows scaling so that the app doesn't get scaled at all. It fixes the badly scaled content, but makes the app look tiny on high resolution displays.

I was wondering if there's any way to fix this issue on the app's side. It should not declare itself as hdpi aware to Windows.

mabe02 commented 2 months ago

I remember something about this years ago; I think the behavior might be different based on what version of the JVM you are using as well. Some versions claims they are HDPI aware and others don't; Oracle changed it at one point.

In the library we have this code to try to work around this: image (AWTTerminalFontConfiguration.java)

Can you have a look with a debugger and see why it doesn't work in your case?

gonza2323 commented 2 months ago

Thanks for the quick response! When debugging, getAdjustedFontSize reads java version 21, and so returns baseFontSize. The emulator still gets (badly) scaled on a pixel per pixel basis. So it would seem newer versions of the jvm do report themselves as hdpi aware.

Nonetheless, the calculation in getHPIAdjustedFontSize wouldn't work for small 1080p screens like the one I had this issue on (a 15'' laptop), which usually run at 125% scaling.

For now I guess I'll disable scaling and try manually adjusting the font size when running on Windows. Thanks for your help!

mabe02 commented 1 month ago

Alright, if you have any suggestions for how to improve this in the library, you can re-open