- java.lang.Object
-
- com.itextpdf.text.pdf.BaseFont
-
- Direct Known Subclasses:
DocumentFont,Type3Font
public abstract class BaseFont extends java.lang.ObjectBase class for the several font types supported- Author:
- Paulo Soares (psoares@consiste.pt)
-
-
Field Summary
Fields Modifier and Type Field Description static intASCENTThe maximum height above the baseline reached by glyphs in this font, excluding the height of glyphs for accented characters.static intAWT_ASCENTjava.awt.Font propertystatic intAWT_DESCENTjava.awt.Font propertystatic intAWT_LEADINGjava.awt.Font propertystatic intAWT_MAXADVANCEjava.awt.Font propertystatic intBBOXLLXThe lower left x glyph coordinate.static intBBOXLLYThe lower left y glyph coordinate.static intBBOXURXThe upper right x glyph coordinate.static intBBOXURYThe upper right y glyph coordinate.protected static java.util.HashMapBuiltinFonts14list of the 14 built in fonts.static booleanCACHEDif the font has to be cachedstatic intCAPHEIGHTThe y coordinate of the top of flat capital letters, measured from the baseline.static int[]CHAR_RANGE_ARABICstatic int[]CHAR_RANGE_CYRILLICstatic int[]CHAR_RANGE_HEBREWstatic int[]CHAR_RANGE_LATINprotected int[][]charBBoxesstatic charCID_NEWLINEThe fake CID code that represents a newline.protected intcompressionLevelThe compression level for the font stream.static java.lang.StringCOURIERThis is a possible value of a base 14 type 1 fontstatic java.lang.StringCOURIER_BOLDThis is a possible value of a base 14 type 1 fontstatic java.lang.StringCOURIER_BOLDOBLIQUEThis is a possible value of a base 14 type 1 fontstatic java.lang.StringCOURIER_OBLIQUEThis is a possible value of a base 14 type 1 fontstatic java.lang.StringCP1250A possible encoding.static java.lang.StringCP1252A possible encoding.static java.lang.StringCP1257A possible encoding.static intDESCENTThe maximum depth below the baseline reached by glyphs in this font.protected java.lang.String[]differencesencoding namesprotected booleandirectTextToByteConvertschardirectly tobyteby casting.protected booleanembeddedtrue if the font is to be embedded in the PDFstatic booleanEMBEDDEDif the font has to be embeddedprotected java.lang.Stringencodingencoding used with this fontprotected booleanfastWinansistatic intFONT_TYPE_CJKThe font is CJK.static intFONT_TYPE_DOCUMENTA font already inside the document.static intFONT_TYPE_T1The font is Type 1.static intFONT_TYPE_T3A Type3 font.static intFONT_TYPE_TTThe font is True Type with a standard encoding.static intFONT_TYPE_TTUNIThe font is True Type with a Unicode encoding.protected static java.util.HashMapfontCachecache for the fonts already used.protected booleanfontSpecifictrue if the font must use its built in encoding.protected booleanforceWidthsOutputForces the output of the width array.static java.lang.StringHELVETICAThis is a possible value of a base 14 type 1 fontstatic java.lang.StringHELVETICA_BOLDThis is a possible value of a base 14 type 1 fontstatic java.lang.StringHELVETICA_BOLDOBLIQUEThis is a possible value of a base 14 type 1 fontstatic java.lang.StringHELVETICA_OBLIQUEThis is a possible value of a base 14 type 1 fontstatic java.lang.StringIDENTITY_HThe Unicode encoding with horizontal writing.static java.lang.StringIDENTITY_VThe Unicode encoding with vertical writing.static intITALICANGLEThe angle, expressed in degrees counterclockwise from the vertical, of the dominant vertical strokes of the font.static java.lang.StringMACROMANA possible encoding.static booleanNOT_CACHEDif the font doesn't have to be cachedstatic booleanNOT_EMBEDDEDif the font doesn't have to be embeddedstatic java.lang.Stringnotdefa not defined character in a custom PDF encodingstatic java.lang.StringRESOURCE_PATHThe path to the font resources.protected IntHashtablespecialMapCustom encodings use this map to key the Unicode character to the single byte code.static intSTRIKETHROUGH_POSITIONThe strikethrough position.static intSTRIKETHROUGH_THICKNESSThe strikethrough thickness.static intSUBSCRIPT_OFFSETThe recommended vertical offset from the baseline for subscripts for this font.static intSUBSCRIPT_SIZEThe recommended vertical size for subscripts for this font.protected booleansubsetIndicates if all the glyphs and widths for that particular encoding should be included in the document.protected java.util.ArrayListsubsetRangesstatic intSUPERSCRIPT_OFFSETThe recommended vertical offset from the baseline for superscripts for this font.static intSUPERSCRIPT_SIZEThe recommended vertical size for superscripts for this font.static java.lang.StringSYMBOLThis is a possible value of a base 14 type 1 fontstatic java.lang.StringTIMES_BOLDThis is a possible value of a base 14 type 1 fontstatic java.lang.StringTIMES_BOLDITALICThis is a possible value of a base 14 type 1 fontstatic java.lang.StringTIMES_ITALICThis is a possible value of a base 14 type 1 fontstatic java.lang.StringTIMES_ROMANThis is a possible value of a base 14 type 1 fontstatic intUNDERLINE_POSITIONThe underline position.static intUNDERLINE_THICKNESSThe underline thickness.protected char[]unicodeDifferencessame as differences but with the unicode codesprotected int[]widthstable of characters widths for this encodingstatic java.lang.StringWINANSIA possible encoding.static java.lang.StringZAPFDINGBATSThis is a possible value of a base 14 type 1 font
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseFont()Creates new BaseFont
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddSubsetRange(int[] range)Adds a character range when subsetting.booleancharExists(int c)Checks if a character exists in this font.voidcorrectArabicAdvance()iText expects Arabic Diactrics (tashkeel) to have zero advance but some fonts, most notably those that come with Windows, like times.ttf, have non-zero advance for those characters.protected voidcreateEncoding()Creates thewidthsand thedifferencesarraysstatic BaseFontcreateFont()Creates a new font.static BaseFontcreateFont(PRIndirectReference fontRef)Creates a font based on an existing document font.static BaseFontcreateFont(java.lang.String name, java.lang.String encoding, boolean embedded)Creates a new font.static BaseFontcreateFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean forceRead)Creates a new font.static BaseFontcreateFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb)Creates a new font.static BaseFontcreateFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow)Creates a new font.static BaseFontcreateFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow, boolean forceRead)Creates a new font.static java.lang.StringcreateSubsetPrefix()Creates a unique subset prefix to be added to the font name when the font is embedded and subset.static java.lang.String[]enumerateTTCNames(byte[] ttcArray)Enumerates the postscript font names present inside a True Type Collection.static java.lang.String[]enumerateTTCNames(java.lang.String ttcFile)Enumerates the postscript font names present inside a True Type Collection.static java.lang.Object[]getAllFontNames(java.lang.String name, java.lang.String encoding, byte[] ttfAfm)Gets all the names from the font.abstract java.lang.String[][]getAllNameEntries()Gets all the entries of the names-table.static java.lang.String[][]getAllNameEntries(java.lang.String name, java.lang.String encoding, byte[] ttfAfm)Gets all the entries of the namestable from the font.intgetAscent(java.lang.String text)Gets the ascent of aStringin normalized 1000 units.floatgetAscentPoint(java.lang.String text, float fontSize)Gets the ascent of aStringin points.protected static java.lang.StringgetBaseName(java.lang.String name)Gets the name without the modifiers Bold, Italic or BoldItalic.int[]getCharBBox(int c)Gets the smallest box enclosing the character contours.intgetCidCode(int c)Gets the CID code given an Unicode.java.lang.String[]getCodePagesSupported()Gets the code pages supported by the font.intgetCompressionLevel()Returns the compression level used for the font streams.intgetDescent(java.lang.String text)Gets the descent of aStringin normalized 1000 units.floatgetDescentPoint(java.lang.String text, float fontSize)Gets the descent of aStringin points.java.lang.String[]getDifferences()Gets the array with the names of the characters.static java.util.ArrayListgetDocumentFonts(PdfReader reader)Gets a list of all document fonts.static java.util.ArrayListgetDocumentFonts(PdfReader reader, int page)Gets a list of the document fonts in a particular page.java.lang.StringgetEncoding()Gets the encoding used to convertStringintobyte[].abstract java.lang.String[][]getFamilyFontName()Gets the family name of the font.abstract floatgetFontDescriptor(int key, float fontSize)Gets the font parameter identified bykey.intgetFontType()Gets the font type.abstract java.lang.String[][]getFullFontName()Gets the full name of the font.static java.lang.String[][]getFullFontName(java.lang.String name, java.lang.String encoding, byte[] ttfAfm)Gets the full name of the font.abstract intgetKerning(int char1, int char2)Gets the kerning between two Unicode chars.abstract java.lang.StringgetPostscriptFontName()Gets the postscript font name.protected abstract int[]getRawCharBBox(int c, java.lang.String name)static java.io.InputStreamgetResourceStream(java.lang.String key)Gets the font resources.static java.io.InputStreamgetResourceStream(java.lang.String key, java.lang.ClassLoader loader)Gets the font resources.char[]getUnicodeDifferences()Gets the array with the unicode characters.intgetUnicodeEquivalent(int c)Gets the Unicode equivalent to a CID.intgetWidth(int char1)Gets the width of acharin normalized 1000 units.intgetWidth(java.lang.String text)Gets the width of aStringin normalized 1000 units.floatgetWidthPoint(int char1, float fontSize)Gets the width of acharin points.floatgetWidthPoint(java.lang.String text, float fontSize)Gets the width of aStringin points.floatgetWidthPointKerned(java.lang.String text, float fontSize)Gets the width of aStringin points taking kerning into account.int[]getWidths()Gets the font width array.abstract booleanhasKernPairs()Checks if the font has any kerning pairs.booleanisDirectTextToByte()Gets the direct conversion ofchartobyte.booleanisEmbedded()Gets the embedded flag.booleanisFontSpecific()Gets the symbolic flag of the font.booleanisForceWidthsOutput()Gets the state of the property.booleanisSubset()Indicates if all the glyphs and widths for that particular encoding should be included in the document.protected static java.lang.StringnormalizeEncoding(java.lang.String enc)Normalize the encoding names.booleansetCharAdvance(int c, int advance)Sets the character advance.voidsetCompressionLevel(int compressionLevel)Sets the compression level to be used for the font streams.voidsetDirectTextToByte(boolean directTextToByte)Sets the conversion ofchardirectly tobyteby casting.voidsetForceWidthsOutput(boolean forceWidthsOutput)Set totrueto force the generation of the widths array.abstract booleansetKerning(int char1, int char2, int kern)Sets the kerning between two Unicode chars.abstract voidsetPostscriptFontName(java.lang.String name)Sets the font name that will appear in the pdf font dictionary.voidsetSubset(boolean subset)Indicates if all the glyphs and widths for that particular encoding should be included in the document.
-
-
-
Field Detail
-
COURIER
public static final java.lang.String COURIER
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
COURIER_BOLD
public static final java.lang.String COURIER_BOLD
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
COURIER_OBLIQUE
public static final java.lang.String COURIER_OBLIQUE
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
COURIER_BOLDOBLIQUE
public static final java.lang.String COURIER_BOLDOBLIQUE
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
HELVETICA
public static final java.lang.String HELVETICA
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
HELVETICA_BOLD
public static final java.lang.String HELVETICA_BOLD
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
HELVETICA_OBLIQUE
public static final java.lang.String HELVETICA_OBLIQUE
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
HELVETICA_BOLDOBLIQUE
public static final java.lang.String HELVETICA_BOLDOBLIQUE
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
SYMBOL
public static final java.lang.String SYMBOL
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
TIMES_ROMAN
public static final java.lang.String TIMES_ROMAN
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
TIMES_BOLD
public static final java.lang.String TIMES_BOLD
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
TIMES_ITALIC
public static final java.lang.String TIMES_ITALIC
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
TIMES_BOLDITALIC
public static final java.lang.String TIMES_BOLDITALIC
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
ZAPFDINGBATS
public static final java.lang.String ZAPFDINGBATS
This is a possible value of a base 14 type 1 font- See Also:
- Constant Field Values
-
ASCENT
public static final int ASCENT
The maximum height above the baseline reached by glyphs in this font, excluding the height of glyphs for accented characters.- See Also:
- Constant Field Values
-
CAPHEIGHT
public static final int CAPHEIGHT
The y coordinate of the top of flat capital letters, measured from the baseline.- See Also:
- Constant Field Values
-
DESCENT
public static final int DESCENT
The maximum depth below the baseline reached by glyphs in this font. The value is a negative number.- See Also:
- Constant Field Values
-
ITALICANGLE
public static final int ITALICANGLE
The angle, expressed in degrees counterclockwise from the vertical, of the dominant vertical strokes of the font. The value is negative for fonts that slope to the right, as almost all italic fonts do.- See Also:
- Constant Field Values
-
BBOXLLX
public static final int BBOXLLX
The lower left x glyph coordinate.- See Also:
- Constant Field Values
-
BBOXLLY
public static final int BBOXLLY
The lower left y glyph coordinate.- See Also:
- Constant Field Values
-
BBOXURX
public static final int BBOXURX
The upper right x glyph coordinate.- See Also:
- Constant Field Values
-
BBOXURY
public static final int BBOXURY
The upper right y glyph coordinate.- See Also:
- Constant Field Values
-
AWT_ASCENT
public static final int AWT_ASCENT
java.awt.Font property- See Also:
- Constant Field Values
-
AWT_DESCENT
public static final int AWT_DESCENT
java.awt.Font property- See Also:
- Constant Field Values
-
AWT_LEADING
public static final int AWT_LEADING
java.awt.Font property- See Also:
- Constant Field Values
-
AWT_MAXADVANCE
public static final int AWT_MAXADVANCE
java.awt.Font property- See Also:
- Constant Field Values
-
UNDERLINE_POSITION
public static final int UNDERLINE_POSITION
The underline position. Usually a negative value.- See Also:
- Constant Field Values
-
UNDERLINE_THICKNESS
public static final int UNDERLINE_THICKNESS
The underline thickness.- See Also:
- Constant Field Values
-
STRIKETHROUGH_POSITION
public static final int STRIKETHROUGH_POSITION
The strikethrough position.- See Also:
- Constant Field Values
-
STRIKETHROUGH_THICKNESS
public static final int STRIKETHROUGH_THICKNESS
The strikethrough thickness.- See Also:
- Constant Field Values
-
SUBSCRIPT_SIZE
public static final int SUBSCRIPT_SIZE
The recommended vertical size for subscripts for this font.- See Also:
- Constant Field Values
-
SUBSCRIPT_OFFSET
public static final int SUBSCRIPT_OFFSET
The recommended vertical offset from the baseline for subscripts for this font. Usually a negative value.- See Also:
- Constant Field Values
-
SUPERSCRIPT_SIZE
public static final int SUPERSCRIPT_SIZE
The recommended vertical size for superscripts for this font.- See Also:
- Constant Field Values
-
SUPERSCRIPT_OFFSET
public static final int SUPERSCRIPT_OFFSET
The recommended vertical offset from the baseline for superscripts for this font.- See Also:
- Constant Field Values
-
FONT_TYPE_T1
public static final int FONT_TYPE_T1
The font is Type 1.- See Also:
- Constant Field Values
-
FONT_TYPE_TT
public static final int FONT_TYPE_TT
The font is True Type with a standard encoding.- See Also:
- Constant Field Values
-
FONT_TYPE_CJK
public static final int FONT_TYPE_CJK
The font is CJK.- See Also:
- Constant Field Values
-
FONT_TYPE_TTUNI
public static final int FONT_TYPE_TTUNI
The font is True Type with a Unicode encoding.- See Also:
- Constant Field Values
-
FONT_TYPE_DOCUMENT
public static final int FONT_TYPE_DOCUMENT
A font already inside the document.- See Also:
- Constant Field Values
-
FONT_TYPE_T3
public static final int FONT_TYPE_T3
A Type3 font.- See Also:
- Constant Field Values
-
IDENTITY_H
public static final java.lang.String IDENTITY_H
The Unicode encoding with horizontal writing.- See Also:
- Constant Field Values
-
IDENTITY_V
public static final java.lang.String IDENTITY_V
The Unicode encoding with vertical writing.- See Also:
- Constant Field Values
-
CP1250
public static final java.lang.String CP1250
A possible encoding.- See Also:
- Constant Field Values
-
CP1252
public static final java.lang.String CP1252
A possible encoding.- See Also:
- Constant Field Values
-
CP1257
public static final java.lang.String CP1257
A possible encoding.- See Also:
- Constant Field Values
-
WINANSI
public static final java.lang.String WINANSI
A possible encoding.- See Also:
- Constant Field Values
-
MACROMAN
public static final java.lang.String MACROMAN
A possible encoding.- See Also:
- Constant Field Values
-
CHAR_RANGE_LATIN
public static final int[] CHAR_RANGE_LATIN
-
CHAR_RANGE_ARABIC
public static final int[] CHAR_RANGE_ARABIC
-
CHAR_RANGE_HEBREW
public static final int[] CHAR_RANGE_HEBREW
-
CHAR_RANGE_CYRILLIC
public static final int[] CHAR_RANGE_CYRILLIC
-
EMBEDDED
public static final boolean EMBEDDED
if the font has to be embedded- See Also:
- Constant Field Values
-
NOT_EMBEDDED
public static final boolean NOT_EMBEDDED
if the font doesn't have to be embedded- See Also:
- Constant Field Values
-
CACHED
public static final boolean CACHED
if the font has to be cached- See Also:
- Constant Field Values
-
NOT_CACHED
public static final boolean NOT_CACHED
if the font doesn't have to be cached- See Also:
- Constant Field Values
-
RESOURCE_PATH
public static final java.lang.String RESOURCE_PATH
The path to the font resources.- See Also:
- Constant Field Values
-
CID_NEWLINE
public static final char CID_NEWLINE
The fake CID code that represents a newline.- See Also:
- Constant Field Values
-
subsetRanges
protected java.util.ArrayList subsetRanges
-
notdef
public static final java.lang.String notdef
a not defined character in a custom PDF encoding- See Also:
- Constant Field Values
-
widths
protected int[] widths
table of characters widths for this encoding
-
differences
protected java.lang.String[] differences
encoding names
-
unicodeDifferences
protected char[] unicodeDifferences
same as differences but with the unicode codes
-
charBBoxes
protected int[][] charBBoxes
-
encoding
protected java.lang.String encoding
encoding used with this font
-
embedded
protected boolean embedded
true if the font is to be embedded in the PDF
-
compressionLevel
protected int compressionLevel
The compression level for the font stream.- Since:
- 2.1.3
-
fontSpecific
protected boolean fontSpecific
true if the font must use its built in encoding. In that case theencodingis only used to map a char to the position inside the font, not to the expected char name.
-
fontCache
protected static java.util.HashMap fontCache
cache for the fonts already used.
-
BuiltinFonts14
protected static final java.util.HashMap BuiltinFonts14
list of the 14 built in fonts.
-
forceWidthsOutput
protected boolean forceWidthsOutput
Forces the output of the width array. Only matters for the 14 built-in fonts.
-
directTextToByte
protected boolean directTextToByte
Convertschardirectly tobyteby casting.
-
subset
protected boolean subset
Indicates if all the glyphs and widths for that particular encoding should be included in the document.
-
fastWinansi
protected boolean fastWinansi
-
specialMap
protected IntHashtable specialMap
Custom encodings use this map to key the Unicode character to the single byte code.
-
-
Method Detail
-
createFont
public static BaseFont createFont() throws DocumentException, java.io.IOException
Creates a new font. This will always be the default Helvetica font (not embedded). This method is introduced because Helvetica is used in many examples.- Returns:
- a BaseFont object (Helvetica, Winansi, not embedded)
- Throws:
java.io.IOException- This shouldn't occur everDocumentException- This shouldn't occur ever- Since:
- 2.1.1
-
createFont
public static BaseFont createFont(java.lang.String name, java.lang.String encoding, boolean embedded) throws DocumentException, java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".The fonts are cached and if they already exist they are extracted from the cache, not parsed again.
Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:
"# simple 32 0020 0041 0042 0454"
Example for a "full" encoding for a Type1 Tex font:
"# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
This method calls:
createFont(name, encoding, embedded, true, null, null);
- Parameters:
name- the name of the font or its location on fileencoding- the encoding to be applied to this fontembedded- true if the font is to be embedded in the PDF- Returns:
- returns a new font. This font may come from the cache
- Throws:
DocumentException- the font is invalidjava.io.IOException- the font file could not be read
-
createFont
public static BaseFont createFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean forceRead) throws DocumentException, java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".The fonts are cached and if they already exist they are extracted from the cache, not parsed again.
Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:
"# simple 32 0020 0041 0042 0454"
Example for a "full" encoding for a Type1 Tex font:
"# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
This method calls:
createFont(name, encoding, embedded, true, null, null);
- Parameters:
name- the name of the font or its location on fileencoding- the encoding to be applied to this fontembedded- true if the font is to be embedded in the PDFforceRead- in some cases (TrueTypeFont, Type1Font), the full font file will be read and kept in memory if forceRead is true- Returns:
- returns a new font. This font may come from the cache
- Throws:
DocumentException- the font is invalidjava.io.IOException- the font file could not be read- Since:
- 2.1.5
-
createFont
public static BaseFont createFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb) throws DocumentException, java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".The fonts may or may not be cached depending on the flag
cached. If thebytearrays are present the font will be read from them instead of the name. A name is still required to identify the font type.Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:
"# simple 32 0020 0041 0042 0454"
Example for a "full" encoding for a Type1 Tex font:
"# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
- Parameters:
name- the name of the font or its location on fileencoding- the encoding to be applied to this fontembedded- true if the font is to be embedded in the PDFcached- true if the font comes from the cache or is added to the cache if new, false if the font is always created newttfAfm- the true type font or the afm in a byte arraypfb- the pfb in a byte array- Returns:
- returns a new font. This font may come from the cache but only if cached is true, otherwise it will always be created new
- Throws:
DocumentException- the font is invalidjava.io.IOException- the font file could not be read- Since:
- iText 0.80
-
createFont
public static BaseFont createFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow) throws DocumentException, java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".The fonts may or may not be cached depending on the flag
cached. If thebytearrays are present the font will be read from them instead of the name. A name is still required to identify the font type.Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:
"# simple 32 0020 0041 0042 0454"
Example for a "full" encoding for a Type1 Tex font:
"# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
- Parameters:
name- the name of the font or its location on fileencoding- the encoding to be applied to this fontembedded- true if the font is to be embedded in the PDFcached- true if the font comes from the cache or is added to the cache if new, false if the font is always created newttfAfm- the true type font or the afm in a byte arraypfb- the pfb in a byte arraynoThrow- if true will not throw an exception if the font is not recognized and will return null, if false will throw an exception if the font is not recognized. Note that even if true an exception may be thrown in some circumstances. This parameter is useful for FontFactory that may have to check many invalid font names before finding the right one- Returns:
- returns a new font. This font may come from the cache but only if cached is true, otherwise it will always be created new
- Throws:
DocumentException- the font is invalidjava.io.IOException- the font file could not be read- Since:
- 2.0.3
-
createFont
public static BaseFont createFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow, boolean forceRead) throws DocumentException, java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".The fonts may or may not be cached depending on the flag
cached. If thebytearrays are present the font will be read from them instead of the name. A name is still required to identify the font type.Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:
"# simple 32 0020 0041 0042 0454"
Example for a "full" encoding for a Type1 Tex font:
"# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
- Parameters:
name- the name of the font or its location on fileencoding- the encoding to be applied to this fontembedded- true if the font is to be embedded in the PDFcached- true if the font comes from the cache or is added to the cache if new, false if the font is always created newttfAfm- the true type font or the afm in a byte arraypfb- the pfb in a byte arraynoThrow- if true will not throw an exception if the font is not recognized and will return null, if false will throw an exception if the font is not recognized. Note that even if true an exception may be thrown in some circumstances. This parameter is useful for FontFactory that may have to check many invalid font names before finding the right oneforceRead- in some cases (TrueTypeFont, Type1Font), the full font file will be read and kept in memory if forceRead is true- Returns:
- returns a new font. This font may come from the cache but only if cached is true, otherwise it will always be created new
- Throws:
DocumentException- the font is invalidjava.io.IOException- the font file could not be read- Since:
- 2.1.5
-
createFont
public static BaseFont createFont(PRIndirectReference fontRef)
Creates a font based on an existing document font. The created font font may not behave as expected, depending on the encoding or subset.- Parameters:
fontRef- the reference to the document font- Returns:
- the font
-
getBaseName
protected static java.lang.String getBaseName(java.lang.String name)
Gets the name without the modifiers Bold, Italic or BoldItalic.- Parameters:
name- the full name of the font- Returns:
- the name without the modifiers Bold, Italic or BoldItalic
-
normalizeEncoding
protected static java.lang.String normalizeEncoding(java.lang.String enc)
Normalize the encoding names. "winansi" is changed to "Cp1252" and "macroman" is changed to "MacRoman".- Parameters:
enc- the encoding to be normalized- Returns:
- the normalized encoding
-
createEncoding
protected void createEncoding()
Creates thewidthsand thedifferencesarrays
-
getKerning
public abstract int getKerning(int char1, int char2)Gets the kerning between two Unicode chars.- Parameters:
char1- the first charchar2- the second char- Returns:
- the kerning to be applied in normalized 1000 units
-
setKerning
public abstract boolean setKerning(int char1, int char2, int kern)Sets the kerning between two Unicode chars.- Parameters:
char1- the first charchar2- the second charkern- the kerning to apply in normalized 1000 units- Returns:
trueif the kerning was applied,falseotherwise
-
getWidth
public int getWidth(int char1)
Gets the width of acharin normalized 1000 units.- Parameters:
char1- the unicodecharto get the width of- Returns:
- the width in normalized 1000 units
-
getWidth
public int getWidth(java.lang.String text)
Gets the width of aStringin normalized 1000 units.- Parameters:
text- theStringto get the width of- Returns:
- the width in normalized 1000 units
-
getDescent
public int getDescent(java.lang.String text)
Gets the descent of aStringin normalized 1000 units. The descent will always be less than or equal to zero even if all the characters have an higher descent.- Parameters:
text- theStringto get the descent of- Returns:
- the descent in normalized 1000 units
-
getAscent
public int getAscent(java.lang.String text)
Gets the ascent of aStringin normalized 1000 units. The ascent will always be greater than or equal to zero even if all the characters have a lower ascent.- Parameters:
text- theStringto get the ascent of- Returns:
- the ascent in normalized 1000 units
-
getDescentPoint
public float getDescentPoint(java.lang.String text, float fontSize)Gets the descent of aStringin points. The descent will always be less than or equal to zero even if all the characters have an higher descent.- Parameters:
text- theStringto get the descent offontSize- the size of the font- Returns:
- the descent in points
-
getAscentPoint
public float getAscentPoint(java.lang.String text, float fontSize)Gets the ascent of aStringin points. The ascent will always be greater than or equal to zero even if all the characters have a lower ascent.- Parameters:
text- theStringto get the ascent offontSize- the size of the font- Returns:
- the ascent in points
-
getWidthPointKerned
public float getWidthPointKerned(java.lang.String text, float fontSize)Gets the width of aStringin points taking kerning into account.- Parameters:
text- theStringto get the width offontSize- the font size- Returns:
- the width in points
-
getWidthPoint
public float getWidthPoint(java.lang.String text, float fontSize)Gets the width of aStringin points.- Parameters:
text- theStringto get the width offontSize- the font size- Returns:
- the width in points
-
getWidthPoint
public float getWidthPoint(int char1, float fontSize)Gets the width of acharin points.- Parameters:
char1- thecharto get the width offontSize- the font size- Returns:
- the width in points
-
getEncoding
public java.lang.String getEncoding()
Gets the encoding used to convertStringintobyte[].- Returns:
- the encoding name
-
getFontDescriptor
public abstract float getFontDescriptor(int key, float fontSize)Gets the font parameter identified bykey. Valid values forkeyareASCENT,AWT_ASCENT,CAPHEIGHT,DESCENT,AWT_DESCENT,ITALICANGLE,BBOXLLX,BBOXLLY,BBOXURXandBBOXURY.- Parameters:
key- the parameter to be extractedfontSize- the font size in points- Returns:
- the parameter in points
-
getFontType
public int getFontType()
Gets the font type. The font types can be: FONT_TYPE_T1, FONT_TYPE_TT, FONT_TYPE_CJK and FONT_TYPE_TTUNI.- Returns:
- the font type
-
isEmbedded
public boolean isEmbedded()
Gets the embedded flag.- Returns:
trueif the font is embedded.
-
isFontSpecific
public boolean isFontSpecific()
Gets the symbolic flag of the font.- Returns:
trueif the font is symbolic
-
createSubsetPrefix
public static java.lang.String createSubsetPrefix()
Creates a unique subset prefix to be added to the font name when the font is embedded and subset.- Returns:
- the subset prefix
-
getPostscriptFontName
public abstract java.lang.String getPostscriptFontName()
Gets the postscript font name.- Returns:
- the postscript font name
-
setPostscriptFontName
public abstract void setPostscriptFontName(java.lang.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
-
getFullFontName
public abstract java.lang.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
-
getAllNameEntries
public abstract java.lang.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 {"4", "", "", "", font name}.- Returns:
- the full name of the font
- Since:
- 2.0.8
-
getFullFontName
public static java.lang.String[][] getFullFontName(java.lang.String name, java.lang.String encoding, byte[] ttfAfm) throws DocumentException, java.io.IOExceptionGets 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}.- Parameters:
name- the name of the fontencoding- the encoding of the fontttfAfm- the true type font or the afm in a byte array- Returns:
- the full name of the font
- Throws:
DocumentException- on errorjava.io.IOException- on error
-
getAllFontNames
public static java.lang.Object[] getAllFontNames(java.lang.String name, java.lang.String encoding, byte[] ttfAfm) throws DocumentException, java.io.IOExceptionGets all the names from the font. Only the required tables are read.- Parameters:
name- the name of the fontencoding- the encoding of the fontttfAfm- the true type font or the afm in a byte array- Returns:
- an array of Object[] built with {getPostscriptFontName(), getFamilyFontName(), getFullFontName()}
- Throws:
DocumentException- on errorjava.io.IOException- on error
-
getAllNameEntries
public static java.lang.String[][] getAllNameEntries(java.lang.String name, java.lang.String encoding, byte[] ttfAfm) throws DocumentException, java.io.IOExceptionGets all the entries of the namestable from the font. Only the required tables are read.- Parameters:
name- the name of the fontencoding- the encoding of the fontttfAfm- the true type font or the afm in a byte array- Returns:
- an array of Object[] built with {getPostscriptFontName(), getFamilyFontName(), getFullFontName()}
- Throws:
DocumentException- on errorjava.io.IOException- on error- Since:
- 2.0.8
-
getFamilyFontName
public abstract java.lang.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
-
getCodePagesSupported
public java.lang.String[] getCodePagesSupported()
Gets the code pages supported by the font. This has only meaning with True Type fonts.- Returns:
- the code pages supported by the font
-
enumerateTTCNames
public static java.lang.String[] enumerateTTCNames(java.lang.String ttcFile) throws DocumentException, java.io.IOExceptionEnumerates the postscript font names present inside a True Type Collection.- Parameters:
ttcFile- the file name of the font- Returns:
- the postscript font names
- Throws:
DocumentException- on errorjava.io.IOException- on error
-
enumerateTTCNames
public static java.lang.String[] enumerateTTCNames(byte[] ttcArray) throws DocumentException, java.io.IOExceptionEnumerates the postscript font names present inside a True Type Collection.- Parameters:
ttcArray- the font as abytearray- Returns:
- the postscript font names
- Throws:
DocumentException- on errorjava.io.IOException- on error
-
getWidths
public int[] getWidths()
Gets the font width array.- Returns:
- the font width array
-
getDifferences
public java.lang.String[] getDifferences()
Gets the array with the names of the characters.- Returns:
- the array with the names of the characters
-
getUnicodeDifferences
public char[] getUnicodeDifferences()
Gets the array with the unicode characters.- Returns:
- the array with the unicode characters
-
isForceWidthsOutput
public boolean isForceWidthsOutput()
Gets the state of the property.- Returns:
- value of property forceWidthsOutput
-
setForceWidthsOutput
public void setForceWidthsOutput(boolean forceWidthsOutput)
Set totrueto force the generation of the widths array.- Parameters:
forceWidthsOutput-trueto force the generation of the widths array
-
isDirectTextToByte
public boolean isDirectTextToByte()
Gets the direct conversion ofchartobyte.- Returns:
- value of property directTextToByte.
- See Also:
setDirectTextToByte(boolean directTextToByte)
-
setDirectTextToByte
public void setDirectTextToByte(boolean directTextToByte)
Sets the conversion ofchardirectly tobyteby casting. This is a low level feature to put the bytes directly in the content stream without passing through String.getBytes().- Parameters:
directTextToByte- New value of property directTextToByte.
-
isSubset
public boolean isSubset()
Indicates if all the glyphs and widths for that particular encoding should be included in the document.- Returns:
falseto include all the glyphs and widths.
-
setSubset
public void setSubset(boolean subset)
Indicates if all the glyphs and widths for that particular encoding should be included in the document. When set totrueonly the glyphs used will be included in the font. When set tofalseandaddSubsetRange(int[])was not called the full font will be included otherwise just the characters ranges will be included.- Parameters:
subset- new value of property subset
-
getResourceStream
public static java.io.InputStream getResourceStream(java.lang.String key)
Gets the font resources.- Parameters:
key- the full name of the resource- Returns:
- the
InputStreamto get the resource ornullif not found
-
getResourceStream
public static java.io.InputStream getResourceStream(java.lang.String key, java.lang.ClassLoader loader)Gets the font resources.- Parameters:
key- the full name of the resourceloader- the ClassLoader to load the resource or null to try the ones available- Returns:
- the
InputStreamto get the resource ornullif not found
-
getUnicodeEquivalent
public int getUnicodeEquivalent(int c)
Gets the Unicode equivalent to a CID. The (inexistent) CIDis translated as '\n'. It has only meaning with CJK fonts with Identity encoding. - Parameters:
c- the CID code- Returns:
- the Unicode equivalent
-
getCidCode
public int getCidCode(int c)
Gets the CID code given an Unicode. It has only meaning with CJK fonts.- Parameters:
c- the Unicode- Returns:
- the CID equivalent
-
hasKernPairs
public abstract boolean hasKernPairs()
Checks if the font has any kerning pairs.- Returns:
trueif the font has any kerning pairs
-
charExists
public boolean charExists(int c)
Checks if a character exists in this font.- Parameters:
c- the character to check- Returns:
trueif the character has a glyph,falseotherwise
-
setCharAdvance
public boolean setCharAdvance(int c, int advance)Sets the character advance.- Parameters:
c- the characteradvance- the character advance normalized to 1000 units- Returns:
trueif the advance was set,falseotherwise
-
getDocumentFonts
public static java.util.ArrayList getDocumentFonts(PdfReader reader)
Gets a list of all document fonts. Each element of theArrayListcontains aObject[]{String,PRIndirectReference}with the font name and the indirect reference to it.- Parameters:
reader- the document where the fonts are to be listed from- Returns:
- the list of fonts and references
-
getDocumentFonts
public static java.util.ArrayList getDocumentFonts(PdfReader reader, int page)
Gets a list of the document fonts in a particular page. Each element of theArrayListcontains aObject[]{String,PRIndirectReference}with the font name and the indirect reference to it.- Parameters:
reader- the document where the fonts are to be listed frompage- the page to list the fonts from- Returns:
- the list of fonts and references
-
getCharBBox
public int[] getCharBBox(int c)
Gets the smallest box enclosing the character contours. It will returnnullif the font has not the information or the character has no contours, as in the case of the space, for example. Characters with no contours may also return [0,0,0,0].- Parameters:
c- the character to get the contour bounding box from- Returns:
- an array of four floats with the bounding box in the format [llx,lly,urx,ury] or
null
-
getRawCharBBox
protected abstract int[] getRawCharBBox(int c, java.lang.String name)
-
correctArabicAdvance
public void correctArabicAdvance()
iText expects Arabic Diactrics (tashkeel) to have zero advance but some fonts, most notably those that come with Windows, like times.ttf, have non-zero advance for those characters. This method makes those character to have zero width advance and work correctly in the iText Arabic shaping and reordering context.
-
addSubsetRange
public void addSubsetRange(int[] range)
Adds a character range when subsetting. The range is anintarray where the first element is the start range inclusive and the second element is the end range inclusive. Several ranges are allowed in the same array.- Parameters:
range- the character range
-
getCompressionLevel
public int getCompressionLevel()
Returns the compression level used for the font streams.- Returns:
- the compression level (0 = best speed, 9 = best compression, -1 is default)
- Since:
- 2.1.3
-
setCompressionLevel
public void setCompressionLevel(int compressionLevel)
Sets the compression level to be used for the font streams.- Parameters:
compressionLevel- a value between 0 (best speed) and 9 (best compression)- Since:
- 2.1.3
-
-