6

Using Oracle(Sun) JDK6 and trying to move to Oracle JDK7

I am using sun.awt.GraphicsEnvironment to find all system fonts in order to use them to change pdf font used in my pdf file. Here is the exact code I am using:

GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();

// get all system fonts
final Font[] fonts = gEnv.getAllFonts();

After that I will need to get the exact font file path on the system, so I use:

FontManager.getFontPath(true) + "/" +
               FontManager.getFileNameForFontName(font_name);

The problem now is that sun.font.FontManager is no longer a class and has been converted to an interface. I searched online and came up with some solutions that I am not satisfied with and I am looking for other ideas to help solve my problem.

The solutions that I found:

  1. Deploy my project on Java 6 instead of Java 7 (Not recommend as I use some new features in Java 7).
  2. I found the code of the FontManager class online, but using it will require including a lot of other classes/interfaces and the process seems dummy and time consuming. Also I am not if I am allowed to use that code as it's proprietary of Sun company.

What I need is: *A way to find the exact font file path on the system*. All ideas are welcomed.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Hassan Radi
  • 936
  • 10
  • 22
  • Marcel Stör's answer about using Win32FontManager helped to solve the problem in Windows only. I suspect it wouldn't work on Linux or Mac OS. – Hassan Radi Jul 02 '14 at 07:41

2 Answers2

3

You're not saying which JDK you use (Oracle, OpenJDK, ..). Possibly you have

FontManagerFactory.getInstance()

available. Or the inteface implementation Win32FontManager (if you're on Windows)?

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • The real question is 'how to get the A way to find the exact font file path on the system' – Jayan Jan 18 '13 at 12:29
  • 1
    @Jayan, yes I know that. Was requested for Java >10 years ago: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4752644 – Marcel Stör Jan 18 '13 at 13:15
  • @Jayan, I got the bounty, thanks. What was the solution in your particular case? – Marcel Stör Jan 26 '13 at 18:53
  • 1
    I used reflection and accessed the variable that holds the file path. I will put that code here later. – Jayan Jan 27 '13 at 01:15
  • @Jayan: Can you please put the code here? I have the same problem now – gexicide Aug 11 '13 at 11:58
  • using Win32FontManager helped to solve the problem, but it introduced another issue as it's platform dependent as I guess. Do you have any idea about a platform independent solution. Thanks:) – Hassan Radi Jul 02 '14 at 07:32
0

This code works on Windows 10 and IBM Java 8

sun.font.SunFontManager.getInstance().getPlatformFontPath(true)

But on macOS 11 and AdoptOpenJDK 8/11 an empty string is returned.

Igor Kudryashov
  • 353
  • 2
  • 10