JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[jalviewjs.git] / bin / javajs / export / PDFObject.js
1 Clazz.declarePackage ("javajs.export");
2 Clazz.load (["javajs.util.SB"], "javajs.export.PDFObject", ["java.io.ByteArrayOutputStream", "java.util.Hashtable", "$.Map", "java.util.zip.Deflater", "$.DeflaterOutputStream"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.dictionary = null;
5 this.stream = null;
6 this.index = 0;
7 this.type = null;
8 this.len = 0;
9 this.pt = 0;
10 Clazz.instantialize (this, arguments);
11 }, javajs["export"], "PDFObject", javajs.util.SB);
12 Clazz.makeConstructor (c$, 
13 function (index) {
14 Clazz.superConstructor (this, javajs["export"].PDFObject, []);
15 this.index = index;
16 }, "~N");
17 Clazz.defineMethod (c$, "getRef", 
18 function () {
19 return this.index + " 0 R";
20 });
21 Clazz.defineMethod (c$, "getID", 
22 function () {
23 return this.type.substring (0, 1) + this.index;
24 });
25 Clazz.defineMethod (c$, "isFont", 
26 function () {
27 return "Font".equals (this.type);
28 });
29 Clazz.defineMethod (c$, "setStream", 
30 function (stream) {
31 this.stream = stream;
32 }, "~A");
33 Clazz.defineMethod (c$, "getDef", 
34 function (key) {
35 return this.dictionary.get (key);
36 }, "~S");
37 Clazz.defineMethod (c$, "addDef", 
38 function (key, value) {
39 if (this.dictionary == null) this.dictionary =  new java.util.Hashtable ();
40 this.dictionary.put (key, value);
41 if (key.equals ("Type")) this.type = (value).substring (1);
42 }, "~S,~O");
43 Clazz.defineMethod (c$, "setAsStream", 
44 function () {
45 this.stream = this.toBytes (0, -1);
46 this.setLength (0);
47 });
48 Clazz.defineMethod (c$, "output", 
49 function (os) {
50 if (this.index > 0) {
51 var s = this.index + " 0 obj\n";
52 this.write (os, s.getBytes (), 0);
53 }var streamLen = 0;
54 if (this.dictionary != null) {
55 if (this.dictionary.containsKey ("Length")) {
56 if (this.stream == null) this.setAsStream ();
57 streamLen = this.stream.length;
58 var doDeflate = (streamLen > 1000);
59 if (doDeflate) {
60 var deflater =  new java.util.zip.Deflater (9);
61 var outBytes =  new java.io.ByteArrayOutputStream (1024);
62 var compBytes =  new java.util.zip.DeflaterOutputStream (outBytes, deflater);
63 compBytes.write (this.stream, 0, streamLen);
64 compBytes.finish ();
65 this.stream = outBytes.toByteArray ();
66 this.dictionary.put ("Filter", "/FlateDecode");
67 streamLen = this.stream.length;
68 }this.dictionary.put ("Length", "" + streamLen);
69 }this.write (os, this.getDictionaryText (this.dictionary, "\n").getBytes (), 0);
70 }if (this.length () > 0) this.write (os, this.toString ().getBytes (), 0);
71 if (this.stream != null) {
72 this.write (os, "stream\r\n".getBytes (), 0);
73 this.write (os, this.stream, streamLen);
74 this.write (os, "\r\nendstream\r\n".getBytes (), 0);
75 }if (this.index > 0) this.write (os, "endobj\n".getBytes (), 0);
76 return this.len;
77 }, "java.io.OutputStream");
78 Clazz.defineMethod (c$, "write", 
79 ($fz = function (os, bytes, nBytes) {
80 if (nBytes == 0) nBytes = bytes.length;
81 this.len += nBytes;
82 os.write (bytes, 0, nBytes);
83 }, $fz.isPrivate = true, $fz), "java.io.OutputStream,~A,~N");
84 Clazz.defineMethod (c$, "getDictionaryText", 
85 ($fz = function (d, nl) {
86 var sb =  new javajs.util.SB ();
87 sb.append ("<<");
88 if (d.containsKey ("Type")) sb.append ("/Type").appendO (d.get ("Type"));
89 for (var e, $e = d.entrySet ().iterator (); $e.hasNext () && ((e = $e.next ()) || true);) {
90 var s = e.getKey ();
91 if (s.equals ("Type") || s.startsWith ("!")) continue;
92 sb.append ("/" + s);
93 var o = e.getValue ();
94 if (Clazz.instanceOf (o, java.util.Map)) {
95 sb.append ((this.getDictionaryText (o, "")));
96 continue;
97 }s = e.getValue ();
98 if (!s.startsWith ("/")) sb.append (" ");
99 sb.appendO (s);
100 }
101 return (sb.length () > 3 ? sb.append (">>").append (nl).toString () : "");
102 }, $fz.isPrivate = true, $fz), "java.util.Map,~S");
103 Clazz.defineMethod (c$, "createSubdict", 
104 ($fz = function (d0, dict) {
105 var d = d0.get (dict);
106 if (d == null) d0.put (dict, d =  new java.util.Hashtable ());
107 return d;
108 }, $fz.isPrivate = true, $fz), "java.util.Map,~S");
109 Clazz.defineMethod (c$, "addResource", 
110 function (type, key, value) {
111 var r = this.createSubdict (this.dictionary, "Resources");
112 if (type != null) r = this.createSubdict (r, type);
113 r.put (key, value);
114 }, "~S,~S,~S");
115 });