Top Description Inners Fields Constructors Methods
de.rototor.pdfbox.graphics2d

public Class PdfBoxGraphics2DFontTextDrawer

extends Object
implements IPdfBoxGraphics2DFontTextDrawer, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer
Known Direct Subclasses
de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawerDefaultFonts
Imports
java.awt.BasicStroke, .Color, .Font, .FontFormatException, .FontMetrics, .Graphics, .Paint, java.awt.font.FontRenderContext, .LineMetrics, .TextAttribute, .TransformAttribute, java.awt.geom.AffineTransform, .Point2D, .Rectangle2D, java.io.Closeable, .File, .FileOutputStream, .IOException, .InputStream, java.text.AttributedCharacterIterator, .CharacterIterator, java.util.ArrayList, .HashMap, .List, .Locale, .Map, java.util.logging.Level, java.util.logging.Logger, org.apache.fontbox.ttf.TrueTypeCollection, .TrueTypeFont, org.apache.pdfbox.io.IOUtils, org.apache.pdfbox.pdmodel.PDPageContentStream, org.apache.pdfbox.pdmodel.font.PDFont, .PDType0Font, .PDType1Font, org.apache.pdfbox.util.Matrix

Default implementation to draw fonts. You can reuse instances of this class within a PDDocument for more then one PdfBoxGraphics2D.

Just ensure that you call close after you closed the PDDocument to free any temporary files.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static interface

Field Summary

Modifier and TypeField and Description
private static final boolean
private PDFont
private final List<PdfBoxGraphics2DFontTextDrawer.FontEntry>
private final Map<String, PDFont>
private static final java.util.logging.Logger
private final List<File>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private PDFont
public boolean
public void
close()

Implements java.io.Closeable.close.

Close / delete all resources associated with this drawer.

public void
private PDFont
public FontMetrics
private float
getSensibleThickness(float thickness, Font font)

protected boolean

Returns:

true if the font mapping is populated on demand. This is usually only the case if this class has been derived. The default implementation just checks for this.
hasDynamicFontMapping
()

private boolean
protected PDFont

Returns:

the PDFont or null if none can be found.
mapFont
(final Font
the java.awt.Font for which a mapping should be found
font
,
final IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv
environment of the font mapper
env
)

Try to map the java.awt.Font to a PDFont.

public void
registerFont(String
the name of the font to use. If null, the name is taken from the font.
fontName
,
InputStream
the input stream of the font. This file must be a ttf/otf file! You have to close the stream outside, this method will not close the stream.
fontStream
)

Register a font.

public void
registerFont(String
the name of the font to use. If null, the name is taken from the font.
fontName
,
File
the font file. This file must exist for the live time of this object, as the font data will be read lazy on demand
fontFile
)

Register a font.

public void
registerFont(File
the font file
fontFile
)

Override for registerFont(null,fontFile)

public void
registerFont(InputStream
the font file
fontStream
)

Override for registerFont(null,fontStream)

public void
registerFont(String
the name of the font as returned by java.awt.Font#getFontName(). This name is used for the mapping the java.awt.Font to this PDFont.
name
,
PDFont
the PDFont to use. This font must be loaded in the current document.
font
)

Register a font which is already associated with the PDDocument

private void
showTextOnStream(final IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, final Font attributeFont, final PDFont font, final boolean isStrikeThrough, final boolean isUnderline, boolean isLigatures, PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState drawState, final Paint paint, String text)

private PDType0Font
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEBUG_BOXback to summary
private static final boolean DEBUG_BOX
fallbackFontUnknownEncodingsback to summary
private PDFont fallbackFontUnknownEncodings
fontFilesback to summary
private final List<PdfBoxGraphics2DFontTextDrawer.FontEntry> fontFiles
fontMapback to summary
private final Map<String, PDFont> fontMap
LOGGERback to summary
private static final java.util.logging.Logger LOGGER
tempFilesback to summary
private final List<File> tempFiles

Constructor Detail

PdfBoxGraphics2DFontTextDrawerback to summary
public PdfBoxGraphics2DFontTextDrawer()

Method Detail

applyFontback to summary
private PDFont applyFont(Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException
canDrawTextback to summary
public boolean canDrawText(AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException

Implements de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.canDrawText.

Parameters
iterator:AttributedCharacterIterator

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.canDrawText.

Has the text and all its properties

env:IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.canDrawText.

Environment

Returns:boolean

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.canDrawText.

true when the given text can be fully drawn using fonts. return false to have the text drawn as vector shapes

Annotations
@Override
Exceptions
IOException:

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.canDrawText.

when a font can not be loaded or a paint can't be applied.

FontFormatException:

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.canDrawText.

when the font file can not be loaded

closeback to summary
public void close()

Implements java.io.Closeable.close.

Close / delete all resources associated with this drawer. This mainly means deleting all temporary files. You can not use this object after a call to close.

Calling close multiple times does nothing.

Annotations
@SuppressWarnings:ResultOfMethodCallIgnored
@Override
drawTextback to summary
public void drawText(AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException

Implements de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.drawText.

Parameters
iterator:AttributedCharacterIterator

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.drawText.

The text with all properties

env:IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.drawText.

Environment

Annotations
@Override
Exceptions
IOException:

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.drawText.

when a font can not be loaded or a paint can't be applied.

FontFormatException:

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.drawText.

when the font file can not be loaded

findFallbackFontback to summary
private PDFont findFallbackFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException
getFontMetricsback to summary
public FontMetrics getFontMetrics(final Font f, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException

Implements de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.getFontMetrics.

Parameters
f:Font

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.getFontMetrics.

the Font

env:IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.getFontMetrics.

the Environment

Returns:FontMetrics

Doc from de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer.getFontMetrics.

the resulting font metrics

Annotations
@Override
getSensibleThicknessback to summary
private float getSensibleThickness(float thickness, Font font)
hasDynamicFontMappingback to summary
protected boolean hasDynamicFontMapping()
Returns:boolean

true if the font mapping is populated on demand. This is usually only the case if this class has been derived. The default implementation just checks for this.

Annotations
@SuppressWarnings:WeakerAccess
iterateRunback to summary
private boolean iterateRun(AttributedCharacterIterator iterator, StringBuilder sb)
mapFontback to summary
protected PDFont mapFont(final Font font, final IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException

Try to map the java.awt.Font to a PDFont.

Parameters
font:Font

the java.awt.Font for which a mapping should be found

env:IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv

environment of the font mapper

Returns:PDFont

the PDFont or null if none can be found.

Annotations
@SuppressWarnings:WeakerAccess
Exceptions
IOException:
when the font can not be loaded
FontFormatException:
when the font file can not be loaded
registerFontback to summary
public void registerFont(String fontName, InputStream fontStream) throws IOException

Register a font. If possible, try to use a font file, i.e. registerFont(String, File). This method will lead to the creation of a temporary file which stores the font data.

Parameters
fontName:String

the name of the font to use. If null, the name is taken from the font.

fontStream:InputStream

the input stream of the font. This file must be a ttf/otf file! You have to close the stream outside, this method will not close the stream.

Annotations
@SuppressWarnings:WeakerAccess
Exceptions
IOException:
when something goes wrong with reading the font or writing the font to the content stream of the PDF:
registerFontback to summary
public void registerFont(String fontName, File fontFile)

Register a font.

Parameters
fontName:String

the name of the font to use. If null, the name is taken from the font.

fontFile:File

the font file. This file must exist for the live time of this object, as the font data will be read lazy on demand

Annotations
@SuppressWarnings:WeakerAccess
registerFontback to summary
public void registerFont(File fontFile)

Override for registerFont(null,fontFile)

Parameters
fontFile:File

the font file

Annotations
@SuppressWarnings:WeakerAccess
registerFontback to summary
public void registerFont(InputStream fontStream) throws IOException

Override for registerFont(null,fontStream)

Parameters
fontStream:InputStream

the font file

Annotations
@SuppressWarnings:WeakerAccess
Exceptions
IOException:
when something goes wrong with reading the font or writing the font to the content stream of the PDF:
registerFontback to summary
public void registerFont(String name, PDFont font)

Register a font which is already associated with the PDDocument

Parameters
name:String

the name of the font as returned by java.awt.Font#getFontName(). This name is used for the mapping the java.awt.Font to this PDFont.

font:PDFont

the PDFont to use. This font must be loaded in the current document.

Annotations
@SuppressWarnings:WeakerAccess
showTextOnStreamback to summary
private void showTextOnStream(final IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, final Font attributeFont, final PDFont font, final boolean isStrikeThrough, final boolean isUnderline, boolean isLigatures, PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState drawState, final Paint paint, String text) throws IOException
tryToLoadFontback to summary
private PDType0Font tryToLoadFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, File foundFontFile)
de.rototor.pdfbox.graphics2d back to summary

private Class PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState

extends Object
Class Inheritance
  • java.lang.Object
  • de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState

Field Summary

Modifier and TypeField and Description
private final PDPageContentStream
private Matrix
pack-priv final List<PdfBoxGraphics2DFontTextDrawer.ITextDecorationDrawer>
private double
private double
private AffineTransform

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
applyTextWidth(float stringWidth)

public void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contentStreamback to summary
private final PDPageContentStream contentStream
currentMatrixback to summary
private Matrix currentMatrix
drawersback to summary
pack-priv final List<PdfBoxGraphics2DFontTextDrawer.ITextDecorationDrawer> drawers
fontOffsetYback to summary
private double fontOffsetY
scaleback to summary
private double scale
textMatrixback to summary
private AffineTransform textMatrix

Constructor Detail

DrawTextDecorationStateback to summary
public DrawTextDecorationState(PDPageContentStream contentStream) throws IOException

Method Detail

applyTextWidthback to summary
public void applyTextWidth(float stringWidth)
setAttributeMatrixback to summary
public void setAttributeMatrix(AffineTransform fontMatrix) throws IOException
setCurrentTextMatrixback to summary
private void setCurrentTextMatrix(AffineTransform newAT) throws IOException
de.rototor.pdfbox.graphics2d back to summary

private Class PdfBoxGraphics2DFontTextDrawer.FontEntry

extends Object
Class Inheritance
  • java.lang.Object
  • de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer.FontEntry

Field Summary

Modifier and TypeField and Description
pack-priv File
pack-priv String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fileback to summary
pack-priv File file
overrideNameback to summary
pack-priv String overrideName

Constructor Detail

FontEntryback to summary
private FontEntry()
de.rototor.pdfbox.graphics2d back to summary

private Interface PdfBoxGraphics2DFontTextDrawer.ITextDecorationDrawer


Method Summary

Modifier and TypeMethod and Description
public void

Method Detail

drawback to summary
public void draw(PDPageContentStream stream) throws IOException