Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install minimal set of fonts required for Java 9 openjdk application

Tags:

java

fonts

I have a blackbox linux machine, it only comes with one font /usr/lib/fonts/ipag.ttfbut it doesnt seem to be properly installed and a custom openjdk 9 installation

The openjdk installation comes without any fonts, but I tried creating a fonts directory and copying this font into it but it had no effect.

When I run my non gui application it requires fonts in order to create a excel spreadsheet with jakarta-poi, but it fails with:

Caused by: java.lang.NullPointerException
    at java.desktop/sun.awt.FontConfiguration.getVersion(Unknown Source)
    at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(Unknown Source)
    at java.desktop/sun.awt.FontConfiguration.init(Unknown Source)
    at java.desktop/sun.awt.X11FontManager.createFontConfiguration(Unknown Source)
    at java.desktop/sun.font.SunFontManager$2.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.desktop/sun.font.SunFontManager.<init>(Unknown Source)
    at java.desktop/sun.awt.FcFontManager.<init>(Unknown Source)
    at java.desktop/sun.awt.X11FontManager.<init>(Unknown Source)

I then tried running

fc-cache -rv /usr/lib/fonts

This reported that font was installed (although fc-list returns nothing)

Now my application gets a bit further, but still fails, reporting

Caused by: java.lang.NullPointerException
    at java.desktop/sun.awt.FcFontManager.getDefaultPlatformFont(Unknown Source)
    at java.desktop/sun.font.SunFontManager$2.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.desktop/sun.font.SunFontManager.<init>(Unknown Source)
    at java.desktop/sun.awt.FcFontManager.<init>(Unknown Source)
    at java.desktop/sun.awt.X11FontManager.<init>(Unknown Source)

So how do I resolve this, I'm unclear what the minimal font set required for my Java application to work. Im unclear do I just need to provide fonts or a fonts.properties file as well, or is that file only used by Oracle jres rather than OpenJdk

I had the same problem with OpenJdk1.8, however from memory simply creating a fonts folder and putting a font into it seemed to work, but that is not working now with this openjdk 9 version.

I can copy fonts onto the machine, but I cant run install commands to actually install fonts directly onto the machine.

Update

I then copied over the Lucida fonts provided with Oracle jre and put into jre/libs/font and ran fc-cache -rv on that folder and it now works, why wasn't the ipag.ttf font enough ?

and why do I have to run fc-cache cant java just picks up the fonts by finding them in the fonts folder.

like image 562
Paul Taylor Avatar asked Nov 07 '22 10:11

Paul Taylor


1 Answers

Recent openjdk and java versions use fontconfig that can choose the appropriate fonts (or font substitutions) on a wide range of locales (for example build text from several fonts when no system font has sufficient coverage alone). It is much more complete and accurate than the old system that only covered a limited set of encoding and locales, could only make simple decisions and required manual declaration of font substitution order in fonts.properties files. Basically the old system could not scale to the number of locales in the world and the complexity of today's unicode

But to compute the correct choices fontconfig needs fonts to be analyzed and indexed first. That's what fc-cache is about. Fontconfig can not just read font files on demand, the Google Noto font family weights gigabytes alone for example, it needs to pre-compute an index to help choose quickly the right file or files whenever there is a run of text to render. Lucida is a legacy font with toy (by current standards) unicode coverage.

Since the whole point of fontconfig is to avoid the huge computing hit of reading numerous complex font files at the last minute just because they are there, it relies on whoever has installed the system to generate its index (cache) at the least problematic moment. Linux distributions like RHEL/Centos do it automatically when you install a font package for example.

I don't remember if you can configure fontconfig to watch its font directories and autogenerate new caches whenever they change. That would probably not be a great idea, indexing complex opentype unicode fonts is very computing intensive and people have been known to move files around a log while putting their font directories in order.

The actual directories where fontconfig expect to find fonts to index depend on the configuration of your system.

Why ipag.ttf was not enough? That depend on the content on the file. A ttf file can contain tens of thousands of glyphs or just a single vanity symbol useless for writing text.

like image 155
nim Avatar answered Nov 15 '22 07:11

nim