Class PDFont
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.font.PDFont
-
- All Implemented Interfaces:
COSObjectable,PDFontLike
- Direct Known Subclasses:
PDSimpleFont,PDType0Font
public abstract class PDFont extends java.lang.Object implements COSObjectable, PDFontLike
This is the base class for all PDF fonts.- Author:
- Ben Litchfield
-
-
Field Summary
Fields Modifier and Type Field Description protected static MatrixDEFAULT_FONT_MATRIXprotected COSDictionarydict
-
Constructor Summary
Constructors Modifier Constructor Description protectedPDFont(COSDictionary fontDictionary)Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidaddToSubset(int codePoint)Adds the given Unicode point to the subset.protected abstract byte[]encode(int unicode)Encodes the given Unicode code point for use in a PDF content stream.byte[]encode(java.lang.String text)Encodes the given string for use in a PDF content stream.booleanequals(java.lang.Object other)floatgetAverageFontWidth()This will get the average font width for all characters.abstract org.apache.fontbox.util.BoundingBoxgetBoundingBox()Returns the font's bounding box.COSDictionarygetCOSObject()Convert this standard java object to a COS object.VectorgetDisplacement(int code)Returns the displacement vector (w0, w1) in text space, for the given character.PDFontDescriptorgetFontDescriptor()Returns the font descriptor, may be null.MatrixgetFontMatrix()Returns the font matrix, which represents the transformation from glyph space to text space.abstract floatgetHeight(int code)Returns the height of the given character, in glyph space.abstract java.lang.StringgetName()Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".VectorgetPositionVector(int code)Returns the position vector (v), in text space, for the given character.floatgetSpaceWidth()Determines the width of the space character.protected org.apache.fontbox.afm.FontMetricsgetStandard14AFM()Returns the AFM if this is a Standard 14 font.protected abstract floatgetStandard14Width(int code)Returns the glyph width from the AFM if this is a Standard 14 font.floatgetStringWidth(java.lang.String text)Returns the width of the given Unicode string.java.lang.StringgetSubType()This will get the subtype of font.java.lang.StringgetType()This will always return "Font" for fonts.floatgetWidth(int code)Returns the advance width of the given character, in glyph space.abstract floatgetWidthFromFont(int code)Returns the width of a glyph in the embedded font file.protected java.util.List<java.lang.Float>getWidths()The widths of the characters.inthashCode()abstract booleanisDamaged()Returns true if the embedded font file is damaged.abstract booleanisEmbedded()Returns true if the font file is embedded in the PDF.booleanisStandard14()Returns true if this font is one of the "Standard 14" fonts and receives special handling.abstract booleanisVertical()Returns true if the font uses vertical writing mode.protected org.apache.fontbox.cmap.CMapreadCMap(COSBase base)Reads a CMap given a COS Stream or Name.abstract intreadCode(java.io.InputStream in)Reads a character code from a content stream string.protected voidsetFontDescriptor(PDFontDescriptor fontDescriptor)Sets the font descriptor when embedding a font.abstract voidsubset()Replaces this font with a subset containing only the given Unicode characters.java.lang.StringtoString()java.lang.StringtoUnicode(int code)Returns the Unicode character sequence which corresponds to the given character code.java.lang.StringtoUnicode(int code, GlyphList customGlyphList)Returns the Unicode character sequence which corresponds to the given character code.abstract booleanwillBeSubset()Returns true if this font will be subset when embedded.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.pdfbox.pdmodel.font.PDFontLike
hasExplicitWidth
-
-
-
-
Field Detail
-
DEFAULT_FONT_MATRIX
protected static final Matrix DEFAULT_FONT_MATRIX
-
dict
protected final COSDictionary dict
-
-
Constructor Detail
-
PDFont
protected PDFont(COSDictionary fontDictionary) throws java.io.IOException
Constructor.- Parameters:
fontDictionary- Font dictionary.- Throws:
java.io.IOException
-
-
Method Detail
-
getStandard14AFM
protected final org.apache.fontbox.afm.FontMetrics getStandard14AFM()
Returns the AFM if this is a Standard 14 font.
-
getFontDescriptor
public PDFontDescriptor getFontDescriptor()
Description copied from interface:PDFontLikeReturns the font descriptor, may be null.- Specified by:
getFontDescriptorin interfacePDFontLike
-
setFontDescriptor
protected final void setFontDescriptor(PDFontDescriptor fontDescriptor)
Sets the font descriptor when embedding a font.
-
readCMap
protected final org.apache.fontbox.cmap.CMap readCMap(COSBase base) throws java.io.IOException
Reads a CMap given a COS Stream or Name. May return null if a predefined CMap does not exist.- Parameters:
base- COSName or COSStream- Throws:
java.io.IOException
-
getCOSObject
public COSDictionary getCOSObject()
Description copied from interface:COSObjectableConvert this standard java object to a COS object.- Specified by:
getCOSObjectin interfaceCOSObjectable- Returns:
- The cos object that matches this Java object.
-
getPositionVector
public Vector getPositionVector(int code)
Description copied from interface:PDFontLikeReturns the position vector (v), in text space, for the given character. This represents the position of vertical origin relative to horizontal origin, for horizontal writing it will always be (0, 0). For vertical writing both x and y are set.- Specified by:
getPositionVectorin interfacePDFontLike- Parameters:
code- character code- Returns:
- position vector
-
getDisplacement
public Vector getDisplacement(int code) throws java.io.IOException
Returns the displacement vector (w0, w1) in text space, for the given character. For horizontal text only the x component is used, for vertical text only the y component.- Parameters:
code- character code- Returns:
- displacement vector
- Throws:
java.io.IOException
-
getWidth
public float getWidth(int code) throws java.io.IOExceptionDescription copied from interface:PDFontLikeReturns the advance width of the given character, in glyph space.If you want the visual bounds of the glyph then call getPath(..) on the appropriate PDFont subclass to retrieve the glyph outline as a GeneralPath instead.
- Specified by:
getWidthin interfacePDFontLike- Parameters:
code- character code- Throws:
java.io.IOException
-
getStandard14Width
protected abstract float getStandard14Width(int code)
Returns the glyph width from the AFM if this is a Standard 14 font.- Parameters:
code- character code- Returns:
- width in 1/1000 text space
-
getWidthFromFont
public abstract float getWidthFromFont(int code) throws java.io.IOExceptionDescription copied from interface:PDFontLikeReturns the width of a glyph in the embedded font file.- Specified by:
getWidthFromFontin interfacePDFontLike- Parameters:
code- character code- Returns:
- width in glyph space
- Throws:
java.io.IOException- if the font could not be read
-
isEmbedded
public abstract boolean isEmbedded()
Description copied from interface:PDFontLikeReturns true if the font file is embedded in the PDF.- Specified by:
isEmbeddedin interfacePDFontLike
-
getHeight
public abstract float getHeight(int code) throws java.io.IOExceptionDescription copied from interface:PDFontLikeReturns the height of the given character, in glyph space. This can be expensive to calculate. Results are only approximate.Warning: This method is deprecated in PDFBox 2.0 because there is no meaningful value which it can return. The
PDFontLike.getWidth(int)method returns the advance width of a glyph, but there is no corresponding advance height. The logical height of a character is the same for every character in a font, so if you want that, retrieve the font bbox's height. Otherwise if you want the visual bounds of the glyph then call getPath(..) on the appropriate PDFont subclass to retrieve the glyph outline as a GeneralPath.- Specified by:
getHeightin interfacePDFontLike- Parameters:
code- character code- Throws:
java.io.IOException
-
encode
public final byte[] encode(java.lang.String text) throws java.io.IOExceptionEncodes the given string for use in a PDF content stream.- Parameters:
text- Any Unicode text.- Returns:
- Array of PDF content stream bytes.
- Throws:
java.io.IOException- If the text could not be encoded.
-
encode
protected abstract byte[] encode(int unicode) throws java.io.IOExceptionEncodes the given Unicode code point for use in a PDF content stream. Content streams use a multi-byte encoding with 1 to 4 bytes.This method is called when embedding text in PDFs and when filling in fields.
- Parameters:
unicode- Unicode code point.- Returns:
- Array of 1 to 4 PDF content stream bytes.
- Throws:
java.io.IOException- If the text could not be encoded.
-
getStringWidth
public float getStringWidth(java.lang.String text) throws java.io.IOExceptionReturns the width of the given Unicode string.- Parameters:
text- The text to get the width of.- Returns:
- The width of the string in 1/1000 units of text space.
- Throws:
java.io.IOException- If there is an error getting the width information.
-
getAverageFontWidth
public float getAverageFontWidth()
This will get the average font width for all characters.- Specified by:
getAverageFontWidthin interfacePDFontLike- Returns:
- The width is in 1000 unit of text space, ie 333 or 777
-
readCode
public abstract int readCode(java.io.InputStream in) throws java.io.IOExceptionReads a character code from a content stream string. Codes may be up to 4 bytes long.- Parameters:
in- string stream- Returns:
- character code
- Throws:
java.io.IOException- if the CMap or stream cannot be read
-
toUnicode
public java.lang.String toUnicode(int code, GlyphList customGlyphList) throws java.io.IOExceptionReturns the Unicode character sequence which corresponds to the given character code.- Parameters:
code- character codecustomGlyphList- a custom glyph list to use instead of the Adobe Glyph List- Returns:
- Unicode character(s)
- Throws:
java.io.IOException
-
toUnicode
public java.lang.String toUnicode(int code) throws java.io.IOExceptionReturns the Unicode character sequence which corresponds to the given character code.- Parameters:
code- character code- Returns:
- Unicode character(s)
- Throws:
java.io.IOException
-
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.
-
getName
public abstract java.lang.String getName()
Description copied from interface:PDFontLikeReturns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".- Specified by:
getNamein interfacePDFontLike
-
getBoundingBox
public abstract org.apache.fontbox.util.BoundingBox getBoundingBox() throws java.io.IOExceptionDescription copied from interface:PDFontLikeReturns the font's bounding box.- Specified by:
getBoundingBoxin interfacePDFontLike- Throws:
java.io.IOException
-
getWidths
protected final 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.
-
getFontMatrix
public Matrix getFontMatrix()
Description copied from interface:PDFontLikeReturns the font matrix, which represents the transformation from glyph space to text space.- Specified by:
getFontMatrixin interfacePDFontLike
-
getSpaceWidth
public float getSpaceWidth()
Determines the width of the space character.- Returns:
- the width of the space character
-
isVertical
public abstract boolean isVertical()
Returns true if the font uses vertical writing mode.
-
isStandard14
public boolean isStandard14()
Returns true if this font is one of the "Standard 14" fonts and receives special handling.
-
addToSubset
public abstract void addToSubset(int codePoint)
Adds the given Unicode point to the subset.- Parameters:
codePoint- Unicode code point
-
subset
public abstract void subset() throws java.io.IOExceptionReplaces this font with a subset containing only the given Unicode characters.- Throws:
java.io.IOException- if the subset could not be written
-
willBeSubset
public abstract boolean willBeSubset()
Returns true if this font will be subset when embedded.
-
isDamaged
public abstract boolean isDamaged()
Description copied from interface:PDFontLikeReturns true if the embedded font file is damaged.- Specified by:
isDamagedin interfacePDFontLike
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-