-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
8
-
x86_64
-
windows_10
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.
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.
- duplicates
-
JDK-4752644 RFE: Getting a java.awt.Font's FilePath
- Open