Package de.rototor.pdfbox.graphics2d
Class PdfBoxGraphics2DFontTextDrawer
- java.lang.Object
-
- de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer
-
- All Implemented Interfaces:
IPdfBoxGraphics2DFontTextDrawer,java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
PdfBoxGraphics2DFontTextDrawerDefaultFonts
public class PdfBoxGraphics2DFontTextDrawer extends java.lang.Object implements IPdfBoxGraphics2DFontTextDrawer, java.io.Closeable
Default implementation to draw fonts. You can reuse instances of this class within a PDDocument for more then onePdfBoxGraphics2D.Just ensure that you call close after you closed the PDDocument to free any temporary files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPdfBoxGraphics2DFontTextDrawer.DrawTextDecorationStateprivate static classPdfBoxGraphics2DFontTextDrawer.FontEntryprivate static interfacePdfBoxGraphics2DFontTextDrawer.ITextDecorationDrawer-
Nested classes/interfaces inherited from interface de.rototor.pdfbox.graphics2d.IPdfBoxGraphics2DFontTextDrawer
IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv
-
-
Field Summary
Fields Modifier and Type Field Description private static booleanDEBUG_BOXprivate org.apache.pdfbox.pdmodel.font.PDFontfallbackFontUnknownEncodingsprivate java.util.List<PdfBoxGraphics2DFontTextDrawer.FontEntry>fontFilesprivate java.util.Map<java.lang.String,org.apache.pdfbox.pdmodel.font.PDFont>fontMapprivate static java.util.logging.LoggerLOGGERprivate java.util.List<java.io.File>tempFiles
-
Constructor Summary
Constructors Constructor Description PdfBoxGraphics2DFontTextDrawer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private org.apache.pdfbox.pdmodel.font.PDFontapplyFont(java.awt.Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env)booleancanDrawText(java.text.AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env)voidclose()Close / delete all resources associated with this drawer.voiddrawText(java.text.AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env)private org.apache.pdfbox.pdmodel.font.PDFontfindFallbackFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env)java.awt.FontMetricsgetFontMetrics(java.awt.Font f, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env)private floatgetSensibleThickness(float thickness, java.awt.Font font)protected booleanhasDynamicFontMapping()private booleaniterateRun(java.text.AttributedCharacterIterator iterator, java.lang.StringBuilder sb)protected org.apache.pdfbox.pdmodel.font.PDFontmapFont(java.awt.Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env)Try to map the java.awt.Font to a PDFont.voidregisterFont(java.io.File fontFile)Override for registerFont(null,fontFile)voidregisterFont(java.io.InputStream fontStream)Override for registerFont(null,fontStream)voidregisterFont(java.lang.String fontName, java.io.File fontFile)Register a font.voidregisterFont(java.lang.String fontName, java.io.InputStream fontStream)Register a font.voidregisterFont(java.lang.String name, org.apache.pdfbox.pdmodel.font.PDFont font)Register a font which is already associated with the PDDocumentprivate voidshowTextOnStream(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, java.awt.Font attributeFont, org.apache.pdfbox.pdmodel.font.PDFont font, boolean isStrikeThrough, boolean isUnderline, boolean isLigatures, PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState drawState, java.awt.Paint paint, java.lang.String text)private org.apache.pdfbox.pdmodel.font.PDType0FonttryToLoadFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, java.io.File foundFontFile)
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
fontFiles
private final java.util.List<PdfBoxGraphics2DFontTextDrawer.FontEntry> fontFiles
-
tempFiles
private final java.util.List<java.io.File> tempFiles
-
fontMap
private final java.util.Map<java.lang.String,org.apache.pdfbox.pdmodel.font.PDFont> fontMap
-
fallbackFontUnknownEncodings
private org.apache.pdfbox.pdmodel.font.PDFont fallbackFontUnknownEncodings
-
DEBUG_BOX
private static final boolean DEBUG_BOX
- See Also:
- Constant Field Values
-
-
Method Detail
-
close
public void 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.
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
registerFont
public void registerFont(java.lang.String fontName, java.io.InputStream fontStream) throws java.io.IOExceptionRegister 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- the name of the font to use. If null, the name is taken from the font.fontStream- 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.- Throws:
java.io.IOException- when something goes wrong with reading the font or writing the font to the content stream of the PDF:
-
registerFont
public void registerFont(java.lang.String fontName, java.io.File fontFile)Register a font.- Parameters:
fontName- the name of the font to use. If null, the name is taken from the font.fontFile- the font file. This file must exist for the live time of this object, as the font data will be read lazy on demand
-
registerFont
public void registerFont(java.io.File fontFile)
Override for registerFont(null,fontFile)- Parameters:
fontFile- the font file
-
registerFont
public void registerFont(java.io.InputStream fontStream) throws java.io.IOExceptionOverride for registerFont(null,fontStream)- Parameters:
fontStream- the font file- Throws:
java.io.IOException- when something goes wrong with reading the font or writing the font to the content stream of the PDF:
-
registerFont
public void registerFont(java.lang.String name, org.apache.pdfbox.pdmodel.font.PDFont font)Register a font which is already associated with the PDDocument- Parameters:
name- the name of the font as returned byFont.getFontName(). This name is used for the mapping the java.awt.Font to this PDFont.font- the PDFont to use. This font must be loaded in the current document.
-
hasDynamicFontMapping
protected boolean hasDynamicFontMapping()
- 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.
-
canDrawText
public boolean canDrawText(java.text.AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws java.io.IOException, java.awt.FontFormatException- Specified by:
canDrawTextin interfaceIPdfBoxGraphics2DFontTextDrawer- Parameters:
iterator- Has the text and all its propertiesenv- Environment- Returns:
- true when the given text can be fully drawn using fonts. return false to have the text drawn as vector shapes
- Throws:
java.io.IOException- when a font can not be loaded or a paint can't be applied.java.awt.FontFormatException- when the font file can not be loaded
-
drawText
public void drawText(java.text.AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws java.io.IOException, java.awt.FontFormatException- Specified by:
drawTextin interfaceIPdfBoxGraphics2DFontTextDrawer- Parameters:
iterator- The text with all propertiesenv- Environment- Throws:
java.io.IOException- when a font can not be loaded or a paint can't be applied.java.awt.FontFormatException- when the font file can not be loaded
-
getFontMetrics
public java.awt.FontMetrics getFontMetrics(java.awt.Font f, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws java.io.IOException, java.awt.FontFormatException- Specified by:
getFontMetricsin interfaceIPdfBoxGraphics2DFontTextDrawer- Parameters:
f- the Fontenv- the Environment- Returns:
- the resulting font metrics
- Throws:
java.io.IOExceptionjava.awt.FontFormatException
-
findFallbackFont
private org.apache.pdfbox.pdmodel.font.PDFont findFallbackFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws java.io.IOException
- Throws:
java.io.IOException
-
tryToLoadFont
private org.apache.pdfbox.pdmodel.font.PDType0Font tryToLoadFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, java.io.File foundFontFile)
-
showTextOnStream
private void showTextOnStream(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, java.awt.Font attributeFont, org.apache.pdfbox.pdmodel.font.PDFont font, boolean isStrikeThrough, boolean isUnderline, boolean isLigatures, PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState drawState, java.awt.Paint paint, java.lang.String text) throws java.io.IOException
- Throws:
java.io.IOException
-
getSensibleThickness
private float getSensibleThickness(float thickness, java.awt.Font font)
-
applyFont
private org.apache.pdfbox.pdmodel.font.PDFont applyFont(java.awt.Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws java.io.IOException, java.awt.FontFormatException- Throws:
java.io.IOExceptionjava.awt.FontFormatException
-
mapFont
protected org.apache.pdfbox.pdmodel.font.PDFont mapFont(java.awt.Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws java.io.IOException, java.awt.FontFormatExceptionTry to map the java.awt.Font to a PDFont.- Parameters:
font- the java.awt.Font for which a mapping should be foundenv- environment of the font mapper- Returns:
- the PDFont or null if none can be found.
- Throws:
java.io.IOException- when the font can not be loadedjava.awt.FontFormatException- when the font file can not be loaded
-
iterateRun
private boolean iterateRun(java.text.AttributedCharacterIterator iterator, java.lang.StringBuilder sb)
-
-