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;
35 import java.util.Iterator;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.util.ForesterUtil;
41 import com.itextpdf.awt.DefaultFontMapper;
42 import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters;
43 import com.itextpdf.awt.PdfGraphics2D;
44 import com.itextpdf.text.Document;
45 import com.itextpdf.text.DocumentException;
46 import com.itextpdf.text.FontFactory;
47 import com.itextpdf.text.Rectangle;
48 import com.itextpdf.text.pdf.BaseFont;
49 import com.itextpdf.text.pdf.PdfContentByte;
50 import com.itextpdf.text.pdf.PdfWriter;
56 * See: http://www.lowagie.com/iText/
58 * Current version: iText-5.5.9
60 final class PdfExporter {
62 private static final int HEIGHT_LIMIT = 100;
63 private static final int WIDTH_LIMIT = 60;
64 private static final int MARGIN_X = 20;
65 private static final int MARGIN_Y = 10;
67 private PdfExporter() {
71 static String writePhylogenyToPdf( final String file_name, final TreePanel tree_panel, final int width, final int height )
75 if ( height < HEIGHT_LIMIT ) {
76 my_height = HEIGHT_LIMIT + 2 * MARGIN_Y;
79 my_height = height + 2 * MARGIN_Y;
81 if ( width < WIDTH_LIMIT ) {
82 my_width = WIDTH_LIMIT + 2 * MARGIN_X;
85 my_width = width + 2 * MARGIN_X;
87 final Phylogeny phylogeny = tree_panel.getPhylogeny();
88 if ( ( phylogeny == null ) || phylogeny.isEmpty() ) {
91 if ( tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1 ) {
92 throw new IOException( "fonts are too small for PDF export" );
94 if ( tree_panel.getMainPanel().getTreeFontSet().getLargeFont().getSize() < 1 ) {
95 throw new IOException( "fonts are too small for PDF export" );
97 final File file = new File( file_name );
98 if ( file.isDirectory() ) {
99 throw new IOException( "[" + file_name + "] is a directory" );
101 final Document document = new Document();
102 document.setPageSize( new Rectangle( my_width, my_height ) );
103 document.setMargins( MARGIN_X, MARGIN_X, MARGIN_Y, MARGIN_Y );
104 PdfWriter writer = null;
106 writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) );
109 catch ( final DocumentException e ) {
110 throw new IOException( e );
113 final DefaultFontMapper mapper = new DefaultFontMapper();
114 FontFactory.registerDirectories();
115 if ( ForesterUtil.isWindows() ) {
116 mapper.insertDirectory( "c:/windows/fonts" );
118 else if ( ForesterUtil.isMac() ) {
119 mapper.insertDirectory( "/Library/Fonts/" );
120 mapper.insertDirectory( "/System/Library/Fonts/" );
123 mapper.insertDirectory( "/usr/X/lib/X11/fonts/TrueType/" );
124 mapper.insertDirectory( "/usr/X/lib/X11/fonts/Type1/" );
125 mapper.insertDirectory( "/usr/share/fonts/default/TrueType/" );
126 mapper.insertDirectory( "/usr/share/fonts/default/Type1/" );
128 enableUnicode( mapper );
129 final PdfContentByte cb = writer.getDirectContent();
131 final Graphics2D g2 = new PdfGraphics2D(cb, my_width, my_height, mapper);
134 tree_panel.paintPhylogeny( g2, true, false, my_width, my_height, 0, 0 );
136 catch ( final Exception e ) {
137 AptxUtil.unexpectedException( e );
144 catch ( final Exception e ) {
148 final String msg = file.toString() + " [size: " + my_width + ", " + my_height + "]";
152 private final static void enableUnicode( final DefaultFontMapper mapper ) {
153 final Map<String, DefaultFontMapper.BaseFontParameters> map = mapper.getMapper();
154 for (final Iterator<String> i = map.keySet().iterator(); i.hasNext();) {
155 final String name = i.next();
156 final String name_lc = name.toLowerCase();
157 if ( name_lc.contains( "unicode" ) || name_lc.equals( "dialog" ) ) {
158 final BaseFontParameters pfps = map.get(name);
160 pfps.encoding = BaseFont.IDENTITY_H;
161 pfps.embedded = true;
163 catch ( Exception e ) {
170 /* not used currently
171 static FontMapper arial_uni = new FontMapper() {
172 public BaseFont awtToPdf(Font font) {
173 System.out.println( font.toString() );
175 return BaseFont.createFont(
176 "c:/windows/fonts/arialuni.ttf",
177 BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
179 catch (DocumentException e) {
182 catch (IOException e) {
189 public Font pdfToAwt( BaseFont arg0, int arg1 ) {