JDK-8284730 : Be able to get font path
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2022-04-06
  • Updated: 2022-04-12
  • Resolved: 2022-04-12
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
It would be convenient if we could be able to the font path for java.awt.Font class.

Example:
// Get the font
Font font = new Font("Arial Black", Font.PLAIN, 1);
String fontPath = font.getFontPath(); --> This method would Return "C:\Windows\Fonts\ariblk.ttf"

In my case, I would like to copy an installed font and paste in an another directory.

From what I know, the currently only method to do this is to use this old librairies (it is deprecated):
sun.font.PhysicalFont;

Here is an example of code to get the font path:
    public String findFont(Font font) {

        /*Font2D f2d = FontManagerFactory.getInstance().findFont2D(f.getFontName(), f.getStyle(),
                FontManager.LOGICAL_FALLBACK).handle.font2D;*/

        Font2D f2d = FontUtilities.getFont2D(font);
        String fontPath = "";
        try {
            Field platName = PhysicalFont.class.getDeclaredField("platName");
            platName.setAccessible(true);
            fontPath = (String) platName.get(f2d);
            platName.setAccessible(false);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            System.out.println(e.getLocalizedMessage());
        }

        return fontPath;
    }

The problem of this librairie is that it only take look in "C:\Windows\Fonts" folder, so it does not look in "%userprofile%\AppData\Local\Microsoft\Windows\Fonts" folder which is a pretty big problem.



Comments
The suggestion to look at "Suggestion here- https://answers.microsoft.com/en-us/windows/forum/all/copying-and-pasting-font-files/a84563ee-85f5-4370-ae6c-4d4dadc6884d" is missing the point of the question. That's about installing fonts. This request is to find the location of an already installed font given a java.awt.Font instance representing it. There's an open RFE on this. Also it is not correct to say PhysicalFont is deprecated. PhysicalFont is JDK internal private API - an application which is even aware of its existence is being very naughty. If it tries to use it then it is being very wrong. Also "per-user" fonts have been supported since JDK 13 : https://bugs.openjdk.java.net/browse/JDK-8218914 [OK there is https://bugs.openjdk.java.net/browse/JDK-8255281 where someone suggests that fix didn't work but I've not verified that and it is still irrelevant to this request] But that does not mean that there's a supported way to find the path of those fonts.
12-04-2022

Suggestion here- https://answers.microsoft.com/en-us/windows/forum/all/copying-and-pasting-font-files/a84563ee-85f5-4370-ae6c-4d4dadc6884d
12-04-2022