Load and save TTF and OTF fonts in Java

Effortlessly manage, inspect, modify, and save TrueType (.ttf) and OpenType (.otf) fonts in your Java applications. Aspose.Font for Java offers a robust, high-performance API for loading font files from local storage or byte streams, extracting metadata, and saving updated font structures—all without native OS font engine dependencies.

 

TrueType and OpenType are the standard font formats across modern operating systems, web applications, and digital publishing workflows. Programmatic font processing enables key enterprise capabilities:

  • Dynamic font file manipulation: - Read TTF/OTF fonts directly from disk, database streams, or memory buffers.
  • Font inspection and metadata extraction: - Access font header data, character maps, glyph definitions, and licensing information.
  • Cross-platform compatibility: - Run font processing workloads seamlessly across Linux, Windows, macOS, and cloud deployment environments.
  • Zero native dependencies: - Eliminate reliance on OS-specific font installation scripts or GUI graphics drivers (like GDI or X11).

Core rendering capabilities

Stream and disk loading - Open TTF and OTF files using FileSystemStreamSource or ByteContentStreamSource.

Metadata and metrics access - Inspect glyph metrics, advance widths, bounding boxes, and font naming tables.

Font data export and saving - Modify font parameters and serialize updated font objects directly back to disk or streams.

Format conversion options - PConvert TrueType/OpenType fonts into modern web formats such as WOFF, WOFF2, or SVG.

How to load a TrueType font in Java

Loading a TrueType or OpenType font requires instantiating a FontDefinition with the appropriate FontType and opening the font via the Font factory class.

import com.aspose.font.FileSystemStreamSource;
import com.aspose.font.Font;
import com.aspose.font.FontDefinition;
import com.aspose.font.FontFileDefinition;
import com.aspose.font.FontType;
import com.aspose.font.TtfFont;

public class LoadTrueTypeFont {
    public static void main(String[] args) {
        // Define path to the TrueType font file (.ttf / .otf)
        String fontFilePath = "path/to/Montserrat-Regular.ttf";

        // Initialize FontDefinition for TTF font type
        FontDefinition fontDef = new FontDefinition(
            FontType.TTF, 
            new FontFileDefinition("ttf", new FileSystemStreamSource(fontFilePath))
        );

        // Open and load the TtfFont object
        TtfFont font = (TtfFont) Font.open(fontDef);

        // Access basic font properties
        System.out.println("Font Name: " + font.getFontName());
        System.out.println("Glyph Count: " + font.getNumGlyphs());
    }
}

How to save a TrueType font in Java

After loading a font into memory and performing data extraction or modifications, you can save the font back to local disk storage or write it to an output byte stream.

import com.aspose.font.ByteContentStreamSource;
import com.aspose.font.Font;
import com.aspose.font.FontDefinition;
import com.aspose.font.FontFileDefinition;
import com.aspose.font.FontType;
import com.aspose.font.TtfFont;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SaveTrueTypeFont {
    public static void main(String[] args) throws IOException {
        // Read font file into byte array stream
        String inputPath = "path/to/source.ttf";
        byte[] fontBytes = Files.readAllBytes(Paths.get(inputPath));

        // Define font source using ByteContentStreamSource
        FontDefinition fontDef = new FontDefinition(
            FontType.TTF, 
            new FontFileDefinition("ttf", new ByteContentStreamSource(fontBytes))
        );

        // Open TTF font object
        TtfFont font = (TtfFont) Font.open(fontDef);

        // Perform font processing or inspection here...

        // Save the updated TtfFont object back to disk
        String outputPath = "path/to/output_saved.ttf";
        font.save(outputPath);

        System.out.println("TrueType font successfully saved to: " + outputPath);
    }
}

Installation and Setup

Include Aspose.Font for Java in your project via Maven.

Package Manager Console Command

<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>Latest</version>
</dependency>

FAQ

1. Can I load OpenType fonts (.otf) using the same methods as TrueType fonts?

Yes. Aspose.Font for Java supports both TrueType (.ttf) and OpenType (.otf) font formats through the TtfFont and FontDefinition architecture.

2. Does loading and saving fonts require installing fonts on the host operating system?

No. Aspose.Font for Java reads font structures directly as file streams or byte buffers. It operates independently without requiring the font to be installed in system directories.

3. Can I convert TTF or OTF fonts into web-ready formats like WOFF or SVG?

Yes. Aspose.Font for Java provides built-in conversion features to transform loaded TrueType and OpenType fonts into WOFF, WOFF2, SVG, and CFF formats.