2
votes

We use

String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

to get a list of available fonts. I want a method which is able to return styles for a particular font. For Eg. Segoe UI is available in following styles : Black, Black Italic, Bold, Bold Italic, Light, Regular and so on.

Or is there any alternative to achieve same output ?

1

1 Answers

2
votes

I think you could try something like this:

public static void main(String[] a)
{
    Set<String> styles = getAvailableStyles("Arial");
    System.out.println(styles);
}

public static Set<String> getAvailableStyles(String name)
{
    Set<String> styles = new HashSet<String>();
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = e.getAllFonts();
    for (Font f : fonts)
    {
        if (f.getFamily().equals(name))
            styles.add(f.getName());
    }
    return styles;
}

Output:

[Arial-BoldMT, Arial-BoldItalicMT, Arial-ItalicMT, ArialMT]

The problem is that you don't know which one is bold or italic etc, because all the fonts have style "plain". Only using the name works.