3 import jalview.datamodel.SequenceI;
4 import jalview.gui.AlignViewport;
5 import jalview.gui.AlignmentPanel;
6 import jalview.gui.AnnotationPanel;
7 import jalview.gui.FeatureRenderer;
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
30 AnnotationPanel annotationPanel;
32 public HtmlSvgOutput(File file, AlignmentPanel ap)
37 this.annotationPanel = ap.getAnnotationPanel();
38 generateHtmlSvgOutput(file);
41 public void generateHtmlSvgOutput(File file)
46 * && !(System.getProperty("java.awt.headless") != null
48 * .getProperty("java.awt.headless").equals("true"))
52 JalviewFileChooser chooser = getHTMLChooser();
53 chooser.setFileView(new jalview.io.JalviewFileView());
54 chooser.setDialogTitle(ap.alignFrame.getTitle());
55 chooser.setToolTipText(MessageManager.getString("action.save"));
56 int value = chooser.showSaveDialog(ap.alignFrame);
58 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
60 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
61 .getSelectedFile().getParent());
62 file = chooser.getSelectedFile();
66 AlignmentDimension aDimension = ap.getAlignmentDimension();
67 SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
68 aDimension.getHeight());
69 SVGGraphics2D g2 = new SVGGraphics2D(aDimension.getWidth(),
70 aDimension.getHeight());
72 String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
75 // If we need to prompt, and if the GUI is visible then
76 // Prompt for rendering style
77 if (renderStyle.equalsIgnoreCase("Prompt each time")
78 && !(System.getProperty("java.awt.headless") != null && System
79 .getProperty("java.awt.headless").equals("true")))
81 HTMLOptions svgOption = new HTMLOptions();
82 renderStyle = svgOption.getValue();
84 if (renderStyle == null || svgOption.cancelled)
90 if (renderStyle.equalsIgnoreCase("lineart"))
92 g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
93 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
94 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
95 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
97 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
99 FileOutputStream out = new FileOutputStream(file);
101 String titleSvgData = g1.getSVGDocument();
102 String alignSvgData = g2.getSVGDocument();
103 String htmlData = getHtml(titleSvgData, alignSvgData);
105 out.write(htmlData.getBytes());
108 if (!(System.getProperty("java.awt.headless") != null && System
109 .getProperty("java.awt.headless").equals("true")))
111 jalview.util.BrowserLauncher.openURL("file:///" + file);
113 } catch (Exception e)
119 static JalviewFileChooser getHTMLChooser()
121 return new jalview.io.JalviewFileChooser(
122 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
123 { "html" }, new String[]
124 { "Hypertext Markup Language" }, "Hypertext Markup Language");
127 public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
128 throws PrinterException
130 int idWidth = ap.getVisibleIdWidth(false);
131 FontMetrics fm = ap.getFontMetrics(av.getFont());
132 int scaleHeight = av.getCharHeight() + fm.getDescent();
134 pg[0].setColor(Color.white);
135 pg[0].fillRect(0, 0, pwidth, pheight);
136 pg[0].setFont(av.getFont());
138 // //////////////////////////////////
139 // / How many sequences and residues can we fit on a printable page?
140 int totalRes = (pwidth - idWidth) / av.getCharWidth();
141 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
142 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
144 // ///////////////////////////
145 // / Only print these sequences and residues on this page
148 // ///////////////////////////
149 // / Only print these sequences and residues on this page
152 // ///////////////////////////
153 // / Only print these sequences and residues on this page
156 // ///////////////////////////
157 // / Only print these sequences and residues on this page
159 startRes = (pi % pagesWide) * totalRes;
160 endRes = (startRes + totalRes) - 1;
162 if (endRes > (av.getAlignment().getWidth() - 1))
164 endRes = av.getAlignment().getWidth() - 1;
166 startSeq = (pi / pagesWide) * totalSeq;
167 endSeq = startSeq + totalSeq;
168 if (endSeq > av.getAlignment().getHeight())
170 endSeq = av.getAlignment().getHeight();
172 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
174 if (av.isShowAnnotation())
176 pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
178 pagesHigh /= pheight;
179 if (pi >= (pagesWide * pagesHigh))
181 return Printable.NO_SUCH_PAGE;
185 pg[1].translate(0, 0);
186 ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
188 pg[1].translate(-idWidth, scaleHeight);
192 Color currentColor = null;
193 Color currentTextColor = null;
194 pg[0].translate(0, scaleHeight);
195 pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
197 for (int i = startSeq; i < endSeq; i++)
199 seq = av.getAlignment().getSequenceAt(i);
200 if ((av.getSelectionGroup() != null)
201 && av.getSelectionGroup().getSequences(null).contains(seq))
203 currentColor = Color.gray;
204 currentTextColor = Color.black;
208 currentColor = av.getSequenceColour(seq);
209 currentTextColor = Color.black;
211 pg[0].setColor(currentColor);
212 pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
214 pg[0].setColor(currentTextColor);
216 if (av.isRightAlignIds())
218 fm = pg[0].getFontMetrics();
220 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
224 seq.getDisplayId(av.getShowJVSuffix()),
226 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
227 - (av.getCharHeight() / 5));
229 pg[0].setFont(av.getFont());
230 pg[0].translate(idWidth, 0);
232 // draw main sequence panel
233 pg[1].translate(idWidth, 0);
234 ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
236 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
238 // draw annotation label - need to offset for current scroll position
239 int offset = -ap.getAlabels().getScrollOffset();
240 pg[0].translate(0, offset);
241 pg[0].translate(-idWidth - 3,
242 (endSeq - startSeq) * av.getCharHeight() + 3);
243 ap.getAlabels().drawComponent(pg[0], idWidth);
244 pg[0].translate(idWidth + 3, 0);
245 pg[0].translate(0, -offset);
247 // draw annotation - need to offset for current scroll position
248 pg[1].translate(0, offset);
249 pg[1].translate(-idWidth - 3,
250 (endSeq - startSeq) * av.getCharHeight() + 3);
251 pg[1].translate(idWidth + 3, 0);
252 ap.getAnnotationPanel().renderer.drawComponent(
253 ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
254 pg[1].translate(0, -offset);
257 return Printable.PAGE_EXISTS;
260 private String getHtml(String titleSvg, String alignmentSvg)
262 StringBuilder htmlSvg = new StringBuilder();
263 htmlSvg.append("<html>"
264 + "<style type=\"text/css\"> "
265 + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
266 + "div.titlex{ width:11%; float: left; }\n"
267 + "div.align{ width:89%; float: right; }\n"
268 + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
269 + "object {pointer-events: none;}"
271 htmlSvg.append("<div>");
273 "<div class=\"titlex\">");
275 "<div class=\"sub-category-container\"> ")
278 .append("</div>\n\n<!-- ========================================================================================== -->\n\n");
280 "<div class=\"align\" >");
282 "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
283 .append(alignmentSvg)
284 .append("</div></div>")
286 htmlSvg.append("</div>");
288 htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
289 + "<script language=\"JavaScript\" type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
291 + "var subCatContainer = $(\".sub-category-container\");\n"
292 + "subCatContainer.scroll(\nfunction() {\n"
293 + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
295 htmlSvg.append("</script></hmtl>");
297 return htmlSvg.toString();