You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A library-agnostic font resource management library for Java. Provides easy access to bundled TTF fonts for use with AWT/Swing, iText, PDFBox, Apache FOP, and other font-consuming libraries.
Features
Load fonts from classpath resources
Library-agnostic design - works with any PDF/font library
Using FontResource (Recommended for PDF libraries)
importcom.ds.fontbundle.FontResource;
importcom.ds.fontbundle.BundledFont;
// Load font resourceFontResourceresource = FontResource.of(BundledFont.ARIAL);
// Get raw bytesbyte[] fontBytes = resource.getBytes();
// Get fresh InputStreamInputStreamstream = resource.newInputStream();
Using FontLoader (For AWT/Swing)
importcom.ds.fontbundle.FontLoader;
importcom.ds.fontbundle.BundledFont;
importjava.awt.Font;
// Get AWT font with sizeFontfont = FontLoader.getAwtFont(BundledFont.ARIAL, 12f);
// Get AWT font with style and sizeFontboldFont = FontLoader.getAwtFont(BundledFont.ARIAL_BOLD, Font.BOLD, 14f);
importcom.ds.fontbundle.FontFamily;
// Get all variants of a font familyFontFamilyarial = FontFamily.ARIAL;
BundledFontregular = arial.getRegular();
BundledFontbold = arial.getBold();
BundledFontitalic = arial.getItalic();
BundledFontboldItalic = arial.getBoldItalic();
// Get variant by styleBundledFontfont = arial.getVariant(true, false); // bold, not italic// Get variant by AWT style constantBundledFontfont2 = arial.getVariant(Font.BOLD | Font.ITALIC);
// Find family by fontFontFamilyfamily = FontFamily.of(BundledFont.ARIAL_BOLD).orElse(null);
// Find family by nameFontFamilytimes = FontFamily.byName("Times New Roman").orElse(null);
// Get all variants as listList<BundledFont> allVariants = arial.getAllVariants();
System Font Registration
// Register a single font with AWT GraphicsEnvironmentFontLoader.registerFont(BundledFont.ARIAL);
// Register multiple fontsFontLoader.registerFonts(BundledFont.ARIAL, BundledFont.ARIAL_BOLD);
// Register entire font familyFontLoader.registerFamily(FontFamily.ARIAL);
// Register all bundled fontsFontLoader.registerAllFonts();
// Check if font is available in systembooleanavailable = FontLoader.isFontAvailable("Arial");
// Get all available font family namesString[] families = FontLoader.getAvailableFontFamilyNames();
Font Validation
importcom.ds.fontbundle.FontValidator;
importcom.ds.fontbundle.FontValidator.ValidationResult;
// Validate a single fontValidationResultresult = FontValidator.validate(BundledFont.ARIAL);
if (result.isValid()) {
System.out.println("Font: " + result.getFontName());
System.out.println("Family: " + result.getFontFamily());
System.out.println("Glyphs: " + result.getNumGlyphs());
System.out.println("Size: " + result.getFileSize() + " bytes");
} else {
System.out.println("Error: " + result.getErrorMessage());
}
// Get summary stringSystem.out.println(result.getSummary());
// Quick validation checkbooleanisValid = FontValidator.isValid(BundledFont.ARIAL);
// Validate all fontsList<ValidationResult> results = FontValidator.validateAll();
// Validate font familyList<ValidationResult> familyResults = FontValidator.validateFamily(FontFamily.ARIAL);
// Check if all fonts are validbooleanallValid = FontValidator.areAllValid();
API Reference
FontResource
Method
Description
FontResource.of(BundledFont)
Factory method to create FontResource
getBytes()
Get raw font bytes as byte array
newInputStream()
Create fresh InputStream for font data
getResourcePath()
Get classpath resource path
getFontName()
Get font name (enum constant name)
getSize()
Get byte array length
FontLoader
Method
Description
getAwtFont(BundledFont, float size)
Get AWT Font with PLAIN style
getAwtFont(BundledFont, int style, float size)
Get AWT Font with specified style
getFontResource(BundledFont)
Get cached FontResource
getResourcePath(BundledFont)
Get classpath resource path
getResourceAsStream(BundledFont)
Get InputStream for font file
clearCache()
Clear font caches
registerFont(BundledFont)
Register font with system GraphicsEnvironment
registerFonts(BundledFont...)
Register multiple fonts
registerFamily(FontFamily)
Register all fonts in a family
registerAllFonts()
Register all bundled fonts
isFontAvailable(String)
Check if font is available in system
getAvailableFontFamilyNames()
Get all available system font names
BundledFont
Enum containing all available bundled fonts. Each constant has a getResourcePath() method returning the classpath location of the TTF file.
FontFamily
Method
Description
getRegular()
Get regular variant
getBold()
Get bold variant
getItalic()
Get italic variant
getBoldItalic()
Get bold italic variant
getAllVariants()
Get all variants as list
getVariant(boolean, boolean)
Get variant by bold/italic flags
getVariant(int)
Get variant by AWT style constant
getDisplayName()
Get human-readable family name
contains(BundledFont)
Check if font belongs to family
FontFamily.of(BundledFont)
Find family containing font
FontFamily.byName(String)
Find family by display name
FontFamily.getAllFamilies()
Get all font families
FontMetadata
Method
Description
FontMetadata.of(BundledFont)
Extract metadata from bundled font
FontMetadata.of(FontResource)
Extract metadata from FontResource
FontMetadata.of(byte[])
Extract metadata from raw bytes
getFontName()
Get font name
getFamily()
Get font family
getPsName()
Get PostScript name
isBold()
Check if bold
isItalic()
Check if italic
isPlain()
Check if plain (not bold/italic)
isBoldItalic()
Check if bold and italic
getStyleName()
Get style as string ("Regular", "Bold", etc.)
getNumGlyphs()
Get number of glyphs
FontValidator
Method
Description
validate(BundledFont)
Validate bundled font, returns ValidationResult
validate(byte[])
Validate font from bytes
validateAll()
Validate all bundled fonts
validateFamily(FontFamily)
Validate all fonts in family
isValid(BundledFont)
Quick validity check
areAllValid()
Check if all bundled fonts are valid
ValidationResult
Method
Description
isValid()
True if font is valid
isResourceFound()
True if resource file exists
isParseable()
True if font file is parseable
getFontName()
Get parsed font name
getFontFamily()
Get parsed font family
getNumGlyphs()
Get glyph count
getFileSize()
Get file size in bytes
getErrorMessage()
Get error message if invalid
getSummary()
Get human-readable summary
Adding New Fonts
Place TTF file in src/main/resources/fonts/[FontFamily]/