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 23KB

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