/** * Copyright (c) 2003-2005, www.pdfbox.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of pdfbox; nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * http://www.pdfbox.org * */ package org.pdfbox.pdmodel.font; import org.pdfbox.afmparser.AFMParser; import org.pdfbox.afmtypes.FontMetric; import org.pdfbox.cmapparser.CMapParser; import org.pdfbox.cmaptypes.CMap; import org.pdfbox.encoding.AFMEncoding; import org.pdfbox.encoding.DictionaryEncoding; import org.pdfbox.encoding.Encoding; import org.pdfbox.encoding.EncodingManager; import org.pdfbox.cos.COSArray; import org.pdfbox.cos.COSBase; import org.pdfbox.cos.COSDictionary; import org.pdfbox.cos.COSFloat; import org.pdfbox.cos.COSName; import org.pdfbox.cos.COSNumber; import org.pdfbox.cos.COSStream; import org.pdfbox.pdmodel.common.COSArrayList; import org.pdfbox.pdmodel.common.COSObjectable; import org.pdfbox.pdmodel.common.PDMatrix; import org.pdfbox.pdmodel.common.PDRectangle; import org.pdfbox.util.ResourceLoader; import org.apache.log4j.Logger; import java.awt.Graphics; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; /** * This is the base class for all PDF fonts. * * @author Ben Litchfield (ben@csh.rit.edu) * @version $Revision: 1.32 $ */ public abstract class PDFont implements COSObjectable { private static Logger log = Logger.getLogger(PDFont.class); /** * The cos dictionary for this font. */ protected COSDictionary font; /** * This is only used if this is a font object and it has an encoding. */ private Encoding fontEncoding = null; /** * This is only used if this is a font object and it has an encoding and it is * a type0 font with a cmap. */ private CMap cmap = null; private static Map afmResources = null; private static Map cmapObjects = null; private static Map afmObjects = null; private static Map cmapSubstitutions = null; static { //these are read-only once they are created afmResources = new HashMap(); cmapSubstitutions = new HashMap(); //these are read-write cmapObjects = Collections.synchronizedMap( new HashMap() ); afmObjects = Collections.synchronizedMap( new HashMap() ); afmResources.put( COSName.getPDFName( "Courier-Bold" ), "Resources/afm/Courier-Bold.afm" ); afmResources.put( COSName.getPDFName( "Courier-BoldOblique" ), "Resources/afm/Courier-BoldOblique.afm" ); afmResources.put( COSName.getPDFName( "Courier" ), "Resources/afm/Courier.afm" ); afmResources.put( COSName.getPDFName( "Courier-Oblique" ), "Resources/afm/Courier-Oblique.afm" ); afmResources.put( COSName.getPDFName( "Helvetica" ), "Resources/afm/Helvetica.afm" ); afmResources.put( COSName.getPDFName( "Helvetica-Bold" ), "Resources/afm/Helvetica-Bold.afm" ); afmResources.put( COSName.getPDFName( "Helvetica-BoldOblique" ), "Resources/afm/Helvetica-BoldOblique.afm" ); afmResources.put( COSName.getPDFName( "Helvetica-Oblique" ), "Resources/afm/Helvetica-Oblique.afm" ); afmResources.put( COSName.getPDFName( "Symbol" ), "Resources/afm/Symbol.afm" ); afmResources.put( COSName.getPDFName( "Times-Bold" ), "Resources/afm/Times-Bold.afm" ); afmResources.put( COSName.getPDFName( "Times-BoldItalic" ), "Resources/afm/Times-BoldItalic.afm" ); afmResources.put( COSName.getPDFName( "Times-Italic" ), "Resources/afm/Times-Italic.afm" ); afmResources.put( COSName.getPDFName( "Times-Roman" ), "Resources/afm/Times-Roman.afm" ); afmResources.put( COSName.getPDFName( "ZapfDingbats" ), "Resources/afm/ZapfDingbats.afm" ); cmapSubstitutions.put( "ETenms-B5-H", "ETen-B5-H" ); cmapSubstitutions.put( "ETenms-B5-V", "ETen-B5-V" ); } /** * This will clear AFM resources that are stored statically. * This is usually not a problem unless you want to reclaim * resources for a long running process. * * SPECIAL NOTE: The font calculations are currently in COSObject, which * is where they will reside until PDFont is mature enough to take them over. * PDFont is the appropriate place for them and not in COSObject but we need font * calculations for text extractaion. THIS METHOD WILL BE MOVED OR REMOVED * TO ANOTHER LOCATION IN A FUTURE VERSION OF PDFBOX. */ public static void clearResources() { afmObjects.clear(); cmapObjects.clear(); } /** * Constructor. */ public PDFont() { font = new COSDictionary(); font.setItem( COSName.TYPE, COSName.FONT ); } /** * Constructor. * * @param fontDictionary The font dictionary according to the PDF specification. */ public PDFont( COSDictionary fontDictionary ) { font = fontDictionary; } /** * @see COSObjectable#getCOSObject() */ public COSBase getCOSObject() { return font; } /** * This will get the font width for a character. * * @param c The character code to get the width for. * @param offset The offset into the array. * @param length The length of the data. * * @return The width is in 1000 unit of text space, ie 333 or 777 * * @throws IOException If an error occurs while parsing. */ public abstract float getFontWidth( byte[] c, int offset, int length ) throws IOException; /** * This will get the width of this string for this font. * * @param string The string to get the width of. * * @return The width of the string in 1000 units of text space, ie 333 567... * * @throws IOException If there is an error getting the width information. */ public float getStringWidth( String string ) throws IOException { byte[] data = string.getBytes(); float totalWidth = 0; for( int i=0; i