From bf7b5c06715bf64a338402aea20825ad9dbad43c Mon Sep 17 00:00:00 2001 From: cmzmasek Date: Thu, 30 Jun 2016 14:05:22 -0700 Subject: [PATCH] new itext version, plus minor changes --- .../_aptx_configuration_file | 6 +- forester/java/build.xml | 2 +- .../org/forester/archaeopteryx/AptxConstants.java | 5 +- .../archaeopteryx/MainFrameApplication.java | 45 +++++++-------- .../org/forester/archaeopteryx/PdfExporter.java | 60 ++++++++++++++++++-- .../org/forester/archaeopteryx/TreeFontSet.java | 2 +- .../src/org/forester/archaeopteryx/TreePanel.java | 10 ++-- 7 files changed, 88 insertions(+), 42 deletions(-) diff --git a/forester/aptx/aptx_configuration_files/_aptx_configuration_file b/forester/aptx/aptx_configuration_files/_aptx_configuration_file index 1ffa5d8..2ca3dc7 100644 --- a/forester/aptx/aptx_configuration_files/_aptx_configuration_file +++ b/forester/aptx/aptx_configuration_files/_aptx_configuration_file @@ -17,11 +17,11 @@ native_ui: ? # value for bootstrap support) # # Font family name: 'font_family': -# Example: 'font_family: Sans,Helvetica' +# Example: 'font_family: Arial_Unicode_MS,Dialog,SansSerif,Sans,Arial,Helvetica' # It is advisable to use more than one value for font_family (in # decreasing order of preference). Font family names have to be # comma separated (no spaces). Spaces in font names have to be -# replaced by underscores (e.g. 'Times_New_Roman'). +# replaced by underscores (e.g. 'Arial_Unicode_MS'). # # Font size: 'font_size': # Example: 'font_size: 10' @@ -162,7 +162,7 @@ native_ui: ? min_confidence_value: 0.0 -font_family: SansSerif,Sans-serif,Sans,Dialog,Tahoma,Helvetica,Arial,Verdana +font_family: Arial_Unicode_MS,Dialog,SansSerif,Sans,Arial,Helvetica font_size: 10 font_size_min: 2 font_size_max: 20 diff --git a/forester/java/build.xml b/forester/java/build.xml index 2cbdd38..41380d2 100644 --- a/forester/java/build.xml +++ b/forester/java/build.xml @@ -4,7 +4,7 @@ - + diff --git a/forester/java/src/org/forester/archaeopteryx/AptxConstants.java b/forester/java/src/org/forester/archaeopteryx/AptxConstants.java index 456dea4..f2e803a 100644 --- a/forester/java/src/org/forester/archaeopteryx/AptxConstants.java +++ b/forester/java/src/org/forester/archaeopteryx/AptxConstants.java @@ -39,13 +39,12 @@ public final class AptxConstants { final static boolean __ALLOW_PHYLOGENETIC_INFERENCE = true; public final static boolean __RELEASE = false; // TODO remove me public final static boolean __SNAPSHOT_RELEASE = false; // TODO remove me - public final static boolean __SYNTH_LF = false; // TODO remove me public final static String PRG_NAME = "Archaeopteryx"; final static String VERSION = "0.9912 beta"; - final static String PRG_DATE = "160629"; + final static String PRG_DATE = "160631"; final static String DEFAULT_CONFIGURATION_FILE_NAME = "_aptx_configuration_file"; final static String[] DEFAULT_FONT_CHOICES = { - "SansSerif", "Sans-serif", "Sans", "Dialog", "Lucida Sans", "Tahoma", "Helvetica", "Arial", "Verdana" }; + "Arial Unicode MS", "Dialog", "SansSerif", "Sans", "Arial", "Helvetica" }; final static boolean VERBOSE_DEFAULT = false; final static int DOMAIN_STRUCTURE_DEFAULT_WIDTH = 100; final static String AUTHOR_EMAIL = "phyloxml@gmail.com"; diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java index b940f35..d78cf09 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java @@ -57,7 +57,6 @@ import javax.swing.UnsupportedLookAndFeelException; import javax.swing.WindowConstants; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.swing.plaf.synth.SynthLookAndFeel; import org.forester.analysis.TaxonomyDataManager; import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE; @@ -196,30 +195,12 @@ public final class MainFrameApplication extends MainFrame { throw new IllegalArgumentException( "configuration is null" ); } try { - boolean synth_exception = false; - if ( AptxConstants.__SYNTH_LF ) { - try { - final SynthLookAndFeel synth = new SynthLookAndFeel(); - synth.load( MainFrameApplication.class.getResourceAsStream( "/resources/synth_look_and_feel_1.xml" ), - MainFrameApplication.class ); - UIManager.setLookAndFeel( synth ); - } - catch ( final Exception ex ) { - synth_exception = true; - ForesterUtil.printWarningMessage( AptxConstants.PRG_NAME, - "could not create synth look and feel: " - + ex.getLocalizedMessage() ); - } + if ( _configuration.isUseNativeUI() ) { + UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); } - if ( !AptxConstants.__SYNTH_LF || synth_exception ) { - if ( _configuration.isUseNativeUI() ) { - UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); - } - else { - UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() ); - } + else { + UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() ); } - //UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ); } catch ( final UnsupportedLookAndFeelException e ) { AptxUtil.dieWithSystemError( "unsupported look and feel: " + e.toString() ); @@ -816,8 +797,16 @@ public final class MainFrameApplication extends MainFrame { List seqs = null; if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) { try { - if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) { - seqs = FastaParser.parse( new FileInputStream( file ) ); + final FileInputStream fis1 = new FileInputStream( file ); + if ( FastaParser.isLikelyFasta( fis1 ) ) { + final FileInputStream fis2 = new FileInputStream( file ); + seqs = FastaParser.parse( fis2 ); + try { + fis2.close(); + } + catch ( final Exception e ) { + // Ignore. + } } else { JOptionPane.showMessageDialog( this, @@ -826,6 +815,12 @@ public final class MainFrameApplication extends MainFrame { JOptionPane.ERROR_MESSAGE ); return; } + try { + fis1.close(); + } + catch ( final Exception e ) { + // Ignore. + } } catch ( final MsaFormatException e ) { setArrowCursor(); diff --git a/forester/java/src/org/forester/archaeopteryx/PdfExporter.java b/forester/java/src/org/forester/archaeopteryx/PdfExporter.java index 9a422d3..2d5bcb0 100644 --- a/forester/java/src/org/forester/archaeopteryx/PdfExporter.java +++ b/forester/java/src/org/forester/archaeopteryx/PdfExporter.java @@ -32,15 +32,20 @@ import java.awt.Graphics2D; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Iterator; +import java.util.Map; import org.forester.phylogeny.Phylogeny; import org.forester.util.ForesterUtil; +import com.itextpdf.awt.DefaultFontMapper; +import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters; +import com.itextpdf.awt.PdfGraphics2D; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Rectangle; -import com.itextpdf.text.pdf.DefaultFontMapper; +import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; @@ -50,7 +55,7 @@ import com.itextpdf.text.pdf.PdfWriter; * * See: http://www.lowagie.com/iText/ * - * Current version: iText-2.1.7 + * Current version: iText-5.5.9 */ final class PdfExporter { @@ -86,6 +91,7 @@ final class PdfExporter { PdfWriter writer = null; try { writer = PdfWriter.getInstance( document, new FileOutputStream( file_name ) ); + } catch ( final DocumentException e ) { throw new IOException( e ); @@ -94,7 +100,7 @@ final class PdfExporter { final DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); if ( ForesterUtil.isWindows() ) { - mapper.insertDirectory( "C:\\WINDOWS\\Fonts\\" ); + mapper.insertDirectory( "c:/windows/fonts" ); } else if ( ForesterUtil.isMac() ) { mapper.insertDirectory( "/Library/Fonts/" ); @@ -106,8 +112,11 @@ final class PdfExporter { mapper.insertDirectory( "/usr/share/fonts/default/TrueType/" ); mapper.insertDirectory( "/usr/share/fonts/default/Type1/" ); } + enableUnicode( mapper ); final PdfContentByte cb = writer.getDirectContent(); - final Graphics2D g2 = cb.createGraphics( width, height, mapper ); + + final Graphics2D g2 = new PdfGraphics2D(cb, width, height, mapper); + try { tree_panel.paintPhylogeny( g2, true, false, width, height, 0, 0 ); } @@ -129,4 +138,47 @@ final class PdfExporter { } return msg; } + + private final static void enableUnicode( final DefaultFontMapper mapper ) { + final Map map = mapper.getMapper(); + for (final Iterator i = map.keySet().iterator(); i.hasNext();) { + final String name = i.next(); + final String name_lc = name.toLowerCase(); + if ( name_lc.contains( "unicode" ) || name_lc.equals( "dialog" ) ) { + final BaseFontParameters pfps = map.get(name); + try { + pfps.encoding = BaseFont.IDENTITY_H; + pfps.embedded = true; + } + catch ( Exception e ) { + //Ignore. + } + } + } + } + + /* not used currently + static FontMapper arial_uni = new FontMapper() { + public BaseFont awtToPdf(Font font) { + System.out.println( font.toString() ); + try { + return BaseFont.createFont( + "c:/windows/fonts/arialuni.ttf", + BaseFont.IDENTITY_H, BaseFont.EMBEDDED); + } + catch (DocumentException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public Font pdfToAwt( BaseFont arg0, int arg1 ) { + return null; + } + }; + */ } diff --git a/forester/java/src/org/forester/archaeopteryx/TreeFontSet.java b/forester/java/src/org/forester/archaeopteryx/TreeFontSet.java index 47fd94e..40d945d 100644 --- a/forester/java/src/org/forester/archaeopteryx/TreeFontSet.java +++ b/forester/java/src/org/forester/archaeopteryx/TreeFontSet.java @@ -39,7 +39,7 @@ public final class TreeFontSet { static final int BOLD_AND_ITALIC = Font.BOLD + Font.ITALIC; final static float FONT_SIZE_CHANGE_STEP = 1.0f; final static float SMALL_FONTS_BASE = 8; - private final static String DEFAULT_FONT = "Verdana"; + private final static String DEFAULT_FONT = "Arial Unicode MS"; private Font _base_font; private boolean _decreased_size_by_system = false; private FontMetrics _fm_large; diff --git a/forester/java/src/org/forester/archaeopteryx/TreePanel.java b/forester/java/src/org/forester/archaeopteryx/TreePanel.java index f6f5911..ff41db5 100644 --- a/forester/java/src/org/forester/archaeopteryx/TreePanel.java +++ b/forester/java/src/org/forester/archaeopteryx/TreePanel.java @@ -313,9 +313,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee private float _y_distance = 0.0f; private int _length_of_longest_text; private int _longest_domain; - // private Image offscreenImage; - // private Graphics offscreenGraphics; - // private Dimension offscreenDimension; static { final DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator( '.' ); @@ -4390,7 +4387,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee setOvXDistance( ov_xdist ); final double height = _phylogeny.getHeight(); if ( height > 0 ) { - final float corr = ( float ) ( ( x - TreePanel.MOVE - getLongestExtNodeInfo() - getXdistance() ) / height ); + final float corr = ( float ) ( ( x - ( 2.0 * TreePanel.MOVE ) - getLongestExtNodeInfo() - getXdistance() ) / height ); setXcorrectionFactor( corr > 0 ? corr : 0 ); final float ov_corr = ( float ) ( ( getOvMaxWidth() - getOvXDistance() ) / height ); setOvXcorrectionFactor( ov_corr > 0 ? ov_corr : 0 ); @@ -4427,7 +4424,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee if ( ( _phylogeny == null ) || _phylogeny.isEmpty() ) { return; } - int max_length = ForesterUtil.roundToInt( ( getSize().getWidth() - MOVE ) + int max_length = ForesterUtil.roundToInt( ( getSize().getWidth() - 2 * MOVE ) * AptxConstants.EXT_NODE_INFO_LENGTH_MAX_RATIO ); if ( max_length < 40 ) { max_length = 40; @@ -4508,6 +4505,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee _longest_ext_node_info = longest; } _length_of_longest_text = calcLengthOfLongestText(); + System.out.println( "_length_of_longest_text=" + _length_of_longest_text ); + //~~ } final void calculateScaleDistance() { @@ -5687,6 +5686,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee + getLongestExtNodeInfo() + ForesterUtil .roundToInt( ( getXcorrectionFactor() * getPhylogeny().getHeight() ) + getXdistance() ); + } else { if ( !isNonLinedUpCladogram() && !isUniformBranchLengthsForCladogram() ) { -- 1.7.10.2