Method from org.apache.pdfbox.pdmodel.font.PDFont Detail: |
public static void clearResources() {
afmObjects.clear();
cmapObjects.clear();
}
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. |
abstract public void drawString(String string,
Graphics g,
float fontSize,
AffineTransform at,
float x,
float y) throws IOException
This will draw a string on a canvas using the font. |
public String encode(byte[] c,
int offset,
int length) throws IOException {
String retval = null;
if( isTypeFont() )
{
if( cmap == null )
{
if( font.getDictionaryObject( COSName.TO_UNICODE ) instanceof COSStream )
{
COSStream toUnicode = (COSStream)font.getDictionaryObject( COSName.TO_UNICODE );
if( toUnicode != null )
{
parseCmap( null, toUnicode.getUnfilteredStream(), null );
}
}
else
{
COSBase encoding = getEncodingObject(); //font.getDictionaryObject( COSName.ENCODING );
if( encoding instanceof COSStream )
{
COSStream encodingStream = (COSStream)encoding;
parseCmap( null, encodingStream.getUnfilteredStream(), null );
}
else if( isType0Font() && encoding instanceof COSName )
{
COSName encodingName = (COSName)encoding;
cmap = cmapObjects.get( encodingName );
if( cmap == null )
{
String cmapName = encodingName.getName();
if (encodingName.getName().equals( COSName.IDENTITY_H.getName() ))
{
COSArray descendantFontArray =
(COSArray)font.getDictionaryObject( COSName.DESCENDANT_FONTS );
if (descendantFontArray != null)
{
COSDictionary descendantFontDictionary =
(COSDictionary)descendantFontArray.getObject( 0 );
PDFont descendentFont = PDFontFactory.createFont( descendantFontDictionary );
COSDictionary cidsysteminfo =
(COSDictionary)descendentFont.font.getDictionaryObject(COSName.CIDSYSTEMINFO);
if (cidsysteminfo != null)
{
String ordering = cidsysteminfo.getString(COSName.ORDERING);
String registry = cidsysteminfo.getString(COSName.REGISTRY);
cmapName = registry + "-" + ordering+"-UCS2";
}
}
}
else
{
cmapName = CMapSubstitution.substituteCMap( cmapName );
}
String resourceRoot = "Resources/cmap/";
String resourceName = resourceRoot + cmapName;
parseCmap( resourceRoot, ResourceLoader.loadResource( resourceName ), encodingName );
if( cmap == null && !encodingName.getName().equals( COSName.IDENTITY_H.getName() ) )
{
throw new IOException( "Error: Could not find predefined " +
"CMAP file for '" + encodingName.getName() + "'" );
}
}
}
else if( encoding instanceof COSName ||
encoding instanceof COSDictionary )
{
Encoding currentFontEncoding = getEncoding();
if( currentFontEncoding != null )
{
retval = currentFontEncoding.getCharacter( getCodeFromArray( c, offset, length ) );
}
}
else
{
COSDictionary fontDescriptor =
(COSDictionary)font.getDictionaryObject( COSName.FONT_DESC );
if( isTrueTypeFont() && fontDescriptor != null &&
(fontDescriptor.getDictionaryObject( COSName.FONT_FILE )!= null ||
fontDescriptor.getDictionaryObject( COSName.FONT_FILE2 ) != null ||
fontDescriptor.getDictionaryObject( COSName.FONT_FILE3 ) != null ) )
{
//If we are using an embedded font then there is not much we can do besides
//return the same character codes.
//retval = new String( c,offset, length );
retval = getStringFromArray( c, offset, length );
}
else
{
//this case will be handled below after checking the cmap
}
}
}
}
}
if( retval == null && cmap != null )
{
retval = cmap.lookup( c, offset, length );
}
COSBase encodingCOS = font.getDictionaryObject(COSName.ENCODING);
if ( encodingCOS instanceof COSName )
{
EncodingConverter converter = EncodingConversionManager.getConverter(((COSName)encodingCOS).getName());
if ( converter != null )
{
if ( retval != null )
{
retval = converter.convertString(retval);
}
else
{
retval = converter.convertBytes(c, offset, length, cmap);
}
return retval;
}
}
//if we havn't found a value yet and
//we are still on the first byte and
//there is no cmap or the cmap does not have 2 byte mappings then try to encode
//using fallback methods.
if( retval == null &&
length == 1 &&
(cmap == null || !cmap.hasTwoByteMappings()))
{
Encoding encoding = getEncoding();
if( encoding != null )
{
retval = encoding.getCharacter( getCodeFromArray( c, offset, length ) );
}
if( retval == null )
{
retval = getStringFromArray( c, offset, length );
}
}
return retval;
}
This will perform the encoding of a character if needed. |
public boolean equals(Object other) {
return other instanceof PDFont && ((PDFont)other).getCOSObject() == this.getCOSObject();
}
|
protected FontMetric getAFM() throws IOException {
if(afm==null){
COSBase baseFont = font.getDictionaryObject( COSName.BASE_FONT );
COSName name = null;
if( baseFont instanceof COSName )
{
name = (COSName)baseFont;
}
else if( baseFont instanceof COSString )
{
COSString string = (COSString)baseFont;
name = COSName.getPDFName( string.getString() );
}
if( name != null )
{
afm = afmObjects.get( name );
if( afm == null )
{
String resource = afmResources.get( name );
if( resource == null )
{
//ok for now
//throw new IOException( "Unknown AFM font '" + name.getName() + "'" );
}
else
{
InputStream afmStream = ResourceLoader.loadResource( resource );
if( afmStream == null )
{
throw new IOException( "Can't handle font width:" + resource );
}
AFMParser parser = new AFMParser( afmStream );
parser.parse();
afm = parser.getResult();
afmObjects.put( name, afm );
}
}
}
}
return afm;
}
This will get an AFM object if one exists. |
abstract public float getAverageFontWidth() throws IOException
This will get the average font width for all characters. |
protected float getAverageFontWidthFromAFMFile() throws IOException {
float retval = 0;
FontMetric metric = getAFM();
if( metric != null )
{
retval = metric.getAverageCharacterWidth();
}
return retval;
}
This will attempt to get the average font width from an AFM file. |
public String getBaseFont() {
return font.getNameAsString( COSName.BASE_FONT );
}
The PostScript name of the font. |
public COSBase getCOSObject() {
return font;
}
|
protected int getCodeFromArray(byte[] data,
int offset,
int length) {
int code = 0;
for( int i=0; i< length; i++ )
{
code < < = 8;
code |= (data[offset+i]+256)%256;
}
return code;
}
Used for multibyte encodings. |
public Encoding getEncoding() throws IOException {
if( fontEncoding == null )
{
EncodingManager manager = getEncodingManager();
COSBase encoding = getEncodingObject(); //font.getDictionaryObject( COSName.ENCODING );
if( encoding == null )
{
FontMetric metric = getAFM();
if( metric != null )
{
fontEncoding = new AFMEncoding( metric );
}
if( fontEncoding == null )
{
fontEncoding = manager.getStandardEncoding();
}
}
/**
* Si la cl� /Encoding existe dans le dictionnaire fonte il y a deux possibilit�s :
* 1er cas : elle est associ� � une reference contenant un dictionnaire de type encoding.
* Ce dictionnaire PDF est repr�sent� par un DictionaryEncoding.
* If the /Encoding Key does exist in the font dictionary, there are two cases :
* case one : The value associated with /Encoding is a reference to a dictionary.
* This dictionary is represented by an instance of DictionaryEncoding class
*/
else if( encoding instanceof COSDictionary )
{
COSDictionary encodingDic = (COSDictionary)encoding;
//Let's see if the encoding dictionary has a base encoding
//If it does not then we will attempt to get it from the font
//file
COSName baseEncodingName = (COSName) encodingDic.getDictionaryObject(
COSName.BASE_ENCODING);
//on ajoute une entr�e /BaseEncoding dans /Encoding uniquement si elle en est absente
//if not find in Encoding dictinary target, we try to find it from else where
if( baseEncodingName == null)
{
COSName fontEncodingFromFile = getEncodingFromFont();
encodingDic.setItem(
COSName.BASE_ENCODING,
fontEncodingFromFile );
}
fontEncoding = new DictionaryEncoding( encodingDic );
}
else if( encoding instanceof COSName )
{
if( !encoding.equals( COSName.IDENTITY_H ) )
{
fontEncoding = manager.getEncoding( (COSName)encoding );
}
}
else
{
throw new IOException( "Unexpected encoding type:" + encoding.getClass().getName() );
}
}
return fontEncoding;
}
This will get or create the encoder.
modified by Christophe Huault : DGBS Strasbourg huault@free.fr october 2004 |
protected static EncodingManager getEncodingManager() {
if(encodingManager == null){
encodingManager = new EncodingManager();
}
return encodingManager;
}
|
public int getFirstChar() {
return font.getInt( COSName.FIRST_CHAR, -1 );
}
The code for the first char or -1 if there is none. |
abstract public PDRectangle getFontBoundingBox() throws IOException
This will get the fonts bouding box. |
abstract public float getFontHeight(byte[] c,
int offset,
int length) throws IOException
This will get the font width for a character. |
public PDMatrix getFontMatrix() {
PDMatrix matrix = null;
COSArray array = (COSArray)font.getDictionaryObject( COSName.FONT_MATRIX );
if( array == null )
{
array = new COSArray();
array.add( new COSFloat( 0.001f ) );
array.add( COSInteger.ZERO );
array.add( COSInteger.ZERO );
array.add( new COSFloat( 0.001f ) );
array.add( COSInteger.ZERO );
array.add( COSInteger.ZERO );
}
matrix = new PDMatrix(array);
return matrix;
}
This will get the matrix that is used to transform glyph space to
text space. By default there are 1000 glyph units to 1 text space
unit, but type3 fonts can use any value.
Note:If this is a type3 font then it can be modified via the PDType3Font.setFontMatrix, otherwise this
is a read-only property. |
abstract public float getFontWidth(byte[] c,
int offset,
int length) throws IOException
This will get the font width for a character. |
protected float getFontWidthFromAFMFile(int code) throws IOException {
float retval = 0;
FontMetric metric = getAFM();
if( metric != null )
{
Encoding encoding = getEncoding();
COSName characterName = encoding.getName( code );
retval = metric.getCharacterWidth( characterName.getName() );
}
return retval;
}
This will attempt to get the font width from an AFM file. |
public int getLastChar() {
return font.getInt( COSName.LAST_CHAR, -1 );
}
The code for the last char or -1 if there is none. |
public float getStringWidth(String string) throws IOException {
byte[] data = string.getBytes();
float totalWidth = 0;
for( int i=0; i< data.length; i++ )
{
totalWidth+=getFontWidth( data, i, 1 );
}
return totalWidth;
}
This will get the width of this string for this font. |
public String getSubType() {
if (subtype == null) {
subtype = font.getNameAsString( COSName.SUBTYPE );
type0Font = "Type0".equals(subtype);
trueTypeFont = "TrueType".equals(subtype);
typeFont = type0Font || "Type1".equals(subtype) || trueTypeFont;
}
return subtype;
}
This will get the subtype of font, Type1, Type3, ... |
public String getType() {
return font.getNameAsString( COSName.TYPE );
}
This will always return "Font" for fonts. |
public List getWidths() {
COSArray array = (COSArray)font.getDictionaryObject( COSName.WIDTHS );
return COSArrayList.convertFloatCOSArrayToList( array );
}
The widths of the characters. This will be null for the standard 14 fonts. |
public int hashCode() {
return this.getCOSObject().hashCode();
}
|
public void setBaseFont(String baseFont) {
font.setName( COSName.BASE_FONT, baseFont );
}
Set the PostScript name of the font. |
public void setEncoding(Encoding enc) {
font.setItem( COSName.ENCODING, enc );
fontEncoding = enc;
}
The will set the encoding for this font. |
public void setFirstChar(int firstChar) {
font.setInt( COSName.FIRST_CHAR, firstChar );
}
Set the first character this font supports. |
public void setLastChar(int lastChar) {
font.setInt( COSName.LAST_CHAR, lastChar );
}
Set the last character this font supports. |
public void setWidths(List widths) {
font.setItem( COSName.WIDTHS, COSArrayList.converterToCOSArray( widths ) );
}
Set the widths of the characters code. |