X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FImageMaker.java;h=016feee7f72dbdde8ca2544928bd18cad38564be;hb=ec24991b1786e17158a43f713c8ae9c4f8647393;hp=731de635b90e87d34e5ef66dfdd0fd33a16e6c98;hpb=c19d2a91ca05e052e3408bf5852d88eb5d0608f1;p=jalview.git diff --git a/src/jalview/util/ImageMaker.java b/src/jalview/util/ImageMaker.java index 731de63..016feee 100755 --- a/src/jalview/util/ImageMaker.java +++ b/src/jalview/util/ImageMaker.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2) - * Copyright (C) 2015 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. * @@ -20,18 +20,13 @@ */ package jalview.util; -import jalview.bin.Jalview; -import jalview.gui.EPSOptions; -import jalview.gui.SVGOptions; -import jalview.io.JalviewFileChooser; - -import java.awt.Component; 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 javax.imageio.ImageIO; @@ -39,11 +34,23 @@ import org.jfree.graphics2d.svg.SVGGraphics2D; import org.jfree.graphics2d.svg.SVGHints; import org.jibble.epsgraphics.EpsGraphics2D; +import jalview.io.JalviewFileChooser; + public class ImageMaker { - EpsGraphics2D pg; + 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 String EPS_EXTENSION = "eps"; - SVGGraphics2D g2; + public static final String PNG_EXTENSION = "png"; + + public static final String PNG_DESCRIPTION = "Portable network graphics"; + + EpsGraphics2D pg; Graphics graphics; @@ -55,21 +62,26 @@ public class ImageMaker public enum TYPE { - EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG( - "PNG", MessageManager.getString("label.png_image"), - getPNGChooser()), SVG("SVG", "SVG", getSVGChooser()); + 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); + + public final String name; - private JalviewFileChooser chooser; + public final String label; - private String name; + public final String extension; - private String label; + public final String description; - TYPE(String name, String label, JalviewFileChooser chooser) + TYPE(String name, String label, String ext, String desc) { this.name = name; this.label = label; - this.chooser = chooser; + this.extension = ext; + this.description = desc; } public String getName() @@ -77,9 +89,9 @@ public class ImageMaker return name; } - public JalviewFileChooser getChooser() + public JalviewFileChooser getFileChooser() { - return chooser; + return new JalviewFileChooser(extension, description); } public String getLabel() @@ -89,50 +101,37 @@ public class ImageMaker } - public ImageMaker(Component parent, TYPE type, String title, int width, - int height, File file, String fileTitle) + /** + * 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, float bitmapscale, + int bitmapwidth, int bitmapheight) throws IOException { - this.type = type; + this.type = imageType; - if (file == null) + out = new FileOutputStream(file); + switch (imageType) { - JalviewFileChooser chooser; - chooser = type.getChooser(); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle(title); - chooser.setToolTipText(MessageManager.getString("action.save")); - int value = chooser.showSaveDialog(parent); - - if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION) - { - jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser - .getSelectedFile().getParent()); - file = chooser.getSelectedFile(); - } - } - - if (file != null) - { - try - { - out = new FileOutputStream(file); - if (type == TYPE.SVG) - { - setupSVG(width, height, fileTitle); - } - else if (type == TYPE.EPS) - { - setupEPS(width, height, fileTitle); - } - else if (type == TYPE.PNG) - { - setupPNG(width, height); - } - - } catch (Exception ex) - { - System.out.println("Error creating " + type.getName() + " file."); - } + case SVG: + setupSVG(width, height, useLineart); + break; + case EPS: + setupEPS(width, height, fileTitle, useLineart); + break; + case PNG: + setupPNG(width, height, bitmapscale, bitmapwidth, bitmapheight); + break; + default: } } @@ -141,6 +140,11 @@ public class ImageMaker 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 @@ -158,7 +162,7 @@ public class ImageMaker out.close(); break; case PNG: - ImageIO.write(bi, "png", out); + ImageIO.write(bi, PNG_EXTENSION, out); out.flush(); out.close(); break; @@ -169,127 +173,98 @@ 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, float scale, + int bitmapwidth, int bitmapheight) { - boolean accurateText = true; + if (width == 0 || height == 0) + return; - String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING", - "Prompt each time"); + float usescale = 0.0f; + int usewidth = width; + int useheight = 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"))) + // use the smallest positive scale (i.e. fit in the box) + if (scale > 0.0f) { - EPSOptions eps = new EPSOptions(); - renderStyle = eps.getValue(); - - if (renderStyle == null || eps.cancelled) - { - return; - } + usescale = scale; + usewidth = Math.round(scale * width); + useheight = Math.round(scale * height); } - - if (renderStyle.equalsIgnoreCase("text")) + if (bitmapwidth > 0) { - accurateText = false; + float wscale = (float) bitmapwidth / width; + if (wscale > 0.0f && (usescale == 0.0f || wscale < usescale)) + { + usescale = wscale; + usewidth = bitmapwidth; + useheight = Math.round(usescale * height); + } } - - try - { - pg = new EpsGraphics2D(title, out, 0, 0, width, height); - Graphics2D ig2 = pg; - ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - - pg.setAccurateTextMode(accurateText); - - graphics = pg; - } catch (Exception ex) + 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; + } } - } - - void setupPNG(int width, int height) - { - bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + 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) + ig2.scale(usescale, usescale); } - void setupSVG(int width, int height, String title) + /** + * 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) { - - g2 = new SVGGraphics2D(width, height); - Graphics2D ig2 = g2; - - String renderStyle = jalview.bin.Cache.getDefault("SVG_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"))) - { - SVGOptions svgOption = new SVGOptions(); - renderStyle = svgOption.getValue(); - - if (renderStyle == null || svgOption.cancelled) - { - return; - } - } - - if (renderStyle.equalsIgnoreCase("lineart")) + SVGGraphics2D g2 = new SVGGraphics2D(width, height); + if (useLineart) { - ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, + g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); } - graphics = g2; } - static JalviewFileChooser getPNGChooser() - { - if (Jalview.isHeadlessMode()) - { - return null; - } - return new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), - new String[] { "png" }, - new String[] { "Portable network graphics" }, - "Portable network graphics"); - } - - static JalviewFileChooser getEPSChooser() + /** + * 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 { - if (Jalview.isHeadlessMode()) - { - return null; - } - return new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), - new String[] { "eps" }, - new String[] { "Encapsulated Postscript" }, - "Encapsulated Postscript"); - } - - static JalviewFileChooser getSVGChooser() - { - if (Jalview.isHeadlessMode()) - { - return null; - } - return new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), - new String[] { "svg" }, - new String[] { "Scalable Vector Graphics" }, - "Scalable Vector Graphics"); + 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; } }