Debian Codegraph @

src/main/java/org/jfree/pdf/dictionary/Dictionary.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf.dictionary;
33
34 import java.awt.geom.Rectangle2D;
35 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.util.HashMap;
38 import java.util.Map;
39 import org.jfree.pdf.internal.PDFObject;
40 import org.jfree.pdf.util.PDFUtils;
41
42 /**
43 * A dictionary is a map and supports writing the bytes for the dictionary
44 * in the PDF syntax. The dictionary has an optional {@code type} entry
45 * which is treated as a special case (to ensure it comes first in the output
46 * if it is specified).
47 */
48 public class Dictionary {
49
50 /**
51 * The type entry. We treat this as a special case, because when a type is
52 * defined, we want it to appear first in the PDF output. Note that it
53 * can be set to null for some dictionaries.
54 */
55 private String type;
56
57 /** Data storage. */
58 private Map<String, Object> map;
59
60 /**
61 * Creates a new instance with no type.
62 */
63 public Dictionary() {
64 this(null);
65 }
66
67 /**
68 * Creates a new dictionary with the specified type (which can be
69 * {@code null}).
70 *
71 * @param type the type value (for example, "/Catalog").
72 */
73 public Dictionary(String type) {
74 this.type = type;
75 this.map = new HashMap<>();
76 }
77
78 /**
79 * Returns the dictionary type.
80 *
81 * @return The dictionary type (possibly ({@code null}).
82 */
83 public String getType() {
84 return this.type;
85 }
86
87 /**
88 * Sets the type (for example, "/Catalog").
89 *
90 * @param type the type ({@code null} permitted).
91 */
92 public void setType(String type) {
93 this.type = type;
94 }
95
96 /**
97 * Returns {@code true} if the dictionary has no entries, and
98 * {@code false} otherwise.
99 *
100 * @return A boolean.
101 *
102 * @see #size()
103 */
104 public boolean isEmpty() {
105 return this.map.isEmpty();
106 }
107
108 /**
109 * Returns the number of items in the dictionary.
110 *
111 * @return The number of items in the dictionary.
112 */
113 public int size() {
114 return this.map.size();
115 }
116
117 /**
118 * Puts an entry in the dictionary.
119 *
120 * @param key the key.
121 * @param value the value.
122 */
123 public void put(String key, Object value) {
124 this.map.put(key, value);
125 }
126
127 /**
128 * Removes an entry from the dictionary, returning the value that was
129 * stored previously.
130 *
131 * @param key the key.
132 *
133 * @return The value that was associated with the key.
134 */
135 public Object remove(String key) {
136 return this.map.remove(key);
137 }
138
139 /**
140 * Returns a byte array containing the ASCII encoding of the dictionary.
141 *
142 * @return A byte array.
143 */
144 public byte[] toPDFBytes() {
145 ByteArrayOutputStream baos = new ByteArrayOutputStream();
146 try {
147 // here we are first creating the String version, then encoding
148 // to bytes...it would be more efficient to go direct to bytes
149 // but this will do for now
150 baos.write(PDFUtils.toBytes(toPDFString()));
151 } catch (IOException ex) {
152 throw new RuntimeException("Dictionary.toPDFBytes() failed.", ex);
153 }
154 return baos.toByteArray();
155 }
156
157 /**
158 * Returns a string containing the PDF text describing the dictionary.
159 * Note that this is a Java string, conversion to byte format happens
160 * elsewhere.
161 *
162 * @return A string.
163 */
164 public String toPDFString() {
165 StringBuilder b = new StringBuilder();
166 b.append("<< ");
167 if (this.type != null) {
168 b.append("/Type ").append(this.type).append("\n");
169 }
170 // now iterate through the dictionary and write its values
171 for (String key : this.map.keySet()) {
172 Object value = this.map.get(key);
173 if (value instanceof Number || value instanceof String) {
174 b.append(key).append(" ");
175 b.append(value.toString()).append("\n");
176 } else if (value instanceof PDFObject) {
177 PDFObject pdfObj = (PDFObject) value;
178 b.append(key).append(" ");
179 b.append(pdfObj.getReference()).append("\n");
180 } else if (value instanceof String[]) {
181 b.append(key).append(" ");
182 String[] array = (String[]) value;
183 b.append("[");
184 for (int i = 0; i < array.length; i++) {
185 if (i != 0) {
186 b.append(" ");
187 }
188 b.append(array[i]);
189 }
190 b.append("]\n");
191 } else if (value instanceof PDFObject[]) {
192 b.append(key).append(" ");
193 PDFObject[] array = (PDFObject[]) value;
194 b.append("[");
195 for (int i = 0; i < array.length; i++) {
196 if (i != 0) {
197 b.append(" ");
198 }
199 b.append(array[i].getReference());
200 }
201 b.append("]\n");
202 } else if (value instanceof Rectangle2D) {
203 Rectangle2D r = (Rectangle2D) value;
204 b.append(key).append(" ");
205 b.append("[").append(r.getX()).append(" ");
206 b.append(r.getY()).append(" ").append(r.getWidth()).append(" ");
207 b.append(r.getHeight()).append("]\n");
208 } else if (value instanceof Dictionary) {
209 b.append(key).append(" ");
210 Dictionary d = (Dictionary) value;
211 b.append(d.toPDFString());
212 } else if (value instanceof float[]) {
213 b.append(key).append(" ");
214 float[] array = (float[]) value;
215 b.append("[");
216 for (int i = 0; i < array.length; i++) {
217 if (i != 0) {
218 b.append(" ");
219 }
220 b.append(array[i]);
221 }
222 b.append("]\n");
223
224 } else {
225 throw new RuntimeException("Unrecognised value type: " + value);
226 }
227 }
228 b.append(">>\n");
229 return b.toString();
230 }
231
232 }