Package com.itextpdf.io.font
Class TrueTypeFont
java.lang.Object
com.itextpdf.io.font.FontProgram
com.itextpdf.io.font.TrueTypeFont
- Direct Known Subclasses:
-
DocTrueTypeFont
Font program backed by an OpenType/TrueType font or a TrueType Collection member.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[][]Per-glyph bounding boxes indexed by glyph ID, in normalized glyph units.protected booleanIndicates that the selected CMap uses vertical writing metrics.protected com.itextpdf.io.util.IntHashtableThe map containing the kerning information.Fields inherited from class com.itextpdf.io.font.FontProgram
avgWidth, codeToGlyph, DEFAULT_WIDTH, encodingScheme, fontIdentification, fontMetrics, fontNames, HORIZONTAL_SCALING_FACTOR, isFontSpecific, registry, unicodeToGlyph, UNITS_NORMALIZATION -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates an uninitialized font.TrueTypeFont(byte[] ttf) Loads a standalone OpenType or TrueType font from its binary contents.TrueTypeFont(byte[] ttf, boolean isLenientMode) Loads a font from binary contents.TrueTypeFont(String path) Loads a standalone OpenType or TrueType font from a path. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying font parser and releases its source data.extractFeatures(Collection<String> otfScriptTags, Map<String, List<OpenTableLookup>> extractedFeatures) Extracts features from GSUB and GPOS tables based on the passed script tags.Gets the preferred character-to-glyph CMap selected from the font.String[]Gets the code pages supported by the font.intThe offset from the start of the file to the table directory.byte[]Gets the bytes of the font.Gets the GDEF table reader.Gets the GPOS table reader.Gets the GSUB table reader.intgetKerning(Glyph first, Glyph second) Gets the kerning between two glyphs.intGets the number of the “cmap” subtables for the currentTrueTypeFontprogram.intComputes the PDF font descriptor flags for this program.byte[]Gets subset based on the passed glyphs.booleanChecks whether this program supplies kerning pairs.booleanisBuiltWith(String fontProgram) Checks whether theFontProgramwas built with corresponding fontName.booleanisCff()Checks whether this OpenType font stores a CFF table.booleanisCmapPresent(int platformID, int encodingID) Checks whether currentTrueTypeFontprogram contains the “cmap” subtable with provided platform ID and encoding ID.mapGlyphsCidsToGids(Set<Integer> glyphs) Maps a set of glyph CIDs (as used in PDF file) to corresponding GID values (as a glyph primary identifier in the font file).static byte[]Deprecated.static byte[]Merges the passed font into one.protected voidReads the GDEF table.protected voidCreates the GPOS table reader.protected voidCreates the GSUB table reader when the font supplies a GSUB table.Gets subset and a number of glyphs in it based on the passed glyphs.voidupdateUsedGlyphs(Map<Integer, Glyph> usedGlyphs, boolean subset, List subsetRanges) The method will update usedGlyphs with additional range or with all glyphs if there is no subset.voidupdateUsedGlyphs(SortedSet<Integer> usedGlyphs, boolean subset, List subsetRanges) The method will update usedGlyphs with additional range or with all glyphs if there is no subset.Methods inherited from class com.itextpdf.io.font.FontProgram
convertGlyphSpaceToTextSpace, convertGlyphSpaceToTextSpace, convertGlyphSpaceToTextSpace, convertTextSpaceToGlyphSpace, countOfGlyphs, fixSpaceIssue, getAvgWidth, getCharBBox, getFontIdentification, getFontMetrics, getFontNames, getGlyph, getGlyphByCode, getKerning, getRegistry, getWidth, isFontSpecific, setBbox, setBold, setCapHeight, setFixedPitch, setFontFamily, setFontName, setFontStretch, setFontWeight, setItalicAngle, setRegistry, setStemH, setStemV, setTypoAscender, setTypoDescender, setXHeight, toString
-
Field Details
-
bBoxes
protected int[][] bBoxesPer-glyph bounding boxes indexed by glyph ID, in normalized glyph units. -
isVertical
protected boolean isVerticalIndicates that the selected CMap uses vertical writing metrics. -
kerning
protected com.itextpdf.io.util.IntHashtable kerningThe map containing the kerning information. It represents the content of table 'kern'. The key is anIntegerwhere the top 16 bits are the glyph number for the first character and the lower 16 bits are the glyph number for the second character. The value is the amount of kerning in normalized 1000 units as anInteger. This value is usually negative.
-
-
Constructor Details
-
TrueTypeFont
protected TrueTypeFont()Creates an uninitialized font. -
TrueTypeFont
Loads a standalone OpenType or TrueType font from a path.- Parameters:
-
path- path to the font file - Throws:
-
IOException- if the file cannot be read or parsed
-
TrueTypeFont
Loads a standalone OpenType or TrueType font from its binary contents.- Parameters:
-
ttf- font bytes - Throws:
-
IOException- if the bytes cannot be parsed
-
TrueTypeFont
Loads a font from binary contents.- Parameters:
-
ttf- font bytes -
isLenientMode-trueto enable lenient parser behavior, seeOpenTypeParser(byte[], boolean) - Throws:
-
IOException- if the bytes cannot be parsed
-
-
Method Details
-
hasKernPairs
public boolean hasKernPairs()Description copied from class:FontProgramChecks whether this program supplies kerning pairs.- Overrides:
-
hasKernPairsin classFontProgram - Returns:
-
trueif supplies,falseotherwise
-
getKerning
Gets the kerning between two glyphs.- Specified by:
-
getKerningin classFontProgram - Parameters:
-
first- the first glyph -
second- the second glyph - Returns:
- the kerning to be applied
-
isCff
public boolean isCff()Checks whether this OpenType font stores a CFF table.- Returns:
-
truefor CFF-flavored OpenType fonts
-
getActiveCmap
Gets the preferred character-to-glyph CMap selected from the font.- Returns:
- mapping from character code to CMap data
-
getFontStreamBytes
public byte[] getFontStreamBytes()Gets the bytes of the font.- Returns:
- CFF bytes for CFF fonts or complete font bytes otherwise
- Throws:
-
IOException- if the source font cannot be read
-
getPdfFontFlags
public int getPdfFontFlags()Description copied from class:FontProgramComputes the PDF font descriptor flags for this program.- Specified by:
-
getPdfFontFlagsin classFontProgram - Returns:
- bit set defined by the PDF font descriptor specification
-
getDirectoryOffset
public int getDirectoryOffset()The offset from the start of the file to the table directory. It is 0 for TTF and may vary for TTC depending on the chosen font.- Returns:
- directory Offset
-
getGsubTable
Gets the GSUB table reader.- Returns:
- substitution table reader
-
getGposTable
Gets the GPOS table reader.- Returns:
- positioning table reader
-
getGdefTable
Gets the GDEF table reader.- Returns:
- glyph definition table reader
-
getSubset
Gets subset based on the passed glyphs.- Parameters:
-
glyphs- the glyphs to subset the font -
subsetTables- whether subset tables (remove `name` and `post` tables) or not. It's used in case of ttc (true type collection) font where single "full" font is needed. Despite the value of that flag, only used glyphs will be left in the font - Returns:
- the subset font
-
subset
Gets subset and a number of glyphs in it based on the passed glyphs.The number of glyphs in a subset is not just glyphs.size() here. It's the biggest glyph id + 1 (for glyph 0). It also may include possible composite glyphs.
- Parameters:
-
glyphs- the glyphs to subset the font -
subsetTables- whether subset tables (remove `name` and `post` tables) or not. It's used in case of ttc (true type collection) font where single "full" font is needed. Despite the value of that flag, only used glyphs will be left in the font - Returns:
- the subset of the font and the number of glyphs in it
-
merge
Deprecated.in favour ofmerge(Map, String, boolean)Merges the passed font into one. Used glyphs per each font are applied to subset the merged font.- Parameters:
-
toMerge- the fonts to merge with used glyphs per each font -
fontName- the name of fonts to merge - Returns:
- the raw data of merged font
-
merge
public static byte[] merge(Map<TrueTypeFont, Set<Integer>> toMerge, String fontName, boolean isCmapCheckRequired) Merges the passed font into one. Used glyphs per each font are applied to subset the merged font.- Parameters:
-
toMerge- the fonts to merge with used glyphs per each font -
fontName- the name of fonts to merge -
isCmapCheckRequired- the flag which specifies whether 'cmap' table should be checked while merging or not - Returns:
- the raw data of merged font
-
mapGlyphsCidsToGids
Maps a set of glyph CIDs (as used in PDF file) to corresponding GID values (as a glyph primary identifier in the font file). This call is only meaningful for fonts that return true forisCff(). For other types of fonts, GID and CID are always the same, so that call would essentially return a set of the same values.- Parameters:
-
glyphs- a set of glyph CIDs - Returns:
- a set of glyph ids corresponding to the passed glyph CIDs
-
isCmapPresent
public boolean isCmapPresent(int platformID, int encodingID) Checks whether currentTrueTypeFontprogram contains the “cmap” subtable with provided platform ID and encoding ID.- Parameters:
-
platformID- platform ID -
encodingID- encoding ID - Returns:
-
trueif “cmap” subtable with provided platform ID and encoding ID is present in the font program,falseotherwise
-
getNumberOfCmaps
public int getNumberOfCmaps()Gets the number of the “cmap” subtables for the currentTrueTypeFontprogram.- Returns:
- the number of the “cmap” subtables
-
extractFeatures
public String extractFeatures(Collection<String> otfScriptTags, Map<String, List<OpenTableLookup>> extractedFeatures) Extracts features from GSUB and GPOS tables based on the passed script tags. The features are put in the passed extractedFeatures map- Parameters:
-
otfScriptTags- the script tags to extract the features for -
extractedFeatures- the features will be added to this map - Returns:
- the script tag which was used to extract features. It may be null if no features were extracted or default script tag was used.
-
readGdefTable
Reads the GDEF table.- Throws:
-
IOException- if the table cannot be read
-
readGsubTable
Creates the GSUB table reader when the font supplies a GSUB table.- Throws:
-
IOException- if the table cannot be read
-
readGposTable
Creates the GPOS table reader.- Throws:
-
IOException- if the table cannot be read
-
getCodePagesSupported
Gets the code pages supported by the font.- Returns:
- the code pages supported by the font
-
isBuiltWith
Description copied from class:FontProgramChecks whether theFontProgramwas built with corresponding fontName. Default value is false unless overridden.- Overrides:
-
isBuiltWithin classFontProgram - Parameters:
-
fontProgram- a font name or path to a font program - Returns:
- true, if the FontProgram was built with the fontProgram. Otherwise false.
-
close
Closes the underlying font parser and releases its source data.- Throws:
-
IOException- if closing the source fails
-
updateUsedGlyphs
The method will update usedGlyphs with additional range or with all glyphs if there is no subset. This set of used glyphs can be used for building width array and ToUnicode CMAP.- Parameters:
-
usedGlyphs- a set of integers, which are glyph ids that denote used glyphs. This set is updated inside the method if needed. -
subset- subset status -
subsetRanges- additional subset ranges
-
updateUsedGlyphs
The method will update usedGlyphs with additional range or with all glyphs if there is no subset. This map of used glyphs can be used for building width array and ToUnicode CMAP.- Parameters:
-
usedGlyphs- a map of glyph ids to glyphs. This map is updated inside the method if needed -
subset- subset status -
subsetRanges- additional subset ranges
-
merge(Map, String, boolean)