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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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.geom.AffineTransform;
  24. import java.awt.geom.Point2D;
  25. import java.awt.geom.Rectangle2D;
  26. import java.io.FileNotFoundException;
  27. import java.io.IOException;
  28. import java.io.OutputStream;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Locale;
  32. import java.util.Map;
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.NodeList;
  35. import org.apache.xmlgraphics.image.loader.ImageException;
  36. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  37. import org.apache.xmlgraphics.image.loader.ImageInfo;
  38. import org.apache.xmlgraphics.image.loader.ImageManager;
  39. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  40. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  41. import org.apache.fop.apps.FOPException;
  42. import org.apache.fop.apps.FOUserAgent;
  43. import org.apache.fop.apps.MimeConstants;
  44. import org.apache.fop.area.Area;
  45. import org.apache.fop.area.Block;
  46. import org.apache.fop.area.BookmarkData;
  47. import org.apache.fop.area.CTM;
  48. import org.apache.fop.area.DestinationData;
  49. import org.apache.fop.area.LineArea;
  50. import org.apache.fop.area.OffDocumentExtensionAttachment;
  51. import org.apache.fop.area.OffDocumentItem;
  52. import org.apache.fop.area.PageSequence;
  53. import org.apache.fop.area.PageViewport;
  54. import org.apache.fop.area.Trait;
  55. import org.apache.fop.area.inline.AbstractTextArea;
  56. import org.apache.fop.area.inline.Image;
  57. import org.apache.fop.area.inline.InlineArea;
  58. import org.apache.fop.area.inline.InlineParent;
  59. import org.apache.fop.area.inline.Leader;
  60. import org.apache.fop.area.inline.SpaceArea;
  61. import org.apache.fop.area.inline.TextArea;
  62. import org.apache.fop.area.inline.Viewport;
  63. import org.apache.fop.area.inline.WordArea;
  64. import org.apache.fop.datatypes.URISpecification;
  65. import org.apache.fop.events.ResourceEventProducer;
  66. import org.apache.fop.fo.extensions.ExtensionAttachment;
  67. import org.apache.fop.fo.extensions.xmp.XMPMetadata;
  68. import org.apache.fop.fonts.Font;
  69. import org.apache.fop.fonts.LazyFont;
  70. import org.apache.fop.fonts.SingleByteFont;
  71. import org.apache.fop.fonts.Typeface;
  72. import org.apache.fop.pdf.PDFAMode;
  73. import org.apache.fop.pdf.PDFAction;
  74. import org.apache.fop.pdf.PDFAnnotList;
  75. import org.apache.fop.pdf.PDFDocument;
  76. import org.apache.fop.pdf.PDFEncryptionParams;
  77. import org.apache.fop.pdf.PDFFactory;
  78. import org.apache.fop.pdf.PDFGoTo;
  79. import org.apache.fop.pdf.PDFInfo;
  80. import org.apache.fop.pdf.PDFLink;
  81. import org.apache.fop.pdf.PDFNumber;
  82. import org.apache.fop.pdf.PDFOutline;
  83. import org.apache.fop.pdf.PDFPage;
  84. import org.apache.fop.pdf.PDFPaintingState;
  85. import org.apache.fop.pdf.PDFResourceContext;
  86. import org.apache.fop.pdf.PDFResources;
  87. import org.apache.fop.pdf.PDFTextUtil;
  88. import org.apache.fop.pdf.PDFXMode;
  89. import org.apache.fop.pdf.PDFXObject;
  90. import org.apache.fop.render.AbstractPathOrientedRenderer;
  91. import org.apache.fop.render.Graphics2DAdapter;
  92. import org.apache.fop.render.RendererContext;
  93. import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
  94. import org.apache.fop.traits.RuleStyle;
  95. import org.apache.fop.util.AbstractPaintingState;
  96. import org.apache.fop.util.CharUtilities;
  97. import org.apache.fop.util.XMLUtil;
  98. import org.apache.fop.util.AbstractPaintingState.AbstractData;
  99. /**
  100. * Renderer that renders areas to PDF.
  101. */
  102. public class PDFRenderer extends AbstractPathOrientedRenderer implements PDFConfigurationConstants {
  103. /** The MIME type for PDF */
  104. public static final String MIME_TYPE = MimeConstants.MIME_PDF;
  105. /** Normal PDF resolution (72dpi) */
  106. public static final int NORMAL_PDF_RESOLUTION = 72;
  107. /** Controls whether comments are written to the PDF stream. */
  108. protected static final boolean WRITE_COMMENTS = true;
  109. /**
  110. * the PDF Document being created
  111. */
  112. protected PDFDocument pdfDoc;
  113. /**
  114. * Utility class which enables all sorts of features that are not directly connected to the
  115. * normal rendering process.
  116. */
  117. protected PDFRenderingUtil pdfUtil;
  118. /**
  119. * Map of pages using the PageViewport as the key
  120. * this is used for prepared pages that cannot be immediately
  121. * rendered
  122. */
  123. private Map pages;
  124. /**
  125. * Maps unique PageViewport key to PDF page reference
  126. */
  127. protected Map pageReferences = new java.util.HashMap();
  128. /**
  129. * Maps unique PageViewport key back to PageViewport itself
  130. */
  131. //protected Map pvReferences = new java.util.HashMap();
  132. /**
  133. * Maps XSL-FO element IDs to their on-page XY-positions
  134. * Must be used in conjunction with the page reference to fully specify the PDFGoTo details
  135. */
  136. protected Map idPositions = new java.util.HashMap();
  137. /**
  138. * Maps XSL-FO element IDs to PDFGoTo objects targeting the corresponding areas
  139. * These objects may not all be fully filled in yet
  140. */
  141. protected Map idGoTos = new java.util.HashMap();
  142. /**
  143. * The PDFGoTos in idGoTos that are not complete yet
  144. */
  145. protected List unfinishedGoTos = new java.util.ArrayList();
  146. // can't use a Set because PDFGoTo.equals returns true if the target is the same,
  147. // even if the object number differs
  148. /**
  149. * The output stream to write the document to
  150. */
  151. protected OutputStream ostream;
  152. /**
  153. * the /Resources object of the PDF document being created
  154. */
  155. protected PDFResources pdfResources;
  156. /** The current content generator to produce PDF commands with */
  157. protected PDFContentGenerator generator;
  158. private PDFBorderPainter borderPainter;
  159. /**
  160. * the current annotation list to add annotations to
  161. */
  162. protected PDFResourceContext currentContext = null;
  163. /**
  164. * the current page to add annotations to
  165. */
  166. protected PDFPage currentPage;
  167. /**
  168. * the current page's PDF reference string (to avoid numerous function calls)
  169. */
  170. protected String currentPageRef;
  171. /** page height */
  172. protected int pageHeight;
  173. /** Image handler registry */
  174. private final PDFImageHandlerRegistry imageHandlerRegistry = new PDFImageHandlerRegistry();
  175. private boolean accessEnabled;
  176. private PDFLogicalStructureHandler logicalStructureHandler;
  177. private int pageSequenceIndex;
  178. /** Reference in the structure tree to the image being rendered. */
  179. private String imageReference;
  180. /**
  181. * create the PDF renderer
  182. */
  183. public PDFRenderer() {
  184. }
  185. /** {@inheritDoc} */
  186. public void setUserAgent(FOUserAgent agent) {
  187. super.setUserAgent(agent);
  188. this.pdfUtil = new PDFRenderingUtil(getUserAgent());
  189. accessEnabled = agent.isAccessibilityEnabled();
  190. }
  191. PDFRenderingUtil getPDFUtil() {
  192. return this.pdfUtil;
  193. }
  194. PDFContentGenerator getGenerator() {
  195. return this.generator;
  196. }
  197. PDFPaintingState getState() {
  198. return getGenerator().getState();
  199. }
  200. /** {@inheritDoc} */
  201. public void startRenderer(OutputStream stream) throws IOException {
  202. if (userAgent == null) {
  203. throw new IllegalStateException("UserAgent must be set before starting the renderer");
  204. }
  205. ostream = stream;
  206. this.pdfDoc = pdfUtil.setupPDFDocument(stream);
  207. if (accessEnabled) {
  208. pdfDoc.getRoot().makeTagged();
  209. logicalStructureHandler = new PDFLogicalStructureHandler(pdfDoc);
  210. }
  211. }
  212. /**
  213. * Checks if there are any unfinished PDFGoTos left in the list and resolves them
  214. * to a default position on the page. Logs a warning, as this should not happen.
  215. */
  216. protected void finishOpenGoTos() {
  217. int count = unfinishedGoTos.size();
  218. if (count > 0) {
  219. // TODO : page height may not be the same for all targeted pages
  220. Point2D.Float defaultPos = new Point2D.Float(0f, pageHeight / 1000f); // top-o-page
  221. while (!unfinishedGoTos.isEmpty()) {
  222. PDFGoTo gt = (PDFGoTo) unfinishedGoTos.get(0);
  223. finishIDGoTo(gt, defaultPos);
  224. }
  225. PDFEventProducer eventProducer = PDFEventProducer.Provider.get(
  226. getUserAgent().getEventBroadcaster());
  227. eventProducer.nonFullyResolvedLinkTargets(this, count);
  228. // dysfunctional if pageref is null
  229. }
  230. }
  231. /** {@inheritDoc} */
  232. public void stopRenderer() throws IOException {
  233. finishOpenGoTos();
  234. pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
  235. pdfDoc.outputTrailer(ostream);
  236. this.pdfDoc = null;
  237. ostream = null;
  238. pages = null;
  239. pageReferences.clear();
  240. //pvReferences.clear();
  241. pdfResources = null;
  242. this.generator = null;
  243. currentContext = null;
  244. currentPage = null;
  245. idPositions.clear();
  246. idGoTos.clear();
  247. }
  248. /**
  249. * {@inheritDoc}
  250. */
  251. public boolean supportsOutOfOrder() {
  252. return !accessEnabled;
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. public void processOffDocumentItem(OffDocumentItem odi) {
  258. if (odi instanceof DestinationData) {
  259. // render Destinations
  260. renderDestination((DestinationData) odi);
  261. } else if (odi instanceof BookmarkData) {
  262. // render Bookmark-Tree
  263. renderBookmarkTree((BookmarkData) odi);
  264. } else if (odi instanceof OffDocumentExtensionAttachment) {
  265. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)odi).getAttachment();
  266. if (XMPMetadata.CATEGORY.equals(attachment.getCategory())) {
  267. pdfUtil.renderXMPMetadata((XMPMetadata)attachment);
  268. }
  269. }
  270. }
  271. private void renderDestination(DestinationData dd) {
  272. String targetID = dd.getIDRef();
  273. if (targetID == null || targetID.length() == 0) {
  274. throw new IllegalArgumentException("DestinationData must contain a ID reference");
  275. }
  276. PageViewport pv = dd.getPageViewport();
  277. if (pv != null) {
  278. PDFGoTo gt = getPDFGoToForID(targetID, pv.getKey());
  279. pdfDoc.getFactory().makeDestination(
  280. dd.getIDRef(), gt.makeReference());
  281. } else {
  282. //Warning already issued by AreaTreeHandler (debug level is sufficient)
  283. log.debug("Unresolved destination item received: " + dd.getIDRef());
  284. }
  285. }
  286. /**
  287. * Renders a Bookmark-Tree object
  288. * @param bookmarks the BookmarkData object containing all the Bookmark-Items
  289. */
  290. protected void renderBookmarkTree(BookmarkData bookmarks) {
  291. for (int i = 0; i < bookmarks.getCount(); i++) {
  292. BookmarkData ext = bookmarks.getSubData(i);
  293. renderBookmarkItem(ext, null);
  294. }
  295. }
  296. private void renderBookmarkItem(BookmarkData bookmarkItem,
  297. PDFOutline parentBookmarkItem) {
  298. PDFOutline pdfOutline = null;
  299. String targetID = bookmarkItem.getIDRef();
  300. if (targetID == null || targetID.length() == 0) {
  301. throw new IllegalArgumentException("DestinationData must contain a ID reference");
  302. }
  303. PageViewport pv = bookmarkItem.getPageViewport();
  304. if (pv != null) {
  305. String pvKey = pv.getKey();
  306. PDFGoTo gt = getPDFGoToForID(targetID, pvKey);
  307. // create outline object:
  308. PDFOutline parent = parentBookmarkItem != null
  309. ? parentBookmarkItem
  310. : pdfDoc.getOutlineRoot();
  311. pdfOutline = pdfDoc.getFactory().makeOutline(parent,
  312. bookmarkItem.getBookmarkTitle(), gt, bookmarkItem.showChildItems());
  313. } else {
  314. //Warning already issued by AreaTreeHandler (debug level is sufficient)
  315. log.debug("Bookmark with IDRef \"" + targetID + "\" has a null PageViewport.");
  316. }
  317. for (int i = 0; i < bookmarkItem.getCount(); i++) {
  318. renderBookmarkItem(bookmarkItem.getSubData(i), pdfOutline);
  319. }
  320. }
  321. /** {@inheritDoc} */
  322. public Graphics2DAdapter getGraphics2DAdapter() {
  323. return new PDFGraphics2DAdapter(this);
  324. }
  325. /** {@inheritDoc} */
  326. protected void saveGraphicsState() {
  327. generator.saveGraphicsState();
  328. }
  329. /** {@inheritDoc} */
  330. protected void restoreGraphicsState() {
  331. generator.restoreGraphicsState();
  332. }
  333. /** Indicates the beginning of a text object. */
  334. protected void beginTextObject() {
  335. generator.beginTextObject();
  336. }
  337. /** Indicates the end of a text object. */
  338. protected void endTextObject() {
  339. generator.endTextObject();
  340. }
  341. /**
  342. * Start the next page sequence.
  343. * For the PDF renderer there is no concept of page sequences
  344. * but it uses the first available page sequence title to set
  345. * as the title of the PDF document, and the language of the
  346. * document.
  347. * @param pageSequence the page sequence
  348. */
  349. public void startPageSequence(PageSequence pageSequence) {
  350. super.startPageSequence(pageSequence);
  351. LineArea seqTitle = pageSequence.getTitle();
  352. if (seqTitle != null) {
  353. String str = convertTitleToString(seqTitle);
  354. PDFInfo info = this.pdfDoc.getInfo();
  355. if (info.getTitle() == null) {
  356. info.setTitle(str);
  357. }
  358. }
  359. Locale language = null;
  360. if (pageSequence.getLanguage() != null) {
  361. String lang = pageSequence.getLanguage();
  362. String country = pageSequence.getCountry();
  363. if (lang != null) {
  364. language = (country == null) ? new Locale(lang) : new Locale(lang, country);
  365. }
  366. if (pdfDoc.getRoot().getLanguage() == null) {
  367. //Only set if not set already (first non-null is used)
  368. //Note: No checking is performed whether the values are valid!
  369. pdfDoc.getRoot().setLanguage(XMLUtil.toRFC3066(language));
  370. }
  371. }
  372. pdfUtil.generateDefaultXMPMetadata();
  373. if (accessEnabled) {
  374. NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
  375. logicalStructureHandler.processStructureTree(nodes, language);
  376. }
  377. }
  378. /**
  379. * The pdf page is prepared by making the page.
  380. * The page is made in the pdf document without any contents
  381. * and then stored to add the contents later.
  382. * The page objects is stored using the area tree PageViewport
  383. * as a key.
  384. *
  385. * @param page the page to prepare
  386. */
  387. public void preparePage(PageViewport page) {
  388. setupPage(page);
  389. if (pages == null) {
  390. pages = new java.util.HashMap();
  391. }
  392. pages.put(page, currentPage);
  393. }
  394. private void setupPage(PageViewport page) {
  395. this.pdfResources = this.pdfDoc.getResources();
  396. Rectangle2D bounds = page.getViewArea();
  397. double w = bounds.getWidth();
  398. double h = bounds.getHeight();
  399. this.currentPage = this.pdfDoc.getFactory().makePage(
  400. this.pdfResources,
  401. (int) Math.round(w / 1000), (int) Math.round(h / 1000),
  402. page.getPageIndex());
  403. pageReferences.put(page.getKey(), currentPage.referencePDF());
  404. //pvReferences.put(page.getKey(), page);
  405. pdfUtil.generatePageLabel(page.getPageIndex(), page.getPageNumberString());
  406. }
  407. /**
  408. * This method creates a PDF stream for the current page
  409. * uses it as the contents of a new page. The page is written
  410. * immediately to the output stream.
  411. * {@inheritDoc}
  412. */
  413. public void renderPage(PageViewport page)
  414. throws IOException, FOPException {
  415. if (pages != null
  416. && (currentPage = (PDFPage) pages.get(page)) != null) {
  417. //Retrieve previously prepared page (out-of-line rendering)
  418. pages.remove(page);
  419. } else {
  420. setupPage(page);
  421. }
  422. currentPageRef = currentPage.referencePDF();
  423. if (accessEnabled) {
  424. logicalStructureHandler.startPage(currentPage);
  425. }
  426. Rectangle bounds = page.getViewArea();
  427. pageHeight = bounds.height;
  428. this.generator = new PDFContentGenerator(this.pdfDoc, this.ostream, this.currentPage);
  429. this.borderPainter = new PDFBorderPainter(this.generator);
  430. // Transform the PDF's default coordinate system (0,0 at lower left) to the PDFRenderer's
  431. AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
  432. pageHeight / 1000f);
  433. generator.concatenate(basicPageTransform);
  434. /*
  435. currentState.concatenate(basicPageTransform);
  436. currentStream.add(CTMHelper.toPDFString(basicPageTransform, false) + " cm\n");
  437. */
  438. super.renderPage(page);
  439. if (accessEnabled) {
  440. logicalStructureHandler.endPage();
  441. }
  442. this.pdfDoc.registerObject(generator.getStream());
  443. currentPage.setContents(generator.getStream());
  444. PDFAnnotList annots = currentPage.getAnnotations();
  445. if (annots != null) {
  446. this.pdfDoc.addObject(annots);
  447. }
  448. this.pdfDoc.addObject(currentPage);
  449. this.borderPainter = null;
  450. this.generator.flushPDFDoc();
  451. this.generator = null;
  452. }
  453. /** {@inheritDoc} */
  454. protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
  455. saveGraphicsState();
  456. // Set the given CTM in the graphics state
  457. /*
  458. currentState.concatenate(
  459. new AffineTransform(CTMHelper.toPDFArray(ctm)));
  460. */
  461. if (clippingRect != null) {
  462. clipRect((float)clippingRect.getX() / 1000f,
  463. (float)clippingRect.getY() / 1000f,
  464. (float)clippingRect.getWidth() / 1000f,
  465. (float)clippingRect.getHeight() / 1000f);
  466. }
  467. // multiply with current CTM
  468. generator.concatenate(new AffineTransform(CTMHelper.toPDFArray(ctm)));
  469. }
  470. /** {@inheritDoc} */
  471. protected void endVParea() {
  472. restoreGraphicsState();
  473. }
  474. /** {@inheritDoc} */
  475. protected void concatenateTransformationMatrix(AffineTransform at) {
  476. generator.concatenate(at);
  477. }
  478. /**
  479. * Formats a float value (normally coordinates) as Strings.
  480. * @param value the value
  481. * @return the formatted value
  482. */
  483. protected static String format(float value) {
  484. return PDFNumber.doubleOut(value);
  485. }
  486. /** {@inheritDoc} */
  487. protected void drawBorderLine(float x1, float y1, float x2, float y2,
  488. boolean horz, boolean startOrBefore, int style, Color col) {
  489. PDFBorderPainter.drawBorderLine(generator, x1, y1, x2, y2, horz, startOrBefore, style, col);
  490. }
  491. /** {@inheritDoc} */
  492. protected void clipRect(float x, float y, float width, float height) {
  493. generator.add(format(x) + " " + format(y) + " "
  494. + format(width) + " " + format(height) + " re ");
  495. clip();
  496. }
  497. /**
  498. * Clip an area.
  499. */
  500. protected void clip() {
  501. generator.add("W\n" + "n\n");
  502. }
  503. /**
  504. * Moves the current point to (x, y), omitting any connecting line segment.
  505. * @param x x coordinate
  506. * @param y y coordinate
  507. */
  508. protected void moveTo(float x, float y) {
  509. generator.add(format(x) + " " + format(y) + " m ");
  510. }
  511. /**
  512. * Appends a straight line segment from the current point to (x, y). The
  513. * new current point is (x, y).
  514. * @param x x coordinate
  515. * @param y y coordinate
  516. */
  517. protected void lineTo(float x, float y) {
  518. generator.add(format(x) + " " + format(y) + " l ");
  519. }
  520. /**
  521. * Closes the current subpath by appending a straight line segment from
  522. * the current point to the starting point of the subpath.
  523. */
  524. protected void closePath() {
  525. generator.add("h ");
  526. }
  527. /**
  528. * {@inheritDoc}
  529. */
  530. protected void fillRect(float x, float y, float width, float height) {
  531. if (width > 0 && height > 0) {
  532. generator.add(format(x) + " " + format(y) + " "
  533. + format(width) + " " + format(height) + " re f\n");
  534. }
  535. }
  536. /**
  537. * Draw a line.
  538. *
  539. * @param startx the start x position
  540. * @param starty the start y position
  541. * @param endx the x end position
  542. * @param endy the y end position
  543. */
  544. private void drawLine(float startx, float starty, float endx, float endy) {
  545. generator.add(format(startx) + " " + format(starty) + " m ");
  546. generator.add(format(endx) + " " + format(endy) + " l S\n");
  547. }
  548. /**
  549. * Breaks out of the state stack to handle fixed block-containers.
  550. * @return the saved state stack to recreate later
  551. */
  552. protected List breakOutOfStateStack() {
  553. PDFPaintingState paintingState = getState();
  554. List breakOutList = new java.util.ArrayList();
  555. AbstractPaintingState.AbstractData data;
  556. while (true) {
  557. data = paintingState.getData();
  558. if (paintingState.restore() == null) {
  559. break;
  560. }
  561. if (breakOutList.size() == 0) {
  562. generator.comment("------ break out!");
  563. }
  564. breakOutList.add(0, data); //Insert because of stack-popping
  565. generator.restoreGraphicsState(false);
  566. }
  567. return breakOutList;
  568. }
  569. /**
  570. * Restores the state stack after a break out.
  571. * @param breakOutList the state stack to restore.
  572. */
  573. protected void restoreStateStackAfterBreakOut(List breakOutList) {
  574. generator.comment("------ restoring context after break-out...");
  575. // currentState.pushAll(breakOutList);
  576. AbstractData data;
  577. Iterator i = breakOutList.iterator();
  578. while (i.hasNext()) {
  579. data = (AbstractData)i.next();
  580. saveGraphicsState();
  581. AffineTransform at = data.getTransform();
  582. concatenateTransformationMatrix(at);
  583. //TODO Break-out: Also restore items such as line width and color
  584. //Left out for now because all this painting stuff is very
  585. //inconsistent. Some values go over PDFState, some don't.
  586. }
  587. generator.comment("------ done.");
  588. }
  589. /**
  590. * Returns area's id if it is the first area in the document with that id
  591. * (i.e. if the area qualifies as a link target).
  592. * Otherwise, or if the area has no id, null is returned.
  593. *
  594. * <i>NOTE</i>: area must be on currentPageViewport, otherwise result may be wrong!
  595. *
  596. * @param area the area for which to return the id
  597. * @return the area's id (null if the area has no id or
  598. * other preceding areas have the same id)
  599. */
  600. protected String getTargetableID(Area area) {
  601. String id = (String) area.getTrait(Trait.PROD_ID);
  602. if (id == null || id.length() == 0
  603. || !currentPageViewport.isFirstWithID(id)
  604. || idPositions.containsKey(id)) {
  605. return null;
  606. } else {
  607. return id;
  608. }
  609. }
  610. /**
  611. * Set XY position in the PDFGoTo and add it to the PDF trailer.
  612. *
  613. * @param gt the PDFGoTo object
  614. * @param position the X,Y position to set
  615. */
  616. protected void finishIDGoTo(PDFGoTo gt, Point2D.Float position) {
  617. gt.setPosition(position);
  618. pdfDoc.addTrailerObject(gt);
  619. unfinishedGoTos.remove(gt);
  620. }
  621. /**
  622. * Set page reference and XY position in the PDFGoTo and add it to the PDF trailer.
  623. *
  624. * @param gt the PDFGoTo object
  625. * @param pdfPageRef the PDF reference string of the target page object
  626. * @param position the X,Y position to set
  627. */
  628. protected void finishIDGoTo(PDFGoTo gt, String pdfPageRef, Point2D.Float position) {
  629. gt.setPageReference(pdfPageRef);
  630. finishIDGoTo(gt, position);
  631. }
  632. /**
  633. * Get a PDFGoTo pointing to the given id. Create one if necessary.
  634. * It is possible that the PDFGoTo is not fully resolved yet. In that case
  635. * it must be completed (and added to the PDF trailer) later.
  636. *
  637. * @param targetID the target id of the PDFGoTo
  638. * @param pvKey the unique key of the target PageViewport
  639. *
  640. * @return the PDFGoTo that was found or created
  641. */
  642. protected PDFGoTo getPDFGoToForID(String targetID, String pvKey) {
  643. // Already a PDFGoTo present for this target? If not, create.
  644. PDFGoTo gt = (PDFGoTo) idGoTos.get(targetID);
  645. if (gt == null) {
  646. String pdfPageRef = (String) pageReferences.get(pvKey);
  647. Point2D.Float position = (Point2D.Float) idPositions.get(targetID);
  648. // can the GoTo already be fully filled in?
  649. if (pdfPageRef != null && position != null) {
  650. // getPDFGoTo shares PDFGoTo objects as much as possible.
  651. // It also takes care of assignObjectNumber and addTrailerObject.
  652. gt = pdfDoc.getFactory().getPDFGoTo(pdfPageRef, position);
  653. } else {
  654. // Not complete yet, can't use getPDFGoTo:
  655. gt = new PDFGoTo(pdfPageRef);
  656. pdfDoc.assignObjectNumber(gt);
  657. // pdfDoc.addTrailerObject() will be called later, from finishIDGoTo()
  658. unfinishedGoTos.add(gt);
  659. }
  660. idGoTos.put(targetID, gt);
  661. }
  662. return gt;
  663. }
  664. /**
  665. * Saves id's absolute position on page for later retrieval by PDFGoTos
  666. *
  667. * @param id the id of the area whose position must be saved
  668. * @param pdfPageRef the PDF page reference string
  669. * @param relativeIPP the *relative* IP position in millipoints
  670. * @param relativeBPP the *relative* BP position in millipoints
  671. * @param tf the transformation to apply once the relative positions have been
  672. * converted to points
  673. */
  674. protected void saveAbsolutePosition(String id, String pdfPageRef,
  675. int relativeIPP, int relativeBPP, AffineTransform tf) {
  676. Point2D.Float position = new Point2D.Float(relativeIPP / 1000f, relativeBPP / 1000f);
  677. tf.transform(position, position);
  678. idPositions.put(id, position);
  679. // is there already a PDFGoTo waiting to be completed?
  680. PDFGoTo gt = (PDFGoTo) idGoTos.get(id);
  681. if (gt != null) {
  682. finishIDGoTo(gt, pdfPageRef, position);
  683. }
  684. /*
  685. // The code below auto-creates a named destination for every id in the document.
  686. // This should probably be controlled by a user-configurable setting, as it may
  687. // make the PDF file grow noticeably.
  688. // *** NOT YET WELL-TESTED ! ***
  689. if (true) {
  690. PDFFactory factory = pdfDoc.getFactory();
  691. if (gt == null) {
  692. gt = factory.getPDFGoTo(pdfPageRef, position);
  693. idGoTos.put(id, gt); // so others can pick it up too
  694. }
  695. factory.makeDestination(id, gt.referencePDF(), currentPageViewport);
  696. // Note: using currentPageViewport is only correct if the id is indeed on
  697. // the current PageViewport. But even if incorrect, it won't interfere with
  698. // what gets created in the PDF.
  699. // For speedup, we should also create a lookup map id -> PDFDestination
  700. }
  701. */
  702. }
  703. /**
  704. * Saves id's absolute position on page for later retrieval by PDFGoTos,
  705. * using the currently valid transformation and the currently valid PDF page reference
  706. *
  707. * @param id the id of the area whose position must be saved
  708. * @param relativeIPP the *relative* IP position in millipoints
  709. * @param relativeBPP the *relative* BP position in millipoints
  710. */
  711. protected void saveAbsolutePosition(String id, int relativeIPP, int relativeBPP) {
  712. saveAbsolutePosition(id, currentPageRef,
  713. relativeIPP, relativeBPP, getState().getTransform());
  714. }
  715. /**
  716. * If the given block area is a possible link target, its id + absolute position will
  717. * be saved. The saved position is only correct if this function is called at the very
  718. * start of renderBlock!
  719. *
  720. * @param block the block area in question
  721. */
  722. protected void saveBlockPosIfTargetable(Block block) {
  723. String id = getTargetableID(block);
  724. if (id != null) {
  725. // FIXME: Like elsewhere in the renderer code, absolute and relative
  726. // directions are happily mixed here. This makes sure that the
  727. // links point to the right location, but it is not correct.
  728. int ipp = block.getXOffset();
  729. int bpp = block.getYOffset() + block.getSpaceBefore();
  730. int positioning = block.getPositioning();
  731. if (!(positioning == Block.FIXED || positioning == Block.ABSOLUTE)) {
  732. ipp += currentIPPosition;
  733. bpp += currentBPPosition;
  734. }
  735. AffineTransform tf = positioning == Block.FIXED
  736. ? getState().getBaseTransform()
  737. : getState().getTransform();
  738. saveAbsolutePosition(id, currentPageRef, ipp, bpp, tf);
  739. }
  740. }
  741. /**
  742. * If the given inline area is a possible link target, its id + absolute position will
  743. * be saved. The saved position is only correct if this function is called at the very
  744. * start of renderInlineArea!
  745. *
  746. * @param inlineArea the inline area in question
  747. */
  748. protected void saveInlinePosIfTargetable(InlineArea inlineArea) {
  749. String id = getTargetableID(inlineArea);
  750. if (id != null) {
  751. int extraMarginBefore = 5000; // millipoints
  752. int ipp = currentIPPosition;
  753. int bpp = currentBPPosition + inlineArea.getOffset() - extraMarginBefore;
  754. saveAbsolutePosition(id, ipp, bpp);
  755. }
  756. }
  757. /**
  758. * {@inheritDoc}
  759. */
  760. protected void renderBlock(Block block) {
  761. saveBlockPosIfTargetable(block);
  762. super.renderBlock(block);
  763. }
  764. /** {@inheritDoc} */
  765. protected void renderLineArea(LineArea line) {
  766. super.renderLineArea(line);
  767. }
  768. /**
  769. * {@inheritDoc}
  770. */
  771. protected void renderInlineArea(InlineArea inlineArea) {
  772. saveInlinePosIfTargetable(inlineArea);
  773. super.renderInlineArea(inlineArea);
  774. }
  775. /**
  776. * Render inline parent area.
  777. * For pdf this handles the inline parent area traits such as
  778. * links, border, background.
  779. * @param ip the inline parent area
  780. */
  781. public void renderInlineParent(InlineParent ip) {
  782. boolean annotsAllowed = pdfDoc.getProfile().isAnnotationAllowed();
  783. // stuff we only need if a link must be created:
  784. Rectangle2D ipRect = null;
  785. PDFFactory factory = null;
  786. PDFAction action = null;
  787. if (annotsAllowed) {
  788. // make sure the rect is determined *before* calling super!
  789. int ipp = currentIPPosition;
  790. int bpp = currentBPPosition + ip.getOffset();
  791. ipRect = new Rectangle2D.Float(ipp / 1000f, bpp / 1000f,
  792. ip.getIPD() / 1000f, ip.getBPD() / 1000f);
  793. AffineTransform transform = getState().getTransform();
  794. ipRect = transform.createTransformedShape(ipRect).getBounds2D();
  795. factory = pdfDoc.getFactory();
  796. }
  797. // render contents
  798. super.renderInlineParent(ip);
  799. boolean linkTraitFound = false;
  800. // try INTERNAL_LINK first
  801. Trait.InternalLink intLink = (Trait.InternalLink) ip.getTrait(Trait.INTERNAL_LINK);
  802. if (intLink != null) {
  803. linkTraitFound = true;
  804. String pvKey = intLink.getPVKey();
  805. String idRef = intLink.getIDRef();
  806. boolean pvKeyOK = pvKey != null && pvKey.length() > 0;
  807. boolean idRefOK = idRef != null && idRef.length() > 0;
  808. if (pvKeyOK && idRefOK) {
  809. if (annotsAllowed) {
  810. action = getPDFGoToForID(idRef, pvKey);
  811. }
  812. } else {
  813. //Warnings already issued by AreaTreeHandler
  814. }
  815. }
  816. // no INTERNAL_LINK, look for EXTERNAL_LINK
  817. if (!linkTraitFound) {
  818. Trait.ExternalLink extLink = (Trait.ExternalLink) ip.getTrait(Trait.EXTERNAL_LINK);
  819. if (extLink != null) {
  820. String extDest = extLink.getDestination();
  821. if (extDest != null && extDest.length() > 0) {
  822. linkTraitFound = true;
  823. if (annotsAllowed) {
  824. action = factory.getExternalAction(extDest, extLink.newWindow());
  825. }
  826. }
  827. }
  828. }
  829. // warn if link trait found but not allowed, else create link
  830. if (linkTraitFound) {
  831. if (!annotsAllowed) {
  832. log.warn("Skipping annotation for a link due to PDF profile: "
  833. + pdfDoc.getProfile());
  834. } else if (action != null) {
  835. PDFLink pdfLink = factory.makeLink(ipRect, action);
  836. if (accessEnabled) {
  837. String ptr = (String) ip.getTrait(Trait.PTR);
  838. logicalStructureHandler.addLinkContentItem(pdfLink, ptr);
  839. }
  840. currentPage.addAnnotation(pdfLink);
  841. }
  842. }
  843. }
  844. /** {@inheritDoc} */
  845. public void renderViewport(Viewport viewport) {
  846. imageReference = (String) viewport.getTrait(Trait.PTR);
  847. super.renderViewport(viewport);
  848. imageReference = null;
  849. }
  850. private Typeface getTypeface(String fontName) {
  851. Typeface tf = (Typeface) fontInfo.getFonts().get(fontName);
  852. if (tf instanceof LazyFont) {
  853. tf = ((LazyFont)tf).getRealFont();
  854. }
  855. return tf;
  856. }
  857. /** {@inheritDoc} */
  858. public void renderText(TextArea text) {
  859. renderInlineAreaBackAndBorders(text);
  860. Color ct = (Color) text.getTrait(Trait.COLOR);
  861. updateColor(ct, true);
  862. if (accessEnabled) {
  863. String ptr = (String) text.getTrait(Trait.PTR);
  864. MarkedContentInfo mci = logicalStructureHandler.addTextContentItem(ptr);
  865. if (generator.getTextUtil().isInTextObject()) {
  866. generator.separateTextElements(mci.tag, mci.mcid);
  867. }
  868. generator.beginTextObject(mci.tag, mci.mcid);
  869. } else {
  870. beginTextObject();
  871. }
  872. String fontName = getInternalFontNameForArea(text);
  873. int size = ((Integer) text.getTrait(Trait.FONT_SIZE)).intValue();
  874. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  875. Typeface tf = getTypeface(fontName);
  876. PDFTextUtil textutil = generator.getTextUtil();
  877. textutil.updateTf(fontName, size / 1000f, tf.isMultiByte());
  878. // word.getOffset() = only height of text itself
  879. // currentBlockIPPosition: 0 for beginning of line; nonzero
  880. // where previous line area failed to take up entire allocated space
  881. int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  882. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  883. textutil.writeTextMatrix(new AffineTransform(1, 0, 0, -1, rx / 1000f, bl / 1000f));
  884. super.renderText(text);
  885. textutil.writeTJ();
  886. renderTextDecoration(tf, size, text, bl, rx);
  887. }
  888. /** {@inheritDoc} */
  889. public void renderWord(WordArea word) {
  890. Font font = getFontFromArea(word.getParentArea());
  891. String s = word.getWord();
  892. escapeText(s, word.getLetterAdjustArray(),
  893. font, (AbstractTextArea)word.getParentArea());
  894. super.renderWord(word);
  895. }
  896. /** {@inheritDoc} */
  897. public void renderSpace(SpaceArea space) {
  898. Font font = getFontFromArea(space.getParentArea());
  899. String s = space.getSpace();
  900. AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
  901. escapeText(s, null, font, textArea);
  902. if (space.isAdjustable()) {
  903. int tws = -((TextArea) space.getParentArea()).getTextWordSpaceAdjust()
  904. - 2 * textArea.getTextLetterSpaceAdjust();
  905. if (tws != 0) {
  906. float adjust = tws / (font.getFontSize() / 1000f);
  907. generator.getTextUtil().adjustGlyphTJ(adjust);
  908. }
  909. }
  910. super.renderSpace(space);
  911. }
  912. /**
  913. * Escapes text according to PDF rules.
  914. * @param s Text to escape
  915. * @param letterAdjust an array of widths for letter adjustment (may be null)
  916. * @param font to font in use
  917. * @param parentArea the parent text area to retrieve certain traits from
  918. */
  919. protected void escapeText(String s,
  920. int[] letterAdjust,
  921. Font font, AbstractTextArea parentArea) {
  922. escapeText(s, 0, s.length(), letterAdjust, font, parentArea);
  923. }
  924. /**
  925. * Escapes text according to PDF rules.
  926. * @param s Text to escape
  927. * @param start the start position in the text
  928. * @param end the end position in the text
  929. * @param letterAdjust an array of widths for letter adjustment (may be null)
  930. * @param font to font in use
  931. * @param parentArea the parent text area to retrieve certain traits from
  932. */
  933. protected void escapeText(String s, int start, int end,
  934. int[] letterAdjust,
  935. Font font, AbstractTextArea parentArea) {
  936. String fontName = font.getFontName();
  937. float fontSize = font.getFontSize() / 1000f;
  938. Typeface tf = getTypeface(fontName);
  939. SingleByteFont singleByteFont = null;
  940. if (tf instanceof SingleByteFont) {
  941. singleByteFont = (SingleByteFont)tf;
  942. }
  943. PDFTextUtil textutil = generator.getTextUtil();
  944. int l = s.length();
  945. for (int i = start; i < end; i++) {
  946. char orgChar = s.charAt(i);
  947. char ch;
  948. float glyphAdjust = 0;
  949. if (font.hasChar(orgChar)) {
  950. ch = font.mapChar(orgChar);
  951. if (singleByteFont != null && singleByteFont.hasAdditionalEncodings()) {
  952. int encoding = ch / 256;
  953. if (encoding == 0) {
  954. textutil.updateTf(fontName, fontSize, tf.isMultiByte());
  955. } else {
  956. textutil.updateTf(fontName + "_" + Integer.toString(encoding),
  957. fontSize, tf.isMultiByte());
  958. ch = (char)(ch % 256);
  959. }
  960. }
  961. int tls = (i < l - 1 ? parentArea.getTextLetterSpaceAdjust() : 0);
  962. glyphAdjust -= tls;
  963. } else {
  964. if (CharUtilities.isFixedWidthSpace(orgChar)) {
  965. //Fixed width space are rendered as spaces so copy/paste works in a reader
  966. ch = font.mapChar(CharUtilities.SPACE);
  967. glyphAdjust = font.getCharWidth(ch) - font.getCharWidth(orgChar);
  968. } else {
  969. ch = font.mapChar(orgChar);
  970. }
  971. }
  972. if (letterAdjust != null && i < l - 1) {
  973. glyphAdjust -= letterAdjust[i + 1];
  974. }
  975. textutil.writeTJMappedChar(ch);
  976. float adjust = glyphAdjust / fontSize;
  977. if (adjust != 0) {
  978. textutil.adjustGlyphTJ(adjust);
  979. }
  980. }
  981. }
  982. /** {@inheritDoc} */
  983. protected void updateColor(Color col, boolean fill) {
  984. generator.updateColor(col, fill, null);
  985. }
  986. /** {@inheritDoc} */
  987. public void renderImage(Image image, Rectangle2D pos) {
  988. endTextObject();
  989. String url = image.getURL();
  990. putImage(url, pos, image.getForeignAttributes());
  991. }
  992. /** {@inheritDoc} */
  993. protected void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
  994. endTextObject();
  995. putImage(url, pos, foreignAttributes);
  996. }
  997. /**
  998. * Adds a PDF XObject (a bitmap or form) to the PDF that will later be referenced.
  999. * @param uri URL of the bitmap
  1000. * @param pos Position of the bitmap
  1001. * @deprecated Use {@link #putImage(String, Rectangle2D, Map)} instead.
  1002. */
  1003. protected void putImage(String uri, Rectangle2D pos) {
  1004. putImage(uri, pos, null);
  1005. }
  1006. /**
  1007. * Adds a PDF XObject (a bitmap or form) to the PDF that will later be referenced.
  1008. * @param uri URL of the bitmap
  1009. * @param pos Position of the bitmap
  1010. * @param foreignAttributes foreign attributes associated with the image
  1011. */
  1012. protected void putImage(String uri, Rectangle2D pos, Map foreignAttributes) {
  1013. Rectangle posInt = new Rectangle(
  1014. (int)pos.getX(),
  1015. (int)pos.getY(),
  1016. (int)pos.getWidth(),
  1017. (int)pos.getHeight());
  1018. uri = URISpecification.getURL(uri);
  1019. PDFXObject xobject = pdfDoc.getXObject(uri);
  1020. if (xobject != null) {
  1021. float w = (float) pos.getWidth() / 1000f;
  1022. float h = (float) pos.getHeight() / 1000f;
  1023. placeImage((float)pos.getX() / 1000f,
  1024. (float)pos.getY() / 1000f, w, h, xobject);
  1025. return;
  1026. }
  1027. Point origin = new Point(currentIPPosition, currentBPPosition);
  1028. int x = origin.x + posInt.x;
  1029. int y = origin.y + posInt.y;
  1030. ImageManager manager = getUserAgent().getFactory().getImageManager();
  1031. ImageInfo info = null;
  1032. try {
  1033. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  1034. info = manager.getImageInfo(uri, sessionContext);
  1035. Map hints = ImageUtil.getDefaultHints(sessionContext);
  1036. ImageFlavor[] supportedFlavors = imageHandlerRegistry.getSupportedFlavors();
  1037. org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
  1038. info, supportedFlavors, hints, sessionContext);
  1039. //First check for a dynamically registered handler
  1040. PDFImageHandler handler
  1041. = (PDFImageHandler)imageHandlerRegistry.getHandler(img.getClass());
  1042. if (handler != null) {
  1043. if (log.isDebugEnabled()) {
  1044. log.debug("Using PDFImageHandler: " + handler.getClass().getName());
  1045. }
  1046. try {
  1047. RendererContext context = createRendererContext(
  1048. x, y, posInt.width, posInt.height, foreignAttributes);
  1049. handler.generateImage(context, img, origin, posInt);
  1050. } catch (IOException ioe) {
  1051. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1052. getUserAgent().getEventBroadcaster());
  1053. eventProducer.imageWritingError(this, ioe);
  1054. return;
  1055. }
  1056. } else {
  1057. throw new UnsupportedOperationException(
  1058. "No PDFImageHandler available for image: "
  1059. + info + " (" + img.getClass().getName() + ")");
  1060. }
  1061. } catch (ImageException ie) {
  1062. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1063. getUserAgent().getEventBroadcaster());
  1064. eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
  1065. } catch (FileNotFoundException fe) {
  1066. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1067. getUserAgent().getEventBroadcaster());
  1068. eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
  1069. } catch (IOException ioe) {
  1070. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  1071. getUserAgent().getEventBroadcaster());
  1072. eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
  1073. }
  1074. // output new data
  1075. try {
  1076. this.generator.flushPDFDoc();
  1077. } catch (IOException ioe) {
  1078. // ioexception will be caught later
  1079. log.error(ioe.getMessage());
  1080. }
  1081. }
  1082. /**
  1083. * Places a previously registered image at a certain place on the page.
  1084. * @param x X coordinate
  1085. * @param y Y coordinate
  1086. * @param w width for image
  1087. * @param h height for image
  1088. * @param xobj the image XObject
  1089. */
  1090. public void placeImage(float x, float y, float w, float h, PDFXObject xobj) {
  1091. if (accessEnabled) {
  1092. MarkedContentInfo mci = logicalStructureHandler.addImageContentItem(imageReference);
  1093. generator.saveGraphicsState(mci.tag, mci.mcid);
  1094. } else {
  1095. saveGraphicsState();
  1096. }
  1097. generator.add(format(w) + " 0 0 "
  1098. + format(-h) + " "
  1099. + format(currentIPPosition / 1000f + x) + " "
  1100. + format(currentBPPosition / 1000f + h + y)
  1101. + " cm\n" + xobj.getName() + " Do\n");
  1102. if (accessEnabled) {
  1103. generator.restoreGraphicsStateAccess();
  1104. } else {
  1105. restoreGraphicsState();
  1106. }
  1107. }
  1108. /** {@inheritDoc} */
  1109. protected RendererContext createRendererContext(int x, int y, int width, int height,
  1110. Map foreignAttributes) {
  1111. RendererContext context = super.createRendererContext(
  1112. x, y, width, height, foreignAttributes);
  1113. context.setProperty(PDFRendererContextConstants.PDF_DOCUMENT, pdfDoc);
  1114. context.setProperty(PDFRendererContextConstants.OUTPUT_STREAM, ostream);
  1115. context.setProperty(PDFRendererContextConstants.PDF_PAGE, currentPage);
  1116. context.setProperty(PDFRendererContextConstants.PDF_CONTEXT,
  1117. currentContext == null ? currentPage : currentContext);
  1118. context.setProperty(PDFRendererContextConstants.PDF_CONTEXT, currentContext);
  1119. context.setProperty(PDFRendererContextConstants.PDF_STREAM, generator.getStream());
  1120. context.setProperty(PDFRendererContextConstants.PDF_FONT_INFO, fontInfo);
  1121. context.setProperty(PDFRendererContextConstants.PDF_FONT_NAME, "");
  1122. context.setProperty(PDFRendererContextConstants.PDF_FONT_SIZE, new Integer(0));
  1123. return context;
  1124. }
  1125. /** {@inheritDoc} */
  1126. public void renderDocument(Document doc, String ns, Rectangle2D pos, Map foreignAttributes) {
  1127. if (accessEnabled) {
  1128. MarkedContentInfo mci = logicalStructureHandler.addImageContentItem(imageReference);
  1129. generator.beginMarkedContentSequence(mci.tag, mci.mcid);
  1130. }
  1131. super.renderDocument(doc, ns, pos, foreignAttributes);
  1132. if (accessEnabled) {
  1133. generator.endMarkedContentSequence();
  1134. }
  1135. }
  1136. /**
  1137. * Render leader area.
  1138. * This renders a leader area which is an area with a rule.
  1139. * @param area the leader area to render
  1140. */
  1141. public void renderLeader(Leader area) {
  1142. renderInlineAreaBackAndBorders(area);
  1143. int style = area.getRuleStyle();
  1144. int ruleThickness = area.getRuleThickness();
  1145. int startx = currentIPPosition + area.getBorderAndPaddingWidthStart();
  1146. int starty = currentBPPosition + area.getOffset() + (ruleThickness / 2);
  1147. int endx = currentIPPosition
  1148. + area.getBorderAndPaddingWidthStart()
  1149. + area.getIPD();
  1150. Color col = (Color)area.getTrait(Trait.COLOR);
  1151. endTextObject();
  1152. borderPainter.drawLine(new Point(startx, starty), new Point(endx, starty),
  1153. ruleThickness, col, RuleStyle.valueOf(style));
  1154. super.renderLeader(area);
  1155. }
  1156. /** {@inheritDoc} */
  1157. public String getMimeType() {
  1158. return MIME_TYPE;
  1159. }
  1160. /**
  1161. * Sets the PDF/A mode for the PDF renderer.
  1162. * @param mode the PDF/A mode
  1163. */
  1164. public void setAMode(PDFAMode mode) {
  1165. this.pdfUtil.setAMode(mode);
  1166. }
  1167. /**
  1168. * Sets the PDF/X mode for the PDF renderer.
  1169. * @param mode the PDF/X mode
  1170. */
  1171. public void setXMode(PDFXMode mode) {
  1172. this.pdfUtil.setXMode(mode);
  1173. }
  1174. /**
  1175. * Sets the output color profile for the PDF renderer.
  1176. * @param outputProfileURI the URI to the output color profile
  1177. */
  1178. public void setOutputProfileURI(String outputProfileURI) {
  1179. this.pdfUtil.setOutputProfileURI(outputProfileURI);
  1180. }
  1181. /**
  1182. * Sets the filter map to be used by the PDF renderer.
  1183. * @param filterMap the filter map
  1184. */
  1185. public void setFilterMap(Map filterMap) {
  1186. this.pdfUtil.setFilterMap(filterMap);
  1187. }
  1188. /**
  1189. * Sets the encryption parameters used by the PDF renderer.
  1190. * @param encryptionParams the encryption parameters
  1191. */
  1192. public void setEncryptionParams(PDFEncryptionParams encryptionParams) {
  1193. this.pdfUtil.setEncryptionParams(encryptionParams);
  1194. }
  1195. MarkedContentInfo addCurrentImageToStructureTree() {
  1196. return logicalStructureHandler.addImageContentItem(imageReference);
  1197. }
  1198. }