org.docx4j.fonts.GlyphCheck Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx4j Show documentation
Show all versions of docx4j Show documentation
docx4j is a library which helps you to work with the Office Open
XML file format as used in docx
documents, pptx presentations, and xlsx spreadsheets.
/**
*
*/
package org.docx4j.fonts;
import java.util.HashSet;
import java.util.concurrent.ExecutionException;
import org.docx4j.fonts.fop.fonts.Typeface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* Check whether a PhysicalFont contains glyph sought.
*
* @author jharrop
*
*/
public class GlyphCheck {
protected static Logger log = LoggerFactory.getLogger(GlyphCheck.class);
private static LoadingCache cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.build(new CacheLoader() {
public Typeface load(PhysicalFont key) {
return key.getTypeface();
}
});
public static boolean hasChar(PhysicalFont physicalFont, char c) throws ExecutionException {
boolean exists = cache.get(physicalFont).hasChar(c);
if (log.isInfoEnabled()
&& !exists) {
log.info("Glyph " + (int) c + " (0x"
+ Integer.toHexString(c)
+ ") not available in font " + physicalFont.name);
}
return exists;
}
private static HashSet warnedAlready = new HashSet();
public static boolean hasChar(String fontName, char c) throws ExecutionException {
PhysicalFont pf = PhysicalFonts.get(fontName);
if (pf==null) {
if (!warnedAlready.contains(fontName)) {
log.warn("Couldn't get font " + fontName);
warnedAlready.add(fontName);
}
return false;
}
return hasChar(pf, c);
}
}