JAL-4274 Use Cache BITMAP_* if no other image restrictions given
[jalview.git] / src / jalview / util / ImageMaker.java
index 18d8436..fa473f3 100755 (executable)
@@ -1,40 +1,56 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.util;
 
-import java.io.*;
-import javax.imageio.*;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
 
-import java.awt.*;
-import java.awt.image.*;
+import javax.imageio.ImageIO;
 
-import org.jibble.epsgraphics.*;
-import jalview.gui.*;
-import jalview.io.*;
+import org.jfree.graphics2d.svg.SVGGraphics2D;
+import org.jfree.graphics2d.svg.SVGHints;
+import org.jibble.epsgraphics.EpsGraphics2D;
+
+import jalview.bin.Console;
+import jalview.io.JalviewFileChooser;
+import jalview.util.imagemaker.BitmapImageSizing;
 
 public class ImageMaker
 {
-  public static final int EPS = 0;
+  public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
+
+  public static final String SVG_EXTENSION = "svg";
+
+  public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
 
-  public static final int PNG = 1;
+  public static final String EPS_EXTENSION = "eps";
 
-  int type = -1;
+  public static final String PNG_EXTENSION = "png";
+
+  public static final String PNG_DESCRIPTION = "Portable  network graphics";
 
   EpsGraphics2D pg;
 
@@ -44,67 +60,93 @@ public class ImageMaker
 
   BufferedImage bi;
 
-  public ImageMaker(Component parent, int type, String title, int width,
-          int height, File file, String EPStitle)
+  TYPE type;
+
+  public enum TYPE
   {
-    this.type = type;
+    EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
+            EPS_DESCRIPTION),
+    PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
+            PNG_DESCRIPTION),
+    SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
 
-    if (file == null)
-    {
-      JalviewFileChooser chooser;
-      chooser = type == EPS ? getEPSChooser() : getPNGChooser();
+    public final String name;
 
-      chooser.setFileView(new jalview.io.JalviewFileView());
-      chooser.setDialogTitle(title);
-      chooser.setToolTipText("Save");
+    public final String label;
 
-      int value = chooser.showSaveDialog(parent);
+    public final String extension;
 
-      if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
-      {
-        jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
-                .getSelectedFile().getParent());
+    public final String description;
 
-        file = chooser.getSelectedFile();
-      }
+    TYPE(String name, String label, String ext, String desc)
+    {
+      this.name = name;
+      this.label = label;
+      this.extension = ext;
+      this.description = desc;
     }
 
-    if (file != null)
+    public String getName()
     {
-      try
-      {
-        out = new FileOutputStream(file);
-
-        if (type == EPS)
-        {
-          setupEPS(width, height, EPStitle);
-        }
-        else
-        {
-          setupPNG(width, height);
-        }
-      } catch (Exception ex)
-      {
-        System.out.println("Error creating "
-                + (type == EPS ? "EPS" : "PNG") + " file.");
-      }
+      return name;
+    }
+
+    public JalviewFileChooser getFileChooser()
+    {
+      return new JalviewFileChooser(extension, description);
     }
+
+    public String getLabel()
+    {
+      return label;
+    }
+
   }
 
-  public Graphics getGraphics()
+  /**
+   * Constructor configures the graphics context ready for writing to
+   * 
+   * @param imageType
+   * @param width
+   * @param height
+   * @param file
+   * @param fileTitle
+   * @param useLineart
+   * @param bitmapscale
+   * @throws IOException
+   */
+  public ImageMaker(TYPE imageType, int width, int height, File file,
+          String fileTitle, boolean useLineart, BitmapImageSizing userBis)
+          throws IOException
   {
-    return graphics;
+    this.type = imageType;
+
+    out = new FileOutputStream(file);
+    switch (imageType)
+    {
+    case SVG:
+      setupSVG(width, height, useLineart);
+      break;
+    case EPS:
+      setupEPS(width, height, fileTitle, useLineart);
+      break;
+    case PNG:
+      setupPNG(width, height, userBis);
+      break;
+    default:
+    }
   }
 
-  void setupPNG(int width, int height)
+  public Graphics getGraphics()
   {
-    bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-    graphics = bi.getGraphics();
-    Graphics2D ig2 = (Graphics2D) graphics;
-    ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-            RenderingHints.VALUE_ANTIALIAS_ON);
+    return graphics;
   }
 
+  /**
+   * For SVG or PNG, writes the generated graphics data to the file output
+   * stream. For EPS, flushes the output graphics (which is written to file as
+   * it is generated).
+   */
   public void writeImage()
   {
     try
@@ -115,8 +157,15 @@ public class ImageMaker
         pg.flush();
         pg.close();
         break;
+      case SVG:
+        String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
+        out.write(svgData.getBytes());
+        out.flush();
+        out.close();
+        break;
       case PNG:
-        ImageIO.write(bi, "png", out);
+        ImageIO.write(bi, PNG_EXTENSION, out);
+        out.flush();
         out.close();
         break;
       }
@@ -126,61 +175,212 @@ public class ImageMaker
     }
   }
 
-  void setupEPS(int width, int height, String title)
+  /**
+   * Sets up a graphics object for the PNG image to be written on
+   * 
+   * @param width
+   * @param height
+   * @param scale
+   */
+  protected void setupPNG(int width, int height, BitmapImageSizing userBis)
   {
-    boolean accurateText = true;
+    if (width == 0 || height == 0)
+      return;
 
-    String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
-            "Prompt each time");
+    BitmapImageSizing bis = ImageMaker.getScaleWidthHeight(width, height,
+            userBis);
+    float usescale = bis.scale();
+    int usewidth = bis.width();
+    int useheight = bis.height();
 
-    // 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")))
+    bi = new BufferedImage(usewidth, useheight, BufferedImage.TYPE_INT_RGB);
+    graphics = bi.getGraphics();
+    Graphics2D ig2 = (Graphics2D) graphics;
+    ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+            RenderingHints.VALUE_ANTIALIAS_ON);
+    if (usescale > 0.0f)
     {
-      EPSOptions eps = new EPSOptions();
-      renderStyle = eps.getValue();
-
-      if (renderStyle == null || eps.cancelled)
-      {
-        return;
-      }
+      ig2.scale(usescale, usescale);
     }
+  }
 
-    if (renderStyle.equalsIgnoreCase("text"))
+  /**
+   * A helper method to configure the SVG output graphics, with choice of Text
+   * or Lineart character rendering
+   * 
+   * @param width
+   * @param height
+   * @param useLineart
+   *          true for Lineart character rendering, false for Text
+   */
+  protected void setupSVG(int width, int height, boolean useLineart)
+  {
+    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
+    if (useLineart)
     {
-      accurateText = false;
+      g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
+              SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
     }
+    graphics = g2;
+  }
 
-    try
-    {
-      pg = new EpsGraphics2D(title, out, 0, 0, width, height);
-      Graphics2D ig2 = (Graphics2D) pg;
-      ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-              RenderingHints.VALUE_ANTIALIAS_ON);
+  /**
+   * A helper method that sets up the EPS graphics output with user choice of
+   * Text or Lineart character rendering
+   * 
+   * @param width
+   * @param height
+   * @param title
+   * @param useLineart
+   *          true for Lineart character rendering, false for Text
+   * @throws IOException
+   */
+  protected void setupEPS(int width, int height, String title,
+          boolean useLineart) throws IOException
+  {
+    pg = new EpsGraphics2D(title, out, 0, 0, width, height);
+    Graphics2D ig2 = pg;
+    ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+            RenderingHints.VALUE_ANTIALIAS_ON);
+    pg.setAccurateTextMode(useLineart);
+    graphics = pg;
+  }
 
-      pg.setAccurateTextMode(accurateText);
+  /**
+   * Takes initial width and height, and suggested float scale, int width, int
+   * height and create a bounding box returned as a BitmapImageSizing object
+   * with consistent scale, width, height fields.
+   * 
+   * @param width
+   *          The unscaled image width
+   * @param height
+   *          The unscaled image height
+   * @param scale
+   *          The suggested scaling
+   * @param bitmapwidth
+   *          The suggested width
+   * @param bitmapheight
+   *          The suggested height
+   * @return BitmapImageSizing A consistent scale,width and height for the final
+   *         image
+   */
+  public static BitmapImageSizing getScaleWidthHeight(int width, int height,
+          float scale, int bitmapwidth, int bitmapheight)
+  {
+    float usescale = 0.0f;
+    int usewidth = width;
+    int useheight = height;
 
-      graphics = pg;
-    } catch (Exception ex)
+    if ((width == 0 && bitmapwidth > 0)
+            || (height == 0 && bitmapheight > 0))
+    {
+      // original image is zero sized! Avoid dividing by zero!
+      return BitmapImageSizing.nullBitmapImageSizing();
+    }
+
+    // use the smallest positive scale (i.e. fit in the box)
+    if (scale > 0.0f)
     {
+      usescale = scale;
+      usewidth = Math.round(scale * width);
+      useheight = Math.round(scale * height);
     }
+    if (bitmapwidth > 0)
+    {
+      float wscale = (float) bitmapwidth / width;
+      if (wscale > 0.0f && (usescale == 0.0f || wscale < usescale))
+      {
+        usescale = wscale;
+        usewidth = bitmapwidth;
+        useheight = Math.round(usescale * height);
+      }
+    }
+    if (bitmapheight > 0)
+    {
+      float hscale = (float) bitmapheight / height;
+      if (hscale > 0.0f && (usescale == 0.0f || hscale < usescale))
+      {
+        usescale = hscale;
+        usewidth = Math.round(usescale * width);
+        useheight = bitmapheight;
+      }
+    }
+    return new BitmapImageSizing(usescale, usewidth, useheight, false);
   }
 
-  JalviewFileChooser getPNGChooser()
+  /**
+   * Takes suggested scale, width, height as a BitmapImageSizing object and
+   * create a bounding box returned as a BitmapImageSizing object with
+   * consistent scale, width, height fields.
+   * 
+   * @param bis
+   * @return BitmapImageSizing
+   */
+  public static BitmapImageSizing getScaleWidthHeight(int width, int height,
+          BitmapImageSizing bis)
   {
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "png" }, new String[]
-            { "Portable network graphics" }, "Portable network graphics");
+    return ImageMaker.getScaleWidthHeight(width, height, bis.scale(),
+            bis.width(), bis.height());
   }
 
-  JalviewFileChooser getEPSChooser()
+  /**
+   * Takes String versions of suggested float scale, int width, int height and
+   * create a bounding box returned as a BitmapImageSizing object with
+   * consistent scale, width, height fields.
+   * 
+   * @param scaleS
+   * @param widthS
+   * @param heightS
+   * @return BitmapImageSizing
+   */
+  public static BitmapImageSizing parseScaleWidthHeightStrings(
+          String scaleS, String widthS, String heightS)
   {
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "eps" }, new String[]
-            { "Encapsulated Postscript" }, "Encapsulated Postscript");
+    if (scaleS == null && widthS == null && heightS == null)
+    {
+      // if all items are null (i.e. not provided) we use the dynamic
+      // preferences set BIS
+      return BitmapImageSizing.defaultBitmapImageSizing();
+    }
+
+    float scale = 0.0f;
+    int width = 0;
+    int height = 0;
+
+    if (scaleS != null)
+    {
+      try
+      {
+        scale = Float.parseFloat(scaleS);
+      } catch (NumberFormatException e)
+      {
+        Console.warn("Did not understand scale '" + scaleS
+                + "', won't be used.");
+      }
+    }
+    if (widthS != null)
+    {
+      try
+      {
+        width = Integer.parseInt(widthS);
+      } catch (NumberFormatException e)
+      {
+        Console.warn("Did not understand width '" + widthS
+                + "', won't be used.");
+      }
+    }
+    if (heightS != null)
+    {
+      try
+      {
+        height = Integer.parseInt(heightS);
+      } catch (NumberFormatException e)
+      {
+        Console.warn("Did not understand height '" + heightS
+                + "', won't be used.");
+      }
+    }
+
+    return new BitmapImageSizing(scale, width, height, false);
   }
 }