java.lang.Object
org.sejda.sambox.pdmodel.font.PDFont
org.sejda.sambox.pdmodel.font.PDType0Font
- All Implemented Interfaces:
COSObjectable,PDFontLike,PDVectorFont,Subsettable
A Composite (Type 0) font.
- Author:
- Ben Litchfield
-
Field Summary
Fields inherited from class org.sejda.sambox.pdmodel.font.PDFont
DEFAULT_FONT_MATRIX, dict -
Constructor Summary
ConstructorsConstructorDescriptionPDType0Font(COSDictionary fontDictionary) Constructor for reading a Type0 font from a PDF file. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddToSubset(int codePoint) Adds the given Unicode point to the subset.intcodeToCID(int code) Returns the CID for the given character code.intcodeToGID(int code) Returns the GID for the given character code.protected byte[]encode(int unicode) Encodes the given Unicode code point for use in a PDF content stream.floatThis will get the average font width for all characters.Returns the PostScript name of the font.org.apache.fontbox.util.BoundingBoxReturns the font's bounding box.org.apache.fontbox.cmap.CMapgetCMap()Returns the font's CMap.org.apache.fontbox.cmap.CMapReturns the font's UCS2 CMap, only present this font uses a predefined CMap.Returns the descendant font.getDisplacement(int code) Returns the displacement vector (w0, w1) in text space, for the given character.Returns the font descriptor, may be null.Returns the font matrix, which represents the transformation from glyph space to text space.floatgetHeight(int code) Returns the height of the given character, in glyph space.getName()Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".getPath(int code) Returns the glyph path for the given character code.getPositionVector(int code) Returns the position vector (v), in text space, for the given character.protected floatgetStandard14Width(int code) Returns the glyph width from the AFM if this is a Standard 14 font.floatgetWidth(int code) Returns the advance width of the given character, in glyph space.floatgetWidthFromFont(int code) Returns the width of a glyph in the embedded font file.booleanhasExplicitWidth(int code) Returns true if the Font dictionary specifies an explicit width for the given glyph.booleanhasGlyph(int code) Returns true if this font contains a glyph for the given character code.booleanReturns true if the embedded font file is damaged.booleanReturns true if the font file is embedded in the PDF.booleanReturns true if a substitute font was searched via font mappings, but an exact match was not found, and a fallback was usedbooleanReturns true if the font file that was supposed to be embedded in the PDF is missing.booleanReturns true if this font is one of the "Standard 14" fonts and receives special handling.booleanReturns true if the font uses vertical writing mode.static PDType0Fontload(PDDocument doc, File file) Loads a TTF to be embedded and subset into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, InputStream input) Loads a TTF to be embedded and subset into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, InputStream input, boolean embedSubset) Loads a TTF to be embedded into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, boolean embedSubset) Loads a TTF to be embedded into a document as a Type 0 font.static PDType0FontloadVertical(PDDocument doc, File file) Loads a TTF to be embedded into a document as a vertical Type 0 font.static PDType0FontloadVertical(PDDocument doc, InputStream input) Loads a TTF to be embedded into a document as a vertical Type 0 font.static PDType0FontloadVertical(PDDocument doc, InputStream input, boolean embedSubset) Loads a TTF to be embedded into a document as a vertical Type 0 font.static PDType0FontloadVertical(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, boolean embedSubset) Loads a TTF to be embedded into a document as a vertical Type 0 font.intreadCode(InputStream in) Reads a character code from a content stream string.voidsubset()Replaces this font with a subset containing only the given Unicode characters.toString()toUnicode(int code) Returns the Unicode character sequence which corresponds to the given character code.booleanMethods inherited from class org.sejda.sambox.pdmodel.font.PDFont
encode, encodeLeniently, equals, getCOSObject, getSpaceWidth, getStandard14AFM, getStringWidth, getStringWidthLeniently, getSubType, getToUnicodeCMap, getTransientMetadata, getType, getWidths, hashCode, readCMap, setFontDescriptor, toUnicode
-
Constructor Details
-
PDType0Font
Constructor for reading a Type0 font from a PDF file.- Parameters:
fontDictionary- The font dictionary according to the PDF specification.- Throws:
IOException
-
-
Method Details
-
load
Loads a TTF to be embedded and subset into a document as a Type 0 font. If you are loading a font for AcroForm, then use the 3-parameter constructor instead.- Parameters:
doc- The PDF document that will hold the embedded font.file- A TrueType font.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font file.
-
load
Loads a TTF to be embedded and subset into a document as a Type 0 font. If you are loading a font for AcroForm, then use the 3-parameter constructor instead.- Parameters:
doc- The PDF document that will hold the embedded font.input- An input stream of a TrueType font. It will be closed before returning.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font stream.
-
load
public static PDType0Font load(PDDocument doc, InputStream input, boolean embedSubset) throws IOException Loads a TTF to be embedded into a document as a Type 0 font.- Parameters:
doc- The PDF document that will hold the embedded font.input- A TrueType font.embedSubset- True if the font will be subset before embedding- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font stream.
-
load
public static PDType0Font load(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, boolean embedSubset) throws IOException Loads a TTF to be embedded into a document as a Type 0 font.- Parameters:
doc- The PDF document that will hold the embedded font.ttf- A TrueType font.embedSubset- True if the font will be subset before embedding- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font stream.
-
loadVertical
Loads a TTF to be embedded into a document as a vertical Type 0 font.- Parameters:
doc- The PDF document that will hold the embedded font.file- A TrueType font.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font file.
-
loadVertical
Loads a TTF to be embedded into a document as a vertical Type 0 font.- Parameters:
doc- The PDF document that will hold the embedded font.input- A TrueType font.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font stream.
-
loadVertical
public static PDType0Font loadVertical(PDDocument doc, InputStream input, boolean embedSubset) throws IOException Loads a TTF to be embedded into a document as a vertical Type 0 font.- Parameters:
doc- The PDF document that will hold the embedded font.input- A TrueType font.embedSubset- True if the font will be subset before embedding- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font stream.
-
loadVertical
public static PDType0Font loadVertical(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, boolean embedSubset) throws IOException Loads a TTF to be embedded into a document as a vertical Type 0 font.- Parameters:
doc- The PDF document that will hold the embedded font.ttf- A TrueType font.embedSubset- True if the font will be subset before embedding- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
IOException- If there is an error reading the font stream.
-
addToSubset
public void addToSubset(int codePoint) Description copied from interface:SubsettableAdds the given Unicode point to the subset.- Specified by:
addToSubsetin interfaceSubsettable- Parameters:
codePoint- Unicode code point
-
subset
Description copied from interface:SubsettableReplaces this font with a subset containing only the given Unicode characters.- Specified by:
subsetin interfaceSubsettable- Throws:
IOException- if the subset could not be written
-
willBeSubset
public boolean willBeSubset()- Specified by:
willBeSubsetin interfaceSubsettable- Returns:
- true if this font will be subset when embedded.
-
getBaseFont
Returns the PostScript name of the font. -
getDescendantFont
Returns the descendant font. -
getCMap
public org.apache.fontbox.cmap.CMap getCMap()Returns the font's CMap. -
getCMapUCS2
public org.apache.fontbox.cmap.CMap getCMapUCS2()Returns the font's UCS2 CMap, only present this font uses a predefined CMap. -
getFontDescriptor
Description copied from interface:PDFontLikeReturns the font descriptor, may be null.- Specified by:
getFontDescriptorin interfacePDFontLike- Overrides:
getFontDescriptorin classPDFont
-
getFontMatrix
Description copied from interface:PDFontLikeReturns the font matrix, which represents the transformation from glyph space to text space.- Specified by:
getFontMatrixin interfacePDFontLike- Overrides:
getFontMatrixin classPDFont
-
isVertical
public boolean isVertical()Description copied from class:PDFontReturns true if the font uses vertical writing mode.- Specified by:
isVerticalin classPDFont
-
getHeight
Description 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. ThePDFontLike.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:
IOException
-
encode
Description copied from class:PDFontEncodes 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.
- Specified by:
encodein classPDFont- Parameters:
unicode- Unicode code point.- Returns:
- Array of 1 to 4 PDF content stream bytes.
- Throws:
IOException- If the text could not be encoded.
-
hasExplicitWidth
Description copied from interface:PDFontLikeReturns true if the Font dictionary specifies an explicit width for the given glyph. This includes Width, W but not default widths entries.- Specified by:
hasExplicitWidthin interfacePDFontLike- Parameters:
code- character code- Throws:
IOException- if the font could not be read
-
getAverageFontWidth
public float getAverageFontWidth()Description copied from class:PDFontThis will get the average font width for all characters.- Specified by:
getAverageFontWidthin interfacePDFontLike- Overrides:
getAverageFontWidthin classPDFont- Returns:
- The width is in 1000 unit of text space, ie 333 or 777
-
getPositionVector
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- Overrides:
getPositionVectorin classPDFont- Parameters:
code- character code- Returns:
- position vector
-
getDisplacement
Description copied from class:PDFontReturns 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.- Overrides:
getDisplacementin classPDFont- Parameters:
code- character code- Returns:
- displacement vector
- Throws:
IOException
-
getWidth
Description 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- Overrides:
getWidthin classPDFont- Parameters:
code- character code- Throws:
IOException
-
getStandard14Width
protected float getStandard14Width(int code) Description copied from class:PDFontReturns the glyph width from the AFM if this is a Standard 14 font.- Specified by:
getStandard14Widthin classPDFont- Parameters:
code- character code- Returns:
- width in 1/1000 text space
-
getWidthFromFont
Description 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:
IOException- if the font could not be read
-
isEmbedded
public boolean isEmbedded()Description copied from interface:PDFontLikeReturns true if the font file is embedded in the PDF.- Specified by:
isEmbeddedin interfacePDFontLike
-
toUnicode
Description copied from class:PDFontReturns the Unicode character sequence which corresponds to the given character code.- Overrides:
toUnicodein classPDFont- Parameters:
code- character code- Returns:
- Unicode character(s)
- Throws:
IOException
-
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
Description copied from interface:PDFontLikeReturns the font's bounding box.- Specified by:
getBoundingBoxin interfacePDFontLike- Throws:
IOException
-
readCode
Description copied from class:PDFontReads a character code from a content stream string. Codes may be up to 4 bytes long.- Specified by:
readCodein classPDFont- Parameters:
in- string stream- Returns:
- character code
- Throws:
IOException- if the CMap or stream cannot be read
-
codeToCID
public int codeToCID(int code) Returns the CID for the given character code. If not found then CID 0 is returned.- Parameters:
code- character code- Returns:
- CID
-
codeToGID
Returns the GID for the given character code.- Parameters:
code- character code- Returns:
- GID
- Throws:
IOException
-
isStandard14
public boolean isStandard14()Description copied from class:PDFontReturns true if this font is one of the "Standard 14" fonts and receives special handling.- Overrides:
isStandard14in classPDFont
-
isDamaged
public boolean isDamaged()Description copied from interface:PDFontLikeReturns true if the embedded font file is damaged.- Specified by:
isDamagedin interfacePDFontLike
-
isOriginalEmbeddedMissing
public boolean isOriginalEmbeddedMissing()Description copied from interface:PDFontLikeReturns true if the font file that was supposed to be embedded in the PDF is missing.- Specified by:
isOriginalEmbeddedMissingin interfacePDFontLike
-
isMappingFallbackUsed
public boolean isMappingFallbackUsed()Description copied from interface:PDFontLikeReturns true if a substitute font was searched via font mappings, but an exact match was not found, and a fallback was used- Specified by:
isMappingFallbackUsedin interfacePDFontLike
-
toString
-
getPath
Description copied from interface:PDVectorFontReturns the glyph path for the given character code.- Specified by:
getPathin interfacePDVectorFont- Parameters:
code- character code in a PDF. Not to be confused with unicode.- Throws:
IOException- if the font could not be read
-
hasGlyph
Description copied from interface:PDVectorFontReturns true if this font contains a glyph for the given character code.- Specified by:
hasGlyphin interfacePDVectorFont- Parameters:
code- character code in a PDF. Not to be confused with unicode.- Throws:
IOException
-