From: Jim Procter Date: Tue, 21 Aug 2018 21:40:02 +0000 (+0100) Subject: JAL-3085 spike - will need to cherrypick from this branch X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Ffeatures%2FJAL-3085_gnupdf;p=jalview.git JAL-3085 spike - will need to cherrypick from this branch --- diff --git a/.classpath b/.classpath index a5878e8..93808ae 100644 --- a/.classpath +++ b/.classpath @@ -32,11 +32,12 @@ - - - - - + + + + + + diff --git a/lib/gnujpdf.jar b/lib/gnujpdf.jar new file mode 100644 index 0000000..8b325ea Binary files /dev/null and b/lib/gnujpdf.jar differ diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index a9fd70c..51e9ce5 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1571,6 +1571,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } @Override + public void createPDF(File f) + { + alignPanel.makePDF(f); + } + + @Override public void createSVG(File f) { alignPanel.makeSVG(f); diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 2c5684a..56d993f 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -1406,6 +1406,11 @@ public class AlignmentPanel extends GAlignmentPanel implements makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile); } + public void makePDF(File pdfFile) + { + makeAlignmentImage(jalview.util.ImageMaker.TYPE.PDF, pdfFile); + } + /** * DOCUMENT ME! */ diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index e71d548..9bc5c6a 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -1109,6 +1109,15 @@ public class GAlignFrame extends JInternalFrame createSVG(null); } }); + JMenuItem createPDF = new JMenuItem("PDF"); + createPDF.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + createPDF(null); + } + }); JMenuItem loadTreeMenuItem = new JMenuItem( MessageManager.getString("label.load_associated_tree")); @@ -1867,6 +1876,7 @@ public class GAlignFrame extends JInternalFrame exportImageMenu.add(createPNG); exportImageMenu.add(createBioJS); exportImageMenu.add(createSVG); + exportImageMenu.add(createPDF); addSequenceMenu.add(addFromFile); addSequenceMenu.add(addFromText); addSequenceMenu.add(addFromURL); @@ -2548,6 +2558,10 @@ public class GAlignFrame extends JInternalFrame { } + public void createPDF(java.io.File f) + { + } + public void createSVG(java.io.File f) { diff --git a/src/jalview/util/ImageMaker.java b/src/jalview/util/ImageMaker.java index 7840261..ed16e11 100755 --- a/src/jalview/util/ImageMaker.java +++ b/src/jalview/util/ImageMaker.java @@ -31,6 +31,8 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; +import java.awt.print.PageFormat; +import java.awt.print.Paper; import java.io.File; import java.io.FileOutputStream; @@ -40,6 +42,8 @@ import org.jfree.graphics2d.svg.SVGGraphics2D; import org.jfree.graphics2d.svg.SVGHints; import org.jibble.epsgraphics.EpsGraphics2D; +import gnu.jpdf.PDFJob; + public class ImageMaker { public static final String SVG_DESCRIPTION = "Scalable Vector Graphics"; @@ -54,6 +58,10 @@ public class ImageMaker public static final String PNG_DESCRIPTION = "Portable network graphics"; + public static final String PDF_EXTENSION = "pdf"; + + public static final String PDF_DESCRIPTION = "Portable Document Format"; + public static final String HTML_EXTENSION = "html"; public static final String HTML_DESCRIPTION = "Hypertext Markup Language"; @@ -62,6 +70,7 @@ public class ImageMaker SVGGraphics2D g2; + Graphics graphics; FileOutputStream out; @@ -76,12 +85,15 @@ public class ImageMaker private boolean headless; + private PDFJob pdfjob; + public enum TYPE { EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG("PNG", MessageManager.getString("label.png_image"), getPNGChooser()), - SVG("SVG", "SVG", getSVGChooser()); + SVG("SVG", "SVG", getSVGChooser()), + PDF("PDF","PDF",getPDFChooser()); private JalviewFileChooser chooser; @@ -165,6 +177,10 @@ public class ImageMaker { setupPNG(width, height); } + else if (type == TYPE.PDF) + { + setupPDF(width, height, fileTitle); + } } catch (Exception ex) { @@ -202,6 +218,10 @@ public class ImageMaker out.flush(); out.close(); break; + case PDF: + graphics = null; + pdfjob.end(); + pdfjob = null; } } catch (Exception ex) { @@ -255,6 +275,50 @@ public class ImageMaker } } + void setupPDF(int width, int height, String title) + { + boolean accurateText = true; + + // String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING", + // "Prompt each time"); + // + // // If we need to prompt, and if the GUI is visible then + // // Prompt for EPS rendering style + // if (renderStyle.equalsIgnoreCase("Prompt each time") + // && !(System.getProperty("java.awt.headless") != null && System + // .getProperty("java.awt.headless").equals("true"))) + // { + // EPSOptions eps = new EPSOptions(); + // renderStyle = eps.getValue(); + // + // if (renderStyle == null || eps.cancelled) + // { + // setProgressMessage(MessageManager.formatMessage( + // "status.cancelled_image_export_operation", "EPS")); + // return; + // } + // } + // + // if (renderStyle.equalsIgnoreCase("text")) + // { + // accurateText = false; + // } + + try + { + pdfjob = new PDFJob(out, title); + + Paper paper = new Paper(); + paper.setSize(width, height); + PageFormat pf = new PageFormat(); + pf.setPaper(paper); + graphics = pdfjob.getGraphics(pf); + setProgressMessage(MessageManager + .formatMessage("status.export_complete", type.getName())); + } catch (Exception ex) + { + } + } void setupPNG(int width, int height) { bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); @@ -313,6 +377,15 @@ public class ImageMaker return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION); } + static JalviewFileChooser getPDFChooser() + { + if (Jalview.isHeadlessMode()) + { + return null; + } + return new JalviewFileChooser(PDF_EXTENSION, PDF_DESCRIPTION); + } + static JalviewFileChooser getEPSChooser() { if (Jalview.isHeadlessMode())