/* * 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. * * 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 . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.io; import java.awt.Graphics; import java.awt.print.PrinterException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Locale; import java.util.concurrent.atomic.AtomicBoolean; import org.jfree.graphics2d.svg.SVGGraphics2D; import org.jfree.graphics2d.svg.SVGHints; import jalview.bin.Cache; import jalview.gui.AlignmentPanel; import jalview.gui.LineartOptions; import jalview.gui.OOMWarning; import jalview.math.AlignmentDimension; import jalview.util.MessageManager; public class HtmlSvgOutput extends HTMLOutput { public HtmlSvgOutput(AlignmentPanel ap) { super(ap, "HTML"); } public int printUnwrapped(int pwidth, int pheight, int pi, Graphics idGraphics, Graphics alignmentGraphics) throws PrinterException { return ap.printUnwrapped(pwidth, pheight, pi, idGraphics, alignmentGraphics); } public int printWrapped(int pwidth, int pheight, int pi, Graphics... pg) throws PrinterException { return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]); } String getHtml(String titleSvg, String alignmentSvg, String jsonData, boolean wrapped) { StringBuilder htmlSvg = new StringBuilder(); htmlSvg.append("\n"); if (jsonData != null) { htmlSvg.append( "  "); htmlSvg.append( ""); htmlSvg.append( "
" + jsonData + "
"); htmlSvg.append("
 "); } htmlSvg.append("\n"); if (!wrapped) { htmlSvg.append("
"); htmlSvg.append("
\n"); htmlSvg.append("
\n"); htmlSvg.append(titleSvg); htmlSvg.append("
"); htmlSvg.append( "
\n\n\n\n"); htmlSvg.append("
"); htmlSvg.append( "
") .append(alignmentSvg).append("
").append("
"); htmlSvg.append("
"); htmlSvg.append( "\n" + "\n" + "\n"); } else { htmlSvg.append("
\n").append(alignmentSvg).append("
"); htmlSvg.append( "\n" + "\n"); } // javascript for launching file in Jalview htmlSvg.append("\n"); htmlSvg.append(""); return htmlSvg.toString(); } @Override public boolean isEmbedData() { return Boolean .valueOf(Cache.getDefault("EXPORT_EMBBED_BIOJSON", "true")); } @Override public boolean isLaunchInBrowserAfterExport() { return true; } @Override public void run() { run(null); } @Override public void run(String renderer) { try { String renderStyle = renderer == null ? Cache.getDefault("HTML_RENDERING", LineartOptions.PROMPT_EACH_TIME) : renderer; AtomicBoolean textOption = new AtomicBoolean( !"lineart".equals(renderStyle.toLowerCase(Locale.ROOT))); /* * configure the action to run on OK in the dialog */ Runnable okAction = () -> { doOutput(textOption.get()); }; /* * Prompt for character rendering style if preference is not set */ if (renderStyle.equalsIgnoreCase(LineartOptions.PROMPT_EACH_TIME) && !isHeadless()) { LineartOptions svgOption = new LineartOptions("HTML", textOption); svgOption.setResponseAction(1, () -> { setProgressMessage(MessageManager.formatMessage( "status.cancelled_image_export_operation", getDescription())); }); svgOption.setResponseAction(0, okAction); svgOption.showDialog(); /* no code here - JalviewJS cannot execute it */ } else { /* * else (if preference set) just do the export action */ doOutput(textOption.get()); } } catch (OutOfMemoryError err) { jalview.bin.Console .outPrintln("########################\n" + "OUT OF MEMORY " + generatedFile + "\n" + "########################"); new OOMWarning("Creating Image for " + generatedFile, err); } catch (Exception e) { e.printStackTrace(); setProgressMessage(MessageManager .formatMessage("info.error_creating_file", getDescription())); } } /** * Builds and writes the image to the file specified by field * generatedFile * * @param textCharacters * true for Text character rendering, false for Lineart */ protected void doOutput(boolean textCharacters) { try { AlignmentDimension aDimension = ap.getAlignmentDimension(); SVGGraphics2D idPanelGraphics = new SVGGraphics2D( aDimension.getWidth(), aDimension.getHeight()); SVGGraphics2D alignPanelGraphics = new SVGGraphics2D( aDimension.getWidth(), aDimension.getHeight()); if (!textCharacters) // Lineart selected { idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); alignPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); } if (ap.av.getWrapAlignment()) { printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0, alignPanelGraphics); } else { printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, idPanelGraphics, alignPanelGraphics); } String idPanelSvgData = idPanelGraphics.getSVGDocument(); String alignPanelSvgData = alignPanelGraphics.getSVGDocument(); String jsonData = getBioJSONData(); String htmlData = getHtml(idPanelSvgData, alignPanelSvgData, jsonData, ap.av.getWrapAlignment()); FileOutputStream out = new FileOutputStream(generatedFile); out.write(htmlData.getBytes()); out.flush(); out.close(); setProgressMessage(MessageManager .formatMessage("status.export_complete", getDescription())); exportCompleted(); } catch (Exception e) { e.printStackTrace(); setProgressMessage(MessageManager .formatMessage("info.error_creating_file", getDescription())); } } }