Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PDFRenderer.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "FOP" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.render.pdf;
  41. // FOP
  42. import org.apache.fop.render.Renderer;
  43. import org.apache.fop.messaging.MessageHandler;
  44. import org.apache.fop.image.ImageArea;
  45. import org.apache.fop.image.FopImage;
  46. import org.apache.fop.apps.FOPException;
  47. import org.apache.fop.fo.properties.*;
  48. import org.apache.fop.layout.*;
  49. import org.apache.fop.datatypes.*;
  50. import org.apache.fop.svg.*;
  51. import org.apache.fop.pdf.*;
  52. // Java
  53. import java.io.IOException;
  54. import java.io.PrintWriter;
  55. import java.util.Enumeration;
  56. import java.awt.Rectangle;
  57. import java.util.Vector;
  58. /**
  59. * Renderer that renders areas to PDF
  60. */
  61. public class PDFRenderer implements Renderer {
  62. /** the PDF Document being created */
  63. protected PDFDocument pdfDoc;
  64. /** the /Resources object of the PDF document being created */
  65. protected PDFResources pdfResources;
  66. /** the current stream to add PDF commands to */
  67. PDFStream currentStream;
  68. /** the current annotation list to add annotations to */
  69. PDFAnnotList currentAnnotList;
  70. /** the current page to add annotations to */
  71. PDFPage currentPage;
  72. /** the current (internal) font name */
  73. protected String currentFontName;
  74. /** the current font size in millipoints */
  75. protected int currentFontSize;
  76. /** the current color/gradient for borders, letters, etc. */
  77. protected PDFPathPaint currentStroke = null;
  78. /** the current color/gradient to fill shapes with */
  79. protected PDFPathPaint currentFill = null;
  80. /** the current colour's red component */
  81. protected float currentRed = 0;
  82. /** the current colour's green component */
  83. protected float currentGreen = 0;
  84. /** the current colour's blue component */
  85. protected float currentBlue = 0;
  86. /** the current vertical position in millipoints from bottom */
  87. protected int currentYPosition = 0;
  88. /** the current horizontal position in millipoints from left */
  89. protected int currentXPosition = 0;
  90. /** the horizontal position of the current area container */
  91. private int currentAreaContainerXPosition = 0;
  92. /**
  93. * create the PDF renderer
  94. */
  95. public PDFRenderer() {
  96. this.pdfDoc = new PDFDocument();
  97. }
  98. /**
  99. * set the PDF document's producer
  100. *
  101. * @param producer string indicating application producing PDF
  102. */
  103. public void setProducer(String producer) {
  104. this.pdfDoc.setProducer(producer);
  105. }
  106. /**
  107. * render the areas into PDF
  108. *
  109. * @param areaTree the laid-out area tree
  110. * @param writer the PrintWriter to write the PDF with
  111. */
  112. public void render(AreaTree areaTree, PrintWriter writer)
  113. throws IOException, FOPException {
  114. MessageHandler.logln("rendering areas to PDF");
  115. IDReferences idReferences=areaTree.getIDReferences();
  116. this.pdfResources = this.pdfDoc.getResources();
  117. this.pdfDoc.setIDReferences(idReferences);
  118. Enumeration e = areaTree.getPages().elements();
  119. while ( e.hasMoreElements() ) {
  120. this.renderPage((Page) e.nextElement());
  121. }
  122. if ( !idReferences.isEveryIdValid() ) {
  123. throw new FOPException("The id \""+idReferences.getNextInvalidId()+"\" was referenced but does not exist\n");
  124. }
  125. MessageHandler.logln("writing out PDF");
  126. this.pdfDoc.output(writer);
  127. }
  128. /**
  129. * add a line to the current stream
  130. *
  131. * @param x1 the start x location in millipoints
  132. * @param y1 the start y location in millipoints
  133. * @param x2 the end x location in millipoints
  134. * @param y2 the end y location in millipoints
  135. * @param th the thickness in millipoints
  136. * @param stroke the stroke color/gradient
  137. */
  138. protected void addLine(int x1, int y1, int x2, int y2, int th,
  139. PDFPathPaint stroke) {
  140. currentStream.add(stroke.getColorSpaceOut(false)
  141. + (x1/1000f) + " " + (y1/1000f) + " m "
  142. + (x2/1000f) + " " + (y2/1000f) + " l "
  143. + (th/1000f) + " w S\n"
  144. + "0 0 0 RG\n");
  145. }
  146. /**
  147. * add a rectangle to the current stream
  148. *
  149. * @param x the x position of left edge in millipoints
  150. * @param y the y position of top edge in millipoints
  151. * @param w the width in millipoints
  152. * @param h the height in millipoints
  153. * @param stroke the stroke color/gradient
  154. */
  155. protected void addRect(int x, int y, int w, int h,
  156. PDFPathPaint stroke) {
  157. currentStream.add(stroke.getColorSpaceOut(false)
  158. + (x/1000f) + " " + (y/1000f) + " "
  159. + (w/1000f) + " " + (h/1000f) + " re S\n"
  160. + "0 0 0 RG\n");
  161. }
  162. /**
  163. * add a filled rectangle to the current stream
  164. *
  165. * @param x the x position of left edge in millipoints
  166. * @param y the y position of top edge in millipoints
  167. * @param w the width in millipoints
  168. * @param h the height in millipoints
  169. * @param fill the fill color/gradient
  170. * @param stroke the stroke color/gradient
  171. */
  172. protected void addRect(int x, int y, int w, int h,
  173. PDFPathPaint stroke,
  174. PDFPathPaint fill) {
  175. currentStream.add(fill.getColorSpaceOut(true)
  176. + stroke.getColorSpaceOut(false)
  177. + (x/1000f) + " " + (y/1000f) + " "
  178. + (w/1000f) + " " + (h/1000f) + " re S\n"
  179. + (x/1000f) + " " + (y/1000f) + " "
  180. + (w/1000f) + " " + (h/1000f) + " re f\n"
  181. + "0 0 0 RG 0 0 0 rg\n");
  182. }
  183. /**
  184. * render area container to PDF
  185. *
  186. * @param area the area container to render
  187. */
  188. public void renderAreaContainer(AreaContainer area) {
  189. int saveY = this.currentYPosition;
  190. int saveX = this.currentAreaContainerXPosition;
  191. if (area.getPosition() == Position.ABSOLUTE) {
  192. // Y position is computed assuming positive Y axis, adjust for negative postscript one
  193. this.currentYPosition = area.getYPosition() - 2 * area.getPaddingTop() - 2 * area.borderWidthTop;
  194. this.currentAreaContainerXPosition = area.getXPosition();
  195. } else if (area.getPosition() == Position.RELATIVE) {
  196. this.currentYPosition -= area.getYPosition();
  197. this.currentAreaContainerXPosition += area.getXPosition();
  198. } else if (area.getPosition() == Position.STATIC) {
  199. this.currentYPosition -= area.getPaddingTop() + area.borderWidthTop;
  200. this.currentAreaContainerXPosition += area.getPaddingLeft() + area.borderWidthLeft;
  201. }
  202. doFrame(area);
  203. Enumeration e = area.getChildren().elements();
  204. while (e.hasMoreElements()) {
  205. Box b = (Box) e.nextElement();
  206. b.render(this);
  207. }
  208. if (area.getPosition() != Position.STATIC) {
  209. this.currentYPosition = saveY;
  210. this.currentAreaContainerXPosition = saveX;
  211. } else
  212. this.currentYPosition -= area.getHeight();
  213. }
  214. private void doFrame(Area area) {
  215. int w, h;
  216. int rx = this.currentAreaContainerXPosition;
  217. w = area.getContentWidth();
  218. if (area instanceof BlockArea)
  219. rx += ((BlockArea)area).getStartIndent();
  220. h = area.getContentHeight();
  221. int ry = this.currentYPosition;
  222. ColorType bg = area.getBackgroundColor();
  223. rx = rx - area.getPaddingLeft();
  224. ry = ry + area.getPaddingTop();
  225. w = w + area.getPaddingLeft() + area.getPaddingRight();
  226. h = h + area.getPaddingTop() + area.getPaddingBottom();
  227. // I'm not sure I should have to check for bg being null
  228. // but I do
  229. if ((bg != null) && (bg.alpha() == 0)) {
  230. this.addRect(rx, ry, w, -h,
  231. new PDFColor(bg),
  232. new PDFColor(bg));
  233. }
  234. rx = rx - area.borderWidthLeft;
  235. ry = ry + area.borderWidthTop;
  236. w = w + area.borderWidthLeft + area.borderWidthRight;
  237. h = h + area.borderWidthTop + area.borderWidthBottom;
  238. if (area.borderWidthTop != 0)
  239. addLine(rx, ry, rx + w, ry,
  240. area.borderWidthTop,
  241. new PDFColor(area.borderColorTop));
  242. if (area.borderWidthLeft != 0)
  243. addLine(rx, ry, rx, ry - h,
  244. area.borderWidthLeft,
  245. new PDFColor(area.borderColorLeft));
  246. if (area.borderWidthRight != 0)
  247. addLine(rx + w, ry, rx + w, ry - h,
  248. area.borderWidthRight,
  249. new PDFColor(area.borderColorRight));
  250. if (area.borderWidthBottom != 0)
  251. addLine(rx, ry - h, rx + w, ry - h,
  252. area.borderWidthBottom,
  253. new PDFColor(area.borderColorBottom));
  254. }
  255. /**
  256. * render block area to PDF
  257. *
  258. * @param area the block area to render
  259. */
  260. public void renderBlockArea(BlockArea area) {
  261. doFrame(area);
  262. Enumeration e = area.getChildren().elements();
  263. while (e.hasMoreElements()) {
  264. Box b = (Box) e.nextElement();
  265. b.render(this);
  266. }
  267. }
  268. /**
  269. * render display space to PDF
  270. *
  271. * @param space the display space to render
  272. */
  273. public void renderDisplaySpace(DisplaySpace space) {
  274. int d = space.getSize();
  275. this.currentYPosition -= d;
  276. }
  277. /**
  278. * render image area to PDF
  279. *
  280. * @param area the image area to render
  281. */
  282. public void renderImageArea(ImageArea area) {
  283. // adapted from contribution by BoBoGi
  284. int x = this.currentAreaContainerXPosition +
  285. area.getXOffset();
  286. int y = this.currentYPosition;
  287. int w = area.getContentWidth();
  288. int h = area.getHeight();
  289. this.currentYPosition -= h;
  290. FopImage img = area.getImage();
  291. int xObjectNum = this.pdfDoc.addImage(img);
  292. currentStream.add("ET\nq\n" + (((float) w) / 1000f) + " 0 0 " +
  293. (((float) h) / 1000f) + " " +
  294. (((float) x) / 1000f) + " " +
  295. (((float) (y - h)) / 1000f) + " cm\n" +
  296. "/Im" + xObjectNum + " Do\nQ\nBT\n");
  297. }
  298. /**
  299. * render SVG area to PDF
  300. *
  301. * @param area the SVG area to render
  302. */
  303. public void renderSVGArea(SVGArea area) {
  304. int x = this.currentAreaContainerXPosition;
  305. int y = this.currentYPosition;
  306. int w = area.getContentWidth();
  307. int h = area.getHeight();
  308. this.currentYPosition -= h;
  309. Enumeration e = area.getChildren().elements();
  310. while (e.hasMoreElements()) {
  311. Object o = e.nextElement();
  312. if (o instanceof RectGraphic) {
  313. int rx = ((RectGraphic)o).x;
  314. int ry = ((RectGraphic)o).y;
  315. int rw = ((RectGraphic)o).width;
  316. int rh = ((RectGraphic)o).height;
  317. addRect(x+rx,y-ry,rw,-rh,new PDFColor(0,0,0));
  318. } else if (o instanceof LineGraphic) {
  319. int x1 = ((LineGraphic)o).x1;
  320. int y1 = ((LineGraphic)o).y1;
  321. int x2 = ((LineGraphic)o).x2;
  322. int y2 = ((LineGraphic)o).y2;
  323. addLine(x+x1,y-y1,x+x2,y-y2,0,new PDFColor(0,0,0));
  324. } else if (o instanceof TextGraphic) {
  325. int tx = ((TextGraphic)o).x;
  326. int ty = ((TextGraphic)o).y;
  327. String s = ((TextGraphic)o).s;
  328. currentStream.add("1 0 0 1 "
  329. + ((x+tx)/1000f) + " "
  330. + ((y-ty)/1000f) + " Tm "
  331. + "(" + s + ") Tj\n");
  332. }
  333. }
  334. }
  335. /**
  336. * render inline area to PDF
  337. *
  338. * @param area inline area to render
  339. */
  340. public void renderInlineArea(InlineArea area) {
  341. char ch;
  342. StringBuffer pdf = new StringBuffer();
  343. String name = area.getFontState().getFontName();
  344. int size = area.getFontState().getFontSize();
  345. PDFColor theAreaColor = new PDFColor(
  346. (double)area.getRed(),
  347. (double)area.getGreen(),
  348. (double)area.getBlue() );
  349. if ((!name.equals(this.currentFontName))
  350. || (size != this.currentFontSize)) {
  351. this.currentFontName = name;
  352. this.currentFontSize = size;
  353. pdf = pdf.append("/" + name + " " + (size/1000) + " Tf\n");
  354. }
  355. //if (theAreaColor.isEquivalent(this.currentFill)) {
  356. this.currentFill = theAreaColor;
  357. pdf = pdf.append(this.currentFill.getColorSpaceOut(true));
  358. //}
  359. int rx = this.currentXPosition;
  360. int bl = this.currentYPosition;
  361. pdf = pdf.append("1 0 0 1 "
  362. +(rx/1000f) + " " + (bl/1000f)
  363. + " Tm (");
  364. String s = area.getText();
  365. int l = s.length();
  366. for (int i=0; i < l; i++) {
  367. ch = s.charAt(i);
  368. if (ch > 127) {
  369. pdf = pdf.append("\\");
  370. pdf = pdf.append(Integer.toOctalString((int)ch));
  371. } else {
  372. switch (ch) {
  373. case '(' : pdf = pdf.append("\\("); break;
  374. case ')' : pdf = pdf.append("\\)"); break;
  375. case '\\' : pdf = pdf.append("\\\\"); break;
  376. default : pdf = pdf.append(ch); break;
  377. }
  378. }
  379. }
  380. pdf = pdf.append(") Tj\n");
  381. currentStream.add(pdf.toString());
  382. this.currentXPosition += area.getContentWidth();
  383. }
  384. /**
  385. * render inline space to PDF
  386. *
  387. * @param space space to render
  388. */
  389. public void renderInlineSpace(InlineSpace space) {
  390. this.currentXPosition += space.getSize();
  391. }
  392. /**
  393. * render line area to PDF
  394. *
  395. * @param area area to render
  396. */
  397. public void renderLineArea(LineArea area) {
  398. int rx = this.currentAreaContainerXPosition
  399. + area.getStartIndent();
  400. int ry = this.currentYPosition;
  401. int w = area.getContentWidth();
  402. int h = area.getHeight();
  403. this.currentYPosition -= area.getPlacementOffset();
  404. this.currentXPosition = rx;
  405. int bl = this.currentYPosition;
  406. Enumeration e = area.getChildren().elements();
  407. while (e.hasMoreElements()) {
  408. Box b = (Box) e.nextElement();
  409. b.render(this);
  410. }
  411. this.currentYPosition = ry-h;
  412. }
  413. /**
  414. * render page into PDF
  415. *
  416. * @param page page to render
  417. */
  418. public void renderPage(Page page) {
  419. AreaContainer body, before, after;
  420. currentStream = this.pdfDoc.makeStream();
  421. body = page.getBody();
  422. before = page.getBefore();
  423. after = page.getAfter();
  424. this.currentFontName = "";
  425. this.currentFontSize = 0;
  426. currentStream.add("BT\n");
  427. renderAreaContainer(body);
  428. if (before != null) {
  429. renderAreaContainer(before);
  430. }
  431. if (after != null) {
  432. renderAreaContainer(after);
  433. }
  434. currentStream.add("ET\n");
  435. currentPage = this.pdfDoc.makePage(this.pdfResources, currentStream,
  436. page.getWidth()/1000,
  437. page.getHeight()/1000, page);
  438. if (page.hasLinks()) {
  439. currentAnnotList = this.pdfDoc.makeAnnotList();
  440. currentPage.setAnnotList(currentAnnotList);
  441. Enumeration e = page.getLinkSets().elements();
  442. while (e.hasMoreElements()) {
  443. LinkSet linkSet = (LinkSet) e.nextElement();
  444. linkSet.align();
  445. String dest = linkSet.getDest();
  446. int linkType = linkSet.getLinkType();
  447. Enumeration f = linkSet.getRects().elements();
  448. while (f.hasMoreElements()) {
  449. LinkedRectangle lrect = (LinkedRectangle) f.nextElement();
  450. currentAnnotList.addLink(
  451. this.pdfDoc.makeLink(lrect.getRectangle(), dest, linkType));
  452. }
  453. }
  454. } else {
  455. // just to be on the safe side
  456. currentAnnotList = null;
  457. }
  458. }
  459. /**
  460. * render rule area into PDF
  461. *
  462. * @param area area to render
  463. */
  464. public void renderRuleArea(RuleArea area) {
  465. int rx = this.currentAreaContainerXPosition
  466. + area.getStartIndent();
  467. int ry = this.currentYPosition;
  468. int w = area.getContentWidth();
  469. int h = area.getHeight();
  470. int th = area.getRuleThickness();
  471. addLine(rx, ry, rx+w, ry, th, new PDFColor(area.getRed(), area.getGreen(),area.getBlue()));
  472. }
  473. /**
  474. * set up the font info
  475. *
  476. * @param fontInfo font info to set up
  477. */
  478. public void setupFontInfo(FontInfo fontInfo) {
  479. FontSetup.setup(fontInfo);
  480. FontSetup.addToResources(this.pdfDoc, fontInfo);
  481. }
  482. }