Font name not found on Linux

In Java, I have some code that works well on OSX but not Linux. This code loads a font file and uses Font.createFont (). Here's the code:

log.debug("Loading ttf file AmericanTypewriter.ttf");
InputStream americanTypewriterInputStream = MyClass.class.getClassLoader().getResourceAsStream("AmericanTypewriter.ttf");
log.debug("File AmericanTypewriter.ttf loaded");
Font americanTypewriter = Font.createFont(Font.TRUETYPE_FONT, americanTypewriterInputStream);
log.debug("Font created");
americanTypewriter = americanTypewriter.deriveFont(16f); // Font size 16
log.debug("Font sized at 16");

      

As mentioned, this works well on OSX, but does not work on Linux. The actual ttf file was extracted by me on mac using:

fondu /Library/Fonts/AmericanTypewriter.dfont 

      

and grabbing the resulting AmericanTypewriter.ttf file and adding it to the java resource path.

I expected this to work on Linux too, as there is no assumption that the font is pre-installed on the host (I add it programmatically), but I might be missing something ... Can you help?

The log looks like this:

11:30:59,418 DEBUG MyClass:167 - Loading ttf file AmericanTypewriter.ttf
11:30:59,419 DEBUG MyClass:167 - File AmericanTypewriter.ttf loaded
java.awt.FontFormatException: Font name not found
  at sun.font.TrueTypeFont.init(TrueTypeFont.java:437)
  at sun.font.TrueTypeFont.<init>(TrueTypeFont.java:154)
  at sun.font.FontManager.createFont2D(FontManager.java:1476)
  at java.awt.Font.<init>(Font.java:454)
  at java.awt.Font.createFont(Font.java:761)
  ...

      

EDIT : There must be something missing here. Java saying "look, here is the ttf file, it has all the information you need", doesn't that mean it is platform independent and it doesn't matter what fonts are installed and where? The ttf file doesn't have everything it needs in java?

+2


source to share


5 answers


To answer my own question - this is a partial answer - I think the problem is the font conversion from my mac to linux box. I don't understand why this is the case, but I tried the same code in a Linux box with other random fonts that I downloaded from the internet and it worked fine, it's just this font giving me a hard time. There is no need to actually install the fonts on the box. If the font file is passed to a java program like me, then that's all you need.

What really worries me is that I expected Java to be standalone and a Java program that runs on host x with all resources passed to it should run on host y when the same resources are passed to it. I suppose there is a hidden Linux box dependency that I just don't understand.



Can anyone provide a better answer?

+1


source


Add americanTypewriterInputStream

to log message. Maybe it is null

.



If this is not the case, then the file may be damaged. Try opening it with a different tool (font installer / viewer, for example kfontview

).

0


source


What version of Linux are we talking about?

The idea of ​​including TTF fonts in Linux is fairly recent.

On Ubuntu, for example, TTF fonts are installed after the OS is installed.

You may find some helpful advice if you are looking for something to do after installing Ubuntu

I'm not sure if this will work with Java and if "American Typewriter" is one of the TTF fonts provided after installation.

EDIT . This may not be particularly relevant, but this page discusses how missing fonts from the point of view of Emacs were "restored" by a fresh installation.

0


source


Depending on where you put the file, you will need to rebuild the font cache by doing

sudo fc-cache -f -v

      

To make Ubuntu see the font exactly, you can put it in /usr/share/fonts/truetype/

and then rebuild the font-cache. If I remember correctly, you can put some fonts in ~/.fonts/

.

0


source


Our sysadmins have updated java to java version "1.6.0_39" OpenJDK Runtime (IcedTea6 1.13.11) (rhel-1.13.11.1.el6_8-x86_64) 64-bit OpenJDK server version (build 23.25-b01, mixed mode)

on Linux version 2.6.32-573.7.1.el6.x86_64 ( mockbuild@x86-031.build.eng.bos.redhat.com ) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) # 1 SMP Thu Sep 10 13:42:16 EDT 2015

I got similar problem by installing them using yum

dejagnu.noarch                                         
dejavu-sans-mono-fonts.noarch                          
dejavu-serif-fonts.noarch                              

      

0


source







All Articles