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.

PDFRenderer.java 71KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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.pdf;
  19. // Java
  20. import java.awt.Color;
  21. import java.awt.Point;
  22. import java.awt.Rectangle;
  23. import java.awt.color.ICC_Profile;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.geom.Point2D;
  26. import java.awt.geom.Rectangle2D;
  27. import java.io.FileNotFoundException;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.OutputStream;
  31. import java.net.URL;
  32. import java.util.Iterator;
  33. import java.util.List;
  34. import java.util.Map;
  35. import javax.xml.transform.Source;
  36. import javax.xml.transform.stream.StreamSource;
  37. import org.apache.commons.io.IOUtils;
  38. import org.apache.fop.AbstractData;
  39. import org.apache.fop.apps.FOPException;
  40. import org.apache.fop.apps.FOUserAgent;
  41. import org.apache.fop.apps.MimeConstants;
  42. import org.apache.fop.area.Area;
  43. import org.apache.fop.area.Block;
  44. import org.apache.fop.area.BookmarkData;
  45. import org.apache.fop.area.CTM;
  46. import org.apache.fop.area.DestinationData;
  47. import org.apache.fop.area.LineArea;
  48. import org.apache.fop.area.OffDocumentExtensionAttachment;
  49. import org.apache.fop.area.OffDocumentItem;
  50. import org.apache.fop.area.PageSequence;
  51. import org.apache.fop.area.PageViewport;
  52. import org.apache.fop.area.Trait;
  53. import org.apache.fop.area.inline.AbstractTextArea;
  54. import org.apache.fop.area.inline.Image;
  55. import org.apache.fop.area.inline.InlineArea;
  56. import org.apache.fop.area.inline.InlineParent;
  57. import org.apache.fop.area.inline.Leader;
  58. import org.apache.fop.area.inline.SpaceArea;
  59. import org.apache.fop.area.inline.TextArea;
  60. import org.apache.fop.area.inline.WordArea;
  61. import org.apache.fop.datatypes.URISpecification;
  62. import org.apache.fop.events.ResourceEventProducer;
  63. import org.apache.fop.fo.Constants;
  64. import org.apache.fop.fo.extensions.ExtensionAttachment;
  65. import org.apache.fop.fo.extensions.xmp.XMPMetadata;
  66. import org.apache.fop.fonts.Font;
  67. import org.apache.fop.fonts.LazyFont;
  68. import org.apache.fop.fonts.SingleByteFont;
  69. import org.apache.fop.fonts.Typeface;
  70. import org.apache.fop.pdf.PDFAMode;
  71. import org.apache.fop.pdf.PDFAction;
  72. import org.apache.fop.pdf.PDFAnnotList;
  73. import org.apache.fop.pdf.PDFColor;
  74. import org.apache.fop.pdf.PDFConformanceException;
  75. import org.apache.fop.pdf.PDFDictionary;
  76. import org.apache.fop.pdf.PDFDocument;
  77. import org.apache.fop.pdf.PDFEncryptionManager;
  78. import org.apache.fop.pdf.PDFEncryptionParams;
  79. import org.apache.fop.pdf.PDFFactory;
  80. import org.apache.fop.pdf.PDFFilterList;
  81. import org.apache.fop.pdf.PDFGoTo;
  82. import org.apache.fop.pdf.PDFICCBasedColorSpace;
  83. import org.apache.fop.pdf.PDFICCStream;
  84. import org.apache.fop.pdf.PDFInfo;
  85. import org.apache.fop.pdf.PDFLink;
  86. import org.apache.fop.pdf.PDFMetadata;
  87. import org.apache.fop.pdf.PDFNumber;
  88. import org.apache.fop.pdf.PDFNumsArray;
  89. import org.apache.fop.pdf.PDFOutline;
  90. import org.apache.fop.pdf.PDFOutputIntent;
  91. import org.apache.fop.pdf.PDFPage;
  92. import org.apache.fop.pdf.PDFPageLabels;
  93. import org.apache.fop.pdf.PDFResourceContext;
  94. import org.apache.fop.pdf.PDFResources;
  95. import org.apache.fop.pdf.PDFState;
  96. import org.apache.fop.pdf.PDFStream;
  97. import org.apache.fop.pdf.PDFTextUtil;
  98. import org.apache.fop.pdf.PDFXMode;
  99. import org.apache.fop.pdf.PDFXObject;
  100. import org.apache.fop.render.AbstractPathOrientedRenderer;
  101. import org.apache.fop.render.Graphics2DAdapter;
  102. import org.apache.fop.render.RendererContext;
  103. import org.apache.fop.util.CharUtilities;
  104. import org.apache.fop.util.ColorProfileUtil;
  105. import org.apache.fop.util.ColorUtil;
  106. import org.apache.xmlgraphics.image.loader.ImageException;
  107. import org.apache.xmlgraphics.image.loader.ImageInfo;
  108. import org.apache.xmlgraphics.image.loader.ImageManager;
  109. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  110. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  111. import org.apache.xmlgraphics.xmp.Metadata;
  112. import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
  113. import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
  114. /**
  115. * Renderer that renders areas to PDF.
  116. */
  117. public class PDFRenderer extends AbstractPathOrientedRenderer {
  118. /**
  119. * The mime type for pdf
  120. */
  121. public static final String MIME_TYPE = MimeConstants.MIME_PDF;
  122. /** Normal PDF resolution (72dpi) */
  123. public static final int NORMAL_PDF_RESOLUTION = 72;
  124. /** PDF encryption parameter: all parameters as object, datatype: PDFEncryptionParams */
  125. public static final String ENCRYPTION_PARAMS = "encryption-params";
  126. /** PDF encryption parameter: user password, datatype: String */
  127. public static final String USER_PASSWORD = "user-password";
  128. /** PDF encryption parameter: owner password, datatype: String */
  129. public static final String OWNER_PASSWORD = "owner-password";
  130. /** PDF encryption parameter: Forbids printing, datatype: Boolean or "true"/"false" */
  131. public static final String NO_PRINT = "noprint";
  132. /** PDF encryption parameter: Forbids copying content, datatype: Boolean or "true"/"false" */
  133. public static final String NO_COPY_CONTENT = "nocopy";
  134. /** PDF encryption parameter: Forbids editing content, datatype: Boolean or "true"/"false" */
  135. public static final String NO_EDIT_CONTENT = "noedit";
  136. /** PDF encryption parameter: Forbids annotations, datatype: Boolean or "true"/"false" */
  137. public static final String NO_ANNOTATIONS = "noannotations";
  138. /** Rendering Options key for the PDF/A mode. */
  139. public static final String PDF_A_MODE = "pdf-a-mode";
  140. /** Rendering Options key for the PDF/X mode. */
  141. public static final String PDF_X_MODE = "pdf-x-mode";
  142. /** Rendering Options key for the ICC profile for the output intent. */
  143. public static final String KEY_OUTPUT_PROFILE = "output-profile";
  144. /**
  145. * Rendering Options key for disabling the sRGB color space (only possible if no PDF/A or
  146. * PDF/X profile is active).
  147. */
  148. public static final String KEY_DISABLE_SRGB_COLORSPACE = "disable-srgb-colorspace";
  149. /** Controls whether comments are written to the PDF stream. */
  150. protected static final boolean WRITE_COMMENTS = true;
  151. /**
  152. * the PDF Document being created
  153. */
  154. protected PDFDocument pdfDoc;
  155. /** the PDF/A mode (Default: disabled) */
  156. protected PDFAMode pdfAMode = PDFAMode.DISABLED;
  157. /** the PDF/X mode (Default: disabled) */
  158. protected PDFXMode pdfXMode = PDFXMode.DISABLED;
  159. /**
  160. * Map of pages using the PageViewport as the key
  161. * this is used for prepared pages that cannot be immediately
  162. * rendered
  163. */
  164. protected Map pages = null;
  165. /**
  166. * Maps unique PageViewport key to PDF page reference
  167. */
  168. protected Map pageReferences = new java.util.HashMap();
  169. /**
  170. * Maps unique PageViewport key back to PageViewport itself
  171. */
  172. protected Map pvReferences = new java.util.HashMap();
  173. /**
  174. * Maps XSL-FO element IDs to their on-page XY-positions
  175. * Must be used in conjunction with the page reference to fully specify the PDFGoTo details
  176. */
  177. protected Map idPositions = new java.util.HashMap();
  178. /**
  179. * Maps XSL-FO element IDs to PDFGoTo objects targeting the corresponding areas
  180. * These objects may not all be fully filled in yet
  181. */
  182. protected Map idGoTos = new java.util.HashMap();
  183. /**
  184. * The PDFGoTos in idGoTos that are not complete yet
  185. */
  186. protected List unfinishedGoTos = new java.util.ArrayList();
  187. // can't use a Set because PDFGoTo.equals returns true if the target is the same,
  188. // even if the object number differs
  189. /**
  190. * The output stream to write the document to
  191. */
  192. protected OutputStream ostream;
  193. /**
  194. * the /Resources object of the PDF document being created
  195. */
  196. protected PDFResources pdfResources;
  197. /**
  198. * the current stream to add PDF commands to
  199. */
  200. protected PDFStream currentStream;
  201. /**
  202. * the current annotation list to add annotations to
  203. */
  204. protected PDFResourceContext currentContext = null;
  205. /**
  206. * the current page to add annotations to
  207. */
  208. protected PDFPage currentPage;
  209. /**
  210. * the current page's PDF reference string (to avoid numerous function calls)
  211. */
  212. protected String currentPageRef;
  213. /** the (optional) encryption parameters */
  214. protected PDFEncryptionParams encryptionParams;
  215. /** the ICC stream used as output profile by this document for PDF/A and PDF/X functionality. */
  216. protected PDFICCStream outputProfile;
  217. /** the default sRGB color space. */
  218. protected PDFICCBasedColorSpace sRGBColorSpace;
  219. /** controls whether the sRGB color space should be installed */
  220. protected boolean disableSRGBColorSpace = false;
  221. /** Optional URI to an output profile to be used. */
  222. protected String outputProfileURI;
  223. /** drawing state */
  224. protected PDFState currentState = null;
  225. /** Text generation utility holding the current font status */
  226. protected PDFTextUtil textutil;
  227. /** page height */
  228. protected int pageHeight;
  229. /** Registry of PDF filters */
  230. protected Map filterMap;
  231. /** Image handler registry */
  232. private final PDFImageHandlerRegistry imageHandlerRegistry = new PDFImageHandlerRegistry();
  233. /**
  234. * create the PDF renderer
  235. */
  236. public PDFRenderer() {
  237. }
  238. private boolean booleanValueOf(Object obj) {
  239. if (obj instanceof Boolean) {
  240. return ((Boolean)obj).booleanValue();
  241. } else if (obj instanceof String) {
  242. return Boolean.valueOf((String)obj).booleanValue();
  243. } else {
  244. throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected.");
  245. }
  246. }
  247. /**
  248. * {@inheritDoc}
  249. */
  250. public void setUserAgent(FOUserAgent agent) {
  251. super.setUserAgent(agent);
  252. PDFEncryptionParams params
  253. = (PDFEncryptionParams)agent.getRendererOptions().get(ENCRYPTION_PARAMS);
  254. if (params != null) {
  255. this.encryptionParams = params; //overwrite if available
  256. }
  257. String pwd;
  258. pwd = (String)agent.getRendererOptions().get(USER_PASSWORD);
  259. if (pwd != null) {
  260. if (encryptionParams == null) {
  261. this.encryptionParams = new PDFEncryptionParams();
  262. }
  263. this.encryptionParams.setUserPassword(pwd);
  264. }
  265. pwd = (String)agent.getRendererOptions().get(OWNER_PASSWORD);
  266. if (pwd != null) {
  267. if (encryptionParams == null) {
  268. this.encryptionParams = new PDFEncryptionParams();
  269. }
  270. this.encryptionParams.setOwnerPassword(pwd);
  271. }
  272. Object setting;
  273. setting = agent.getRendererOptions().get(NO_PRINT);
  274. if (setting != null) {
  275. if (encryptionParams == null) {
  276. this.encryptionParams = new PDFEncryptionParams();
  277. }
  278. this.encryptionParams.setAllowPrint(!booleanValueOf(setting));
  279. }
  280. setting = agent.getRendererOptions().get(NO_COPY_CONTENT);
  281. if (setting != null) {
  282. if (encryptionParams == null) {
  283. this.encryptionParams = new PDFEncryptionParams();
  284. }
  285. this.encryptionParams.setAllowCopyContent(!booleanValueOf(setting));
  286. }
  287. setting = agent.getRendererOptions().get(NO_EDIT_CONTENT);
  288. if (setting != null) {
  289. if (encryptionParams == null) {
  290. this.encryptionParams = new PDFEncryptionParams();
  291. }
  292. this.encryptionParams.setAllowEditContent(!booleanValueOf(setting));
  293. }
  294. setting = agent.getRendererOptions().get(NO_ANNOTATIONS);
  295. if (setting != null) {
  296. if (encryptionParams == null) {
  297. this.encryptionParams = new PDFEncryptionParams();
  298. }
  299. this.encryptionParams.setAllowEditAnnotations(!booleanValueOf(setting));
  300. }
  301. String s = (String)agent.getRendererOptions().get(PDF_A_MODE);
  302. if (s != null) {
  303. this.pdfAMode = PDFAMode.valueOf(s);
  304. }
  305. s = (String)agent.getRendererOptions().get(PDF_X_MODE);
  306. if (s != null) {
  307. this.pdfXMode = PDFXMode.valueOf(s);
  308. }
  309. s = (String)agent.getRendererOptions().get(KEY_OUTPUT_PROFILE);
  310. if (s != null) {
  311. this.outputProfileURI = s;
  312. }
  313. setting = agent.getRendererOptions().get(KEY_DISABLE_SRGB_COLORSPACE);
  314. if (setting != null) {
  315. this.disableSRGBColorSpace = booleanValueOf(setting);
  316. }
  317. }
  318. /**
  319. * {@inheritDoc}
  320. */
  321. public void startRenderer(OutputStream stream) throws IOException {
  322. if (userAgent == null) {
  323. throw new IllegalStateException("UserAgent must be set before starting the renderer");
  324. }
  325. ostream = stream;
  326. this.pdfDoc = new PDFDocument(
  327. userAgent.getProducer() != null ? userAgent.getProducer() : "");
  328. this.pdfDoc.getProfile().setPDFAMode(this.pdfAMode);
  329. this.pdfDoc.getProfile().setPDFXMode(this.pdfXMode);
  330. this.pdfDoc.getInfo().setCreator(userAgent.getCreator());
  331. this.pdfDoc.getInfo().setCreationDate(userAgent.getCreationDate());
  332. this.pdfDoc.getInfo().setAuthor(userAgent.getAuthor());
  333. this.pdfDoc.getInfo().setTitle(userAgent.getTitle());
  334. this.pdfDoc.getInfo().setKeywords(userAgent.getKeywords());
  335. this.pdfDoc.setFilterMap(filterMap);
  336. this.pdfDoc.outputHeader(ostream);
  337. //Setup encryption if necessary
  338. PDFEncryptionManager.setupPDFEncryption(encryptionParams, this.pdfDoc);
  339. addsRGBColorSpace();
  340. if (this.outputProfileURI != null) {
  341. addDefaultOutputProfile();
  342. }
  343. if (pdfXMode != PDFXMode.DISABLED) {
  344. log.debug(pdfXMode + " is active.");
  345. log.warn("Note: " + pdfXMode
  346. + " support is work-in-progress and not fully implemented, yet!");
  347. addPDFXOutputIntent();
  348. }
  349. if (pdfAMode.isPDFA1LevelB()) {
  350. log.debug("PDF/A is active. Conformance Level: " + pdfAMode);
  351. addPDFA1OutputIntent();
  352. }
  353. }
  354. private void addsRGBColorSpace() throws IOException {
  355. if (disableSRGBColorSpace) {
  356. if (this.pdfAMode != PDFAMode.DISABLED
  357. || this.pdfXMode != PDFXMode.DISABLED
  358. || this.outputProfileURI != null) {
  359. throw new IllegalStateException("It is not possible to disable the sRGB color"
  360. + " space if PDF/A or PDF/X functionality is enabled or an"
  361. + " output profile is set!");
  362. }
  363. } else {
  364. if (this.sRGBColorSpace != null) {
  365. return;
  366. }
  367. //Map sRGB as default RGB profile for DeviceRGB
  368. this.sRGBColorSpace = PDFICCBasedColorSpace.setupsRGBAsDefaultRGBColorSpace(pdfDoc);
  369. }
  370. }
  371. private void addDefaultOutputProfile() throws IOException {
  372. if (this.outputProfile != null) {
  373. return;
  374. }
  375. ICC_Profile profile;
  376. InputStream in = null;
  377. if (this.outputProfileURI != null) {
  378. this.outputProfile = pdfDoc.getFactory().makePDFICCStream();
  379. Source src = userAgent.resolveURI(this.outputProfileURI);
  380. if (src == null) {
  381. throw new IOException("Output profile not found: " + this.outputProfileURI);
  382. }
  383. if (src instanceof StreamSource) {
  384. in = ((StreamSource)src).getInputStream();
  385. } else {
  386. in = new URL(src.getSystemId()).openStream();
  387. }
  388. try {
  389. profile = ICC_Profile.getInstance(in);
  390. } finally {
  391. IOUtils.closeQuietly(in);
  392. }
  393. this.outputProfile.setColorSpace(profile, null);
  394. } else {
  395. //Fall back to sRGB profile
  396. outputProfile = sRGBColorSpace.getICCStream();
  397. }
  398. }
  399. /**
  400. * Adds an OutputIntent to the PDF as mandated by PDF/A-1 when uncalibrated color spaces
  401. * are used (which is true if we use DeviceRGB to represent sRGB colors).
  402. * @throws IOException in case of an I/O problem
  403. */
  404. private void addPDFA1OutputIntent() throws IOException {
  405. addDefaultOutputProfile();
  406. String desc = ColorProfileUtil.getICCProfileDescription(this.outputProfile.getICCProfile());
  407. PDFOutputIntent outputIntent = pdfDoc.getFactory().makeOutputIntent();
  408. outputIntent.setSubtype(PDFOutputIntent.GTS_PDFA1);
  409. outputIntent.setDestOutputProfile(this.outputProfile);
  410. outputIntent.setOutputConditionIdentifier(desc);
  411. outputIntent.setInfo(outputIntent.getOutputConditionIdentifier());
  412. pdfDoc.getRoot().addOutputIntent(outputIntent);
  413. }
  414. /**
  415. * Adds an OutputIntent to the PDF as mandated by PDF/X when uncalibrated color spaces
  416. * are used (which is true if we use DeviceRGB to represent sRGB colors).
  417. * @throws IOException in case of an I/O problem
  418. */
  419. private void addPDFXOutputIntent() throws IOException {
  420. addDefaultOutputProfile();
  421. String desc = ColorProfileUtil.getICCProfileDescription(this.outputProfile.getICCProfile());
  422. int deviceClass = this.outputProfile.getICCProfile().getProfileClass();
  423. if (deviceClass != ICC_Profile.CLASS_OUTPUT) {
  424. throw new PDFConformanceException(pdfDoc.getProfile().getPDFXMode() + " requires that"
  425. + " the DestOutputProfile be an Output Device Profile. "
  426. + desc + " does not match that requirement.");
  427. }
  428. PDFOutputIntent outputIntent = pdfDoc.getFactory().makeOutputIntent();
  429. outputIntent.setSubtype(PDFOutputIntent.GTS_PDFX);
  430. outputIntent.setDestOutputProfile(this.outputProfile);
  431. outputIntent.setOutputConditionIdentifier(desc);
  432. outputIntent.setInfo(outputIntent.getOutputConditionIdentifier());
  433. pdfDoc.getRoot().addOutputIntent(outputIntent);
  434. }
  435. /**
  436. * Checks if there are any unfinished PDFGoTos left in the list and resolves them
  437. * to a default position on the page. Logs a warning, as this should not happen.
  438. */
  439. protected void finishOpenGoTos() {
  440. int count = unfinishedGoTos.size();
  441. if (count > 0) {
  442. // TODO : page height may not be the same for all targeted pages
  443. Point2D.Float defaultPos = new Point2D.Float(0f, pageHeight / 1000f); // top-o-page
  444. while (!unfinishedGoTos.isEmpty()) {
  445. PDFGoTo gt = (PDFGoTo) unfinishedGoTos.get(0);
  446. finishIDGoTo(gt, defaultPos);
  447. }
  448. PDFEventProducer eventProducer = PDFEventProducer.Provider.get(
  449. getUserAgent().getEventBroadcaster());
  450. eventProducer.nonFullyResolvedLinkTargets(this, count);
  451. // dysfunctional if pageref is null
  452. }
  453. }
  454. /**
  455. * {@inheritDoc}
  456. */
  457. public void stopRenderer() throws IOException {
  458. finishOpenGoTos();
  459. pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
  460. pdfDoc.outputTrailer(ostream);
  461. this.pdfDoc = null;
  462. ostream = null;
  463. pages = null;
  464. pageReferences.clear();
  465. pvReferences.clear();
  466. pdfResources = null;
  467. currentStream = null;
  468. currentContext = null;
  469. currentPage = null;
  470. currentState = null;
  471. this.textutil = null;
  472. idPositions.clear();
  473. idGoTos.clear();
  474. }
  475. /**
  476. * {@inheritDoc}
  477. */
  478. public boolean supportsOutOfOrder() {
  479. //return false;
  480. return true;
  481. }
  482. /**
  483. * {@inheritDoc}
  484. */
  485. public void processOffDocumentItem(OffDocumentItem odi) {
  486. if (odi instanceof DestinationData) {
  487. // render Destinations
  488. renderDestination((DestinationData) odi);
  489. } else if (odi instanceof BookmarkData) {
  490. // render Bookmark-Tree
  491. renderBookmarkTree((BookmarkData) odi);
  492. } else if (odi instanceof OffDocumentExtensionAttachment) {
  493. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)odi).getAttachment();
  494. if (XMPMetadata.CATEGORY.equals(attachment.getCategory())) {
  495. renderXMPMetadata((XMPMetadata)attachment);
  496. }
  497. }
  498. }
  499. private void renderDestination(DestinationData dd) {
  500. String targetID = dd.getIDRef();
  501. if (targetID == null || targetID.length() == 0) {
  502. throw new IllegalArgumentException("DestinationData must contain a ID reference");
  503. }
  504. PageViewport pv = dd.getPageViewport();
  505. if (pv != null) {
  506. PDFGoTo gt = getPDFGoToForID(targetID, pv.getKey());
  507. pdfDoc.getFactory().makeDestination(
  508. dd.getIDRef(), gt.makeReference());
  509. } else {
  510. //Warning already issued by AreaTreeHandler (debug level is sufficient)
  511. log.debug("Unresolved destination item received: " + dd.getIDRef());
  512. }
  513. }
  514. /**
  515. * Renders a Bookmark-Tree object
  516. * @param bookmarks the BookmarkData object containing all the Bookmark-Items
  517. */
  518. protected void renderBookmarkTree(BookmarkData bookmarks) {
  519. for (int i = 0; i < bookmarks.getCount(); i++) {
  520. BookmarkData ext = bookmarks.getSubData(i);
  521. renderBookmarkItem(ext, null);
  522. }
  523. }
  524. private void renderBookmarkItem(BookmarkData bookmarkItem,
  525. PDFOutline parentBookmarkItem) {
  526. PDFOutline pdfOutline = null;
  527. String targetID = bookmarkItem.getIDRef();
  528. if (targetID == null || targetID.length() == 0) {
  529. throw new IllegalArgumentException("DestinationData must contain a ID reference");
  530. }
  531. PageViewport pv = bookmarkItem.getPageViewport();
  532. if (pv != null) {
  533. String pvKey = pv.getKey();
  534. PDFGoTo gt = getPDFGoToForID(targetID, pvKey);
  535. // create outline object:
  536. PDFOutline parent = parentBookmarkItem != null
  537. ? parentBookmarkItem
  538. : pdfDoc.getOutlineRoot();
  539. pdfOutline = pdfDoc.getFactory().makeOutline(parent,
  540. bookmarkItem.getBookmarkTitle(), gt, bookmarkItem.showChildItems());
  541. } else {
  542. //Warning already issued by AreaTreeHandler (debug level is sufficient)
  543. log.debug("Bookmark with IDRef \"" + targetID + "\" has a null PageViewport.");
  544. }
  545. for (int i = 0; i < bookmarkItem.getCount(); i++) {
  546. renderBookmarkItem(bookmarkItem.getSubData(i), pdfOutline);
  547. }
  548. }
  549. private void renderXMPMetadata(XMPMetadata metadata) {
  550. Metadata docXMP = metadata.getMetadata();
  551. Metadata fopXMP = PDFMetadata.createXMPFromPDFDocument(pdfDoc);
  552. //Merge FOP's own metadata into the one from the XSL-FO document
  553. fopXMP.mergeInto(docXMP);
  554. XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(docXMP);
  555. //Metadata was changed so update metadata date
  556. xmpBasic.setMetadataDate(new java.util.Date());
  557. PDFMetadata.updateInfoFromMetadata(docXMP, pdfDoc.getInfo());
  558. PDFMetadata pdfMetadata = pdfDoc.getFactory().makeMetadata(
  559. docXMP, metadata.isReadOnly());
  560. pdfDoc.getRoot().setMetadata(pdfMetadata);
  561. }
  562. /** {@inheritDoc} */
  563. public Graphics2DAdapter getGraphics2DAdapter() {
  564. return new PDFGraphics2DAdapter(this);
  565. }
  566. /**
  567. * writes out a comment.
  568. * @param text text for the comment
  569. */
  570. protected void comment(String text) {
  571. if (WRITE_COMMENTS) {
  572. currentStream.add("% " + text + "\n");
  573. }
  574. }
  575. /** {@inheritDoc} */
  576. protected void saveGraphicsState() {
  577. endTextObject();
  578. currentState.push();
  579. currentStream.add("q\n");
  580. }
  581. private void restoreGraphicsState(boolean popState) {
  582. endTextObject();
  583. currentStream.add("Q\n");
  584. if (popState) {
  585. currentState.pop();
  586. }
  587. }
  588. /** {@inheritDoc} */
  589. protected void restoreGraphicsState() {
  590. restoreGraphicsState(true);
  591. }
  592. /** Indicates the beginning of a text object. */
  593. protected void beginTextObject() {
  594. if (!textutil.isInTextObject()) {
  595. textutil.beginTextObject();
  596. }
  597. }
  598. /** Indicates the end of a text object. */
  599. protected void endTextObject() {
  600. if (textutil.isInTextObject()) {
  601. textutil.endTextObject();
  602. }
  603. }
  604. /**
  605. * Start the next page sequence.
  606. * For the PDF renderer there is no concept of page sequences
  607. * but it uses the first available page sequence title to set
  608. * as the title of the PDF document, and the language of the
  609. * document.
  610. * @param pageSequence the page sequence
  611. */
  612. public void startPageSequence(PageSequence pageSequence) {
  613. super.startPageSequence(pageSequence);
  614. LineArea seqTitle = pageSequence.getTitle();
  615. if (seqTitle != null) {
  616. String str = convertTitleToString(seqTitle);
  617. PDFInfo info = this.pdfDoc.getInfo();
  618. if (info.getTitle() == null) {
  619. info.setTitle(str);
  620. }
  621. }
  622. if (pageSequence.getLanguage() != null) {
  623. String lang = pageSequence.getLanguage();
  624. String country = pageSequence.getCountry();
  625. String langCode = lang + (country != null ? "-" + country : "");
  626. if (pdfDoc.getRoot().getLanguage() == null) {
  627. //Only set if not set already (first non-null is used)
  628. //Note: No checking is performed whether the values are valid!
  629. pdfDoc.getRoot().setLanguage(langCode);
  630. }
  631. }
  632. if (pdfDoc.getRoot().getMetadata() == null) {
  633. //If at this time no XMP metadata for the overall document has been set, create it
  634. //from the PDFInfo object.
  635. Metadata xmp = PDFMetadata.createXMPFromPDFDocument(pdfDoc);
  636. PDFMetadata pdfMetadata = pdfDoc.getFactory().makeMetadata(
  637. xmp, true);
  638. pdfDoc.getRoot().setMetadata(pdfMetadata);
  639. }
  640. }
  641. /**
  642. * The pdf page is prepared by making the page.
  643. * The page is made in the pdf document without any contents
  644. * and then stored to add the contents later.
  645. * The page objects is stored using the area tree PageViewport
  646. * as a key.
  647. *
  648. * @param page the page to prepare
  649. */
  650. public void preparePage(PageViewport page) {
  651. setupPage(page);
  652. if (pages == null) {
  653. pages = new java.util.HashMap();
  654. }
  655. pages.put(page, currentPage);
  656. }
  657. private void setupPage(PageViewport page) {
  658. this.pdfResources = this.pdfDoc.getResources();
  659. Rectangle2D bounds = page.getViewArea();
  660. double w = bounds.getWidth();
  661. double h = bounds.getHeight();
  662. currentPage = this.pdfDoc.getFactory().makePage(
  663. this.pdfResources,
  664. (int) Math.round(w / 1000), (int) Math.round(h / 1000),
  665. page.getPageIndex());
  666. pageReferences.put(page.getKey(), currentPage.referencePDF());
  667. pvReferences.put(page.getKey(), page);
  668. //Produce page labels
  669. PDFPageLabels pageLabels = this.pdfDoc.getRoot().getPageLabels();
  670. if (pageLabels == null) {
  671. //Set up PageLabels
  672. pageLabels = this.pdfDoc.getFactory().makePageLabels();
  673. this.pdfDoc.getRoot().setPageLabels(pageLabels);
  674. }
  675. PDFNumsArray nums = pageLabels.getNums();
  676. PDFDictionary dict = new PDFDictionary(nums);
  677. dict.put("P", page.getPageNumberString());
  678. //TODO If the sequence of generated page numbers were inspected, this could be
  679. //expressed in a more space-efficient way
  680. nums.put(page.getPageIndex(), dict);
  681. }
  682. /**
  683. * This method creates a pdf stream for the current page
  684. * uses it as the contents of a new page. The page is written
  685. * immediately to the output stream.
  686. * {@inheritDoc}
  687. */
  688. public void renderPage(PageViewport page)
  689. throws IOException, FOPException {
  690. if (pages != null
  691. && (currentPage = (PDFPage) pages.get(page)) != null) {
  692. //Retrieve previously prepared page (out-of-line rendering)
  693. pages.remove(page);
  694. } else {
  695. setupPage(page);
  696. }
  697. currentPageRef = currentPage.referencePDF();
  698. Rectangle2D bounds = page.getViewArea();
  699. double h = bounds.getHeight();
  700. pageHeight = (int) h;
  701. currentStream = this.pdfDoc.getFactory()
  702. .makeStream(PDFFilterList.CONTENT_FILTER, false);
  703. this.textutil = new PDFTextUtil() {
  704. protected void write(String code) {
  705. currentStream.add(code);
  706. }
  707. };
  708. currentState = new PDFState();
  709. // Transform the PDF's default coordinate system (0,0 at lower left) to the PDFRenderer's
  710. AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
  711. pageHeight / 1000f);
  712. currentState.concatenate(basicPageTransform);
  713. currentStream.add(CTMHelper.toPDFString(basicPageTransform, false) + " cm\n");
  714. super.renderPage(page);
  715. this.pdfDoc.registerObject(currentStream);
  716. currentPage.setContents(currentStream);
  717. PDFAnnotList annots = currentPage.getAnnotations();
  718. if (annots != null) {
  719. this.pdfDoc.addObject(annots);
  720. }
  721. this.pdfDoc.addObject(currentPage);
  722. this.pdfDoc.output(ostream);
  723. this.textutil = null;
  724. }
  725. /** {@inheritDoc} */
  726. protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
  727. saveGraphicsState();
  728. // Set the given CTM in the graphics state
  729. currentState.concatenate(
  730. new AffineTransform(CTMHelper.toPDFArray(ctm)));
  731. if (clippingRect != null) {
  732. clipRect((float)clippingRect.getX() / 1000f,
  733. (float)clippingRect.getY() / 1000f,
  734. (float)clippingRect.getWidth() / 1000f,
  735. (float)clippingRect.getHeight() / 1000f);
  736. }
  737. // multiply with current CTM
  738. currentStream.add(CTMHelper.toPDFString(ctm) + " cm\n");
  739. }
  740. /** {@inheritDoc} */
  741. protected void endVParea() {
  742. restoreGraphicsState();
  743. }
  744. /** {@inheritDoc} */
  745. protected void concatenateTransformationMatrix(AffineTransform at) {
  746. if (!at.isIdentity()) {
  747. currentState.concatenate(at);
  748. currentStream.add(CTMHelper.toPDFString(at, false) + " cm\n");
  749. }
  750. }
  751. /**
  752. * Formats a float value (normally coordinates) as Strings.
  753. * @param value the value
  754. * @return the formatted value
  755. */
  756. protected static String format(float value) {
  757. return PDFNumber.doubleOut(value);
  758. }
  759. /** {@inheritDoc} */
  760. protected void drawBorderLine(float x1, float y1, float x2, float y2,
  761. boolean horz, boolean startOrBefore, int style, Color col) {
  762. float w = x2 - x1;
  763. float h = y2 - y1;
  764. if ((w < 0) || (h < 0)) {
  765. log.error("Negative extent received (w=" + w + ", h=" + h
  766. + "). Border won't be painted.");
  767. return;
  768. }
  769. switch (style) {
  770. case Constants.EN_DASHED:
  771. setColor(col, false, null);
  772. if (horz) {
  773. float unit = Math.abs(2 * h);
  774. int rep = (int)(w / unit);
  775. if (rep % 2 == 0) {
  776. rep++;
  777. }
  778. unit = w / rep;
  779. currentStream.add("[" + format(unit) + "] 0 d ");
  780. currentStream.add(format(h) + " w\n");
  781. float ym = y1 + (h / 2);
  782. currentStream.add(format(x1) + " " + format(ym) + " m "
  783. + format(x2) + " " + format(ym) + " l S\n");
  784. } else {
  785. float unit = Math.abs(2 * w);
  786. int rep = (int)(h / unit);
  787. if (rep % 2 == 0) {
  788. rep++;
  789. }
  790. unit = h / rep;
  791. currentStream.add("[" + format(unit) + "] 0 d ");
  792. currentStream.add(format(w) + " w\n");
  793. float xm = x1 + (w / 2);
  794. currentStream.add(format(xm) + " " + format(y1) + " m "
  795. + format(xm) + " " + format(y2) + " l S\n");
  796. }
  797. break;
  798. case Constants.EN_DOTTED:
  799. setColor(col, false, null);
  800. currentStream.add("1 J ");
  801. if (horz) {
  802. float unit = Math.abs(2 * h);
  803. int rep = (int)(w / unit);
  804. if (rep % 2 == 0) {
  805. rep++;
  806. }
  807. unit = w / rep;
  808. currentStream.add("[0 " + format(unit) + "] 0 d ");
  809. currentStream.add(format(h) + " w\n");
  810. float ym = y1 + (h / 2);
  811. currentStream.add(format(x1) + " " + format(ym) + " m "
  812. + format(x2) + " " + format(ym) + " l S\n");
  813. } else {
  814. float unit = Math.abs(2 * w);
  815. int rep = (int)(h / unit);
  816. if (rep % 2 == 0) {
  817. rep++;
  818. }
  819. unit = h / rep;
  820. currentStream.add("[0 " + format(unit) + " ] 0 d ");
  821. currentStream.add(format(w) + " w\n");
  822. float xm = x1 + (w / 2);
  823. currentStream.add(format(xm) + " " + format(y1) + " m "
  824. + format(xm) + " " + format(y2) + " l S\n");
  825. }
  826. break;
  827. case Constants.EN_DOUBLE:
  828. setColor(col, false, null);
  829. currentStream.add("[] 0 d ");
  830. if (horz) {
  831. float h3 = h / 3;
  832. currentStream.add(format(h3) + " w\n");
  833. float ym1 = y1 + (h3 / 2);
  834. float ym2 = ym1 + h3 + h3;
  835. currentStream.add(format(x1) + " " + format(ym1) + " m "
  836. + format(x2) + " " + format(ym1) + " l S\n");
  837. currentStream.add(format(x1) + " " + format(ym2) + " m "
  838. + format(x2) + " " + format(ym2) + " l S\n");
  839. } else {
  840. float w3 = w / 3;
  841. currentStream.add(format(w3) + " w\n");
  842. float xm1 = x1 + (w3 / 2);
  843. float xm2 = xm1 + w3 + w3;
  844. currentStream.add(format(xm1) + " " + format(y1) + " m "
  845. + format(xm1) + " " + format(y2) + " l S\n");
  846. currentStream.add(format(xm2) + " " + format(y1) + " m "
  847. + format(xm2) + " " + format(y2) + " l S\n");
  848. }
  849. break;
  850. case Constants.EN_GROOVE:
  851. case Constants.EN_RIDGE:
  852. {
  853. float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
  854. currentStream.add("[] 0 d ");
  855. if (horz) {
  856. Color uppercol = ColorUtil.lightenColor(col, -colFactor);
  857. Color lowercol = ColorUtil.lightenColor(col, colFactor);
  858. float h3 = h / 3;
  859. currentStream.add(format(h3) + " w\n");
  860. float ym1 = y1 + (h3 / 2);
  861. setColor(uppercol, false, null);
  862. currentStream.add(format(x1) + " " + format(ym1) + " m "
  863. + format(x2) + " " + format(ym1) + " l S\n");
  864. setColor(col, false, null);
  865. currentStream.add(format(x1) + " " + format(ym1 + h3) + " m "
  866. + format(x2) + " " + format(ym1 + h3) + " l S\n");
  867. setColor(lowercol, false, null);
  868. currentStream.add(format(x1) + " " + format(ym1 + h3 + h3) + " m "
  869. + format(x2) + " " + format(ym1 + h3 + h3) + " l S\n");
  870. } else {
  871. Color leftcol = ColorUtil.lightenColor(col, -colFactor);
  872. Color rightcol = ColorUtil.lightenColor(col, colFactor);
  873. float w3 = w / 3;
  874. currentStream.add(format(w3) + " w\n");
  875. float xm1 = x1 + (w3 / 2);
  876. setColor(leftcol, false, null);
  877. currentStream.add(format(xm1) + " " + format(y1) + " m "
  878. + format(xm1) + " " + format(y2) + " l S\n");
  879. setColor(col, false, null);
  880. currentStream.add(format(xm1 + w3) + " " + format(y1) + " m "
  881. + format(xm1 + w3) + " " + format(y2) + " l S\n");
  882. setColor(rightcol, false, null);
  883. currentStream.add(format(xm1 + w3 + w3) + " " + format(y1) + " m "
  884. + format(xm1 + w3 + w3) + " " + format(y2) + " l S\n");
  885. }
  886. break;
  887. }
  888. case Constants.EN_INSET:
  889. case Constants.EN_OUTSET:
  890. {
  891. float colFactor = (style == EN_OUTSET ? 0.4f : -0.4f);
  892. currentStream.add("[] 0 d ");
  893. Color c = col;
  894. if (horz) {
  895. c = ColorUtil.lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  896. currentStream.add(format(h) + " w\n");
  897. float ym1 = y1 + (h / 2);
  898. setColor(c, false, null);
  899. currentStream.add(format(x1) + " " + format(ym1) + " m "
  900. + format(x2) + " " + format(ym1) + " l S\n");
  901. } else {
  902. c = ColorUtil.lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  903. currentStream.add(format(w) + " w\n");
  904. float xm1 = x1 + (w / 2);
  905. setColor(c, false, null);
  906. currentStream.add(format(xm1) + " " + format(y1) + " m "
  907. + format(xm1) + " " + format(y2) + " l S\n");
  908. }
  909. break;
  910. }
  911. case Constants.EN_HIDDEN:
  912. break;
  913. default:
  914. setColor(col, false, null);
  915. currentStream.add("[] 0 d ");
  916. if (horz) {
  917. currentStream.add(format(h) + " w\n");
  918. float ym = y1 + (h / 2);
  919. currentStream.add(format(x1) + " " + format(ym) + " m "
  920. + format(x2) + " " + format(ym) + " l S\n");
  921. } else {
  922. currentStream.add(format(w) + " w\n");
  923. float xm = x1 + (w / 2);
  924. currentStream.add(format(xm) + " " + format(y1) + " m "
  925. + format(xm) + " " + format(y2) + " l S\n");
  926. }
  927. }
  928. }
  929. /**
  930. * Sets the current line width in points.
  931. * @param width line width in points
  932. */
  933. private void updateLineWidth(float width) {
  934. if (currentState.setLineWidth(width)) {
  935. //Only write if value has changed WRT the current line width
  936. currentStream.add(format(width) + " w\n");
  937. }
  938. }
  939. /** {@inheritDoc} */
  940. protected void clipRect(float x, float y, float width, float height) {
  941. currentStream.add(format(x) + " " + format(y) + " "
  942. + format(width) + " " + format(height) + " re ");
  943. clip();
  944. }
  945. /**
  946. * Clip an area.
  947. */
  948. protected void clip() {
  949. currentStream.add("W\n");
  950. currentStream.add("n\n");
  951. }
  952. /**
  953. * Moves the current point to (x, y), omitting any connecting line segment.
  954. * @param x x coordinate
  955. * @param y y coordinate
  956. */
  957. protected void moveTo(float x, float y) {
  958. currentStream.add(format(x) + " " + format(y) + " m ");
  959. }
  960. /**
  961. * Appends a straight line segment from the current point to (x, y). The
  962. * new current point is (x, y).
  963. * @param x x coordinate
  964. * @param y y coordinate
  965. */
  966. protected void lineTo(float x, float y) {
  967. currentStream.add(format(x) + " " + format(y) + " l ");
  968. }
  969. /**
  970. * Closes the current subpath by appending a straight line segment from
  971. * the current point to the starting point of the subpath.
  972. */
  973. protected void closePath() {
  974. currentStream.add("h ");
  975. }
  976. /**
  977. * {@inheritDoc}
  978. */
  979. protected void fillRect(float x, float y, float width, float height) {
  980. if (width > 0 && height > 0) {
  981. currentStream.add(format(x) + " " + format(y) + " "
  982. + format(width) + " " + format(height) + " re f\n");
  983. }
  984. }
  985. /**
  986. * Draw a line.
  987. *
  988. * @param startx the start x position
  989. * @param starty the start y position
  990. * @param endx the x end position
  991. * @param endy the y end position
  992. */
  993. private void drawLine(float startx, float starty, float endx, float endy) {
  994. currentStream.add(format(startx) + " " + format(starty) + " m ");
  995. currentStream.add(format(endx) + " " + format(endy) + " l S\n");
  996. }
  997. /**
  998. * Breaks out of the state stack to handle fixed block-containers.
  999. * @return the saved state stack to recreate later
  1000. */
  1001. protected List breakOutOfStateStack() {
  1002. // return currentState.popAll();
  1003. List breakOutList = new java.util.ArrayList();
  1004. AbstractData data;
  1005. while (true) {
  1006. data = currentState.getData();
  1007. if (currentState.pop() == null) {
  1008. break;
  1009. }
  1010. if (breakOutList.size() == 0) {
  1011. comment("------ break out!");
  1012. }
  1013. breakOutList.add(0, data); //Insert because of stack-popping
  1014. restoreGraphicsState(false);
  1015. }
  1016. return breakOutList;
  1017. }
  1018. /**
  1019. * Restores the state stack after a break out.
  1020. * @param breakOutList the state stack to restore.
  1021. */
  1022. protected void restoreStateStackAfterBreakOut(List breakOutList) {
  1023. comment("------ restoring context after break-out...");
  1024. // currentState.pushAll(breakOutList);
  1025. AbstractData data;
  1026. Iterator i = breakOutList.iterator();
  1027. while (i.hasNext()) {
  1028. data = (AbstractData)i.next();
  1029. saveGraphicsState();
  1030. AffineTransform at = data.getTransform();
  1031. concatenateTransformationMatrix(at);
  1032. //TODO Break-out: Also restore items such as line width and color
  1033. //Left out for now because all this painting stuff is very
  1034. //inconsistent. Some values go over PDFState, some don't.
  1035. }
  1036. comment("------ done.");
  1037. }
  1038. /**
  1039. * Returns area's id if it is the first area in the document with that id
  1040. * (i.e. if the area qualifies as a link target).
  1041. * Otherwise, or if the area has no id, null is returned.
  1042. *
  1043. * <i>NOTE</i>: area must be on currentPageViewport, otherwise result may be wrong!
  1044. *
  1045. * @param area the area for which to return the id
  1046. * @return the area's id (null if the area has no id or
  1047. * other preceding areas have the same id)
  1048. */
  1049. protected String getTargetableID(Area area) {
  1050. String id = (String) area.getTrait(Trait.PROD_ID);
  1051. if (id == null || id.length() == 0
  1052. || !currentPageViewport.isFirstWithID(id)
  1053. || idPositions.containsKey(id)) {
  1054. return null;
  1055. } else {
  1056. return id;
  1057. }
  1058. }
  1059. /**
  1060. * Set XY position in the PDFGoTo and add it to the PDF trailer.
  1061. *
  1062. * @param gt the PDFGoTo object
  1063. * @param position the X,Y position to set
  1064. */
  1065. protected void finishIDGoTo(PDFGoTo gt, Point2D.Float position) {
  1066. gt.setPosition(position);
  1067. pdfDoc.addTrailerObject(gt);
  1068. unfinishedGoTos.remove(gt);
  1069. }
  1070. /**
  1071. * Set page reference and XY position in the PDFGoTo and add it to the PDF trailer.
  1072. *
  1073. * @param gt the PDFGoTo object
  1074. * @param pdfPageRef the PDF reference string of the target page object
  1075. * @param position the X,Y position to set
  1076. */
  1077. protected void finishIDGoTo(PDFGoTo gt, String pdfPageRef, Point2D.Float position) {
  1078. gt.setPageReference(pdfPageRef);
  1079. finishIDGoTo(gt, position);
  1080. }
  1081. /**
  1082. * Get a PDFGoTo pointing to the given id. Create one if necessary.
  1083. * It is possible that the PDFGoTo is not fully resolved yet. In that case
  1084. * it must be completed (and added to the PDF trailer) later.
  1085. *
  1086. * @param targetID the target id of the PDFGoTo
  1087. * @param pvKey the unique key of the target PageViewport
  1088. *
  1089. * @return the PDFGoTo that was found or created
  1090. */
  1091. protected PDFGoTo getPDFGoToForID(String targetID, String pvKey) {
  1092. // Already a PDFGoTo present for this target? If not, create.
  1093. PDFGoTo gt = (PDFGoTo) idGoTos.get(targetID);
  1094. if (gt == null) {
  1095. String pdfPageRef = (String) pageReferences.get(pvKey);
  1096. Point2D.Float position = (Point2D.Float) idPositions.get(targetID);
  1097. // can the GoTo already be fully filled in?
  1098. if (pdfPageRef != null && position != null) {
  1099. // getPDFGoTo shares PDFGoTo objects as much as possible.
  1100. // It also takes care of assignObjectNumber and addTrailerObject.
  1101. gt = pdfDoc.getFactory().getPDFGoTo(pdfPageRef, position);
  1102. } else {
  1103. // Not complete yet, can't use getPDFGoTo:
  1104. gt = new PDFGoTo(pdfPageRef);
  1105. pdfDoc.assignObjectNumber(gt);
  1106. // pdfDoc.addTrailerObject() will be called later, from finishIDGoTo()
  1107. unfinishedGoTos.add(gt);
  1108. }
  1109. idGoTos.put(targetID, gt);
  1110. }
  1111. return gt;
  1112. }
  1113. /**
  1114. * Saves id's absolute position on page for later retrieval by PDFGoTos
  1115. *
  1116. * @param id the id of the area whose position must be saved
  1117. * @param pdfPageRef the PDF page reference string
  1118. * @param relativeIPP the *relative* IP position in millipoints
  1119. * @param relativeBPP the *relative* BP position in millipoints
  1120. * @param tf the transformation to apply once the relative positions have been
  1121. * converted to points
  1122. */
  1123. protected void saveAbsolutePosition(String id, String pdfPageRef,
  1124. int relativeIPP, int relativeBPP, AffineTransform tf) {
  1125. Point2D.Float position = new Point2D.Float(relativeIPP / 1000f, relativeBPP / 1000f);
  1126. tf.transform(position, position);
  1127. idPositions.put(id, position);
  1128. // is there already a PDFGoTo waiting to be completed?
  1129. PDFGoTo gt = (PDFGoTo) idGoTos.get(id);
  1130. if (gt != null) {
  1131. finishIDGoTo(gt, pdfPageRef, position);
  1132. }
  1133. /*
  1134. // The code below auto-creates a named destination for every id in the document.
  1135. // This should probably be controlled by a user-configurable setting, as it may
  1136. // make the PDF file grow noticeably.
  1137. // *** NOT YET WELL-TESTED ! ***
  1138. if (true) {
  1139. PDFFactory factory = pdfDoc.getFactory();
  1140. if (gt == null) {
  1141. gt = factory.getPDFGoTo(pdfPageRef, position);
  1142. idGoTos.put(id, gt); // so others can pick it up too
  1143. }
  1144. factory.makeDestination(id, gt.referencePDF(), currentPageViewport);
  1145. // Note: using currentPageViewport is only correct if the id is indeed on
  1146. // the current PageViewport. But even if incorrect, it won't interfere with
  1147. // what gets created in the PDF.
  1148. // For speedup, we should also create a lookup map id -> PDFDestination
  1149. }
  1150. */
  1151. }
  1152. /**
  1153. * Saves id's absolute position on page for later retrieval by PDFGoTos,
  1154. * using the currently valid transformation and the currently valid PDF page reference
  1155. *
  1156. * @param id the id of the area whose position must be saved
  1157. * @param relativeIPP the *relative* IP position in millipoints
  1158. * @param relativeBPP the *relative* BP position in millipoints
  1159. */
  1160. protected void saveAbsolutePosition(String id, int relativeIPP, int relativeBPP) {
  1161. saveAbsolutePosition(id, currentPageRef,
  1162. relativeIPP, relativeBPP, currentState.getTransform());
  1163. }
  1164. /**
  1165. * If the given block area is a possible link target, its id + absolute position will
  1166. * be saved. The saved position is only correct if this function is called at the very
  1167. * start of renderBlock!
  1168. *
  1169. * @param block the block area in question
  1170. */
  1171. protected void saveBlockPosIfTargetable(Block block) {
  1172. String id = getTargetableID(block);
  1173. if (id != null) {
  1174. // FIXME: Like elsewhere in the renderer code, absolute and relative
  1175. // directions are happily mixed here. This makes sure that the
  1176. // links point to the right location, but it is not correct.
  1177. int ipp = block.getXOffset();
  1178. int bpp = block.getYOffset() + block.getSpaceBefore();
  1179. int positioning = block.getPositioning();
  1180. if (!(positioning == Block.FIXED || positioning == Block.ABSOLUTE)) {
  1181. ipp += currentIPPosition;
  1182. bpp += currentBPPosition;
  1183. }
  1184. AffineTransform tf = positioning == Block.FIXED
  1185. ? currentState.getBaseTransform()
  1186. : currentState.getTransform();
  1187. saveAbsolutePosition(id, currentPageRef, ipp, bpp, tf);
  1188. }
  1189. }
  1190. /**
  1191. * If the given inline area is a possible link target, its id + absolute position will
  1192. * be saved. The saved position is only correct if this function is called at the very
  1193. * start of renderInlineArea!
  1194. *
  1195. * @param inlineArea the inline area in question
  1196. */
  1197. protected void saveInlinePosIfTargetable(InlineArea inlineArea) {
  1198. String id = getTargetableID(inlineArea);
  1199. if (id != null) {
  1200. int extraMarginBefore = 5000; // millipoints
  1201. int ipp = currentIPPosition;
  1202. int bpp = currentBPPosition + inlineArea.getOffset() - extraMarginBefore;
  1203. saveAbsolutePosition(id, ipp, bpp);
  1204. }
  1205. }
  1206. /**
  1207. * {@inheritDoc}
  1208. */
  1209. protected void renderBlock(Block block) {
  1210. saveBlockPosIfTargetable(block);
  1211. super.renderBlock(block);
  1212. }
  1213. /** {@inheritDoc} */
  1214. protected void renderLineArea(LineArea line) {
  1215. super.renderLineArea(line);
  1216. }
  1217. /**
  1218. * {@inheritDoc}
  1219. */
  1220. protected void renderInlineArea(InlineArea inlineArea) {
  1221. saveInlinePosIfTargetable(inlineArea);
  1222. super.renderInlineArea(inlineArea);
  1223. }
  1224. /**
  1225. * Render inline parent area.
  1226. * For pdf this handles the inline parent area traits such as
  1227. * links, border, background.
  1228. * @param ip the inline parent area
  1229. */
  1230. public void renderInlineParent(InlineParent ip) {
  1231. boolean annotsAllowed = pdfDoc.getProfile().isAnnotationAllowed();
  1232. // stuff we only need if a link must be created:
  1233. Rectangle2D ipRect = null;
  1234. PDFFactory factory = null;
  1235. PDFAction action = null;
  1236. if (annotsAllowed) {
  1237. // make sure the rect is determined *before* calling super!
  1238. int ipp = currentIPPosition;
  1239. int bpp = currentBPPosition + ip.getOffset();
  1240. ipRect = new Rectangle2D.Float(ipp / 1000f, bpp / 1000f,
  1241. ip.getIPD() / 1000f, ip.getBPD() / 1000f);
  1242. AffineTransform transform = currentState.getTransform();
  1243. ipRect = transform.createTransformedShape(ipRect).getBounds2D();
  1244. factory = pdfDoc.getFactory();
  1245. }
  1246. // render contents
  1247. super.renderInlineParent(ip);
  1248. boolean linkTraitFound = false;
  1249. // try INTERNAL_LINK first
  1250. Trait.InternalLink intLink = (Trait.InternalLink) ip.getTrait(Trait.INTERNAL_LINK);
  1251. if (intLink != null) {
  1252. linkTraitFound = true;
  1253. String pvKey = intLink.getPVKey();
  1254. String idRef = intLink.getIDRef();
  1255. boolean pvKeyOK = pvKey != null && pvKey.length() > 0;
  1256. boolean idRefOK = idRef != null && idRef.length() > 0;
  1257. if (pvKeyOK && idRefOK) {
  1258. if (annotsAllowed) {
  1259. action = getPDFGoToForID(idRef, pvKey);
  1260. }
  1261. } else {
  1262. //Warnings already issued by AreaTreeHandler
  1263. }
  1264. }
  1265. // no INTERNAL_LINK, look for EXTERNAL_LINK
  1266. if (!linkTraitFound) {
  1267. Trait.ExternalLink extLink = (Trait.ExternalLink) ip.getTrait(Trait.EXTERNAL_LINK);
  1268. if (extLink != null) {
  1269. String extDest = extLink.getDestination();
  1270. if (extDest != null && extDest.length() > 0) {
  1271. linkTraitFound = true;
  1272. if (annotsAllowed) {
  1273. action = factory.getExternalAction(extDest, extLink.newWindow());
  1274. }
  1275. }
  1276. }
  1277. }
  1278. // warn if link trait found but not allowed, else create link
  1279. if (linkTraitFound) {
  1280. if (!annotsAllowed) {
  1281. log.warn("Skipping annotation for a link due to PDF profile: "
  1282. + pdfDoc.getProfile());
  1283. } else if (action != null) {
  1284. PDFLink pdfLink = factory.makeLink(ipRect, action);
  1285. currentPage.addAnnotation(pdfLink);
  1286. }
  1287. }
  1288. }
  1289. private Typeface getTypeface(String fontName) {
  1290. Typeface tf = (Typeface) fontInfo.getFonts().get(fontName);
  1291. if (tf instanceof LazyFont) {
  1292. tf = ((LazyFont)tf).getRealFont();
  1293. }
  1294. return tf;
  1295. }
  1296. /** {@inheritDoc} */
  1297. public void renderText(TextArea text) {
  1298. renderInlineAreaBackAndBorders(text);
  1299. Color ct = (Color) text.getTrait(Trait.COLOR);
  1300. updateColor(ct, true);
  1301. beginTextObject();
  1302. String fontName = getInternalFontNameForArea(text);
  1303. int size = ((Integer) text.getTrait(Trait.FONT_SIZE)).intValue();
  1304. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  1305. Typeface tf = getTypeface(fontName);
  1306. textutil.updateTf(fontName, size / 1000f, tf.isMultiByte());
  1307. // word.getOffset() = only height of text itself
  1308. // currentBlockIPPosition: 0 for beginning of line; nonzero
  1309. // where previous line area failed to take up entire allocated space
  1310. int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  1311. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  1312. textutil.writeTextMatrix(new AffineTransform(1, 0, 0, -1, rx / 1000f, bl / 1000f));
  1313. super.renderText(text);
  1314. textutil.writeTJ();
  1315. renderTextDecoration(tf, size, text, bl, rx);
  1316. }
  1317. /** {@inheritDoc} */
  1318. public void renderWord(WordArea word) {
  1319. Font font = getFontFromArea(word.getParentArea());
  1320. String s = word.getWord();
  1321. escapeText(s, word.getLetterAdjustArray(),
  1322. font, (AbstractTextArea)word.getParentArea());
  1323. super.renderWord(word);
  1324. }
  1325. /** {@inheritDoc} */
  1326. public void renderSpace(SpaceArea space) {
  1327. Font font = getFontFromArea(space.getParentArea());
  1328. String s = space.getSpace();
  1329. AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
  1330. escapeText(s, null, font, textArea);
  1331. if (space.isAdjustable()) {
  1332. int tws = -((TextArea) space.getParentArea()).getTextWordSpaceAdjust()
  1333. - 2 * textArea.getTextLetterSpaceAdjust();
  1334. if (tws != 0) {
  1335. float adjust = tws / (font.getFontSize() / 1000f);
  1336. textutil.adjustGlyphTJ(adjust);
  1337. }
  1338. }
  1339. super.renderSpace(space);
  1340. }
  1341. /**
  1342. * Escapes text according to PDF rules.
  1343. * @param s Text to escape
  1344. * @param letterAdjust an array of widths for letter adjustment (may be null)
  1345. * @param font to font in use
  1346. * @param parentArea the parent text area to retrieve certain traits from
  1347. */
  1348. protected void escapeText(String s,
  1349. int[] letterAdjust,
  1350. Font font, AbstractTextArea parentArea) {
  1351. escapeText(s, 0, s.length(), letterAdjust, font, parentArea);
  1352. }
  1353. /**
  1354. * Escapes text according to PDF rules.
  1355. * @param s Text to escape
  1356. * @param start the start position in the text
  1357. * @param end the end position in the text
  1358. * @param letterAdjust an array of widths for letter adjustment (may be null)
  1359. * @param font to font in use
  1360. * @param parentArea the parent text area to retrieve certain traits from
  1361. */
  1362. protected void escapeText(String s, int start, int end,
  1363. int[] letterAdjust,
  1364. Font font, AbstractTextArea parentArea) {
  1365. String fontName = font.getFontName();
  1366. float fontSize = font.getFontSize() / 1000f;
  1367. Typeface tf = getTypeface(fontName);
  1368. SingleByteFont singleByteFont = null;
  1369. if (tf instanceof SingleByteFont) {
  1370. singleByteFont = (SingleByteFont)tf;
  1371. }
  1372. int l = s.length();
  1373. for (int i = start; i < end; i++) {
  1374. char orgChar = s.charAt(i);
  1375. char ch;
  1376. float glyphAdjust = 0;
  1377. if (font.hasChar(orgChar)) {
  1378. ch = font.mapChar(orgChar);
  1379. if (singleByteFont != null && singleByteFont.hasAdditionalEncodings()) {
  1380. int encoding = ch / 256;
  1381. if (encoding == 0) {
  1382. textutil.updateTf(fontName, fontSize, tf.isMultiByte());
  1383. } else {
  1384. textutil.updateTf(fontName + "_" + Integer.toString(encoding),
  1385. fontSize, tf.isMultiByte());
  1386. ch = (char)(ch % 256);
  1387. }
  1388. }
  1389. int tls = (i < l - 1 ? parentArea.getTextLetterSpaceAdjust() : 0);
  1390. glyphAdjust -= tls;
  1391. } else {
  1392. if (CharUtilities.isFixedWidthSpace(orgChar)) {
  1393. //Fixed width space are rendered as spaces so copy/paste works in a reader
  1394. ch = font.mapChar(CharUtilities.SPACE);
  1395. glyphAdjust = font.getCharWidth(ch) - font.getCharWidth(orgChar);
  1396. } else {
  1397. ch = font.mapChar(orgChar);
  1398. }
  1399. }
  1400. if (letterAdjust != null && i < l - 1) {
  1401. glyphAdjust -= letterAdjust[i + 1];
  1402. }
  1403. textutil.writeTJMappedChar(ch);
  1404. float adjust = glyphAdjust / fontSize;
  1405. if (adjust != 0) {
  1406. textutil.adjustGlyphTJ(adjust);
  1407. }
  1408. }
  1409. }
  1410. /**
  1411. * Establishes a new foreground or fill color. In contrast to updateColor
  1412. * this method does not check the PDFState for optimization possibilities.
  1413. * @param col the color to apply
  1414. * @param fill true to set the fill color, false for the foreground color
  1415. * @param pdf StringBuffer to write the PDF code to, if null, the code is
  1416. * written to the current stream.
  1417. */
  1418. protected void setColor(Color col, boolean fill, StringBuffer pdf) {
  1419. PDFColor color = new PDFColor(this.pdfDoc, col);
  1420. if (pdf != null) {
  1421. pdf.append(color.getColorSpaceOut(fill));
  1422. } else {
  1423. currentStream.add(color.getColorSpaceOut(fill));
  1424. }
  1425. }
  1426. /**
  1427. * Establishes a new foreground or fill color.
  1428. * @param col the color to apply (null skips this operation)
  1429. * @param fill true to set the fill color, false for the foreground color
  1430. * @param pdf StringBuffer to write the PDF code to, if null, the code is
  1431. * written to the current stream.
  1432. */
  1433. private void updateColor(Color col, boolean fill, StringBuffer pdf) {
  1434. if (col == null) {
  1435. return;
  1436. }
  1437. boolean update = false;
  1438. if (fill) {
  1439. update = currentState.setBackColor(col);
  1440. } else {
  1441. update = currentState.setColor(col);
  1442. }
  1443. if (update) {
  1444. setColor(col, fill, pdf);
  1445. }
  1446. }
  1447. /** {@inheritDoc} */
  1448. protected void updateColor(Color col, boolean fill) {
  1449. updateColor(col, fill, null);
  1450. }
  1451. /** {@inheritDoc} */
  1452. public void renderImage(Image image, Rectangle2D pos) {
  1453. endTextObject();
  1454. String url = image.getURL();
  1455. putImage(url, pos, image.getForeignAttributes());
  1456. }
  1457. /** {@inheritDoc} */
  1458. protected void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
  1459. endTextObject();
  1460. putImage(url, pos, foreignAttributes);
  1461. }
  1462. /**
  1463. * Adds a PDF XObject (a bitmap or form) to the PDF that will later be referenced.
  1464. * @param uri URL of the bitmap
  1465. * @param pos Position of the bitmap
  1466. * @deprecated Use {@link #putImage(String, Rectangle2D, Map)} instead.
  1467. */
  1468. protected void putImage(String uri, Rectangle2D pos) {
  1469. putImage(uri, pos, null);
  1470. }
  1471. /**
  1472. * Adds a PDF XObject (a bitmap or form) to the PDF that will later be referenced.
  1473. * @param uri URL of the bitmap
  1474. * @param pos Position of the bitmap
  1475. * @param foreignAttributes foreign attributes associated with the image
  1476. */
  1477. protected void putImage(String uri, Rectangle2D pos, Map foreignAttributes) {
  1478. Rectangle posInt = new Rectangle(
  1479. (int)pos.getX(),
  1480. (int)pos.getY(),
  1481. (int)pos.getWidth(),
  1482. (int)pos.getHeight());
  1483. uri = URISpecification.getURL(uri);
  1484. PDFXObject xobject = pdfDoc.getXObject(uri);
  1485. if (xobject != null) {
  1486. float w = (float) pos.getWidth() / 1000f;
  1487. float h = (float) pos.getHeight() / 1000f;
  1488. placeImage((float)pos.getX() / 1000f,
  1489. (float)pos.getY() / 1000f, w, h, xobject);
  1490. return;
  1491. }
  1492. Point origin = new Point(currentIPPosition, currentBPPosition);
  1493. int x = origin.x + posInt.x;
  1494. int y = origin.y + posInt.y;
  1495. ImageManager manager = getUserAgent().getFactory().getImageManager();
  1496. ImageInfo info = null;
  1497. try {
  1498. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  1499. info = manager.getImageInfo(uri, sessionContext);
  1500. Map hints = ImageUtil.getDefaultHints(sessionContext);
  1501. org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
  1502. info, imageHandlerRegistry.getSupportedFlavors(), hints, sessionContext);
  1503. //First check for a dynamically registered handler
  1504. PDFImageHandler handler = imageHandlerRegistry.getHandler(img.getClass());
  1505. if (handler != null) {
  1506. if (log.isDebugEnabled()) {
  1507. log.debug("Using PDFImageHandler: " + handler.getClass().getName());
  1508. }
  1509. try {
  1510. RendererContext context = createRendererContext(
  1511. x, y, posInt.width, posInt.height, foreignAttributes);
  1512. handler.generateImage(context, img, origin, posInt);
  1513. } catch (IOException ioe) {
  1514. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1515. getUserAgent().getEventBroadcaster());
  1516. eventProducer.imageWritingError(this, ioe);
  1517. return;
  1518. }
  1519. } else {
  1520. throw new UnsupportedOperationException(
  1521. "No PDFImageHandler available for image: "
  1522. + info + " (" + img.getClass().getName() + ")");
  1523. }
  1524. } catch (ImageException ie) {
  1525. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1526. getUserAgent().getEventBroadcaster());
  1527. eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
  1528. } catch (FileNotFoundException fe) {
  1529. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1530. getUserAgent().getEventBroadcaster());
  1531. eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
  1532. } catch (IOException ioe) {
  1533. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1534. getUserAgent().getEventBroadcaster());
  1535. eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
  1536. }
  1537. // output new data
  1538. try {
  1539. this.pdfDoc.output(ostream);
  1540. } catch (IOException ioe) {
  1541. // ioexception will be caught later
  1542. log.error(ioe.getMessage());
  1543. }
  1544. }
  1545. /**
  1546. * Places a previously registered image at a certain place on the page.
  1547. * @param x X coordinate
  1548. * @param y Y coordinate
  1549. * @param w width for image
  1550. * @param h height for image
  1551. * @param xobj the image XObject
  1552. */
  1553. public void placeImage(float x, float y, float w, float h, PDFXObject xobj) {
  1554. saveGraphicsState();
  1555. currentStream.add(format(w) + " 0 0 "
  1556. + format(-h) + " "
  1557. + format(currentIPPosition / 1000f + x) + " "
  1558. + format(currentBPPosition / 1000f + h + y)
  1559. + " cm\n" + xobj.getName() + " Do\n");
  1560. restoreGraphicsState();
  1561. }
  1562. /** {@inheritDoc} */
  1563. protected RendererContext createRendererContext(int x, int y, int width, int height,
  1564. Map foreignAttributes) {
  1565. RendererContext context = super.createRendererContext(
  1566. x, y, width, height, foreignAttributes);
  1567. context.setProperty(PDFRendererContextConstants.PDF_DOCUMENT, pdfDoc);
  1568. context.setProperty(PDFRendererContextConstants.OUTPUT_STREAM, ostream);
  1569. context.setProperty(PDFRendererContextConstants.PDF_STATE, currentState);
  1570. context.setProperty(PDFRendererContextConstants.PDF_PAGE, currentPage);
  1571. context.setProperty(PDFRendererContextConstants.PDF_CONTEXT,
  1572. currentContext == null ? currentPage : currentContext);
  1573. context.setProperty(PDFRendererContextConstants.PDF_CONTEXT, currentContext);
  1574. context.setProperty(PDFRendererContextConstants.PDF_STREAM, currentStream);
  1575. context.setProperty(PDFRendererContextConstants.PDF_FONT_INFO, fontInfo);
  1576. context.setProperty(PDFRendererContextConstants.PDF_FONT_NAME, "");
  1577. context.setProperty(PDFRendererContextConstants.PDF_FONT_SIZE, new Integer(0));
  1578. return context;
  1579. }
  1580. /**
  1581. * Render leader area.
  1582. * This renders a leader area which is an area with a rule.
  1583. * @param area the leader area to render
  1584. */
  1585. public void renderLeader(Leader area) {
  1586. renderInlineAreaBackAndBorders(area);
  1587. currentState.push();
  1588. saveGraphicsState();
  1589. int style = area.getRuleStyle();
  1590. float startx = (currentIPPosition + area.getBorderAndPaddingWidthStart()) / 1000f;
  1591. float starty = (currentBPPosition + area.getOffset()) / 1000f;
  1592. float endx = (currentIPPosition + area.getBorderAndPaddingWidthStart()
  1593. + area.getIPD()) / 1000f;
  1594. float ruleThickness = area.getRuleThickness() / 1000f;
  1595. Color col = (Color)area.getTrait(Trait.COLOR);
  1596. switch (style) {
  1597. case EN_SOLID:
  1598. case EN_DASHED:
  1599. case EN_DOUBLE:
  1600. drawBorderLine(startx, starty, endx, starty + ruleThickness,
  1601. true, true, style, col);
  1602. break;
  1603. case EN_DOTTED:
  1604. clipRect(startx, starty, endx - startx, ruleThickness);
  1605. //This displaces the dots to the right by half a dot's width
  1606. //TODO There's room for improvement here
  1607. currentStream.add("1 0 0 1 " + format(ruleThickness / 2) + " 0 cm\n");
  1608. drawBorderLine(startx, starty, endx, starty + ruleThickness,
  1609. true, true, style, col);
  1610. break;
  1611. case EN_GROOVE:
  1612. case EN_RIDGE:
  1613. float half = area.getRuleThickness() / 2000f;
  1614. setColor(ColorUtil.lightenColor(col, 0.6f), true, null);
  1615. currentStream.add(format(startx) + " " + format(starty) + " m\n");
  1616. currentStream.add(format(endx) + " " + format(starty) + " l\n");
  1617. currentStream.add(format(endx) + " " + format(starty + 2 * half) + " l\n");
  1618. currentStream.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
  1619. currentStream.add("h\n");
  1620. currentStream.add("f\n");
  1621. setColor(col, true, null);
  1622. if (style == EN_GROOVE) {
  1623. currentStream.add(format(startx) + " " + format(starty) + " m\n");
  1624. currentStream.add(format(endx) + " " + format(starty) + " l\n");
  1625. currentStream.add(format(endx) + " " + format(starty + half) + " l\n");
  1626. currentStream.add(format(startx + half) + " " + format(starty + half) + " l\n");
  1627. currentStream.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
  1628. } else {
  1629. currentStream.add(format(endx) + " " + format(starty) + " m\n");
  1630. currentStream.add(format(endx) + " " + format(starty + 2 * half) + " l\n");
  1631. currentStream.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
  1632. currentStream.add(format(startx) + " " + format(starty + half) + " l\n");
  1633. currentStream.add(format(endx - half) + " " + format(starty + half) + " l\n");
  1634. }
  1635. currentStream.add("h\n");
  1636. currentStream.add("f\n");
  1637. break;
  1638. default:
  1639. throw new UnsupportedOperationException("rule style not supported");
  1640. }
  1641. restoreGraphicsState();
  1642. currentState.pop();
  1643. beginTextObject();
  1644. super.renderLeader(area);
  1645. }
  1646. /** {@inheritDoc} */
  1647. public String getMimeType() {
  1648. return MIME_TYPE;
  1649. }
  1650. /**
  1651. * Sets the PDF/A mode for the PDF renderer.
  1652. * @param mode the PDF/A mode
  1653. */
  1654. public void setAMode(PDFAMode mode) {
  1655. this.pdfAMode = mode;
  1656. }
  1657. /**
  1658. * Sets the PDF/X mode for the PDF renderer.
  1659. * @param mode the PDF/X mode
  1660. */
  1661. public void setXMode(PDFXMode mode) {
  1662. this.pdfXMode = mode;
  1663. }
  1664. /**
  1665. * Sets the output color profile for the PDF renderer.
  1666. * @param outputProfileURI the URI to the output color profile
  1667. */
  1668. public void setOutputProfileURI(String outputProfileURI) {
  1669. this.outputProfileURI = outputProfileURI;
  1670. }
  1671. /**
  1672. * Sets the filter map to be used by the PDF renderer.
  1673. * @param filterMap the filter map
  1674. */
  1675. public void setFilterMap(Map filterMap) {
  1676. this.filterMap = filterMap;
  1677. }
  1678. /**
  1679. * Sets the encryption parameters used by the PDF renderer.
  1680. * @param encryptionParams the encryption parameters
  1681. */
  1682. public void setEncryptionParams(PDFEncryptionParams encryptionParams) {
  1683. this.encryptionParams = encryptionParams;
  1684. }
  1685. }