Class TrueTypeFont
java.lang.Object
com.gitlab.pdftk_java.com.lowagie.text.pdf.BaseFont
com.gitlab.pdftk_java.com.lowagie.text.pdf.TrueTypeFont
- Direct Known Subclasses:
EnumerateTTC
,TrueTypeFontUnicode
Reads a Truetype font
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
The components of table 'head'.protected static class
The components of table 'hhea'.protected static class
The components of table 'OS/2'.Nested classes/interfaces inherited from class com.gitlab.pdftk_java.com.lowagie.text.pdf.BaseFont
BaseFont.StreamFont
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[][]
protected boolean
protected int
protected int
protected HashMap
The map containing the code information for the table 'cmap', encoding 1.0.protected HashMap
The map containing the code information for the table 'cmap', encoding 3.1 in Unicode.(package private) static final String[]
The code pages possible for a True Type font.protected int
The offset from the start of the file to the table directory.protected String[][]
The family name of the fontprotected String
The file name.protected String
The font name.protected String[][]
The full name of the fontprotected int[]
The width of the glyphs.protected TrueTypeFont.FontHeader
The content of table 'head'.protected TrueTypeFont.HorizontalHeader
The content of table 'hhea'.protected boolean
true
if all the glyphs have the same width.protected double
The italic angle.protected boolean
protected IntHashtable
The map containing the kerning information.protected TrueTypeFont.WindowsMetrics
The content of table 'OS/2'.protected RandomAccessFileOrArray
The file in use.protected String
The style modifierprotected HashMap
Contains the location of the several tables.protected String
The index for the TTC font.Fields inherited from class com.gitlab.pdftk_java.com.lowagie.text.pdf.BaseFont
ASCENT, AWT_ASCENT, AWT_DESCENT, AWT_LEADING, AWT_MAXADVANCE, BBOXLLX, BBOXLLY, BBOXURX, BBOXURY, BuiltinFonts14, CACHED, CAPHEIGHT, charBBoxes, CID_NEWLINE, COURIER, COURIER_BOLD, COURIER_BOLDOBLIQUE, COURIER_OBLIQUE, CP1250, CP1252, CP1257, DESCENT, differences, directTextToByte, embedded, EMBEDDED, encoding, fastWinansi, FONT_TYPE_CJK, FONT_TYPE_DOCUMENT, FONT_TYPE_T1, FONT_TYPE_T3, FONT_TYPE_TT, FONT_TYPE_TTUNI, fontCache, fontSpecific, fontType, forceWidthsOutput, HELVETICA, HELVETICA_BOLD, HELVETICA_BOLDOBLIQUE, HELVETICA_OBLIQUE, IDENTITY_H, IDENTITY_V, ITALICANGLE, MACROMAN, NOT_CACHED, NOT_EMBEDDED, notdef, RESOURCE_PATH, subset, SYMBOL, TIMES_BOLD, TIMES_BOLDITALIC, TIMES_ITALIC, TIMES_ROMAN, unicodeDifferences, widths, WINANSI, ZAPFDINGBATS
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
This constructor is present to allow extending the class.(package private)
TrueTypeFont
(String ttFile, String enc, boolean emb, byte[] ttfAfm) (package private)
TrueTypeFont
(String ttFile, String enc, boolean emb, byte[] ttfAfm, boolean justNames) Creates a new TrueType font. -
Method Summary
Modifier and TypeMethodDescription(package private) void
checkCff()
(package private) void
Reads the tables 'head', 'hhea', 'OS/2' and 'post' filling several variables.(package private) String
Gets the Postscript font name.String[]
Gets the code pages supported by the font.String[][]
Gets the family name of the font.protected PdfDictionary
getFontBaseType
(PdfIndirectReference fontDescriptor, String subsetPrefix, int firstChar, int lastChar, byte[] shortTag) Generates the font dictionary for this font.float
getFontDescriptor
(int key, float fontSize) Gets the font parameter identified bykey
.protected PdfDictionary
getFontDescriptor
(PdfIndirectReference fontStream, String subsetPrefix) Generates the font descriptor for this font.String[][]
Gets the full name of the font.protected int
getGlyphWidth
(int glyph) Gets a glyph width.int
getKerning
(char char1, char char2) Gets the kerning between two Unicode chars.int[]
getMetricsTT
(int c) Gets the glyph index and metrics for a character.(package private) String[][]
getNames
(int id) Extracts the names of the font in all the languages available.Gets the postscript font name.protected int[]
getRawCharBBox
(int c, String name) (package private) int
getRawWidth
(int c, String name) Gets the width from the font according to the unicode charc
.protected static String
getTTCName
(String name) Gets the name from a composed TTC file name.boolean
Checks if the font has any kerning pairs.(package private) void
process
(byte[] ttfAfm) Reads the font data.private void
readBbox()
(package private) void
Reads the several maps from the table 'cmap'.(package private) HashMap
The information in the maps of the table 'cmap' is coded in several formats.(package private) HashMap
The information in the maps of the table 'cmap' is coded in several formats.(package private) HashMap
The information in the maps of the table 'cmap' is coded in several formats.protected void
Reads the glyphs widths.(package private) void
Reads the kerning information from the 'kern' table.protected String
readStandardString
(int length) Reads aString
from the font file as bytes using the Cp1252 encoding.protected String
readUnicodeString
(int length) Reads a UnicodeString
from the font file.boolean
setKerning
(char char1, char char2, int kern) Sets the kerning between two Unicode chars.void
setPostscriptFontName
(String name) Sets the font name that will appear in the pdf font dictionary.(package private) void
writeFont
(PdfWriter writer, PdfIndirectReference ref, Object[] params) Outputs to the writer the font dictionaries and streams.Methods inherited from class com.gitlab.pdftk_java.com.lowagie.text.pdf.BaseFont
charExists, convertToBytes, correctArabicAdvance, createEncoding, createFont, createFont, createFont, createSubsetPrefix, enumerateTTCNames, enumerateTTCNames, getAllFontNames, getAscent, getAscentPoint, getBaseName, getCharBBox, getCidCode, getDescent, getDescentPoint, getDifferences, getDocumentFonts, getDocumentFonts, getEncoding, getFontType, getFullFontName, getResourceStream, getResourceStream, getUnicodeDifferences, getUnicodeDifferences, getUnicodeEquivalent, getWidth, getWidth, getWidthPoint, getWidthPoint, getWidthPointKerned, getWidths, isDirectTextToByte, isEmbedded, isFontSpecific, isForceWidthsOutput, isSubset, normalizeEncoding, setCharAdvance, setDirectTextToByte, setForceWidthsOutput, setSubset
-
Field Details
-
codePages
The code pages possible for a True Type font. -
justNames
protected boolean justNames -
tables
Contains the location of the several tables. The key is the name of the table and the value is anint[2]
where position 0 is the offset from the start of the file and position 1 is the length of the table. -
rf
The file in use. -
fileName
The file name. -
cff
protected boolean cff -
cffOffset
protected int cffOffset -
cffLength
protected int cffLength -
directoryOffset
protected int directoryOffsetThe 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. -
ttcIndex
The index for the TTC font. It is an emptyString
for a TTF file. -
style
The style modifier -
head
The content of table 'head'. -
hhea
The content of table 'hhea'. -
os_2
The content of table 'OS/2'. -
GlyphWidths
protected int[] GlyphWidthsThe width of the glyphs. This is essentially the content of table 'hmtx' normalized to 1000 units. -
bboxes
protected int[][] bboxes -
cmap10
The map containing the code information for the table 'cmap', encoding 1.0. The key is the code and the value is anint[2]
where position 0 is the glyph number and position 1 is the glyph width normalized to 1000 units. -
cmap31
The map containing the code information for the table 'cmap', encoding 3.1 in Unicode.The key is the code and the value is an
int
[2] where position 0 is the glyph number and position 1 is the glyph width normalized to 1000 units. -
kerning
The map containing the kerning information. It represents the content of table 'kern'. The key is anInteger
where 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. -
fontName
The font name. This name is usually extracted from the table 'name' with the 'Name ID' 6. -
fullName
The full name of the font -
familyName
The family name of the font -
italicAngle
protected double italicAngleThe italic angle. It is usually extracted from the 'post' table or in it's absence with the code:-Math.atan2(hhea.caretSlopeRun, hhea.caretSlopeRise) * 180 / Math.PI
-
isFixedPitch
protected boolean isFixedPitchtrue
if all the glyphs have the same width.
-
-
Constructor Details
-
TrueTypeFont
protected TrueTypeFont()This constructor is present to allow extending the class. -
TrueTypeFont
TrueTypeFont(String ttFile, String enc, boolean emb, byte[] ttfAfm) throws DocumentException, IOException - Throws:
DocumentException
IOException
-
TrueTypeFont
TrueTypeFont(String ttFile, String enc, boolean emb, byte[] ttfAfm, boolean justNames) throws DocumentException, IOException Creates a new TrueType font.- Parameters:
ttFile
- the location of the font on file. The file must end in '.ttf' or '.ttc' but can have modifiers after the nameenc
- the encoding to be applied to this fontemb
- true if the font is to be embedded in the PDFttfAfm
- the font as abyte
array- Throws:
DocumentException
- the font is invalidIOException
- the font file could not be read
-
-
Method Details
-
getTTCName
Gets the name from a composed TTC file name. If I have for input "myfont.ttc,2" the return will be "myfont.ttc".- Parameters:
name
- the full name- Returns:
- the simple file name
-
fillTables
Reads the tables 'head', 'hhea', 'OS/2' and 'post' filling several variables.- Throws:
DocumentException
- the font is invalidIOException
- the font file could not be read
-
getBaseFont
Gets the Postscript font name.- Returns:
- the Postscript font name
- Throws:
DocumentException
- the font is invalidIOException
- the font file could not be read
-
getNames
Extracts the names of the font in all the languages available.- Parameters:
id
- the name id to retrieve- Throws:
DocumentException
- on errorIOException
- on error
-
checkCff
- Throws:
DocumentException
IOException
-
process
Reads the font data.- Parameters:
ttfAfm
- the font as abyte
array, possiblynull
- Throws:
DocumentException
- the font is invalidIOException
- the font file could not be read
-
readStandardString
Reads aString
from the font file as bytes using the Cp1252 encoding.- Parameters:
length
- the length of bytes to read- Returns:
- the
String
read - Throws:
IOException
- the font file could not be read
-
readUnicodeString
Reads a UnicodeString
from the font file. Each character is represented by two bytes.- Parameters:
length
- the length of bytes to read. TheString
will havelength
/2 characters- Returns:
- the
String
read - Throws:
IOException
- the font file could not be read
-
readGlyphWidths
Reads the glyphs widths. The widths are extracted from the table 'hmtx'. The glyphs are normalized to 1000 units.- Throws:
DocumentException
- the font is invalidIOException
- the font file could not be read
-
getGlyphWidth
protected int getGlyphWidth(int glyph) Gets a glyph width.- Parameters:
glyph
- the glyph to get the width of- Returns:
- the width of the glyph in normalized 1000 units
-
readBbox
- Throws:
DocumentException
IOException
-
readCMaps
Reads the several maps from the table 'cmap'. The maps of interest are 1.0 for symbolic fonts and 3.1 for all others. A symbolic font is defined as having the map 3.0.- Throws:
DocumentException
- the font is invalidIOException
- the font file could not be read
-
readFormat0
The information in the maps of the table 'cmap' is coded in several formats. Format 0 is the Apple standard character to glyph index mapping table.- Returns:
- a
HashMap
representing this map - Throws:
IOException
- the font file could not be read
-
readFormat4
The information in the maps of the table 'cmap' is coded in several formats. Format 4 is the Microsoft standard character to glyph index mapping table.- Returns:
- a
HashMap
representing this map - Throws:
IOException
- the font file could not be read
-
readFormat6
The information in the maps of the table 'cmap' is coded in several formats. Format 6 is a trimmed table mapping. It is similar to format 0 but can have less than 256 entries.- Returns:
- a
HashMap
representing this map - Throws:
IOException
- the font file could not be read
-
readKerning
Reads the kerning information from the 'kern' table.- Throws:
IOException
- the font file could not be read
-
getKerning
public int getKerning(char char1, char char2) Gets the kerning between two Unicode chars.- Specified by:
getKerning
in classBaseFont
- Parameters:
char1
- the first charchar2
- the second char- Returns:
- the kerning to be applied
-
getRawWidth
Gets the width from the font according to the unicode charc
. If thename
is null it's a symbolic font.- Specified by:
getRawWidth
in classBaseFont
- Parameters:
c
- the unicode charname
- the glyph name- Returns:
- the width of the char
-
getFontDescriptor
protected PdfDictionary getFontDescriptor(PdfIndirectReference fontStream, String subsetPrefix) throws DocumentException Generates the font descriptor for this font.- Parameters:
subsetPrefix
- the subset prefixfontStream
- the indirect reference to a PdfStream containing the font ornull
- Returns:
- the PdfDictionary containing the font descriptor or
null
- Throws:
DocumentException
- if there is an error
-
getFontBaseType
protected PdfDictionary getFontBaseType(PdfIndirectReference fontDescriptor, String subsetPrefix, int firstChar, int lastChar, byte[] shortTag) throws DocumentException Generates the font dictionary for this font.- Parameters:
subsetPrefix
- the subset prefxfirstChar
- the first valid characterlastChar
- the last valid charactershortTag
- a 256 bytes longbyte
array where each unused byte is represented by 0fontDescriptor
- the indirect reference to a PdfDictionary containing the font descriptor ornull
- Returns:
- the PdfDictionary containing the font dictionary
- Throws:
DocumentException
- if there is an error
-
writeFont
void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) throws DocumentException, IOException Outputs to the writer the font dictionaries and streams.- Specified by:
writeFont
in classBaseFont
- Parameters:
writer
- the writer for this documentref
- the font indirect referenceparams
- several parameters that depend on the font type- Throws:
IOException
- on errorDocumentException
- error in generating the object
-
getFontDescriptor
public float getFontDescriptor(int key, float fontSize) Gets the font parameter identified bykey
. Valid values forkey
areASCENT
,CAPHEIGHT
,DESCENT
andITALICANGLE
.- Specified by:
getFontDescriptor
in classBaseFont
- Parameters:
key
- the parameter to be extractedfontSize
- the font size in points- Returns:
- the parameter in points
-
getMetricsTT
public int[] getMetricsTT(int c) Gets the glyph index and metrics for a character.- Parameters:
c
- the character- Returns:
- an
int
array with {glyph index, width}
-
getPostscriptFontName
Gets the postscript font name.- Specified by:
getPostscriptFontName
in classBaseFont
- Returns:
- the postscript font name
-
getCodePagesSupported
Gets the code pages supported by the font.- Overrides:
getCodePagesSupported
in classBaseFont
- Returns:
- the code pages supported by the font
-
getFullFontName
Gets the full name of the font. If it is a True Type font each array element will have {Platform ID, Platform Encoding ID, Language ID, font name}. The interpretation of this values can be found in the Open Type specification, chapter 2, in the 'name' table.
For the other fonts the array has a single element with {"", "", "", font name}.- Specified by:
getFullFontName
in classBaseFont
- Returns:
- the full name of the font
-
getFamilyFontName
Gets the family name of the font. If it is a True Type font each array element will have {Platform ID, Platform Encoding ID, Language ID, font name}. The interpretation of this values can be found in the Open Type specification, chapter 2, in the 'name' table.
For the other fonts the array has a single element with {"", "", "", font name}.- Specified by:
getFamilyFontName
in classBaseFont
- Returns:
- the family name of the font
-
hasKernPairs
public boolean hasKernPairs()Checks if the font has any kerning pairs.- Specified by:
hasKernPairs
in classBaseFont
- Returns:
true
if the font has any kerning pairs
-
setPostscriptFontName
Sets the font name that will appear in the pdf font dictionary. Use with care as it can easily make a font unreadable if not embedded.- Specified by:
setPostscriptFontName
in classBaseFont
- Parameters:
name
- the new font name
-
setKerning
public boolean setKerning(char char1, char char2, int kern) Sets the kerning between two Unicode chars.- Specified by:
setKerning
in classBaseFont
- Parameters:
char1
- the first charchar2
- the second charkern
- the kerning to apply in normalized 1000 units- Returns:
true
if the kerning was applied,false
otherwise
-
getRawCharBBox
- Specified by:
getRawCharBBox
in classBaseFont
-