Package org.apache.pdfbox.pdmodel.font
Class PDFont
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.font.PDFont
-
- All Implemented Interfaces:
COSObjectable
- Direct Known Subclasses:
PDSimpleFont
public abstract class PDFont extends java.lang.Object implements COSObjectable
This is the base class for all PDF fonts.- Author:
- Ben Litchfield
-
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.fontbox.cmap.CMapcmapThis is only used if this is a font object and it has an encoding and it is a type0 font with a cmap.protected static java.util.Map<java.lang.String,org.apache.fontbox.cmap.CMap>cmapObjectsprotected COSDictionaryfontThe cos dictionary for this font.protected PDMatrixfontMatrixThe font matrix.protected static java.lang.StringresourceRootCMAPprotected org.apache.fontbox.cmap.CMaptoUnicodeCmapThe CMap holding the ToUnicode mapping.
-
Constructor Summary
Constructors Constructor Description PDFont()Constructor.PDFont(COSDictionary fontDictionary)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclear()Calling this will release all cached information.static voidclearResources()This will clear AFM resources that are stored statically.protected java.lang.StringcmapEncoding(int code, int length, boolean isCIDFont, org.apache.fontbox.cmap.CMap sourceCmap)Encode the given value using the CMap of the font.protected abstract voiddetermineEncoding()Determines the encoding for the font.abstract voiddrawString(java.lang.String string, int[] codePoints, java.awt.Graphics g, float fontSize, java.awt.geom.AffineTransform at, float x, float y)This will draw a string on a canvas using the font.voiddrawString(java.lang.String string, java.awt.Graphics g, float fontSize, java.awt.geom.AffineTransform at, float x, float y)Deprecated.java.lang.Stringencode(byte[] c, int offset, int length)This will perform the encoding of a character if needed.intencodeToCID(byte[] c, int offset, int length)booleanequals(java.lang.Object other)protected org.apache.fontbox.afm.FontMetricgetAFM()This will get an AFM object if one exists.abstract floatgetAverageFontWidth()This will get the average font width for all characters.protected floatgetAverageFontWidthFromAFMFile()This will attempt to get the average font width from an AFM file.java.lang.StringgetBaseFont()The PostScript name of the font.intgetCodeFromArray(byte[] data, int offset, int length)Used for multibyte encodings.COSBasegetCOSObject()Convert this standard java object to a COS object.protected COSBasegetEncoding()cache theCOSName.ENCODINGobject from the font's dictionary since it is called so often.intgetFirstChar()The code for the first char or -1 if there is none.abstract PDRectanglegetFontBoundingBox()This will get the fonts bounding box.PDFontDescriptorgetFontDescriptor()This will get the font descriptor for this font.EncodinggetFontEncoding()This will get or create the encoder.abstract floatgetFontHeight(byte[] c, int offset, int length)This will get the font height for a character.PDMatrixgetFontMatrix()This will get the matrix that is used to transform glyph space to text space.abstract floatgetFontWidth(byte[] c, int offset, int length)This will get the font width for a character.floatgetFontWidth(int charCode)Determines the width of the given character.protected floatgetFontWidthFromAFMFile(int code)This will attempt to get the font width from an AFM file.intgetLastChar()The code for the last char or -1 if there is none.abstract floatgetSpaceWidth()Determines the width of the space character.protected static java.lang.StringgetStringFromArray(byte[] c, int offset, int length)Map the given byte(s) to a string.floatgetStringWidth(java.lang.String string)This will get the width of this string for this font.java.lang.StringgetSubType()This will get the subtype of font, Type1, Type3, ...org.apache.fontbox.cmap.CMapgetToUnicodeCMap()Returns the toUnicode mapping if present.java.lang.StringgetType()This will always return "Font" for fonts.java.util.List<java.lang.Float>getWidths()The widths of the characters.inthashCode()protected booleanhasToUnicode()Determines if a font as a ToUnicode entry.booleanisSymbolicFont()Determines if the font is a symbolic font.protected booleanisType0Font()Determines if the font is a type 0 font.protected booleanisType1Font()Determines if the font is a type 1 font.booleanisType3Font()Determines if the font is a type 3 font.protected org.apache.fontbox.cmap.CMapparseCmap(java.lang.String cmapRoot, java.io.InputStream cmapStream)voidsetBaseFont(java.lang.String baseFont)Set the PostScript name of the font.protected voidsetEncoding(COSBase encodingValue)Set the encoding object from the fonts dictionary.voidsetFirstChar(int firstChar)Set the first character this font supports.voidsetFontDescriptor(PDFontDescriptorDictionary fdDictionary)This will set the font descriptor.voidsetFontEncoding(Encoding enc)The will set the encoding for this font.protected voidsetHasToUnicode(boolean hasToUnicodeValue)Sets hasToUnicode to the given value.voidsetLastChar(int lastChar)Set the last character this font supports.voidsetWidths(java.util.List<java.lang.Float> widthsList)Set the widths of the characters code.
-
-
-
Field Detail
-
font
protected COSDictionary font
The cos dictionary for this font.
-
fontMatrix
protected PDMatrix fontMatrix
The font matrix.
-
cmap
protected org.apache.fontbox.cmap.CMap cmap
This is only used if this is a font object and it has an encoding and it is a type0 font with a cmap.
-
toUnicodeCmap
protected org.apache.fontbox.cmap.CMap toUnicodeCmap
The CMap holding the ToUnicode mapping.
-
cmapObjects
protected static java.util.Map<java.lang.String,org.apache.fontbox.cmap.CMap> cmapObjects
-
resourceRootCMAP
protected static final java.lang.String resourceRootCMAP
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PDFont
public PDFont()
Constructor.
-
PDFont
public PDFont(COSDictionary fontDictionary)
Constructor.- Parameters:
fontDictionary- The font dictionary according to the PDF specification.
-
-
Method Detail
-
clearResources
public static void clearResources()
This will clear AFM resources that are stored statically. This is usually not a problem unless you want to reclaim resources for a long running process. SPECIAL NOTE: The font calculations are currently in COSObject, which is where they will reside until PDFont is mature enough to take them over. PDFont is the appropriate place for them and not in COSObject but we need font calculations for text extraction. THIS METHOD WILL BE MOVED OR REMOVED TO ANOTHER LOCATION IN A FUTURE VERSION OF PDFBOX.
-
getFontDescriptor
public PDFontDescriptor getFontDescriptor()
This will get the font descriptor for this font.- Returns:
- The font descriptor for this font.
-
setFontDescriptor
public void setFontDescriptor(PDFontDescriptorDictionary fdDictionary)
This will set the font descriptor.- Parameters:
fdDictionary- The font descriptor.
-
determineEncoding
protected abstract void determineEncoding()
Determines the encoding for the font. This method as to be overwritten, as there are different possibilities to define a mapping.
-
getCOSObject
public COSBase getCOSObject()
Convert this standard java object to a COS object.- Specified by:
getCOSObjectin interfaceCOSObjectable- Returns:
- The cos object that matches this Java object.
-
getFontWidth
public abstract float getFontWidth(byte[] c, int offset, int length) throws java.io.IOExceptionThis will get the font width for a character.- Parameters:
c- The character code to get the width for.offset- The offset into the array.length- The length of the data.- Returns:
- The width is in 1000 unit of text space, ie 333 or 777
- Throws:
java.io.IOException- If an error occurs while parsing.
-
getFontHeight
public abstract float getFontHeight(byte[] c, int offset, int length) throws java.io.IOExceptionThis will get the font height for a character.- Parameters:
c- The character code to get the height for.offset- The offset into the array.length- The length of the data.- Returns:
- The height is in 1000 unit of text space, ie 333 or 777
- Throws:
java.io.IOException- If an error occurs while parsing.
-
getStringWidth
public float getStringWidth(java.lang.String string) throws java.io.IOExceptionThis will get the width of this string for this font.- Parameters:
string- The string to get the width of.- Returns:
- The width of the string in 1000 units of text space, ie 333 567...
- Throws:
java.io.IOException- If there is an error getting the width information.
-
getAverageFontWidth
public abstract float getAverageFontWidth() throws java.io.IOExceptionThis will get the average font width for all characters.- Returns:
- The width is in 1000 unit of text space, ie 333 or 777
- Throws:
java.io.IOException- If an error occurs while parsing.
-
drawString
public void drawString(java.lang.String string, java.awt.Graphics g, float fontSize, java.awt.geom.AffineTransform at, float x, float y) throws java.io.IOExceptionDeprecated.This will draw a string on a canvas using the font.- Parameters:
string- The string to draw.g- The graphics to draw onto.fontSize- The size of the font to draw.at- The transformation matrix with all information for scaling and shearing of the font.x- The x coordinate to draw at.y- The y coordinate to draw at.- Throws:
java.io.IOException- If there is an error drawing the specific string.
-
drawString
public abstract void drawString(java.lang.String string, int[] codePoints, java.awt.Graphics g, float fontSize, java.awt.geom.AffineTransform at, float x, float y) throws java.io.IOExceptionThis will draw a string on a canvas using the font.- Parameters:
string- The string to draw.codePoints- The codePoints of the given string.g- The graphics to draw onto.fontSize- The size of the font to draw.at- The transformation matrix with all information for scaling and shearing of the font.x- The x coordinate to draw at.y- The y coordinate to draw at.- Throws:
java.io.IOException- If there is an error drawing the specific string.
-
getCodeFromArray
public int getCodeFromArray(byte[] data, int offset, int length)Used for multibyte encodings.- Parameters:
data- The array of data.offset- The offset into the array.length- The number of bytes to use.- Returns:
- The int value of data from the array.
-
getFontWidthFromAFMFile
protected float getFontWidthFromAFMFile(int code) throws java.io.IOExceptionThis will attempt to get the font width from an AFM file.- Parameters:
code- The character code we are trying to get.- Returns:
- The font width from the AFM file.
- Throws:
java.io.IOException- if we cannot find the width.
-
getAverageFontWidthFromAFMFile
protected float getAverageFontWidthFromAFMFile() throws java.io.IOExceptionThis will attempt to get the average font width from an AFM file.- Returns:
- The average font width from the AFM file.
- Throws:
java.io.IOException- if we cannot find the width.
-
getAFM
protected org.apache.fontbox.afm.FontMetric getAFM()
This will get an AFM object if one exists.- Returns:
- The afm object from the name.
-
getEncoding
protected COSBase getEncoding()
cache theCOSName.ENCODINGobject from the font's dictionary since it is called so often.Use this method instead of
font.getDictionaryObject(COSName.ENCODING);
- Returns:
- the encoding
-
setEncoding
protected void setEncoding(COSBase encodingValue)
Set the encoding object from the fonts dictionary.- Parameters:
encodingValue- the given encoding.
-
cmapEncoding
protected java.lang.String cmapEncoding(int code, int length, boolean isCIDFont, org.apache.fontbox.cmap.CMap sourceCmap) throws java.io.IOExceptionEncode the given value using the CMap of the font.- Parameters:
code- the code to encode.length- the byte length of the given code.isCIDFont- indicates that the used font is a CID font.- Returns:
- The value of the encoded character.
- Throws:
java.io.IOException- if something went wrong
-
encode
public java.lang.String encode(byte[] c, int offset, int length) throws java.io.IOExceptionThis will perform the encoding of a character if needed.- Parameters:
c- The character to encode.offset- The offset into the array to get the datalength- The number of bytes to read.- Returns:
- The value of the encoded character.
- Throws:
java.io.IOException- If there is an error during the encoding.
-
encodeToCID
public int encodeToCID(byte[] c, int offset, int length) throws java.io.IOException- Throws:
java.io.IOException
-
getStringFromArray
protected static java.lang.String getStringFromArray(byte[] c, int offset, int length)Map the given byte(s) to a string.- Parameters:
c- the byte arrayoffset- the offset of the byte(s)length- the number of bytes, usually 1 or 2- Returns:
- the mapped string
-
parseCmap
protected org.apache.fontbox.cmap.CMap parseCmap(java.lang.String cmapRoot, java.io.InputStream cmapStream)
-
setFontEncoding
public void setFontEncoding(Encoding enc)
The will set the encoding for this font.- Parameters:
enc- The font encoding.
-
getFontEncoding
public Encoding getFontEncoding()
This will get or create the encoder.- Returns:
- The encoding to use.
-
getType
public java.lang.String getType()
This will always return "Font" for fonts.- Returns:
- The type of object that this is.
-
getSubType
public java.lang.String getSubType()
This will get the subtype of font, Type1, Type3, ...- Returns:
- The type of font that this is.
-
isType1Font
protected boolean isType1Font()
Determines if the font is a type 1 font.- Returns:
- returns true if the font is a type 1 font
-
isType3Font
public boolean isType3Font()
Determines if the font is a type 3 font.- Returns:
- returns true if the font is a type 3 font
-
isType0Font
protected boolean isType0Font()
Determines if the font is a type 0 font.- Returns:
- returns true if the font is a type 0 font
-
isSymbolicFont
public boolean isSymbolicFont()
Determines if the font is a symbolic font.- Returns:
- returns true if the font is a symbolic font
-
getBaseFont
public java.lang.String getBaseFont()
The PostScript name of the font.- Returns:
- The postscript name of the font.
-
setBaseFont
public void setBaseFont(java.lang.String baseFont)
Set the PostScript name of the font.- Parameters:
baseFont- The postscript name for the font.
-
getFirstChar
public int getFirstChar()
The code for the first char or -1 if there is none.- Returns:
- The code for the first character.
-
setFirstChar
public void setFirstChar(int firstChar)
Set the first character this font supports.- Parameters:
firstChar- The first character.
-
getLastChar
public int getLastChar()
The code for the last char or -1 if there is none.- Returns:
- The code for the last character.
-
setLastChar
public void setLastChar(int lastChar)
Set the last character this font supports.- Parameters:
lastChar- The last character.
-
getWidths
public java.util.List<java.lang.Float> getWidths()
The widths of the characters. This will be null for the standard 14 fonts.- Returns:
- The widths of the characters.
-
setWidths
public void setWidths(java.util.List<java.lang.Float> widthsList)
Set the widths of the characters code.- Parameters:
widthsList- The widths of the character codes.
-
getFontMatrix
public PDMatrix getFontMatrix()
This will get the matrix that is used to transform glyph space to text space. By default there are 1000 glyph units to 1 text space unit, but type3 fonts can use any value. Note:If this is a type3 font then it can be modified via the PDType3Font.setFontMatrix, otherwise this is a read-only property.- Returns:
- The matrix to transform from glyph space to text space.
-
getFontBoundingBox
public abstract PDRectangle getFontBoundingBox() throws java.io.IOException
This will get the fonts bounding box.- Returns:
- The fonts bounding box.
- Throws:
java.io.IOException- If there is an error getting the bounding box.
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
getFontWidth
public float getFontWidth(int charCode)
Determines the width of the given character.- Parameters:
charCode- the code of the given character- Returns:
- the width of the character
-
hasToUnicode
protected boolean hasToUnicode()
Determines if a font as a ToUnicode entry.- Returns:
- true if the font has a ToUnicode entry
-
setHasToUnicode
protected void setHasToUnicode(boolean hasToUnicodeValue)
Sets hasToUnicode to the given value.- Parameters:
hasToUnicodeValue- the given value for hasToUnicode
-
getSpaceWidth
public abstract float getSpaceWidth()
Determines the width of the space character.- Returns:
- the width of the space character
-
getToUnicodeCMap
public org.apache.fontbox.cmap.CMap getToUnicodeCMap()
Returns the toUnicode mapping if present.- Returns:
- the CMap representing the toUnicode mapping
-
clear
public void clear()
Calling this will release all cached information.
-
-