How to Fetch Font Metrics using Java

This quick tutorial will guide you on how to fetch font metrics using Java. A detailed description along with the sample code to get font metrics using Java will be available such that first the environment will be configured and then you will get a step-by-step procedure and a working Java code to load a PFB font and fetch the font metrics. This process to retrieve font metrics can be used in any of the common operating systems like Windows, macOS, or Linux.

Steps to Fetch Font Metrics using Java

  1. Configure the Aspose.Font library from the Maven Repository to fetch the font metrics in the application
  2. Load the sample PFB font file into FileSystemStreamSource class object to retrieve the metrics
  3. Declare FontFileDefinition class object by providing the FileSystemStreamSource object
  4. Initialize the FontDefinition class object and set the FontType and FontFileDefinition object
  5. Use the function to load the font definition and cast it as Type1Font
  6. Fetch and display all the desired font metrics information using the Type1Font object

These steps describe a step-by-step approach to load the PFB font file and then use a sequence of different classes objects that are required to fetch the font definition and then extract font metrics using Java. All the necessary font metrics information is available in the Type1Font class object.

Code to Retrieve Font Metrics using Java

In this sample code, we first load the PFB file and then use Type1Font class for opening the loaded font’s definition. The Type1Font class contains properties like FontName, NumGlyphs, Ascender, Descender, TypoAscender, TypoDescender, and UnitsPerEM. You may retrieve these properties to display on the console or process them as per your application requirement.

In this tutorial, we have gone through the steps to retrieve font metrics using Java. If you want to learn other features like converting Word file to JPG refer to the article on how to convert Word to JPG in Java.