Package org.apache.fontbox.ttf
Class TrueTypeFont
- java.lang.Object
-
- org.apache.fontbox.ttf.TrueTypeFont
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,FontBoxFont
- Direct Known Subclasses:
OpenTypeFont
public class TrueTypeFont extends java.lang.Object implements FontBoxFont, java.io.Closeable
A TrueType font file.
-
-
Field Summary
Fields Modifier and Type Field Description private TTFDataStreamdataprivate java.util.List<java.lang.String>enabledGsubFeaturesprivate booleanenableGsubprivate java.lang.ObjectlockPSNamesprivate java.lang.ObjectlockReadtableprivate static org.apache.commons.logging.LogLOGprivate intnumberOfGlyphsprivate java.util.Map<java.lang.String,java.lang.Integer>postScriptNamesprotected java.util.Map<java.lang.String,TTFTable>tablesprivate intunitsPerEmprivate floatversion
-
Constructor Summary
Constructors Constructor Description TrueTypeFont(TTFDataStream fontData)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddTable(TTFTable table)Add a table definition.voidclose()voiddisableGsubFeature(java.lang.String featureTag)Disable a particular glyph substitution feature.voidenableGsubFeature(java.lang.String featureTag)Enable a particular glyph substitution feature.voidenableVerticalSubstitutions()Enable glyph substitutions for vertical writing.intgetAdvanceHeight(int gid)Returns the height for the given GID.intgetAdvanceWidth(int gid)Returns the width for the given GID.CmapTablegetCmap()Get the "cmap" table for this TTF.BoundingBoxgetFontBBox()Returns the font's bounding box in PostScript units.java.util.List<java.lang.Number>getFontMatrix()Returns the FontMatrix in PostScript units.GlyphTablegetGlyph()Get the glyf table for this TTF.GlyphSubstitutionTablegetGsub()Get the "gsub" table for this TTF.GsubDatagetGsubData()Returns the GSubData of the GlyphSubstitutionTable if present.HeaderTablegetHeader()Get the head table for this TTF.HorizontalHeaderTablegetHorizontalHeader()Get the hhea table for this TTF.HorizontalMetricsTablegetHorizontalMetrics()Get the hmtx table for this TTF.IndexToLocationTablegetIndexToLocation()Get the loca table for this TTF.KerningTablegetKerning()Get the "kern" table for this TTF.MaximumProfileTablegetMaximumProfile()Get the maxp table for this TTF.java.lang.StringgetName()The PostScript name of the font.NamingTablegetNaming()This will get the naming table for the true type font.intgetNumberOfGlyphs()Returns the number of glyphs (MaximumProfile.numGlyphs).java.io.InputStreamgetOriginalData()Get the data of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).longgetOriginalDataSize()Get the data size of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).OS2WindowsMetricsTablegetOS2Windows()Get the OS/2 table for this TTF.java.awt.geom.GeneralPathgetPath(java.lang.String name)Returns the path for the character with the given name.PostScriptTablegetPostScript()Get the postscript table for this TTF.protected TTFTablegetTable(java.lang.String tag)This will get the table for the given tag.byte[]getTableBytes(TTFTable table)Returns the raw bytes of the given table.java.util.Map<java.lang.String,TTFTable>getTableMap()Get all of the tables.byte[]getTableNBytes(TTFTable table, int limit)Returns the raw bytes of the given table, no more thanlimitbytes.java.util.Collection<TTFTable>getTables()Get all of the tables.private CmapSubtablegetUnicodeCmapImpl(boolean isStrict)CmapLookupgetUnicodeCmapLookup()Returns the best Unicode from the font (the most general).CmapLookupgetUnicodeCmapLookup(boolean isStrict)Returns the best Unicode from the font (the most general).intgetUnitsPerEm()Returns the units per EM (Header.unitsPerEm).floatgetVersion()VerticalHeaderTablegetVerticalHeader()Get the vhea table for this TTF.VerticalMetricsTablegetVerticalMetrics()Get the vmtx table for this TTF.VerticalOriginTablegetVerticalOrigin()Get the VORG table for this TTF.floatgetWidth(java.lang.String name)Returns the advance width for the character with the given name.booleanhasGlyph(java.lang.String name)Returns true if the font contains the given glyph.booleanisEnableGsub()intnameToGID(java.lang.String name)Returns the GID for the given PostScript name, if the "post" table is present.private intparseUniName(java.lang.String name)Parses a Unicode PostScript name in the format uniXXXX.private voidreadPostScriptNames()(package private) voidreadTable(TTFTable table)Read the given table if necessary.(package private) voidreadTableHeaders(java.lang.String tag, FontHeaders outHeaders)Read the given table headers.voidsetEnableGsub(boolean enableGsub)Enable or disable the GSUB table for this font.(package private) voidsetVersion(float versionValue)Set the version.java.lang.StringtoString()
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
version
private float version
-
numberOfGlyphs
private int numberOfGlyphs
-
unitsPerEm
private int unitsPerEm
-
enableGsub
private boolean enableGsub
-
tables
protected final java.util.Map<java.lang.String,TTFTable> tables
-
data
private final TTFDataStream data
-
postScriptNames
private volatile java.util.Map<java.lang.String,java.lang.Integer> postScriptNames
-
lockReadtable
private final java.lang.Object lockReadtable
-
lockPSNames
private final java.lang.Object lockPSNames
-
enabledGsubFeatures
private final java.util.List<java.lang.String> enabledGsubFeatures
-
-
Constructor Detail
-
TrueTypeFont
TrueTypeFont(TTFDataStream fontData)
Constructor. Clients should use the TTFParser to create a new TrueTypeFont object.- Parameters:
fontData- The font data.
-
-
Method Detail
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getVersion
public float getVersion()
- Returns:
- Returns the version.
-
setVersion
void setVersion(float versionValue)
Set the version. Package-private, used by TTFParser only.- Parameters:
versionValue- The version to set.
-
isEnableGsub
public boolean isEnableGsub()
- Returns:
- Returns true if the GSUB table can be used for this font
-
setEnableGsub
public void setEnableGsub(boolean enableGsub)
Enable or disable the GSUB table for this font. GSUB table is enabled by default.
-
addTable
void addTable(TTFTable table)
Add a table definition. Package-private, used by TTFParser only.- Parameters:
table- The table to add.
-
getTables
public java.util.Collection<TTFTable> getTables()
Get all of the tables.- Returns:
- All of the tables.
-
getTableMap
public java.util.Map<java.lang.String,TTFTable> getTableMap()
Get all of the tables.- Returns:
- All of the tables.
-
getTableBytes
public byte[] getTableBytes(TTFTable table) throws java.io.IOException
Returns the raw bytes of the given table.- Parameters:
table- the table to read.- Returns:
- the raw bytes of the given table
- Throws:
java.io.IOException- if there was an error accessing the table.
-
getTable
protected TTFTable getTable(java.lang.String tag) throws java.io.IOException
This will get the table for the given tag.- Parameters:
tag- the name of the table to be returned- Returns:
- The table with the given tag.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getTableNBytes
public byte[] getTableNBytes(TTFTable table, int limit) throws java.io.IOException
Returns the raw bytes of the given table, no more thanlimitbytes.- Parameters:
table- the table to read.limit- maximum length of array to return- Returns:
- the raw bytes of the given table
- Throws:
java.io.IOException- if there was an error accessing the table.
-
getNaming
public NamingTable getNaming() throws java.io.IOException
This will get the naming table for the true type font.- Returns:
- The naming table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getPostScript
public PostScriptTable getPostScript() throws java.io.IOException
Get the postscript table for this TTF.- Returns:
- The postscript table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getOS2Windows
public OS2WindowsMetricsTable getOS2Windows() throws java.io.IOException
Get the OS/2 table for this TTF.- Returns:
- The OS/2 table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getMaximumProfile
public MaximumProfileTable getMaximumProfile() throws java.io.IOException
Get the maxp table for this TTF.- Returns:
- The maxp table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getHeader
public HeaderTable getHeader() throws java.io.IOException
Get the head table for this TTF.- Returns:
- The head table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getHorizontalHeader
public HorizontalHeaderTable getHorizontalHeader() throws java.io.IOException
Get the hhea table for this TTF.- Returns:
- The hhea table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getHorizontalMetrics
public HorizontalMetricsTable getHorizontalMetrics() throws java.io.IOException
Get the hmtx table for this TTF.- Returns:
- The hmtx table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getIndexToLocation
public IndexToLocationTable getIndexToLocation() throws java.io.IOException
Get the loca table for this TTF.- Returns:
- The loca table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getGlyph
public GlyphTable getGlyph() throws java.io.IOException
Get the glyf table for this TTF.- Returns:
- The glyf table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getCmap
public CmapTable getCmap() throws java.io.IOException
Get the "cmap" table for this TTF.- Returns:
- The "cmap" table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getVerticalHeader
public VerticalHeaderTable getVerticalHeader() throws java.io.IOException
Get the vhea table for this TTF.- Returns:
- The vhea table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getVerticalMetrics
public VerticalMetricsTable getVerticalMetrics() throws java.io.IOException
Get the vmtx table for this TTF.- Returns:
- The vmtx table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getVerticalOrigin
public VerticalOriginTable getVerticalOrigin() throws java.io.IOException
Get the VORG table for this TTF.- Returns:
- The VORG table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getKerning
public KerningTable getKerning() throws java.io.IOException
Get the "kern" table for this TTF.- Returns:
- The "kern" table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getGsub
public GlyphSubstitutionTable getGsub() throws java.io.IOException
Get the "gsub" table for this TTF.- Returns:
- The "gsub" table or null if it doesn't exist.
- Throws:
java.io.IOException- if there was an error reading the table.
-
getOriginalData
public java.io.InputStream getOriginalData() throws java.io.IOExceptionGet the data of the TrueType Font program representing the stream used to build this object (normally from the TTFParser object).- Returns:
- COSStream TrueType font program stream
- Throws:
java.io.IOException- If there is an error getting the font data.
-
getOriginalDataSize
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:
- the size.
-
readTable
void readTable(TTFTable table) throws java.io.IOException
Read the given table if necessary. Package-private, used by TTFParser only.- Parameters:
table- the table to be initialized- Throws:
java.io.IOException- if there was an error reading the table.
-
readTableHeaders
void readTableHeaders(java.lang.String tag, FontHeaders outHeaders) throws java.io.IOExceptionRead the given table headers. Package-private, used by TTFParser only.- Parameters:
tag- the name of the table to be readoutHeaders- consumes headers- Throws:
java.io.IOException- if there was an error reading the table.
-
getNumberOfGlyphs
public int getNumberOfGlyphs() throws java.io.IOExceptionReturns the number of glyphs (MaximumProfile.numGlyphs).- Returns:
- the number of glyphs
- Throws:
java.io.IOException- if there was an error reading the table.
-
getUnitsPerEm
public int getUnitsPerEm() throws java.io.IOExceptionReturns the units per EM (Header.unitsPerEm).- Returns:
- units per EM
- Throws:
java.io.IOException- if there was an error reading the table.
-
getAdvanceWidth
public int getAdvanceWidth(int gid) throws java.io.IOExceptionReturns the width for the given GID.- Parameters:
gid- the GID- Returns:
- the width
- Throws:
java.io.IOException- if there was an error reading the metrics table.
-
getAdvanceHeight
public int getAdvanceHeight(int gid) throws java.io.IOExceptionReturns the height for the given GID.- Parameters:
gid- the GID- Returns:
- the height
- Throws:
java.io.IOException- if there was an error reading the metrics table.
-
getName
public java.lang.String getName() throws java.io.IOExceptionDescription copied from interface:FontBoxFontThe PostScript name of the font.- Specified by:
getNamein interfaceFontBoxFont- Returns:
- the postscript of the font or null
- Throws:
java.io.IOException- if something went wrong when accessing the font data
-
readPostScriptNames
private void readPostScriptNames() throws java.io.IOException- Throws:
java.io.IOException
-
getUnicodeCmapLookup
public CmapLookup getUnicodeCmapLookup() throws java.io.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:
- cmap to perform glyph substitution
- Throws:
java.io.IOException- if the font could not be read
-
getUnicodeCmapLookup
public CmapLookup getUnicodeCmapLookup(boolean isStrict) throws java.io.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- False if we allow falling back to any cmap, even if it's not Unicode.- Returns:
- cmap to perform glyph substitution
- Throws:
java.io.IOException- if the font could not be read, or there is no Unicode cmap
-
getUnicodeCmapImpl
private CmapSubtable getUnicodeCmapImpl(boolean isStrict) throws java.io.IOException
- Throws:
java.io.IOException
-
nameToGID
public int nameToGID(java.lang.String name) throws java.io.IOExceptionReturns the GID for the given PostScript name, if the "post" table is present.- Parameters:
name- the PostScript name.- Returns:
- the GID for the given PostScript name
- Throws:
java.io.IOException- if the font data could not be read
-
getGsubData
public GsubData getGsubData() throws java.io.IOException
Returns the GSubData of the GlyphSubstitutionTable if present.- Returns:
- the GSubData of the GlyphSubstitutionTable or
GsubData.NO_DATA_FOUNDif no GSUB data is available, its scripts are not supported or it was disabled for that font - Throws:
java.io.IOException- if the font data could not be read
-
parseUniName
private int parseUniName(java.lang.String name)
Parses a Unicode PostScript name in the format uniXXXX.
-
getPath
public java.awt.geom.GeneralPath getPath(java.lang.String name) throws java.io.IOExceptionDescription copied from interface:FontBoxFontReturns the path for the character with the given name.- Specified by:
getPathin interfaceFontBoxFont- Parameters:
name- PostScript glyph name- Returns:
- glyph path
- Throws:
java.io.IOException- if the path could not be read
-
getWidth
public float getWidth(java.lang.String name) throws java.io.IOExceptionDescription copied from interface:FontBoxFontReturns the advance width for the character with the given name.- Specified by:
getWidthin interfaceFontBoxFont- Parameters:
name- PostScript glyph name- Returns:
- glyph advance width
- Throws:
java.io.IOException- if the path could not be read
-
hasGlyph
public boolean hasGlyph(java.lang.String name) throws java.io.IOExceptionDescription copied from interface:FontBoxFontReturns true if the font contains the given glyph.- Specified by:
hasGlyphin interfaceFontBoxFont- Parameters:
name- PostScript glyph name- Returns:
- true if the font contains a glyph with the given name, otherwise false
- Throws:
java.io.IOException- if something went wrong when accessing the font data
-
getFontBBox
public BoundingBox getFontBBox() throws java.io.IOException
Description copied from interface:FontBoxFontReturns the font's bounding box in PostScript units.- Specified by:
getFontBBoxin interfaceFontBoxFont- Returns:
- the bounding box of the font
- Throws:
java.io.IOException- if something went wrong when accessing the font data
-
getFontMatrix
public java.util.List<java.lang.Number> getFontMatrix() throws java.io.IOExceptionDescription copied from interface:FontBoxFontReturns the FontMatrix in PostScript units.- Specified by:
getFontMatrixin interfaceFontBoxFont- Returns:
- the font matrix
- Throws:
java.io.IOException- if something went wrong when accessing the font data
-
enableGsubFeature
public void enableGsubFeature(java.lang.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- The GSUB feature to enable
-
disableGsubFeature
public void disableGsubFeature(java.lang.String featureTag)
Disable a particular glyph substitution feature.- Parameters:
featureTag- The GSUB feature to disable
-
enableVerticalSubstitutions
public void enableVerticalSubstitutions()
Enable glyph substitutions for vertical writing.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-