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