3c9c60893c3ef4225c63c82d4a7b9a37d34cb9ed
[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       if (!(System.getProperty("java.awt.headless") != null && System
109               .getProperty("java.awt.headless").equals("true")))
110       {
111       jalview.util.BrowserLauncher.openURL("file:///" + file);
112       }
113     } catch (Exception e)
114     {
115       e.printStackTrace();
116     }
117   }
118   
119   static JalviewFileChooser getHTMLChooser()
120   {
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");
125   }
126
127   public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
128           throws PrinterException
129   {
130     int idWidth = ap.getVisibleIdWidth(false);
131     FontMetrics fm = ap.getFontMetrics(av.getFont());
132     int scaleHeight = av.getCharHeight() + fm.getDescent();
133
134     pg[0].setColor(Color.white);
135     pg[0].fillRect(0, 0, pwidth, pheight);
136     pg[0].setFont(av.getFont());
137
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;
143
144     // ///////////////////////////
145     // / Only print these sequences and residues on this page
146     int startRes;
147
148     // ///////////////////////////
149     // / Only print these sequences and residues on this page
150     int endRes;
151
152     // ///////////////////////////
153     // / Only print these sequences and residues on this page
154     int startSeq;
155
156     // ///////////////////////////
157     // / Only print these sequences and residues on this page
158     int endSeq;
159     startRes = (pi % pagesWide) * totalRes;
160     endRes = (startRes + totalRes) - 1;
161
162     if (endRes > (av.getAlignment().getWidth() - 1))
163     {
164       endRes = av.getAlignment().getWidth() - 1;
165     }
166     startSeq = (pi / pagesWide) * totalSeq;
167     endSeq = startSeq + totalSeq;
168     if (endSeq > av.getAlignment().getHeight())
169     {
170       endSeq = av.getAlignment().getHeight();
171     }
172     int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
173             * pheight;
174     if (av.isShowAnnotation())
175     {
176       pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
177     }
178     pagesHigh /= pheight;
179     if (pi >= (pagesWide * pagesHigh))
180     {
181       return Printable.NO_SUCH_PAGE;
182     }
183
184     // draw Scale
185     pg[1].translate(0, 0);
186     ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
187             scaleHeight);
188     pg[1].translate(-idWidth, scaleHeight);
189
190     // //////////////
191     // Draw the ids
192     Color currentColor = null;
193     Color currentTextColor = null;
194     pg[0].translate(0, scaleHeight);
195     pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
196     SequenceI seq;
197     for (int i = startSeq; i < endSeq; i++)
198     {
199       seq = av.getAlignment().getSequenceAt(i);
200       if ((av.getSelectionGroup() != null)
201               && av.getSelectionGroup().getSequences(null).contains(seq))
202       {
203         currentColor = Color.gray;
204         currentTextColor = Color.black;
205       }
206       else
207       {
208         currentColor = av.getSequenceColour(seq);
209         currentTextColor = Color.black;
210       }
211       pg[0].setColor(currentColor);
212       pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
213               av.getCharHeight());
214       pg[0].setColor(currentTextColor);
215       int xPos = 0;
216       if (av.isRightAlignIds())
217       {
218         fm = pg[0].getFontMetrics();
219         xPos = idWidth
220                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
221                 - 4;
222       }
223       pg[0].drawString(
224               seq.getDisplayId(av.getShowJVSuffix()),
225               xPos,
226               (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
227                       - (av.getCharHeight() / 5));
228     }
229     pg[0].setFont(av.getFont());
230     pg[0].translate(idWidth, 0);
231
232     // draw main sequence panel
233     pg[1].translate(idWidth, 0);
234     ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
235             endSeq, 0);
236     if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
237     {
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);
246
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);
255     }
256
257     return Printable.PAGE_EXISTS;
258   }
259   
260   private String getHtml(String titleSvg, String alignmentSvg)
261   {
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;}"
270             + "</style>");
271     htmlSvg.append("<div>");
272     htmlSvg.append(
273 "<div class=\"titlex\">");
274     htmlSvg.append(
275 "<div class=\"sub-category-container\"> ")
276             .append(titleSvg)
277             .append("</div>")
278             .append("</div>\n\n<!-- ========================================================================================== -->\n\n");
279     htmlSvg.append(
280 "<div class=\"align\" >");
281     htmlSvg.append(
282             "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
283             .append(alignmentSvg)
284 .append("</div></div>")
285             .append("</div>");
286     htmlSvg.append("</div>");
287
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"
290             + "<script>\n"
291             + "var subCatContainer = $(\".sub-category-container\");\n"
292             + "subCatContainer.scroll(\nfunction() {\n"
293             + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
294
295     htmlSvg.append("</script></hmtl>");
296
297     return htmlSvg.toString();
298   }
299 }