views:

394

answers:

3

Hello ,

I have a Font object in Java for a font file ... I need to convert that object to a File object or get the font file path ... So i can pass it to another method.

Is there a way to do this ?

Thanks ...

---------------------------------------------------------- Edit: More explanation :

What i'm doing here is calling a method from an external library that loads a font file to use it in writing :

loadTTF(PDDocument pdfFile, File fontfile);

So i wanted to let the user choose a font from the ones defined in his operating system using :

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts();

Then when the user chooses a font from them i pass it to the loadTTF(...) method to load it. Is there a bad practice here ?

A: 

No.

A Font in Java is just a representation and definition of how characters can be displayed graphically. It has nothing to do with the filesystem, and technically need not even be ultimately defined in a file (see for example the createFont() method that takes an arbitrary input stream, which could come from anywhere e.g. a network socket). In any case, it would certainly be a ridiculous break in abstraction for you to be able to get the path of the underlying system file that defines the font.

I would suggest that you might be doing the wrong thing in your other method if you're relying on accepting a file. Or if this really is needed, then you're doing the wrong thing in this method by thinking that a Font object has a simple correlation to an underlying file. If you really need to get the file path of a particular font you'll need to approach it from a different angle that doesn't involve java.awt.Font.

Andrzej Doyle
+1  A: 

Ok ... This will return the font file path :

String fontFilePath = FontManager.getFontPath( true ) + "/" + FontManager.getFileNameForFontName( fontName );

I have tried this in Windows and Linux and in both it worked fine.

Brad
A: 

How do I can get it in jdk 1.4?

eldest