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;
37 import org.forester.util.ForesterUtil;
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;
51 * See: http://www.lowagie.com/iText/
53 * Current version: iText-2.1.7
55 final class PdfExporter {
57 private static final int HEIGHT_LIMIT = 100;
58 private static final int WIDTH_LIMIT = 60;
60 private PdfExporter() {
64 static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, int width, int height )
66 if ( height < HEIGHT_LIMIT ) {
67 height = HEIGHT_LIMIT;
69 if ( width < WIDTH_LIMIT ) {
72 final Phylogeny phylogeny = tree_panel.getPhylogeny();
73 if ( ( phylogeny == null ) || phylogeny.isEmpty() ) {
76 if ( tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1 ) {
77 throw new IOException( "fonts are too small for PDF export" );
79 final File file = new File( file_name );
80 if ( file.isDirectory() ) {
81 throw new IOException( "[" + file_name + "] is a directory" );
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;
88 writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) );
90 catch ( final DocumentException e ) {
91 throw new IOException( e );
94 final DefaultFontMapper mapper = new DefaultFontMapper();
95 FontFactory.registerDirectories();
96 if ( ForesterUtil.isWindows() ) {
97 mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" );
99 else if ( ForesterUtil.isMac() ) {
100 mapper.insertDirectory( "/Library/Fonts/" );
101 mapper.insertDirectory( "/System/Library/Fonts/" );
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/" );
109 final PdfContentByte cb = writer.getDirectContent();
110 final Graphics2D g2 = cb.createGraphics( width, height, mapper );
112 tree_panel.paintPhylogeny( g2, true, false, width, height, 0, 0 );
114 catch ( final Exception e ) {
115 AptxUtil.unexpectedException( e );
122 catch ( final Exception e ) {
126 String msg = file.toString();
127 if ( ( width > 0 ) && ( height > 0 ) ) {
128 msg += " [size: " + width + ", " + height + "]";