JAL-1780 JAL-653 moved AlignExportSettings reference on AlignViewportI to FileParse...
[jalview.git] / src / jalview / io / HtmlSvgOutput.java
1 package jalview.io;
2
3 import jalview.api.FeatureRenderer;
4 import jalview.datamodel.SequenceI;
5 import jalview.gui.AlignViewport;
6 import jalview.gui.AlignmentPanel;
7 import jalview.gui.HTMLOptions;
8 import jalview.math.AlignmentDimension;
9 import jalview.util.MessageManager;
10
11 import java.awt.Color;
12 import java.awt.FontMetrics;
13 import java.awt.Graphics;
14 import java.awt.print.Printable;
15 import java.awt.print.PrinterException;
16 import java.io.File;
17 import java.io.FileOutputStream;
18
19 import org.jfree.graphics2d.svg.SVGGraphics2D;
20 import org.jfree.graphics2d.svg.SVGHints;
21
22 public class HtmlSvgOutput
23 {
24   AlignViewport av;
25
26   FeatureRenderer fr;
27   AlignmentPanel ap;
28
29
30   public HtmlSvgOutput(File file, AlignmentPanel ap)
31   {
32     this.av = ap.av;
33     this.ap = ap;
34     fr = ap.cloneFeatureRenderer();
35     generateHtmlSvgOutput(file);
36   }
37
38   public void generateHtmlSvgOutput(File file)
39   {
40     try
41     {
42       if (file == null /*
43                         * && !(System.getProperty("java.awt.headless") != null
44                         * && System
45                         * .getProperty("java.awt.headless").equals("true"))
46                         */)
47       {
48
49       JalviewFileChooser chooser = getHTMLChooser();
50       chooser.setFileView(new jalview.io.JalviewFileView());
51       chooser.setDialogTitle(ap.alignFrame.getTitle());
52       chooser.setToolTipText(MessageManager.getString("action.save"));
53       int value = chooser.showSaveDialog(ap.alignFrame);
54
55       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
56       {
57         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
58                 .getSelectedFile().getParent());
59         file = chooser.getSelectedFile();
60       }
61       }
62
63       AlignmentDimension aDimension = ap.getAlignmentDimension();
64       SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
65               aDimension.getHeight());
66       SVGGraphics2D g2 = new SVGGraphics2D(aDimension.getWidth(),
67               aDimension.getHeight());
68
69       String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
70               "Prompt each time");
71
72       // If we need to prompt, and if the GUI is visible then
73       // Prompt for rendering style
74       if (renderStyle.equalsIgnoreCase("Prompt each time")
75               && !(System.getProperty("java.awt.headless") != null && System
76                       .getProperty("java.awt.headless").equals("true")))
77       {
78         HTMLOptions svgOption = new HTMLOptions();
79         renderStyle = svgOption.getValue();
80
81         if (renderStyle == null || svgOption.cancelled)
82         {
83           return;
84         }
85       }
86
87       if (renderStyle.equalsIgnoreCase("lineart"))
88       {
89         g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
90                 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
91         g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
92                 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
93       }
94       printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
95               g2);
96       FileOutputStream out = new FileOutputStream(file);
97
98       String titleSvgData = g1.getSVGDocument();
99       String alignSvgData = g2.getSVGDocument();
100       String jsonData = JSONFile.getJSONData(ap);
101       String htmlData = getHtml(titleSvgData, alignSvgData, jsonData);
102
103       out.write(htmlData.getBytes());
104       out.flush();
105       out.close();
106       if (!(System.getProperty("java.awt.headless") != null && System
107               .getProperty("java.awt.headless").equals("true")))
108       {
109       jalview.util.BrowserLauncher.openURL("file:///" + file);
110       }
111     } catch (Exception e)
112     {
113       e.printStackTrace();
114     }
115   }
116   
117   static JalviewFileChooser getHTMLChooser()
118   {
119     return new jalview.io.JalviewFileChooser(
120             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
121             { "html" }, new String[]
122             { "Hypertext Markup Language" }, "Hypertext Markup Language");
123   }
124
125   public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
126           throws PrinterException
127   {
128     int idWidth = ap.getVisibleIdWidth(false);
129     FontMetrics fm = ap.getFontMetrics(av.getFont());
130     int scaleHeight = av.getCharHeight() + fm.getDescent();
131
132     pg[0].setColor(Color.white);
133     pg[0].fillRect(0, 0, pwidth, pheight);
134     pg[0].setFont(av.getFont());
135
136     // //////////////////////////////////
137     // / How many sequences and residues can we fit on a printable page?
138     int totalRes = (pwidth - idWidth) / av.getCharWidth();
139     int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
140     int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
141
142     // ///////////////////////////
143     // / Only print these sequences and residues on this page
144     int startRes;
145
146     // ///////////////////////////
147     // / Only print these sequences and residues on this page
148     int endRes;
149
150     // ///////////////////////////
151     // / Only print these sequences and residues on this page
152     int startSeq;
153
154     // ///////////////////////////
155     // / Only print these sequences and residues on this page
156     int endSeq;
157     startRes = (pi % pagesWide) * totalRes;
158     endRes = (startRes + totalRes) - 1;
159
160     if (endRes > (av.getAlignment().getWidth() - 1))
161     {
162       endRes = av.getAlignment().getWidth() - 1;
163     }
164     startSeq = (pi / pagesWide) * totalSeq;
165     endSeq = startSeq + totalSeq;
166     if (endSeq > av.getAlignment().getHeight())
167     {
168       endSeq = av.getAlignment().getHeight();
169     }
170     int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
171             * pheight;
172     if (av.isShowAnnotation())
173     {
174       pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
175     }
176     pagesHigh /= pheight;
177     if (pi >= (pagesWide * pagesHigh))
178     {
179       return Printable.NO_SUCH_PAGE;
180     }
181
182     // draw Scale
183     pg[1].translate(0, 0);
184     ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
185             scaleHeight);
186     pg[1].translate(-idWidth, scaleHeight);
187
188     // //////////////
189     // Draw the ids
190     Color currentColor = null;
191     Color currentTextColor = null;
192     pg[0].translate(0, scaleHeight);
193     pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
194     SequenceI seq;
195     for (int i = startSeq; i < endSeq; i++)
196     {
197       seq = av.getAlignment().getSequenceAt(i);
198       if ((av.getSelectionGroup() != null)
199               && av.getSelectionGroup().getSequences(null).contains(seq))
200       {
201         currentColor = Color.gray;
202         currentTextColor = Color.black;
203       }
204       else
205       {
206         currentColor = av.getSequenceColour(seq);
207         currentTextColor = Color.black;
208       }
209       pg[0].setColor(currentColor);
210       pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
211               av.getCharHeight());
212       pg[0].setColor(currentTextColor);
213       int xPos = 0;
214       if (av.isRightAlignIds())
215       {
216         fm = pg[0].getFontMetrics();
217         xPos = idWidth
218                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
219                 - 4;
220       }
221       pg[0].drawString(
222               seq.getDisplayId(av.getShowJVSuffix()),
223               xPos,
224               (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
225                       - (av.getCharHeight() / 5));
226     }
227     pg[0].setFont(av.getFont());
228     pg[0].translate(idWidth, 0);
229
230     // draw main sequence panel
231     pg[1].translate(idWidth, 0);
232     ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
233             endSeq, 0);
234     if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
235     {
236       // draw annotation label - need to offset for current scroll position
237       int offset = -ap.getAlabels().getScrollOffset();
238       pg[0].translate(0, offset);
239       pg[0].translate(-idWidth - 3,
240               (endSeq - startSeq) * av.getCharHeight() + 3);
241       ap.getAlabels().drawComponent(pg[0], idWidth);
242       pg[0].translate(idWidth + 3, 0);
243       pg[0].translate(0, -offset);
244
245       // draw annotation - need to offset for current scroll position
246       pg[1].translate(0, offset);
247       pg[1].translate(-idWidth - 3,
248               (endSeq - startSeq) * av.getCharHeight() + 3);
249       pg[1].translate(idWidth + 3, 0);
250       ap.getAnnotationPanel().renderer.drawComponent(
251               ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
252       pg[1].translate(0, -offset);
253     }
254
255     return Printable.PAGE_EXISTS;
256   }
257   
258   private String getHtml(String titleSvg, String alignmentSvg,
259           String jsonData)
260   {
261     StringBuilder htmlSvg = new StringBuilder();
262     htmlSvg.append("<html>\n");
263     if (jsonData != null)
264     {
265       htmlSvg.append("<input type=\"hidden\" name=\"seqData\" id=\"seqData\" value='"
266               + jsonData + "'>");
267     }
268     htmlSvg.append("\n<style type=\"text/css\"> "
269             + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
270             + "div.titlex{ width:11%; float: left; }\n"
271             + "div.align{ width:89%; float: right; }\n"
272             + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
273             + "object {pointer-events: none;}"
274             + "</style>");
275     htmlSvg.append("<div>");
276     htmlSvg.append(
277 "<div class=\"titlex\">");
278     htmlSvg.append(
279 "<div class=\"sub-category-container\"> ")
280             .append(titleSvg)
281             .append("</div>")
282             .append("</div>\n\n<!-- ========================================================================================== -->\n\n");
283     htmlSvg.append(
284 "<div class=\"align\" >");
285     htmlSvg.append(
286             "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
287             .append(alignmentSvg)
288 .append("</div></div>")
289             .append("</div>");
290     htmlSvg.append("</div>");
291
292     htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
293             + "<script language=\"JavaScript\" type=\"text/javascript\"  src=\"//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
294             + "<script>\n"
295             + "var subCatContainer = $(\".sub-category-container\");\n"
296             + "subCatContainer.scroll(\nfunction() {\n"
297             + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
298
299     htmlSvg.append("</script></hmtl>");
300
301     return htmlSvg.toString();
302   }
303 }