org.pdfbox.pdmodel.font

Class PDFont

Implemented Interfaces:
COSObjectable
Known Direct Subclasses:
PDCIDFont, PDSimpleFont, PDType0Font

public abstract class PDFont
extends java.lang.Object
implements COSObjectable

This is the base class for all PDF fonts.
Version:
$Revision: 1.43 $
Author:
Ben Litchfield

Field Summary

protected COSDictionary
font
The cos dictionary for this font.

Constructor Summary

PDFont()
Constructor.
PDFont(COSDictionary fontDictionary)
Constructor.

Method Summary

static void
clearResources()
This will clear AFM resources that are stored statically.
abstract void
drawString(String string, Graphics g, float fontSize, float xScale, float yScale, float x, float y)
This will draw a string on a canvas using the font.
String
encode(byte[] c, int offset, int length)
This will perform the encoding of a character if needed.
boolean
equals(Object other)
protected FontMetric
getAFM()
This will get an AFM object if one exists.
abstract float
getAverageFontWidth()
This will get the average font width for all characters.
protected float
getAverageFontWidthFromAFMFile()
This will attempt to get the average font width from an AFM file.
String
getBaseFont()
The PostScript name of the font.
COSBase
getCOSObject()
protected int
getCodeFromArray(byte[] data, int offset, int length)
Used for multibyte encodings.
Encoding
getEncoding()
This will get or create the encoder.
int
getFirstChar()
The code for the first char or -1 if there is none.
abstract PDRectangle
getFontBoundingBox()
This will get the fonts bouding box.
abstract float
getFontHeight(byte[] c, int offset, int length)
This will get the font width for a character.
PDMatrix
getFontMatrix()
This will get the matrix that is used to transform glyph space to text space.
abstract float
getFontWidth(byte[] c, int offset, int length)
This will get the font width for a character.
protected float
getFontWidthFromAFMFile(int code)
This will attempt to get the font width from an AFM file.
int
getLastChar()
The code for the last char or -1 if there is none.
float
getStringWidth(String string)
This will get the width of this string for this font.
String
getSubType()
This will get the subtype of font, Type1, Type3, ...
String
getType()
This will always return "Font" for fonts.
List
getWidths()
The widths of the characters.
int
hashCode()
void
setBaseFont(String baseFont)
Set the PostScript name of the font.
void
setEncoding(Encoding enc)
The will set the encoding for this font.
void
setFirstChar(int firstChar)
Set the first character this font supports.
void
setLastChar(int lastChar)
Set the last character this font supports.
void
setWidths(List widths)
Set the widths of the characters code.

Field Details

font

protected COSDictionary font
The cos dictionary for this font.

Constructor Details

PDFont

public PDFont()
Constructor.

PDFont

public PDFont(COSDictionary fontDictionary)
Constructor.
Parameters:
fontDictionary - The font dictionary according to the PDF specification.

Method Details

clearResources

public static void clearResources()
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.

drawString

public abstract void drawString(String string,
                                Graphics g,
                                float fontSize,
                                float xScale,
                                float yScale,
                                float x,
                                float y)
            throws IOException
This will draw a string on a canvas using the font.
Parameters:
string - The string to draw.
g - The graphics to draw onto.
fontSize - The size of the font to draw.
xScale - The x scaling percent.
yScale - The y scaling percent.
x - The x coordinate to draw at.
y - The y coordinate to draw at.

encode

public String encode(byte[] c,
                     int offset,
                     int length)
            throws IOException
This will perform the encoding of a character if needed.
Parameters:
c - The character to encode.
offset - The offset into the array to get the data
length - The number of bytes to read.
Returns:
The value of the encoded character.

equals

public boolean equals(Object other)

getAFM

protected FontMetric getAFM()
            throws IOException
This will get an AFM object if one exists.
Returns:
The afm object from the name.

getAverageFontWidth

public abstract float getAverageFontWidth()
            throws IOException
This will get the average font width for all characters.
Returns:
The width is in 1000 unit of text space, ie 333 or 777

getAverageFontWidthFromAFMFile

protected float getAverageFontWidthFromAFMFile()
            throws IOException
This will attempt to get the average font width from an AFM file.
Returns:
The average font width from the AFM file.

getBaseFont

public String getBaseFont()
The PostScript name of the font.
Returns:
The postscript name of the font.

getCOSObject

public COSBase getCOSObject()
Specified by:
getCOSObject in interface COSObjectable

getCodeFromArray

protected int getCodeFromArray(byte[] data,
                               int offset,
                               int length)
Used for multibyte encodings.
Parameters:
data - The array of data.
offset - The offset into the array.
length - The number of bytes to use.
Returns:
The int value of data from the array.

getEncoding

public Encoding getEncoding()
            throws IOException
This will get or create the encoder. modified by Christophe Huault : DGBS Strasbourg huault@free.fr october 2004
Returns:
The encoding to use.

getFirstChar

public int getFirstChar()
The code for the first char or -1 if there is none.
Returns:
The code for the first character.

getFontBoundingBox

public abstract PDRectangle getFontBoundingBox()
            throws IOException
This will get the fonts bouding box.
Returns:
The fonts bouding box.

getFontHeight

public abstract float getFontHeight(byte[] c,
                                    int offset,
                                    int length)
            throws IOException
This will get the font width for a character.
Parameters:
c - The character code to get the width for.
offset - The offset into the array.
length - The length of the data.
Returns:
The width is in 1000 unit of text space, ie 333 or 777

getFontMatrix

public PDMatrix getFontMatrix()
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.
Returns:
The matrix to transform from glyph space to text space.

getFontWidth

public abstract float getFontWidth(byte[] c,
                                   int offset,
                                   int length)
            throws IOException
This will get the font width for a character.
Parameters:
c - The character code to get the width for.
offset - The offset into the array.
length - The length of the data.
Returns:
The width is in 1000 unit of text space, ie 333 or 777

getFontWidthFromAFMFile

protected float getFontWidthFromAFMFile(int code)
            throws IOException
This will attempt to get the font width from an AFM file.
Parameters:
code - The character code we are trying to get.
Returns:
The font width from the AFM file.

getLastChar

public int getLastChar()
The code for the last char or -1 if there is none.
Returns:
The code for the last character.

getStringWidth

public float getStringWidth(String string)
            throws IOException
This will get the width of this string for this font.
Parameters:
string - The string to get the width of.
Returns:
The width of the string in 1000 units of text space, ie 333 567...

getSubType

public String getSubType()
This will get the subtype of font, Type1, Type3, ...
Returns:
The type of font that this is.

getType

public String getType()
This will always return "Font" for fonts.
Returns:
The type of object that this is.

getWidths

public List getWidths()
The widths of the characters. This will be null for the standard 14 fonts.
Returns:
The widths of the characters.

hashCode

public int hashCode()

setBaseFont

public void setBaseFont(String baseFont)
Set the PostScript name of the font.
Parameters:
baseFont - The postscript name for the font.

setEncoding

public void setEncoding(Encoding enc)
The will set the encoding for this font.
Parameters:
enc - The font encoding.

setFirstChar

public void setFirstChar(int firstChar)
Set the first character this font supports.
Parameters:
firstChar - The first character.

setLastChar

public void setLastChar(int lastChar)
Set the last character this font supports.
Parameters:
lastChar - The last character.

setWidths

public void setWidths(List widths)
Set the widths of the characters code.
Parameters:
widths - The widths of the character codes.