您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PDFRenderer.java 49KB

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