Load and save CFF fonts in Java

Process PostScript-based Compact Font Format (.cff) files programmatically with Aspose.Font for Java. Open CFF font binaries from local file paths or in-memory byte arrays, inspect font structures using the CffFont class, and save modified font data back to disk or streams without external dependencies.

 

The Compact Font Format (CFF) is a compact representation of PostScript Type 1 font outlines, widely used within PDF documents, OpenType CFF (.otf) fonts, and digital publishing pipelines. With Aspose.Font for Java, developers can:

  • In-memory font processing: Load CFF font bytes directly from database BLOBs, PDF streams, or cloud storage without writing temporary files to disk.
  • Compact storage management: Manipulate low-footprint PostScript outline font structures for optimized document embedding.
  • Flexible File Operations: Read CFF font files using FileSystemStreamSource or ByteContentStreamSource and export them via CffFont.save().

CFF font processing workflow

Load CFF from disk - uses (File path (.cff)) and FontDefinition + FileSystemStreamSource

Load CFF from memory - uses Binary byte array (byte[]) and FontDefinition + ByteContentStreamSource

Inspect/edit CFF data - uses CffFont object and CffFont.getFontName(), CffFont.getNumGlyphs(), etc.

Save CFF to disk - uses CffFont object and CffFont.getFontName(), CffFont.getNumGlyphs(), etc.

How to load a CFF from a disk font in Java?

Load a .cff font file by defining a FontDefinition with FontType.CFF and a FileSystemStreamSource.

import com.aspose.font.*;

public class LoadCffFontFromDisk {
    public static void main(String[] args) {
        // Step 1: Specify path to the .cff font file
        String fileName = "path/to/OpenSans-Regular.cff";

        // Step 2: Initialize FontDefinition with CFF format and FileSystemStreamSource
        FontDefinition fd = new FontDefinition(
            FontType.CFF, 
            new FontFileDefinition("cff", new FileSystemStreamSource(fileName))
        );

        // Step 3: Open the CFF font
        CffFont font = (CffFont) Font.open(fd);

        // Step 4: Verify font properties
        System.out.println("Font Name: " + font.getFontName());
        System.out.println("Glyph Count: " + font.getNumGlyphs());
        System.out.println("CFF Font successfully loaded from disk.");
    }
}

How to load a CFF font from a byte array (`byte[]`)?

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.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class LoadCffFontFromByteArray {
    public static void main(String[] args) throws Exception {
        String fileName = "path/to/OpenSans-Regular.cff";

        // Step 1: Read font file bytes into memory
        byte[] fontMemoryData = Files.readAllBytes(Paths.get(fileName));

        // Step 2: Create FontDefinition using ByteContentStreamSource
        FontDefinition fd = new FontDefinition(
            FontType.CFF, 
            new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData))
        );

        // Step 3: Open font instance from byte array
        CffFont font = (CffFont) Font.open(fd);

        System.out.println("Loaded CFF Font Name from memory: " + font.getFontName());
    }
}

How to modify and save CFF fonts to disk?

After loading and interacting with a CffFont instance, write the font file out to a new location using the save() method.

import com.aspose.font.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SaveCffFontToDisk {
    public static void main(String[] args) throws Exception {
        String inputPath = "path/to/OpenSans-Regular.cff";
        String outputPath = "path/to/OpenSans-Regular_out.cff";

        // Step 1: Load font from byte array
        byte[] fontMemoryData = Files.readAllBytes(Paths.get(inputPath));
        FontDefinition fd = new FontDefinition(
            FontType.CFF, 
            new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData))
        );
        CffFont font = (CffFont) Font.open(fd);

        System.out.println("Loaded CFF Font: " + font.getFontName());

        // Step 2: Work with font data (inspect metrics, glyphs, or update properties)

        // Step 3: Save CFF font back to disk
        font.save(outputPath);

        System.out.println("CFF 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. What is the difference between OpenType CFF (.otf) and standalone CFF (.cff)?

A standalone .cff file contains raw PostScript font program data without OpenType table wrappers. An .otf file with CFF outlines wraps the .cff stream inside standard OpenType table structures (CFF, cmap, head, etc.). Aspose.Font for Java supports handling both standalone CFF and OpenType CFF formats.

2. Can I convert CFF fonts to other font formats like TTF or WOFF in Java?

Yes. Once loaded into a CffFont instance, Aspose.Font for Java provides rendering and encoding facilities that allow converting font data into other target formats like TTF, WOFF, or WOFF2.

3. Can I save a modified CffFont directly to an OutputStream?

Yes. In addition to passing a file path string to font.save(fileName), you can pass a java.io.OutputStream to write binary font data directly to network or memory streams.