You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EPSReader.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.image.analyser;
  8. // Java
  9. import java.io.BufferedInputStream;
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.net.URL;
  14. import org.xml.sax.InputSource;
  15. import org.xml.sax.XMLReader;
  16. import org.apache.fop.fo.FOUserAgent;
  17. /**
  18. * ImageReader object for EPS document image type.
  19. */
  20. public class EPSReader extends AbstractImageReader {
  21. private long[] bbox;
  22. private boolean isAscii; // True if plain ascii eps file
  23. // offsets if not ascii
  24. long psStart = 0;
  25. long psLength = 0;
  26. long wmfStart = 0;
  27. long wmfLength = 0;
  28. long tiffStart = 0;
  29. long tiffLength = 0;
  30. /** raw eps file */
  31. private byte[] rawEps;
  32. /** eps part */
  33. private byte[] epsFile;
  34. private byte[] preview = null;
  35. private long getLong(byte[] buf, int idx) {
  36. int b1 = buf[idx] & 0xff;
  37. int b2 = buf[idx + 1] & 0xff;
  38. int b3 = buf[idx + 2] & 0xff;
  39. int b4 = buf[idx + 3] & 0xff;
  40. return (long)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
  41. }
  42. public boolean verifySignature(String uri, BufferedInputStream fis,
  43. FOUserAgent ua) throws IOException {
  44. boolean isEPS = false;
  45. this.imageStream = fis;
  46. fis.mark(32);
  47. byte[] header = new byte[30];
  48. fis.read(header, 0, 30);
  49. fis.reset();
  50. // Check if binary header
  51. if (getLong(header, 0) == 0xC6D3D0C5) {
  52. isAscii = false;
  53. isEPS = true;
  54. psStart = getLong(header, 4);
  55. psLength = getLong(header, 8);
  56. wmfStart = getLong(header, 12);
  57. wmfLength = getLong(header, 16);
  58. tiffStart = getLong(header, 20);
  59. tiffLength = getLong(header, 24);
  60. } else {
  61. // Check if plain ascii
  62. byte[] epsh = "%!PS".getBytes();
  63. if (epsh[0] == header[0] && epsh[1] == header[1] &&
  64. epsh[2] == header[2] && epsh[3] == header[3]) {
  65. isAscii = true;
  66. isEPS = true;
  67. }
  68. }
  69. if (isEPS) {
  70. readEPSImage(fis);
  71. bbox = readBBox();
  72. if (bbox != null) {
  73. width = (int)(bbox[2] - bbox[0]);
  74. height = (int)(bbox[3] - bbox[1]);
  75. } else {
  76. // Ain't eps if no BoundingBox
  77. isEPS = false;
  78. }
  79. }
  80. return isEPS;
  81. }
  82. /** read the eps file and extract eps part */
  83. private void readEPSImage(BufferedInputStream fis) throws IOException {
  84. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  85. byte[] file;
  86. byte[] readBuf = new byte[20480];
  87. int bytes_read;
  88. int index = 0;
  89. boolean cont = true;
  90. try {
  91. while ((bytes_read = fis.read(readBuf)) != -1) {
  92. baos.write(readBuf, 0, bytes_read);
  93. }
  94. } catch (java.io.IOException ex) {
  95. throw new IOException("Error while loading EPS image " +
  96. ex.getMessage());
  97. }
  98. file = baos.toByteArray();
  99. if (isAscii) {
  100. rawEps = null;
  101. epsFile = new byte[file.length];
  102. System.arraycopy(file, 0, epsFile, 0, epsFile.length);
  103. } else {
  104. rawEps = new byte[file.length];
  105. epsFile = new byte[(int) psLength];
  106. System.arraycopy(file, 0, rawEps, 0, rawEps.length);
  107. System.arraycopy(rawEps, (int) psStart, epsFile, 0,
  108. (int) psLength);
  109. }
  110. }
  111. public byte[] getEpsFile() {
  112. return epsFile;
  113. }
  114. /* Get embedded preview or null */
  115. public byte[] getPreview() {
  116. InputStream is = null;
  117. if (preview == null) {
  118. if (tiffLength > 0) {
  119. preview = new byte[(int) tiffLength];
  120. System.arraycopy(rawEps, (int) tiffStart, preview, 0,
  121. (int) tiffLength);
  122. }
  123. }
  124. return preview;
  125. }
  126. /** Extract bounding box from eps part
  127. */
  128. private long[] readBBox() {
  129. long[] mbbox = null;
  130. int idx = 0;
  131. byte[] bbxName = "%%BoundingBox: ".getBytes();
  132. boolean found = false;
  133. while (!found && (epsFile.length > (idx + bbxName.length))) {
  134. boolean sfound = true;
  135. int i = idx;
  136. for (i = idx; sfound && (i - idx) < bbxName.length; i++) {
  137. if (bbxName[i - idx] != epsFile[i])
  138. sfound = false;
  139. }
  140. if (sfound) {
  141. found = true;
  142. idx = i;
  143. } else {
  144. idx++;
  145. }
  146. }
  147. if (!found)
  148. return mbbox;
  149. mbbox = new long[4];
  150. idx += readLongString(mbbox, 0, idx);
  151. idx += readLongString(mbbox, 1, idx);
  152. idx += readLongString(mbbox, 2, idx);
  153. idx += readLongString(mbbox, 3, idx);
  154. return mbbox;
  155. }
  156. private int readLongString(long[] mbbox, int i, int idx) {
  157. while (idx < epsFile.length && (epsFile[idx] == 32))
  158. idx++;
  159. int nidx = idx;
  160. while (nidx < epsFile.length &&
  161. (epsFile[nidx] >= 48 && epsFile[nidx] <= 57))
  162. nidx++;
  163. byte[] num = new byte[nidx - idx];
  164. System.arraycopy(epsFile, idx, num, 0, nidx - idx);
  165. String ns = new String(num);
  166. mbbox[i] = Long.parseLong(ns);
  167. return (1 + nidx - idx);
  168. }
  169. public String getMimeType() {
  170. return "image/eps";
  171. }
  172. /**
  173. * Return the BoundingBox
  174. */
  175. public int[] getBBox() {
  176. int[] bbox = new int[4];
  177. bbox[0] = (int) this.bbox[0];
  178. bbox[1] = (int) this.bbox[1];
  179. bbox[2] = (int) this.bbox[2];
  180. bbox[3] = (int) this.bbox[3];
  181. return bbox;
  182. }
  183. }