I recently found out how to register a TTF font with the local GraphicsEnvironment, s.t., for my use case (SVG-to-PNG transcoding), Apache Batik may recognize the font:
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
// [...]
GraphicsEnvironment lge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
lge.registerFont(font);
} catch (FontFormatException e) {
logger.warn(e.getMessage(), e);
} catch (IOException e) {
logger.warn(e.getMessage(), e);
}
However, I was wondering if I could unregister any pre-existing fonts in order to guarantee that only the fonts I register will be used in transcoding.
There is no GraphicsEnvironment#unregisterFont(...), how could I achieve this instead?
PS: I don't want to subclass GraphicsEnvironment, as I cannot assume the presence any specific subclass, like sun.awt.Win32GraphicsEnvironment.
EDIT: Some more infos:
- As sun.font.FontManager changes with Java7 (from class to interface, and whatnot), I'd rather not use any workaround relying on it.
- My JVM is the Oracle JVM.
This cannot be done without reflection of private static variables and such... are you sure you need to do this?
Check out the source code to
sun.font.FontManager.registerFont
, it may already have the safety you desire. (This is the method that does the actual work when you callGraphicsEnvironment.registerFont
)