3 import jalview.api.FeatureRenderer;
4 import jalview.datamodel.AlignmentExportData;
5 import jalview.datamodel.SequenceI;
6 import jalview.gui.AlignViewport;
7 import jalview.gui.AlignmentPanel;
8 import jalview.gui.HTMLOptions;
9 import jalview.math.AlignmentDimension;
10 import jalview.util.MessageManager;
12 import java.awt.Color;
13 import java.awt.FontMetrics;
14 import java.awt.Graphics;
15 import java.awt.print.Printable;
16 import java.awt.print.PrinterException;
18 import java.io.FileOutputStream;
20 import org.jfree.graphics2d.svg.SVGGraphics2D;
21 import org.jfree.graphics2d.svg.SVGHints;
23 public class HtmlSvgOutput
31 public HtmlSvgOutput(File file, AlignmentPanel ap)
35 fr = ap.cloneFeatureRenderer();
36 generateHtmlSvgOutput(file);
39 public void generateHtmlSvgOutput(File file)
44 * && !(System.getProperty("java.awt.headless") != null
46 * .getProperty("java.awt.headless").equals("true"))
50 JalviewFileChooser chooser = getHTMLChooser();
51 chooser.setFileView(new jalview.io.JalviewFileView());
52 chooser.setDialogTitle(ap.alignFrame.getTitle());
53 chooser.setToolTipText(MessageManager.getString("action.save"));
54 int value = chooser.showSaveDialog(ap.alignFrame);
56 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
58 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
59 .getSelectedFile().getParent());
60 file = chooser.getSelectedFile();
64 AlignmentDimension aDimension = ap.getAlignmentDimension();
65 SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
66 aDimension.getHeight());
67 SVGGraphics2D g2 = new SVGGraphics2D(aDimension.getWidth(),
68 aDimension.getHeight());
70 String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
73 // If we need to prompt, and if the GUI is visible then
74 // Prompt for rendering style
75 if (renderStyle.equalsIgnoreCase("Prompt each time")
76 && !(System.getProperty("java.awt.headless") != null && System
77 .getProperty("java.awt.headless").equals("true")))
79 HTMLOptions svgOption = new HTMLOptions();
80 renderStyle = svgOption.getValue();
82 if (renderStyle == null || svgOption.cancelled)
88 if (renderStyle.equalsIgnoreCase("lineart"))
90 g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
91 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
92 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
93 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
95 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
98 String titleSvgData = g1.getSVGDocument();
99 String alignSvgData = g2.getSVGDocument();
101 AlignmentExportData exportData = ap.alignFrame.getAlignmentForExport(
102 JSONFile.FILE_DESC, av);
103 if (exportData.getSettings().isCancelled())
107 String jsonData = new FormatAdapter(ap, exportData.getSettings())
108 .formatSequences(JSONFile.FILE_DESC, exportData
109 .getAlignment(), exportData.getOmitHidden(),
110 exportData.getStartEndPostions(), ap
111 .getAlignViewport().getColumnSelection());
113 // String jsonData = JSONFile.getJSONData(ap);
114 String htmlData = getHtml(titleSvgData, alignSvgData, jsonData);
116 FileOutputStream out = new FileOutputStream(file);
117 out.write(htmlData.getBytes());
120 if (!(System.getProperty("java.awt.headless") != null && System
121 .getProperty("java.awt.headless").equals("true")))
123 jalview.util.BrowserLauncher.openURL("file:///" + file);
125 } catch (Exception e)
131 static JalviewFileChooser getHTMLChooser()
133 return new jalview.io.JalviewFileChooser(
134 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
135 { "html" }, new String[]
136 { "Hypertext Markup Language" }, "Hypertext Markup Language");
139 public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
140 throws PrinterException
142 int idWidth = ap.getVisibleIdWidth(false);
143 FontMetrics fm = ap.getFontMetrics(av.getFont());
144 int scaleHeight = av.getCharHeight() + fm.getDescent();
146 pg[0].setColor(Color.white);
147 pg[0].fillRect(0, 0, pwidth, pheight);
148 pg[0].setFont(av.getFont());
150 // //////////////////////////////////
151 // / How many sequences and residues can we fit on a printable page?
152 int totalRes = (pwidth - idWidth) / av.getCharWidth();
153 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
154 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
156 // ///////////////////////////
157 // / Only print these sequences and residues on this page
160 // ///////////////////////////
161 // / Only print these sequences and residues on this page
164 // ///////////////////////////
165 // / Only print these sequences and residues on this page
168 // ///////////////////////////
169 // / Only print these sequences and residues on this page
171 startRes = (pi % pagesWide) * totalRes;
172 endRes = (startRes + totalRes) - 1;
174 if (endRes > (av.getAlignment().getWidth() - 1))
176 endRes = av.getAlignment().getWidth() - 1;
178 startSeq = (pi / pagesWide) * totalSeq;
179 endSeq = startSeq + totalSeq;
180 if (endSeq > av.getAlignment().getHeight())
182 endSeq = av.getAlignment().getHeight();
184 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
186 if (av.isShowAnnotation())
188 pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
190 pagesHigh /= pheight;
191 if (pi >= (pagesWide * pagesHigh))
193 return Printable.NO_SUCH_PAGE;
197 pg[1].translate(0, 0);
198 ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
200 pg[1].translate(-idWidth, scaleHeight);
204 Color currentColor = null;
205 Color currentTextColor = null;
206 pg[0].translate(0, scaleHeight);
207 pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
209 for (int i = startSeq; i < endSeq; i++)
211 seq = av.getAlignment().getSequenceAt(i);
212 if ((av.getSelectionGroup() != null)
213 && av.getSelectionGroup().getSequences(null).contains(seq))
215 currentColor = Color.gray;
216 currentTextColor = Color.black;
220 currentColor = av.getSequenceColour(seq);
221 currentTextColor = Color.black;
223 pg[0].setColor(currentColor);
224 pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
226 pg[0].setColor(currentTextColor);
228 if (av.isRightAlignIds())
230 fm = pg[0].getFontMetrics();
232 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
236 seq.getDisplayId(av.getShowJVSuffix()),
238 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
239 - (av.getCharHeight() / 5));
241 pg[0].setFont(av.getFont());
242 pg[0].translate(idWidth, 0);
244 // draw main sequence panel
245 pg[1].translate(idWidth, 0);
246 ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
248 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
250 // draw annotation label - need to offset for current scroll position
251 int offset = -ap.getAlabels().getScrollOffset();
252 pg[0].translate(0, offset);
253 pg[0].translate(-idWidth - 3,
254 (endSeq - startSeq) * av.getCharHeight() + 3);
255 ap.getAlabels().drawComponent(pg[0], idWidth);
256 pg[0].translate(idWidth + 3, 0);
257 pg[0].translate(0, -offset);
259 // draw annotation - need to offset for current scroll position
260 pg[1].translate(0, offset);
261 pg[1].translate(-idWidth - 3,
262 (endSeq - startSeq) * av.getCharHeight() + 3);
263 pg[1].translate(idWidth + 3, 0);
264 ap.getAnnotationPanel().renderer.drawComponent(
265 ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
266 pg[1].translate(0, -offset);
269 return Printable.PAGE_EXISTS;
272 private String getHtml(String titleSvg, String alignmentSvg,
275 StringBuilder htmlSvg = new StringBuilder();
276 htmlSvg.append("<html>\n");
277 if (jsonData != null)
279 htmlSvg.append("<button onclick=\"javascipt:openJalviewUsingCurrentUrl();\">Launch in Jalview</button>");
280 htmlSvg.append("<input type=\"hidden\" name=\"seqData\" id=\"seqData\" value='"
283 htmlSvg.append("\n<style type=\"text/css\"> "
284 + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
285 + "div.titlex{ width:11%; float: left; }\n"
286 + "div.align{ width:89%; float: right; }\n"
287 + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
288 + "object {pointer-events: none;}"
290 htmlSvg.append("<div>");
292 "<div class=\"titlex\">");
294 "<div class=\"sub-category-container\"> ")
297 .append("</div>\n\n<!-- ========================================================================================== -->\n\n");
299 "<div class=\"align\" >");
301 "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
302 .append(alignmentSvg)
303 .append("</div></div>")
305 htmlSvg.append("</div>");
307 htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
308 + "<script language=\"JavaScript\" type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
310 + "var subCatContainer = $(\".sub-category-container\");\n"
311 + "subCatContainer.scroll(\nfunction() {\n"
312 + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
314 htmlSvg.append("</script>\n");
316 // javascript for launching file in Jalview
318 htmlSvg.append("<script language=\"JavaScript\">\n");
319 htmlSvg.append("function openJalviewUsingCurrentUrl(){\n");
320 htmlSvg.append(" var json = JSON.parse(document.getElementById(\"seqData\").value);\n");
321 htmlSvg.append(" var jalviewVersion = json['appSettings'].version;\n");
322 htmlSvg.append(" var url = json['appSettings'].webStartUrl;\n");
323 htmlSvg.append(" var myForm = document.createElement(\"form\");\n\n");
324 htmlSvg.append(" var heap = document.createElement(\"input\");\n");
325 htmlSvg.append(" heap.setAttribute(\"name\", \"jvm-max-heap\") ;\n");
326 htmlSvg.append(" heap.setAttribute(\"value\", \"2G\");\n\n");
327 htmlSvg.append(" var target = document.createElement(\"input\");\n");
328 htmlSvg.append(" target.setAttribute(\"name\", \"open\");\n");
329 htmlSvg.append(" target.setAttribute(\"value\", document.URL);\n\n");
330 htmlSvg.append(" var jvVersion = document.createElement(\"input\");\n");
331 htmlSvg.append(" jvVersion.setAttribute(\"name\", \"version\") ;\n");
332 htmlSvg.append(" jvVersion.setAttribute(\"value\", jalviewVersion);\n\n");
333 htmlSvg.append(" myForm.action = url;\n");
334 htmlSvg.append(" myForm.appendChild(heap);\n");
335 htmlSvg.append(" myForm.appendChild(target);\n");
336 htmlSvg.append(" myForm.appendChild(jvVersion);\n");
337 htmlSvg.append(" document.body.appendChild(myForm);\n");
338 htmlSvg.append(" myForm.submit() ;\n");
339 htmlSvg.append(" document.body.removeChild(myForm);\n");
340 htmlSvg.append("}\n");
341 htmlSvg.append("</script>\n");
342 htmlSvg.append("</hmtl>");
343 return htmlSvg.toString();