JAL-3085 spike - will need to cherrypick from this branch features/JAL-3085_gnupdf
authorJim Procter <jprocter@issues.jalview.org>
Tue, 21 Aug 2018 21:40:02 +0000 (22:40 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Tue, 21 Aug 2018 21:40:02 +0000 (22:40 +0100)
.classpath
lib/gnujpdf.jar [new file with mode: 0644]
src/jalview/gui/AlignFrame.java
src/jalview/gui/AlignmentPanel.java
src/jalview/jbgui/GAlignFrame.java
src/jalview/util/ImageMaker.java

index a5878e8..93808ae 100644 (file)
        <classpathentry kind="lib" path="lib/jfreesvg-2.1.jar"/>
        <classpathentry kind="lib" path="lib/quaqua-filechooser-only-8.0.jar"/>
        <classpathentry kind="lib" path="lib/VAqua4.jar"/>
-       <classpathentry kind="lib" path="lib/httpclient-4.5.6.jar"/>
-       <classpathentry kind="lib" path="lib/httpcore-4.4.10.jar"/>
-       <classpathentry kind="lib" path="lib/httpmime-4.5.6.jar"/>
-       <classpathentry kind="lib" path="lib/commons-codec-1.10.jar"/>
-       <classpathentry kind="lib" path="lib/json-20180130.jar"/>
+       <classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
+       <classpathentry kind="lib" path="lib/httpclient-4.0.3.jar"/>
+       <classpathentry kind="lib" path="lib/httpcore-4.0.1.jar"/>
+       <classpathentry kind="lib" path="lib/httpmime-4.0.3.jar"/>
+       <classpathentry kind="lib" path="lib/jersey-json-1.19.jar"/>
+       <classpathentry kind="lib" path="lib/gnujpdf.jar"/>
        <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/plugin"/>
        <classpathentry kind="lib" path="lib/xml-apis.jar"/>
        <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
diff --git a/lib/gnujpdf.jar b/lib/gnujpdf.jar
new file mode 100644 (file)
index 0000000..8b325ea
Binary files /dev/null and b/lib/gnujpdf.jar differ
index a9fd70c..51e9ce5 100644 (file)
@@ -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);
index 2c5684a..56d993f 100644 (file)
@@ -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!
    */
index e71d548..9bc5c6a 100755 (executable)
@@ -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)
   {
 
index 7840261..ed16e11 100755 (executable)
@@ -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())