Top Description Fields Constructors Methods
org.apache.fontbox.ttf

public Class TrueTypeFont

extends Object
implements FontBoxFont, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, org.apache.fontbox.FontBoxFont
Known Direct Subclasses
org.apache.fontbox.ttf.OpenTypeFont
Imports
java.awt.geom.GeneralPath, java.io.Closeable, .IOException, .InputStream, java.util.ArrayList, .Arrays, .Collection, .Collections, .HashMap, .List, .Map, org.apache.commons.logging.Log, .LogFactory, org.apache.fontbox.FontBoxFont, org.apache.fontbox.ttf.model.GsubData, org.apache.fontbox.util.BoundingBox

A TrueType font file.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private final TTFDataStream
private final List<String>
private boolean
private final Object
private final Object
private static final Log
private int
private volatile Map<String, Integer>
protected final Map<String, TTFTable>
private int
private float

Constructor Summary

AccessConstructor and Description
pack-priv
TrueTypeFont(TTFDataStream
The font data.
fontData
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
addTable(TTFTable
The table to add.
table
)

Add a table definition.

public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public void
disableGsubFeature(String
The GSUB feature to disable
featureTag
)

Disable a particular glyph substitution feature.

public void
enableGsubFeature(String
The GSUB feature to enable
featureTag
)

Enable a particular glyph substitution feature.

public void
enableVerticalSubstitutions()

Enable glyph substitutions for vertical writing.

public int

Returns:

the height
getAdvanceHeight
(int
the GID
gid
)

Returns the height for the given GID.

public int

Returns:

the width
getAdvanceWidth
(int
the GID
gid
)

Returns the width for the given GID.

public CmapTable

Returns:

The "cmap" table or null if it doesn't exist.
getCmap
()

Get the "cmap" table for this TTF.

public BoundingBox
getFontBBox()

Implements org.apache.fontbox.FontBoxFont.getFontBBox.

Returns the font's bounding box in PostScript units.

public List<Number>
getFontMatrix()

Implements org.apache.fontbox.FontBoxFont.getFontMatrix.

Returns the FontMatrix in PostScript units.

public GlyphTable

Returns:

The glyf table or null if it doesn't exist.
getGlyph
()

Get the glyf table for this TTF.

public GlyphSubstitutionTable

Returns:

The "gsub" table or null if it doesn't exist.
getGsub
()

Get the "gsub" table for this TTF.

public GsubData

Returns:

the GSubData of the GlyphSubstitutionTable or GsubData#NO_DATA_FOUND if no GSUB data is available, its scripts are not supported or it was disabled for that font
getGsubData
()

Returns the GSubData of the GlyphSubstitutionTable if present.

public HeaderTable

Returns:

The head table or null if it doesn't exist.
getHeader
()

Get the head table for this TTF.

public HorizontalHeaderTable

Returns:

The hhea table or null if it doesn't exist.
getHorizontalHeader
()

Get the hhea table for this TTF.

public HorizontalMetricsTable

Returns:

The hmtx table or null if it doesn't exist.
getHorizontalMetrics
()

Get the hmtx table for this TTF.

public IndexToLocationTable

Returns:

The loca table or null if it doesn't exist.
getIndexToLocation
()

Get the loca table for this TTF.

public KerningTable

Returns:

The "kern" table or null if it doesn't exist.
getKerning
()

Get the "kern" table for this TTF.

public MaximumProfileTable

Returns:

The maxp table or null if it doesn't exist.
getMaximumProfile
()

Get the maxp table for this TTF.

public String
getName()

Implements org.apache.fontbox.FontBoxFont.getName.

The PostScript name of the font.

public NamingTable

Returns:

The naming table or null if it doesn't exist.
getNaming
()

This will get the naming table for the true type font.

public int

Returns:

the number of glyphs
getNumberOfGlyphs
()

Returns the number of glyphs (MaximumProfile.numGlyphs).

public InputStream

Returns:

COSStream TrueType font program stream
getOriginalData
()

Get the data of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).

public long

Returns:

the size.
getOriginalDataSize
()

Get the data size of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).

public OS2WindowsMetricsTable

Returns:

The OS/2 table or null if it doesn't exist.
getOS2Windows
()

Get the OS/2 table for this TTF.

public GeneralPath
getPath(String
PostScript glyph name
name
)

Implements org.apache.fontbox.FontBoxFont.getPath.

Returns the path for the character with the given name.

public PostScriptTable

Returns:

The postscript table or null if it doesn't exist.
getPostScript
()

Get the postscript table for this TTF.

protected TTFTable

Returns:

The table with the given tag.
getTable
(String
the name of the table to be returned
tag
)

This will get the table for the given tag.

public byte[]

Returns:

the raw bytes of the given table
getTableBytes
(TTFTable
the table to read.
table
)

Returns the raw bytes of the given table.

public Map<String, TTFTable>

Returns:

All of the tables.
getTableMap
()

Get all of the tables.

public byte[]

Returns:

the raw bytes of the given table
getTableNBytes
(TTFTable
the table to read.
table
,
int
maximum length of array to return
limit
)

Returns the raw bytes of the given table, no more than limit bytes.

public Collection<TTFTable>

Returns:

All of the tables.
getTables
()

Get all of the tables.

private CmapSubtable
getUnicodeCmapImpl(boolean isStrict)

public CmapLookup

Returns:

cmap to perform glyph substitution
getUnicodeCmapLookup
()

Returns the best Unicode from the font (the most general).

public CmapLookup

Returns:

cmap to perform glyph substitution
getUnicodeCmapLookup
(boolean
False if we allow falling back to any cmap, even if it's not Unicode.
isStrict
)

Returns the best Unicode from the font (the most general).

public int

Returns:

units per EM
getUnitsPerEm
()

Returns the units per EM (Header.unitsPerEm).

public float

Returns:

Returns the version.
getVersion
()

public VerticalHeaderTable

Returns:

The vhea table or null if it doesn't exist.
getVerticalHeader
()

Get the vhea table for this TTF.

public VerticalMetricsTable

Returns:

The vmtx table or null if it doesn't exist.
getVerticalMetrics
()

Get the vmtx table for this TTF.

public VerticalOriginTable

Returns:

The VORG table or null if it doesn't exist.
getVerticalOrigin
()

Get the VORG table for this TTF.

public float
getWidth(String
PostScript glyph name
name
)

Implements org.apache.fontbox.FontBoxFont.getWidth.

Returns the advance width for the character with the given name.

public boolean
hasGlyph(String
PostScript glyph name
name
)

Implements org.apache.fontbox.FontBoxFont.hasGlyph.

Returns true if the font contains the given glyph.

public boolean

Returns:

Returns true if the GSUB table can be used for this font
isEnableGsub
()

public int

Returns:

the GID for the given PostScript name
nameToGID
(String
the PostScript name.
name
)

Returns the GID for the given PostScript name, if the "post" table is present.

private int
parseUniName(String name)

Parses a Unicode PostScript name in the format uniXXXX.

private void
pack-priv void
readTable(TTFTable
the table to be initialized
table
)

Read the given table if necessary.

pack-priv void
readTableHeaders(String
the name of the table to be read
tag
,
FontHeaders
consumes headers
outHeaders
)

Read the given table headers.

public void
setEnableGsub(boolean enableGsub)

Enable or disable the GSUB table for this font.

pack-priv void
setVersion(float
The version to set.
versionValue
)

Set the version.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

databack to summary
private final TTFDataStream data
enabledGsubFeaturesback to summary
private final List<String> enabledGsubFeatures
enableGsubback to summary
private boolean enableGsub
lockPSNamesback to summary
private final Object lockPSNames
lockReadtableback to summary
private final Object lockReadtable
LOGback to summary
private static final Log LOG
numberOfGlyphsback to summary
private int numberOfGlyphs
postScriptNamesback to summary
private volatile Map<String, Integer> postScriptNames
tablesback to summary
protected final Map<String, TTFTable> tables
unitsPerEmback to summary
private int unitsPerEm
versionback to summary
private float version

Constructor Detail

TrueTypeFontback to summary
pack-priv TrueTypeFont(TTFDataStream fontData)

Constructor. Clients should use the TTFParser to create a new TrueTypeFont object.

Parameters
fontData:TTFDataStream

The font data.

Method Detail

addTableback to summary
pack-priv void addTable(TTFTable table)

Add a table definition. Package-private, used by TTFParser only.

Parameters
table:TTFTable

The table to add.

closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
disableGsubFeatureback to summary
public void disableGsubFeature(String featureTag)

Disable a particular glyph substitution feature.

Parameters
featureTag:String

The GSUB feature to disable

enableGsubFeatureback to summary
public void enableGsubFeature(String featureTag)

Enable a particular glyph substitution feature. This feature might not be supported by the font, or might not be implemented in PDFBox yet.

Parameters
featureTag:String

The GSUB feature to enable

enableVerticalSubstitutionsback to summary
public void enableVerticalSubstitutions()

Enable glyph substitutions for vertical writing.

getAdvanceHeightback to summary
public int getAdvanceHeight(int gid) throws IOException

Returns the height for the given GID.

Parameters
gid:int

the GID

Returns:int

the height

Exceptions
IOException:
if there was an error reading the metrics table.
getAdvanceWidthback to summary
public int getAdvanceWidth(int gid) throws IOException

Returns the width for the given GID.

Parameters
gid:int

the GID

Returns:int

the width

Exceptions
IOException:
if there was an error reading the metrics table.
getCmapback to summary
public CmapTable getCmap() throws IOException

Get the "cmap" table for this TTF.

Returns:CmapTable

The "cmap" table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getFontBBoxback to summary
public BoundingBox getFontBBox() throws IOException

Implements org.apache.fontbox.FontBoxFont.getFontBBox.

Doc from org.apache.fontbox.FontBoxFont.getFontBBox.

Returns the font's bounding box in PostScript units.

Returns:BoundingBox

the bounding box of the font

Annotations
@Override
Exceptions
IOException:
if something went wrong when accessing the font data
getFontMatrixback to summary
public List<Number> getFontMatrix() throws IOException

Implements org.apache.fontbox.FontBoxFont.getFontMatrix.

Doc from org.apache.fontbox.FontBoxFont.getFontMatrix.

Returns the FontMatrix in PostScript units.

Returns:List<Number>

the font matrix

Annotations
@Override
Exceptions
IOException:
if something went wrong when accessing the font data
getGlyphback to summary
public GlyphTable getGlyph() throws IOException

Get the glyf table for this TTF.

Returns:GlyphTable

The glyf table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getGsubback to summary
public GlyphSubstitutionTable getGsub() throws IOException

Get the "gsub" table for this TTF.

Returns:GlyphSubstitutionTable

The "gsub" table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getGsubDataback to summary
public GsubData getGsubData() throws IOException

Returns the GSubData of the GlyphSubstitutionTable if present.

Returns:GsubData

the GSubData of the GlyphSubstitutionTable or GsubData#NO_DATA_FOUND if no GSUB data is available, its scripts are not supported or it was disabled for that font

Exceptions
IOException:
if the font data could not be read
getHeaderback to summary
public HeaderTable getHeader() throws IOException

Get the head table for this TTF.

Returns:HeaderTable

The head table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getHorizontalHeaderback to summary
public HorizontalHeaderTable getHorizontalHeader() throws IOException

Get the hhea table for this TTF.

Returns:HorizontalHeaderTable

The hhea table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getHorizontalMetricsback to summary
public HorizontalMetricsTable getHorizontalMetrics() throws IOException

Get the hmtx table for this TTF.

Returns:HorizontalMetricsTable

The hmtx table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getIndexToLocationback to summary
public IndexToLocationTable getIndexToLocation() throws IOException

Get the loca table for this TTF.

Returns:IndexToLocationTable

The loca table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getKerningback to summary
public KerningTable getKerning() throws IOException

Get the "kern" table for this TTF.

Returns:KerningTable

The "kern" table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getMaximumProfileback to summary
public MaximumProfileTable getMaximumProfile() throws IOException

Get the maxp table for this TTF.

Returns:MaximumProfileTable

The maxp table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getNameback to summary
public String getName() throws IOException

Implements org.apache.fontbox.FontBoxFont.getName.

Doc from org.apache.fontbox.FontBoxFont.getName.

The PostScript name of the font.

Returns:String

the postscript of the font or null

Annotations
@Override
Exceptions
IOException:
if something went wrong when accessing the font data
getNamingback to summary
public NamingTable getNaming() throws IOException

This will get the naming table for the true type font.

Returns:NamingTable

The naming table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getNumberOfGlyphsback to summary
public int getNumberOfGlyphs() throws IOException

Returns the number of glyphs (MaximumProfile.numGlyphs).

Returns:int

the number of glyphs

Exceptions
IOException:
if there was an error reading the table.
getOriginalDataback to summary
public InputStream getOriginalData() throws IOException

Get the data of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).

Returns:InputStream

COSStream TrueType font program stream

Exceptions
IOException:
If there is an error getting the font data.
getOriginalDataSizeback to summary
public long getOriginalDataSize()

Get the data size of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).

Returns:long

the size.

getOS2Windowsback to summary
public OS2WindowsMetricsTable getOS2Windows() throws IOException

Get the OS/2 table for this TTF.

Returns:OS2WindowsMetricsTable

The OS/2 table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getPathback to summary
public GeneralPath getPath(String name) throws IOException

Implements org.apache.fontbox.FontBoxFont.getPath.

Doc from org.apache.fontbox.FontBoxFont.getPath.

Returns the path for the character with the given name.

Parameters
name:String

PostScript glyph name

Returns:GeneralPath

glyph path

Annotations
@Override
Exceptions
IOException:
if the path could not be read
getPostScriptback to summary
public PostScriptTable getPostScript() throws IOException

Get the postscript table for this TTF.

Returns:PostScriptTable

The postscript table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getTableback to summary
protected TTFTable getTable(String tag) throws IOException

This will get the table for the given tag.

Parameters
tag:String

the name of the table to be returned

Returns:TTFTable

The table with the given tag.

Exceptions
IOException:
if there was an error reading the table.
getTableBytesback to summary
public byte[] getTableBytes(TTFTable table) throws IOException

Returns the raw bytes of the given table.

Parameters
table:TTFTable

the table to read.

Returns:byte[]

the raw bytes of the given table

Exceptions
IOException:
if there was an error accessing the table.
getTableMapback to summary
public Map<String, TTFTable> getTableMap()

Get all of the tables.

Returns:Map<String, TTFTable>

All of the tables.

getTableNBytesback to summary
public byte[] getTableNBytes(TTFTable table, int limit) throws IOException

Returns the raw bytes of the given table, no more than limit bytes.

Parameters
table:TTFTable

the table to read.

limit:int

maximum length of array to return

Returns:byte[]

the raw bytes of the given table

Exceptions
IOException:
if there was an error accessing the table.
getTablesback to summary
public Collection<TTFTable> getTables()

Get all of the tables.

Returns:Collection<TTFTable>

All of the tables.

getUnicodeCmapImplback to summary
private CmapSubtable getUnicodeCmapImpl(boolean isStrict) throws IOException
getUnicodeCmapLookupback to summary
public CmapLookup getUnicodeCmapLookup() throws IOException

Returns the best Unicode from the font (the most general). The PDF spec says that "The means by which this is accomplished are implementation-dependent." The returned cmap will perform glyph substitution.

Returns:CmapLookup

cmap to perform glyph substitution

Exceptions
IOException:
if the font could not be read
getUnicodeCmapLookupback to summary
public CmapLookup getUnicodeCmapLookup(boolean isStrict) throws IOException

Returns the best Unicode from the font (the most general). The PDF spec says that "The means by which this is accomplished are implementation-dependent." The returned cmap will perform glyph substitution.

Parameters
isStrict:boolean

False if we allow falling back to any cmap, even if it's not Unicode.

Returns:CmapLookup

cmap to perform glyph substitution

Exceptions
IOException:
if the font could not be read, or there is no Unicode cmap
getUnitsPerEmback to summary
public int getUnitsPerEm() throws IOException

Returns the units per EM (Header.unitsPerEm).

Returns:int

units per EM

Exceptions
IOException:
if there was an error reading the table.
getVersionback to summary
public float getVersion()
Returns:float

Returns the version.

getVerticalHeaderback to summary
public VerticalHeaderTable getVerticalHeader() throws IOException

Get the vhea table for this TTF.

Returns:VerticalHeaderTable

The vhea table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getVerticalMetricsback to summary
public VerticalMetricsTable getVerticalMetrics() throws IOException

Get the vmtx table for this TTF.

Returns:VerticalMetricsTable

The vmtx table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getVerticalOriginback to summary
public VerticalOriginTable getVerticalOrigin() throws IOException

Get the VORG table for this TTF.

Returns:VerticalOriginTable

The VORG table or null if it doesn't exist.

Exceptions
IOException:
if there was an error reading the table.
getWidthback to summary
public float getWidth(String name) throws IOException

Implements org.apache.fontbox.FontBoxFont.getWidth.

Doc from org.apache.fontbox.FontBoxFont.getWidth.

Returns the advance width for the character with the given name.

Parameters
name:String

PostScript glyph name

Returns:float

glyph advance width

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

Implements org.apache.fontbox.FontBoxFont.hasGlyph.

Doc from org.apache.fontbox.FontBoxFont.hasGlyph.

Returns true if the font contains the given glyph.

Parameters
name:String

PostScript glyph name

Returns:boolean

true if the font contains a glyph with the given name, otherwise false

Annotations
@Override
Exceptions
IOException:
if something went wrong when accessing the font data
isEnableGsubback to summary
public boolean isEnableGsub()
Returns:boolean

Returns true if the GSUB table can be used for this font

nameToGIDback to summary
public int nameToGID(String name) throws IOException

Returns the GID for the given PostScript name, if the "post" table is present.

Parameters
name:String

the PostScript name.

Returns:int

the GID for the given PostScript name

Exceptions
IOException:
if the font data could not be read
parseUniNameback to summary
private int parseUniName(String name)

Parses a Unicode PostScript name in the format uniXXXX.

readPostScriptNamesback to summary
private void readPostScriptNames() throws IOException
readTableback to summary
pack-priv void readTable(TTFTable table) throws IOException

Read the given table if necessary. Package-private, used by TTFParser only.

Parameters
table:TTFTable

the table to be initialized

Exceptions
IOException:
if there was an error reading the table.
readTableHeadersback to summary
pack-priv void readTableHeaders(String tag, FontHeaders outHeaders) throws IOException

Read the given table headers. Package-private, used by TTFParser only.

Parameters
tag:String

the name of the table to be read

outHeaders:FontHeaders

consumes headers

Exceptions
IOException:
if there was an error reading the table.
setEnableGsubback to summary
public void setEnableGsub(boolean enableGsub)

Enable or disable the GSUB table for this font. GSUB table is enabled by default.

setVersionback to summary
pack-priv void setVersion(float versionValue)

Set the version. Package-private, used by TTFParser only.

Parameters
versionValue:float

The version to set.

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