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());
109 jalview.util.BrowserLauncher.openURL("file:///" + file);
110 } catch (Exception e)
116 static JalviewFileChooser getHTMLChooser()
118 return new jalview.io.JalviewFileChooser(
119 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
120 { "html" }, new String[]
121 { "Hypertext Markup Language" }, "Hypertext Markup Language");
124 public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
125 throws PrinterException
127 int idWidth = ap.getVisibleIdWidth(false);
128 FontMetrics fm = ap.getFontMetrics(av.getFont());
129 int scaleHeight = av.getCharHeight() + fm.getDescent();
131 pg[0].setColor(Color.white);
132 pg[0].fillRect(0, 0, pwidth, pheight);
133 pg[0].setFont(av.getFont());
135 // //////////////////////////////////
136 // / How many sequences and residues can we fit on a printable page?
137 int totalRes = (pwidth - idWidth) / av.getCharWidth();
138 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
139 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
141 // ///////////////////////////
142 // / Only print these sequences and residues on this page
145 // ///////////////////////////
146 // / Only print these sequences and residues on this page
149 // ///////////////////////////
150 // / Only print these sequences and residues on this page
153 // ///////////////////////////
154 // / Only print these sequences and residues on this page
156 startRes = (pi % pagesWide) * totalRes;
157 endRes = (startRes + totalRes) - 1;
159 if (endRes > (av.getAlignment().getWidth() - 1))
161 endRes = av.getAlignment().getWidth() - 1;
163 startSeq = (pi / pagesWide) * totalSeq;
164 endSeq = startSeq + totalSeq;
165 if (endSeq > av.getAlignment().getHeight())
167 endSeq = av.getAlignment().getHeight();
169 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
171 if (av.isShowAnnotation())
173 pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
175 pagesHigh /= pheight;
176 if (pi >= (pagesWide * pagesHigh))
178 return Printable.NO_SUCH_PAGE;
182 pg[1].translate(0, 0);
183 ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
185 pg[1].translate(-idWidth, scaleHeight);
189 Color currentColor = null;
190 Color currentTextColor = null;
191 pg[0].translate(0, scaleHeight);
192 pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
194 for (int i = startSeq; i < endSeq; i++)
196 seq = av.getAlignment().getSequenceAt(i);
197 if ((av.getSelectionGroup() != null)
198 && av.getSelectionGroup().getSequences(null).contains(seq))
200 currentColor = Color.gray;
201 currentTextColor = Color.black;
205 currentColor = av.getSequenceColour(seq);
206 currentTextColor = Color.black;
208 pg[0].setColor(currentColor);
209 pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
211 pg[0].setColor(currentTextColor);
213 if (av.isRightAlignIds())
215 fm = pg[0].getFontMetrics();
217 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
221 seq.getDisplayId(av.getShowJVSuffix()),
223 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
224 - (av.getCharHeight() / 5));
226 pg[0].setFont(av.getFont());
227 pg[0].translate(idWidth, 0);
229 // draw main sequence panel
230 pg[1].translate(idWidth, 0);
231 ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
233 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
235 // draw annotation label - need to offset for current scroll position
236 int offset = -ap.getAlabels().getScrollOffset();
237 pg[0].translate(0, offset);
238 pg[0].translate(-idWidth - 3,
239 (endSeq - startSeq) * av.getCharHeight() + 3);
240 ap.getAlabels().drawComponent(pg[0], idWidth);
241 pg[0].translate(idWidth + 3, 0);
242 pg[0].translate(0, -offset);
244 // draw annotation - need to offset for current scroll position
245 pg[1].translate(0, offset);
246 pg[1].translate(-idWidth - 3,
247 (endSeq - startSeq) * av.getCharHeight() + 3);
248 pg[1].translate(idWidth + 3, 0);
249 ap.getAnnotationPanel().renderer.drawComponent(
250 ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
251 pg[1].translate(0, -offset);
254 return Printable.PAGE_EXISTS;
257 private String getHtml(String titleSvg, String alignmentSvg)
259 StringBuilder htmlSvg = new StringBuilder();
260 htmlSvg.append("<html><style type=\"text/css\"> div.title {"
261 + "height: 100%; width:11%; float: left; }"
262 + "div.align { height: 100%; width:89%;"
263 + "overflow: scroll; float: right; } </style>"
264 + "<div style=\"width:100%; height:100%; overflow: hidden\">"
265 + "<div class=\"title\">");
266 htmlSvg.append(titleSvg);
267 htmlSvg.append("</div><div class=\"align\">").append(alignmentSvg);
268 htmlSvg.append("</div>");
269 return htmlSvg.toString();