Package org.apache.pdfbox.pdmodel.font
Class FontMapperImpl
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.font.FontMapperImpl
-
- All Implemented Interfaces:
FontMapper
final class FontMapperImpl extends java.lang.Object implements FontMapper
Font mapper, locates non-embedded fonts via a pluggable FontProvider.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFontMapperImpl.DefaultFontProviderprivate static classFontMapperImpl.FontMatchA potential match for a font substitution.
-
Field Summary
Fields Modifier and Type Field Description private static FontCachefontCacheprivate java.util.Map<java.lang.String,FontInfo>fontInfoByNameprivate FontProviderfontProviderprivate TrueTypeFontlastResortFontprivate static org.apache.commons.logging.LogLOGprivate java.util.Map<java.lang.String,java.util.List<java.lang.String>>substitutesMap of PostScript name substitutes, in priority order.
-
Constructor Summary
Constructors Constructor Description FontMapperImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSubstitute(java.lang.String match, java.lang.String replace)Adds a top-priority substitute for the given font.private voidaddSubstitutes(java.lang.String match, java.util.List<java.lang.String> replacements)private java.util.Map<java.lang.String,FontInfo>createFontInfoByName(java.util.List<? extends FontInfo> fontInfoList)private FontBoxFontfindFont(FontFormat format, java.lang.String postScriptName)Finds a font with the given PostScript name, or a suitable substitute, or null.private FontBoxFontfindFontBoxFont(java.lang.String postScriptName)Finds a font with the given PostScript name, or a suitable substitute, or null.CIDFontMappinggetCIDFont(java.lang.String baseFont, PDFontDescriptor fontDescriptor, PDCIDSystemInfo cidSystemInfo)Finds a CFF CID-Keyed font with the given PostScript name, or a suitable substitute, or null.private java.lang.StringgetFallbackFontName(PDFontDescriptor fontDescriptor)Attempts to find a good fallback based on the font descriptor.private FontInfogetFont(FontFormat format, java.lang.String postScriptName)Finds the named font with the given format.FontMapping<FontBoxFont>getFontBoxFont(java.lang.String baseFont, PDFontDescriptor fontDescriptor)Finds a font with the given PostScript name, or a suitable substitute, or null.FontCachegetFontCache()Returns the font cache associated with this FontMapper.private java.util.PriorityQueue<FontMapperImpl.FontMatch>getFontMatches(PDFontDescriptor fontDescriptor, PDCIDSystemInfo cidSystemInfo)Returns a list of matching fonts, scored by suitability.private java.util.Set<java.lang.String>getPostScriptNames(java.lang.String postScriptName)Gets alternative names, as seen in some PDFs, e.g.FontProvidergetProvider()Returns the font service provider.private java.util.List<java.lang.String>getSubstitutes(java.lang.String postScriptName)Returns the substitutes for a given font.FontMapping<TrueTypeFont>getTrueTypeFont(java.lang.String baseFont, PDFontDescriptor fontDescriptor)Finds a TrueType font with the given PostScript name, or a suitable substitute, or null.private booleanisCharSetMatch(PDCIDSystemInfo cidSystemInfo, FontInfo info)Returns true if the character set described by CIDSystemInfo is present in the given font.private FontMapperImpl.FontMatchprintMatches(java.util.PriorityQueue<FontMapperImpl.FontMatch> queue)For debugging.private booleanprobablyBarcodeFont(PDFontDescriptor fontDescriptor)voidsetProvider(FontProvider fontProvider)Sets the font service provider.
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
fontCache
private static final FontCache fontCache
-
fontProvider
private FontProvider fontProvider
-
fontInfoByName
private java.util.Map<java.lang.String,FontInfo> fontInfoByName
-
lastResortFont
private final TrueTypeFont lastResortFont
-
substitutes
private final java.util.Map<java.lang.String,java.util.List<java.lang.String>> substitutes
Map of PostScript name substitutes, in priority order.
-
-
Method Detail
-
setProvider
public void setProvider(FontProvider fontProvider)
Sets the font service provider.
-
getProvider
public FontProvider getProvider()
Returns the font service provider. Defaults to using FileSystemFontProvider.
-
getFontCache
public FontCache getFontCache()
Returns the font cache associated with this FontMapper. This method is needed by FontProvider subclasses.
-
createFontInfoByName
private java.util.Map<java.lang.String,FontInfo> createFontInfoByName(java.util.List<? extends FontInfo> fontInfoList)
-
getPostScriptNames
private java.util.Set<java.lang.String> getPostScriptNames(java.lang.String postScriptName)
Gets alternative names, as seen in some PDFs, e.g. PDFBOX-142.
-
addSubstitute
public void addSubstitute(java.lang.String match, java.lang.String replace)Adds a top-priority substitute for the given font.- Parameters:
match- PostScript name of the font to matchreplace- PostScript name of the font to use as a replacement
-
addSubstitutes
private void addSubstitutes(java.lang.String match, java.util.List<java.lang.String> replacements)
-
getSubstitutes
private java.util.List<java.lang.String> getSubstitutes(java.lang.String postScriptName)
Returns the substitutes for a given font.
-
getFallbackFontName
private java.lang.String getFallbackFontName(PDFontDescriptor fontDescriptor)
Attempts to find a good fallback based on the font descriptor.
-
getTrueTypeFont
public FontMapping<TrueTypeFont> getTrueTypeFont(java.lang.String baseFont, PDFontDescriptor fontDescriptor)
Finds a TrueType font with the given PostScript name, or a suitable substitute, or null.- Specified by:
getTrueTypeFontin interfaceFontMapper- Parameters:
fontDescriptor- FontDescriptorbaseFont- PostScript name of the mapped font- Returns:
- the mapped TrueType font or null
-
getFontBoxFont
public FontMapping<FontBoxFont> getFontBoxFont(java.lang.String baseFont, PDFontDescriptor fontDescriptor)
Finds a font with the given PostScript name, or a suitable substitute, or null. This allows any font to be substituted with a PFB, TTF or OTF.- Specified by:
getFontBoxFontin interfaceFontMapper- Parameters:
fontDescriptor- the FontDescriptor of the font to findbaseFont- PostScript name of the mapped font- Returns:
- the mapped FontBox font or null
-
findFontBoxFont
private FontBoxFont findFontBoxFont(java.lang.String postScriptName)
Finds a font with the given PostScript name, or a suitable substitute, or null.- Parameters:
postScriptName- PostScript font name
-
findFont
private FontBoxFont findFont(FontFormat format, java.lang.String postScriptName)
Finds a font with the given PostScript name, or a suitable substitute, or null.- Parameters:
postScriptName- PostScript font name
-
getFont
private FontInfo getFont(FontFormat format, java.lang.String postScriptName)
Finds the named font with the given format.
-
getCIDFont
public CIDFontMapping getCIDFont(java.lang.String baseFont, PDFontDescriptor fontDescriptor, PDCIDSystemInfo cidSystemInfo)
Finds a CFF CID-Keyed font with the given PostScript name, or a suitable substitute, or null. This method can also map CJK fonts via their CIDSystemInfo (ROS).- Specified by:
getCIDFontin interfaceFontMapper- Parameters:
fontDescriptor- FontDescriptorcidSystemInfo- the CID system info, e.g. "Adobe-Japan1", if any.baseFont- PostScript name of the mapped font- Returns:
- the mapped CIDFont font or null
-
getFontMatches
private java.util.PriorityQueue<FontMapperImpl.FontMatch> getFontMatches(PDFontDescriptor fontDescriptor, PDCIDSystemInfo cidSystemInfo)
Returns a list of matching fonts, scored by suitability. Positive scores indicate matches for certain attributes, while negative scores indicate mismatches. Zero scores are neutral.- Parameters:
fontDescriptor- FontDescriptor, always present.cidSystemInfo- Font's CIDSystemInfo, may be null.
-
probablyBarcodeFont
private boolean probablyBarcodeFont(PDFontDescriptor fontDescriptor)
-
isCharSetMatch
private boolean isCharSetMatch(PDCIDSystemInfo cidSystemInfo, FontInfo info)
Returns true if the character set described by CIDSystemInfo is present in the given font. Only applies to Adobe-GB1, Adobe-CNS1, Adobe-Japan1, Adobe-Korea1, as per the PDF spec.
-
printMatches
private FontMapperImpl.FontMatch printMatches(java.util.PriorityQueue<FontMapperImpl.FontMatch> queue)
For debugging. Prints all matches and returns the best match.
-
-