in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / PdfExporter.java
index 28fdfa9..56b41d6 100644 (file)
@@ -24,7 +24,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.archaeopteryx;
 
@@ -32,14 +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;
 
@@ -49,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() ) {
@@ -75,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 );
@@ -92,10 +112,10 @@ final class PdfExporter {
         document.open();
         final DefaultFontMapper mapper = new DefaultFontMapper();
         FontFactory.registerDirectories();
-        if ( Util.isWindows() ) {
-            mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" );
+        if ( ForesterUtil.isWindows() ) {
+            mapper.insertDirectory( "c:/windows/fonts" );
         }
-        else if ( Util.isMac() ) {
+        else if ( ForesterUtil.isMac() ) {
             mapper.insertDirectory( "/Library/Fonts/" );
             mapper.insertDirectory( "/System/Library/Fonts/" );
         }
@@ -105,13 +125,16 @@ 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 ) {
-            Util.unexpectedException( e );
+            AptxUtil.unexpectedException( e );
         }
         finally {
             try {
@@ -122,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<String, DefaultFontMapper.BaseFontParameters> map = mapper.getMapper();
+        for (final Iterator<String> 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;
+        }
+    };
+    */
 }