inprogress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / PdfExporter.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon
10 // All rights reserved
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 //
26 // Contact: phylosoft @ gmail . com
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28
29 package org.forester.archaeopteryx;
30
31 import java.awt.Graphics2D;
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35
36 import org.forester.phylogeny.Phylogeny;
37
38 import com.itextpdf.text.Document;
39 import com.itextpdf.text.DocumentException;
40 import com.itextpdf.text.FontFactory;
41 import com.itextpdf.text.Rectangle;
42 import com.itextpdf.text.pdf.DefaultFontMapper;
43 import com.itextpdf.text.pdf.PdfContentByte;
44 import com.itextpdf.text.pdf.PdfWriter;
45
46 /*
47  * 
48  * This uses iText.
49  * 
50  * See: http://www.lowagie.com/iText/
51  * 
52  * Current version: iText-2.1.7
53  */
54 final class PdfExporter {
55
56     private static final int HEIGHT_LIMIT = 100;
57     private static final int WIDTH_LIMIT  = 60;
58
59     private PdfExporter() {
60         // Empty constructor.
61     }
62
63     static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, int width, int height )
64             throws IOException {
65         if ( height < HEIGHT_LIMIT ) {
66             height = HEIGHT_LIMIT;
67         }
68         if ( width < WIDTH_LIMIT ) {
69             width = WIDTH_LIMIT;
70         }
71         final Phylogeny phylogeny = tree_panel.getPhylogeny();
72         if ( ( phylogeny == null ) || phylogeny.isEmpty() ) {
73             return "";
74         }
75         if ( tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1 ) {
76             throw new IOException( "fonts are too small for PDF export" );
77         }
78         final File file = new File( file_name );
79         if ( file.isDirectory() ) {
80             throw new IOException( "[" + file_name + "] is a directory" );
81         }
82         final Document document = new Document();
83         document.setPageSize( new Rectangle( width, height ) );
84         document.setMargins( WIDTH_LIMIT / 2, WIDTH_LIMIT / 2, HEIGHT_LIMIT / 2, HEIGHT_LIMIT / 2 );
85         PdfWriter writer = null;
86         try {
87             writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) );
88         }
89         catch ( final DocumentException e ) {
90             throw new IOException( e );
91         }
92         document.open();
93         final DefaultFontMapper mapper = new DefaultFontMapper();
94         FontFactory.registerDirectories();
95         if ( AptxUtil.isWindows() ) {
96             mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" );
97         }
98         else if ( AptxUtil.isMac() ) {
99             mapper.insertDirectory( "/Library/Fonts/" );
100             mapper.insertDirectory( "/System/Library/Fonts/" );
101         }
102         else {
103             mapper.insertDirectory( "/usr/X/lib/X11/fonts/TrueType/" );
104             mapper.insertDirectory( "/usr/X/lib/X11/fonts/Type1/" );
105             mapper.insertDirectory( "/usr/share/fonts/default/TrueType/" );
106             mapper.insertDirectory( "/usr/share/fonts/default/Type1/" );
107         }
108         final PdfContentByte cb = writer.getDirectContent();
109         final Graphics2D g2 = cb.createGraphics( width, height, mapper );
110         try {
111             tree_panel.paintPhylogeny( g2, true, false, width, height, 0, 0 );
112         }
113         catch ( final Exception e ) {
114             AptxUtil.unexpectedException( e );
115         }
116         finally {
117             try {
118                 g2.dispose();
119                 document.close();
120             }
121             catch ( final Exception e ) {
122                 //Do nothing.
123             }
124         }
125         String msg = file.toString();
126         if ( ( width > 0 ) && ( height > 0 ) ) {
127             msg += " [size: " + width + ", " + height + "]";
128         }
129         return msg;
130     }
131 }