org.apache.pdfbox.pdmodel.font

## Class PDTrueTypeFont

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

All Implemented Interfaces:
  COSObjectable, PDFontLike, PDVectorFont

    public class PDTrueTypeFont
    extends PDSimpleFont
    implements PDVectorFont

TrueType font.

Author:
  Ben Litchfield

### Field Summary

### Fields inherited from class org.apache.pdfbox.pdmodel.font.PDSimpleFont

`encoding, glyphList`

### Fields inherited from class org.apache.pdfbox.pdmodel.font.PDFont

`DEFAULT_FONT_MATRIX, dict`

### Constructor Summary

Constructors
Constructor and Description
`PDTrueTypeFont(COSDictionary fontDictionary)` Creates a new TrueType font from a Font dictionary.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`int` `codeToGID(int code)` Returns the GID for the given character code.
`protected byte[]` `encode(int unicode)` Encodes the given Unicode code point for use in a PDF content stream.
`String` `getBaseFont()` Returns the PostScript name of the font.
`org.apache.fontbox.util.BoundingBox` `getBoundingBox()` Returns the font's bounding box.
`org.apache.fontbox.FontBoxFont` `getFontBoxFont()` Returns the embedded or system font used for rendering.
`protected Map<Integer,Integer>` `getGIDToCode()` Inverts the font's code -> GID mapping.
`float` `getHeight(int code)` Returns the height of the given character, in glyph space.
`String` `getName()` Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".
`GeneralPath` `getNormalizedPath(int code)` Returns the normalized glyph path for the given character code in a PDF.
`GeneralPath` `getPath(int code)` Returns the glyph path for the given character code.
`GeneralPath` `getPath(String name)` Returns the path for the character with the given name.
`org.apache.fontbox.ttf.TrueTypeFont` `getTrueTypeFont()` Returns the embedded or substituted TrueType font.
`float` `getWidthFromFont(int code)` Returns the width of a glyph in the embedded font file.
`boolean` `hasGlyph(int code)` Returns true if this font contains a glyph for the given character code in a PDF.
`boolean` `hasGlyph(String name)` Returns true if the font contains the character with the given name.
`boolean` `isDamaged()` Returns true if the embedded font file is damaged.
`boolean` `isEmbedded()` Returns true if the font file is embedded in the PDF.
`static PDTrueTypeFont` `load(PDDocument doc, File file, Encoding encoding)` Loads a TTF to be embedded into a document as a simple font.
`static PDTrueTypeFont` `load(PDDocument doc, InputStream input, Encoding encoding)` Loads a TTF to be embedded into a document as a simple font.
`static PDTrueTypeFont` `load(PDDocument doc, org.apache.pdfbox.io.RandomAccessRead randomAccessRead, Encoding encoding)` Loads a TTF to be embedded into a document as a simple font.
`static PDTrueTypeFont` `load(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, Encoding encoding)` Loads a TTF to be embedded into a document as a simple font.
`int` `readCode(InputStream in)` Reads a character code from a content stream string.
`protected Encoding` `readEncodingFromFont()` Called by readEncoding() if the encoding needs to be extracted from the font file.

### Methods inherited from class org.apache.pdfbox.pdmodel.font.PDSimpleFont

`addToSubset, getEncoding, getGlyphList, getStandard14Width, getSymbolicFlag, hasExplicitWidth, isFontSymbolic, isNonZeroBoundingBox, isStandard14, isSymbolic, isVertical, readEncoding, subset, toUnicode, toUnicode, willBeSubset`

### Methods inherited from class org.apache.pdfbox.pdmodel.font.PDFont

`encode, equals, getAverageFontWidth, getCOSObject, getDisplacement, getFontDescriptor, getFontMatrix, getPositionVector, getSpaceWidth, getStandard14AFM, getStringWidth, getSubType, getToUnicodeCMap, getType, getWidth, getWidths, hashCode, readCMap, setFontDescriptor, toString`

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

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

### Constructor Detail

#### PDTrueTypeFont

    public PDTrueTypeFont(COSDictionary fontDictionary)
                   throws IOException

Creates a new TrueType font from a Font dictionary.

Parameters:
  `fontDictionary` - The font dictionary according to the PDF specification.
Throws:
  `IOException` - if the font could not be created

### Method Detail

#### load

    public static PDTrueTypeFont load(PDDocument doc,
                                      File file,
                                      Encoding encoding)
                               throws IOException

Loads a TTF to be embedded into a document as a simple font.

Note: Simple fonts only support 256 characters. For Unicode support, use `PDType0Font.load(PDDocument, File)` instead.

Parameters:
  `doc` - The PDF document that will hold the embedded font.
  `file` - A TTF file.
  `encoding` - The PostScript encoding vector to be used for embedding.
Returns:
  a PDTrueTypeFont instance.
Throws:
  `IOException` - If there is an error loading the data.

#### load

    public static PDTrueTypeFont load(PDDocument doc,
                                      InputStream input,
                                      Encoding encoding)
                               throws IOException

Loads a TTF to be embedded into a document as a simple font.

Note: Simple fonts only support 256 characters. For Unicode support, use `PDType0Font.load(PDDocument, InputStream)` instead.

Parameters:
  `doc` - The PDF document that will hold the embedded font.
  `input` - A TTF file stream. It will be closed before returning.
  `encoding` - The PostScript encoding vector to be used for embedding.
Returns:
  a PDTrueTypeFont instance.
Throws:
  `IOException` - If there is an error loading the data.

#### load

    public static PDTrueTypeFont load(PDDocument doc,
                                      org.apache.fontbox.ttf.TrueTypeFont ttf,
                                      Encoding encoding)
                               throws IOException

Loads a TTF to be embedded into a document as a simple font.

Note: Simple fonts only support 256 characters. For Unicode support, use `PDType0Font.load(PDDocument, InputStream)` instead.

Parameters:
  `doc` - The PDF document that will hold the embedded font.
  `ttf` - A true type font
  `encoding` - The PostScript encoding vector to be used for embedding.
Returns:
  a PDTrueTypeFont instance.
Throws:
  `IOException` - If there is an error loading the data.

#### load

    public static PDTrueTypeFont load(PDDocument doc,
                                      org.apache.pdfbox.io.RandomAccessRead randomAccessRead,
                                      Encoding encoding)
                               throws IOException

Loads a TTF to be embedded into a document as a simple font.

Note: Simple fonts only support 256 characters. For Unicode support, use `PDType0Font.load(PDDocument, File)` instead.

Parameters:
  `doc` - The PDF document that will hold the embedded font.
  `randomAccessRead` - the source of the TTF.
  `encoding` - The PostScript encoding vector to be used for embedding.
Returns:
  a PDTrueTypeFont instance.
Throws:
  `IOException` - If there is an error loading the data.

#### getBaseFont

    public final String getBaseFont()

Returns the PostScript name of the font.

Returns:
  the PostScript name of the font

#### readEncodingFromFont

    protected Encoding readEncodingFromFont()
                                     throws IOException

Description copied from class: `PDSimpleFont`
Called by readEncoding() if the encoding needs to be extracted from the font file.

Specified by:
  `readEncodingFromFont` in class `PDSimpleFont`
Returns:
  encoding of the font
Throws:
  `IOException` - if the font file could not be read.

#### readCode

    public int readCode(InputStream in)
                 throws IOException

Description copied from class: `PDFont`
Reads a character code from a content stream string. Codes may be up to 4 bytes long.

Specified by:
  `readCode` in class `PDFont`
Parameters:
  `in` - string stream
Returns:
  character code
Throws:
  `IOException` - if the CMap or stream cannot be read

#### getName

    public String getName()

Description copied from interface: `PDFontLike`
Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".

Specified by:
  `getName` in interface `PDFontLike`
Returns:
  the name of the font

#### getBoundingBox

    public org.apache.fontbox.util.BoundingBox getBoundingBox()
                                                       throws IOException

Description copied from interface: `PDFontLike`
Returns the font's bounding box.

Specified by:
  `getBoundingBox` in interface `PDFontLike`
Returns:
  the bounding box
Throws:
  `IOException` - if the bounding box could not be read

#### isDamaged

    public boolean isDamaged()

Description copied from interface: `PDFontLike`
Returns true if the embedded font file is damaged.

Specified by:
  `isDamaged` in interface `PDFontLike`
Returns:
  true if the embedded font file is damaged

#### getTrueTypeFont

    public org.apache.fontbox.ttf.TrueTypeFont getTrueTypeFont()

Returns the embedded or substituted TrueType font.

Returns:
  the embedded or substituted TrueType font

#### getWidthFromFont

    public float getWidthFromFont(int code)
                           throws IOException

Description copied from interface: `PDFontLike`
Returns the width of a glyph in the embedded font file.

Specified by:
  `getWidthFromFont` in interface `PDFontLike`
Parameters:
  `code` - character code
Returns:
  width in glyph space
Throws:
  `IOException` - if the font could not be read

#### getHeight

    public float getHeight(int code)
                    throws IOException

Description copied from interface: `PDFontLike`
Returns the height of the given character, in glyph space. This can be expensive to calculate. Results are only approximate.

Warning: This method is deprecated in PDFBox 2.0 because there is no meaningful value which it can return. The `PDFontLike.getWidth(int)` method returns the advance width of a glyph, but there is no corresponding advance height. The logical height of a character is the same for every character in a font, so if you want that, retrieve the font bbox's height. Otherwise 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. See the cyan rectangles in the DrawPrintTextLocations.java example to see this in action.

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

#### encode

    protected byte[] encode(int unicode)
                     throws IOException

Description copied from class: `PDFont`
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.

Specified by:
  `encode` in class `PDFont`
Parameters:
  `unicode` - Unicode code point.
Returns:
  Array of 1 to 4 PDF content stream bytes.
Throws:
  `IOException` - If the text could not be encoded.

#### getGIDToCode

    protected Map<Integer,Integer> getGIDToCode()
                                         throws IOException

Inverts the font's code -> GID mapping. Any duplicate (GID -> code) mappings will be lost.

Returns:
  the GID for the given code
Throws:
  `IOException` - if the data could not be read

#### isEmbedded

    public boolean isEmbedded()

Description copied from interface: `PDFontLike`
Returns true if the font file is embedded in the PDF.

Specified by:
  `isEmbedded` in interface `PDFontLike`
Returns:
  true if the font file is embedded in the PDF

#### getPath

    public GeneralPath getPath(int code)
                        throws IOException

Description copied from interface: `PDVectorFont`
Returns the glyph path for the given character code.

Specified by:
  `getPath` in interface `PDVectorFont`
Parameters:
  `code` - character code in a PDF. Not to be confused with unicode.
Returns:
  the glyph path for the given character code
Throws:
  `IOException` - if the font could not be read

#### getPath

    public GeneralPath getPath(String name)
                        throws IOException

Description copied from class: `PDSimpleFont`
Returns the path for the character with the given name. For some fonts, GIDs may be used instead of names when calling this method. *

Specified by:
  `getPath` in class `PDSimpleFont`
Parameters:
  `name` - glyph name
Returns:
  glyph path of the character with the given name
Throws:
  `IOException` - if the path could not be read

#### getNormalizedPath

    public GeneralPath getNormalizedPath(int code)
                                  throws IOException

Description copied from interface: `PDVectorFont`
Returns the normalized glyph path for the given character code in a PDF. The resulting path is normalized to the PostScript 1000 unit square, and fallback glyphs are returned where appropriate, e.g. for missing glyphs.

Specified by:
  `getNormalizedPath` in interface `PDVectorFont`
Parameters:
  `code` - character code in a PDF. Not to be confused with unicode.
Returns:
  the normalized glyph path for the given character code
Throws:
  `IOException` - if the font could not be read

#### hasGlyph

    public boolean hasGlyph(String name)
                     throws IOException

Description copied from class: `PDSimpleFont`
Returns true if the font contains the character with the given name.

Specified by:
  `hasGlyph` in class `PDSimpleFont`
Parameters:
  `name` - glyph name
Returns:
  true if the font contains the character with the given name
Throws:
  `IOException` - if the path could not be read

#### getFontBoxFont

    public org.apache.fontbox.FontBoxFont getFontBoxFont()

Description copied from class: `PDSimpleFont`
Returns the embedded or system font used for rendering. This is never null.

Specified by:
  `getFontBoxFont` in class `PDSimpleFont`
Returns:
  the embedded or system font used for rendering

#### hasGlyph

    public boolean hasGlyph(int code)
                     throws IOException

Description copied from interface: `PDVectorFont`
Returns true if this font contains a glyph for the given character code in a PDF.

Specified by:
  `hasGlyph` in interface `PDVectorFont`
Parameters:
  `code` - character code in a PDF. Not to be confused with unicode.
Returns:
  true if this font contains a glyph for the given character code
Throws:
  `IOException` - if the font could not be read

#### codeToGID

    public int codeToGID(int code)
                  throws IOException

Returns the GID for the given character code.

Parameters:
  `code` - character code
Returns:
  GID (glyph index)
Throws:
  `IOException` - if the data could not be read