Class PDType0Font
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.font.PDFont
-
- org.apache.pdfbox.pdmodel.font.PDType0Font
-
- All Implemented Interfaces:
COSObjectable,PDFontLike,PDVectorFont
public class PDType0Font extends PDFont implements PDVectorFont
A Composite (Type 0) font.
-
-
Field Summary
Fields Modifier and Type Field Description private CMapcMapprivate CmapLookupcmapLookupprivate CMapcMapUCS2private PDCIDFontdescendantFontprivate PDCIDFontType2Embedderembedderprivate GsubDatagsubDataprivate booleanisCMapPredefinedprivate booleanisDescendantCJKprivate static org.apache.commons.logging.LogLOGprivate java.util.Set<java.lang.Integer>noUnicodeprivate TrueTypeFontttf-
Fields inherited from class org.apache.pdfbox.pdmodel.font.PDFont
DEFAULT_FONT_MATRIX, dict
-
-
Constructor Summary
Constructors Modifier Constructor Description PDType0Font(COSDictionary fontDictionary)Constructor for reading a Type0 font from a PDF file.privatePDType0Font(PDDocument document, TrueTypeFont ttf, boolean embedSubset, boolean closeTTF, boolean vertical)Private.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddGlyphsToSubset(java.util.Set<java.lang.Integer> glyphIds)voidaddToSubset(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.byte[]encodeGlyphId(int glyphId)Returns the encoded value for the given glyph ID.private voidfetchCMapUCS2()Fetches the corresponding UCS2 CMap if the font's CMap is predefined.floatgetAverageFontWidth()This will get the average font width for all characters.java.lang.StringgetBaseFont()Returns the PostScript name of the font.BoundingBoxgetBoundingBox()Returns the font's bounding box.CMapgetCMap()Returns the font's CMap.CmapLookupgetCmapLookup()Returns the CMap lookup table if present.CMapgetCMapUCS2()Returns the font's UCS2 CMap, only present this font uses a predefined CMap.PDCIDFontgetDescendantFont()Returns the descendant font.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.GsubDatagetGsubData()Returns the GSubData if present.floatgetHeight(int code)Returns the height of the given character, in glyph space.java.lang.StringgetName()Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".java.awt.geom.GeneralPathgetNormalizedPath(int code)Returns the normalized glyph path for the given character code in a PDF.java.awt.geom.GeneralPathgetPath(int code)Returns the glyph path for the given character code.VectorgetPositionVector(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 in a PDF.booleanisDamaged()Returns true if the embedded font file is damaged.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.booleanisVertical()Returns true if the font uses vertical writing mode.static PDType0Fontload(PDDocument doc, java.io.File file)Loads a TTF to be embedded and subset into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, java.io.InputStream input)Loads a TTF to be embedded and subset into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, java.io.InputStream input, boolean embedSubset)Loads a TTF to be embedded into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, TrueTypeFont ttf, boolean embedSubset)Loads a TTF to be embedded into a document as a Type 0 font.static PDType0Fontload(PDDocument doc, RandomAccessRead randomAccessRead, boolean embedSubset, boolean vertical)Loads a TTF to be embedded into a document as a Type 0 font.static PDType0FontloadVertical(PDDocument doc, java.io.File file)Loads a TTF to be embedded into a document as a vertical Type 0 font.static PDType0FontloadVertical(PDDocument doc, java.io.InputStream input)Loads a TTF to be embedded into a document as a vertical Type 0 font.static PDType0FontloadVertical(PDDocument doc, java.io.InputStream input, boolean embedSubset)Loads a TTF to be embedded into a document as a vertical Type 0 font.static PDType0FontloadVertical(PDDocument doc, TrueTypeFont ttf, boolean embedSubset)Loads a TTF to be embedded into a document as a vertical Type 0 font.intreadCode(java.io.InputStream in)Reads a character code from a content stream string.private voidreadEncoding()Reads the font's Encoding entry, which should be a CMap name/stream.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.booleanwillBeSubset()Returns true if this font will be subset when embedded.-
Methods inherited from class org.apache.pdfbox.pdmodel.font.PDFont
encode, equals, getCOSObject, getSpaceWidth, getStandard14AFM, getStringWidth, getSubType, getToUnicodeCMap, getType, getWidths, hashCode, readCMap, setFontDescriptor, toUnicode
-
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
descendantFont
private final PDCIDFont descendantFont
-
noUnicode
private final java.util.Set<java.lang.Integer> noUnicode
-
gsubData
private final GsubData gsubData
-
cmapLookup
private final CmapLookup cmapLookup
-
cMap
private CMap cMap
-
cMapUCS2
private CMap cMapUCS2
-
isCMapPredefined
private boolean isCMapPredefined
-
isDescendantCJK
private boolean isDescendantCJK
-
embedder
private PDCIDFontType2Embedder embedder
-
ttf
private TrueTypeFont ttf
-
-
Constructor Detail
-
PDType0Font
public PDType0Font(COSDictionary fontDictionary) throws java.io.IOException
Constructor for reading a Type0 font from a PDF file.- Parameters:
fontDictionary- The font dictionary according to the PDF specification.- Throws:
java.io.IOException- if the descendant font is missing.
-
PDType0Font
private PDType0Font(PDDocument document, TrueTypeFont ttf, boolean embedSubset, boolean closeTTF, boolean vertical) throws java.io.IOException
Private. Creates a new PDType0Font font for embedding.- Parameters:
document-ttf-embedSubset-closeTTF- whether to close the ttf parameter after embedding. Must be true when the ttf parameter was created in the load() method, false when the ttf parameter was passed to the load() method.vertical- whether to enable vertical substitutions.- Throws:
java.io.IOException
-
-
Method Detail
-
load
public static PDType0Font load(PDDocument doc, java.io.File file) throws java.io.IOException
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:
java.io.IOException- If there is an error reading the font file.
-
load
public static PDType0Font load(PDDocument doc, java.io.InputStream input) throws java.io.IOException
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:
java.io.IOException- If there is an error reading the font stream.
-
load
public static PDType0Font load(PDDocument doc, java.io.InputStream input, boolean embedSubset) throws java.io.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- An input stream of a TrueType font. It will be closed before returning.embedSubset- True if the font will be subset before embedding. Set this to false when creating a font for AcroForm.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
java.io.IOException- If there is an error reading the font stream.
-
load
public static PDType0Font load(PDDocument doc, RandomAccessRead randomAccessRead, boolean embedSubset, boolean vertical) throws java.io.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.randomAccessRead- source of a TrueType font.embedSubset- True if the font will be subset before embedding. Set this to false when creating a font for AcroForm.vertical- whether to enable vertical substitutions.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
java.io.IOException- If there is an error reading the font stream.
-
load
public static PDType0Font load(PDDocument doc, TrueTypeFont ttf, boolean embedSubset) throws java.io.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. Set this to false when creating a font for AcroForm.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
java.io.IOException- If there is an error reading the font stream.
-
loadVertical
public static PDType0Font loadVertical(PDDocument doc, java.io.File file) throws java.io.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.file- A TrueType font.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
java.io.IOException- If there is an error reading the font file.
-
loadVertical
public static PDType0Font loadVertical(PDDocument doc, java.io.InputStream input) throws java.io.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. It will be closed before returning.- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
java.io.IOException- If there is an error reading the font stream.
-
loadVertical
public static PDType0Font loadVertical(PDDocument doc, java.io.InputStream input, boolean embedSubset) throws java.io.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. It will be closed before returning.embedSubset- True if the font will be subset before embedding- Returns:
- A Type0 font with a CIDFontType2 descendant.
- Throws:
java.io.IOException- If there is an error reading the font stream.
-
loadVertical
public static PDType0Font loadVertical(PDDocument doc, TrueTypeFont ttf, boolean embedSubset) throws java.io.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:
java.io.IOException- If there is an error reading the font stream.
-
addToSubset
public void addToSubset(int codePoint)
Description copied from class:PDFontAdds the given Unicode point to the subset.- Specified by:
addToSubsetin classPDFont- Parameters:
codePoint- Unicode code point
-
addGlyphsToSubset
public void addGlyphsToSubset(java.util.Set<java.lang.Integer> glyphIds)
-
subset
public void subset() throws java.io.IOExceptionDescription copied from class:PDFontReplaces this font with a subset containing only the given Unicode characters.
-
willBeSubset
public boolean willBeSubset()
Description copied from class:PDFontReturns true if this font will be subset when embedded.- Specified by:
willBeSubsetin classPDFont- Returns:
- true if this font will be subset when embedded
-
readEncoding
private void readEncoding() throws java.io.IOExceptionReads the font's Encoding entry, which should be a CMap name/stream.- Throws:
java.io.IOException
-
fetchCMapUCS2
private void fetchCMapUCS2() throws java.io.IOExceptionFetches the corresponding UCS2 CMap if the font's CMap is predefined.- Throws:
java.io.IOException
-
getBaseFont
public java.lang.String getBaseFont()
Returns the PostScript name of the font.- Returns:
- the PostScript name of the font
-
getDescendantFont
public PDCIDFont getDescendantFont()
Returns the descendant font.- Returns:
- the descendant font
-
getCMap
public CMap getCMap()
Returns the font's CMap.- Returns:
- the font's CMap
-
getCMapUCS2
public CMap getCMapUCS2()
Returns the font's UCS2 CMap, only present this font uses a predefined CMap.- Returns:
- the font's UCS2 CMap if present
-
getFontDescriptor
public PDFontDescriptor getFontDescriptor()
Description copied from interface:PDFontLikeReturns the font descriptor, may be null.- Specified by:
getFontDescriptorin interfacePDFontLike- Overrides:
getFontDescriptorin classPDFont- Returns:
- the font descriptor or null
-
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- Overrides:
getFontMatrixin classPDFont- Returns:
- the font matrix
-
isVertical
public boolean isVertical()
Description copied from class:PDFontReturns true if the font uses vertical writing mode.- Specified by:
isVerticalin classPDFont- Returns:
- true if the font uses vertical writing mode
-
getHeight
public 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. See the cyan rectangles in the DrawPrintTextLocations.java example to see this in action.- Specified by:
getHeightin interfacePDFontLike- Parameters:
code- character code- Returns:
- the height of the given character
- Throws:
java.io.IOException- if the height could not be read
-
encode
protected byte[] encode(int unicode) throws java.io.IOExceptionDescription 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.
-
hasExplicitWidth
public boolean hasExplicitWidth(int code) throws java.io.IOExceptionDescription 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- Returns:
- true if the Font dictionary specifies an explicit width for the given glyph
- Throws:
java.io.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
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- Overrides:
getPositionVectorin classPDFont- Parameters:
code- character code- Returns:
- position vector
-
getDisplacement
public Vector getDisplacement(int code) throws java.io.IOException
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:
java.io.IOException- if the data could not be read
-
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. See the cyan rectangles in the DrawPrintTextLocations.java example to see this in action.
- Specified by:
getWidthin interfacePDFontLike- Overrides:
getWidthin classPDFont- Parameters:
code- character code- Returns:
- the width of the given character
- Throws:
java.io.IOException- if the width could not be read
-
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
public 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 boolean isEmbedded()
Description copied from interface:PDFontLikeReturns true if the font file is embedded in the PDF.- Specified by:
isEmbeddedin interfacePDFontLike- Returns:
- true if the font file is embedded in the PDF
-
toUnicode
public java.lang.String toUnicode(int code)
Description copied from class:PDFontReturns the Unicode character sequence which corresponds to the given character code.
-
getName
public 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- Returns:
- the name of the font
-
getBoundingBox
public BoundingBox getBoundingBox() throws java.io.IOException
Description copied from interface:PDFontLikeReturns the font's bounding box.- Specified by:
getBoundingBoxin interfacePDFontLike- Returns:
- the bounding box
- Throws:
java.io.IOException- if the bounding box could not be read
-
readCode
public int readCode(java.io.InputStream in) throws java.io.IOExceptionDescription copied from class:PDFontReads a character code from a content stream string. Codes may be up to 4 bytes long.
-
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 for the given character code
-
codeToGID
public int codeToGID(int code) throws java.io.IOExceptionReturns the GID for the given character code.- Parameters:
code- character code- Returns:
- GID for the given character code
- Throws:
java.io.IOException- if the data could not be read
-
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- Returns:
- true if this font is one of the "Standard 14" fonts
-
isDamaged
public boolean isDamaged()
Description copied from interface:PDFontLikeReturns true if the embedded font file is damaged.- Specified by:
isDamagedin interfacePDFontLike- Returns:
- true if the embedded font file is damaged
-
getPath
public java.awt.geom.GeneralPath getPath(int code) throws java.io.IOExceptionDescription 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.- Returns:
- the glyph path for the given character code
- Throws:
java.io.IOException- if the font could not be read
-
getNormalizedPath
public java.awt.geom.GeneralPath getNormalizedPath(int code) throws java.io.IOExceptionDescription copied from interface:PDVectorFontReturns the normalized glyph path for the given character code in a PDF. The resulting path is normalized to the PostScript 1000 unit square, and fallback glyphs are returned where appropriate, e.g. for missing glyphs.- Specified by:
getNormalizedPathin interfacePDVectorFont- Parameters:
code- character code in a PDF. Not to be confused with unicode.- Returns:
- the normalized glyph path for the given character code
- Throws:
java.io.IOException- if the font could not be read
-
hasGlyph
public boolean hasGlyph(int code) throws java.io.IOExceptionDescription copied from interface:PDVectorFontReturns true if this font contains a glyph for the given character code in a PDF.- Specified by:
hasGlyphin interfacePDVectorFont- Parameters:
code- character code in a PDF. Not to be confused with unicode.- Returns:
- true if this font contains a glyph for the given character code
- Throws:
java.io.IOException- if the font could not be read
-
getGsubData
public GsubData getGsubData()
Returns the GSubData if present.- Returns:
- the GSubData if present
-
encodeGlyphId
public byte[] encodeGlyphId(int glyphId)
Returns the encoded value for the given glyph ID.- Parameters:
glyphId- the ID of the glyph to be encoded- Returns:
- the encoded glyph ID
-
getCmapLookup
public CmapLookup getCmapLookup()
Returns the CMap lookup table if present.- Returns:
- the CMap lookup table if present
-
-