Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

embedding a font in Java

Tags:

java

fonts

I have a custom font I want to show off in a Java program where the user can view it without having to install it. Does anyone know how to do that?

working solution


I have implemented the following:

font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(Clazz.class.getResource("/resources/segoescb.ttf").toURI()));
font = font.deriveFont(11.0F);
like image 628
Ky. Avatar asked Dec 20 '10 08:12

Ky.


People also ask

How do you embed the font?

Embed fonts in a document or presentation Open the file you want to embed fonts in. On the application (PowerPoint or Word) menu, select Preferences. In the dialog box, under Output and Sharing, select Save. Under Font Embedding, select Embed fonts in the file.

How do I add fonts to swing?

To set the font for a Swing component, use its setFont() method of the component. JButton closeButton = new JButton("Close"); closeButton. setFont(f4);

What fonts does Java support?

Logical fonts are the five font families defined by the Java platform which must be supported by any Java runtime environment: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries.


1 Answers

I have never done it, but it seems like the methods you want are

Importing the Font

Font createFont(int fontFormat, InputStream fontStream)

or alternatively

Font createFont(int fontFormat, File fontFile)

The int parameter is either Font.TRUETYPE_FONT or Font.TYPE1_FONT, while the InputStream or File parameter contains the font's binary data.

Using the Font after import:

To make the Font available to Font constructors the returned Font must be registered in the GraphicsEnviroment by calling registerFont(Font).

like image 157
Sean Patrick Floyd Avatar answered Oct 04 '22 03:10

Sean Patrick Floyd