X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Farchaeopteryx%2FPdfExporter.java;h=56b41d60afb4ea4176316629b295e78a488e83a5;hb=693353f5ea85d3e50a77f9b1cd9d87711d23412b;hp=9a422d382f7850d7cea9aff638e8fda9e1a92bfd;hpb=236967e4df09b7a3fd6659a8131aa97ef9906a28;p=jalview.git diff --git a/forester/java/src/org/forester/archaeopteryx/PdfExporter.java b/forester/java/src/org/forester/archaeopteryx/PdfExporter.java index 9a422d3..56b41d6 100644 --- a/forester/java/src/org/forester/archaeopteryx/PdfExporter.java +++ b/forester/java/src/org/forester/archaeopteryx/PdfExporter.java @@ -32,15 +32,20 @@ import java.awt.Graphics2D; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Iterator; +import java.util.Map; import org.forester.phylogeny.Phylogeny; import org.forester.util.ForesterUtil; +import com.itextpdf.awt.DefaultFontMapper; +import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters; +import com.itextpdf.awt.PdfGraphics2D; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Rectangle; -import com.itextpdf.text.pdf.DefaultFontMapper; +import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; @@ -50,24 +55,34 @@ import com.itextpdf.text.pdf.PdfWriter; * * See: http://www.lowagie.com/iText/ * - * Current version: iText-2.1.7 + * Current version: iText-5.5.9 */ final class PdfExporter { private static final int HEIGHT_LIMIT = 100; private static final int WIDTH_LIMIT = 60; - + private static final int MARGIN_X = 20; + private static final int MARGIN_Y = 10; + private PdfExporter() { // Empty constructor. } - static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, int width, int height ) + static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, final int width, final int height ) throws IOException { + final int my_height; + final int my_width; if ( height < HEIGHT_LIMIT ) { - height = HEIGHT_LIMIT; + my_height = HEIGHT_LIMIT + 2 * MARGIN_Y; + } + else { + my_height = height + 2 * MARGIN_Y; } if ( width < WIDTH_LIMIT ) { - width = WIDTH_LIMIT; + my_width = WIDTH_LIMIT + 2 * MARGIN_X; + } + else { + my_width = width + 2 * MARGIN_X; } final Phylogeny phylogeny = tree_panel.getPhylogeny(); if ( ( phylogeny == null ) || phylogeny.isEmpty() ) { @@ -76,16 +91,20 @@ final class PdfExporter { if ( tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1 ) { throw new IOException( "fonts are too small for PDF export" ); } + if ( tree_panel.getMainPanel().getTreeFontSet().getLargeFont().getSize() < 1 ) { + throw new IOException( "fonts are too small for PDF export" ); + } final File file = new File( file_name ); if ( file.isDirectory() ) { throw new IOException( "[" + file_name + "] is a directory" ); } final Document document = new Document(); - document.setPageSize( new Rectangle( width, height ) ); - document.setMargins( WIDTH_LIMIT / 2, WIDTH_LIMIT / 2, HEIGHT_LIMIT / 2, HEIGHT_LIMIT / 2 ); + document.setPageSize( new Rectangle( my_width, my_height ) ); + document.setMargins( MARGIN_X, MARGIN_X, MARGIN_Y, MARGIN_Y ); PdfWriter writer = null; try { writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) ); + } catch ( final DocumentException e ) { throw new IOException( e ); @@ -94,7 +113,7 @@ final class PdfExporter { final DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); if ( ForesterUtil.isWindows() ) { - mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" ); + mapper.insertDirectory( "c:/windows/fonts" ); } else if ( ForesterUtil.isMac() ) { mapper.insertDirectory( "/Library/Fonts/" ); @@ -106,10 +125,13 @@ final class PdfExporter { mapper.insertDirectory( "/usr/share/fonts/default/TrueType/" ); mapper.insertDirectory( "/usr/share/fonts/default/Type1/" ); } + enableUnicode( mapper ); final PdfContentByte cb = writer.getDirectContent(); - final Graphics2D g2 = cb.createGraphics( width, height, mapper ); + + final Graphics2D g2 = new PdfGraphics2D(cb, my_width, my_height, mapper); + try { - tree_panel.paintPhylogeny( g2, true, false, width, height, 0, 0 ); + tree_panel.paintPhylogeny( g2, true, false, my_width, my_height, 0, 0 ); } catch ( final Exception e ) { AptxUtil.unexpectedException( e ); @@ -123,10 +145,50 @@ final class PdfExporter { //Do nothing. } } - String msg = file.toString(); - if ( ( width > 0 ) && ( height > 0 ) ) { - msg += " [size: " + width + ", " + height + "]"; - } + final String msg = file.toString() + " [size: " + my_width + ", " + my_height + "]"; return msg; } + + private final static void enableUnicode( final DefaultFontMapper mapper ) { + final Map map = mapper.getMapper(); + for (final Iterator i = map.keySet().iterator(); i.hasNext();) { + final String name = i.next(); + final String name_lc = name.toLowerCase(); + if ( name_lc.contains( "unicode" ) || name_lc.equals( "dialog" ) ) { + final BaseFontParameters pfps = map.get(name); + try { + pfps.encoding = BaseFont.IDENTITY_H; + pfps.embedded = true; + } + catch ( Exception e ) { + //Ignore. + } + } + } + } + + /* not used currently + static FontMapper arial_uni = new FontMapper() { + public BaseFont awtToPdf(Font font) { + System.out.println( font.toString() ); + try { + return BaseFont.createFont( + "c:/windows/fonts/arialuni.ttf", + BaseFont.IDENTITY_H, BaseFont.EMBEDDED); + } + catch (DocumentException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public Font pdfToAwt( BaseFont arg0, int arg1 ) { + return null; + } + }; + */ }