24 August 2018

Java: Use Arial Fonts as Default Swing Fonts on Linux

I guess we all agree that sometimes the default fonts that Java uses on Linux are pretty ugly, doesn't it ? This issue may cause also problems when implementing GUI projects on Linux that will be used later on Windows machines: The default font used on Linux may have different dimensions / outlook than the fonts used on Windows, causing differences in the flow of text or / and messing up components in windows.


A quick-and-dirty solution to overcome this situation is to install and use Microsoft's free TrueType Core fonts on Linux and configure Java to use them. I prefer the Arial fonts, you can try any other available type you wish.  Just follow the next steps described in this document to install and configure Java accordingly. 

(!) Note that this configuration has been tested and working on Linux Mint 18.3 distributions (both MATE and Cinnamon) with Oracle Java 7 and Oracle Java 8.

Instructions

1) Using your package manager, install the ttf-mscorefonts-installer package. This will install the free TrueType Core fonts. You can skip this step if they're already installed on your system.

2) Open this file in a text editor (even better, keep a backup of the file FIRST) and do the following :

morbid@inferno:~$ sudo nano /etc/fonts/conf.avail/60-latin.conf

* Add the Times New Roman font family to the top of the list for the serif fonts.
* Add the Arial family to the top of the list for the sans-serif fonts.
* Add the Courier New font family to the top of the list for the monospace fonts.
* Save and close the file. 

(Alternatively,  instead of editing, you can download and use this version of the file). 

3) Run this command to update fonts:

morbid@inferno:~$ sudo dpkg-reconfigure fontconfig

This command will regenerate the fonts cache. 

Additional Steps If The Fonts Remained As Is

a) The new fonts will not be used if an operating system specific Java fontconfig property file exists that matches the operating system being used. If such a file exists it must be removed. Here is an example of removing such a file: 

morbid@inferno:~$ cd /usr/lib/jvm/java-8-oracle/jre/lib/
morbid@inferno:~$ sudo rm fontconfig.Ubuntu.properties

(keep a backup of the file FIRST, you may want to revert to your old configuration!)

b) Be aware that in later versions of Linux (in my case, Mint 18.3), the DejaVu font files start with a lower number in /etc/fonts/conf.avail so it has a higher priority than 60-latin.conf we changed earlier. You can fix this like that:

* Either comment out the relevant sections of the DejaVu files or (even better) rename the Latin file to a lower number: I renamed my file as 56-latin.conf (just make sure that the number you will use does not exist in any other file!)

* If you chose to rename the file, you need to create a new soft link in /etc/fonts/conf.d and remove the broken link:

morbid@inferno:~$ cd /etc/fonts/conf.d
morbid@inferno:~$ sudo rm 60-latin.conf
morbid@inferno:~$ sudo ln -sv /etc/fonts/conf.avail/56-latin.conf 56-latin.conf

Regenerate Font cache again: 

morbid@inferno:~$ sudo dpkg-reconfigure fontconfig

 ... and you're ready!


That's it! The next time you launch a Java application the Windows fonts will be used.

(*) Info found on Perfect Abstractions Blog

No comments:

Post a Comment