Java, Semeru Runtimes and Runtimes for Business

Java, Semeru Runtimes and Runtimes for Business

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only

A Developer's Guide to Font Setup and Configuration in Java

By Pasam Soujanya posted yesterday

  

If you're working with fonts in a Java application, The IBM Semeru JDK proves to be an efficient option. It’s not just open-source, but also widely adopted—it also handles font rendering quite well. Its built-in support for logical and physical fonts, Unicode text rendering, and integration with native font systems provides a strong foundation for building cross-platform applications.

Before diving into how The IBM Semeru JDK efficiently handles fonts, let’s first get familiar with the concepts of FontConfiguration, different types of Fonts and how they are managed. 

What is FontConfiguration:

FontConfig refers to a setup or configuration used by Java runtime to customize, locate and render fonts on systems. This configuration allows java applications to discover available fonts and map them with the physical fonts on the system.  If the mapping of logical names with physical fonts fails, then the application throws an error.

What are Logical Fonts:

Logical Fonts in Java are symbolic names that the Java Virtual Machine (JVM) maps to physical fonts available in the underlying operating system. The primary purpose of these fonts is to provide a level of platform independence for font rendering in Java applications. 
The five logical font family names are: 

  • Serif: Typically mapped to a font with serifs, like Times New Roman.  
  • SansSerif: Typically mapped to a font without serifs, like Arial or Helvetica. 
  • Monospaced: Typically mapped to a fixed-width font, like Courier New or Consolas.
  • Dialog: Intended for use in dialog boxes and user interface elements.
  • DialogInput: Intended for use in text input fields within dialog boxes.   
 

When fontconfig mapping fails to locate the logical font, then the application throws an error
“Can’t find font for logical font name ‘Dialog’” 

What are Physical Fonts:

Physical fonts refer to the actual, real font files installed on a system that contains glyph (the visual representation of a character in a specific font) data for rendering text. These fonts vary between configurations; some of the examples of these fonts are Helvetica, Palatino, HonMincho etc., 


When the logical font fails to map the physical font on the system, then the application throws an  

“Exception in thread "main" java.lang.Error: Probable fatal error: No physical fonts found.” 

Sample Java code that throws an exception if logical and physical fonts map fails on the system 

import javax.swing.*; 
import java.awt.*; 
  
public class AvailableFonts { 
    public static void main(String[] args) { 
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
        String[] fontFamilies = ge.getAvailableFontFamilyNames(); 
  
        System.out.println("Available Font Families:"); 
        for (String font : fontFamilies) { 
            System.out.println(font); 
        } 
        // Create a specific font 
        String fontName = "Dialog"; // Change to any available font family 
        int fontStyle = Font.BOLD; 
        int fontSize = 18; 
  
        Font customFont = new Font(fontName, fontStyle, fontSize);  //may throw error, if fonts not found                                                                                                    
        System.out.println("Font Name: " + customFont.getFontName()); 
    } 
} 

Usage of fontconfig: 


In the context of Java, AWT (Abstract Window Toolkit) and Swing libraries use fontconfig internally for rendering fonts. This is especially important for GUI apps that display text. FontConfig files are available in both properties and binary formats. 
   


How Java maps fontconfig:

Java runtime picks the appropriate font configuration file based on below information: 

  1. OS specific files:  FontConfiguration class is responsible for finding the OS release/version based on the release files. Once these fontconfig files are found, logical fonts are mapped with physical fonts.
    Ex:- If a user is on a SuSE machine and his application makes use of Dialog font, then java calculates the OS name and version based on release files like /etc/SuSE-release, then maps to SuSE fontconfig properties present under $JAVA_HOME/jre/lib. Dialog font is the logical name which tries to map its physical font on the system using a SuSE specific configuration i.e, $JAVA_HOME /lib/fontconfig.OS.Version.properties 


    $JAVA_HOME /lib/fontconfig.OS.properties

  2. Default files: If FontConfiguration class is unable to find OS specific files, then java runtime fallback to the default configuration and renders the fonts based on the physical fonts available on the system.

    

$JAVA_HOME /lib/fontconfig.properties
  3. User/custom files: When a user wants to use custom fonts according to their requirements, then they can create fontconfig files by following default fontconfig file format. Users must use a system property for the java runtime to use this configuration. Custom/user configuration files are used when user has specific requirements for fonts, or when OS specific files aren’t picked by runtime, or default configuration fails in rendering fonts. 



    -Dsun.awt.fontconfig=<path to custom fontconfig files>

Migration to The IBM Semeru JDK:


Due to modularity, The IBM Semeru JDK doesn’t ship any fonts with Runtime keeping the SDK lighter. The IBM Semeru JDK applications are entirely dependent on the fonts installed on the operating system.  
libfontmanager.so in the JDK is responsible for initializing fonts on the system. Java FontManager acts as an interface between Java Fonts (java.awt.Font) and the underlying font files/native font resources installed on the Operating System.

Can we use custom fonts with the Semeru JDK? Yes, it is possible to use custom fonts with the IBM Semeru JDK by passing the below system property. 
-Dsun.java2d.fontpath=<path to custom font folder on the system> 

Ex: -Dsun.java2d.fontpath=/usr/local/myfonts 

Generic Debugging Options:

To debug font related issues in Java, the options below can clarify the font configurations. 

  1. -Dsun.java2d.debugfonts=true: When enabled with Java application, this logs what fonts are being loaded and used during runtime. 
Sample debug log looks like below:
    $ java -Dsun.java2d.debugfonts=true  AvailableFonts  
    Jul 10, 2025 11:58:30 AM sun.font.FontUtilities logInfo 
    Jul 10, 2025 11:58:31 AM sun.awt.FontConfiguration findFontConfigFile 
    INFO: Did not find a fontconfig file. 
    Jul 10, 2025 11:58:31 AM sun.font.FontUtilities logInfo 
    INFO: successfully parsed the fontconfig file at
  2. fc-list: This is a UNIX command that works on Linux and MAC. It lists what fonts are available or installed on the system.  
To use fc-list on MAC, please install fontconfig prior as below
    brew install fontconfig 
  3. Use dir "C:\Windows\Fonts" to list available fonts on Windows systems. 

Conclusion: 


In summary, understanding how FontConfiguration works in Java is essential to managing how text is rendered across platforms and environments. This blog post focuses on understanding different kinds of fonts and how they can be customized according to user requirements. This blog also covers how FontConfiguration is used in the IBM Semeru JDK along with helpful debugging tools. 

Reference Links:

  1. Introduction to the IBM Semeru Runtimes: https://developer.ibm.com/blogs/introducing-the-ibm-semeru-runtimes/ 
  2. Physical and Logical Fonts: https://docs.oracle.com/javase/tutorial/2d/text/fonts.html 
  3. Font Configuration Files: https://docs.oracle.com/en/java/javase/17/intl/font-configuration-files.html#GUID-304F2802-A1D0-4A82-83F3-C6DCB141906E 

  4. Pre-requisites for Font Support: https://blog.adoptopenjdk.net/2021/01/prerequisites-for-font-support-in-adoptopenjdk/ 
0 comments
27 views

Permalink