📄 basefontfactory.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner;
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* --------------------
* BaseFontFactory.java
* --------------------
* (C)opyright 2002, 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: BaseFontFactory.java,v 1.14 2003/11/07 18:33:55 taqua Exp $
*
* Changes
* -------
* 05-Dec-2002 : Added Javadocs (DG);
* 01-Feb-2003 : Refactoring moved this class from package
* com.jefinery.report.targets.pageable.output
*
*/
package org.jfree.report.modules.output.support.itext;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.DefaultFontMapper;
import org.jfree.report.modules.misc.configstore.base.ConfigFactory;
import org.jfree.report.modules.misc.configstore.base.ConfigStorage;
import org.jfree.report.modules.misc.configstore.base.ConfigStoreException;
import org.jfree.report.util.HashNMap;
import org.jfree.report.util.Log;
import org.jfree.report.util.ReportConfiguration;
import org.jfree.report.util.StringUtil;
/**
* The BaseFontFactory is used to find and register all TrueType fonts for embedding them
* in the PDF file.
*
* @author Thomas Morgner
*/
public final class BaseFontFactory extends DefaultFontMapper
{
/** The 'PDF auto init' property key. */
public static final String ITEXT_FONT_AUTOINIT
= "org.jfree.report.modules.output.support.itext.AutoInit";
/** The default 'PDF auto init' property value. */
public static final String ITEXT_FONT_AUTOINIT_ONINIT = "onInit";
/** The default 'PDF auto init' property value. */
public static final String ITEXT_FONT_AUTOINIT_LAZY = "lazy";
/** The default 'PDF auto init' property value. */
public static final String ITEXT_FONT_AUTOINIT_NEVER = "never";
/** The default 'PDF auto init' property value. */
public static final String ITEXT_FONT_AUTOINIT_DEFAULT = ITEXT_FONT_AUTOINIT_LAZY;
/**
* The iText font encoding specifies how to encode the text of iText documents
* for the given font.
*/
public static final String ITEXT_FONT_ENCODING
= "org.jfree.report.modules.output.support.itext.Encoding";
/** The default 'PDF encoding' property value. */
public static final String ITEXT_FONT_ENCODING_DEFAULT
= ReportConfiguration.getPlatformDefaultEncoding();
/**
* The name of the report property, which defines, whether the
* GarbageCollector should be run after the font registration.
*/
public static final String GC_AFTER_REGISTER =
"org.jfree.report.modules.output.support.itext.GCAfterRegister";
/**
* The font path filter is used to collect font files and directories
* during the font path registration.
*/
private static class FontPathFilter implements FileFilter
{
/**
* Default Constructor.
*/
public FontPathFilter()
{
}
/**
* Tests whether or not the specified abstract pathname should be
* included in a pathname list.
*
* @param pathname The abstract pathname to be tested
* @return <code>true</code> if and only if <code>pathname</code>
* should be included
*/
public boolean accept(final File pathname)
{
if (pathname.canRead() == false)
{
return false;
}
if (pathname.isDirectory())
{
return true;
}
final String name = pathname.getName();
if (StringUtil.endsWithIgnoreCase(name, ".afm"))
{
return true;
}
if (StringUtil.endsWithIgnoreCase(name, ".pfb"))
{
return true;
}
if (StringUtil.endsWithIgnoreCase(name, ".ttf"))
{
return true;
}
if (StringUtil.endsWithIgnoreCase(name, ".ttc"))
{
return true;
}
if (StringUtil.endsWithIgnoreCase(name, ".otf"))
{
return true;
}
return false;
}
}
/** The singleton instance of the font path filter. */
private static final FontPathFilter FONTPATHFILTER = new FontPathFilter();
/**
* The storage path for the config storage provider to cache the registered
* font names.
*/
private static final String FONTS_STORAGE_PATH = "registered_itext_fonts";
/**
* The storage path for the config storage provider to cache the registered
* font files. This is used to detect changes in the font setup.
*/
private static final String KNOWN_FONTS_PATH = "known_itext_font_files";
/**
* The storage path for the config storage provider to cache the registered
* font files. This is used to detect changes in the font setup.
*/
private static final String NO_EMBEDDING_FONTS_PATH = "noEmbedding_itext_font_files";
/** Singleton instance of the BaseFontFactory. */
private static BaseFontFactory fontFactory;
/**
* A set of fonts which have licence restrictions and may not be embedded.
* This map is keyed by the font file name. We prefer unrestricted fonts.
*/
private final Properties notEmbeddedFonts;
/** Fonts stored by name. */
private final Properties fontsByName;
/** A flag to check whether this factory is initialized. */
private boolean initialized;
/** A map of all confirmed (existent or seen) files. */
private Properties confirmedFiles;
/**
* Creates a new factory.
*/
private BaseFontFactory()
{
fontsByName = new Properties();
notEmbeddedFonts = new Properties();
}
/**
* Register os-specific font paths to the PDF-FontFactory. For unix-like operating
* systems, X11 is searched in /usr/X11R6 and the default truetype fontpath is added.
* For windows the system font path is added (%windir%/fonts)
*/
public synchronized void registerDefaultFontPath()
{
final ConfigStorage store = ConfigFactory.getInstance().getSystemStorage();
final HashNMap knownFonts = new HashNMap();
Properties seenFiles = new Properties();
confirmedFiles = new Properties();
if (store.existsProperties(FONTS_STORAGE_PATH))
{
try
{
final Properties propKnownFonts = store.loadProperties(FONTS_STORAGE_PATH, null);
final Enumeration keys = propKnownFonts.keys();
while (keys.hasMoreElements())
{
final String fontName = (String) keys.nextElement();
final String fileName = propKnownFonts.getProperty(fontName);
knownFonts.add(fileName, fontName);
}
final Properties propEmbedded = store.loadProperties(NO_EMBEDDING_FONTS_PATH, null);
notEmbeddedFonts.putAll(propEmbedded);
Log.info ("Registering fonts for the iText library; using a cached font registry.");
}
catch (ConfigStoreException cse)
{
Log.info("Unable to load font configuration, rebuilding.");
}
}
else
{
Log.info ("Registering fonts for the iText library.");
}
if (store.existsProperties(KNOWN_FONTS_PATH))
{
try
{
seenFiles = store.loadProperties(KNOWN_FONTS_PATH, null);
}
catch (ConfigStoreException cse)
{
Log.info("Unable to load known font files, rebuilding.");
}
}
String encoding = getDefaultFontEncoding();
// Correct the encoding for truetype fonts
if (encoding.equals(BaseFont.IDENTITY_H) || encoding.equals(BaseFont.IDENTITY_V))
{
// is this correct?
//encoding = "iso-8859-1";
encoding = "UTF-16";
}
final String osname = System.getProperty("os.name");
final String jrepath = System.getProperty("java.home");
final String fs = System.getProperty("file.separator");
Log.debug("Running on operating system: " + osname);
Log.debug("Character encoding used as default: " + encoding);
if (System.getProperty("mrj.version") != null)
{
final String userhome = System.getProperty("user.home");
Log.debug("Detected MacOS (Property 'mrj.version' is present.");
registerFontPath(new File(userhome + "/Library/Fonts"), encoding, knownFonts, seenFiles);
registerFontPath(new File("/Library/Fonts"), encoding, knownFonts, seenFiles);
registerFontPath(new File("/Network/Library/Fonts"), encoding, knownFonts, seenFiles);
registerFontPath(new File("/System/Library/Fonts"), encoding, knownFonts, seenFiles);
}
else if (StringUtil.startsWithIgnoreCase(osname, "windows"))
{
registerWindowsFontPath(encoding, knownFonts, seenFiles);
}
else
{
Log.debug("Assuming unix like file structures");
// Assume X11 is installed in the default location.
registerFontPath(new File("/usr/X11R6/lib/X11/fonts"), encoding, knownFonts, seenFiles);
registerFontPath(new File("/usr/share/fonts"), encoding, knownFonts, seenFiles);
}
registerFontPath(new File(jrepath, "lib" + fs + "fonts"), encoding, knownFonts, seenFiles);
try
{
store.storeProperties(FONTS_STORAGE_PATH, fontsByName);
store.storeProperties(KNOWN_FONTS_PATH, confirmedFiles);
store.storeProperties(NO_EMBEDDING_FONTS_PATH, notEmbeddedFonts);
}
catch (ConfigStoreException cse)
{
Log.info("Failed to store font configuration. This error is non-fatal, " +
"the font configuration will be rebuild from scratch, if necessary.");
}
Log.info("Completed font registration.");
initialized = true;
}
/**
* Registers the default windows font path. Once a font was found in the
* old seenFiles map and confirmed, that this font still exists, it gets
* copied into the confirmedFiles map.
*
* @param encoding the default font encoding.
* @param knownFonts a map containing all known fonts
* @param seenFiles a map containing all known font files.
*/
private void registerWindowsFontPath(final String encoding,
final HashNMap knownFonts,
final Properties seenFiles)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -