JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / img / PdfEncoder.js
1 Clazz.declarePackage ("javajs.img");\r
2 Clazz.load (["javajs.img.ImageEncoder"], "javajs.img.PdfEncoder", ["java.util.Hashtable", "javajs.export.PDFCreator"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.isLandscape = false;\r
5 this.pdf = null;\r
6 this.comment = null;\r
7 Clazz.instantialize (this, arguments);\r
8 }, javajs.img, "PdfEncoder", javajs.img.ImageEncoder);\r
9 Clazz.makeConstructor (c$, \r
10 function () {\r
11 Clazz.superConstructor (this, javajs.img.PdfEncoder, []);\r
12 });\r
13 Clazz.overrideMethod (c$, "setParams", \r
14 function (params) {\r
15 this.isLandscape = (this.quality > 1);\r
16 this.comment = "Jmol " + params.get ("comment");\r
17 }, "java.util.Map");\r
18 Clazz.overrideMethod (c$, "generate", \r
19 function () {\r
20 this.pdf =  new javajs["export"].PDFCreator ();\r
21 var pageWidth = 576;\r
22 var pageHeight = 792;\r
23 this.pdf.setOutputStream (this.out);\r
24 this.pdf.newDocument (pageWidth, pageHeight, this.isLandscape);\r
25 this.addMyImage (pageWidth, pageHeight);\r
26 var ht =  new java.util.Hashtable ();\r
27 if (this.comment != null) ht.put ("Producer", this.comment);\r
28 ht.put ("Author", "JMol");\r
29 ht.put ("CreationDate", this.date);\r
30 this.pdf.addInfo (ht);\r
31 this.pdf.closeDocument ();\r
32 });\r
33 Clazz.defineMethod (c$, "addMyImage", \r
34 ($fz = function (pageWidth, pageHeight) {\r
35 this.pdf.addImageResource ("img1", this.width, this.height, this.pixels, true);\r
36 var w = (this.isLandscape ? pageHeight : pageWidth);\r
37 var h = (this.isLandscape ? pageWidth : pageHeight);\r
38 var iw = this.width;\r
39 var ih = this.height;\r
40 if (iw > 0.9 * w) {\r
41 ih = Clazz.doubleToInt (ih * 0.9 * w / iw);\r
42 iw = Clazz.doubleToInt (w * 0.9);\r
43 }if (ih > 0.9 * h) {\r
44 iw = Clazz.doubleToInt (iw * 0.9 * h / ih);\r
45 ih = Clazz.doubleToInt (h * 0.9);\r
46 }var x = 0;\r
47 var y = 0;\r
48 var x1 = iw;\r
49 var y1 = ih;\r
50 if (w > iw) {\r
51 x = Clazz.doubleToInt ((w - iw) / 2);\r
52 x1 = iw + x;\r
53 }if (h > ih) {\r
54 y = Clazz.doubleToInt ((h - ih) / 2);\r
55 y1 = ih + y;\r
56 }this.pdf.drawImage ("img1", x, y, x1, y1, 0, 0, this.width, this.height);\r
57 }, $fz.isPrivate = true, $fz), "~N,~N");\r
58 });\r