Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PSDocumentHandler.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.ps;
  19. import java.awt.Dimension;
  20. import java.awt.geom.Rectangle2D;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.Collection;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Map;
  29. import javax.xml.transform.Source;
  30. import org.apache.commons.io.IOUtils;
  31. import org.apache.commons.logging.Log;
  32. import org.apache.commons.logging.LogFactory;
  33. import org.apache.xmlgraphics.ps.DSCConstants;
  34. import org.apache.xmlgraphics.ps.PSGenerator;
  35. import org.apache.xmlgraphics.ps.PSPageDeviceDictionary;
  36. import org.apache.xmlgraphics.ps.PSProcSets;
  37. import org.apache.xmlgraphics.ps.PSResource;
  38. import org.apache.xmlgraphics.ps.dsc.DSCException;
  39. import org.apache.xmlgraphics.ps.dsc.ResourceTracker;
  40. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox;
  41. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox;
  42. import org.apache.fop.apps.FOUserAgent;
  43. import org.apache.fop.apps.MimeConstants;
  44. import org.apache.fop.fonts.LazyFont;
  45. import org.apache.fop.fonts.Typeface;
  46. import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
  47. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  48. import org.apache.fop.render.intermediate.IFException;
  49. import org.apache.fop.render.intermediate.IFPainter;
  50. import org.apache.fop.render.ps.extensions.PSExtensionAttachment;
  51. import org.apache.fop.render.ps.extensions.PSSetPageDevice;
  52. /**
  53. * {@code IFDocumentHandler} implementation that produces PostScript.
  54. */
  55. public class PSDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
  56. /** logging instance */
  57. private static Log log = LogFactory.getLog(PSDocumentHandler.class);
  58. /**
  59. * Utility class which enables all sorts of features that are not directly connected to the
  60. * normal rendering process.
  61. */
  62. protected PSRenderingUtil psUtil;
  63. /** The PostScript generator used to output the PostScript */
  64. protected PSGenerator gen;
  65. /** the temporary file in case of two-pass processing */
  66. private File tempFile;
  67. private int currentPageNumber = 0;
  68. /** Is used to determine the document's bounding box */
  69. private Rectangle2D documentBoundingBox;
  70. /** Used to temporarily store PSSetupCode instance until they can be written. */
  71. private List setupCodeList;
  72. /** This is a map of PSResource instances of all fonts defined (key: font key) */
  73. private Map fontResources;
  74. /** This is a map of PSResource instances of all forms (key: uri) */
  75. private Map formResources;
  76. /** encapsulation of dictionary used in setpagedevice instruction **/
  77. private PSPageDeviceDictionary pageDeviceDictionary;
  78. /** This is a collection holding all document header comments */
  79. private Collection headerComments;
  80. /** This is a collection holding all document footer comments */
  81. private Collection footerComments;
  82. /**
  83. * Default constructor.
  84. */
  85. public PSDocumentHandler() {
  86. }
  87. /** {@inheritDoc} */
  88. public boolean supportsPagesOutOfOrder() {
  89. return false;
  90. }
  91. /** {@inheritDoc} */
  92. public String getMimeType() {
  93. return MimeConstants.MIME_POSTSCRIPT;
  94. }
  95. /** {@inheritDoc} */
  96. public void setUserAgent(FOUserAgent ua) {
  97. super.setUserAgent(ua);
  98. this.psUtil = new PSRenderingUtil(ua);
  99. }
  100. /** {@inheritDoc} */
  101. public IFDocumentHandlerConfigurator getConfigurator() {
  102. return new PSRendererConfigurator(getUserAgent());
  103. }
  104. PSRenderingUtil getPSUtil() {
  105. return this.psUtil;
  106. }
  107. /** {@inheritDoc} */
  108. public void startDocument() throws IFException {
  109. try {
  110. if (getUserAgent() == null) {
  111. throw new IllegalStateException(
  112. "User agent must be set before starting PostScript generation");
  113. }
  114. if (this.outputStream == null) {
  115. throw new IllegalStateException("OutputStream hasn't been set through setResult()");
  116. }
  117. OutputStream out;
  118. if (psUtil.isOptimizeResources()) {
  119. this.tempFile = File.createTempFile("fop", null);
  120. out = new java.io.FileOutputStream(this.tempFile);
  121. out = new java.io.BufferedOutputStream(out);
  122. } else {
  123. out = this.outputStream;
  124. }
  125. //Setup for PostScript generation
  126. this.gen = new PSGenerator(out) {
  127. /** Need to subclass PSGenerator to have better URI resolution */
  128. public Source resolveURI(String uri) {
  129. return getUserAgent().resolveURI(uri);
  130. }
  131. };
  132. this.gen.setPSLevel(psUtil.getLanguageLevel());
  133. this.currentPageNumber = 0;
  134. this.documentBoundingBox = new Rectangle2D.Double();
  135. //Initial default page device dictionary settings
  136. this.pageDeviceDictionary = new PSPageDeviceDictionary();
  137. pageDeviceDictionary.setFlushOnRetrieval(!psUtil.isDSCComplianceEnabled());
  138. pageDeviceDictionary.put("/ImagingBBox", "null");
  139. } catch (IOException e) {
  140. throw new IFException("I/O error in startDocument()", e);
  141. }
  142. }
  143. private void writeHeader() throws IOException {
  144. //PostScript Header
  145. gen.writeln(DSCConstants.PS_ADOBE_30);
  146. gen.writeDSCComment(DSCConstants.CREATOR, new String[] {getUserAgent().getProducer()});
  147. gen.writeDSCComment(DSCConstants.CREATION_DATE, new Object[] {new java.util.Date()});
  148. gen.writeDSCComment(DSCConstants.LANGUAGE_LEVEL, new Integer(gen.getPSLevel()));
  149. gen.writeDSCComment(DSCConstants.PAGES, new Object[] {DSCConstants.ATEND});
  150. gen.writeDSCComment(DSCConstants.BBOX, DSCConstants.ATEND);
  151. gen.writeDSCComment(DSCConstants.HIRES_BBOX, DSCConstants.ATEND);
  152. gen.writeDSCComment(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES,
  153. new Object[] {DSCConstants.ATEND});
  154. if (headerComments != null) {
  155. for (Iterator iter = headerComments.iterator(); iter.hasNext();) {
  156. PSExtensionAttachment comment = (PSExtensionAttachment)iter.next();
  157. gen.writeln("%" + comment.getContent());
  158. }
  159. }
  160. gen.writeDSCComment(DSCConstants.END_COMMENTS);
  161. //Defaults
  162. gen.writeDSCComment(DSCConstants.BEGIN_DEFAULTS);
  163. gen.writeDSCComment(DSCConstants.END_DEFAULTS);
  164. //Prolog and Setup written right before the first page-sequence, see startPageSequence()
  165. //Do this only once, as soon as we have all the content for the Setup section!
  166. //Prolog
  167. gen.writeDSCComment(DSCConstants.BEGIN_PROLOG);
  168. PSProcSets.writeStdProcSet(gen);
  169. PSProcSets.writeEPSProcSet(gen);
  170. gen.writeDSCComment(DSCConstants.END_PROLOG);
  171. //Setup
  172. gen.writeDSCComment(DSCConstants.BEGIN_SETUP);
  173. PSRenderingUtil.writeSetupCodeList(gen, setupCodeList, "SetupCode");
  174. if (!psUtil.isOptimizeResources()) {
  175. this.fontResources = PSFontUtils.writeFontDict(gen, fontInfo);
  176. } else {
  177. gen.commentln("%FOPFontSetup"); //Place-holder, will be replaced in the second pass
  178. }
  179. gen.writeDSCComment(DSCConstants.END_SETUP);
  180. }
  181. /** {@inheritDoc} */
  182. public void endDocumentHeader() throws IFException {
  183. try {
  184. writeHeader();
  185. } catch (IOException ioe) {
  186. throw new IFException("I/O error writing the PostScript header", ioe);
  187. }
  188. }
  189. /** {@inheritDoc} */
  190. public void endDocument() throws IFException {
  191. try {
  192. //Write trailer
  193. gen.writeDSCComment(DSCConstants.TRAILER);
  194. if (footerComments != null) {
  195. for (Iterator iter = footerComments.iterator(); iter.hasNext();) {
  196. PSExtensionAttachment comment = (PSExtensionAttachment)iter.next();
  197. gen.commentln("%" + comment.getContent());
  198. }
  199. footerComments.clear();
  200. }
  201. gen.writeDSCComment(DSCConstants.PAGES, new Integer(this.currentPageNumber));
  202. new DSCCommentBoundingBox(this.documentBoundingBox).generate(gen);
  203. new DSCCommentHiResBoundingBox(this.documentBoundingBox).generate(gen);
  204. gen.getResourceTracker().writeResources(false, gen);
  205. gen.writeDSCComment(DSCConstants.EOF);
  206. gen.flush();
  207. log.debug("Rendering to PostScript complete.");
  208. if (psUtil.isOptimizeResources()) {
  209. IOUtils.closeQuietly(gen.getOutputStream());
  210. rewritePostScriptFile();
  211. }
  212. if (footerComments != null) {
  213. headerComments.clear();
  214. }
  215. if (pageDeviceDictionary != null) {
  216. pageDeviceDictionary.clear();
  217. }
  218. } catch (IOException ioe) {
  219. throw new IFException("I/O error in endDocument()", ioe);
  220. }
  221. super.endDocument();
  222. }
  223. /**
  224. * Used for two-pass production. This will rewrite the PostScript file from the temporary
  225. * file while adding all needed resources.
  226. * @throws IOException In case of an I/O error.
  227. */
  228. private void rewritePostScriptFile() throws IOException {
  229. log.debug("Processing PostScript resources...");
  230. long startTime = System.currentTimeMillis();
  231. ResourceTracker resTracker = gen.getResourceTracker();
  232. InputStream in = new java.io.FileInputStream(this.tempFile);
  233. in = new java.io.BufferedInputStream(in);
  234. try {
  235. try {
  236. ResourceHandler handler = new ResourceHandler(getUserAgent(), this.fontInfo,
  237. resTracker, this.formResources);
  238. handler.process(in, this.outputStream,
  239. this.currentPageNumber, this.documentBoundingBox);
  240. this.outputStream.flush();
  241. } catch (DSCException e) {
  242. throw new RuntimeException(e.getMessage());
  243. }
  244. } finally {
  245. IOUtils.closeQuietly(in);
  246. if (!this.tempFile.delete()) {
  247. this.tempFile.deleteOnExit();
  248. log.warn("Could not delete temporary file: " + this.tempFile);
  249. }
  250. }
  251. if (log.isDebugEnabled()) {
  252. long duration = System.currentTimeMillis() - startTime;
  253. log.debug("Resource Processing complete in " + duration + " ms.");
  254. }
  255. }
  256. /** {@inheritDoc} */
  257. public void startPageSequence(String id) throws IFException {
  258. //nop
  259. }
  260. /** {@inheritDoc} */
  261. public void endPageSequence() throws IFException {
  262. //nop
  263. }
  264. /** {@inheritDoc} */
  265. public void startPage(int index, String name, String pageMasterName, Dimension size)
  266. throws IFException {
  267. try {
  268. if (this.currentPageNumber == 0) {
  269. //writeHeader();
  270. }
  271. this.currentPageNumber++;
  272. gen.getResourceTracker().notifyStartNewPage();
  273. gen.getResourceTracker().notifyResourceUsageOnPage(PSProcSets.STD_PROCSET);
  274. gen.writeDSCComment(DSCConstants.PAGE, new Object[]
  275. {name,
  276. new Integer(this.currentPageNumber)});
  277. double pageWidth = size.width / 1000.0;
  278. double pageHeight = size.height / 1000.0;
  279. boolean rotate = false;
  280. List pageSizes = new java.util.ArrayList();
  281. if (this.psUtil.isAutoRotateLandscape() && (pageHeight < pageWidth)) {
  282. rotate = true;
  283. pageSizes.add(new Long(Math.round(pageHeight)));
  284. pageSizes.add(new Long(Math.round(pageWidth)));
  285. } else {
  286. pageSizes.add(new Long(Math.round(pageWidth)));
  287. pageSizes.add(new Long(Math.round(pageHeight)));
  288. }
  289. pageDeviceDictionary.put("/PageSize", pageSizes);
  290. //TODO Handle extension attachments for the page!!!!!!!
  291. /*
  292. if (page.hasExtensionAttachments()) {
  293. for (Iterator iter = page.getExtensionAttachments().iterator();
  294. iter.hasNext();) {
  295. ExtensionAttachment attachment = (ExtensionAttachment) iter.next();
  296. if (attachment instanceof PSSetPageDevice) {*/
  297. /**
  298. * Extract all PSSetPageDevice instances from the
  299. * attachment list on the s-p-m and add all
  300. * dictionary entries to our internal representation
  301. * of the the page device dictionary.
  302. *//*
  303. PSSetPageDevice setPageDevice = (PSSetPageDevice)attachment;
  304. String content = setPageDevice.getContent();
  305. if (content != null) {
  306. try {
  307. pageDeviceDictionary.putAll(PSDictionary.valueOf(content));
  308. } catch (PSDictionaryFormatException e) {
  309. PSEventProducer eventProducer = PSEventProducer.Provider.get(
  310. getUserAgent().getEventBroadcaster());
  311. eventProducer.postscriptDictionaryParseError(this, content, e);
  312. }
  313. }
  314. }
  315. }
  316. }*/
  317. if (setupCodeList != null) {
  318. PSRenderingUtil.writeEnclosedExtensionAttachments(gen, setupCodeList);
  319. setupCodeList.clear();
  320. }
  321. final Integer zero = new Integer(0);
  322. Rectangle2D pageBoundingBox = new Rectangle2D.Double();
  323. if (rotate) {
  324. pageBoundingBox.setRect(0, 0, pageHeight, pageWidth);
  325. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[] {
  326. zero, zero, new Long(Math.round(pageHeight)),
  327. new Long(Math.round(pageWidth)) });
  328. gen.writeDSCComment(DSCConstants.PAGE_HIRES_BBOX, new Object[] {
  329. zero, zero, new Double(pageHeight),
  330. new Double(pageWidth) });
  331. gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
  332. } else {
  333. pageBoundingBox.setRect(0, 0, pageWidth, pageHeight);
  334. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[] {
  335. zero, zero, new Long(Math.round(pageWidth)),
  336. new Long(Math.round(pageHeight)) });
  337. gen.writeDSCComment(DSCConstants.PAGE_HIRES_BBOX, new Object[] {
  338. zero, zero, new Double(pageWidth),
  339. new Double(pageHeight) });
  340. if (psUtil.isAutoRotateLandscape()) {
  341. gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION,
  342. "Portrait");
  343. }
  344. }
  345. this.documentBoundingBox.add(pageBoundingBox);
  346. gen.writeDSCComment(DSCConstants.PAGE_RESOURCES,
  347. new Object[] {DSCConstants.ATEND});
  348. gen.commentln("%FOPSimplePageMaster: " + pageMasterName);
  349. gen.writeDSCComment(DSCConstants.BEGIN_PAGE_SETUP);
  350. //TODO Handle extension attachments for the page!!!!!!!
  351. /*
  352. if (page.hasExtensionAttachments()) {
  353. List extensionAttachments = page.getExtensionAttachments();
  354. for (int i = 0; i < extensionAttachments.size(); i++) {
  355. Object attObj = extensionAttachments.get(i);
  356. if (attObj instanceof PSExtensionAttachment) {
  357. PSExtensionAttachment attachment = (PSExtensionAttachment)attObj;
  358. if (attachment instanceof PSCommentBefore) {
  359. gen.commentln("%" + attachment.getContent());
  360. } else if (attachment instanceof PSSetupCode) {
  361. gen.writeln(attachment.getContent());
  362. }
  363. }
  364. }
  365. }*/
  366. // Write any unwritten changes to page device dictionary
  367. if (!pageDeviceDictionary.isEmpty()) {
  368. String content = pageDeviceDictionary.getContent();
  369. if (psUtil.isSafeSetPageDevice()) {
  370. content += " SSPD";
  371. } else {
  372. content += " setpagedevice";
  373. }
  374. PSRenderingUtil.writeEnclosedExtensionAttachment(gen, new PSSetPageDevice(content));
  375. }
  376. if (rotate) {
  377. gen.writeln(Math.round(pageHeight) + " 0 translate");
  378. gen.writeln("90 rotate");
  379. }
  380. gen.concatMatrix(1, 0, 0, -1, 0, pageHeight);
  381. gen.writeDSCComment(DSCConstants.END_PAGE_SETUP);
  382. } catch (IOException ioe) {
  383. throw new IFException("I/O error in startPage()", ioe);
  384. }
  385. }
  386. /** {@inheritDoc} */
  387. public IFPainter startPageContent() throws IFException {
  388. return new PSPainter(this);
  389. }
  390. /** {@inheritDoc} */
  391. public void endPageContent() throws IFException {
  392. try {
  393. //Show page
  394. gen.writeln("showpage");
  395. } catch (IOException ioe) {
  396. throw new IFException("I/O error in endPageContent()", ioe);
  397. }
  398. }
  399. /** {@inheritDoc} */
  400. public void endPage() throws IFException {
  401. try {
  402. gen.writeDSCComment(DSCConstants.PAGE_TRAILER);
  403. //TODO Handle extension attachments for the page!!!!!!!
  404. /*
  405. if (page.hasExtensionAttachments()) {
  406. List extensionAttachments = page.getExtensionAttachments();
  407. for (int i = 0; i < extensionAttachments.size(); i++) {
  408. Object attObj = extensionAttachments.get(i);
  409. if (attObj instanceof PSExtensionAttachment) {
  410. PSExtensionAttachment attachment = (PSExtensionAttachment)attObj;
  411. if (attachment instanceof PSCommentAfter) {
  412. gen.commentln("%" + attachment.getContent());
  413. }
  414. }
  415. }
  416. }*/
  417. gen.getResourceTracker().writeResources(true, gen);
  418. } catch (IOException ioe) {
  419. throw new IFException("I/O error in endPage()", ioe);
  420. }
  421. }
  422. /** {@inheritDoc} */
  423. public void handleExtensionObject(Object extension) throws IFException {
  424. log.debug("Don't know how to handle extension object. Ignoring: "
  425. + extension + " (" + extension.getClass().getName() + ")");
  426. }
  427. private String getPostScriptNameForFontKey(String key) {
  428. int pos = key.indexOf('_');
  429. String postFix = null;
  430. if (pos > 0) {
  431. postFix = key.substring(pos);
  432. key = key.substring(0, pos);
  433. }
  434. Map fonts = fontInfo.getFonts();
  435. Typeface tf = (Typeface)fonts.get(key);
  436. if (tf instanceof LazyFont) {
  437. tf = ((LazyFont)tf).getRealFont();
  438. }
  439. if (tf == null) {
  440. throw new IllegalStateException("Font not available: " + key);
  441. }
  442. if (postFix == null) {
  443. return tf.getFontName();
  444. } else {
  445. return tf.getFontName() + postFix;
  446. }
  447. }
  448. /**
  449. * Returns the PSResource for the given font key.
  450. * @param key the font key ("F*")
  451. * @return the matching PSResource
  452. */
  453. protected PSResource getPSResourceForFontKey(String key) {
  454. PSResource res = null;
  455. if (this.fontResources != null) {
  456. res = (PSResource)this.fontResources.get(key);
  457. } else {
  458. this.fontResources = new java.util.HashMap();
  459. }
  460. if (res == null) {
  461. res = new PSResource(PSResource.TYPE_FONT, getPostScriptNameForFontKey(key));
  462. this.fontResources.put(key, res);
  463. }
  464. return res;
  465. }
  466. /**
  467. * Returns a PSResource instance representing a image as a PostScript form.
  468. * @param uri the image URI
  469. * @return a PSResource instance
  470. */
  471. protected PSResource getFormForImage(String uri) {
  472. if (uri == null || "".equals(uri)) {
  473. throw new IllegalArgumentException("uri must not be empty or null");
  474. }
  475. if (this.formResources == null) {
  476. this.formResources = new java.util.HashMap();
  477. }
  478. PSResource form = (PSResource)this.formResources.get(uri);
  479. if (form == null) {
  480. form = new PSImageFormResource(this.formResources.size() + 1, uri);
  481. this.formResources.put(uri, form);
  482. }
  483. return form;
  484. }
  485. }