2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
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
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.
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.
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
26 // Contact: phylosoft @ gmail . com
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
29 package org.forester.archaeopteryx;
31 import java.awt.Graphics2D;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
36 import org.forester.phylogeny.Phylogeny;
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;
50 * See: http://www.lowagie.com/iText/
52 * Current version: iText-2.1.7
54 final class PdfExporter {
56 private static final int HEIGHT_LIMIT = 100;
57 private static final int WIDTH_LIMIT = 60;
59 private PdfExporter() {
63 static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, int width, int height )
65 if ( height < HEIGHT_LIMIT ) {
66 height = HEIGHT_LIMIT;
68 if ( width < WIDTH_LIMIT ) {
71 final Phylogeny phylogeny = tree_panel.getPhylogeny();
72 if ( ( phylogeny == null ) || phylogeny.isEmpty() ) {
75 if ( tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1 ) {
76 throw new IOException( "fonts are too small for PDF export" );
78 final File file = new File( file_name );
79 if ( file.isDirectory() ) {
80 throw new IOException( "[" + file_name + "] is a directory" );
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;
87 writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) );
89 catch ( final DocumentException e ) {
90 throw new IOException( e );
93 final DefaultFontMapper mapper = new DefaultFontMapper();
94 FontFactory.registerDirectories();
95 if ( AptxUtil.isWindows() ) {
96 mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" );
98 else if ( AptxUtil.isMac() ) {
99 mapper.insertDirectory( "/Library/Fonts/" );
100 mapper.insertDirectory( "/System/Library/Fonts/" );
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/" );
108 final PdfContentByte cb = writer.getDirectContent();
109 final Graphics2D g2 = cb.createGraphics( width, height, mapper );
111 tree_panel.paintPhylogeny( g2, true, false, width, height, 0, 0 );
113 catch ( final Exception e ) {
114 AptxUtil.unexpectedException( e );
121 catch ( final Exception e ) {
125 String msg = file.toString();
126 if ( ( width > 0 ) && ( height > 0 ) ) {
127 msg += " [size: " + width + ", " + height + "]";