Package org.docx4j.fonts
Class BestMatchingMapper
java.lang.Object
org.docx4j.fonts.Mapper
org.docx4j.fonts.BestMatchingMapper
This mapper uses Panose to guess the physical font
which is a closest fit for the font used in the
document.
It is most likely to be suitable on Linux or OSX
systems which don't have Microsoft's fonts installed.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Map<String,FontSubstitutions.Replace> The substitutions listed in FontSubstitutions.xml Will be used only if there is no panose match.(package private) intprotected static org.slf4j.Loggerstatic final intMax difference for it to be considered an acceptable match.private static final intprivate static final HashMap<String,MicrosoftFonts.Font> private static final Map<String,PhysicalFont> Physical fonts remapped using the short key convention in FontSubstitutions.xml; For purpose, see comments below.Fields inherited from class org.docx4j.fonts.Mapper
boldForms, boldItalicForms, FONT_FALLBACK, fontMappings, italicForms, regularForms -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringfindClosestPanoseMatch(String documentFontName, Panose documentFontPanose, Map<String, PhysicalFont> physicalFontSpace, int matchThreshold) Logic to search panose space for closest matching physical font file.private static StringgenerateFontKey(String fontName) private static voidprivate PhysicalFontgetAssociatedPhysicalFont(String documentFontName, String orignalKey, Panose soughtPanose) static final Map<String,MicrosoftFonts.Font> private static PhysicalFontstatic voidprivate static voidpanoseDebugReportOnPhysicalFonts(Map<String, PhysicalFont> physicalFontMap) voidpopulateFontMappings(Set<String> documentFontNames, Fonts wmlFonts) Populate the fontMappings object.private static final voidGet candidate substitutions On a non-MS platform, we need these for two things: 1.private static final voidGet Microsoft fonts We need these: 1.Methods inherited from class org.docx4j.fonts.Mapper
get, getBoldForm, getBoldItalicForm, getFontMappings, getItalicForm, getRegularForm, getSubstituteFontXsltExtension, getSubstituteFontXsltExtension, put, registerBoldForm, registerBoldItalicForm, registerItalicForm, registerRegularForm, size
-
Field Details
-
log
protected static org.slf4j.Logger log -
msFontsFilenames
-
explicitSubstitutionsMap
The substitutions listed in FontSubstitutions.xml Will be used only if there is no panose match. -
physicalFontsByKey
Physical fonts remapped using the short key convention in FontSubstitutions.xml; For purpose, see comments below. -
lastSeenNumberOfPhysicalFonts
int lastSeenNumberOfPhysicalFonts -
MATCH_THRESHOLD
public static final int MATCH_THRESHOLDMax difference for it to be considered an acceptable match. Note that this value will depend on the weights in the difference function.- See Also:
-
MATCH_THRESHOLD_INTRA_FAMILY
private static final int MATCH_THRESHOLD_INTRA_FAMILY- See Also:
-
-
Constructor Details
-
BestMatchingMapper
public BestMatchingMapper()
-
-
Method Details
-
getMsFontsFilenames
-
setupMicrosoftFontFilenames
Get Microsoft fonts We need these: 1. On Microsoft platform, to embed in PDF output 2. docx4all - all platforms - to populate font dropdown list- Throws:
Exception
-
generateKeysForPhysicalFonts
private static void generateKeysForPhysicalFonts() -
generateFontKey
-
getPhysicalFontByKey
-
setupExplicitSubstitutionsMap
Get candidate substitutions On a non-MS platform, we need these for two things: 1. to embed this font in the PDF output, in place of MS font 2. in docx4all, use in editor but it will only be used if there is no panose match. Issues with FontSubstitutions.xml, as noted and addressed by Jeromy Evans http://www.docx4java.org/forums/docx-java-f6/bestmatchingmapper-bugs-handling-explicit-substitutions-t940.html (1) FontSubstutitions.xml uses the lowercase whitespace and punctuation removed name of the font. If the document contains "Times New Roman" it is not matched to the equivalent replace element for "timesnewroman". Similarly "Arial" is not matched to "arial". (2) When matched, the method searching PhysicalFonts for the substitution font also uses the short key, not the proper name used by PhysicalFonts. For example, if matching "arial" to a substitute it tries to find "freesans" in PhysicalFont's map instead of "Free Sans". (3) On the system tested, the SubsFonts value is inclusive of the leading whitespace (eg in the line above, the first token is "\n\t\tarial' instead of "arial" (seems odd that whitespace is included after unmarshalling). This means the first substitution always fails to match a font. As, by convention, the first token is usually the name of the font, this effectively means on systems where msttcorefonts are installed, the BestMatchingMapper fails to match the exact font. ie. it can't match "arial" to "arial" because the substitution is named "\n\t\tarial".- Throws:
Exception
-
populateFontMappings
Populate the fontMappings object. We make an entry for each of the documentFontNames.- Specified by:
populateFontMappingsin classMapper- Parameters:
documentFontNames- - the fonts used in the documentwmlFonts- - the content model for the fonts part- Throws:
Exception
-
getAssociatedPhysicalFont
private PhysicalFont getAssociatedPhysicalFont(String documentFontName, String orignalKey, Panose soughtPanose) - Parameters:
fm-soughtPanose-
-
findClosestPanoseMatch
private String findClosestPanoseMatch(String documentFontName, Panose documentFontPanose, Map<String, PhysicalFont> physicalFontSpace, int matchThreshold) Logic to search panose space for closest matching physical font file. Returns key of matching font in physicalFontMap. -
main
- Throws:
Exception
-
panoseDebugReportOnPhysicalFonts
-