org.apache.pdfbox.pdmodel.font

## Class PDFont

- java.lang.Object
- - org.apache.pdfbox.pdmodel.font.PDFont

All Implemented Interfaces:
  COSObjectable, PDFontLike

Direct Known Subclasses:
  PDSimpleFont, PDType0Font

    public abstract class PDFont
    extends Object
    implements COSObjectable, PDFontLike

This is the base class for all PDF fonts.

Author:
  Ben Litchfield

### Field Summary

Fields
`protected static Matrix` `DEFAULT_FONT_MATRIX`
`protected COSDictionary` `dict`

### Constructor Summary

Constructors
`protected` `PDFont(COSDictionary fontDictionary)` Constructor.

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods
`abstract void` `addToSubset(int codePoint)` Adds the given Unicode point to the subset.
`protected abstract byte[]` `encode(int unicode)` Encodes the given Unicode code point for use in a PDF content stream.
`byte[]` `encode(String text)` Encodes the given string for use in a PDF content stream.
`boolean` `equals(Object other)`
`float` `getAverageFontWidth()` This will get the average font width for all characters.
`COSDictionary` `getCOSObject()` Convert this standard java object to a COS object.
`Vector` `getDisplacement(int code)` Returns the displacement vector (w0, w1) in text space, for the given character.
`PDFontDescriptor` `getFontDescriptor()` Returns the font descriptor, may be null.
`Matrix` `getFontMatrix()` Returns the font matrix, which represents the transformation from glyph space to text space.
`Vector` `getPositionVector(int code)` Returns the position vector (v), in text space, for the given character.
`float` `getSpaceWidth()` Determines the width of the space character.
`protected org.apache.fontbox.afm.FontMetrics` `getStandard14AFM()` Returns the AFM if this is a Standard 14 font.
`protected abstract float` `getStandard14Width(int code)` Returns the glyph width from the AFM if this is a Standard 14 font.
`float` `getStringWidth(String text)` Returns the width of the given Unicode string.
`String` `getSubType()` This will get the subtype of font.
`protected org.apache.fontbox.cmap.CMap` `getToUnicodeCMap()` Get the /ToUnicode CMap.
`String` `getType()` This will always return "Font" for fonts.
`float` `getWidth(int code)` Returns the advance width of the given character, in glyph space.
`protected List<Float>` `getWidths()` The widths of the characters.
`int` `hashCode()`
`boolean` `isStandard14()` Returns true if this font is one of the "Standard 14" fonts and receives special handling.
`abstract boolean` `isVertical()` Returns true if the font uses vertical writing mode.
`protected org.apache.fontbox.cmap.CMap` `readCMap(COSBase base)` Reads a CMap given a COS Stream or Name.
`abstract int` `readCode(InputStream in)` Reads a character code from a content stream string.
`protected void` `setFontDescriptor(PDFontDescriptor fontDescriptor)` Sets the font descriptor when embedding a font.
`abstract void` `subset()` Replaces this font with a subset containing only the given Unicode characters.
`String` `toString()`
`String` `toUnicode(int code)` Returns the Unicode character sequence which corresponds to the given character code.
`String` `toUnicode(int code, GlyphList customGlyphList)` Returns the Unicode character sequence which corresponds to the given character code.
`abstract boolean` `willBeSubset()` Returns true if this font will be subset when embedded.

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.apache.pdfbox.pdmodel.font.PDFontLike

`getBoundingBox, getHeight, getName, getWidthFromFont, hasExplicitWidth, isDamaged, isEmbedded`

### Field Detail

#### DEFAULT_FONT_MATRIX

    protected static final Matrix DEFAULT_FONT_MATRIX

#### dict

    protected final COSDictionary dict

### Constructor Detail

#### PDFont

    protected PDFont(COSDictionary fontDictionary)

Constructor.

Parameters:
  `fontDictionary` - Font dictionary.

### Method Detail

#### getStandard14AFM

    protected final org.apache.fontbox.afm.FontMetrics getStandard14AFM()

Returns the AFM if this is a Standard 14 font.

Returns:
  the AFM

#### getFontDescriptor

    public PDFontDescriptor getFontDescriptor()

Description copied from interface: `PDFontLike`
Returns the font descriptor, may be null.

Specified by:
  `getFontDescriptor` in interface `PDFontLike`
Returns:
  the font descriptor or null

#### setFontDescriptor

    protected final void setFontDescriptor(PDFontDescriptor fontDescriptor)

Sets the font descriptor when embedding a font.

Parameters:
  `fontDescriptor` - font descriptor of the embedded font

#### readCMap

    protected final org.apache.fontbox.cmap.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` - COSName or COSStream
Returns:
  the CMap if present
Throws:
  `IOException` - if the CMap could not be read

#### getCOSObject

    public COSDictionary getCOSObject()

Description copied from interface: `COSObjectable`
Convert this standard java object to a COS object.

Specified by:
  `getCOSObject` in interface `COSObjectable`
Returns:
  The cos object that matches this Java object.

#### getPositionVector

    public Vector getPositionVector(int code)

Description copied from interface: `PDFontLike`
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.

Specified by:
  `getPositionVector` in interface `PDFontLike`
Parameters:
  `code` - character code
Returns:
  position vector

#### getDisplacement

    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` - character code
Returns:
  displacement vector
Throws:
  `IOException` - if the data could not be read

#### getWidth

    public float getWidth(int code)
                   throws IOException

Description copied from interface: `PDFontLike`
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.

Specified by:
  `getWidth` in interface `PDFontLike`
Parameters:
  `code` - character code
Returns:
  the width of the given character
Throws:
  `IOException` - if the width could not be read

#### getStandard14Width

    protected abstract float getStandard14Width(int code)

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

Parameters:
  `code` - character code
Returns:
  width in 1/1000 text space

#### encode

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

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

Parameters:
  `text` - Any Unicode text.
Returns:
  Array of PDF content stream bytes.
Throws:
  `IOException` - If the text could not be encoded.
  `IllegalArgumentException` - if a character isn't supported by the font.

#### encode

    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` - Unicode code point.
Returns:
  Array of 1 to 4 PDF content stream bytes.
Throws:
  `IOException` - If the text could not be encoded.
  `IllegalArgumentException` - if a character isn't supported by the font.

#### getStringWidth

    public float getStringWidth(String text)
                         throws IOException

Returns the width of the given Unicode string.

Parameters:
  `text` - The text to get the width of.
Returns:
  The width of the string in 1/1000 units of text space.
Throws:
  `IOException` - If there is an error getting the width information.
  `IllegalArgumentException` - if a character isn't supported by the font.

#### getAverageFontWidth

    public float getAverageFontWidth()

This will get the average font width for all characters.

Specified by:
  `getAverageFontWidth` in interface `PDFontLike`
Returns:
  The width is in 1000 unit of text space, ie 333 or 777

#### readCode

    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` - string stream
Returns:
  character code
Throws:
  `IOException` - if the CMap or stream cannot be read

#### toUnicode

    public String toUnicode(int code,
                            GlyphList customGlyphList)

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

Parameters:
  `code` - character code
  `customGlyphList` - a custom glyph list to use instead of the Adobe Glyph List
Returns:
  Unicode character(s)

#### toUnicode

    public String toUnicode(int code)

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

Parameters:
  `code` - character code
Returns:
  Unicode character(s)

#### getType

    public String getType()

This will always return "Font" for fonts.

Returns:
  The type of object that this is.

#### getSubType

    public String getSubType()

This will get the subtype of font.

Returns:
  the subtype

#### getWidths

    protected final List<Float> getWidths()

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

Returns:
  The widths of the characters.

#### getFontMatrix

    public Matrix getFontMatrix()

Description copied from interface: `PDFontLike`
Returns the font matrix, which represents the transformation from glyph space to text space.

Specified by:
  `getFontMatrix` in interface `PDFontLike`
Returns:
  the font matrix

#### getSpaceWidth

    public float getSpaceWidth()

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

Returns:
  the width of the space character

#### isVertical

    public abstract boolean isVertical()

Returns true if the font uses vertical writing mode.

Returns:
  true if the font uses vertical writing mode

#### isStandard14

    public boolean isStandard14()

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

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

#### addToSubset

    public abstract void addToSubset(int codePoint)

Adds the given Unicode point to the subset.

Parameters:
  `codePoint` - Unicode code point

#### subset

    public abstract void subset()
                         throws IOException

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

Throws:
  `IOException` - if the subset could not be written

#### willBeSubset

    public abstract boolean willBeSubset()

Returns true if this font will be subset when embedded.

Returns:
  true if this font will be subset when embedded

#### equals

    public boolean equals(Object other)

Overrides:
  `equals` in class `Object`

#### hashCode

    public int hashCode()

Overrides:
  `hashCode` in class `Object`

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

#### getToUnicodeCMap

    protected org.apache.fontbox.cmap.CMap getToUnicodeCMap()

Get the /ToUnicode CMap.

Returns:
  The /ToUnicode CMap or null if there is none.