mabe02 / lanterna

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

Font looking different on linux and on windows #609

Closed Zabbum closed 4 months ago

Zabbum commented 4 months ago

I include truetype font in resources and import it to the application like that:

// Set font
InputStream inputStream = Application.class.getClassLoader()
  .getResourceAsStream("font/C64_Pro_Mono-STYLE.ttf");
Font font = null;
try {
  Font fontTmp = Font.createFont(Font.TRUETYPE_FONT, inputStream);
  font = fontTmp.deriveFont(fontSize);
} catch (IOException | FontFormatException e) {
  e.printStackTrace();
  return;
}
inputStream.close();
// Config terminal
DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory();
terminalFactory.setTerminalEmulatorFontConfiguration(
  SwingTerminalFontConfiguration.newInstance(font));

The application is being develped on linux and everything works great (screenshot below). Screenshot from 2024-07-15 21-17-23

But when i run the jar (or compile it from zero) on windows the font is completely broken. Screenshot from 2024-07-15 21-21-59

For the fully colored tile i use Û character (Unicode Value U+00db). The font used by me is C64 Pro Mono STYLE from https://style64.org/c64-truetype. I use amazon corretto 17 JDK.

Thank you in advance.

avl42 commented 4 months ago

In what kind of "terminal" does this run on windows? cmd.exe ? cygwin ? ... ? or as a graphical application?

Setting the font (I haven't done that, so cannot say, if it's done right) can only work when running as a graphical application.

Another question: is the block-character (of which the large "OEL" consists) the same as the one in the bottom lines e.g. enclosing the text "ZABBUM"? if the latter are just blanks in reverse-color mode, maybe you could use that trick also for the large "OEL" ?

Zabbum commented 4 months ago

In what kind of "terminal" does this run on windows? cmd.exe ? cygwin ? ... ? or as a graphical application?

It's graphical application I suppose based on that it creates Swing frame. To config and create terminal i use:

// Config terminal
DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory();
terminalFactory.setInitialTerminalSize(new TerminalSize(53, 34));
terminalFactory.setTerminalEmulatorFontConfiguration(SwingTerminalFontConfiguration.newInstance(font));
terminalFactory.setTerminalEmulatorTitle("Window title");

// Initialize terminal
Terminal terminal = terminalFactory.createTerminalEmulator();

Setting the font (I haven't done that, so cannot say, if it's done right) can only work when running as a graphical application.

So i do. (I hope that in right way)

Another question: is the block-character (of which the large "OEL" consists) the same as the one in the bottom lines e.g. enclosing the text "ZABBUM"? if the latter are just blanks in reverse-color mode, maybe you could use that trick also for the large "OEL" ?

In this particular case this would work but there are also other ASCII arts that seem broken. Now i observed that £ character is broken as well but in a bit different way. (screenshots below)

Linux image

Windows image

avl42 commented 4 months ago

it looks like your application outputs utf-8 but the windowing system expects iso-8859-1 or similar. are the strings embedded in java-source or loaded from a file at runtime?

maybe you could add debugging code (or use the debugger) to see if the string length of the string containing the graphical chars or £ is already wrong... that may help finding out if the problem happens on output, or alreadx on reading the strings from some file...

Paweł Kubas @.***> schrieb am Sa., 20. Juli 2024, 22:45:

In what kind of "terminal" does this run on windows? cmd.exe ? cygwin ? ... ? or as a graphical application?

It's graphical application I suppose based on that it creates Swing frame. To config and create terminal i use:

// Config terminalDefaultTerminalFactory terminalFactory = new DefaultTerminalFactory();terminalFactory.setInitialTerminalSize(new TerminalSize(53, 34));terminalFactory.setTerminalEmulatorFontConfiguration(SwingTerminalFontConfiguration.newInstance(font));terminalFactory.setTerminalEmulatorTitle("Window title"); // Initialize terminalTerminal terminal = terminalFactory.createTerminalEmulator();

Setting the font (I haven't done that, so cannot say, if it's done right) can only work when running as a graphical application.

So i do. (I hope that in right way)

Another question: is the block-character (of which the large "OEL" consists) the same as the one in the bottom lines e.g. enclosing the text "ZABBUM"? if the latter are just blanks in reverse-color mode, maybe you could use that trick also for the large "OEL" ?

In this particular case this would work but there are also other ASCII arts that seem broken. Now i observed that £ character is broken as well but in a bit different way. (screenshots below)

Linux image.png (view on web) https://github.com/user-attachments/assets/48442fec-ac7c-46c9-af40-10ca4af110c4

Windows image.png (view on web) https://github.com/user-attachments/assets/f9031a9a-5165-45df-b0f6-724fe2638419

— Reply to this email directly, view it on GitHub https://github.com/mabe02/lanterna/issues/609#issuecomment-2241289997, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIDBMXEED5YBBMXFXYO2WDZNLEAHAVCNFSM6AAAAABK5F236OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGI4DSOJZG4 . You are receiving this because you commented.Message ID: @.***>

Zabbum commented 4 months ago

it looks like your application outputs utf-8 but the windowing system expects iso-8859-1 or similar. are the strings embedded in java-source or loaded from a file at runtime?

They are loaded from a file.

maybe you could add debugging code (or use the debugger) to see if the string length of the string containing the graphical chars or £ is already wrong... that may help finding out if the problem happens on output, or alreadx on reading the strings from some file...

Yeah, now I tried to do it with the Û char (present in file) and it seems that in memory it is present as Ă›.

Enforced using UTF-8 in reading a file and now works perfectly!

void textLoader(InputStream inputStream) {
    // Transform InputStream to File
    File dataFile = File.createTempFile("art", ".json");
    FileOutputStream out = new FileOutputStream(dataFile);
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }
    out.close();

    // Create JSONObject
    JSONParser parser = new JSONParser();
    FileReader fileReader = new FileReader(dataFile, StandardCharsets.UTF_8);
    Object object = parser.parse(fileReader);
    fileReader.close();
    JSONObject dataObject = (JSONObject) object;
}

Thank you very much! @avl42