Skip to content

Write AFP TrueType trailer fonts in a stable order - #136

Open
haakonrp wants to merge 1 commit into
apache:mainfrom
haakonrp:afp-trailer-font-order
Open

haakonrp wants to merge 1 commit into
apache:mainfrom
haakonrp:afp-trailer-font-order

Conversation

@haakonrp

@haakonrp haakonrp commented Nov 24, 2026 •

Copy link
Copy Markdown

Since FOP-3337 (commit feb2323), AFPResourceManager collects embedded TrueType fonts in trailerFonts, a HashMap<AFPResourceInfo, AFPTrailerFont>, and writes them in the map's iteration order in writeToStream().

AFPResourceInfo.hashCode() includes AFPResourceLevel.hashCode(), which includes ResourceType.hashCode(). That is an enum, so its hash is an identity hash, and its value depends on what the JVM did before the enum was first hashed. As a result, the order of the TrueType font objects in the resource group, and the RES/OC names they get, can differ between runs for the same input. The pages render the same, but the AFP bytes don't. We hit this in byte-level regression tests after upgrading from 2.11 to 2.12: the same document produced different output depending on which tests had run earlier in the JVM.

This changes trailerFonts to a LinkedHashMap, so fonts are written in the order they were first registered.

Checked by rendering an invoice with several TrueType fonts to AFP on 2.12, with and without this class patched in, in JVMs that ran different sets of work first. Unpatched, the output differed between those JVMs. Patched, it was byte-identical every time.

AFPResourceManager keeps trailer fonts in a HashMap keyed by AFPResourceInfo, whose hashCode includes the ResourceType enum's identity hash. The order the fonts are written in, and the generated resource names, therefore depend on what the JVM did before the enum was first hashed. The same input can produce different AFP bytes in different runs. A LinkedHashMap writes the fonts in the order they were first used.

Co-authored-by: claude-opus-5.5 <claude-opus-5.5@users.noreply.github.com>
@haakonrp

Copy link
Copy Markdown
Author

@simonsteiner1984

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant