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