public class FontCache extends Object
Every call to Font.createFont(int, InputStream) and its siblings registers a font
with the JDK font manager that is never released again, see
JDK-8239833. Creating the same font
over and over - which is what happens when documents using @font-face rules or
useFont are rendered repeatedly - therefore leaks memory (and temporary files) until
the JVM exits. Caching the created fonts avoids that.
The cache is shared by all renderers and documents. Unlike for example PDFBox fonts, AWT fonts are immutable and safe to use from more than one document.
| Modifier and Type | Method and Description |
|---|---|
static String |
contentCacheKey(byte[] content)
A cache key derived from the content itself, for fonts that are only available as bytes and
therefore have no stable name to be keyed on.
|
static Font |
get(FSSupplier<Font> fontSupplier)
Like
get(String, long, FSSupplier) with the key and last modified stamp taken from
the supplier itself. |
static Font |
get(String cacheKey,
long lastModified,
FSSupplier<Font> fontSupplier)
Returns the cached font for the given key, creating it with the supplier if it is not
cached yet or if the resource it was created from changed.
|
static Font |
getTrueTypeFont(byte[] fontBytes)
Returns the true type font in the given bytes, keyed on a hash of the bytes themselves as
there is no name to key them on.
|
static Font |
getTrueTypeFont(File fontFile)
Returns the true type font in the given file, keyed on its path and reloaded when the file
changes.
|
static Font |
getTrueTypeFont(FSSupplier<InputStream> streamSupplier)
Returns the true type font supplied by the given supplier, keyed on
FSSupplier.cacheKey(). |
static Font |
getTrueTypeFont(String cacheKey,
long lastModified,
FSSupplier<InputStream> streamSupplier)
Like
getTrueTypeFont(FSSupplier) but with an explicit key, so that callers can opt
out of caching by passing null. |
static void |
invalidateAll()
Removes every font from the cache, for example because font resources changed in a way this
cache can not see, such as a web font served under an unchanged URL.
|
public static Font get(String cacheKey, long lastModified, FSSupplier<Font> fontSupplier)
cacheKey - identifies the font resource, or null if the caller can not identify it,
in which case the font is created without being cached.lastModified - a value that changes whenever the font resource changes, or
FSSupplier.UNKNOWN_LAST_MODIFIED if that can not be determined, in
which case the font is cached until invalidateAll() is called.public static Font get(FSSupplier<Font> fontSupplier)
get(String, long, FSSupplier) with the key and last modified stamp taken from
the supplier itself.public static Font getTrueTypeFont(File fontFile) throws IOException, FontFormatException
IOExceptionFontFormatExceptionpublic static Font getTrueTypeFont(byte[] fontBytes) throws IOException, FontFormatException
IOExceptionFontFormatExceptionpublic static Font getTrueTypeFont(FSSupplier<InputStream> streamSupplier) throws IOException, FontFormatException
FSSupplier.cacheKey().IOExceptionFontFormatExceptionpublic static Font getTrueTypeFont(String cacheKey, long lastModified, FSSupplier<InputStream> streamSupplier) throws IOException, FontFormatException
getTrueTypeFont(FSSupplier) but with an explicit key, so that callers can opt
out of caching by passing null.IOExceptionFontFormatExceptionpublic static String contentCacheKey(byte[] content)
public static void invalidateAll()
Note that fonts already handed to the JDK font manager can not be released, so recreating the same fonts after clearing the cache will increase memory usage.
Copyright © 2004–2026. All rights reserved.