com.lowagie.text.pdf

Class TrueTypeFont

class TrueTypeFont extends BaseFont

Reads a Truetype font

Author: Paulo Soares (psoares@consiste.pt)

Nested Class Summary
protected static classTrueTypeFont.FontHeader
The components of table 'head'.
protected static classTrueTypeFont.HorizontalHeader
The components of table 'hhea'.
protected static classTrueTypeFont.WindowsMetrics
The components of table 'OS/2'.
Field Summary
protected String[][]allNameEntries
All the names of the Names-Table
protected int[][]bboxes
protected booleancff
protected intcffLength
protected intcffOffset
protected HashMapcmap10
The map containing the code information for the table 'cmap', encoding 1.0.
protected HashMapcmap31
The map containing the code information for the table 'cmap', encoding 3.1 in Unicode.
protected HashMapcmapExt
static String[]codePages
The code pages possible for a True Type font.
protected intdirectoryOffset
The offset from the start of the file to the table directory.
protected String[][]familyName
The family name of the font
protected StringfileName
The file name.
protected StringfontName
The font name.
protected String[][]fullName
The full name of the font
protected int[]GlyphWidths
The width of the glyphs.
protected TrueTypeFont.FontHeaderhead
The content of table 'head'.
protected TrueTypeFont.HorizontalHeaderhhea
The content of table 'hhea'.
protected booleanisFixedPitch
true if all the glyphs have the same width.
protected doubleitalicAngle
The italic angle.
protected booleanjustNames
protected IntHashtablekerning
The map containing the kerning information.
protected TrueTypeFont.WindowsMetricsos_2
The content of table 'OS/2'.
protected RandomAccessFileOrArrayrf
The file in use.
protected Stringstyle
The style modifier
protected HashMaptables
Contains the location of the several tables.
protected StringttcIndex
The index for the TTC font.
protected intunderlinePosition
protected intunderlineThickness
Constructor Summary
protected TrueTypeFont()
This constructor is present to allow extending the class.
TrueTypeFont(String ttFile, String enc, boolean emb, byte[] ttfAfm, boolean justNames, boolean forceRead)
Creates a new TrueType font.
Method Summary
protected voidaddRangeUni(HashMap longTag, boolean includeMetrics, boolean subsetp)
voidcheckCff()
protected static int[]compactRanges(ArrayList ranges)
voidfillTables()
Reads the tables 'head', 'hhea', 'OS/2' and 'post' filling several variables.
String[][]getAllNameEntries()
Gets all the entries of the Names-Table.
String[][]getAllNames()
Extracts all the names of the names-Table
StringgetBaseFont()
Gets the Postscript font name.
String[]getCodePagesSupported()
Gets the code pages supported by the font.
String[][]getFamilyFontName()
Gets the family name of the font.
protected PdfDictionarygetFontBaseType(PdfIndirectReference fontDescriptor, String subsetPrefix, int firstChar, int lastChar, byte[] shortTag)
Generates the font dictionary for this font.
protected PdfDictionarygetFontDescriptor(PdfIndirectReference fontStream, String subsetPrefix, PdfIndirectReference cidset)
Generates the font descriptor for this font.
floatgetFontDescriptor(int key, float fontSize)
Gets the font parameter identified by key.
protected byte[]getFullFont()
String[][]getFullFontName()
Gets the full name of the font.
PdfStreamgetFullFontStream()
Returns a PdfStream object with the full font program.
protected intgetGlyphWidth(int glyph)
Gets a glyph width.
intgetKerning(int char1, int char2)
Gets the kerning between two Unicode chars.
int[]getMetricsTT(int c)
Gets the glyph index and metrics for a character.
String[][]getNames(int id)
Extracts the names of the font in all the languages available.
StringgetPostscriptFontName()
Gets the postscript font name.
protected int[]getRawCharBBox(int c, String name)
intgetRawWidth(int c, String name)
Gets the width from the font according to the unicode char c.
protected static StringgetTTCName(String name)
Gets the name from a composed TTC file name.
booleanhasKernPairs()
Checks if the font has any kerning pairs.
voidprocess(byte[] ttfAfm, boolean preload)
Reads the font data.
voidreadBbox()
protected byte[]readCffFont()
If this font file is using the Compact Font File Format, then this method will return the raw bytes needed for the font stream.
voidreadCMaps()
Reads the several maps from the table 'cmap'.
HashMapreadFormat0()
The information in the maps of the table 'cmap' is coded in several formats.
HashMapreadFormat12()
HashMapreadFormat4()
The information in the maps of the table 'cmap' is coded in several formats.
HashMapreadFormat6()
The information in the maps of the table 'cmap' is coded in several formats.
protected voidreadGlyphWidths()
Reads the glyphs widths.
voidreadKerning()
Reads the kerning information from the 'kern' table.
protected StringreadStandardString(int length)
Reads a String from the font file as bytes using the Cp1252 encoding.
protected StringreadUnicodeString(int length)
Reads a Unicode String from the font file.
booleansetKerning(int char1, int char2, int kern)
Sets the kerning between two Unicode chars.
voidsetPostscriptFontName(String name)
Sets the font name that will appear in the pdf font dictionary.
voidwriteFont(PdfWriter writer, PdfIndirectReference ref, Object[] params)
Outputs to the writer the font dictionaries and streams.

Field Detail

allNameEntries

protected String[][] allNameEntries
All the names of the Names-Table

bboxes

protected int[][] bboxes

cff

protected boolean cff

cffLength

protected int cffLength

cffOffset

protected int cffOffset

cmap10

protected HashMap cmap10
The map containing the code information for the table 'cmap', encoding 1.0. 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.

cmap31

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

cmapExt

protected HashMap cmapExt

codePages

static final String[] codePages
The code pages possible for a True Type font.

directoryOffset

protected int directoryOffset
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.

familyName

protected String[][] familyName
The family name of the font

fileName

protected String fileName
The file name.

fontName

protected String fontName
The font name. This name is usually extracted from the table 'name' with the 'Name ID' 6.

fullName

protected String[][] fullName
The full name of the font

GlyphWidths

protected int[] GlyphWidths
The width of the glyphs. This is essentially the content of table 'hmtx' normalized to 1000 units.

head

protected TrueTypeFont.FontHeader head
The content of table 'head'.

hhea

protected TrueTypeFont.HorizontalHeader hhea
The content of table 'hhea'.

isFixedPitch

protected boolean isFixedPitch
true if all the glyphs have the same width.

italicAngle

protected double italicAngle
The 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
 

justNames

protected boolean justNames

kerning

protected IntHashtable kerning
The map containing the kerning information. It represents the content of table 'kern'. The key is an Integer 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 an Integer. This value is usually negative.

os_2

protected TrueTypeFont.WindowsMetrics os_2
The content of table 'OS/2'.

rf

protected RandomAccessFileOrArray rf
The file in use.

style

protected String style
The style modifier

tables

protected HashMap tables
Contains the location of the several tables. The key is the name of the table and the value is an int[2] where position 0 is the offset from the start of the file and position 1 is the length of the table.

ttcIndex

protected String ttcIndex
The index for the TTC font. It is an empty String for a TTF file.

underlinePosition

protected int underlinePosition

underlineThickness

protected int underlineThickness

Constructor Detail

TrueTypeFont

protected TrueTypeFont()
This constructor is present to allow extending the class.

TrueTypeFont

TrueTypeFont(String ttFile, String enc, boolean emb, byte[] ttfAfm, boolean justNames, boolean forceRead)
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 name enc the encoding to be applied to this font emb true if the font is to be embedded in the PDF ttfAfm the font as a byte array

Throws: DocumentException the font is invalid IOException the font file could not be read

Since: 2.1.5

Method Detail

addRangeUni

protected void addRangeUni(HashMap longTag, boolean includeMetrics, boolean subsetp)

checkCff

void checkCff()

compactRanges

protected static int[] compactRanges(ArrayList ranges)

fillTables

void fillTables()
Reads the tables 'head', 'hhea', 'OS/2' and 'post' filling several variables.

Throws: DocumentException the font is invalid IOException the font file could not be read

getAllNameEntries

public String[][] getAllNameEntries()
Gets all the entries of the Names-Table. If it is a True Type font each array element will have {Name ID, 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}.

Returns: the full name of the font

getAllNames

String[][] getAllNames()
Extracts all the names of the names-Table

Throws: DocumentException on error IOException on error

getBaseFont

String getBaseFont()
Gets the Postscript font name.

Returns: the Postscript font name

Throws: DocumentException the font is invalid IOException the font file could not be read

getCodePagesSupported

public String[] getCodePagesSupported()
Gets the code pages supported by the font.

Returns: the code pages supported by the font

getFamilyFontName

public String[][] 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}.

Returns: the family name of the font

getFontBaseType

protected PdfDictionary getFontBaseType(PdfIndirectReference fontDescriptor, String subsetPrefix, int firstChar, int lastChar, byte[] shortTag)
Generates the font dictionary for this font.

Parameters: subsetPrefix the subset prefix firstChar the first valid character lastChar the last valid character shortTag a 256 bytes long byte array where each unused byte is represented by 0 fontDescriptor the indirect reference to a PdfDictionary containing the font descriptor or null

Returns: the PdfDictionary containing the font dictionary

getFontDescriptor

protected PdfDictionary getFontDescriptor(PdfIndirectReference fontStream, String subsetPrefix, PdfIndirectReference cidset)
Generates the font descriptor for this font.

Parameters: subsetPrefix the subset prefix fontStream the indirect reference to a PdfStream containing the font or null

Returns: the PdfDictionary containing the font descriptor or null

getFontDescriptor

public float getFontDescriptor(int key, float fontSize)
Gets the font parameter identified by key. Valid values for key are ASCENT, CAPHEIGHT, DESCENT and ITALICANGLE.

Parameters: key the parameter to be extracted fontSize the font size in points

Returns: the parameter in points

getFullFont

protected byte[] getFullFont()

getFullFontName

public String[][] 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}.

Returns: the full name of the font

getFullFontStream

public PdfStream getFullFontStream()
Returns a PdfStream object with the full font program.

Returns: a PdfStream with the font program

Since: 2.1.3

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

getKerning

public int getKerning(int char1, int char2)
Gets the kerning between two Unicode chars.

Parameters: char1 the first char char2 the second char

Returns: the kerning to be applied

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}

getNames

String[][] getNames(int id)
Extracts the names of the font in all the languages available.

Parameters: id the name id to retrieve

Throws: DocumentException on error IOException on error

getPostscriptFontName

public String getPostscriptFontName()
Gets the postscript font name.

Returns: the postscript font name

getRawCharBBox

protected int[] getRawCharBBox(int c, String name)

getRawWidth

int getRawWidth(int c, String name)
Gets the width from the font according to the unicode char c. If the name is null it's a symbolic font.

Parameters: c the unicode char name the glyph name

Returns: the width of the char

getTTCName

protected static String getTTCName(String name)
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

hasKernPairs

public boolean hasKernPairs()
Checks if the font has any kerning pairs.

Returns: true if the font has any kerning pairs

process

void process(byte[] ttfAfm, boolean preload)
Reads the font data.

Parameters: ttfAfm the font as a byte array, possibly null

Throws: DocumentException the font is invalid IOException the font file could not be read

Since: 2.1.5

readBbox

private void readBbox()

readCffFont

protected byte[] readCffFont()
If this font file is using the Compact Font File Format, then this method will return the raw bytes needed for the font stream. If this method is ever made public: make sure to add a test if (cff == true).

Returns: a byte array

Since: 2.1.3

readCMaps

void 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 invalid IOException the font file could not be read

readFormat0

HashMap 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

readFormat12

HashMap readFormat12()

readFormat4

HashMap 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

HashMap 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

readGlyphWidths

protected void 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 invalid IOException the font file could not be read

readKerning

void readKerning()
Reads the kerning information from the 'kern' table.

Throws: IOException the font file could not be read

readStandardString

protected String readStandardString(int length)
Reads a String 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

protected String readUnicodeString(int length)
Reads a Unicode String from the font file. Each character is represented by two bytes.

Parameters: length the length of bytes to read. The String will have length/2 characters

Returns: the String read

Throws: IOException the font file could not be read

setKerning

public boolean setKerning(int char1, int char2, int kern)
Sets the kerning between two Unicode chars.

Parameters: char1 the first char char2 the second char kern the kerning to apply in normalized 1000 units

Returns: true if the kerning was applied, false otherwise

setPostscriptFontName

public void setPostscriptFontName(String name)
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.

Parameters: name the new font name

writeFont

void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params)
Outputs to the writer the font dictionaries and streams.

Parameters: writer the writer for this document ref the font indirect reference params several parameters that depend on the font type

Throws: IOException on error DocumentException error in generating the object