JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[jalviewjs.git] / bin / javajs / export / PDFCreator.js
1 Clazz.declarePackage ("javajs.export");
2 Clazz.load (null, "javajs.export.PDFCreator", ["java.lang.Double", "java.util.Hashtable", "javajs.export.PDFObject", "javajs.util.Lst", "$.SB"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.os = null;
5 this.indirectObjects = null;
6 this.root = null;
7 this.graphics = null;
8 this.pt = 0;
9 this.xrefPt = 0;
10 this.count = 0;
11 this.height = 0;
12 this.width = 0;
13 this.fonts = null;
14 this.images = null;
15 Clazz.instantialize (this, arguments);
16 }, javajs["export"], "PDFCreator");
17 Clazz.makeConstructor (c$, 
18 function () {
19 });
20 Clazz.defineMethod (c$, "setOutputStream", 
21 function (os) {
22 this.os = os;
23 }, "java.io.OutputStream");
24 Clazz.defineMethod (c$, "newDocument", 
25 function (paperWidth, paperHeight, isLandscape) {
26 this.width = (isLandscape ? paperHeight : paperWidth);
27 this.height = (isLandscape ? paperWidth : paperHeight);
28 System.out.println ("Creating PDF with width=" + this.width + " and height=" + this.height);
29 this.fonts =  new java.util.Hashtable ();
30 this.indirectObjects =  new javajs.util.Lst ();
31 this.root = this.newObject ("Catalog");
32 var pages = this.newObject ("Pages");
33 var page = this.newObject ("Page");
34 var pageContents = this.newObject (null);
35 this.graphics = this.newObject ("XObject");
36 this.root.addDef ("Pages", pages.getRef ());
37 pages.addDef ("Count", "1");
38 pages.addDef ("Kids", "[ " + page.getRef () + " ]");
39 page.addDef ("Parent", pages.getRef ());
40 page.addDef ("MediaBox", "[ 0 0 " + paperWidth + " " + paperHeight + " ]");
41 if (isLandscape) page.addDef ("Rotate", "90");
42 pageContents.addDef ("Length", "?");
43 pageContents.append ((isLandscape ? "q 0 1 1 0 0 0 " : "q 1 0 0 -1 0 " + (paperHeight)) + " cm /" + this.graphics.getID () + " Do Q");
44 page.addDef ("Contents", pageContents.getRef ());
45 this.addProcSet (page);
46 this.addProcSet (this.graphics);
47 this.graphics.addDef ("Subtype", "/Form");
48 this.graphics.addDef ("FormType", "1");
49 this.graphics.addDef ("BBox", "[0 0 " + this.width + " " + this.height + "]");
50 this.graphics.addDef ("Matrix", "[1 0 0 1 0 0]");
51 this.graphics.addDef ("Length", "?");
52 page.addResource ("XObject", this.graphics.getID (), this.graphics.getRef ());
53 this.g ("q 1 w 1 J 1 j 10 M []0 d q ");
54 this.clip (0, 0, this.width, this.height);
55 }, "~N,~N,~B");
56 Clazz.defineMethod (c$, "addProcSet", 
57 ($fz = function (o) {
58 o.addResource (null, "ProcSet", "[/PDF /Text /ImageB /ImageC /ImageI]");
59 }, $fz.isPrivate = true, $fz), "javajs.export.PDFObject");
60 Clazz.defineMethod (c$, "clip", 
61 ($fz = function (x1, y1, x2, y2) {
62 this.moveto (x1, y1);
63 this.lineto (x2, y1);
64 this.lineto (x2, y2);
65 this.lineto (x1, y2);
66 this.g ("h W n");
67 }, $fz.isPrivate = true, $fz), "~N,~N,~N,~N");
68 Clazz.defineMethod (c$, "moveto", 
69 function (x, y) {
70 this.g (x + " " + y + " m");
71 }, "~N,~N");
72 Clazz.defineMethod (c$, "lineto", 
73 function (x, y) {
74 this.g (x + " " + y + " l");
75 }, "~N,~N");
76 Clazz.defineMethod (c$, "newObject", 
77 ($fz = function (type) {
78 var o =  new javajs["export"].PDFObject (++this.count);
79 if (type != null) o.addDef ("Type", "/" + type);
80 this.indirectObjects.addLast (o);
81 return o;
82 }, $fz.isPrivate = true, $fz), "~S");
83 Clazz.defineMethod (c$, "addInfo", 
84 function (data) {
85 var info =  new java.util.Hashtable ();
86 for (var e, $e = data.entrySet ().iterator (); $e.hasNext () && ((e = $e.next ()) || true);) {
87 var value = "(" + e.getValue ().$replace (')', '_').$replace ('(', '_') + ")";
88 info.put (e.getKey (), value);
89 }
90 this.root.addDef ("Info", info);
91 }, "java.util.Map");
92 Clazz.defineMethod (c$, "addFontResource", 
93 ($fz = function (fname) {
94 var f = this.newObject ("Font");
95 this.fonts.put (fname, f);
96 f.addDef ("BaseFont", fname);
97 f.addDef ("Encoding", "/WinAnsiEncoding");
98 f.addDef ("Subtype", "/Type1");
99 this.graphics.addResource ("Font", f.getID (), f.getRef ());
100 return f;
101 }, $fz.isPrivate = true, $fz), "~S");
102 Clazz.defineMethod (c$, "addImageResource", 
103 function (newImage, width, height, buffer, isRGB) {
104 var imageObj = this.newObject ("XObject");
105 if (this.images == null) this.images =  new java.util.Hashtable ();
106 this.images.put (newImage, imageObj);
107 imageObj.addDef ("Subtype", "/Image");
108 imageObj.addDef ("Length", "?");
109 imageObj.addDef ("ColorSpace", isRGB ? "/DeviceRGB" : "/DeviceGray");
110 imageObj.addDef ("BitsPerComponent", "8");
111 imageObj.addDef ("Width", "" + width);
112 imageObj.addDef ("Height", "" + height);
113 this.graphics.addResource ("XObject", imageObj.getID (), imageObj.getRef ());
114 var n = buffer.length;
115 var stream =  Clazz.newByteArray (n * (isRGB ? 3 : 1), 0);
116 if (isRGB) {
117 for (var i = 0, pt = 0; i < n; i++) {
118 stream[pt++] = ((buffer[i] >> 16) & 0xFF);
119 stream[pt++] = ((buffer[i] >> 8) & 0xFF);
120 stream[pt++] = (buffer[i] & 0xFF);
121 }
122 } else {
123 for (var i = 0; i < n; i++) stream[i] = buffer[i];
124
125 }imageObj.setStream (stream);
126 this.graphics.addResource ("XObject", imageObj.getID (), imageObj.getRef ());
127 }, "~O,~N,~N,~A,~B");
128 Clazz.defineMethod (c$, "g", 
129 function (cmd) {
130 this.graphics.append (cmd).appendC ('\n');
131 }, "~S");
132 Clazz.defineMethod (c$, "output", 
133 ($fz = function (s) {
134 var b = s.getBytes ();
135 this.os.write (b, 0, b.length);
136 this.pt += b.length;
137 }, $fz.isPrivate = true, $fz), "~S");
138 Clazz.defineMethod (c$, "closeDocument", 
139 function () {
140 this.g ("Q Q");
141 this.outputHeader ();
142 this.writeObjects ();
143 this.writeXRefTable ();
144 this.writeTrailer ();
145 this.os.flush ();
146 this.os.close ();
147 });
148 Clazz.defineMethod (c$, "outputHeader", 
149 ($fz = function () {
150 this.output ("%PDF-1.3\n%");
151 var b =  Clazz.newByteArray (-1, [-1, -1, -1, -1]);
152 this.os.write (b, 0, b.length);
153 this.pt += 4;
154 this.output ("\n");
155 }, $fz.isPrivate = true, $fz));
156 Clazz.defineMethod (c$, "writeTrailer", 
157 ($fz = function () {
158 var trailer =  new javajs["export"].PDFObject (-2);
159 this.output ("trailer");
160 trailer.addDef ("Size", "" + this.indirectObjects.size ());
161 trailer.addDef ("Root", this.root.getRef ());
162 trailer.output (this.os);
163 this.output ("startxref\n");
164 this.output ("" + this.xrefPt + "\n");
165 this.output ("%%EOF\n");
166 }, $fz.isPrivate = true, $fz));
167 Clazz.defineMethod (c$, "writeObjects", 
168 ($fz = function () {
169 var nObj = this.indirectObjects.size ();
170 for (var i = 0; i < nObj; i++) {
171 var o = this.indirectObjects.get (i);
172 if (!o.isFont ()) continue;
173 o.pt = this.pt;
174 this.pt += o.output (this.os);
175 }
176 for (var i = 0; i < nObj; i++) {
177 var o = this.indirectObjects.get (i);
178 if (o.isFont ()) continue;
179 o.pt = this.pt;
180 this.pt += o.output (this.os);
181 }
182 }, $fz.isPrivate = true, $fz));
183 Clazz.defineMethod (c$, "writeXRefTable", 
184 ($fz = function () {
185 this.xrefPt = this.pt;
186 var nObj = this.indirectObjects.size ();
187 var sb =  new javajs.util.SB ();
188 sb.append ("xref\n0 " + (nObj + 1) + "\n0000000000 65535 f\r\n");
189 for (var i = 0; i < nObj; i++) {
190 var o = this.indirectObjects.get (i);
191 var s = "0000000000" + o.pt;
192 sb.append (s.substring (s.length - 10));
193 sb.append (" 00000 n\r\n");
194 }
195 this.output (sb.toString ());
196 }, $fz.isPrivate = true, $fz));
197 Clazz.defineMethod (c$, "canDoLineTo", 
198 function () {
199 return true;
200 });
201 Clazz.defineMethod (c$, "fill", 
202 function () {
203 this.g ("f");
204 });
205 Clazz.defineMethod (c$, "stroke", 
206 function () {
207 this.g ("S");
208 });
209 Clazz.defineMethod (c$, "doCircle", 
210 function (x, y, r, doFill) {
211 var d = r * 4 * (Math.sqrt (2) - 1) / 3;
212 var dx = x;
213 var dy = y;
214 this.g ((dx + r) + " " + dy + " m");
215 this.g ((dx + r) + " " + (dy + d) + " " + (dx + d) + " " + (dy + r) + " " + (dx) + " " + (dy + r) + " " + " c");
216 this.g ((dx - d) + " " + (dy + r) + " " + (dx - r) + " " + (dy + d) + " " + (dx - r) + " " + (dy) + " c");
217 this.g ((dx - r) + " " + (dy - d) + " " + (dx - d) + " " + (dy - r) + " " + (dx) + " " + (dy - r) + " c");
218 this.g ((dx + d) + " " + (dy - r) + " " + (dx + r) + " " + (dy - d) + " " + (dx + r) + " " + (dy) + " c");
219 this.g (doFill ? "f" : "s");
220 }, "~N,~N,~N,~B");
221 Clazz.defineMethod (c$, "doPolygon", 
222 function (axPoints, ayPoints, nPoints, doFill) {
223 this.moveto (axPoints[0], ayPoints[0]);
224 for (var i = 1; i < nPoints; i++) this.lineto (axPoints[i], ayPoints[i]);
225
226 this.g (doFill ? "f" : "s");
227 }, "~A,~A,~N,~B");
228 Clazz.defineMethod (c$, "doRect", 
229 function (x, y, width, height, doFill) {
230 this.g (x + " " + y + " " + width + " " + height + " re " + (doFill ? "f" : "s"));
231 }, "~N,~N,~N,~N,~B");
232 Clazz.defineMethod (c$, "drawImage", 
233 function (image, destX0, destY0, destX1, destY1, srcX0, srcY0, srcX1, srcY1) {
234 var imageObj = this.images.get (image);
235 if (imageObj == null) return;
236 this.g ("q");
237 this.clip (destX0, destY0, destX1, destY1);
238 var iw = Double.parseDouble (imageObj.getDef ("Width"));
239 var ih = Double.parseDouble (imageObj.getDef ("Height"));
240 var dw = (destX1 - destX0 + 1);
241 var dh = (destY1 - destY0 + 1);
242 var sw = (srcX1 - srcX0 + 1);
243 var sh = (srcY1 - srcY0 + 1);
244 var scaleX = dw / sw;
245 var scaleY = dh / sh;
246 var transX = destX0 - srcX0 * scaleX;
247 var transY = destY0 + (ih - srcY0) * scaleY;
248 this.g (scaleX * iw + " 0 0 " + -scaleY * ih + " " + transX + " " + transY + " cm");
249 this.g ("/" + imageObj.getID () + " Do");
250 this.g ("Q");
251 }, "~O,~N,~N,~N,~N,~N,~N,~N,~N");
252 Clazz.defineMethod (c$, "drawStringRotated", 
253 function (s, x, y, angle) {
254 this.g ("q " + this.getRotation (angle) + " " + x + " " + y + " cm BT(" + s + ")Tj ET Q");
255 }, "~S,~N,~N,~N");
256 Clazz.defineMethod (c$, "getRotation", 
257 function (angle) {
258 var cos = 0;
259 var sin = 0;
260 switch (angle) {
261 case 0:
262 cos = 1;
263 break;
264 case 90:
265 sin = 1;
266 break;
267 case -90:
268 sin = -1;
269 break;
270 case 180:
271 cos = -1;
272 break;
273 default:
274 var a = (angle / 180.0 * 3.141592653589793);
275 cos = Math.cos (a);
276 sin = Math.sin (a);
277 if (Math.abs (cos) < 0.0001) cos = 0;
278 if (Math.abs (sin) < 0.0001) sin = 0;
279 }
280 return cos + " " + sin + " " + sin + " " + -cos;
281 }, "~N");
282 Clazz.defineMethod (c$, "setColor", 
283 function (rgb, isFill) {
284 this.g (rgb[0] + " " + rgb[1] + " " + rgb[2] + (isFill ? " rg" : " RG"));
285 }, "~A,~B");
286 Clazz.defineMethod (c$, "setFont", 
287 function (fname, size) {
288 var f = this.fonts.get (fname);
289 if (f == null) f = this.addFontResource (fname);
290 this.g ("/" + f.getID () + " " + size + " Tf");
291 }, "~S,~N");
292 Clazz.defineMethod (c$, "setLineWidth", 
293 function (width) {
294 this.g (width + " w");
295 }, "~N");
296 Clazz.defineMethod (c$, "translateScale", 
297 function (x, y, scale) {
298 this.g (scale + " 0 0 " + scale + " " + x + " " + y + " cm");
299 }, "~N,~N,~N");
300 });