added URL tree reading back
[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 import org.forester.util.ForesterUtil;
38
39 import com.itextpdf.text.Document;
40 import com.itextpdf.text.DocumentException;
41 import com.itextpdf.text.FontFactory;
42 import com.itextpdf.text.Rectangle;
43 import com.itextpdf.text.pdf.DefaultFontMapper;
44 import com.itextpdf.text.pdf.PdfContentByte;
45 import com.itextpdf.text.pdf.PdfWriter;
46
47 /*
48  * 
49  * This uses iText.
50  * 
51  * See: http://www.lowagie.com/iText/
52  * 
53  * Current version: iText-2.1.7
54  */
55 final class PdfExporter {
56
57     private static final int HEIGHT_LIMIT = 100;
58     private static final int WIDTH_LIMIT  = 60;
59
60     private PdfExporter() {
61         // Empty constructor.
62     }
63
64     static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, int width, int height )
65             throws IOException {
66         if ( height < HEIGHT_LIMIT ) {
67             height = HEIGHT_LIMIT;
68         }
69         if ( width < WIDTH_LIMIT ) {
70             width = WIDTH_LIMIT;
71         }
72         final Phylogeny phylogeny = tree_panel.getPhylogeny();
73         if ( ( phylogeny == null ) || phylogeny.isEmpty() ) {
74             return "";
75         }
76         if ( tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1 ) {
77             throw new IOException( "fonts are too small for PDF export" );
78         }
79         final File file = new File( file_name );
80         if ( file.isDirectory() ) {
81             throw new IOException( "[" + file_name + "] is a directory" );
82         }
83         final Document document = new Document();
84         document.setPageSize( new Rectangle( width, height ) );
85         document.setMargins( WIDTH_LIMIT / 2, WIDTH_LIMIT / 2, HEIGHT_LIMIT / 2, HEIGHT_LIMIT / 2 );
86         PdfWriter writer = null;
87         try {
88             writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) );
89         }
90         catch ( final DocumentException e ) {
91             throw new IOException( e );
92         }
93         document.open();
94         final DefaultFontMapper mapper = new DefaultFontMapper();
95         FontFactory.registerDirectories();
96         if ( ForesterUtil.isWindows() ) {
97             mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" );
98         }
99         else if ( ForesterUtil.isMac() ) {
100             mapper.insertDirectory( "/Library/Fonts/" );
101             mapper.insertDirectory( "/System/Library/Fonts/" );
102         }
103         else {
104             mapper.insertDirectory( "/usr/X/lib/X11/fonts/TrueType/" );
105             mapper.insertDirectory( "/usr/X/lib/X11/fonts/Type1/" );
106             mapper.insertDirectory( "/usr/share/fonts/default/TrueType/" );
107             mapper.insertDirectory( "/usr/share/fonts/default/Type1/" );
108         }
109         final PdfContentByte cb = writer.getDirectContent();
110         final Graphics2D g2 = cb.createGraphics( width, height, mapper );
111         try {
112             tree_panel.paintPhylogeny( g2, true, false, width, height, 0, 0 );
113         }
114         catch ( final Exception e ) {
115             AptxUtil.unexpectedException( e );
116         }
117         finally {
118             try {
119                 g2.dispose();
120                 document.close();
121             }
122             catch ( final Exception e ) {
123                 //Do nothing.
124             }
125         }
126         String msg = file.toString();
127         if ( ( width > 0 ) && ( height > 0 ) ) {
128             msg += " [size: " + width + ", " + height + "]";
129         }
130         return msg;
131     }
132 }