How to find the path to a font file in Java 7 and 8

In Java 7 and 8, when changed FontManager, now I have to find the path to the font file, for example c:/windows/fonts/arial.ttf. Now, how can I do this with Java 7 and 8?

Edit

I have a font object or font name, now I would like to know the path to the font file. For example, I have a font name or an object Font, for example Extra CVS bold, now I would like to get a way to get c:/windows/fonts/x32cvs_b.ttfwhere required x32cvs_b.ttf.

+4
source share
1 answer
Object font2D;
try {
    // Java 7+.
    font2D = Class.forName("sun.font.FontUtilities").getDeclaredMethod("getFont2D", new Class[] {Font.class})
        .invoke(null, new Object[] {font});
} catch (Throwable ignored) {
    font2D = Class.forName("sun.font.FontManager").getDeclaredMethod("getFont2D", new Class[] {Font.class})
        .invoke(null, new Object[] {font});
}
Field platNameField = Class.forName("sun.font.PhysicalFont").getDeclaredField("platName");
platNameField.setAccessible(true);
String file = (String)platNameField.get(font2D);
0
source

Source: https://habr.com/ru/post/1541902/


All Articles