Skip to content

Fonts and encoding

Oliver Tigges edited this page Sep 13, 2026 · 1 revision

Fonts and encoding

A Font combines a PDFBox font with a size, color, and metrics. A FontFace represents a font loaded into one document and derives Font objects at different sizes and colors.

Standard fonts

The convenience paragraph uses Helvetica at 8 points. Use SimpleFont.helvetica(size) or SimpleFont.helvetica_bold(size) to select a size explicitly. These helpers currently live in inspired.pdf.unbox.internal.

The standard Helvetica fonts use WinAnsiEncoding, which cannot represent every character. By default, unsupported characters are replaced with ?. This is also the default for fonts derived from a loaded face when a glyph is missing.

Load your own font and enable strict encoding

Save as FontExample.java. Pass the path to a TrueType font as the program's first argument. The chosen font must contain the sample characters φ, ≥, and Ā.

import inspired.pdf.unbox.Document;
import inspired.pdf.unbox.Font;
import inspired.pdf.unbox.FontFace;

import java.awt.Color;
import java.nio.file.Path;

import static inspired.pdf.unbox.Unbox.paragraph;

/** Loads a caller-supplied font and rejects text with missing glyphs. */
public class FontExample {
    public static void main(String[] args) {
        if (args.length != 1) {
            throw new IllegalArgumentException("Pass the path to a TrueType font file");
        }
        try (Document document = new Document()) {
            FontFace face = document.loadFont(Path.of(args[0])).strict();
            Font body = face.at(11);
            Font title = face.at(18, new Color(30, 70, 110));
            document.render(paragraph("Font example", title));
            document.render(paragraph("Outside WinAnsi: φ ≥ Ā", body));
            document.finishTo(Path.of("fonts.pdf"));
        }
    }
}

loadFont(Path) embeds the font into that document. loadFont(InputStream) is also available for font data from an application resource. Unbox does not bundle a font family; applications supply their own font files.

face.strict() returns a new face sharing the loaded font. Both at(size) and at(size, color) then reject unsupported characters with a PdfUnboxException identifying the code point and font. Measurement and drawing use the same policy. The original face and fonts previously derived from it remain unchanged.

For a standard font, the corresponding strict configuration is SimpleFont.helvetica(11).withReplacement(null). A non-null replacement, such as '#', must itself be encodable by the selected font.

Reuse and font choice

  • Load a face once for a document, then derive sizes and colors from it.
  • Load separate regular and bold files if you need both weights. Increasing size does not select a bold font.
  • A loaded face and its fonts belong to that document. Load new faces for another document; do not cache embedded faces globally.
  • A TrueType file still needs the glyphs for your text. Loading it does not add automatic font fallback or guarantee every language, script, or emoji will render.
  • A different font can change text width, wrapping, and page breaks. Review the report layout when changing fonts.

Continue with Text layout.

Clone this wiki locally