tags:

views:

626

answers:

6

Helvetica is available in one form or another on Windows, Mac OS X, and Linux. Under Windows, I can see it from Microsoft Word. On the two UNIX platforms, I can find it with xlsfonts | grep -i helvetica; the name seems to be adobe-helvetica.

But the JDK can't find it! It's not listed from GraphicsEnvironment.getAllFonts(), nor does it come up when I call new Font("Helvetica", ...) [using several name variants, either, like "Adobe Helvetica"]; instead I get defaulted to "Dialog" font and it seems to name it with whatever name I used.

This font is available on every relevant Java target platform today. How do I use it from within Java?

+2  A: 

I realize that this isn't actually answering the question, but...

On Windows, Helvetica isn't always installed. My machine at work (the one I'm using now) doesn't, despite having Microsoft Office XP.

R. Bemrose
Microsoft uses Arial which is pretty much a rejiggered Helvetica
Nick
Arial is sufficiently different from Helvetica to be an unacceptable substitute for those who care about such things. And it certainly wouldn't be selected automatically when you search for "Helvetica".
Mark Ransom
And to comment on the answer, instead of commenting on the comment - Helvetica is probably quite rare on Windows machines, especially for developers. It has never been bundled with any Microsoft product: http://www.microsoft.com/typography/fonts/default.aspx
Mark Ransom
A: 

Oficially, the only fonts guaranteed to come with Java are the Lucida fronts (Bright, Sans, and Typewriter). Depending on the install and configuration of Java, it may be able to read the front from your machine's font directory, but that's not guaranteed.

If it has to be Helvetica, you can bundle the font with Font.CreateFont(), passing in an int representing the type (Font.BOLD, or Font.PLAIN, etc), and an InputStream to the TrueType font.

Matt Poush
+2  A: 
Font f = new Font("Helvetica", Font.PLAIN, 10);  // make a new font object

ObjectName.setFont(f); // set the objects font using setFont();

where "Helvetica" is the font, Font.PLAIN defines the style, and 10 defines the size. Of course it must be installed to work, and you can bundle it using CreateFont().

Try the Java API for Fonts for more reference.

John T
As specified in the question, when I make that new Font() call, I don't get the font.
skiphoppy
Then you must use Font.CreateFont() and bundle it with your application
John T
A: 

I think if you put the the font file in your src folder and call it from there it will work.

nunolourenco
+1  A: 

On Linux, there is a font.properties file somewhere in the jre or jdk directory that you're supposed to be able to add new fonts to, but I've tried a bazillion different things to add fonts to it, even ones that are known to the OS and show up in xlsfonts, and never gotten it to work.

Paul Tomblin
Yeah, I spent some time with that this morning. I was hoping I could make my own local font.properties file, outside of the JDK. But even the one in java.home doesn't seem to work. And I read somewhere this file is unsupported on Windows, anyway.
skiphoppy
A: 

Installing TrueType fonts is usually pretty straightforward. I use maven for my project so I just keep the font file in my resources directory and grab it using a class loader.

InputStream is = MyClass.class.getClassLoader().getResourceAsStream("my/package/Avenir.ttf");
Font f = Font.createFont(Font.TRUETYPE_FONT, is);
is.close();

That font should be loaded into memory to be used elsewhere in the app using the font name. You can make sure this worked and see what fonts are available to you on your system by using the code below.

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();

for(String name : fontNames)
 System.out.println(name);
Zack
I'm 98% certain getAvailableFontFamilyNames() doesn't have new created fonts made available to it when you create a font with Font.createFont().
skiphoppy