3 import jalview.api.FeatureRenderer;
4 import jalview.datamodel.SequenceI;
5 import jalview.gui.AlignViewport;
6 import jalview.gui.AlignmentPanel;
7 import jalview.gui.HTMLOptions;
8 import jalview.math.AlignmentDimension;
9 import jalview.util.MessageManager;
11 import java.awt.Color;
12 import java.awt.FontMetrics;
13 import java.awt.Graphics;
14 import java.awt.print.Printable;
15 import java.awt.print.PrinterException;
17 import java.io.FileOutputStream;
19 import org.jfree.graphics2d.svg.SVGGraphics2D;
20 import org.jfree.graphics2d.svg.SVGHints;
22 public class HtmlSvgOutput
30 public HtmlSvgOutput(File file, AlignmentPanel ap)
34 fr = ap.cloneFeatureRenderer();
35 generateHtmlSvgOutput(file);
38 public void generateHtmlSvgOutput(File file)
43 * && !(System.getProperty("java.awt.headless") != null
45 * .getProperty("java.awt.headless").equals("true"))
49 JalviewFileChooser chooser = getHTMLChooser();
50 chooser.setFileView(new jalview.io.JalviewFileView());
51 chooser.setDialogTitle(ap.alignFrame.getTitle());
52 chooser.setToolTipText(MessageManager.getString("action.save"));
53 int value = chooser.showSaveDialog(ap.alignFrame);
55 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
57 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
58 .getSelectedFile().getParent());
59 file = chooser.getSelectedFile();
63 AlignmentDimension aDimension = ap.getAlignmentDimension();
64 SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
65 aDimension.getHeight());
66 SVGGraphics2D g2 = new SVGGraphics2D(aDimension.getWidth(),
67 aDimension.getHeight());
69 String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
72 // If we need to prompt, and if the GUI is visible then
73 // Prompt for rendering style
74 if (renderStyle.equalsIgnoreCase("Prompt each time")
75 && !(System.getProperty("java.awt.headless") != null && System
76 .getProperty("java.awt.headless").equals("true")))
78 HTMLOptions svgOption = new HTMLOptions();
79 renderStyle = svgOption.getValue();
81 if (renderStyle == null || svgOption.cancelled)
87 if (renderStyle.equalsIgnoreCase("lineart"))
89 g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
90 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
91 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
92 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
94 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
96 FileOutputStream out = new FileOutputStream(file);
98 String titleSvgData = g1.getSVGDocument();
99 String alignSvgData = g2.getSVGDocument();
100 String jsonData = JSONFile.getJSONData(ap);
101 String htmlData = getHtml(titleSvgData, alignSvgData, jsonData);
103 out.write(htmlData.getBytes());
106 if (!(System.getProperty("java.awt.headless") != null && System
107 .getProperty("java.awt.headless").equals("true")))
109 jalview.util.BrowserLauncher.openURL("file:///" + file);
111 } catch (Exception e)
117 static JalviewFileChooser getHTMLChooser()
119 return new jalview.io.JalviewFileChooser(
120 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
121 { "html" }, new String[]
122 { "Hypertext Markup Language" }, "Hypertext Markup Language");
125 public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
126 throws PrinterException
128 int idWidth = ap.getVisibleIdWidth(false);
129 FontMetrics fm = ap.getFontMetrics(av.getFont());
130 int scaleHeight = av.getCharHeight() + fm.getDescent();
132 pg[0].setColor(Color.white);
133 pg[0].fillRect(0, 0, pwidth, pheight);
134 pg[0].setFont(av.getFont());
136 // //////////////////////////////////
137 // / How many sequences and residues can we fit on a printable page?
138 int totalRes = (pwidth - idWidth) / av.getCharWidth();
139 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
140 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
142 // ///////////////////////////
143 // / Only print these sequences and residues on this page
146 // ///////////////////////////
147 // / Only print these sequences and residues on this page
150 // ///////////////////////////
151 // / Only print these sequences and residues on this page
154 // ///////////////////////////
155 // / Only print these sequences and residues on this page
157 startRes = (pi % pagesWide) * totalRes;
158 endRes = (startRes + totalRes) - 1;
160 if (endRes > (av.getAlignment().getWidth() - 1))
162 endRes = av.getAlignment().getWidth() - 1;
164 startSeq = (pi / pagesWide) * totalSeq;
165 endSeq = startSeq + totalSeq;
166 if (endSeq > av.getAlignment().getHeight())
168 endSeq = av.getAlignment().getHeight();
170 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
172 if (av.isShowAnnotation())
174 pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
176 pagesHigh /= pheight;
177 if (pi >= (pagesWide * pagesHigh))
179 return Printable.NO_SUCH_PAGE;
183 pg[1].translate(0, 0);
184 ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
186 pg[1].translate(-idWidth, scaleHeight);
190 Color currentColor = null;
191 Color currentTextColor = null;
192 pg[0].translate(0, scaleHeight);
193 pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
195 for (int i = startSeq; i < endSeq; i++)
197 seq = av.getAlignment().getSequenceAt(i);
198 if ((av.getSelectionGroup() != null)
199 && av.getSelectionGroup().getSequences(null).contains(seq))
201 currentColor = Color.gray;
202 currentTextColor = Color.black;
206 currentColor = av.getSequenceColour(seq);
207 currentTextColor = Color.black;
209 pg[0].setColor(currentColor);
210 pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
212 pg[0].setColor(currentTextColor);
214 if (av.isRightAlignIds())
216 fm = pg[0].getFontMetrics();
218 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
222 seq.getDisplayId(av.getShowJVSuffix()),
224 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
225 - (av.getCharHeight() / 5));
227 pg[0].setFont(av.getFont());
228 pg[0].translate(idWidth, 0);
230 // draw main sequence panel
231 pg[1].translate(idWidth, 0);
232 ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
234 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
236 // draw annotation label - need to offset for current scroll position
237 int offset = -ap.getAlabels().getScrollOffset();
238 pg[0].translate(0, offset);
239 pg[0].translate(-idWidth - 3,
240 (endSeq - startSeq) * av.getCharHeight() + 3);
241 ap.getAlabels().drawComponent(pg[0], idWidth);
242 pg[0].translate(idWidth + 3, 0);
243 pg[0].translate(0, -offset);
245 // draw annotation - need to offset for current scroll position
246 pg[1].translate(0, offset);
247 pg[1].translate(-idWidth - 3,
248 (endSeq - startSeq) * av.getCharHeight() + 3);
249 pg[1].translate(idWidth + 3, 0);
250 ap.getAnnotationPanel().renderer.drawComponent(
251 ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
252 pg[1].translate(0, -offset);
255 return Printable.PAGE_EXISTS;
258 private String getHtml(String titleSvg, String alignmentSvg,
261 StringBuilder htmlSvg = new StringBuilder();
262 htmlSvg.append("<html>\n");
263 if (jsonData != null)
265 htmlSvg.append("<input type=\"hidden\" name=\"seqData\" id=\"seqData\" value='"
268 htmlSvg.append("\n<style type=\"text/css\"> "
269 + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
270 + "div.titlex{ width:11%; float: left; }\n"
271 + "div.align{ width:89%; float: right; }\n"
272 + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
273 + "object {pointer-events: none;}"
275 htmlSvg.append("<div>");
277 "<div class=\"titlex\">");
279 "<div class=\"sub-category-container\"> ")
282 .append("</div>\n\n<!-- ========================================================================================== -->\n\n");
284 "<div class=\"align\" >");
286 "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
287 .append(alignmentSvg)
288 .append("</div></div>")
290 htmlSvg.append("</div>");
292 htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
293 + "<script language=\"JavaScript\" type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
295 + "var subCatContainer = $(\".sub-category-container\");\n"
296 + "subCatContainer.scroll(\nfunction() {\n"
297 + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
299 htmlSvg.append("</script></hmtl>");
301 return htmlSvg.toString();