3

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

e.g

String path = getPathToFont("Arial");
Clemens
  • 99
  • 1
  • 10

2 Answers2

2

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.

david
  • 997
  • 6
  • 15
0

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()

Community
  • 1
  • 1
Nathan Dunn
  • 447
  • 5
  • 16
  • I need the absolute path to a font. e.g "C:\WINDOWS\Fonts\arial.ttf" – Clemens Nov 11 '15 at 18:37
  • Do you mean the system fonts? If you know what your fonts directory is and you're loading the Fonts from there, you can use FileUtils. Are you trying to say (1) for a given Font object, can I can get the absolute file file? (2) can I find out where the system fonts directory is? I think its the former, but its unclear. If you could expand on your use-case that might help. – Nathan Dunn Nov 11 '15 at 19:14
  • @NathanDunn second link is 404. – luckydonald Sep 16 '19 at 18:06