All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.fonts.GlyphCheck Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 6.1.2
Show newest version
/**
 * 
 */
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.error("Couldn't get font " + fontName);
				warnedAlready.add(fontName);
			}
			return false;
		}
		
		return hasChar(pf, c);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy