-package javajs.export;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.IOException;\r
-import java.io.OutputStream;\r
-import java.util.Hashtable;\r
-import java.util.Map;\r
-import java.util.Map.Entry;\r
-import java.util.zip.Deflater;\r
-import java.util.zip.DeflaterOutputStream;\r
-\r
-import javajs.util.SB;\r
-\r
-\r
-/**\r
- * A rudimentary class for working with PDF document creation.\r
- * Written from scratch based on PDF Reference 13.\r
- * \r
- * @author hansonr Bob Hanson hansonr@stolaf.edu 10/28/2013\r
- * \r
- */\r
-class PDFObject extends SB { \r
- private Map<String, Object> dictionary;\r
- private byte[] stream;\r
- private int index;\r
- String type;\r
- int len;\r
- int pt;\r
- \r
- PDFObject(int index) {\r
- this.index = index;\r
- }\r
-\r
- String getRef() {\r
- return index + " 0 R";\r
- }\r
- \r
- String getID() {\r
- return type.substring(0, 1) + index;\r
- }\r
- \r
- boolean isFont() {\r
- return "Font".equals(type);\r
- }\r
-\r
- void setStream(byte[] stream) {\r
- this.stream = stream;\r
- }\r
-\r
- Object getDef(String key) {\r
- return dictionary.get(key);\r
- }\r
- \r
- void addDef(String key, Object value) {\r
- if (dictionary == null)\r
- dictionary = new Hashtable<String, Object>();\r
- dictionary.put(key, value);\r
- if (key.equals("Type"))\r
- type = ((String) value).substring(1);\r
- }\r
- \r
- void setAsStream() {\r
- stream = toBytes(0, -1);\r
- setLength(0);\r
- }\r
- \r
- int output(OutputStream os) throws IOException {\r
- if (index > 0) {\r
- String s = index + " 0 obj\n";\r
- write(os, s.getBytes(), 0);\r
- }\r
- int streamLen = 0;\r
- if (dictionary != null) {\r
- if (dictionary.containsKey("Length")) {\r
- if (stream == null)\r
- setAsStream();\r
- streamLen = stream.length;\r
- boolean doDeflate = (streamLen > 1000);\r
- if (doDeflate) {\r
- Deflater deflater = new Deflater(9);\r
- ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024);\r
- DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes,\r
- deflater);\r
- compBytes.write(stream, 0, streamLen);\r
- compBytes.finish();\r
- stream = outBytes.toByteArray();\r
- dictionary.put("Filter", "/FlateDecode");\r
- streamLen = stream.length;\r
- }\r
- dictionary.put("Length", "" + streamLen);\r
- }\r
- write(os, getDictionaryText(dictionary, "\n").getBytes(), 0);\r
- }\r
- if (length() > 0)\r
- write(os, this.toString().getBytes(), 0);\r
- if (stream != null) {\r
- write(os, "stream\r\n".getBytes(), 0);\r
- write(os, stream, streamLen);\r
- write(os, "\r\nendstream\r\n".getBytes(), 0);\r
- }\r
- if (index > 0)\r
- write(os, "endobj\n".getBytes(), 0);\r
- return len;\r
- }\r
-\r
- private void write(OutputStream os, byte[] bytes, int nBytes) throws IOException {\r
- if (nBytes == 0)\r
- nBytes = bytes.length;\r
- len += nBytes;\r
- os.write(bytes, 0, nBytes);\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- private String getDictionaryText(Map<String, Object> d, String nl) {\r
- SB sb = new SB();\r
- sb.append("<<");\r
- if (d.containsKey("Type"))\r
- sb.append("/Type").appendO(d.get("Type"));\r
- for (Entry<String, Object> e : d.entrySet()) {\r
- String s = e.getKey();\r
- if (s.equals("Type") || s.startsWith("!"))\r
- continue;\r
- sb.append("/" + s);\r
- Object o = e.getValue();\r
- if (o instanceof Map<?, ?>) {\r
- sb.append((getDictionaryText((Map<String, Object>) o, "")));\r
- continue;\r
- }\r
- s = (String) e.getValue();\r
- if (!s.startsWith("/"))\r
- sb.append(" ");\r
- sb.appendO(s);\r
- }\r
- return (sb.length() > 3 ? sb.append(">>").append(nl).toString() : "");\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- private Map<String, Object> createSubdict(Map<String, Object> d0, String dict) {\r
- Map<String, Object> d = (Map<String, Object>) d0.get(dict);\r
- if (d == null)\r
- d0.put(dict, d = new Hashtable<String, Object>());\r
- return d;\r
- }\r
-\r
- void addResource(String type, String key, String value) {\r
- Map<String, Object> r = createSubdict(dictionary, "Resources");\r
- if (type != null)\r
- r = createSubdict(r, type);\r
- r.put(key, value);\r
- }\r
- \r
-}\r
+package javajs.export;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.zip.Deflater;
+import java.util.zip.DeflaterOutputStream;
+
+import javajs.util.SB;
+
+
+/**
+ * A rudimentary class for working with PDF document creation.
+ * Written from scratch based on PDF Reference 13.
+ *
+ * @author hansonr Bob Hanson hansonr@stolaf.edu 10/28/2013
+ *
+ */
+class PDFObject extends SB {
+ private Map<String, Object> dictionary;
+ private byte[] stream;
+ private int index;
+ String type;
+ int len;
+ int pt;
+
+ PDFObject(int index) {
+ this.index = index;
+ }
+
+ String getRef() {
+ return index + " 0 R";
+ }
+
+ String getID() {
+ return type.substring(0, 1) + index;
+ }
+
+ boolean isFont() {
+ return "Font".equals(type);
+ }
+
+ void setStream(byte[] stream) {
+ this.stream = stream;
+ }
+
+ Object getDef(String key) {
+ return dictionary.get(key);
+ }
+
+ void addDef(String key, Object value) {
+ if (dictionary == null)
+ dictionary = new Hashtable<String, Object>();
+ dictionary.put(key, value);
+ if (key.equals("Type"))
+ type = ((String) value).substring(1);
+ }
+
+ void setAsStream() {
+ stream = toBytes(0, -1);
+ setLength(0);
+ }
+
+ int output(OutputStream os) throws IOException {
+ if (index > 0) {
+ String s = index + " 0 obj\n";
+ write(os, s.getBytes(), 0);
+ }
+ int streamLen = 0;
+ if (dictionary != null) {
+ if (dictionary.containsKey("Length")) {
+ if (stream == null)
+ setAsStream();
+ streamLen = stream.length;
+ boolean doDeflate = (streamLen > 1000);
+ if (doDeflate) {
+ Deflater deflater = new Deflater(9);
+ ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024);
+ DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes,
+ deflater);
+ compBytes.write(stream, 0, streamLen);
+ compBytes.finish();
+ stream = outBytes.toByteArray();
+ dictionary.put("Filter", "/FlateDecode");
+ streamLen = stream.length;
+ }
+ dictionary.put("Length", "" + streamLen);
+ }
+ write(os, getDictionaryText(dictionary, "\n").getBytes(), 0);
+ }
+ if (length() > 0)
+ write(os, this.toString().getBytes(), 0);
+ if (stream != null) {
+ write(os, "stream\r\n".getBytes(), 0);
+ write(os, stream, streamLen);
+ write(os, "\r\nendstream\r\n".getBytes(), 0);
+ }
+ if (index > 0)
+ write(os, "endobj\n".getBytes(), 0);
+ return len;
+ }
+
+ private void write(OutputStream os, byte[] bytes, int nBytes) throws IOException {
+ if (nBytes == 0)
+ nBytes = bytes.length;
+ len += nBytes;
+ os.write(bytes, 0, nBytes);
+ }
+
+ @SuppressWarnings("unchecked")
+ private String getDictionaryText(Map<String, Object> d, String nl) {
+ SB sb = new SB();
+ sb.append("<<");
+ if (d.containsKey("Type"))
+ sb.append("/Type").appendO(d.get("Type"));
+ for (Entry<String, Object> e : d.entrySet()) {
+ String s = e.getKey();
+ if (s.equals("Type") || s.startsWith("!"))
+ continue;
+ sb.append("/" + s);
+ Object o = e.getValue();
+ if (o instanceof Map<?, ?>) {
+ sb.append((getDictionaryText((Map<String, Object>) o, "")));
+ continue;
+ }
+ s = (String) e.getValue();
+ if (!s.startsWith("/"))
+ sb.append(" ");
+ sb.appendO(s);
+ }
+ return (sb.length() > 3 ? sb.append(">>").append(nl).toString() : "");
+ }
+
+ @SuppressWarnings("unchecked")
+ private Map<String, Object> createSubdict(Map<String, Object> d0, String dict) {
+ Map<String, Object> d = (Map<String, Object>) d0.get(dict);
+ if (d == null)
+ d0.put(dict, d = new Hashtable<String, Object>());
+ return d;
+ }
+
+ void addResource(String type, String key, String value) {
+ Map<String, Object> r = createSubdict(dictionary, "Resources");
+ if (type != null)
+ r = createSubdict(r, type);
+ r.put(key, value);
+ }
+
+}