Top Description Fields Constructors Methods
org.apache.pdfbox.pdmodel.font

public abstract Class PDFont

extends Object
implements COSObjectable, PDFontLike
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.pdmodel.font.PDFontLike, org.apache.pdfbox.pdmodel.common.COSObjectable
Known Direct Subclasses
org.apache.pdfbox.pdmodel.font.PDSimpleFont, org.apache.pdfbox.pdmodel.font.PDType0Font
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, java.util.Collections, .HashMap, .List, .Map, java.util.concurrent.ConcurrentHashMap, org.apache.commons.logging.Log, .LogFactory, org.apache.fontbox.afm.FontMetrics, org.apache.fontbox.cmap.CMap, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSName, .COSNumber, .COSStream, org.apache.pdfbox.io.RandomAccessRead, org.apache.pdfbox.pdmodel.common.COSObjectable, org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName, org.apache.pdfbox.pdmodel.font.encoding.GlyphList, org.apache.pdfbox.util.Matrix, .Vector

This is the base class for all PDF fonts.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private final FontMetrics
afmStandard14

AFM for standard 14 fonts

private float
private final Map<Integer, Float>
protected static final Matrix
protected final COSDictionary
private PDFontDescriptor
private float
private static final Log
private final CMap
private List<Float>

Constructor Summary

AccessConstructor and Description
pack-priv
PDFont()

Constructor for embedding.

pack-priv
PDFont(Standard14Fonts.FontName baseFont)

Constructor for Standard 14.

protected
PDFont(COSDictionary
Font dictionary.
fontDictionary
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public abstract void
addToSubset(int
Unicode code point
codePoint
)

Adds the given Unicode point to the subset.

public final byte[]

Returns:

Array of PDF content stream bytes.
encode
(String
Any Unicode text.
text
)

Encodes the given string for use in a PDF content stream.

protected abstract byte[]

Returns:

Array of 1 to 4 PDF content stream bytes.
encode
(int
Unicode code point.
unicode
)

Encodes the given Unicode code point for use in a PDF content stream.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public float

Returns:

The width is in 1000 unit of text space, ie 333 or 777
getAverageFontWidth
()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getAverageFontWidth.

This will get the average font width for all characters.

public COSDictionary
getCOSObject()

Implements org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

Convert this standard java object to a COS object.

public Vector

Returns:

displacement vector
getDisplacement
(int
character code
code
)

Returns the displacement vector (w0, w1) in text space, for the given character.

public PDFontDescriptor
getFontDescriptor()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getFontDescriptor.

Returns the font descriptor, may be null.

public Matrix
getFontMatrix()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getFontMatrix.

Returns the font matrix, which represents the transformation from glyph space to text space.

public Vector
getPositionVector(int
character code
code
)

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getPositionVector.

Returns the position vector (v), in text space, for the given character.

public float

Returns:

the width of the space character
getSpaceWidth
()

Determines the width of the space character.

protected final FontMetrics

Returns:

the AFM
getStandard14AFM
()

Returns the AFM if this is a Standard 14 font.

protected abstract float

Returns:

width in 1/1000 text space
getStandard14Width
(int
character code
code
)

Returns the glyph width from the AFM if this is a Standard 14 font.

public float

Returns:

The width of the string in 1/1000 units of text space.
getStringWidth
(String
The text to get the width of.
text
)

Returns the width of the given Unicode string.

public String

Returns:

the subtype
getSubType
()

This will get the subtype of font.

protected CMap

Returns:

The /ToUnicode CMap or null if there is none.
getToUnicodeCMap
()

Get the /ToUnicode CMap.

public String

Returns:

The type of object that this is.
getType
()

This will always return "Font" for fonts.

public float
getWidth(int
character code
code
)

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getWidth.

Returns the advance width of the given character, in glyph space.

protected final List<Float>

Returns:

The widths of the characters.
getWidths
()

The widths of the characters.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

true if this font is one of the "Standard 14" fonts
isStandard14
()

Returns true if this font is one of the "Standard 14" fonts and receives special handling.

public abstract boolean

Returns:

true if the font uses vertical writing mode
isVertical
()

Returns true if the font uses vertical writing mode.

private PDFontDescriptor
private CMap
protected final CMap

Returns:

the CMap if present
readCMap
(COSBase
COSName or COSStream
base
)

Reads a CMap given a COS Stream or Name.

public abstract int

Returns:

character code
readCode
(InputStream
string stream
in
)

Reads a character code from a content stream string.

protected final void
setFontDescriptor(PDFontDescriptor
font descriptor of the embedded font
fontDescriptor
)

Sets the font descriptor when embedding a font.

public abstract void
subset()

Replaces this font with a subset containing only the given Unicode characters.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String

Returns:

Unicode character(s)
toUnicode
(int
character code
code
,
GlyphList
a custom glyph list to use instead of the Adobe Glyph List
customGlyphList
)

Returns the Unicode character sequence which corresponds to the given character code.

public String

Returns:

Unicode character(s)
toUnicode
(int
character code
code
)

Returns the Unicode character sequence which corresponds to the given character code.

public abstract boolean

Returns:

true if this font will be subset when embedded
willBeSubset
()

Returns true if this font will be subset when embedded.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

afmStandard14back to summary
private final FontMetrics afmStandard14

AFM for standard 14 fonts

avgFontWidthback to summary
private float avgFontWidth
codeToWidthMapback to summary
private final Map<Integer, Float> codeToWidthMap
DEFAULT_FONT_MATRIXback to summary
protected static final Matrix DEFAULT_FONT_MATRIX
dictback to summary
protected final COSDictionary dict
fontDescriptorback to summary
private PDFontDescriptor fontDescriptor
fontWidthOfSpaceback to summary
private float fontWidthOfSpace
LOGback to summary
private static final Log LOG
toUnicodeCMapback to summary
private final CMap toUnicodeCMap
widthsback to summary
private List<Float> widths

Constructor Detail

PDFontback to summary
pack-priv PDFont()

Constructor for embedding.

PDFontback to summary
pack-priv PDFont(Standard14Fonts.FontName baseFont)

Constructor for Standard 14.

PDFontback to summary
protected PDFont(COSDictionary fontDictionary)

Constructor.

Parameters
fontDictionary:COSDictionary

Font dictionary.

Method Detail

addToSubsetback to summary
public abstract void addToSubset(int codePoint)

Adds the given Unicode point to the subset.

Parameters
codePoint:int

Unicode code point

encodeback to summary
public final byte[] encode(String text) throws IOException

Encodes the given string for use in a PDF content stream.

Parameters
text:String

Any Unicode text.

Returns:byte[]

Array of PDF content stream bytes.

Exceptions
IOException:
If the text could not be encoded.
IllegalArgumentException:
if a character isn't supported by the font.
encodeback to summary
protected abstract byte[] encode(int unicode) throws IOException

Encodes the given Unicode code point for use in a PDF content stream. Content streams use a multi-byte encoding with 1 to 4 bytes.

This method is called when embedding text in PDFs and when filling in fields.

Parameters
unicode:int

Unicode code point.

Returns:byte[]

Array of 1 to 4 PDF content stream bytes.

Exceptions
IOException:
If the text could not be encoded.
IllegalArgumentException:
if a character isn't supported by the font.
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAverageFontWidthback to summary
public float getAverageFontWidth()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getAverageFontWidth.

This will get the average font width for all characters.

Returns:float

The width is in 1000 unit of text space, ie 333 or 777

Annotations
@Override
getCOSObjectback to summary
public COSDictionary getCOSObject()

Implements org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

Doc from org.apache.pdfbox.pdmodel.common.COSObjectable.getCOSObject.

Convert this standard java object to a COS object.

Returns:COSDictionary

The cos object that matches this Java object.

Annotations
@Override
getDisplacementback to summary
public Vector getDisplacement(int code) throws IOException

Returns the displacement vector (w0, w1) in text space, for the given character. For horizontal text only the x component is used, for vertical text only the y component.

Parameters
code:int

character code

Returns:Vector

displacement vector

Exceptions
IOException:
if the data could not be read
getFontDescriptorback to summary
public PDFontDescriptor getFontDescriptor()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getFontDescriptor.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getFontDescriptor.

Returns the font descriptor, may be null.

Returns:PDFontDescriptor

the font descriptor or null

Annotations
@Override
getFontMatrixback to summary
public Matrix getFontMatrix()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getFontMatrix.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getFontMatrix.

Returns the font matrix, which represents the transformation from glyph space to text space.

Returns:Matrix

the font matrix

Annotations
@Override
getPositionVectorback to summary
public Vector getPositionVector(int code)

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getPositionVector.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getPositionVector.

Returns the position vector (v), in text space, for the given character. This represents the position of vertical origin relative to horizontal origin, for horizontal writing it will always be (0, 0). For vertical writing both x and y are set.

Parameters
code:int

character code

Returns:Vector

position vector

Annotations
@Override
getSpaceWidthback to summary
public float getSpaceWidth()

Determines the width of the space character. This is very important for text extraction.

Returns:float

the width of the space character

getStandard14AFMback to summary
protected final FontMetrics getStandard14AFM()

Returns the AFM if this is a Standard 14 font.

Returns:FontMetrics

the AFM

getStandard14Widthback to summary
protected abstract float getStandard14Width(int code)

Returns the glyph width from the AFM if this is a Standard 14 font.

Parameters
code:int

character code

Returns:float

width in 1/1000 text space

getStringWidthback to summary
public float getStringWidth(String text) throws IOException

Returns the width of the given Unicode string.

Parameters
text:String

The text to get the width of.

Returns:float

The width of the string in 1/1000 units of text space.

Exceptions
IOException:
If there is an error getting the width information.
IllegalArgumentException:
if a character isn't supported by the font.
getSubTypeback to summary
public String getSubType()

This will get the subtype of font.

Returns:String

the subtype

getToUnicodeCMapback to summary
protected CMap getToUnicodeCMap()

Get the /ToUnicode CMap.

Returns:CMap

The /ToUnicode CMap or null if there is none.

getTypeback to summary
public String getType()

This will always return "Font" for fonts.

Returns:String

The type of object that this is.

getWidthback to summary
public float getWidth(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getWidth.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getWidth.

Returns the advance width of the given character, in glyph space.

If you want the visual bounds of the glyph then call getPath(..) on the appropriate PDFont subclass to retrieve the glyph outline as a GeneralPath instead. See the cyan rectangles in the DrawPrintTextLocations.java example to see this in action.

Parameters
code:int

character code

Returns:float

the width of the given character

Annotations
@Override
Exceptions
IOException:
if the width could not be read
getWidthsback to summary
protected final List<Float> getWidths()

The widths of the characters. This will be null for the standard 14 fonts.

Returns:List<Float>

The widths of the characters.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isStandard14back to summary
public boolean isStandard14()

Returns true if this font is one of the "Standard 14" fonts and receives special handling.

Returns:boolean

true if this font is one of the "Standard 14" fonts

isVerticalback to summary
public abstract boolean isVertical()

Returns true if the font uses vertical writing mode.

Returns:boolean

true if the font uses vertical writing mode

loadFontDescriptorback to summary
private PDFontDescriptor loadFontDescriptor()
loadUnicodeCmapback to summary
private CMap loadUnicodeCmap()
readCMapback to summary
protected final CMap readCMap(COSBase base) throws IOException

Reads a CMap given a COS Stream or Name. May return null if a predefined CMap does not exist.

Parameters
base:COSBase

COSName or COSStream

Returns:CMap

the CMap if present

Exceptions
IOException:
if the CMap could not be read
readCodeback to summary
public abstract int readCode(InputStream in) throws IOException

Reads a character code from a content stream string. Codes may be up to 4 bytes long.

Parameters
in:InputStream

string stream

Returns:int

character code

Exceptions
IOException:
if the CMap or stream cannot be read
setFontDescriptorback to summary
protected final void setFontDescriptor(PDFontDescriptor fontDescriptor)

Sets the font descriptor when embedding a font.

Parameters
fontDescriptor:PDFontDescriptor

font descriptor of the embedded font

subsetback to summary
public abstract void subset() throws IOException

Replaces this font with a subset containing only the given Unicode characters.

Exceptions
IOException:
if the subset could not be written
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
toUnicodeback to summary
public String toUnicode(int code, GlyphList customGlyphList)

Returns the Unicode character sequence which corresponds to the given character code.

Parameters
code:int

character code

customGlyphList:GlyphList

a custom glyph list to use instead of the Adobe Glyph List

Returns:String

Unicode character(s)

toUnicodeback to summary
public String toUnicode(int code)

Returns the Unicode character sequence which corresponds to the given character code.

Parameters
code:int

character code

Returns:String

Unicode character(s)

willBeSubsetback to summary
public abstract boolean willBeSubset()

Returns true if this font will be subset when embedded.

Returns:boolean

true if this font will be subset when embedded