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.

PSDocumentHandler.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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.Dimension2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.BufferedInputStream;
  23. import java.io.BufferedOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.net.URI;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import javax.xml.transform.Source;
  34. import org.apache.commons.io.IOUtils;
  35. import org.apache.commons.logging.Log;
  36. import org.apache.commons.logging.LogFactory;
  37. import org.apache.xmlgraphics.io.TempResourceURIGenerator;
  38. import org.apache.xmlgraphics.java2d.Dimension2DDouble;
  39. import org.apache.xmlgraphics.ps.DSCConstants;
  40. import org.apache.xmlgraphics.ps.PSDictionary;
  41. import org.apache.xmlgraphics.ps.PSDictionaryFormatException;
  42. import org.apache.xmlgraphics.ps.PSGenerator;
  43. import org.apache.xmlgraphics.ps.PSPageDeviceDictionary;
  44. import org.apache.xmlgraphics.ps.PSProcSets;
  45. import org.apache.xmlgraphics.ps.PSResource;
  46. import org.apache.xmlgraphics.ps.dsc.DSCException;
  47. import org.apache.xmlgraphics.ps.dsc.ResourceTracker;
  48. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox;
  49. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox;
  50. import org.apache.fop.apps.MimeConstants;
  51. import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
  52. import org.apache.fop.render.intermediate.IFContext;
  53. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  54. import org.apache.fop.render.intermediate.IFException;
  55. import org.apache.fop.render.intermediate.IFPainter;
  56. import org.apache.fop.render.ps.PSRendererConfig.PSRendererConfigParser;
  57. import org.apache.fop.render.ps.extensions.PSCommentAfter;
  58. import org.apache.fop.render.ps.extensions.PSCommentBefore;
  59. import org.apache.fop.render.ps.extensions.PSPageTrailerCodeBefore;
  60. import org.apache.fop.render.ps.extensions.PSSetPageDevice;
  61. import org.apache.fop.render.ps.extensions.PSSetupCode;
  62. /**
  63. * {@link org.apache.fop.render.intermediate.IFDocumentHandler} implementation
  64. * that produces PostScript.
  65. */
  66. public class PSDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
  67. /** logging instance */
  68. private static Log log = LogFactory.getLog(PSDocumentHandler.class);
  69. /**
  70. * Utility class which enables all sorts of features that are not directly connected to the
  71. * normal rendering process.
  72. */
  73. private PSRenderingUtil psUtil;
  74. /** The PostScript generator used to output the PostScript */
  75. PSGenerator gen;
  76. /** the temporary file in case of two-pass processing */
  77. private URI tempURI;
  78. private static final TempResourceURIGenerator TEMP_URI_GENERATOR
  79. = new TempResourceURIGenerator("ps-optimize");
  80. private int currentPageNumber;
  81. private PageDefinition currentPageDefinition;
  82. /** Is used to determine the document's bounding box */
  83. private Rectangle2D documentBoundingBox;
  84. /** Used to temporarily store PSSetupCode instance until they can be written. */
  85. private List setupCodeList;
  86. /** This is a cache of PSResource instances of all fonts defined */
  87. private FontResourceCache fontResources;
  88. /** This is a map of PSResource instances of all forms (key: uri) */
  89. private Map formResources;
  90. /** encapsulation of dictionary used in setpagedevice instruction **/
  91. private PSPageDeviceDictionary pageDeviceDictionary;
  92. /** This is a collection holding all document header comments */
  93. private Collection[] comments = new Collection[4];
  94. private static final int COMMENT_DOCUMENT_HEADER = 0;
  95. private static final int COMMENT_DOCUMENT_TRAILER = 1;
  96. private static final int COMMENT_PAGE_TRAILER = 2;
  97. private static final int PAGE_TRAILER_CODE_BEFORE = 3;
  98. private PSEventProducer eventProducer;
  99. /**
  100. * Default constructor.
  101. */
  102. public PSDocumentHandler(IFContext context) {
  103. super(context);
  104. this.psUtil = new PSRenderingUtil(context.getUserAgent());
  105. }
  106. /** {@inheritDoc} */
  107. public boolean supportsPagesOutOfOrder() {
  108. return false;
  109. }
  110. /** {@inheritDoc} */
  111. public String getMimeType() {
  112. return MimeConstants.MIME_POSTSCRIPT;
  113. }
  114. PSGenerator getGenerator() {
  115. return gen;
  116. }
  117. /** {@inheritDoc} */
  118. public IFDocumentHandlerConfigurator getConfigurator() {
  119. return new PSRendererConfigurator(getUserAgent(), new PSRendererConfigParser());
  120. }
  121. public PSRenderingUtil getPSUtil() {
  122. return this.psUtil;
  123. }
  124. /** {@inheritDoc} */
  125. public void startDocument() throws IFException {
  126. super.startDocument();
  127. this.fontResources = new FontResourceCache(getFontInfo());
  128. try {
  129. final OutputStream out;
  130. if (psUtil.isOptimizeResources()) {
  131. tempURI = TEMP_URI_GENERATOR.generate();
  132. out = new BufferedOutputStream(getUserAgent().getResourceResolver().getOutputStream(tempURI));
  133. } else {
  134. out = this.outputStream;
  135. }
  136. //Setup for PostScript generation
  137. this.gen = new FOPPSGeneratorImpl(out);
  138. this.gen.setPSLevel(psUtil.getLanguageLevel());
  139. this.gen.setAcrobatDownsample(psUtil.isAcrobatDownsample());
  140. this.currentPageNumber = 0;
  141. this.documentBoundingBox = new Rectangle2D.Double();
  142. //Initial default page device dictionary settings
  143. this.pageDeviceDictionary = new PSPageDeviceDictionary();
  144. pageDeviceDictionary.setFlushOnRetrieval(!psUtil.isDSCComplianceEnabled());
  145. pageDeviceDictionary.put("/ImagingBBox", "null");
  146. } catch (IOException e) {
  147. throw new IFException("I/O error in startDocument()", e);
  148. }
  149. }
  150. public interface FOPPSGenerator {
  151. PSDocumentHandler getHandler();
  152. BufferedOutputStream getTempStream(URI uri) throws IOException;
  153. Map<Integer, URI> getImages();
  154. }
  155. public class FOPPSGeneratorImpl extends PSGenerator implements FOPPSGenerator {
  156. private Map<Integer, URI> images = new HashMap<Integer, URI>();
  157. public FOPPSGeneratorImpl(OutputStream out) {
  158. super(out);
  159. }
  160. /** Need to subclass PSGenerator to have better URI resolution */
  161. @Override
  162. public Source resolveURI(String uri) {
  163. return getUserAgent().resolveURI(uri);
  164. }
  165. public PSDocumentHandler getHandler() {
  166. return PSDocumentHandler.this;
  167. }
  168. public BufferedOutputStream getTempStream(URI uri) throws IOException {
  169. return new BufferedOutputStream(getUserAgent().getResourceResolver().getOutputStream(uri));
  170. }
  171. public Map<Integer, URI> getImages() {
  172. return images;
  173. }
  174. }
  175. private void writeHeader() throws IOException {
  176. //PostScript Header
  177. gen.writeln(DSCConstants.PS_ADOBE_30);
  178. gen.writeDSCComment(DSCConstants.CREATOR, new String[] {getUserAgent().getProducer()});
  179. gen.writeDSCComment(DSCConstants.CREATION_DATE, new Object[] {new java.util.Date()});
  180. gen.writeDSCComment(DSCConstants.LANGUAGE_LEVEL, gen.getPSLevel());
  181. gen.writeDSCComment(DSCConstants.PAGES, new Object[] {DSCConstants.ATEND});
  182. gen.writeDSCComment(DSCConstants.BBOX, DSCConstants.ATEND);
  183. gen.writeDSCComment(DSCConstants.HIRES_BBOX, DSCConstants.ATEND);
  184. gen.writeDSCComment(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES,
  185. new Object[] {DSCConstants.ATEND});
  186. writeExtensions(COMMENT_DOCUMENT_HEADER);
  187. gen.writeDSCComment(DSCConstants.END_COMMENTS);
  188. //Defaults
  189. gen.writeDSCComment(DSCConstants.BEGIN_DEFAULTS);
  190. gen.writeDSCComment(DSCConstants.END_DEFAULTS);
  191. //Prolog and Setup written right before the first page-sequence, see startPageSequence()
  192. //Do this only once, as soon as we have all the content for the Setup section!
  193. //Prolog
  194. gen.writeDSCComment(DSCConstants.BEGIN_PROLOG);
  195. PSProcSets.writeStdProcSet(gen);
  196. PSProcSets.writeEPSProcSet(gen);
  197. FOPProcSet.INSTANCE.writeTo(gen);
  198. gen.writeDSCComment(DSCConstants.END_PROLOG);
  199. //Setup
  200. gen.writeDSCComment(DSCConstants.BEGIN_SETUP);
  201. PSRenderingUtil.writeSetupCodeList(gen, setupCodeList, "SetupCode");
  202. if (!psUtil.isOptimizeResources()) {
  203. this.fontResources.addAll(PSFontUtils.writeFontDict(gen, fontInfo, eventProducer));
  204. } else {
  205. gen.commentln("%FOPFontSetup"); //Place-holder, will be replaced in the second pass
  206. }
  207. gen.writeDSCComment(DSCConstants.END_SETUP);
  208. }
  209. /** {@inheritDoc} */
  210. public void endDocumentHeader() throws IFException {
  211. try {
  212. writeHeader();
  213. } catch (IOException ioe) {
  214. throw new IFException("I/O error writing the PostScript header", ioe);
  215. }
  216. }
  217. /** {@inheritDoc} */
  218. public void endDocument() throws IFException {
  219. try {
  220. //Write trailer
  221. gen.writeDSCComment(DSCConstants.TRAILER);
  222. writeExtensions(COMMENT_DOCUMENT_TRAILER);
  223. gen.writeDSCComment(DSCConstants.PAGES, this.currentPageNumber);
  224. new DSCCommentBoundingBox(this.documentBoundingBox).generate(gen);
  225. new DSCCommentHiResBoundingBox(this.documentBoundingBox).generate(gen);
  226. gen.getResourceTracker().writeResources(false, gen);
  227. gen.writeDSCComment(DSCConstants.EOF);
  228. gen.flush();
  229. log.debug("Rendering to PostScript complete.");
  230. if (psUtil.isOptimizeResources()) {
  231. IOUtils.closeQuietly(gen.getOutputStream());
  232. rewritePostScriptFile();
  233. }
  234. if (pageDeviceDictionary != null) {
  235. pageDeviceDictionary.clear();
  236. }
  237. } catch (IOException ioe) {
  238. throw new IFException("I/O error in endDocument()", ioe);
  239. }
  240. super.endDocument();
  241. }
  242. /**
  243. * Used for two-pass production. This will rewrite the PostScript file from the temporary
  244. * file while adding all needed resources.
  245. * @throws IOException In case of an I/O error.
  246. */
  247. private void rewritePostScriptFile() throws IOException {
  248. log.debug("Processing PostScript resources...");
  249. long startTime = System.currentTimeMillis();
  250. ResourceTracker resTracker = gen.getResourceTracker();
  251. InputStream in = new BufferedInputStream(getUserAgent().getResourceResolver().getResource(tempURI));
  252. try {
  253. try {
  254. ResourceHandler handler = new ResourceHandler(getUserAgent(), eventProducer,
  255. this.fontInfo, resTracker, this.formResources);
  256. handler.process(in, this.outputStream,
  257. this.currentPageNumber, this.documentBoundingBox, psUtil);
  258. this.outputStream.flush();
  259. } catch (DSCException e) {
  260. throw new RuntimeException(e.getMessage());
  261. }
  262. } finally {
  263. IOUtils.closeQuietly(in);
  264. }
  265. if (log.isDebugEnabled()) {
  266. long duration = System.currentTimeMillis() - startTime;
  267. log.debug("Resource Processing complete in " + duration + " ms.");
  268. }
  269. }
  270. /** {@inheritDoc} */
  271. public void startPageSequence(String id) throws IFException {
  272. //nop
  273. }
  274. /** {@inheritDoc} */
  275. public void endPageSequence() throws IFException {
  276. //nop
  277. }
  278. /** {@inheritDoc} */
  279. public void startPage(int index, String name, String pageMasterName, Dimension size)
  280. throws IFException {
  281. try {
  282. /* if (this.currentPageNumber == 0) {
  283. //writeHeader();
  284. } */
  285. this.currentPageNumber++;
  286. gen.getResourceTracker().notifyStartNewPage();
  287. gen.getResourceTracker().notifyResourceUsageOnPage(PSProcSets.STD_PROCSET);
  288. gen.writeDSCComment(DSCConstants.PAGE, new Object[]
  289. {name,
  290. this.currentPageNumber});
  291. double pageWidth = size.width / 1000.0;
  292. double pageHeight = size.height / 1000.0;
  293. boolean rotate = false;
  294. List pageSizes = new java.util.ArrayList();
  295. if (this.psUtil.isAutoRotateLandscape() && (pageHeight < pageWidth)) {
  296. rotate = true;
  297. pageSizes.add(Math.round(pageHeight));
  298. pageSizes.add(Math.round(pageWidth));
  299. } else {
  300. pageSizes.add(Math.round(pageWidth));
  301. pageSizes.add(Math.round(pageHeight));
  302. }
  303. pageDeviceDictionary.put("/PageSize", pageSizes);
  304. this.currentPageDefinition = new PageDefinition(
  305. new Dimension2DDouble(pageWidth, pageHeight), rotate);
  306. //TODO Handle extension attachments for the page!!!!!!!
  307. /*
  308. if (page.hasExtensionAttachments()) {
  309. for (Iterator iter = page.getExtensionAttachments().iterator();
  310. iter.hasNext();) {
  311. ExtensionAttachment attachment = (ExtensionAttachment) iter.next();
  312. if (attachment instanceof PSSetPageDevice) {*/
  313. /**
  314. * Extract all PSSetPageDevice instances from the
  315. * attachment list on the s-p-m and add all
  316. * dictionary entries to our internal representation
  317. * of the the page device dictionary.
  318. *//*
  319. PSSetPageDevice setPageDevice = (PSSetPageDevice)attachment;
  320. String content = setPageDevice.getContent();
  321. if (content != null) {
  322. try {
  323. pageDeviceDictionary.putAll(PSDictionary.valueOf(content));
  324. } catch (PSDictionaryFormatException e) {
  325. PSEventProducer eventProducer = PSEventProducer.Provider.get(
  326. getUserAgent().getEventBroadcaster());
  327. eventProducer.postscriptDictionaryParseError(this, content, e);
  328. }
  329. }
  330. }
  331. }
  332. }*/
  333. final Integer zero = 0;
  334. Rectangle2D pageBoundingBox = new Rectangle2D.Double();
  335. if (rotate) {
  336. pageBoundingBox.setRect(0, 0, pageHeight, pageWidth);
  337. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[] {
  338. zero, zero, Math.round(pageHeight),
  339. Math.round(pageWidth)});
  340. gen.writeDSCComment(DSCConstants.PAGE_HIRES_BBOX, new Object[] {
  341. zero, zero, pageHeight,
  342. pageWidth});
  343. gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
  344. } else {
  345. pageBoundingBox.setRect(0, 0, pageWidth, pageHeight);
  346. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[] {
  347. zero, zero, Math.round(pageWidth),
  348. Math.round(pageHeight)});
  349. gen.writeDSCComment(DSCConstants.PAGE_HIRES_BBOX, new Object[] {
  350. zero, zero, pageWidth,
  351. pageHeight});
  352. if (psUtil.isAutoRotateLandscape()) {
  353. gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION,
  354. "Portrait");
  355. }
  356. }
  357. this.documentBoundingBox.add(pageBoundingBox);
  358. gen.writeDSCComment(DSCConstants.PAGE_RESOURCES,
  359. new Object[] {DSCConstants.ATEND});
  360. gen.commentln("%FOPSimplePageMaster: " + pageMasterName);
  361. } catch (IOException ioe) {
  362. throw new IFException("I/O error in startPage()", ioe);
  363. }
  364. }
  365. /** {@inheritDoc} */
  366. public void startPageHeader() throws IFException {
  367. super.startPageHeader();
  368. try {
  369. gen.writeDSCComment(DSCConstants.BEGIN_PAGE_SETUP);
  370. } catch (IOException ioe) {
  371. throw new IFException("I/O error in startPageHeader()", ioe);
  372. }
  373. }
  374. /** {@inheritDoc} */
  375. public void endPageHeader() throws IFException {
  376. try {
  377. // Write any unwritten changes to page device dictionary
  378. if (!pageDeviceDictionary.isEmpty()) {
  379. String content = pageDeviceDictionary.getContent();
  380. if (psUtil.isSafeSetPageDevice()) {
  381. content += " SSPD";
  382. } else {
  383. content += " setpagedevice";
  384. }
  385. PSRenderingUtil.writeEnclosedExtensionAttachment(gen, new PSSetPageDevice(content));
  386. }
  387. double pageHeight = this.currentPageDefinition.dimensions.getHeight();
  388. if (this.currentPageDefinition.rotate) {
  389. gen.writeln(gen.formatDouble(pageHeight) + " 0 translate");
  390. gen.writeln("90 rotate");
  391. }
  392. gen.concatMatrix(1, 0, 0, -1, 0, pageHeight);
  393. gen.writeDSCComment(DSCConstants.END_PAGE_SETUP);
  394. } catch (IOException ioe) {
  395. throw new IFException("I/O error in endPageHeader()", ioe);
  396. }
  397. super.endPageHeader();
  398. }
  399. private void writeExtensions(int which) throws IOException {
  400. Collection extensions = comments[which];
  401. if (extensions != null) {
  402. PSRenderingUtil.writeEnclosedExtensionAttachments(gen, extensions);
  403. extensions.clear();
  404. }
  405. }
  406. /** {@inheritDoc} */
  407. public IFPainter startPageContent() throws IFException {
  408. return new PSPainter(this);
  409. }
  410. /** {@inheritDoc} */
  411. public void endPageContent() throws IFException {
  412. try {
  413. gen.showPage();
  414. } catch (IOException ioe) {
  415. throw new IFException("I/O error in endPageContent()", ioe);
  416. }
  417. }
  418. /** {@inheritDoc} */
  419. public void startPageTrailer() throws IFException {
  420. try {
  421. writeExtensions(PAGE_TRAILER_CODE_BEFORE);
  422. super.startPageTrailer();
  423. gen.writeDSCComment(DSCConstants.PAGE_TRAILER);
  424. } catch (IOException ioe) {
  425. throw new IFException("I/O error in startPageTrailer()", ioe);
  426. }
  427. }
  428. /** {@inheritDoc} */
  429. public void endPageTrailer() throws IFException {
  430. try {
  431. writeExtensions(COMMENT_PAGE_TRAILER);
  432. } catch (IOException ioe) {
  433. throw new IFException("I/O error in endPageTrailer()", ioe);
  434. }
  435. super.endPageTrailer();
  436. }
  437. /** {@inheritDoc} */
  438. public void endPage() throws IFException {
  439. try {
  440. gen.getResourceTracker().writeResources(true, gen);
  441. } catch (IOException ioe) {
  442. throw new IFException("I/O error in endPage()", ioe);
  443. }
  444. this.currentPageDefinition = null;
  445. }
  446. private boolean inPage() {
  447. return this.currentPageDefinition != null;
  448. }
  449. /** {@inheritDoc} */
  450. public void handleExtensionObject(Object extension) throws IFException {
  451. try {
  452. if (extension instanceof PSSetupCode) {
  453. if (inPage()) {
  454. PSRenderingUtil.writeEnclosedExtensionAttachment(gen, (PSSetupCode)extension);
  455. } else {
  456. //A special collection for setup code as it's put in a different place
  457. //than the "before comments".
  458. if (setupCodeList == null) {
  459. setupCodeList = new java.util.ArrayList();
  460. }
  461. if (!setupCodeList.contains(extension)) {
  462. setupCodeList.add(extension);
  463. }
  464. }
  465. } else if (extension instanceof PSSetPageDevice) {
  466. /**
  467. * Extract all PSSetPageDevice instances from the
  468. * attachment list on the s-p-m and add all dictionary
  469. * entries to our internal representation of the the
  470. * page device dictionary.
  471. */
  472. PSSetPageDevice setPageDevice = (PSSetPageDevice)extension;
  473. String content = setPageDevice.getContent();
  474. if (content != null) {
  475. try {
  476. this.pageDeviceDictionary.putAll(PSDictionary.valueOf(content));
  477. } catch (PSDictionaryFormatException e) {
  478. PSEventProducer eventProducer = PSEventProducer.Provider.get(
  479. getUserAgent().getEventBroadcaster());
  480. eventProducer.postscriptDictionaryParseError(this, content, e);
  481. }
  482. }
  483. } else if (extension instanceof PSCommentBefore) {
  484. if (inPage()) {
  485. PSRenderingUtil.writeEnclosedExtensionAttachment(
  486. gen, (PSCommentBefore)extension);
  487. } else {
  488. if (comments[COMMENT_DOCUMENT_HEADER] == null) {
  489. comments[COMMENT_DOCUMENT_HEADER] = new java.util.ArrayList();
  490. }
  491. comments[COMMENT_DOCUMENT_HEADER].add(extension);
  492. }
  493. } else if (extension instanceof PSCommentAfter) {
  494. int targetCollection = (inPage() ? COMMENT_PAGE_TRAILER : COMMENT_DOCUMENT_TRAILER);
  495. if (comments[targetCollection] == null) {
  496. comments[targetCollection] = new java.util.ArrayList();
  497. }
  498. comments[targetCollection].add(extension);
  499. } else if (extension instanceof PSPageTrailerCodeBefore) {
  500. if (comments[PAGE_TRAILER_CODE_BEFORE] == null) {
  501. comments[PAGE_TRAILER_CODE_BEFORE] = new ArrayList();
  502. }
  503. comments[PAGE_TRAILER_CODE_BEFORE].add(extension);
  504. }
  505. } catch (IOException ioe) {
  506. throw new IFException("I/O error in handleExtensionObject()", ioe);
  507. }
  508. }
  509. /**
  510. * Returns the PSResource for the given font key.
  511. * @param key the font key ("F*")
  512. * @return the matching PSResource
  513. */
  514. protected PSFontResource getPSResourceForFontKey(String key) {
  515. return this.fontResources.getFontResourceForFontKey(key);
  516. }
  517. /**
  518. * Returns a PSResource instance representing a image as a PostScript form.
  519. * @param uri the image URI
  520. * @return a PSResource instance
  521. */
  522. public PSResource getFormForImage(String uri) {
  523. if (uri == null || "".equals(uri)) {
  524. throw new IllegalArgumentException("uri must not be empty or null");
  525. }
  526. if (this.formResources == null) {
  527. this.formResources = new java.util.HashMap();
  528. }
  529. PSResource form = (PSResource)this.formResources.get(uri);
  530. if (form == null) {
  531. form = new PSImageFormResource(this.formResources.size() + 1, uri);
  532. this.formResources.put(uri, form);
  533. }
  534. return form;
  535. }
  536. private static final class PageDefinition {
  537. private Dimension2D dimensions;
  538. private boolean rotate;
  539. private PageDefinition(Dimension2D dimensions, boolean rotate) {
  540. this.dimensions = dimensions;
  541. this.rotate = rotate;
  542. }
  543. }
  544. }