1 51 package org.apache.fop.pdf; 52 53 import org.apache.fop.configuration.Configuration; 55 import org.apache.fop.messaging.MessageHandler; 56 57 import java.io.ByteArrayOutputStream ; 59 import java.io.OutputStream ; 60 import java.io.IOException ; 61 import java.io.UnsupportedEncodingException ; 62 import java.util.List ; 63 64 72 public class PDFStream extends PDFObject { 73 74 77 protected ByteArrayOutputStream _data; 78 79 82 private List _filters; 83 84 89 public PDFStream(int number) { 90 super(number); 91 _data = new ByteArrayOutputStream (); 92 _filters = new java.util.ArrayList (); 93 } 94 95 100 public void add(String s) { 101 try { 102 try { 103 _data.write(s.getBytes(PDFDocument.ENCODING)); 104 } catch (UnsupportedEncodingException ue) { 105 _data.write(s.getBytes()); 106 } 107 } catch (IOException ex) { 108 ex.printStackTrace(); 109 } 110 111 } 112 113 120 public void addFilter(PDFFilter filter) { 121 if (filter != null) { 122 _filters.add(filter); 123 } 124 125 } 126 127 public void addFilter(String filterType) { 128 if (filterType == null) { 129 return; 130 } 131 if (filterType.equals("flate")) { 132 addFilter(new FlateFilter()); 133 } else if (filterType.equals("ascii-85")) { 134 addFilter(new ASCII85Filter()); 135 } else if (filterType.equals("ascii-hex")) { 136 addFilter(new ASCIIHexFilter()); 137 } else if (filterType.equals("")) { 138 return; 139 } else { 140 MessageHandler.errorln("Unsupported filter type in stream-filter-list: " 141 + filterType); 142 } 143 } 144 145 146 protected void addDefaultFilters() { 147 List filters = Configuration.getListValue("stream-filter-list", 148 Configuration.PDF); 149 if (filters == null) { 150 String filter = Configuration.getStringValue("stream-filter-list", 152 Configuration.PDF); 153 if (filter == null) { 154 addFilter(new FlateFilter()); 156 } else { 157 addFilter(filter); 158 } 159 } else { 160 for (int i = 0; i < filters.size(); i++) { 161 String v = (String )filters.get(i); 162 addFilter(v); 163 } 164 } 165 } 166 167 168 175 public void addImageArray(int[] pixels, int width, int height) { 176 try { 177 for (int i = 0; i < height; i++) { 178 for (int j = 0; j < width; j++) { 179 int p = pixels[i * width + j]; 180 int r = (p >> 16) & 0xFF; 181 int g = (p >> 8) & 0xFF; 182 int b = (p) & 0xFF; 183 if (r < 16) { 184 _data.write('0'); 185 } 186 try { 187 _data.write(Integer.toHexString(r).getBytes(PDFDocument.ENCODING)); 188 } catch (UnsupportedEncodingException ue) { 189 _data.write(Integer.toHexString(r).getBytes()); 190 } 191 if (g < 16) { 192 _data.write('0'); 193 } 194 try { 195 _data.write(Integer.toHexString(g).getBytes(PDFDocument.ENCODING)); 196 } catch (UnsupportedEncodingException ue) { 197 _data.write(Integer.toHexString(g).getBytes()); 198 } 199 if (b < 16) { 200 _data.write('0'); 201 } 202 try { 203 _data.write(Integer.toHexString(b).getBytes(PDFDocument.ENCODING)); 204 } catch (UnsupportedEncodingException ue) { 205 _data.write(Integer.toHexString(b).getBytes()); 206 } 207 _data.write(' '); 208 } 209 } 210 try { 211 _data.write(">\n".getBytes(PDFDocument.ENCODING)); 212 } catch (UnsupportedEncodingException ue) { 213 _data.write(">\n".getBytes()); 214 } 215 } catch (IOException ex) { 216 ex.printStackTrace(); 217 } 218 219 } 220 221 public void setData(byte[] data) throws IOException { 222 _data.reset(); 223 _data.write(data); 224 } 225 226 public byte[] getData() { 227 return _data.toByteArray(); 228 } 229 230 public int getDataLength() { 231 return _data.size(); 232 } 233 234 235 236 241 254 public byte[] toPDF() { 255 throw new RuntimeException (); 256 } 257 258 259 protected int output(OutputStream stream) throws IOException { 262 int length = 0; 263 String filterEntry = applyFilters(); 264 String s = this.number + " " + this.generation + " obj\n<< /Length " 265 + (_data.size() + 1) + " " + filterEntry 266 + " >>\n"; 267 byte[] p; 268 try { 269 p = s.getBytes(PDFDocument.ENCODING); 270 } catch (UnsupportedEncodingException ue) { 271 p = s.getBytes(); 272 } 273 stream.write(p); 274 length += p.length; 275 length += outputStreamData(stream); 276 try { 277 p = "endobj\n".getBytes(PDFDocument.ENCODING); 278 } catch (UnsupportedEncodingException ue) { 279 p = "endobj\n".getBytes(); 280 } 281 stream.write(p); 282 length += p.length; 283 return length; 284 285 } 286 287 290 protected int outputStreamData(OutputStream stream) throws IOException { 291 int length = 0; 292 byte[] p; 293 try { 294 p = "stream\n".getBytes(PDFDocument.ENCODING); 295 } catch (UnsupportedEncodingException ue) { 296 p = "stream\n".getBytes(); 297 } 298 stream.write(p); 299 length += p.length; 300 _data.writeTo(stream); 301 length += _data.size(); 302 try { 303 p = "\nendstream\n".getBytes(PDFDocument.ENCODING); 304 } catch (UnsupportedEncodingException ue) { 305 p = "\nendstream\n".getBytes(); 306 } 307 stream.write(p); 308 length += p.length; 309 return length; 310 311 } 312 313 314 321 protected String applyFilters() throws IOException { 322 if (_filters.size() > 0) { 323 List names = new java.util.ArrayList (); 324 List parms = new java.util.ArrayList (); 325 326 for (int i = 0; i < _filters.size(); i++) { 328 PDFFilter filter = (PDFFilter)_filters.get(i); 329 if (!filter.isApplied()) { 331 byte[] tmp = filter.encode(_data.toByteArray()); 332 _data.reset(); 333 _data.write(tmp); 334 filter.setApplied(true); 335 } 336 names.add(0, filter.getName()); 338 parms.add(0, filter.getDecodeParms()); 339 } 340 341 return buildFilterEntries(names) + buildDecodeParms(parms); 343 } 344 return ""; 345 346 } 347 348 private String buildFilterEntries(List names) { 349 StringBuffer sb = new StringBuffer (); 350 sb.append("/Filter "); 351 if (names.size() > 1) { 352 sb.append("[ "); 353 } 354 for (int i = 0; i < names.size(); i++) { 355 sb.append((String )names.get(i)); 356 sb.append(" "); 357 } 358 if (names.size() > 1) { 359 sb.append("]"); 360 } 361 sb.append("\n"); 362 return sb.toString(); 363 } 364 365 private String buildDecodeParms(List parms) { 366 StringBuffer sb = new StringBuffer (); 367 boolean needParmsEntry = false; 368 sb.append("/DecodeParms "); 369 370 if (parms.size() > 1) { 371 sb.append("[ "); 372 } 373 for (int i = 0; i < parms.size(); i++) { 374 String s = (String )parms.get(i); 375 if (s != null) { 376 sb.append(s); 377 needParmsEntry = true; 378 } else { 379 sb.append("null"); 380 } 381 sb.append(" "); 382 } 383 if (parms.size() > 1) { 384 sb.append("]"); 385 } 386 sb.append("\n"); 387 if (needParmsEntry) { 388 return sb.toString(); 389 } else { 390 return ""; 391 } 392 } 393 394 395 } 396 | Popular Tags |