Merge branch 'features/JAL-1605_html-svg-export' into develop
[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(File file, AlignmentPanel ap)
33   {
34
35       this.av = ap.av;
36       this.ap = ap;
37       this.annotationPanel = ap.getAnnotationPanel();
38     generateHtmlSvgOutput(file);
39   }
40
41   public void generateHtmlSvgOutput(File file)
42   {
43     try
44     {
45       if (file == null /*
46                         * && !(System.getProperty("java.awt.headless") != null
47                         * && System
48                         * .getProperty("java.awt.headless").equals("true"))
49                         */)
50       {
51
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);
57
58       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
59       {
60         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
61                 .getSelectedFile().getParent());
62         file = chooser.getSelectedFile();
63       }
64       }
65
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());
71
72       String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
73               "Prompt each time");
74
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")))
80       {
81         HTMLOptions svgOption = new HTMLOptions();
82         renderStyle = svgOption.getValue();
83
84         if (renderStyle == null || svgOption.cancelled)
85         {
86           return;
87         }
88       }
89
90       if (renderStyle.equalsIgnoreCase("lineart"))
91       {
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);
96       }
97       printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
98               g2);
99       FileOutputStream out = new FileOutputStream(file);
100
101       String titleSvgData = g1.getSVGDocument();
102       String alignSvgData = g2.getSVGDocument();
103       String htmlData = getHtml(titleSvgData, alignSvgData);
104
105       out.write(htmlData.getBytes());
106       out.flush();
107       out.close();
108
109       jalview.util.BrowserLauncher.openURL("file:///" + file);
110     } catch (Exception e)
111     {
112       e.printStackTrace();
113     }
114   }
115   
116   static JalviewFileChooser getHTMLChooser()
117   {
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");
122   }
123
124   public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
125           throws PrinterException
126   {
127     int idWidth = ap.getVisibleIdWidth(false);
128     FontMetrics fm = ap.getFontMetrics(av.getFont());
129     int scaleHeight = av.getCharHeight() + fm.getDescent();
130
131     pg[0].setColor(Color.white);
132     pg[0].fillRect(0, 0, pwidth, pheight);
133     pg[0].setFont(av.getFont());
134
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;
140
141     // ///////////////////////////
142     // / Only print these sequences and residues on this page
143     int startRes;
144
145     // ///////////////////////////
146     // / Only print these sequences and residues on this page
147     int endRes;
148
149     // ///////////////////////////
150     // / Only print these sequences and residues on this page
151     int startSeq;
152
153     // ///////////////////////////
154     // / Only print these sequences and residues on this page
155     int endSeq;
156     startRes = (pi % pagesWide) * totalRes;
157     endRes = (startRes + totalRes) - 1;
158
159     if (endRes > (av.getAlignment().getWidth() - 1))
160     {
161       endRes = av.getAlignment().getWidth() - 1;
162     }
163     startSeq = (pi / pagesWide) * totalSeq;
164     endSeq = startSeq + totalSeq;
165     if (endSeq > av.getAlignment().getHeight())
166     {
167       endSeq = av.getAlignment().getHeight();
168     }
169     int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
170             * pheight;
171     if (av.isShowAnnotation())
172     {
173       pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
174     }
175     pagesHigh /= pheight;
176     if (pi >= (pagesWide * pagesHigh))
177     {
178       return Printable.NO_SUCH_PAGE;
179     }
180
181     // draw Scale
182     pg[1].translate(0, 0);
183     ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
184             scaleHeight);
185     pg[1].translate(-idWidth, scaleHeight);
186
187     // //////////////
188     // Draw the ids
189     Color currentColor = null;
190     Color currentTextColor = null;
191     pg[0].translate(0, scaleHeight);
192     pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
193     SequenceI seq;
194     for (int i = startSeq; i < endSeq; i++)
195     {
196       seq = av.getAlignment().getSequenceAt(i);
197       if ((av.getSelectionGroup() != null)
198               && av.getSelectionGroup().getSequences(null).contains(seq))
199       {
200         currentColor = Color.gray;
201         currentTextColor = Color.black;
202       }
203       else
204       {
205         currentColor = av.getSequenceColour(seq);
206         currentTextColor = Color.black;
207       }
208       pg[0].setColor(currentColor);
209       pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
210               av.getCharHeight());
211       pg[0].setColor(currentTextColor);
212       int xPos = 0;
213       if (av.isRightAlignIds())
214       {
215         fm = pg[0].getFontMetrics();
216         xPos = idWidth
217                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
218                 - 4;
219       }
220       pg[0].drawString(
221               seq.getDisplayId(av.getShowJVSuffix()),
222               xPos,
223               (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
224                       - (av.getCharHeight() / 5));
225     }
226     pg[0].setFont(av.getFont());
227     pg[0].translate(idWidth, 0);
228
229     // draw main sequence panel
230     pg[1].translate(idWidth, 0);
231     ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
232             endSeq, 0);
233     if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
234     {
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);
243
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);
252     }
253
254     return Printable.PAGE_EXISTS;
255   }
256   
257   private String getHtml(String titleSvg, String alignmentSvg)
258   {
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();
270   }
271 }