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

public Class PDTrueTypeFont

extends PDSimpleFont
implements PDVectorFont
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.pdmodel.font.PDVectorFont
Imports
java.awt.geom.AffineTransform, .GeneralPath, java.io.File, .IOException, .InputStream, java.nio.charset.StandardCharsets, java.util.HashMap, .Map, org.apache.commons.logging.Log, .LogFactory, org.apache.fontbox.FontBoxFont, org.apache.fontbox.cff.CFFFont, .Type2CharString, org.apache.fontbox.ttf.CmapSubtable, .CmapTable, .GlyphData, .GlyphTable, .OTFParser, .OpenTypeFont, .PostScriptTable, .TTFParser, .TrueTypeFont, org.apache.fontbox.util.BoundingBox, org.apache.pdfbox.cos.COSDictionary, .COSName, org.apache.pdfbox.io.IOUtils, .RandomAccessRead, .RandomAccessReadBuffer, .RandomAccessReadBufferedFile, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.common.PDRectangle, .PDStream, org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName, org.apache.pdfbox.pdmodel.font.encoding.BuiltInEncoding, .Encoding, .GlyphList, .MacOSRomanEncoding, .MacRomanEncoding, .StandardEncoding, .Type1Encoding, .WinAnsiEncoding

TrueType font.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private boolean
private CmapSubtable
private CmapSubtable
private CmapSubtable
private BoundingBox
private Map<Integer, Integer>
private static final Map<String, Integer>
private final boolean
private final boolean
private static final Log
private final OpenTypeFont
private static final int
private static final int
private static final int
private final TrueTypeFont
Inherited from org.apache.pdfbox.pdmodel.font.PDSimpleFont:
encodingglyphList

Constructor Summary

AccessConstructor and Description
public
PDTrueTypeFont(COSDictionary
The font dictionary according to the PDF specification.
fontDictionary
)

Creates a new TrueType font from a Font dictionary.

private
PDTrueTypeFont(PDDocument document, TrueTypeFont ttf, Encoding encoding, boolean closeTTF)

Creates a new TrueType font for embedding.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

GID (glyph index)
codeToGID
(int
character code
code
)

Returns the GID for the given character code.

protected byte[]
encode(int
Unicode code point.
unicode
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.encode.

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

private void
extractCmapTable()

extract all useful "cmap" subtables.

private BoundingBox
public final String

Returns:

the PostScript name of the font
getBaseFont
()

Returns the PostScript name of the font.

public BoundingBox
getBoundingBox()

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

Returns the font's bounding box.

public FontBoxFont
getFontBoxFont()

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontBoxFont.

Returns the embedded or system font used for rendering.

protected Map<Integer, Integer>

Returns:

the GID for the given code
getGIDToCode
()

Inverts the font's code -> GID mapping.

public float
getHeight(int
character code
code
)

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

Returns the height of the given character, in glyph space.

public String
getName()

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

Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".

public GeneralPath
getNormalizedPath(int
character code in a PDF. Not to be confused with unicode.
code
)

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getNormalizedPath.

Returns the normalized glyph path for the given character code in a PDF.

private TTFParser
getParser(RandomAccessRead randomAccessRead, boolean isEmbedded)

public GeneralPath
getPath(int
character code in a PDF. Not to be confused with unicode.
code
)

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getPath.

Returns the glyph path for the given character code.

public GeneralPath
getPath(String
glyph name
name
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getPath.

Returns the path for the character with the given name.

private GeneralPath
public TrueTypeFont

Returns:

the embedded or substituted TrueType font
getTrueTypeFont
()

Returns the embedded or substituted TrueType font.

public float
getWidthFromFont(int
character code
code
)

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

Returns the width of a glyph in the embedded font file.

public boolean
hasGlyph(String
glyph name
name
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.hasGlyph.

Returns true if the font contains the character with the given name.

public boolean
hasGlyph(int
character code in a PDF. Not to be confused with unicode.
code
)

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.hasGlyph.

Returns true if this font contains a glyph for the given character code in a PDF.

public boolean
isDamaged()

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

Returns true if the embedded font file is damaged.

public boolean
isEmbedded()

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

Returns true if the font file is embedded in the PDF.

public static PDTrueTypeFont

Returns:

a PDTrueTypeFont instance.
load
(PDDocument
The PDF document that will hold the embedded font.
doc
,
File
A TTF file.
file
,
Encoding
The PostScript encoding vector to be used for embedding.
encoding
)

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

public static PDTrueTypeFont

Returns:

a PDTrueTypeFont instance.
load
(PDDocument
The PDF document that will hold the embedded font.
doc
,
InputStream
A TTF file stream
input
,
Encoding
The PostScript encoding vector to be used for embedding.
encoding
)

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

public static PDTrueTypeFont

Returns:

a PDTrueTypeFont instance.
load
(PDDocument
The PDF document that will hold the embedded font.
doc
,
TrueTypeFont
A true type font
ttf
,
Encoding
The PostScript encoding vector to be used for embedding.
encoding
)

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

public static PDTrueTypeFont

Returns:

a PDTrueTypeFont instance.
load
(PDDocument
The PDF document that will hold the embedded font.
doc
,
RandomAccessRead
the source of the TTF.
randomAccessRead
,
Encoding
The PostScript encoding vector to be used for embedding.
encoding
)

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

public int
readCode(InputStream
string stream
in
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.readCode.

Reads a character code from a content stream string.

protected Encoding
readEncodingFromFont()

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.readEncodingFromFont.

Called by readEncoding() if the encoding needs to be extracted from the font file.

Inherited from org.apache.pdfbox.pdmodel.font.PDSimpleFont:
addToSubsetgetEncodinggetGlyphListgetStandard14WidthgetSymbolicFlaghasExplicitWidthisFontSymbolicisNonZeroBoundingBoxisStandard14isSymbolicisVerticalreadEncodingsubsettoUnicodetoUnicodewillBeSubset

Field Detail

cmapInitializedback to summary
private boolean cmapInitialized
cmapMacRomanback to summary
private CmapSubtable cmapMacRoman
cmapWinSymbolback to summary
private CmapSubtable cmapWinSymbol
cmapWinUnicodeback to summary
private CmapSubtable cmapWinUnicode
fontBBoxback to summary
private BoundingBox fontBBox
gidToCodeback to summary
private Map<Integer, Integer> gidToCode
INVERTED_MACOS_ROMANback to summary
private static final Map<String, Integer> INVERTED_MACOS_ROMAN
isDamagedback to summary
private final boolean isDamaged
isEmbeddedback to summary
private final boolean isEmbedded
LOGback to summary
private static final Log LOG

Hides org.apache.pdfbox.pdmodel.font.PDSimpleFont.LOG.

otfback to summary
private final OpenTypeFont otf
START_RANGE_F000back to summary
private static final int START_RANGE_F000
START_RANGE_F100back to summary
private static final int START_RANGE_F100
START_RANGE_F200back to summary
private static final int START_RANGE_F200
ttfback to summary
private final TrueTypeFont ttf

Constructor Detail

PDTrueTypeFontback to summary
public PDTrueTypeFont(COSDictionary fontDictionary) throws IOException

Creates a new TrueType font from a Font dictionary.

Parameters
fontDictionary:COSDictionary

The font dictionary according to the PDF specification.

Exceptions
IOException:
if the font could not be created
PDTrueTypeFontback to summary
private PDTrueTypeFont(PDDocument document, TrueTypeFont ttf, Encoding encoding, boolean closeTTF) throws IOException

Creates a new TrueType font for embedding.

Method Detail

codeToGIDback to summary
public int codeToGID(int code) throws IOException

Returns the GID for the given character code.

Parameters
code:int

character code

Returns:int

GID (glyph index)

Exceptions
IOException:
if the data could not be read
encodeback to summary
protected byte[] encode(int unicode) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.encode.

Doc from org.apache.pdfbox.pdmodel.font.PDFont.encode.

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.

Annotations
@Override
Exceptions
IOException:
If the text could not be encoded.
extractCmapTableback to summary
private void extractCmapTable() throws IOException

extract all useful "cmap" subtables.

generateBoundingBoxback to summary
private BoundingBox generateBoundingBox() throws IOException
getBaseFontback to summary
public final String getBaseFont()

Returns the PostScript name of the font.

Returns:String

the PostScript name of the font

getBoundingBoxback to summary
public BoundingBox getBoundingBox() throws IOException

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

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

Returns the font's bounding box.

Returns:BoundingBox

the bounding box

Annotations
@Override
Exceptions
IOException:
if the bounding box could not be read
getFontBoxFontback to summary
public FontBoxFont getFontBoxFont()

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontBoxFont.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontBoxFont.

Returns the embedded or system font used for rendering. This is never null.

Returns:FontBoxFont

the embedded or system font used for rendering

Annotations
@Override
getGIDToCodeback to summary
protected Map<Integer, Integer> getGIDToCode() throws IOException

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

Returns:Map<Integer, Integer>

the GID for the given code

Exceptions
IOException:
if the data could not be read
getHeightback to summary
public float getHeight(int code) throws IOException

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

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

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 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.

Parameters
code:int

character code

Returns:float

the height of the given character

Annotations
@Override
Exceptions
IOException:
if the height could not be read
getNameback to summary
public String getName()

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

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

Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".

Returns:String

the name of the font

Annotations
@Override
getNormalizedPathback to summary
public GeneralPath getNormalizedPath(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getNormalizedPath.

Doc from org.apache.pdfbox.pdmodel.font.PDVectorFont.getNormalizedPath.

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.

Parameters
code:int

character code in a PDF. Not to be confused with unicode.

Returns:GeneralPath

the normalized glyph path for the given character code

Annotations
@Override
Exceptions
IOException:
if the font could not be read
getParserback to summary
private TTFParser getParser(RandomAccessRead randomAccessRead, boolean isEmbedded) throws IOException
getPathback to summary
public GeneralPath getPath(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getPath.

Doc from org.apache.pdfbox.pdmodel.font.PDVectorFont.getPath.

Returns the glyph path for the given character code.

Parameters
code:int

character code in a PDF. Not to be confused with unicode.

Returns:GeneralPath

the glyph path for the given character code

Annotations
@Override
Exceptions
IOException:
if the font could not be read
getPathback to summary
public GeneralPath getPath(String name) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getPath.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.getPath.

Returns the path for the character with the given name. For some fonts, GIDs may be used instead of names when calling this method. *

Parameters
name:String

glyph name

Returns:GeneralPath

glyph path of the character with the given name

Annotations
@Override
Exceptions
IOException:
if the path could not be read
getPathFromOutlinesback to summary
private GeneralPath getPathFromOutlines(int code) throws IOException
getTrueTypeFontback to summary
public TrueTypeFont getTrueTypeFont()

Returns the embedded or substituted TrueType font.

Returns:TrueTypeFont

the embedded or substituted TrueType font

getWidthFromFontback to summary
public float getWidthFromFont(int code) throws IOException

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

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

Returns the width of a glyph in the embedded font file.

Parameters
code:int

character code

Returns:float

width in glyph space

Annotations
@Override
Exceptions
IOException:
if the font could not be read
hasGlyphback to summary
public boolean hasGlyph(String name) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.hasGlyph.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.hasGlyph.

Returns true if the font contains the character with the given name.

Parameters
name:String

glyph name

Returns:boolean

true if the font contains the character with the given name

Annotations
@Override
Exceptions
IOException:
if the path could not be read
hasGlyphback to summary
public boolean hasGlyph(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.hasGlyph.

Doc from org.apache.pdfbox.pdmodel.font.PDVectorFont.hasGlyph.

Returns true if this font contains a glyph for the given character code in a PDF.

Parameters
code:int

character code in a PDF. Not to be confused with unicode.

Returns:boolean

true if this font contains a glyph for the given character code

Annotations
@Override
Exceptions
IOException:
if the font could not be read
isDamagedback to summary
public boolean isDamaged()

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

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

Returns true if the embedded font file is damaged.

Returns:boolean

true if the embedded font file is damaged

Annotations
@Override
isEmbeddedback to summary
public boolean isEmbedded()

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

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

Returns true if the font file is embedded in the PDF.

Returns:boolean

true if the font file is embedded in the PDF

Annotations
@Override
loadback to summary
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:PDDocument

The PDF document that will hold the embedded font.

file:File

A TTF file.

encoding:Encoding

The PostScript encoding vector to be used for embedding.

Returns:PDTrueTypeFont

a PDTrueTypeFont instance.

Exceptions
IOException:
If there is an error loading the data.
loadback to summary
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:PDDocument

The PDF document that will hold the embedded font.

input:InputStream

A TTF file stream

encoding:Encoding

The PostScript encoding vector to be used for embedding.

Returns:PDTrueTypeFont

a PDTrueTypeFont instance.

Exceptions
IOException:
If there is an error loading the data.
loadback to summary
public static PDTrueTypeFont load(PDDocument doc, 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:PDDocument

The PDF document that will hold the embedded font.

ttf:TrueTypeFont

A true type font

encoding:Encoding

The PostScript encoding vector to be used for embedding.

Returns:PDTrueTypeFont

a PDTrueTypeFont instance.

Exceptions
IOException:
If there is an error loading the data.
loadback to summary
public static PDTrueTypeFont load(PDDocument doc, 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:PDDocument

The PDF document that will hold the embedded font.

randomAccessRead:RandomAccessRead

the source of the TTF.

encoding:Encoding

The PostScript encoding vector to be used for embedding.

Returns:PDTrueTypeFont

a PDTrueTypeFont instance.

Exceptions
IOException:
If there is an error loading the data.
readCodeback to summary
public int readCode(InputStream in) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.readCode.

Doc from org.apache.pdfbox.pdmodel.font.PDFont.readCode.

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

Annotations
@Override
Exceptions
IOException:
if the CMap or stream cannot be read
readEncodingFromFontback to summary
protected Encoding readEncodingFromFont() throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.readEncodingFromFont.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.readEncodingFromFont.

Called by readEncoding() if the encoding needs to be extracted from the font file.

Returns:Encoding

encoding of the font

Annotations
@Override
Exceptions
IOException:
if the font file could not be read.