How to get list of font names from Java
Asked Answered
F

4

12

In Java we can create a Font object as such:

new Font("Helvetica", Font.PLAIN, 12);

My question is how do we get the entire list of font names from Java, for example "Helvetica" which we can use it as one the argument for the Font constructor?

I tried the following, but I can't find "Helvetica" in all of the lists.

    GraphicsEnvironment ge;  
    ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  

    String[] names = ge.getAvailableFontFamilyNames();
    Font[] allFonts = ge.getAllFonts();

    for(int x=0; x<names.length; x++)
        System.out.println(names[x]);

    for(int x=0; x<allFonts.length; x++){           
        System.out.println(allFonts[x].getName());
        System.out.println(allFonts[x].getFontName());
        System.out.println(allFonts[x].getFamily());
        System.out.println(allFonts[x].getPSName());
    }

Edit: More importantly, I also want to know what is the first attribute call in Font constructornew Font("What attribute is this?", Font.PLAIN, 12)

Q: Is it a fontName, family, fontFace, name or what?

Figone answered 28/2, 2015 at 21:30 Comment(7)
Can you provide output? In my case I see more than 10 strings contains Helivetica word.Nonalcoholic
@maxd I hope I could, as that will be useful to you all, but my output s too long. over 3000 lines of fonts.Figone
Just past it to gist.github.com or pastebin.comNonalcoholic
Could you please specify your OS too?Nonalcoholic
@maxd Window 7 Home Premium SP 1Figone
Here is a answer on your question about first String argument of Font class.Nonalcoholic
@maxd thanks for your effort, I already went to that page several times today already. But still unsure which name is it referring to. Can I assume the name mentioned in the API is same as what I can receive from allFonts[x].getName() ?Figone
W
8

On your system, that font may well be mapped to something else

Font helvetica = new Font("Helvetica", Font.PLAIN, 12);
System.out.println(helvetica.getFontName(Locale.US));

and I get

SansSerif.plain

To output the names of all local fonts, you could use something like

GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();

Font[] allFonts = ge.getAllFonts();

for (Font font : allFonts) {
    System.out.println(font.getFontName(Locale.US));
}
Wyndham answered 28/2, 2015 at 21:35 Comment(4)
@ElliotFrisch Thanks for your help, but I still don't see it in my list of fonts. What is the "helvetica" I am looking for. Is it call fontName, family, fontFace or font or name? Knowing that, I will be able to know which method to use to grab the list of allowable string to fill the Font constructor. Thanks!Figone
It's the font name, and it starts with a capital letter and it may not be available on your system (it isn't on mine).Wyndham
Thanks a lot for your relpy. So I guess I can simply use allFonts[x].getFontName() ? I was confused because the API only mentioned name instead of fontName.Figone
Fonts (and typography) are a somewhat complex topic, and the name Helvetica refers to a typeface. You can call Font.getName() which Returns the logical name of this Font and/or Font.getFontName() which says (as an example) that Helvetica Bold could be returned as a font face name.Wyndham
P
4
new Font("Helvetica", Font.PLAIN, 12);

In this case, it is better to use something like:

new Font(Font.SANS_SERIF, Font.PLAIN, 12);

That will produce the undecorated Font used by default on that OS.

On Windows it would be Arial. On OS X it would be Helvetica. On *nix machines it might be either, or a 3rd undecorated Font.


In answer to your specific question, I've always found the 'font family' string to be useful for creating an instance of the font.

Plafker answered 1/3, 2015 at 1:18 Comment(0)
C
4

this programme will show you list of all font in you system :

import java.awt.GraphicsEnvironment;

public class ListJavaFonts
{

  public static void main(String[] args)
  {
    String fonts[] = 
      GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    for ( int i = 0; i < fonts.length; i++ )
    {
      System.out.println(fonts[i]);
    }
  }

}
Curb answered 18/11, 2017 at 16:38 Comment(0)
S
2

I know this is an old question, but there are some unanswered questions.

More importantly, I also want to know what is the first attribute call in Font constructor new Font("What attribute is this?", Font.PLAIN, 12) Q: Is it a fontName, family, fontFace, name or what?

If you decompile the Java class with an IDE (I'm using IntelliJ), you will see:

public Font(String name, int style, int size) {
    this.name = (name != null) ? name : "Default";
    this.style = (style & ~0x03) == 0 ? style : 0;
    this.size = size;
    this.pointSize = size;
}
public String getName() {
        return name;
    }
public String getFontName() {
  return getFontName(Locale.getDefault());
}

This tells you that the name you use when calling the constructor can be retrieved with getName, but a call to getFontName will return your default text. That is why you can set the name to Helvetica, then call getFontName and it return something other than Helvetica.

Shaver answered 15/6, 2016 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.