Manipulate CFF, TrueType, and Type1 Fonts using Java

manipulate fonts in java

Fonts play an important role in the presentation of the text within the documents and web pages. Various font families are available that allow you to use fancy characters in order to make your text appealing. Since Aspose deals with file format automation, we have launched a dedicated font manipulation API to work with font file formats. In this article, you will get familiar with our Java Font API and learn how to work with CFF, TrueType, OpenType, and Type1 fonts from within your Java applications.

Java Font Manipulation API

Aspose.Font for Java is the font manipulation API that allows you to load, save, and manipulate popular fonts including CFF, TrueType, Type1, EOT, and OpenType. In addition, the API supports retrieving font metrics as well as rendering the text using the supported font types. You can download the API or install it within your Maven-based applications using the following configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-font</artifactId>
    <version>20.10</version>
</dependency>

Load CFF, TrueType, and Type1 Fonts using Java

You can easily load the font from a file located on your storage from within your Java program. Once you have loaded the font, you can perform further operations as required. The following are the steps to load a font using Aspose.Font for Java.

  • Use the FontDefinition class to load the font by specifying its type such as TrueType, Type1, etc.
  • Use CffFontTtfFont, or Type1Font class to access the CFF, TrueType, and Type1 fonts respectively from FontDefinition object.

Load CFF Fonts using Java

This is how you can load a CFF font from file using Java.

Load TrueType Fonts using Java

The following Java code sample loads a TrueType font.

Load Type1 Fonts using Java

The following code sample shows how to load a Type1 font using Java.

Extract Font Metrics from TrueType or Type1 Fonts using Java

The font files also contain the font’s information that is used to specify the line spacing, placement of subscript and superscript, alignment, and etc. Aspose.Font for Java also allows you to extract the font metric information including AscenderDescenderTypoAscenderTypoDescender, and UnitsPerEm. The following are the steps to perform this operation.

  • Use FontDefinition class to load TrueType or Type1 font.
  • Access the font using TtfFont or Type1Font class according to the font’s type.
  • Extract the font’s metrics information.

Get Font Metrics from TrueType Font using Java

Extract Font Metrics from Type1 Font using Java

Conclusion

In this article, you have learned how to work with CFF, TrueType, and Type1 fonts programmatically using Java. Furthermore, you have seen how to access the font metrics information of a specific font. You can explore more about the Java font manipulation API using documentation and source code samples.