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

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