Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if font exists [duplicate]

Tags:

java

fonts

awt

I'm working with java.awt.Font , How can I discover if the font defined is installed on the system?

Specifically, I have

Font font = new Font("FooBar", 0, 14);

I want to discover if "font" is a valid font on the system and default to something more universal if it isn't available.

like image 439
Cogman Avatar asked Nov 08 '13 21:11

Cogman


1 Answers

Try checking your Font in a loop like this:

public class SO2 {
  public static void main(String args[]) {
      GraphicsEnvironment g= null;
        g=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String []fonts=g.getAvailableFontFamilyNames();
            for (int i = 0; i < fonts.length; i++) {
            System.out.println(fonts[i]);
            if(fonts[i].equals("YOUR FONT")){
                System.out.println("Found!");
            }
    }

}}

It should output "Found!" if it locates your font.

Good Luck!

like image 91
Levenal Avatar answered Nov 05 '22 05:11

Levenal