JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / img / PdfEncoder.java
1 /* $RCSfile$\r
2  * $Author: hansonr $\r
3  * $Date: 2009-06-30 18:58:33 -0500 (Tue, 30 Jun 2009) $\r
4  * $Revision: 11158 $\r
5  *\r
6  * Copyright (C) 2002-2005  The Jmol Development Team\r
7  *\r
8  * Contact: jmol-developers@lists.sf.net\r
9  *\r
10  *  This library is free software; you can redistribute it and/or\r
11  *  modify it under the terms of the GNU Lesser General Public\r
12  *  License as published by the Free Software Foundation; either\r
13  *  version 2.1 of the License, or (at your option) any later version.\r
14  *\r
15  *  This library is distributed in the hope that it will be useful,\r
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
18  *  Lesser General Public License for more details.\r
19  *\r
20  *  You should have received a copy of the GNU Lesser General Public\r
21  *  License along with this library; if not, write to the Free Software\r
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
23  */\r
24 package javajs.img;\r
25 \r
26 import java.util.Hashtable;\r
27 import java.util.Map;\r
28 \r
29 import javajs.export.PDFCreator;\r
30 \r
31 /**\r
32  * A relatively primitive PDF generator that just makes a document with an image\r
33  * in it.\r
34  * \r
35  */\r
36 public class PdfEncoder extends ImageEncoder {\r
37 \r
38   private boolean isLandscape;\r
39   private PDFCreator pdf;\r
40   private String comment;\r
41 \r
42   public PdfEncoder() {\r
43     // for Class.forName  \r
44   }\r
45 \r
46   @Override\r
47   protected void setParams(Map<String, Object> params) {\r
48         isLandscape = (quality > 1);\r
49     comment = "Jmol " + (String) params.get("comment");\r
50   }\r
51 \r
52   @Override\r
53   protected void generate() throws Exception {\r
54     pdf = new PDFCreator();\r
55     int pageWidth = 8 * 72;\r
56     int pageHeight = 11 * 72;\r
57     pdf.setOutputStream(out);\r
58     pdf.newDocument(pageWidth, pageHeight, isLandscape); // A4 or Letter\r
59     addMyImage(pageWidth, pageHeight);\r
60     Map<String, String> ht = new Hashtable<String, String>();\r
61     if (comment != null)\r
62       ht.put("Producer", comment);\r
63     ht.put("Author", "JMol");\r
64     ht.put("CreationDate", date);\r
65     pdf.addInfo(ht);\r
66     pdf.closeDocument();\r
67   }\r
68 \r
69   \r
70   /**\r
71    * centered on the page\r
72    * \r
73    * @param pageWidth\r
74    * @param pageHeight\r
75    */\r
76   private void addMyImage(int pageWidth, int pageHeight) {\r
77     pdf.addImageResource("img1", width, height, pixels, true);\r
78     int w = (isLandscape ? pageHeight : pageWidth);\r
79     int h = (isLandscape ? pageWidth : pageHeight);\r
80     int iw = width;\r
81     int ih = height;\r
82     if (iw > 0.9 * w) {\r
83       ih = (int) (ih * 0.9 * w / iw);\r
84       iw = (int) (w * 0.9);\r
85     }\r
86     if (ih > 0.9 * h) {\r
87       iw = (int) (iw * 0.9 * h / ih);\r
88       ih = (int) (h * 0.9);\r
89     }\r
90     int x = 0;\r
91     int y = 0;\r
92     int x1 = iw;\r
93     int y1 = ih;\r
94     if (w > iw) {\r
95       x = (w - iw) / 2;\r
96       x1 = iw + x;\r
97     }\r
98     if (h > ih) {\r
99       y = (h - ih) / 2;\r
100       y1 = ih + y;\r
101     }\r
102     pdf.drawImage("img1", x, y, x1, y1, 0, 0, width, height);\r
103   }\r
104 \r
105 }\r