選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PSRenderer.java 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. // Java
  20. import java.awt.Color;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.RenderedImage;
  23. import java.io.File;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.LineNumberReader;
  28. import java.io.OutputStream;
  29. import java.util.Collection;
  30. import java.util.Iterator;
  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.image.loader.ImageException;
  38. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  39. import org.apache.xmlgraphics.image.loader.ImageInfo;
  40. import org.apache.xmlgraphics.image.loader.ImageManager;
  41. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  42. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  43. import org.apache.xmlgraphics.image.loader.impl.ImageRawCCITTFax;
  44. import org.apache.xmlgraphics.image.loader.impl.ImageRawEPS;
  45. import org.apache.xmlgraphics.image.loader.impl.ImageRawJPEG;
  46. import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
  47. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  48. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  49. import org.apache.xmlgraphics.image.loader.pipeline.ImageProviderPipeline;
  50. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  51. import org.apache.xmlgraphics.ps.DSCConstants;
  52. import org.apache.xmlgraphics.ps.ImageEncoder;
  53. import org.apache.xmlgraphics.ps.PSGenerator;
  54. import org.apache.xmlgraphics.ps.PSImageUtils;
  55. import org.apache.xmlgraphics.ps.PSProcSets;
  56. import org.apache.xmlgraphics.ps.PSResource;
  57. import org.apache.xmlgraphics.ps.PSState;
  58. import org.apache.xmlgraphics.ps.dsc.DSCException;
  59. import org.apache.xmlgraphics.ps.dsc.ResourceTracker;
  60. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox;
  61. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox;
  62. import org.apache.fop.apps.FOPException;
  63. import org.apache.fop.apps.FOUserAgent;
  64. import org.apache.fop.area.Area;
  65. import org.apache.fop.area.BlockViewport;
  66. import org.apache.fop.area.CTM;
  67. import org.apache.fop.area.LineArea;
  68. import org.apache.fop.area.OffDocumentExtensionAttachment;
  69. import org.apache.fop.area.OffDocumentItem;
  70. import org.apache.fop.area.PageViewport;
  71. import org.apache.fop.area.RegionViewport;
  72. import org.apache.fop.area.Trait;
  73. import org.apache.fop.area.inline.AbstractTextArea;
  74. import org.apache.fop.area.inline.Image;
  75. import org.apache.fop.area.inline.InlineParent;
  76. import org.apache.fop.area.inline.Leader;
  77. import org.apache.fop.area.inline.SpaceArea;
  78. import org.apache.fop.area.inline.TextArea;
  79. import org.apache.fop.area.inline.WordArea;
  80. import org.apache.fop.datatypes.URISpecification;
  81. import org.apache.fop.fo.Constants;
  82. import org.apache.fop.fo.extensions.ExtensionAttachment;
  83. import org.apache.fop.fonts.Font;
  84. import org.apache.fop.fonts.LazyFont;
  85. import org.apache.fop.fonts.Typeface;
  86. import org.apache.fop.render.AbstractPathOrientedRenderer;
  87. import org.apache.fop.render.Graphics2DAdapter;
  88. import org.apache.fop.render.ImageAdapter;
  89. import org.apache.fop.render.RendererContext;
  90. import org.apache.fop.render.ps.extensions.PSCommentAfter;
  91. import org.apache.fop.render.ps.extensions.PSCommentBefore;
  92. import org.apache.fop.render.ps.extensions.PSExtensionAttachment;
  93. import org.apache.fop.render.ps.extensions.PSSetPageDevice;
  94. import org.apache.fop.render.ps.extensions.PSSetupCode;
  95. import org.apache.fop.util.CharUtilities;
  96. /**
  97. * Renderer that renders to PostScript.
  98. * <br>
  99. * This class currently generates PostScript Level 2 code. The only exception
  100. * is the FlateEncode filter which is a Level 3 feature. The filters in use
  101. * are hardcoded at the moment.
  102. * <br>
  103. * This class follows the Document Structuring Conventions (DSC) version 3.0.
  104. * If anyone modifies this renderer please make
  105. * sure to also follow the DSC to make it simpler to programmatically modify
  106. * the generated Postscript files (ex. extract pages etc.).
  107. * <br>
  108. * This renderer inserts FOP-specific comments into the PostScript stream which
  109. * may help certain users to do certain types of post-processing of the output.
  110. * These comments all start with "%FOP".
  111. *
  112. * @author <a href="mailto:fop-dev@xmlgraphics.apache.org">Apache FOP Development Team</a>
  113. * @version $Id$
  114. */
  115. public class PSRenderer extends AbstractPathOrientedRenderer
  116. implements ImageAdapter, PSSupportedFlavors {
  117. /** logging instance */
  118. private static Log log = LogFactory.getLog(PSRenderer.class);
  119. /** The MIME type for PostScript */
  120. public static final String MIME_TYPE = "application/postscript";
  121. private static final String AUTO_ROTATE_LANDSCAPE = "auto-rotate-landscape";
  122. private static final String OPTIMIZE_RESOURCES = "optimize-resources";
  123. private static final String LANGUAGE_LEVEL = "language-level";
  124. /** The application producing the PostScript */
  125. private int currentPageNumber = 0;
  126. private boolean enableComments = true;
  127. private boolean autoRotateLandscape = false;
  128. private int languageLevel = PSGenerator.DEFAULT_LANGUAGE_LEVEL;
  129. /** the OutputStream the PS file is written to */
  130. private OutputStream outputStream;
  131. /** the temporary file in case of two-pass processing */
  132. private File tempFile;
  133. /** The PostScript generator used to output the PostScript */
  134. protected PSGenerator gen;
  135. /** Determines whether the PS file is generated in two passes to minimize file size */
  136. private boolean twoPassGeneration = false;
  137. private boolean ioTrouble = false;
  138. private boolean inTextMode = false;
  139. /** Used to temporarily store PSSetupCode instance until they can be written. */
  140. private List setupCodeList;
  141. /** This is a map of PSResource instances of all fonts defined (key: font key) */
  142. private Map fontResources;
  143. /** This is a map of PSResource instances of all forms (key: uri) */
  144. private Map formResources;
  145. /** encapsulation of dictionary used in setpagedevice instruction **/
  146. private PSPageDeviceDictionary pageDeviceDictionary;
  147. /** Whether or not the safe set page device macro will be used or not */
  148. private boolean safeSetPageDevice = false;
  149. /** Whether or not Dublin Core Standard (dsc) compliant output is enforced */
  150. private boolean dscCompliant = true;
  151. /** Is used to determine the document's bounding box */
  152. private Rectangle2D documentBoundingBox;
  153. /** This is a collection holding all document header comments */
  154. private Collection headerComments;
  155. /** This is a collection holding all document footer comments */
  156. private Collection footerComments;
  157. /**
  158. * {@inheritDoc}
  159. */
  160. public void setUserAgent(FOUserAgent agent) {
  161. super.setUserAgent(agent);
  162. Object obj;
  163. obj = agent.getRendererOptions().get(AUTO_ROTATE_LANDSCAPE);
  164. if (obj != null) {
  165. setAutoRotateLandscape(booleanValueOf(obj));
  166. }
  167. obj = agent.getRendererOptions().get(LANGUAGE_LEVEL);
  168. if (obj != null) {
  169. setLanguageLevel(intValueOf(obj));
  170. }
  171. obj = agent.getRendererOptions().get(OPTIMIZE_RESOURCES);
  172. if (obj != null) {
  173. setOptimizeResources(booleanValueOf(obj));
  174. }
  175. }
  176. private boolean booleanValueOf(Object obj) {
  177. if (obj instanceof Boolean) {
  178. return ((Boolean)obj).booleanValue();
  179. } else if (obj instanceof String) {
  180. return Boolean.valueOf((String)obj).booleanValue();
  181. } else {
  182. throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected.");
  183. }
  184. }
  185. private int intValueOf(Object obj) {
  186. if (obj instanceof Integer) {
  187. return ((Integer)obj).intValue();
  188. } else if (obj instanceof String) {
  189. return Integer.parseInt((String)obj);
  190. } else {
  191. throw new IllegalArgumentException("Integer or String with a number expected.");
  192. }
  193. }
  194. /**
  195. * Sets the landscape mode for this renderer.
  196. * @param value false will normally generate a "pseudo-portrait" page, true will rotate
  197. * a "wider-than-long" page by 90 degrees.
  198. */
  199. public void setAutoRotateLandscape(boolean value) {
  200. this.autoRotateLandscape = value;
  201. }
  202. /** @return true if the renderer is configured to rotate landscape pages */
  203. public boolean isAutoRotateLandscape() {
  204. return this.autoRotateLandscape;
  205. }
  206. /**
  207. * Sets the PostScript language level that the renderer should produce.
  208. * @param level the language level (currently allowed: 2 or 3)
  209. */
  210. public void setLanguageLevel(int level) {
  211. if (level == 2 || level == 3) {
  212. this.languageLevel = level;
  213. } else {
  214. throw new IllegalArgumentException("Only language levels 2 or 3 are allowed/supported");
  215. }
  216. }
  217. /**
  218. * Return the PostScript language level that the renderer produces.
  219. * @return the language level
  220. */
  221. public int getLanguageLevel() {
  222. return this.languageLevel;
  223. }
  224. /**
  225. * Sets the resource optimization mode. If set to true, the renderer does two passes to
  226. * only embed the necessary resources in the PostScript file. This is slower, but produces
  227. * smaller files.
  228. * @param value true to enable the resource optimization
  229. */
  230. public void setOptimizeResources(boolean value) {
  231. this.twoPassGeneration = value;
  232. }
  233. /** @return true if the renderer does two passes to optimize PostScript resources */
  234. public boolean isOptimizeResources() {
  235. return this.twoPassGeneration;
  236. }
  237. /** {@inheritDoc} */
  238. public Graphics2DAdapter getGraphics2DAdapter() {
  239. return new PSGraphics2DAdapter(this);
  240. }
  241. /** {@inheritDoc} */
  242. public ImageAdapter getImageAdapter() {
  243. return this;
  244. }
  245. /**
  246. * Write out a command
  247. * @param cmd PostScript command
  248. */
  249. protected void writeln(String cmd) {
  250. try {
  251. gen.writeln(cmd);
  252. } catch (IOException ioe) {
  253. handleIOTrouble(ioe);
  254. }
  255. }
  256. /**
  257. * Central exception handler for I/O exceptions.
  258. * @param ioe IOException to handle
  259. */
  260. protected void handleIOTrouble(IOException ioe) {
  261. if (!ioTrouble) {
  262. log.error("Error while writing to target file", ioe);
  263. ioTrouble = true;
  264. }
  265. }
  266. /**
  267. * Write out a comment
  268. * @param comment Comment to write
  269. */
  270. protected void comment(String comment) {
  271. if (this.enableComments) {
  272. if (comment.startsWith("%")) {
  273. writeln(comment);
  274. } else {
  275. writeln("%" + comment);
  276. }
  277. }
  278. }
  279. /**
  280. * Make sure the cursor is in the right place.
  281. */
  282. protected void movetoCurrPosition() {
  283. moveTo(this.currentIPPosition, this.currentBPPosition);
  284. }
  285. /** {@inheritDoc} */
  286. protected void clip() {
  287. writeln("clip newpath");
  288. }
  289. /**
  290. * Clip an area.
  291. * Write a clipping operation given coordinates in the current
  292. * transform.
  293. * @param x the x coordinate
  294. * @param y the y coordinate
  295. * @param width the width of the area
  296. * @param height the height of the area
  297. */
  298. protected void clipRect(float x, float y, float width, float height) {
  299. try {
  300. gen.defineRect(x, y, width, height);
  301. clip();
  302. } catch (IOException ioe) {
  303. handleIOTrouble(ioe);
  304. }
  305. }
  306. /** {@inheritDoc} */
  307. protected void moveTo(float x, float y) {
  308. writeln(gen.formatDouble(x) + " " + gen.formatDouble(y) + " M");
  309. }
  310. /**
  311. * Moves the current point by (x, y) relative to the current position,
  312. * omitting any connecting line segment.
  313. * @param x x coordinate
  314. * @param y y coordinate
  315. */
  316. protected void rmoveTo(float x, float y) {
  317. writeln(gen.formatDouble(x) + " " + gen.formatDouble(y) + " RM");
  318. }
  319. /** {@inheritDoc} */
  320. protected void lineTo(float x, float y) {
  321. writeln(gen.formatDouble(x) + " " + gen.formatDouble(y) + " lineto");
  322. }
  323. /** {@inheritDoc} */
  324. protected void closePath() {
  325. writeln("cp");
  326. }
  327. /** {@inheritDoc} */
  328. protected void fillRect(float x, float y, float width, float height) {
  329. if (width != 0 && height != 0) {
  330. try {
  331. gen.defineRect(x, y, width, height);
  332. gen.writeln("fill");
  333. } catch (IOException ioe) {
  334. handleIOTrouble(ioe);
  335. }
  336. }
  337. }
  338. /** {@inheritDoc} */
  339. protected void updateColor(Color col, boolean fill) {
  340. try {
  341. useColor(col);
  342. } catch (IOException ioe) {
  343. handleIOTrouble(ioe);
  344. }
  345. }
  346. /**
  347. * Indicates whether an image should be inlined or added as a PostScript form.
  348. * @param uri the URI of the image
  349. * @return true if the image should be inlined rather than added as a form
  350. */
  351. protected boolean isImageInlined(String uri) {
  352. return !isOptimizeResources() || uri == null || "".equals(uri);
  353. }
  354. /**
  355. * Indicates whether an image should be inlined or added as a PostScript form.
  356. * @param info the ImageInfo object of the image
  357. * @return true if the image should be inlined rather than added as a form
  358. */
  359. protected boolean isImageInlined(ImageInfo info) {
  360. if (isImageInlined(info.getOriginalURI())) {
  361. return true;
  362. }
  363. if (!isOptimizeResources()) {
  364. throw new IllegalStateException("Must not get here if form support is enabled");
  365. }
  366. //Investigate choice for inline mode
  367. ImageFlavor[] inlineFlavors = getInlineFlavors();
  368. ImageManager manager = getUserAgent().getFactory().getImageManager();
  369. ImageProviderPipeline[] inlineCandidates
  370. = manager.getPipelineFactory().determineCandidatePipelines(
  371. info, inlineFlavors);
  372. ImageProviderPipeline inlineChoice = manager.choosePipeline(inlineCandidates);
  373. ImageFlavor inlineFlavor = (inlineChoice != null ? inlineChoice.getTargetFlavor() : null);
  374. //Investigate choice for form mode
  375. ImageFlavor[] formFlavors = getFormFlavors();
  376. ImageProviderPipeline[] formCandidates
  377. = manager.getPipelineFactory().determineCandidatePipelines(
  378. info, formFlavors);
  379. ImageProviderPipeline formChoice = manager.choosePipeline(formCandidates);
  380. ImageFlavor formFlavor = (formChoice != null ? formChoice.getTargetFlavor() : null);
  381. //Inline if form is not supported or if a better choice is available with inline mode
  382. return formFlavor == null || !formFlavor.equals(inlineFlavor);
  383. }
  384. /** {@inheritDoc} */
  385. protected void drawImage(String uri, Rectangle2D pos, Map foreignAttributes) {
  386. endTextObject();
  387. int x = currentIPPosition + (int)Math.round(pos.getX());
  388. int y = currentBPPosition + (int)Math.round(pos.getY());
  389. uri = URISpecification.getURL(uri);
  390. if (log.isDebugEnabled()) {
  391. log.debug("Handling image: " + uri);
  392. }
  393. ImageManager manager = getUserAgent().getFactory().getImageManager();
  394. ImageInfo info = null;
  395. try {
  396. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  397. info = manager.getImageInfo(uri, sessionContext);
  398. int width = (int)pos.getWidth();
  399. int height = (int)pos.getHeight();
  400. //millipoints --> points for PostScript
  401. float ptx = x / 1000f;
  402. float pty = y / 1000f;
  403. float ptw = width / 1000f;
  404. float pth = height / 1000f;
  405. if (isImageInlined(info)) {
  406. if (log.isDebugEnabled()) {
  407. log.debug("Image " + info + " is inlined");
  408. }
  409. //Only now fully load/prepare the image
  410. Map hints = ImageUtil.getDefaultHints(sessionContext);
  411. org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
  412. info, getInlineFlavors(), hints, sessionContext);
  413. //...and embed as inline image
  414. if (img instanceof ImageGraphics2D) {
  415. ImageGraphics2D imageG2D = (ImageGraphics2D)img;
  416. RendererContext context = createRendererContext(
  417. x, y, width, height, foreignAttributes);
  418. getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
  419. context, x, y, width, height);
  420. } else if (img instanceof ImageRendered) {
  421. ImageRendered imgRend = (ImageRendered)img;
  422. RenderedImage ri = imgRend.getRenderedImage();
  423. PSImageUtils.renderBitmapImage(ri, ptx, pty, ptw, pth, gen);
  424. } else if (img instanceof ImageXMLDOM) {
  425. ImageXMLDOM imgXML = (ImageXMLDOM)img;
  426. renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
  427. pos, foreignAttributes);
  428. } else if (img instanceof ImageRawStream) {
  429. final ImageRawStream raw = (ImageRawStream)img;
  430. if (raw instanceof ImageRawEPS) {
  431. ImageRawEPS eps = (ImageRawEPS)raw;
  432. Rectangle2D bbox = eps.getBoundingBox();
  433. InputStream in = raw.createInputStream();
  434. try {
  435. PSImageUtils.renderEPS(in, uri,
  436. new Rectangle2D.Float(ptx, pty, ptw, pth),
  437. bbox,
  438. gen);
  439. } finally {
  440. IOUtils.closeQuietly(in);
  441. }
  442. } else if (raw instanceof ImageRawCCITTFax) {
  443. final ImageRawCCITTFax ccitt = (ImageRawCCITTFax)raw;
  444. ImageEncoder encoder = new ImageEncoderCCITTFax(ccitt);
  445. Rectangle2D targetRect = new Rectangle2D.Float(
  446. ptx, pty, ptw, pth);
  447. PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
  448. uri, targetRect,
  449. ccitt.getColorSpace(), 1, false, gen);
  450. } else if (raw instanceof ImageRawJPEG) {
  451. ImageRawJPEG jpeg = (ImageRawJPEG)raw;
  452. ImageEncoder encoder = new ImageEncoderJPEG(jpeg);
  453. Rectangle2D targetRect = new Rectangle2D.Float(
  454. ptx, pty, ptw, pth);
  455. PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
  456. uri, targetRect,
  457. jpeg.getColorSpace(), 8, jpeg.isInverted(), gen);
  458. } else {
  459. throw new UnsupportedOperationException("Unsupported raw image: " + info);
  460. }
  461. } else {
  462. throw new UnsupportedOperationException("Unsupported image type: " + img);
  463. }
  464. } else {
  465. if (log.isDebugEnabled()) {
  466. log.debug("Image " + info + " is embedded as a form later");
  467. }
  468. //Don't load image at this time, just put a form placeholder in the stream
  469. PSResource form = getFormForImage(uri);
  470. Rectangle2D targetRect = new Rectangle2D.Double(ptx, pty, ptw, pth);
  471. PSImageUtils.paintForm(form, info.getSize().getDimensionPt(), targetRect, gen);
  472. }
  473. } catch (ImageException ie) {
  474. log.error("Error while processing image: "
  475. + (info != null ? info.toString() : uri), ie);
  476. } catch (FileNotFoundException fe) {
  477. log.error(fe.getMessage());
  478. } catch (IOException ioe) {
  479. handleIOTrouble(ioe);
  480. }
  481. }
  482. private ImageFlavor[] getInlineFlavors() {
  483. ImageFlavor[] flavors;
  484. if (gen.getPSLevel() >= 3) {
  485. flavors = LEVEL_3_FLAVORS_INLINE;
  486. } else {
  487. flavors = LEVEL_2_FLAVORS_INLINE;
  488. }
  489. return flavors;
  490. }
  491. private ImageFlavor[] getFormFlavors() {
  492. ImageFlavor[] flavors;
  493. if (gen.getPSLevel() >= 3) {
  494. flavors = LEVEL_3_FLAVORS_FORM;
  495. } else {
  496. flavors = LEVEL_2_FLAVORS_FORM;
  497. }
  498. return flavors;
  499. }
  500. /**
  501. * Returns a PSResource instance representing a image as a PostScript form.
  502. * @param uri the image URI
  503. * @return a PSResource instance
  504. */
  505. protected PSResource getFormForImage(String uri) {
  506. if (uri == null || "".equals(uri)) {
  507. throw new IllegalArgumentException("uri must not be empty or null");
  508. }
  509. if (this.formResources == null) {
  510. this.formResources = new java.util.HashMap();
  511. }
  512. PSResource form = (PSResource)this.formResources.get(uri);
  513. if (form == null) {
  514. form = new PSImageFormResource(this.formResources.size() + 1, uri);
  515. this.formResources.put(uri, form);
  516. }
  517. return form;
  518. }
  519. /** {@inheritDoc} */
  520. public void paintImage(RenderedImage image, RendererContext context,
  521. int x, int y, int width, int height) throws IOException {
  522. float fx = (float)x / 1000f;
  523. x += currentIPPosition / 1000f;
  524. float fy = (float)y / 1000f;
  525. y += currentBPPosition / 1000f;
  526. float fw = (float)width / 1000f;
  527. float fh = (float)height / 1000f;
  528. PSImageUtils.renderBitmapImage(image, fx, fy, fw, fh, gen);
  529. }
  530. /**
  531. * Draw a line.
  532. *
  533. * @param startx the start x position
  534. * @param starty the start y position
  535. * @param endx the x end position
  536. * @param endy the y end position
  537. */
  538. private void drawLine(float startx, float starty, float endx, float endy) {
  539. writeln(gen.formatDouble(startx) + " "
  540. + gen.formatDouble(starty) + " M "
  541. + gen.formatDouble(endx) + " "
  542. + gen.formatDouble(endy) + " lineto stroke newpath");
  543. }
  544. /** Saves the graphics state of the rendering engine. */
  545. public void saveGraphicsState() {
  546. endTextObject();
  547. try {
  548. //delegate
  549. gen.saveGraphicsState();
  550. } catch (IOException ioe) {
  551. handleIOTrouble(ioe);
  552. }
  553. }
  554. /** Restores the last graphics state of the rendering engine. */
  555. public void restoreGraphicsState() {
  556. try {
  557. //delegate
  558. gen.restoreGraphicsState();
  559. } catch (IOException ioe) {
  560. handleIOTrouble(ioe);
  561. }
  562. }
  563. /**
  564. * Concats the transformation matrix.
  565. * @param a A part
  566. * @param b B part
  567. * @param c C part
  568. * @param d D part
  569. * @param e E part
  570. * @param f F part
  571. */
  572. protected void concatMatrix(double a, double b,
  573. double c, double d,
  574. double e, double f) {
  575. try {
  576. gen.concatMatrix(a, b, c, d, e, f);
  577. } catch (IOException ioe) {
  578. handleIOTrouble(ioe);
  579. }
  580. }
  581. /**
  582. * Concats the transformations matrix.
  583. * @param matrix Matrix to use
  584. */
  585. protected void concatMatrix(double[] matrix) {
  586. try {
  587. gen.concatMatrix(matrix);
  588. } catch (IOException ioe) {
  589. handleIOTrouble(ioe);
  590. }
  591. }
  592. private String getPostScriptNameForFontKey(String key) {
  593. Map fonts = fontInfo.getFonts();
  594. Typeface tf = (Typeface)fonts.get(key);
  595. if (tf instanceof LazyFont) {
  596. tf = ((LazyFont)tf).getRealFont();
  597. }
  598. if (tf == null) {
  599. throw new IllegalStateException("Font not available: " + key);
  600. }
  601. return tf.getFontName();
  602. }
  603. /**
  604. * Returns the PSResource for the given font key.
  605. * @param key the font key ("F*")
  606. * @return the matching PSResource
  607. */
  608. protected PSResource getPSResourceForFontKey(String key) {
  609. PSResource res = null;
  610. if (this.fontResources != null) {
  611. res = (PSResource)this.fontResources.get(key);
  612. } else {
  613. this.fontResources = new java.util.HashMap();
  614. }
  615. if (res == null) {
  616. res = new PSResource(PSResource.TYPE_FONT, getPostScriptNameForFontKey(key));
  617. this.fontResources.put(key, res);
  618. }
  619. return res;
  620. }
  621. /**
  622. * Changes the currently used font.
  623. * @param key key of the font ("F*")
  624. * @param size font size
  625. */
  626. protected void useFont(String key, int size) {
  627. try {
  628. PSResource res = getPSResourceForFontKey(key);
  629. //gen.useFont(key, size / 1000f);
  630. gen.useFont("/" + res.getName(), size / 1000f);
  631. gen.getResourceTracker().notifyResourceUsageOnPage(res);
  632. } catch (IOException ioe) {
  633. handleIOTrouble(ioe);
  634. }
  635. }
  636. private void useColor(Color col) throws IOException {
  637. gen.useColor(col);
  638. }
  639. /** {@inheritDoc}
  640. * Area, float, float, float, float) */
  641. protected void drawBackAndBorders(Area area, float startx, float starty,
  642. float width, float height) {
  643. if (area.hasTrait(Trait.BACKGROUND)
  644. || area.hasTrait(Trait.BORDER_BEFORE)
  645. || area.hasTrait(Trait.BORDER_AFTER)
  646. || area.hasTrait(Trait.BORDER_START)
  647. || area.hasTrait(Trait.BORDER_END)) {
  648. comment("%FOPBeginBackgroundAndBorder: "
  649. + startx + " " + starty + " " + width + " " + height);
  650. super.drawBackAndBorders(area, startx, starty, width, height);
  651. comment("%FOPEndBackgroundAndBorder");
  652. }
  653. }
  654. /** {@inheritDoc} */
  655. protected void drawBorderLine(float x1, float y1, float x2, float y2,
  656. boolean horz, boolean startOrBefore, int style, Color col) {
  657. try {
  658. float w = x2 - x1;
  659. float h = y2 - y1;
  660. if ((w < 0) || (h < 0)) {
  661. log.error("Negative extent received. Border won't be painted.");
  662. return;
  663. }
  664. switch (style) {
  665. case Constants.EN_DASHED:
  666. useColor(col);
  667. if (horz) {
  668. float unit = Math.abs(2 * h);
  669. int rep = (int)(w / unit);
  670. if (rep % 2 == 0) {
  671. rep++;
  672. }
  673. unit = w / rep;
  674. gen.useDash("[" + unit + "] 0");
  675. gen.useLineCap(0);
  676. gen.useLineWidth(h);
  677. float ym = y1 + (h / 2);
  678. drawLine(x1, ym, x2, ym);
  679. } else {
  680. float unit = Math.abs(2 * w);
  681. int rep = (int)(h / unit);
  682. if (rep % 2 == 0) {
  683. rep++;
  684. }
  685. unit = h / rep;
  686. gen.useDash("[" + unit + "] 0");
  687. gen.useLineCap(0);
  688. gen.useLineWidth(w);
  689. float xm = x1 + (w / 2);
  690. drawLine(xm, y1, xm, y2);
  691. }
  692. break;
  693. case Constants.EN_DOTTED:
  694. useColor(col);
  695. gen.useLineCap(1); //Rounded!
  696. if (horz) {
  697. float unit = Math.abs(2 * h);
  698. int rep = (int)(w / unit);
  699. if (rep % 2 == 0) {
  700. rep++;
  701. }
  702. unit = w / rep;
  703. gen.useDash("[0 " + unit + "] 0");
  704. gen.useLineWidth(h);
  705. float ym = y1 + (h / 2);
  706. drawLine(x1, ym, x2, ym);
  707. } else {
  708. float unit = Math.abs(2 * w);
  709. int rep = (int)(h / unit);
  710. if (rep % 2 == 0) {
  711. rep++;
  712. }
  713. unit = h / rep;
  714. gen.useDash("[0 " + unit + "] 0");
  715. gen.useLineWidth(w);
  716. float xm = x1 + (w / 2);
  717. drawLine(xm, y1, xm, y2);
  718. }
  719. break;
  720. case Constants.EN_DOUBLE:
  721. useColor(col);
  722. gen.useDash(null);
  723. if (horz) {
  724. float h3 = h / 3;
  725. gen.useLineWidth(h3);
  726. float ym1 = y1 + (h3 / 2);
  727. float ym2 = ym1 + h3 + h3;
  728. drawLine(x1, ym1, x2, ym1);
  729. drawLine(x1, ym2, x2, ym2);
  730. } else {
  731. float w3 = w / 3;
  732. gen.useLineWidth(w3);
  733. float xm1 = x1 + (w3 / 2);
  734. float xm2 = xm1 + w3 + w3;
  735. drawLine(xm1, y1, xm1, y2);
  736. drawLine(xm2, y1, xm2, y2);
  737. }
  738. break;
  739. case Constants.EN_GROOVE:
  740. case Constants.EN_RIDGE:
  741. float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
  742. gen.useDash(null);
  743. if (horz) {
  744. Color uppercol = lightenColor(col, -colFactor);
  745. Color lowercol = lightenColor(col, colFactor);
  746. float h3 = h / 3;
  747. gen.useLineWidth(h3);
  748. float ym1 = y1 + (h3 / 2);
  749. gen.useColor(uppercol);
  750. drawLine(x1, ym1, x2, ym1);
  751. gen.useColor(col);
  752. drawLine(x1, ym1 + h3, x2, ym1 + h3);
  753. gen.useColor(lowercol);
  754. drawLine(x1, ym1 + h3 + h3, x2, ym1 + h3 + h3);
  755. } else {
  756. Color leftcol = lightenColor(col, -colFactor);
  757. Color rightcol = lightenColor(col, colFactor);
  758. float w3 = w / 3;
  759. gen.useLineWidth(w3);
  760. float xm1 = x1 + (w3 / 2);
  761. gen.useColor(leftcol);
  762. drawLine(xm1, y1, xm1, y2);
  763. gen.useColor(col);
  764. drawLine(xm1 + w3, y1, xm1 + w3, y2);
  765. gen.useColor(rightcol);
  766. drawLine(xm1 + w3 + w3, y1, xm1 + w3 + w3, y2);
  767. }
  768. break;
  769. case Constants.EN_INSET:
  770. case Constants.EN_OUTSET:
  771. colFactor = (style == EN_OUTSET ? 0.4f : -0.4f);
  772. gen.useDash(null);
  773. if (horz) {
  774. Color c = lightenColor(col, (startOrBefore ? 1 : -1) * colFactor);
  775. gen.useLineWidth(h);
  776. float ym1 = y1 + (h / 2);
  777. gen.useColor(c);
  778. drawLine(x1, ym1, x2, ym1);
  779. } else {
  780. Color c = lightenColor(col, (startOrBefore ? 1 : -1) * colFactor);
  781. gen.useLineWidth(w);
  782. float xm1 = x1 + (w / 2);
  783. gen.useColor(c);
  784. drawLine(xm1, y1, xm1, y2);
  785. }
  786. break;
  787. case Constants.EN_HIDDEN:
  788. break;
  789. default:
  790. useColor(col);
  791. gen.useDash(null);
  792. gen.useLineCap(0);
  793. if (horz) {
  794. gen.useLineWidth(h);
  795. float ym = y1 + (h / 2);
  796. drawLine(x1, ym, x2, ym);
  797. } else {
  798. gen.useLineWidth(w);
  799. float xm = x1 + (w / 2);
  800. drawLine(xm, y1, xm, y2);
  801. }
  802. }
  803. } catch (IOException ioe) {
  804. handleIOTrouble(ioe);
  805. }
  806. }
  807. /**
  808. * {@inheritDoc}
  809. */
  810. public void startRenderer(OutputStream outputStream)
  811. throws IOException {
  812. log.debug("Rendering areas to PostScript...");
  813. this.outputStream = outputStream;
  814. OutputStream out;
  815. if (isOptimizeResources()) {
  816. this.tempFile = File.createTempFile("fop", null);
  817. out = new java.io.FileOutputStream(this.tempFile);
  818. out = new java.io.BufferedOutputStream(out);
  819. } else {
  820. out = this.outputStream;
  821. }
  822. //Setup for PostScript generation
  823. this.gen = new PSGenerator(out) {
  824. /** Need to subclass PSGenerator to have better URI resolution */
  825. public Source resolveURI(String uri) {
  826. return userAgent.resolveURI(uri);
  827. }
  828. };
  829. this.gen.setPSLevel(getLanguageLevel());
  830. this.currentPageNumber = 0;
  831. //Initial default page device dictionary settings
  832. this.pageDeviceDictionary = new PSPageDeviceDictionary();
  833. pageDeviceDictionary.setFlushOnRetrieval(!this.dscCompliant);
  834. pageDeviceDictionary.put("/ImagingBBox", "null");
  835. }
  836. private void writeHeader() throws IOException {
  837. //PostScript Header
  838. writeln(DSCConstants.PS_ADOBE_30);
  839. gen.writeDSCComment(DSCConstants.CREATOR, new String[] {userAgent.getProducer()});
  840. gen.writeDSCComment(DSCConstants.CREATION_DATE, new Object[] {new java.util.Date()});
  841. gen.writeDSCComment(DSCConstants.LANGUAGE_LEVEL, new Integer(gen.getPSLevel()));
  842. gen.writeDSCComment(DSCConstants.PAGES, new Object[] {DSCConstants.ATEND});
  843. gen.writeDSCComment(DSCConstants.BBOX, DSCConstants.ATEND);
  844. gen.writeDSCComment(DSCConstants.HIRES_BBOX, DSCConstants.ATEND);
  845. this.documentBoundingBox = new Rectangle2D.Double();
  846. gen.writeDSCComment(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES,
  847. new Object[] {DSCConstants.ATEND});
  848. if (headerComments != null) {
  849. for (Iterator iter = headerComments.iterator(); iter.hasNext();) {
  850. PSExtensionAttachment comment = (PSExtensionAttachment)iter.next();
  851. gen.writeln("%" + comment.getContent());
  852. }
  853. }
  854. gen.writeDSCComment(DSCConstants.END_COMMENTS);
  855. //Defaults
  856. gen.writeDSCComment(DSCConstants.BEGIN_DEFAULTS);
  857. gen.writeDSCComment(DSCConstants.END_DEFAULTS);
  858. //Prolog and Setup written right before the first page-sequence, see startPageSequence()
  859. //Do this only once, as soon as we have all the content for the Setup section!
  860. //Prolog
  861. gen.writeDSCComment(DSCConstants.BEGIN_PROLOG);
  862. PSProcSets.writeStdProcSet(gen);
  863. PSProcSets.writeEPSProcSet(gen);
  864. gen.writeDSCComment(DSCConstants.END_PROLOG);
  865. //Setup
  866. gen.writeDSCComment(DSCConstants.BEGIN_SETUP);
  867. writeSetupCodeList(setupCodeList, "SetupCode");
  868. if (!isOptimizeResources()) {
  869. this.fontResources = PSFontUtils.writeFontDict(gen, fontInfo);
  870. } else {
  871. gen.commentln("%FOPFontSetup");
  872. }
  873. gen.writeDSCComment(DSCConstants.END_SETUP);
  874. }
  875. /**
  876. * {@inheritDoc}
  877. */
  878. public void stopRenderer() throws IOException {
  879. //Notify resource usage for font which are not supplied
  880. /* done in useFont now
  881. Map fonts = fontInfo.getUsedFonts();
  882. Iterator e = fonts.keySet().iterator();
  883. while (e.hasNext()) {
  884. String key = (String)e.next();
  885. PSResource res = (PSResource)this.fontResources.get(key);
  886. gen.notifyResourceUsage(res);
  887. }*/
  888. //Write trailer
  889. gen.writeDSCComment(DSCConstants.TRAILER);
  890. if (footerComments != null) {
  891. for (Iterator iter = footerComments.iterator(); iter.hasNext();) {
  892. PSExtensionAttachment comment = (PSExtensionAttachment)iter.next();
  893. gen.commentln("%" + comment.getContent());
  894. }
  895. footerComments.clear();
  896. }
  897. gen.writeDSCComment(DSCConstants.PAGES, new Integer(this.currentPageNumber));
  898. new DSCCommentBoundingBox(this.documentBoundingBox).generate(gen);
  899. new DSCCommentHiResBoundingBox(this.documentBoundingBox).generate(gen);
  900. gen.getResourceTracker().writeResources(false, gen);
  901. gen.writeDSCComment(DSCConstants.EOF);
  902. gen.flush();
  903. log.debug("Rendering to PostScript complete.");
  904. if (isOptimizeResources()) {
  905. IOUtils.closeQuietly(gen.getOutputStream());
  906. rewritePostScriptFile();
  907. }
  908. if (footerComments != null) {
  909. headerComments.clear();
  910. }
  911. if (pageDeviceDictionary != null) {
  912. pageDeviceDictionary.clear();
  913. }
  914. }
  915. /**
  916. * Used for two-pass production. This will rewrite the PostScript file from the temporary
  917. * file while adding all needed resources.
  918. * @throws IOException In case of an I/O error.
  919. */
  920. private void rewritePostScriptFile() throws IOException {
  921. log.debug("Processing PostScript resources...");
  922. long startTime = System.currentTimeMillis();
  923. ResourceTracker resTracker = gen.getResourceTracker();
  924. InputStream in = new java.io.FileInputStream(this.tempFile);
  925. in = new java.io.BufferedInputStream(in);
  926. try {
  927. try {
  928. ResourceHandler.process(this.userAgent, in, this.outputStream,
  929. this.fontInfo, resTracker, this.formResources,
  930. this.currentPageNumber, this.documentBoundingBox);
  931. this.outputStream.flush();
  932. } catch (DSCException e) {
  933. throw new RuntimeException(e.getMessage());
  934. }
  935. } finally {
  936. IOUtils.closeQuietly(in);
  937. if (!this.tempFile.delete()) {
  938. this.tempFile.deleteOnExit();
  939. log.warn("Could not delete temporary file: " + this.tempFile);
  940. }
  941. }
  942. if (log.isDebugEnabled()) {
  943. long duration = System.currentTimeMillis() - startTime;
  944. log.debug("Resource Processing complete in " + duration + " ms.");
  945. }
  946. }
  947. /** {@inheritDoc} */
  948. public void processOffDocumentItem(OffDocumentItem oDI) {
  949. if (log.isDebugEnabled()) {
  950. log.debug("Handling OffDocumentItem: " + oDI.getName());
  951. }
  952. if (oDI instanceof OffDocumentExtensionAttachment) {
  953. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)oDI).getAttachment();
  954. if (attachment != null) {
  955. if (PSExtensionAttachment.CATEGORY.equals(attachment.getCategory())) {
  956. if (attachment instanceof PSSetupCode) {
  957. if (setupCodeList == null) {
  958. setupCodeList = new java.util.ArrayList();
  959. }
  960. if (!setupCodeList.contains(attachment)) {
  961. setupCodeList.add(attachment);
  962. }
  963. } else if (attachment instanceof PSSetPageDevice) {
  964. /**
  965. * Extract all PSSetPageDevice instances from the
  966. * attachment list on the s-p-m and add all dictionary
  967. * entries to our internal representation of the the
  968. * page device dictionary.
  969. */
  970. PSSetPageDevice setPageDevice = (PSSetPageDevice)attachment;
  971. String content = setPageDevice.getContent();
  972. if (content != null) {
  973. try {
  974. this.pageDeviceDictionary.putAll(PSDictionary.valueOf(content));
  975. } catch (PSDictionaryFormatException e) {
  976. log.error("Failed to parse dictionary string: "
  977. + e.getMessage() + ", content = '" + content + "'");
  978. }
  979. }
  980. } else if (attachment instanceof PSCommentBefore) {
  981. if (headerComments == null) {
  982. headerComments = new java.util.ArrayList();
  983. }
  984. headerComments.add(attachment);
  985. } else if (attachment instanceof PSCommentAfter) {
  986. if (footerComments == null) {
  987. footerComments = new java.util.ArrayList();
  988. }
  989. footerComments.add(attachment);
  990. }
  991. }
  992. }
  993. }
  994. super.processOffDocumentItem(oDI);
  995. }
  996. /** {@inheritDoc} */
  997. public void startPageSequence(LineArea seqTitle) {
  998. super.startPageSequence(seqTitle);
  999. }
  1000. /**
  1001. * Formats and writes a List of PSSetupCode instances to the output stream.
  1002. * @param setupCodeList a List of PSSetupCode instances
  1003. * @param type the type of code section
  1004. */
  1005. private void writeSetupCodeList(List setupCodeList, String type) throws IOException {
  1006. if (setupCodeList != null) {
  1007. Iterator i = setupCodeList.iterator();
  1008. while (i.hasNext()) {
  1009. PSSetupCode setupCode = (PSSetupCode)i.next();
  1010. gen.commentln("%FOPBegin" + type + ": ("
  1011. + (setupCode.getName() != null ? setupCode.getName() : "")
  1012. + ")");
  1013. LineNumberReader reader = new LineNumberReader(
  1014. new java.io.StringReader(setupCode.getContent()));
  1015. String line;
  1016. while ((line = reader.readLine()) != null) {
  1017. line = line.trim();
  1018. if (line.length() > 0) {
  1019. gen.writeln(line.trim());
  1020. }
  1021. }
  1022. gen.commentln("%FOPEnd" + type);
  1023. i.remove();
  1024. }
  1025. }
  1026. }
  1027. /**
  1028. * {@inheritDoc}
  1029. */
  1030. public void renderPage(PageViewport page)
  1031. throws IOException, FOPException {
  1032. log.debug("renderPage(): " + page);
  1033. if (this.currentPageNumber == 0) {
  1034. writeHeader();
  1035. }
  1036. this.currentPageNumber++;
  1037. gen.getResourceTracker().notifyStartNewPage();
  1038. gen.getResourceTracker().notifyResourceUsageOnPage(PSProcSets.STD_PROCSET);
  1039. gen.writeDSCComment(DSCConstants.PAGE, new Object[]
  1040. {page.getPageNumberString(),
  1041. new Integer(this.currentPageNumber)});
  1042. double pageWidth = Math.round(page.getViewArea().getWidth()) / 1000f;
  1043. double pageHeight = Math.round(page.getViewArea().getHeight()) / 1000f;
  1044. boolean rotate = false;
  1045. List pageSizes = new java.util.ArrayList();
  1046. if (this.autoRotateLandscape && (pageHeight < pageWidth)) {
  1047. rotate = true;
  1048. pageSizes.add(new Long(Math.round(pageHeight)));
  1049. pageSizes.add(new Long(Math.round(pageWidth)));
  1050. } else {
  1051. pageSizes.add(new Long(Math.round(pageWidth)));
  1052. pageSizes.add(new Long(Math.round(pageHeight)));
  1053. }
  1054. pageDeviceDictionary.put("/PageSize", pageSizes);
  1055. if (page.hasExtensionAttachments()) {
  1056. for (Iterator iter = page.getExtensionAttachments().iterator();
  1057. iter.hasNext();) {
  1058. ExtensionAttachment attachment = (ExtensionAttachment) iter.next();
  1059. if (attachment instanceof PSSetPageDevice) {
  1060. /**
  1061. * Extract all PSSetPageDevice instances from the
  1062. * attachment list on the s-p-m and add all
  1063. * dictionary entries to our internal representation
  1064. * of the the page device dictionary.
  1065. */
  1066. PSSetPageDevice setPageDevice = (PSSetPageDevice)attachment;
  1067. String content = setPageDevice.getContent();
  1068. if (content != null) {
  1069. try {
  1070. pageDeviceDictionary.putAll(PSDictionary.valueOf(content));
  1071. } catch (PSDictionaryFormatException e) {
  1072. log.error("failed to parse dictionary string: "
  1073. + e.getMessage() + ", [" + content + "]");
  1074. }
  1075. }
  1076. }
  1077. }
  1078. }
  1079. try {
  1080. if (setupCodeList != null) {
  1081. writeEnclosedExtensionAttachments(setupCodeList);
  1082. setupCodeList.clear();
  1083. }
  1084. } catch (IOException e) {
  1085. log.error(e.getMessage());
  1086. }
  1087. final Integer zero = new Integer(0);
  1088. Rectangle2D pageBoundingBox = new Rectangle2D.Double();
  1089. if (rotate) {
  1090. pageBoundingBox.setRect(0, 0, pageHeight, pageWidth);
  1091. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[] {
  1092. zero, zero, new Long(Math.round(pageHeight)),
  1093. new Long(Math.round(pageWidth)) });
  1094. gen.writeDSCComment(DSCConstants.PAGE_HIRES_BBOX, new Object[] {
  1095. zero, zero, new Double(pageHeight),
  1096. new Double(pageWidth) });
  1097. gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
  1098. } else {
  1099. pageBoundingBox.setRect(0, 0, pageWidth, pageHeight);
  1100. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[] {
  1101. zero, zero, new Long(Math.round(pageWidth)),
  1102. new Long(Math.round(pageHeight)) });
  1103. gen.writeDSCComment(DSCConstants.PAGE_HIRES_BBOX, new Object[] {
  1104. zero, zero, new Double(pageWidth),
  1105. new Double(pageHeight) });
  1106. if (autoRotateLandscape) {
  1107. gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION,
  1108. "Portrait");
  1109. }
  1110. }
  1111. this.documentBoundingBox.add(pageBoundingBox);
  1112. gen.writeDSCComment(DSCConstants.PAGE_RESOURCES,
  1113. new Object[] {DSCConstants.ATEND});
  1114. gen.commentln("%FOPSimplePageMaster: " + page.getSimplePageMasterName());
  1115. gen.writeDSCComment(DSCConstants.BEGIN_PAGE_SETUP);
  1116. if (page.hasExtensionAttachments()) {
  1117. List extensionAttachments = page.getExtensionAttachments();
  1118. for (int i = 0; i < extensionAttachments.size(); i++) {
  1119. Object attObj = extensionAttachments.get(i);
  1120. if (attObj instanceof PSExtensionAttachment) {
  1121. PSExtensionAttachment attachment = (PSExtensionAttachment)attObj;
  1122. if (attachment instanceof PSCommentBefore) {
  1123. gen.commentln("%" + attachment.getContent());
  1124. }
  1125. }
  1126. }
  1127. }
  1128. // Write any unwritten changes to page device dictionary
  1129. if (!pageDeviceDictionary.isEmpty()) {
  1130. String content = pageDeviceDictionary.getContent();
  1131. if (safeSetPageDevice) {
  1132. content += " SSPD";
  1133. } else {
  1134. content += " setpagedevice";
  1135. }
  1136. writeEnclosedExtensionAttachment(new PSSetPageDevice(content));
  1137. }
  1138. if (rotate) {
  1139. gen.writeln(Math.round(pageHeight) + " 0 translate");
  1140. gen.writeln("90 rotate");
  1141. }
  1142. concatMatrix(1, 0, 0, -1, 0, pageHeight);
  1143. gen.writeDSCComment(DSCConstants.END_PAGE_SETUP);
  1144. //Process page
  1145. super.renderPage(page);
  1146. //Show page
  1147. writeln("showpage");
  1148. gen.writeDSCComment(DSCConstants.PAGE_TRAILER);
  1149. if (page.hasExtensionAttachments()) {
  1150. List extensionAttachments = page.getExtensionAttachments();
  1151. for (int i = 0; i < extensionAttachments.size(); i++) {
  1152. Object attObj = extensionAttachments.get(i);
  1153. if (attObj instanceof PSExtensionAttachment) {
  1154. PSExtensionAttachment attachment = (PSExtensionAttachment)attObj;
  1155. if (attachment instanceof PSCommentAfter) {
  1156. gen.commentln("%" + attachment.getContent());
  1157. }
  1158. }
  1159. }
  1160. }
  1161. gen.getResourceTracker().writeResources(true, gen);
  1162. }
  1163. /** {@inheritDoc} */
  1164. protected void renderRegionViewport(RegionViewport port) {
  1165. if (port != null) {
  1166. comment("%FOPBeginRegionViewport: " + port.getRegionReference().getRegionName());
  1167. super.renderRegionViewport(port);
  1168. comment("%FOPEndRegionViewport");
  1169. }
  1170. }
  1171. /** Indicates the beginning of a text object. */
  1172. protected void beginTextObject() {
  1173. if (!inTextMode) {
  1174. saveGraphicsState();
  1175. writeln("BT");
  1176. inTextMode = true;
  1177. }
  1178. }
  1179. /** Indicates the end of a text object. */
  1180. protected void endTextObject() {
  1181. if (inTextMode) {
  1182. writeln("ET");
  1183. restoreGraphicsState();
  1184. inTextMode = false;
  1185. }
  1186. }
  1187. /**
  1188. * {@inheritDoc}
  1189. */
  1190. public void renderText(TextArea area) {
  1191. renderInlineAreaBackAndBorders(area);
  1192. String fontname = getInternalFontNameForArea(area);
  1193. int fontsize = area.getTraitAsInteger(Trait.FONT_SIZE);
  1194. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  1195. Typeface tf = (Typeface) fontInfo.getFonts().get(fontname);
  1196. //Determine position
  1197. int rx = currentIPPosition + area.getBorderAndPaddingWidthStart();
  1198. int bl = currentBPPosition + area.getOffset() + area.getBaselineOffset();
  1199. useFont(fontname, fontsize);
  1200. Color ct = (Color)area.getTrait(Trait.COLOR);
  1201. if (ct != null) {
  1202. try {
  1203. useColor(ct);
  1204. } catch (IOException ioe) {
  1205. handleIOTrouble(ioe);
  1206. }
  1207. }
  1208. beginTextObject();
  1209. writeln("1 0 0 -1 " + gen.formatDouble(rx / 1000f)
  1210. + " " + gen.formatDouble(bl / 1000f) + " Tm");
  1211. super.renderText(area); //Updates IPD
  1212. renderTextDecoration(tf, fontsize, area, bl, rx);
  1213. }
  1214. /**
  1215. * {@inheritDoc}
  1216. */
  1217. protected void renderWord(WordArea word) {
  1218. renderText((TextArea)word.getParentArea(), word.getWord(), word.getLetterAdjustArray());
  1219. super.renderWord(word);
  1220. }
  1221. /**
  1222. * {@inheritDoc}
  1223. */
  1224. protected void renderSpace(SpaceArea space) {
  1225. AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
  1226. String s = space.getSpace();
  1227. char sp = s.charAt(0);
  1228. Font font = getFontFromArea(textArea);
  1229. int tws = (space.isAdjustable()
  1230. ? ((TextArea) space.getParentArea()).getTextWordSpaceAdjust()
  1231. + 2 * textArea.getTextLetterSpaceAdjust()
  1232. : 0);
  1233. rmoveTo((font.getCharWidth(sp) + tws) / 1000f, 0);
  1234. super.renderSpace(space);
  1235. }
  1236. private void renderText(AbstractTextArea area, String text, int[] letterAdjust) {
  1237. Font font = getFontFromArea(area);
  1238. Typeface tf = (Typeface) fontInfo.getFonts().get(font.getFontName());
  1239. int initialSize = text.length();
  1240. initialSize += initialSize / 2;
  1241. StringBuffer sb = new StringBuffer(initialSize);
  1242. int textLen = text.length();
  1243. if (letterAdjust == null
  1244. && area.getTextLetterSpaceAdjust() == 0
  1245. && area.getTextWordSpaceAdjust() == 0) {
  1246. sb.append("(");
  1247. for (int i = 0; i < textLen; i++) {
  1248. final char c = text.charAt(i);
  1249. final char mapped = tf.mapChar(c);
  1250. PSGenerator.escapeChar(mapped, sb);
  1251. }
  1252. sb.append(") t");
  1253. } else {
  1254. sb.append("(");
  1255. int[] offsets = new int[textLen];
  1256. for (int i = 0; i < textLen; i++) {
  1257. final char c = text.charAt(i);
  1258. final char mapped = tf.mapChar(c);
  1259. int wordSpace;
  1260. if (CharUtilities.isAdjustableSpace(mapped)) {
  1261. wordSpace = area.getTextWordSpaceAdjust();
  1262. } else {
  1263. wordSpace = 0;
  1264. }
  1265. int cw = tf.getWidth(mapped, font.getFontSize()) / 1000;
  1266. int ladj = (letterAdjust != null && i < textLen - 1 ? letterAdjust[i + 1] : 0);
  1267. int tls = (i < textLen - 1 ? area.getTextLetterSpaceAdjust() : 0);
  1268. offsets[i] = cw + ladj + tls + wordSpace;
  1269. PSGenerator.escapeChar(mapped, sb);
  1270. }
  1271. sb.append(")" + PSGenerator.LF + "[");
  1272. for (int i = 0; i < textLen; i++) {
  1273. if (i > 0) {
  1274. if (i % 8 == 0) {
  1275. sb.append(PSGenerator.LF);
  1276. } else {
  1277. sb.append(" ");
  1278. }
  1279. }
  1280. sb.append(gen.formatDouble(offsets[i] / 1000f));
  1281. }
  1282. sb.append("]" + PSGenerator.LF + "xshow");
  1283. }
  1284. writeln(sb.toString());
  1285. }
  1286. /** {@inheritDoc} */
  1287. protected List breakOutOfStateStack() {
  1288. try {
  1289. List breakOutList = new java.util.ArrayList();
  1290. PSState state;
  1291. while (true) {
  1292. if (breakOutList.size() == 0) {
  1293. endTextObject();
  1294. comment("------ break out!");
  1295. }
  1296. state = gen.getCurrentState();
  1297. if (!gen.restoreGraphicsState()) {
  1298. break;
  1299. }
  1300. breakOutList.add(0, state); //Insert because of stack-popping
  1301. }
  1302. return breakOutList;
  1303. } catch (IOException ioe) {
  1304. handleIOTrouble(ioe);
  1305. return null;
  1306. }
  1307. }
  1308. /** {@inheritDoc} */
  1309. protected void restoreStateStackAfterBreakOut(List breakOutList) {
  1310. try {
  1311. comment("------ restoring context after break-out...");
  1312. PSState state;
  1313. Iterator i = breakOutList.iterator();
  1314. while (i.hasNext()) {
  1315. state = (PSState)i.next();
  1316. saveGraphicsState();
  1317. state.reestablish(gen);
  1318. }
  1319. comment("------ done.");
  1320. } catch (IOException ioe) {
  1321. handleIOTrouble(ioe);
  1322. }
  1323. }
  1324. /**
  1325. * {@inheritDoc}
  1326. */
  1327. protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
  1328. saveGraphicsState();
  1329. if (clippingRect != null) {
  1330. clipRect((float)clippingRect.getX() / 1000f,
  1331. (float)clippingRect.getY() / 1000f,
  1332. (float)clippingRect.getWidth() / 1000f,
  1333. (float)clippingRect.getHeight() / 1000f);
  1334. }
  1335. // multiply with current CTM
  1336. final double[] matrix = ctm.toArray();
  1337. matrix[4] /= 1000f;
  1338. matrix[5] /= 1000f;
  1339. concatMatrix(matrix);
  1340. }
  1341. /**
  1342. * {@inheritDoc}
  1343. */
  1344. protected void endVParea() {
  1345. endTextObject();
  1346. restoreGraphicsState();
  1347. }
  1348. /** {@inheritDoc} */
  1349. protected void renderBlockViewport(BlockViewport bv, List children) {
  1350. comment("%FOPBeginBlockViewport: " + bv.toString());
  1351. super.renderBlockViewport(bv, children);
  1352. comment("%FOPEndBlockViewport");
  1353. }
  1354. /** {@inheritDoc} */
  1355. protected void renderInlineParent(InlineParent ip) {
  1356. super.renderInlineParent(ip);
  1357. }
  1358. /**
  1359. * {@inheritDoc}
  1360. */
  1361. public void renderLeader(Leader area) {
  1362. renderInlineAreaBackAndBorders(area);
  1363. endTextObject();
  1364. saveGraphicsState();
  1365. int style = area.getRuleStyle();
  1366. float startx = (currentIPPosition + area.getBorderAndPaddingWidthStart()) / 1000f;
  1367. float starty = (currentBPPosition + area.getOffset()) / 1000f;
  1368. float endx = (currentIPPosition + area.getBorderAndPaddingWidthStart()
  1369. + area.getIPD()) / 1000f;
  1370. float ruleThickness = area.getRuleThickness() / 1000f;
  1371. Color col = (Color)area.getTrait(Trait.COLOR);
  1372. try {
  1373. switch (style) {
  1374. case EN_SOLID:
  1375. case EN_DASHED:
  1376. case EN_DOUBLE:
  1377. drawBorderLine(startx, starty, endx, starty + ruleThickness,
  1378. true, true, style, col);
  1379. break;
  1380. case EN_DOTTED:
  1381. clipRect(startx, starty, endx - startx, ruleThickness);
  1382. //This displaces the dots to the right by half a dot's width
  1383. //TODO There's room for improvement here
  1384. gen.concatMatrix(1, 0, 0, 1, ruleThickness / 2, 0);
  1385. drawBorderLine(startx, starty, endx, starty + ruleThickness,
  1386. true, true, style, col);
  1387. break;
  1388. case EN_GROOVE:
  1389. case EN_RIDGE:
  1390. float half = area.getRuleThickness() / 2000f;
  1391. gen.useColor(lightenColor(col, 0.6f));
  1392. moveTo(startx, starty);
  1393. lineTo(endx, starty);
  1394. lineTo(endx, starty + 2 * half);
  1395. lineTo(startx, starty + 2 * half);
  1396. closePath();
  1397. gen.writeln(" fill newpath");
  1398. gen.useColor(col);
  1399. if (style == EN_GROOVE) {
  1400. moveTo(startx, starty);
  1401. lineTo(endx, starty);
  1402. lineTo(endx, starty + half);
  1403. lineTo(startx + half, starty + half);
  1404. lineTo(startx, starty + 2 * half);
  1405. } else {
  1406. moveTo(endx, starty);
  1407. lineTo(endx, starty + 2 * half);
  1408. lineTo(startx, starty + 2 * half);
  1409. lineTo(startx, starty + half);
  1410. lineTo(endx - half, starty + half);
  1411. }
  1412. closePath();
  1413. gen.writeln(" fill newpath");
  1414. break;
  1415. default:
  1416. throw new UnsupportedOperationException("rule style not supported");
  1417. }
  1418. } catch (IOException ioe) {
  1419. handleIOTrouble(ioe);
  1420. }
  1421. restoreGraphicsState();
  1422. super.renderLeader(area);
  1423. }
  1424. /**
  1425. * {@inheritDoc}
  1426. */
  1427. public void renderImage(Image image, Rectangle2D pos) {
  1428. drawImage(image.getURL(), pos);
  1429. }
  1430. /**
  1431. * {@inheritDoc}
  1432. */
  1433. protected RendererContext createRendererContext(int x, int y, int width, int height,
  1434. Map foreignAttributes) {
  1435. RendererContext context = super.createRendererContext(
  1436. x, y, width, height, foreignAttributes);
  1437. context.setProperty(PSRendererContextConstants.PS_GENERATOR, this.gen);
  1438. context.setProperty(PSRendererContextConstants.PS_FONT_INFO, fontInfo);
  1439. return context;
  1440. }
  1441. /** {@inheritDoc} */
  1442. public String getMimeType() {
  1443. return MIME_TYPE;
  1444. }
  1445. /**
  1446. * Formats and writes a PSExtensionAttachment to the output stream.
  1447. *
  1448. * @param attachment an PSExtensionAttachment instance
  1449. */
  1450. private void writeEnclosedExtensionAttachment(PSExtensionAttachment attachment)
  1451. throws IOException {
  1452. String info = "";
  1453. if (attachment instanceof PSSetupCode) {
  1454. PSSetupCode setupCodeAttach = (PSSetupCode)attachment;
  1455. String name = setupCodeAttach.getName();
  1456. if (name != null) {
  1457. info += ": (" + name + ")";
  1458. }
  1459. }
  1460. String type = attachment.getType();
  1461. gen.commentln("%FOPBegin" + type + info);
  1462. LineNumberReader reader = new LineNumberReader(
  1463. new java.io.StringReader(attachment.getContent()));
  1464. String line;
  1465. while ((line = reader.readLine()) != null) {
  1466. line = line.trim();
  1467. if (line.length() > 0) {
  1468. gen.writeln(line);
  1469. }
  1470. }
  1471. gen.commentln("%FOPEnd" + type);
  1472. }
  1473. /**
  1474. * Formats and writes a Collection of PSExtensionAttachment instances to
  1475. * the output stream.
  1476. *
  1477. * @param attachmentCollection
  1478. * a Collection of PSExtensionAttachment instances
  1479. */
  1480. private void writeEnclosedExtensionAttachments(Collection attachmentCollection)
  1481. throws IOException {
  1482. Iterator iter = attachmentCollection.iterator();
  1483. while (iter.hasNext()) {
  1484. PSExtensionAttachment attachment = (PSExtensionAttachment)iter
  1485. .next();
  1486. if (attachment != null) {
  1487. writeEnclosedExtensionAttachment(attachment);
  1488. }
  1489. iter.remove();
  1490. }
  1491. }
  1492. /**
  1493. * Sets whether or not the safe set page device macro should be used
  1494. * (as opposed to directly invoking setpagedevice) when setting the
  1495. * postscript page device.
  1496. *
  1497. * This option is a useful option when you want to guard against the possibility
  1498. * of invalid/unsupported postscript key/values being placed in the page device.
  1499. *
  1500. * @param safeSetPageDevice setting to false and the renderer will make a
  1501. * standard "setpagedevice" call, setting to true will make a safe set page
  1502. * device macro call (default is false).
  1503. */
  1504. public void setSafeSetPageDevice(boolean safeSetPageDevice) {
  1505. this.safeSetPageDevice = safeSetPageDevice;
  1506. }
  1507. /**
  1508. * Sets whether or not Dublin Core Standard (dsc) compliance is enforced.
  1509. *
  1510. * It can cause problems (unwanted postscript subsystem initgraphics/erasepage calls)
  1511. * on some printers when the pagedevice is set. If this causes problems on a
  1512. * particular implementation then use this setting with a 'false' value to try and
  1513. * minimize the number of setpagedevice calls in the postscript document output.
  1514. *
  1515. * Set this value to false if you experience unwanted blank pages in your
  1516. * postscript output.
  1517. * @param dscCompliant boolean value (default is true)
  1518. */
  1519. public void setDSCCompliant(boolean dscCompliant) {
  1520. this.dscCompliant = dscCompliant;
  1521. }
  1522. }