Custom fonts in Java

How to fix a problem with custom fonts in Java?

For example, my application uses a font that is not on all computers. Can I somehow include it in a compiled executable, and then call it from there if it does not exist on the client computer?

What are the other alternatives? I could make all fonts symbols as images (before, in some graphics applications) and then display an image for each char ... is this normal?

+7
source share
2 answers

Here is the utility that I use to download the font file from the .ttf file (may be included):

private static final Font SERIF_FONT = new Font("serif", Font.PLAIN, 24); private static Font getFont(String name) { Font font = null; if (name == null) { return SERIF_FONT; } try { // load from a cache map, if exists if (fonts != null && (font = fonts.get(name)) != null) { return font; } String fName = Params.get().getFontPath() + name; File fontFile = new File(fName); font = Font.createFont(Font.TRUETYPE_FONT, fontFile); GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); ge.registerFont(font); fonts.put(name, font); } catch (Exception ex) { log.info(name + " not loaded. Using serif font."); font = SERIF_FONT; } return font; } 
+18
source

You can enable the font with your application and create it on the fly.

 InputStream is = this.getResourceAsStream(font_file_name); Font font = Font.createFont(Font.TRUETYPE_FONT, is); 
+6
source

All Articles