Clemens
Clemens

Reputation: 99

Java 8 get Path to Font

Is there a way to get the Path for a Font, regardless of the operating system?

e.g

String path = getPathToFont("Arial");

Upvotes: 3

Views: 2587

Answers (2)

david
david

Reputation: 1016

No, there isn't.

Since Java 1.3, the createFont() method allows Java code to start drawing with a TrueType or PostScript font brought in from a file, or any other source of an InputStream, such as a JAR resource, network socket, or decrypted byte array. There's also a method that does take a File, but there's no method that returns the File even for fonts created that way.

Furthermore, the system-provided fonts (from GraphicsEnvironment.getAllFonts()) are not required to only be TrueType or PostScript fonts. They could be in some OS-specific format, or some implementation-private format; and they might not be loaded from a file at all, instead backed by static data (in a class or native code), or drawn purely algorithmically.

Upvotes: 2

Nathan Dunn
Nathan Dunn

Reputation: 447

I would use Commons IO FileUtils.

File fontFile = FileUtils.getFile("Arial.ttf") ;

This will probably execute from your classpath, though.

Are you wanting to get the "system" fonts or the path to the system fonts? For the former do something like this:

GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()

Upvotes: 0

Related Questions