Class PdfBoxGraphics2DFontTextDrawer
java.lang.Object
de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer
- All Implemented Interfaces:
IPdfBoxGraphics2DFontTextDrawer, Closeable, AutoCloseable
- Direct Known Subclasses:
PdfBoxGraphics2DFontTextDrawerDefaultFonts
public class PdfBoxGraphics2DFontTextDrawer
extends Object
implements IPdfBoxGraphics2DFontTextDrawer, Closeable
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 Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static classprivate static interfaceNested classes/interfaces inherited from interface IPdfBoxGraphics2DFontTextDrawer
IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanprivate org.apache.pdfbox.pdmodel.font.PDFontprivate final List<PdfBoxGraphics2DFontTextDrawer.FontEntry> private static final Logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate org.apache.pdfbox.pdmodel.font.PDFontbooleancanDrawText(AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) voidclose()Close / delete all resources associated with this drawer.voiddrawText(AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) private org.apache.pdfbox.pdmodel.font.PDFontprivate floatgetSensibleThickness(float thickness, Font font) protected booleanprivate booleaniterateRun(AttributedCharacterIterator iterator, StringBuilder sb) protected org.apache.pdfbox.pdmodel.font.PDFontTry to map the java.awt.Font to a PDFont.voidregisterFont(File fontFile) Override for registerFont(null,fontFile)voidregisterFont(InputStream fontStream) Override for registerFont(null,fontStream)voidregisterFont(String fontName, File fontFile) Register a font.voidregisterFont(String fontName, InputStream fontStream) Register a font.voidregisterFont(String name, org.apache.pdfbox.pdmodel.font.PDFont font) Register a font which is already associated with the PDDocumentprivate voidshowTextOnStream(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, Font attributeFont, org.apache.pdfbox.pdmodel.font.PDFont font, boolean isStrikeThrough, boolean isUnderline, boolean isLigatures, PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState drawState, Paint paint, String text) private org.apache.pdfbox.pdmodel.font.PDType0FonttryToLoadFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, File foundFontFile)
-
Field Details
-
LOGGER
-
fontFiles
-
tempFiles
-
fontMap
-
fallbackFontUnknownEncodings
private org.apache.pdfbox.pdmodel.font.PDFont fallbackFontUnknownEncodings -
DEBUG_BOX
private static final boolean DEBUG_BOX- See Also:
-
-
Constructor Details
-
PdfBoxGraphics2DFontTextDrawer
public PdfBoxGraphics2DFontTextDrawer()
-
-
Method Details
-
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 interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
registerFont
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- 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:
IOException- when something goes wrong with reading the font or writing the font to the content stream of the PDF:
-
registerFont
-
registerFont
Override for registerFont(null,fontFile)- Parameters:
fontFile- the font file
-
registerFont
Override for registerFont(null,fontStream)- Parameters:
fontStream- the font file- Throws:
IOException- when something goes wrong with reading the font or writing the font to the content stream of the PDF:
-
registerFont
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(AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, 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:
IOException- when a font can not be loaded or a paint can't be applied.FontFormatException- when the font file can not be loaded
-
drawText
public void drawText(AttributedCharacterIterator iterator, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException - Specified by:
drawTextin interfaceIPdfBoxGraphics2DFontTextDrawer- Parameters:
iterator- The text with all propertiesenv- Environment- Throws:
IOException- when a font can not be loaded or a paint can't be applied.FontFormatException- when the font file can not be loaded
-
getFontMetrics
public FontMetrics getFontMetrics(Font f, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException - Specified by:
getFontMetricsin interfaceIPdfBoxGraphics2DFontTextDrawer- Parameters:
f- the Fontenv- the Environment- Returns:
- the resulting font metrics
- Throws:
IOExceptionFontFormatException
-
findFallbackFont
private org.apache.pdfbox.pdmodel.font.PDFont findFallbackFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException - Throws:
IOException
-
tryToLoadFont
private org.apache.pdfbox.pdmodel.font.PDType0Font tryToLoadFont(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, File foundFontFile) -
showTextOnStream
private void showTextOnStream(IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env, Font attributeFont, org.apache.pdfbox.pdmodel.font.PDFont font, boolean isStrikeThrough, boolean isUnderline, boolean isLigatures, PdfBoxGraphics2DFontTextDrawer.DrawTextDecorationState drawState, Paint paint, String text) throws IOException - Throws:
IOException
-
getSensibleThickness
-
applyFont
private org.apache.pdfbox.pdmodel.font.PDFont applyFont(Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException - Throws:
IOExceptionFontFormatException
-
mapFont
protected org.apache.pdfbox.pdmodel.font.PDFont mapFont(Font font, IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv env) throws IOException, FontFormatException Try 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:
IOException- when the font can not be loadedFontFormatException- when the font file can not be loaded
-
iterateRun
-