Gets all available fonts in the system - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

Gets all available fonts in the system

Demo Code

/*//from  w  w w .  j  a va2s  .  c om
 *  Copyright (C) 2010-2015 JPEXS, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */
//package com.java2s;
import java.awt.Font;

import java.awt.GraphicsEnvironment;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class Main {
    /**
     * Gets all available fonts in the system
     *
     * @return Map<FamilyName,Map<FontNAme,Font>>
     */
    public static Map<String, Map<String, Font>> getInstalledFonts() {
        Map<String, Map<String, Font>> ret = new HashMap<>();
        Font fonts[] = null;

        try {

            Object fm = getFontManager();
            Class<?> clFm = Class.forName("sun.font.SunFontManager");

            //Delete cached installed names
            Field inField = clFm.getDeclaredField("installedNames");
            inField.setAccessible(true);
            inField.set(null, null);
            inField.setAccessible(false);

            //Delete cached family names
            Field allFamField = clFm.getDeclaredField("allFamilies");
            allFamField.setAccessible(true);
            allFamField.set(fm, null);
            allFamField.setAccessible(false);

            //Delete cached fonts
            Field allFonField = clFm.getDeclaredField("allFonts");
            allFonField.setAccessible(true);
            allFonField.set(fm, null);
            allFonField.setAccessible(false);

            fonts = (Font[]) clFm.getDeclaredMethod("getAllInstalledFonts")
                    .invoke(fm);
        } catch (Throwable ex) {
            // ignore
        }

        if (fonts == null) {
            fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getAllFonts();
        }

        List<String> javaFonts = Arrays.asList("Dialog", "DialogInput",
                "Monospaced", "Serif", "SansSerif");
        for (Font f : fonts) {
            String fam = f.getFamily(Locale.ENGLISH);
            //Do not want Java logical fonts
            if (javaFonts.contains(fam)) {
                continue;
            }
            if (!ret.containsKey(fam)) {
                ret.put(fam, new HashMap<String, Font>());
            }

            ret.get(fam).put(f.getFontName(Locale.ENGLISH), f);
        }

        return ret;
    }

    private static Object getFontManager() throws ClassNotFoundException,
            NoSuchMethodException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        Class<?> clFmFactory = Class.forName("sun.font.FontManagerFactory");
        return clFmFactory.getDeclaredMethod("getInstance").invoke(null);
    }
}

Related Tutorials