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 () {
5 this.indirectObjects = null;
15 Clazz.instantialize (this, arguments);
16 }, javajs["export"], "PDFCreator");
17 Clazz.makeConstructor (c$,
20 Clazz.defineMethod (c$, "setOutputStream",
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);
56 Clazz.defineMethod (c$, "addProcSet",
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) {
67 }, $fz.isPrivate = true, $fz), "~N,~N,~N,~N");
68 Clazz.defineMethod (c$, "moveto",
70 this.g (x + " " + y + " m");
72 Clazz.defineMethod (c$, "lineto",
74 this.g (x + " " + y + " l");
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);
82 }, $fz.isPrivate = true, $fz), "~S");
83 Clazz.defineMethod (c$, "addInfo",
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);
90 this.root.addDef ("Info", info);
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 ());
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);
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);
123 for (var i = 0; i < n; i++) stream[i] = buffer[i];
125 }imageObj.setStream (stream);
126 this.graphics.addResource ("XObject", imageObj.getID (), imageObj.getRef ());
127 }, "~O,~N,~N,~A,~B");
128 Clazz.defineMethod (c$, "g",
130 this.graphics.append (cmd).appendC ('\n');
132 Clazz.defineMethod (c$, "output",
133 ($fz = function (s) {
134 var b = s.getBytes ();
135 this.os.write (b, 0, b.length);
137 }, $fz.isPrivate = true, $fz), "~S");
138 Clazz.defineMethod (c$, "closeDocument",
141 this.outputHeader ();
142 this.writeObjects ();
143 this.writeXRefTable ();
144 this.writeTrailer ();
148 Clazz.defineMethod (c$, "outputHeader",
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);
155 }, $fz.isPrivate = true, $fz));
156 Clazz.defineMethod (c$, "writeTrailer",
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",
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;
174 this.pt += o.output (this.os);
176 for (var i = 0; i < nObj; i++) {
177 var o = this.indirectObjects.get (i);
178 if (o.isFont ()) continue;
180 this.pt += o.output (this.os);
182 }, $fz.isPrivate = true, $fz));
183 Clazz.defineMethod (c$, "writeXRefTable",
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");
195 this.output (sb.toString ());
196 }, $fz.isPrivate = true, $fz));
197 Clazz.defineMethod (c$, "canDoLineTo",
201 Clazz.defineMethod (c$, "fill",
205 Clazz.defineMethod (c$, "stroke",
209 Clazz.defineMethod (c$, "doCircle",
210 function (x, y, r, doFill) {
211 var d = r * 4 * (Math.sqrt (2) - 1) / 3;
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");
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]);
226 this.g (doFill ? "f" : "s");
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;
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");
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");
256 Clazz.defineMethod (c$, "getRotation",
274 var a = (angle / 180.0 * 3.141592653589793);
277 if (Math.abs (cos) < 0.0001) cos = 0;
278 if (Math.abs (sin) < 0.0001) sin = 0;
280 return cos + " " + sin + " " + sin + " " + -cos;
282 Clazz.defineMethod (c$, "setColor",
283 function (rgb, isFill) {
284 this.g (rgb[0] + " " + rgb[1] + " " + rgb[2] + (isFill ? " rg" : " RG"));
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");
292 Clazz.defineMethod (c$, "setLineWidth",
294 this.g (width + " w");
296 Clazz.defineMethod (c$, "translateScale",
297 function (x, y, scale) {
298 this.g (scale + " 0 0 " + scale + " " + x + " " + y + " cm");