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(AlignmentPanel ap)
37 this.annotationPanel = ap.getAnnotationPanel();
38 generateHtmlSvgOutput();
41 public void generateHtmlSvgOutput()
46 JalviewFileChooser chooser = getHTMLChooser();
47 chooser.setFileView(new jalview.io.JalviewFileView());
48 chooser.setDialogTitle(ap.alignFrame.getTitle());
49 chooser.setToolTipText(MessageManager.getString("action.save"));
50 int value = chooser.showSaveDialog(ap.alignFrame);
52 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
54 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
55 .getSelectedFile().getParent());
56 file = chooser.getSelectedFile();
59 AlignmentDimension aDimension = ap.getAlignmentDimension();
60 SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
61 aDimension.getHeight());
62 SVGGraphics2D g2 = new SVGGraphics2D(aDimension.getWidth(),
63 aDimension.getHeight());
65 String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
68 // If we need to prompt, and if the GUI is visible then
69 // Prompt for rendering style
70 if (renderStyle.equalsIgnoreCase("Prompt each time")
71 && !(System.getProperty("java.awt.headless") != null && System
72 .getProperty("java.awt.headless").equals("true")))
74 HTMLOptions svgOption = new HTMLOptions();
75 renderStyle = svgOption.getValue();
77 if (renderStyle == null || svgOption.cancelled)
83 if (renderStyle.equalsIgnoreCase("lineart"))
85 g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
86 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
87 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
88 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
90 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
92 FileOutputStream out = new FileOutputStream(file);
94 String titleSvgData = g1.getSVGDocument();
95 String alignSvgData = g2.getSVGDocument();
96 String htmlData = getHtml(titleSvgData, alignSvgData);
98 out.write(htmlData.getBytes());
102 jalview.util.BrowserLauncher.openURL("file:///" + file);
103 } catch (Exception e)
109 static JalviewFileChooser getHTMLChooser()
111 return new jalview.io.JalviewFileChooser(
112 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
113 { "html" }, new String[]
114 { "Hypertext Markup Language" }, "Hypertext Markup Language");
117 public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
118 throws PrinterException
120 int idWidth = ap.getVisibleIdWidth(false);
121 FontMetrics fm = ap.getFontMetrics(av.getFont());
122 int scaleHeight = av.getCharHeight() + fm.getDescent();
124 pg[0].setColor(Color.white);
125 pg[0].fillRect(0, 0, pwidth, pheight);
126 pg[0].setFont(av.getFont());
128 // //////////////////////////////////
129 // / How many sequences and residues can we fit on a printable page?
130 int totalRes = (pwidth - idWidth) / av.getCharWidth();
131 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
132 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
134 // ///////////////////////////
135 // / Only print these sequences and residues on this page
138 // ///////////////////////////
139 // / Only print these sequences and residues on this page
142 // ///////////////////////////
143 // / Only print these sequences and residues on this page
146 // ///////////////////////////
147 // / Only print these sequences and residues on this page
149 startRes = (pi % pagesWide) * totalRes;
150 endRes = (startRes + totalRes) - 1;
152 if (endRes > (av.getAlignment().getWidth() - 1))
154 endRes = av.getAlignment().getWidth() - 1;
156 startSeq = (pi / pagesWide) * totalSeq;
157 endSeq = startSeq + totalSeq;
158 if (endSeq > av.getAlignment().getHeight())
160 endSeq = av.getAlignment().getHeight();
162 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
164 if (av.isShowAnnotation())
166 pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
168 pagesHigh /= pheight;
169 if (pi >= (pagesWide * pagesHigh))
171 return Printable.NO_SUCH_PAGE;
175 pg[1].translate(0, 0);
176 ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
178 pg[1].translate(-idWidth, scaleHeight);
182 Color currentColor = null;
183 Color currentTextColor = null;
184 pg[0].translate(0, scaleHeight);
185 pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
187 for (int i = startSeq; i < endSeq; i++)
189 seq = av.getAlignment().getSequenceAt(i);
190 if ((av.getSelectionGroup() != null)
191 && av.getSelectionGroup().getSequences(null).contains(seq))
193 currentColor = Color.gray;
194 currentTextColor = Color.black;
198 currentColor = av.getSequenceColour(seq);
199 currentTextColor = Color.black;
201 pg[0].setColor(currentColor);
202 pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
204 pg[0].setColor(currentTextColor);
206 if (av.isRightAlignIds())
208 fm = pg[0].getFontMetrics();
210 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
214 seq.getDisplayId(av.getShowJVSuffix()),
216 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
217 - (av.getCharHeight() / 5));
219 pg[0].setFont(av.getFont());
220 pg[0].translate(idWidth, 0);
222 // draw main sequence panel
223 pg[1].translate(idWidth, 0);
224 ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
226 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
228 // draw annotation label - need to offset for current scroll position
229 int offset = -ap.getAlabels().getScrollOffset();
230 pg[0].translate(0, offset);
231 pg[0].translate(-idWidth - 3,
232 (endSeq - startSeq) * av.getCharHeight() + 3);
233 ap.getAlabels().drawComponent(pg[0], idWidth);
234 pg[0].translate(idWidth + 3, 0);
235 pg[0].translate(0, -offset);
237 // draw annotation - need to offset for current scroll position
238 pg[1].translate(0, offset);
239 pg[1].translate(-idWidth - 3,
240 (endSeq - startSeq) * av.getCharHeight() + 3);
241 pg[1].translate(idWidth + 3, 0);
242 ap.getAnnotationPanel().renderer.drawComponent(
243 ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
244 pg[1].translate(0, -offset);
247 return Printable.PAGE_EXISTS;
250 private String getHtml(String titleSvg, String alignmentSvg)
252 StringBuilder htmlSvg = new StringBuilder();
253 htmlSvg.append("<html><style type=\"text/css\"> div.title {"
254 + "height: 100%; width:11%; float: left; }"
255 + "div.align { height: 100%; width:89%;"
256 + "overflow: scroll; float: right; } </style>"
257 + "<div style=\"width:100%; height:100%; overflow: hidden\">"
258 + "<div class=\"title\">");
259 htmlSvg.append(titleSvg);
260 htmlSvg.append("</div><div class=\"align\">").append(alignmentSvg);
261 htmlSvg.append("</div>");
262 return htmlSvg.toString();