Top Description Fields Constructors Methods
org.apache.pdfbox.pdmodel.font

public Class PDType1Font

extends PDSimpleFont
implements PDVectorFont
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.pdmodel.font.PDVectorFont
Known Direct Subclasses
org.apache.pdfbox.pdmodel.font.PDMMType1Font
Imports
java.awt.geom.AffineTransform, .GeneralPath, .Point2D, java.io.IOException, .InputStream, java.util.Arrays, .HashMap, .List, .Map, org.apache.commons.logging.Log, .LogFactory, org.apache.fontbox.EncodedFont, .FontBoxFont, org.apache.fontbox.type1.DamagedFontException, .Type1Font, org.apache.fontbox.util.BoundingBox, org.apache.pdfbox.cos.COSDictionary, .COSName, .COSStream, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.common.PDRectangle, .PDStream, org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName, org.apache.pdfbox.pdmodel.font.encoding.Encoding, .StandardEncoding, .Type1Encoding, .WinAnsiEncoding, .ZapfDingbatsEncoding, .SymbolEncoding, org.apache.pdfbox.util.Matrix

A PostScript Type 1 Font.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private static final Map<String, String>
private final Map<Integer, byte[]>
codeToBytesMap

to improve encoding speed.

private BoundingBox
private Matrix
private final AffineTransform
private final FontBoxFont
genericFont

embedded or system font for rendering.

private final boolean
private final boolean
private static final Log
private static final int
private final Type1Font
type1font

embedded font.

Inherited from org.apache.pdfbox.pdmodel.font.PDSimpleFont:
encodingglyphList

Constructor Summary

AccessConstructor and Description
public
PDType1Font(Standard14Fonts.FontName
One of the standard 14 PostScript names, e.g. FontName#HELVETICA_BOLD.
baseFont
)

Creates a Type 1 standard 14 font for embedding.

public
PDType1Font(PDDocument
PDF document to write to
doc
,
InputStream
PFB file stream
pfbIn
)

Creates a new Type 1 font for embedding.

public
PDType1Font(PDDocument
PDF document to write to
doc
,
InputStream
PFB file stream
pfbIn
,
Encoding
encoding to be used for the font
encoding
)

Creates a new Type 1 font for embedding.

public
PDType1Font(COSDictionary
font dictionary.
fontDictionary
)

Creates a Type 1 font from a Font dictionary in a PDF.

Method Summary

Modifier and TypeMethod and Description
public String
codeToName(int code)

protected byte[]
encode(int
Unicode code point.
unicode
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.encode.

Encodes the given Unicode code point for use in a PDF content stream.

private static int
findBinaryOffsetAfterExec(byte[] bytes, int startOffset)

private BoundingBox
public float
public final String

Returns:

the PostScript name of the font
getBaseFont
()

Returns the PostScript name of the font.

public BoundingBox
getBoundingBox()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getBoundingBox.

Returns the font's bounding box.

public FontBoxFont
getFontBoxFont()

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontBoxFont.

Returns the embedded or system font used for rendering.

public final Matrix
getFontMatrix()

Overrides org.apache.pdfbox.pdmodel.font.PDFont.getFontMatrix.

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getFontMatrix.

Returns the font matrix, which represents the transformation from glyph space to text space.

public float
getHeight(int
character code
code
)

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getHeight.

Returns the height of the given character, in glyph space.

public String
getName()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getName.

Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".

private String
getNameInFont(String name)

Maps a PostScript glyph name to the name in the underlying font, for example when using a TTF font we might map "W" to "uni0057".

public GeneralPath
getNormalizedPath(int
character code in a PDF. Not to be confused with unicode.
code
)

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getNormalizedPath.

Returns the normalized glyph path for the given character code in a PDF.

public GeneralPath
getPath(String
glyph name
name
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getPath.

Returns the path for the character with the given name.

public GeneralPath
getPath(int
character code in a PDF. Not to be confused with unicode.
code
)

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getPath.

Returns the glyph path for the given character code.

public Type1Font

Returns:

the embedded or substituted Type 1 font or null
getType1Font
()

Returns the embedded or substituted Type 1 font, or null if there is none.

public float
getWidthFromFont(int
character code
code
)

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getWidthFromFont.

Returns the width of a glyph in the embedded font file.

public boolean
hasGlyph(String
glyph name
name
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.hasGlyph.

Returns true if the font contains the character with the given name.

public boolean
hasGlyph(int
character code in a PDF. Not to be confused with unicode.
code
)

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.hasGlyph.

Returns true if this font contains a glyph for the given character code in a PDF.

public boolean
isDamaged()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.isDamaged.

Returns true if the embedded font file is damaged.

public boolean
isEmbedded()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.isEmbedded.

Returns true if the font file is embedded in the PDF.

public int
readCode(InputStream
string stream
in
)

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.readCode.

Reads a character code from a content stream string.

protected Encoding
readEncodingFromFont()

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.readEncodingFromFont.

Called by readEncoding() if the encoding needs to be extracted from the font file.

private int

Returns:

repaired Length1 value
repairLength1
(byte[]
Type 1 stream bytes
bytes
,
int
Length1 from the Type 1 stream
length1
)

Some Type 1 fonts have an invalid Length1, which causes the binary segment of the font to be truncated, see PDFBOX-2350, PDFBOX-3677.

private int

Returns:

repaired Length2 value
repairLength2
(byte[]
Type 1 stream bytes
bytes
,
int
Length1 from the Type 1 stream
length1
,
int
Length2 from the Type 1 stream
length2
)

Some Type 1 fonts have an invalid Length2, see PDFBOX-3475.

Inherited from org.apache.pdfbox.pdmodel.font.PDSimpleFont:
addToSubsetgetEncodinggetGlyphListgetStandard14WidthgetSymbolicFlaghasExplicitWidthisFontSymbolicisNonZeroBoundingBoxisStandard14isSymbolicisVerticalreadEncodingsubsettoUnicodetoUnicodewillBeSubset

Field Detail

ALT_NAMESback to summary
private static final Map<String, String> ALT_NAMES
codeToBytesMapback to summary
private final Map<Integer, byte[]> codeToBytesMap

to improve encoding speed.

fontBBoxback to summary
private BoundingBox fontBBox
fontMatrixback to summary
private Matrix fontMatrix
fontMatrixTransformback to summary
private final AffineTransform fontMatrixTransform
genericFontback to summary
private final FontBoxFont genericFont

embedded or system font for rendering.

isDamagedback to summary
private final boolean isDamaged
isEmbeddedback to summary
private final boolean isEmbedded
LOGback to summary
private static final Log LOG

Hides org.apache.pdfbox.pdmodel.font.PDSimpleFont.LOG.

PFB_START_MARKERback to summary
private static final int PFB_START_MARKER
type1fontback to summary
private final Type1Font type1font

embedded font.

Constructor Detail

PDType1Fontback to summary
public PDType1Font(Standard14Fonts.FontName baseFont)

Creates a Type 1 standard 14 font for embedding.

Parameters
baseFont:Standard14Fonts.FontName

One of the standard 14 PostScript names, e.g. FontName#HELVETICA_BOLD.

PDType1Fontback to summary
public PDType1Font(PDDocument doc, InputStream pfbIn) throws IOException

Creates a new Type 1 font for embedding.

Parameters
doc:PDDocument

PDF document to write to

pfbIn:InputStream

PFB file stream

Exceptions
IOException:
if the font could not be read
PDType1Fontback to summary
public PDType1Font(PDDocument doc, InputStream pfbIn, Encoding encoding) throws IOException

Creates a new Type 1 font for embedding.

Parameters
doc:PDDocument

PDF document to write to

pfbIn:InputStream

PFB file stream

encoding:Encoding

encoding to be used for the font

Exceptions
IOException:
if the font could not be read
PDType1Fontback to summary
public PDType1Font(COSDictionary fontDictionary) throws IOException

Creates a Type 1 font from a Font dictionary in a PDF.

Parameters
fontDictionary:COSDictionary

font dictionary.

Exceptions
IOException:
if there was an error initializing the font.
IllegalArgumentException:
if /FontFile3 was used.

Method Detail

codeToNameback to summary
public String codeToName(int code) throws IOException
encodeback to summary
protected byte[] encode(int unicode) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.encode.

Doc from org.apache.pdfbox.pdmodel.font.PDFont.encode.

Encodes 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:int

Unicode code point.

Returns:byte[]

Array of 1 to 4 PDF content stream bytes.

Annotations
@Override
Exceptions
IOException:
If the text could not be encoded.
findBinaryOffsetAfterExecback to summary
private static int findBinaryOffsetAfterExec(byte[] bytes, int startOffset)
generateBoundingBoxback to summary
private BoundingBox generateBoundingBox() throws IOException
getAverageFontWidthback to summary
public float getAverageFontWidth()

Overrides org.apache.pdfbox.pdmodel.font.PDFont.getAverageFontWidth.

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getAverageFontWidth.

Doc from org.apache.pdfbox.pdmodel.font.PDFont.getAverageFontWidth.

This will get the average font width for all characters.

Returns:float

The width is in 1000 unit of text space, ie 333 or 777

Annotations
@Override
getBaseFontback to summary
public final String getBaseFont()

Returns the PostScript name of the font.

Returns:String

the PostScript name of the font

getBoundingBoxback to summary
public BoundingBox getBoundingBox() throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getBoundingBox.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getBoundingBox.

Returns the font's bounding box.

Returns:BoundingBox

the bounding box

Annotations
@Override
Exceptions
IOException:
if the bounding box could not be read
getFontBoxFontback to summary
public FontBoxFont getFontBoxFont()

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontBoxFont.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontBoxFont.

Returns the embedded or system font used for rendering. This is never null.

Returns:FontBoxFont

the embedded or system font used for rendering

Annotations
@Override
getFontMatrixback to summary
public final Matrix getFontMatrix()

Overrides org.apache.pdfbox.pdmodel.font.PDFont.getFontMatrix.

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getFontMatrix.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getFontMatrix.

Returns the font matrix, which represents the transformation from glyph space to text space.

Returns:Matrix

the font matrix

Annotations
@Override
getHeightback to summary
public float getHeight(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getHeight.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getHeight.

Returns 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 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.

Parameters
code:int

character code

Returns:float

the height of the given character

Annotations
@Override
Exceptions
IOException:
if the height could not be read
getNameback to summary
public String getName()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getName.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getName.

Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".

Returns:String

the name of the font

Annotations
@Override
getNameInFontback to summary
private String getNameInFont(String name) throws IOException

Maps a PostScript glyph name to the name in the underlying font, for example when using a TTF font we might map "W" to "uni0057".

getNormalizedPathback to summary
public GeneralPath getNormalizedPath(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getNormalizedPath.

Doc from org.apache.pdfbox.pdmodel.font.PDVectorFont.getNormalizedPath.

Returns 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.

Parameters
code:int

character code in a PDF. Not to be confused with unicode.

Returns:GeneralPath

the normalized glyph path for the given character code

Annotations
@Override
Exceptions
IOException:
if the font could not be read
getPathback to summary
public GeneralPath getPath(String name) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.getPath.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.getPath.

Returns the path for the character with the given name. For some fonts, GIDs may be used instead of names when calling this method. *

Parameters
name:String

glyph name

Returns:GeneralPath

glyph path of the character with the given name

Annotations
@Override
Exceptions
IOException:
if the path could not be read
getPathback to summary
public GeneralPath getPath(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.getPath.

Doc from org.apache.pdfbox.pdmodel.font.PDVectorFont.getPath.

Returns the glyph path for the given character code.

Parameters
code:int

character code in a PDF. Not to be confused with unicode.

Returns:GeneralPath

the glyph path for the given character code

Annotations
@Override
Exceptions
IOException:
if the font could not be read
getType1Fontback to summary
public Type1Font getType1Font()

Returns the embedded or substituted Type 1 font, or null if there is none.

Returns:Type1Font

the embedded or substituted Type 1 font or null

getWidthFromFontback to summary
public float getWidthFromFont(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.getWidthFromFont.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.getWidthFromFont.

Returns the width of a glyph in the embedded font file.

Parameters
code:int

character code

Returns:float

width in glyph space

Annotations
@Override
Exceptions
IOException:
if the font could not be read
hasGlyphback to summary
public boolean hasGlyph(String name) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.hasGlyph.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.hasGlyph.

Returns true if the font contains the character with the given name.

Parameters
name:String

glyph name

Returns:boolean

true if the font contains the character with the given name

Annotations
@Override
Exceptions
IOException:
if the path could not be read
hasGlyphback to summary
public boolean hasGlyph(int code) throws IOException

Implements org.apache.pdfbox.pdmodel.font.PDVectorFont.hasGlyph.

Doc from org.apache.pdfbox.pdmodel.font.PDVectorFont.hasGlyph.

Returns true if this font contains a glyph for the given character code in a PDF.

Parameters
code:int

character code in a PDF. Not to be confused with unicode.

Returns:boolean

true if this font contains a glyph for the given character code

Annotations
@Override
Exceptions
IOException:
if the font could not be read
isDamagedback to summary
public boolean isDamaged()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.isDamaged.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.isDamaged.

Returns true if the embedded font file is damaged.

Returns:boolean

true if the embedded font file is damaged

Annotations
@Override
isEmbeddedback to summary
public boolean isEmbedded()

Implements org.apache.pdfbox.pdmodel.font.PDFontLike.isEmbedded.

Doc from org.apache.pdfbox.pdmodel.font.PDFontLike.isEmbedded.

Returns true if the font file is embedded in the PDF.

Returns:boolean

true if the font file is embedded in the PDF

Annotations
@Override
readCodeback to summary
public int readCode(InputStream in) throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDFont.readCode.

Doc from org.apache.pdfbox.pdmodel.font.PDFont.readCode.

Reads a character code from a content stream string. Codes may be up to 4 bytes long.

Parameters
in:InputStream

string stream

Returns:int

character code

Annotations
@Override
Exceptions
IOException:
if the CMap or stream cannot be read
readEncodingFromFontback to summary
protected Encoding readEncodingFromFont() throws IOException

Implements abstract org.apache.pdfbox.pdmodel.font.PDSimpleFont.readEncodingFromFont.

Doc from org.apache.pdfbox.pdmodel.font.PDSimpleFont.readEncodingFromFont.

Called by readEncoding() if the encoding needs to be extracted from the font file.

Returns:Encoding

encoding of the font

Annotations
@Override
Exceptions
IOException:
if the font file could not be read.
repairLength1back to summary
private int repairLength1(byte[] bytes, int length1)

Some Type 1 fonts have an invalid Length1, which causes the binary segment of the font to be truncated, see PDFBOX-2350, PDFBOX-3677.

Parameters
bytes:byte[]

Type 1 stream bytes

length1:int

Length1 from the Type 1 stream

Returns:int

repaired Length1 value

repairLength2back to summary
private int repairLength2(byte[] bytes, int length1, int length2)

Some Type 1 fonts have an invalid Length2, see PDFBOX-3475. A negative /Length2 brings an IllegalArgumentException in Arrays.copyOfRange(), a huge value eats up memory because of padding.

Parameters
bytes:byte[]

Type 1 stream bytes

length1:int

Length1 from the Type 1 stream

length2:int

Length2 from the Type 1 stream

Returns:int

repaired Length2 value