Change the Html export to utilize svg
[jalview.git] / src / jalview / io / HtmlSvgOutput.java
1 package jalview.io;
2
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;
11
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;
17 import java.io.File;
18 import java.io.FileOutputStream;
19
20 import org.jfree.graphics2d.svg.SVGGraphics2D;
21 import org.jfree.graphics2d.svg.SVGHints;
22
23 public class HtmlSvgOutput
24 {
25   AlignViewport av;
26
27   FeatureRenderer fr;
28   AlignmentPanel ap;
29
30   AnnotationPanel annotationPanel;
31
32   public HtmlSvgOutput(AlignmentPanel ap)
33   {
34
35       this.av = ap.av;
36       this.ap = ap;
37       this.annotationPanel = ap.getAnnotationPanel();
38     generateHtmlSvgOutput();
39   }
40
41   public void generateHtmlSvgOutput()
42   {
43     File file = null;
44     try
45     {
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);
51
52       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
53       {
54         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
55                 .getSelectedFile().getParent());
56         file = chooser.getSelectedFile();
57       }
58
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());
64
65       String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
66               "Prompt each time");
67
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")))
73       {
74         HTMLOptions svgOption = new HTMLOptions();
75         renderStyle = svgOption.getValue();
76
77         if (renderStyle == null || svgOption.cancelled)
78         {
79           return;
80         }
81       }
82
83       if (renderStyle.equalsIgnoreCase("lineart"))
84       {
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);
89       }
90       printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
91               g2);
92       FileOutputStream out = new FileOutputStream(file);
93
94       String titleSvgData = g1.getSVGDocument();
95       String alignSvgData = g2.getSVGDocument();
96       String htmlData = getHtml(titleSvgData, alignSvgData);
97
98       out.write(htmlData.getBytes());
99       out.flush();
100       out.close();
101
102       jalview.util.BrowserLauncher.openURL("file:///" + file);
103     } catch (Exception e)
104     {
105       e.printStackTrace();
106     }
107   }
108   
109   static JalviewFileChooser getHTMLChooser()
110   {
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");
115   }
116
117   public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
118           throws PrinterException
119   {
120     int idWidth = ap.getVisibleIdWidth(false);
121     FontMetrics fm = ap.getFontMetrics(av.getFont());
122     int scaleHeight = av.getCharHeight() + fm.getDescent();
123
124     pg[0].setColor(Color.white);
125     pg[0].fillRect(0, 0, pwidth, pheight);
126     pg[0].setFont(av.getFont());
127
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;
133
134     // ///////////////////////////
135     // / Only print these sequences and residues on this page
136     int startRes;
137
138     // ///////////////////////////
139     // / Only print these sequences and residues on this page
140     int endRes;
141
142     // ///////////////////////////
143     // / Only print these sequences and residues on this page
144     int startSeq;
145
146     // ///////////////////////////
147     // / Only print these sequences and residues on this page
148     int endSeq;
149     startRes = (pi % pagesWide) * totalRes;
150     endRes = (startRes + totalRes) - 1;
151
152     if (endRes > (av.getAlignment().getWidth() - 1))
153     {
154       endRes = av.getAlignment().getWidth() - 1;
155     }
156     startSeq = (pi / pagesWide) * totalSeq;
157     endSeq = startSeq + totalSeq;
158     if (endSeq > av.getAlignment().getHeight())
159     {
160       endSeq = av.getAlignment().getHeight();
161     }
162     int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
163             * pheight;
164     if (av.isShowAnnotation())
165     {
166       pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
167     }
168     pagesHigh /= pheight;
169     if (pi >= (pagesWide * pagesHigh))
170     {
171       return Printable.NO_SUCH_PAGE;
172     }
173
174     // draw Scale
175     pg[1].translate(0, 0);
176     ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
177             scaleHeight);
178     pg[1].translate(-idWidth, scaleHeight);
179
180     // //////////////
181     // Draw the ids
182     Color currentColor = null;
183     Color currentTextColor = null;
184     pg[0].translate(0, scaleHeight);
185     pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
186     SequenceI seq;
187     for (int i = startSeq; i < endSeq; i++)
188     {
189       seq = av.getAlignment().getSequenceAt(i);
190       if ((av.getSelectionGroup() != null)
191               && av.getSelectionGroup().getSequences(null).contains(seq))
192       {
193         currentColor = Color.gray;
194         currentTextColor = Color.black;
195       }
196       else
197       {
198         currentColor = av.getSequenceColour(seq);
199         currentTextColor = Color.black;
200       }
201       pg[0].setColor(currentColor);
202       pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
203               av.getCharHeight());
204       pg[0].setColor(currentTextColor);
205       int xPos = 0;
206       if (av.isRightAlignIds())
207       {
208         fm = pg[0].getFontMetrics();
209         xPos = idWidth
210                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
211                 - 4;
212       }
213       pg[0].drawString(
214               seq.getDisplayId(av.getShowJVSuffix()),
215               xPos,
216               (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
217                       - (av.getCharHeight() / 5));
218     }
219     pg[0].setFont(av.getFont());
220     pg[0].translate(idWidth, 0);
221
222     // draw main sequence panel
223     pg[1].translate(idWidth, 0);
224     ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
225             endSeq, 0);
226     if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
227     {
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);
236
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);
245     }
246
247     return Printable.PAGE_EXISTS;
248   }
249   
250   private String getHtml(String titleSvg, String alignmentSvg)
251   {
252     StringBuilder htmlSvg = new StringBuilder();
253     htmlSvg.append("<html><style type=\"text/css\">" + "div.title {"
254             + "height: 100%;" + "width: 9%;" + "float: left;" + "}"
255             + "div.align {" + "height: 100%;" + "width: 91%;"
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();
263   }
264 }