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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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.layout.inline.*;
  50. import org.apache.fop.datatypes.*;
  51. import org.apache.fop.svg.PathPoint;
  52. import org.apache.fop.pdf.*;
  53. import org.apache.fop.layout.*;
  54. import org.apache.fop.image.*;
  55. import org.apache.fop.configuration.Configuration;
  56. import org.apache.fop.extensions.*;
  57. import org.apache.fop.datatypes.IDReferences;
  58. import org.w3c.dom.*;
  59. import org.w3c.dom.svg.*;
  60. import org.w3c.dom.css.*;
  61. import org.w3c.dom.svg.SVGLength;
  62. import org.apache.fop.dom.svg.*;
  63. import org.apache.fop.dom.svg.SVGRectElementImpl;
  64. import org.apache.fop.dom.svg.SVGTextElementImpl;
  65. import org.apache.fop.dom.svg.SVGLineElementImpl;
  66. import org.apache.fop.dom.svg.SVGArea;
  67. // Java
  68. import java.io.IOException;
  69. import java.io.OutputStream;
  70. import java.util.Enumeration;
  71. import java.awt.Rectangle;
  72. import java.util.Vector;
  73. import java.util.Hashtable;
  74. /**
  75. * Renderer that renders areas to PDF
  76. */
  77. public class PDFRenderer implements Renderer {
  78. private static final boolean OPTIMIZE_TEXT = true;
  79. /** the PDF Document being created */
  80. protected PDFDocument pdfDoc;
  81. /** the /Resources object of the PDF document being created */
  82. protected PDFResources pdfResources;
  83. /** the IDReferences for this document */
  84. protected IDReferences idReferences;
  85. /** the current stream to add PDF commands to */
  86. PDFStream currentStream;
  87. /** the current annotation list to add annotations to */
  88. PDFAnnotList currentAnnotList;
  89. /** the current page to add annotations to */
  90. PDFPage currentPage;
  91. /** the current (internal) font name */
  92. protected String currentFontName;
  93. /** the current font size in millipoints */
  94. protected int currentFontSize;
  95. /** the current color/gradient for borders, letters, etc. */
  96. protected PDFPathPaint currentStroke = null;
  97. /** the current color/gradient to fill shapes with */
  98. protected PDFPathPaint currentFill = null;
  99. /** the current colour's red component */
  100. protected float currentRed = 0;
  101. /** the current colour's green component */
  102. protected float currentGreen = 0;
  103. /** the current colour's blue component */
  104. protected float currentBlue = 0;
  105. /** the current vertical position in millipoints from bottom */
  106. protected int currentYPosition = 0;
  107. /** the current horizontal position in millipoints from left */
  108. protected int currentXPosition = 0;
  109. /** the horizontal position of the current area container */
  110. private int currentAreaContainerXPosition = 0;
  111. /** the current colour for use in svg */
  112. private PDFColor currentColour = new PDFColor(0, 0, 0);
  113. // previous values used for text-decoration drawing
  114. int prevUnderlineXEndPos;
  115. int prevUnderlineYEndPos;
  116. int prevUnderlineSize;
  117. PDFColor prevUnderlineColor;
  118. int prevOverlineXEndPos;
  119. int prevOverlineYEndPos;
  120. int prevOverlineSize;
  121. PDFColor prevOverlineColor;
  122. int prevLineThroughXEndPos;
  123. int prevLineThroughYEndPos;
  124. int prevLineThroughSize;
  125. PDFColor prevLineThroughColor;
  126. /** true if a TJ command is left to be written */
  127. boolean textOpen = false;
  128. /** the previous Y coordinate of the last word written.
  129. Used to decide if we can draw the next word on the same line. */
  130. int prevWordY = 0;
  131. /** the previous X coordinate of the last word written.
  132. used to calculate how much space between two words */
  133. int prevWordX = 0;
  134. /** The width of the previous word. Used to calculate space between */
  135. int prevWordWidth = 0;
  136. boolean useKerning;
  137. private PDFOutline rootOutline;
  138. /**
  139. * create the PDF renderer
  140. */
  141. public PDFRenderer() {
  142. this.pdfDoc = new PDFDocument();
  143. String cfgKern = Configuration.getStringValue("use-kerning");
  144. useKerning = ("yes".equals(cfgKern) || "true".equals(cfgKern));
  145. }
  146. /**
  147. * set the PDF document's producer
  148. *
  149. * @param producer string indicating application producing PDF
  150. */
  151. public void setProducer(String producer) {
  152. this.pdfDoc.setProducer(producer);
  153. }
  154. /**
  155. * render the areas into PDF
  156. *
  157. * @param areaTree the laid-out area tree
  158. * @param stream the OutputStream to write the PDF to
  159. */
  160. public void render(AreaTree areaTree,
  161. OutputStream stream) throws IOException, FOPException {
  162. MessageHandler.logln("rendering areas to PDF");
  163. idReferences = areaTree.getIDReferences();
  164. this.pdfResources = this.pdfDoc.getResources();
  165. this.pdfDoc.setIDReferences(idReferences);
  166. Enumeration e = areaTree.getPages().elements();
  167. while (e.hasMoreElements()) {
  168. this.renderPage((Page) e.nextElement());
  169. }
  170. if (!idReferences.isEveryIdValid()) {
  171. // throw new FOPException("The following id's were referenced but not found: "+idReferences.getInvalidIds()+"\n");
  172. MessageHandler.errorln("WARNING: The following id's were referenced but not found: "+
  173. idReferences.getInvalidIds() + "\n");
  174. }
  175. renderRootExtensions(areaTree);
  176. MessageHandler.logln("writing out PDF");
  177. this.pdfDoc.output(stream);
  178. }
  179. /**
  180. * add a line to the current stream
  181. *
  182. * @param x1 the start x location in millipoints
  183. * @param y1 the start y location in millipoints
  184. * @param x2 the end x location in millipoints
  185. * @param y2 the end y location in millipoints
  186. * @param th the thickness in millipoints
  187. * @param r the red component
  188. * @param g the green component
  189. * @param b the blue component
  190. */
  191. protected void addLine(int x1, int y1, int x2, int y2, int th,
  192. PDFPathPaint stroke) {
  193. closeText();
  194. currentStream.add("ET\nq\n" + stroke.getColorSpaceOut(false) +
  195. (x1 / 1000f) + " "+ (y1 / 1000f) + " m " +
  196. (x2 / 1000f) + " "+ (y2 / 1000f) + " l " +
  197. (th / 1000f) + " w S\n" + "Q\nBT\n");
  198. }
  199. /**
  200. * add a line to the current stream
  201. *
  202. * @param x1 the start x location in millipoints
  203. * @param y1 the start y location in millipoints
  204. * @param x2 the end x location in millipoints
  205. * @param y2 the end y location in millipoints
  206. * @param th the thickness in millipoints
  207. * @param rs the rule style as String containing dashArray + dashPhase
  208. * @param r the red component
  209. * @param g the green component
  210. * @param b the blue component
  211. */
  212. protected void addLine(int x1, int y1, int x2, int y2, int th,
  213. String rs, PDFPathPaint stroke) {
  214. closeText();
  215. currentStream.add("ET\nq\n" + stroke.getColorSpaceOut(false) +
  216. rs + (x1 / 1000f) + " "+ (y1 / 1000f) + " m " +
  217. (x2 / 1000f) + " "+ (y2 / 1000f) + " l " +
  218. (th / 1000f) + " w S\n" + "Q\nBT\n");
  219. }
  220. /**
  221. * add a rectangle to the current stream
  222. *
  223. * @param x the x position of left edge in millipoints
  224. * @param y the y position of top edge in millipoints
  225. * @param w the width in millipoints
  226. * @param h the height in millipoints
  227. * @param stroke the stroke color/gradient
  228. */
  229. protected void addRect(int x, int y, int w, int h,
  230. PDFPathPaint stroke) {
  231. closeText();
  232. currentStream.add("ET\nq\n" + stroke.getColorSpaceOut(false) +
  233. (x / 1000f) + " " + (y / 1000f) + " " + (w / 1000f) +
  234. " " + (h / 1000f) + " re s\n" + "Q\nBT\n");
  235. }
  236. /**
  237. * add a filled rectangle to the current stream
  238. *
  239. * @param x the x position of left edge in millipoints
  240. * @param y the y position of top edge in millipoints
  241. * @param w the width in millipoints
  242. * @param h the height in millipoints
  243. * @param fill the fill color/gradient
  244. * @param stroke the stroke color/gradient
  245. */
  246. protected void addRect(int x, int y, int w, int h,
  247. PDFPathPaint stroke, PDFPathPaint fill) {
  248. closeText();
  249. currentStream.add("ET\nq\n" + fill.getColorSpaceOut(true) +
  250. stroke.getColorSpaceOut(false) + (x / 1000f) + " " +
  251. (y / 1000f) + " " + (w / 1000f) + " " + (h / 1000f) +
  252. " re b\n" + "Q\nBT\n");
  253. }
  254. /**
  255. * render area container to PDF
  256. *
  257. * @param area the area container to render
  258. */
  259. public void renderAreaContainer(AreaContainer area) {
  260. int saveY = this.currentYPosition;
  261. int saveX = this.currentAreaContainerXPosition;
  262. if (area.getPosition() == Position.ABSOLUTE) {
  263. // Y position is computed assuming positive Y axis, adjust for negative postscript one
  264. this.currentYPosition =
  265. area.getYPosition() - 2 * area.getPaddingTop() -
  266. 2 * area.borderWidthTop;
  267. this.currentAreaContainerXPosition = area.getXPosition();
  268. } else if (area.getPosition() == Position.RELATIVE) {
  269. this.currentYPosition -= area.getYPosition();
  270. this.currentAreaContainerXPosition += area.getXPosition();
  271. } else if (area.getPosition() == Position.STATIC) {
  272. this.currentYPosition -=
  273. area.getPaddingTop() + area.borderWidthTop;
  274. this.currentAreaContainerXPosition +=
  275. area.getPaddingLeft() + area.borderWidthLeft;
  276. }
  277. this.currentXPosition = this.currentAreaContainerXPosition;
  278. doFrame(area);
  279. Enumeration e = area.getChildren().elements();
  280. while (e.hasMoreElements()) {
  281. Box b = (Box) e.nextElement();
  282. b.render(this);
  283. }
  284. if (area.getPosition() != Position.STATIC) {
  285. this.currentYPosition = saveY;
  286. this.currentAreaContainerXPosition = saveX;
  287. } else
  288. this.currentYPosition -= area.getHeight();
  289. }
  290. public void renderBodyAreaContainer(BodyAreaContainer area) {
  291. int saveY = this.currentYPosition;
  292. int saveX = this.currentAreaContainerXPosition;
  293. if (area.getPosition() == Position.ABSOLUTE) {
  294. // Y position is computed assuming positive Y axis, adjust for negative postscript one
  295. this.currentYPosition = area.getYPosition();
  296. this.currentAreaContainerXPosition = area.getXPosition();
  297. } else if (area.getPosition() == Position.RELATIVE) {
  298. this.currentYPosition -= area.getYPosition();
  299. this.currentAreaContainerXPosition += area.getXPosition();
  300. }
  301. this.currentXPosition = this.currentAreaContainerXPosition;
  302. int w, h;
  303. int rx = this.currentAreaContainerXPosition;
  304. w = area.getContentWidth();
  305. h = area.getContentHeight();
  306. int ry = this.currentYPosition;
  307. ColorType bg = area.getBackgroundColor();
  308. // I'm not sure I should have to check for bg being null
  309. // but I do
  310. if ((bg != null) && (bg.alpha() == 0)) {
  311. this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
  312. }
  313. // floats & footnotes stuff
  314. renderAreaContainer(area.getBeforeFloatReferenceArea());
  315. renderAreaContainer(area.getFootnoteReferenceArea());
  316. // main reference area
  317. Enumeration e = area.getMainReferenceArea().getChildren().elements();
  318. while (e.hasMoreElements()) {
  319. Box b = (Box) e.nextElement();
  320. b.render(this); // span areas
  321. }
  322. if (area.getPosition() != Position.STATIC) {
  323. this.currentYPosition = saveY;
  324. this.currentAreaContainerXPosition = saveX;
  325. } else
  326. this.currentYPosition -= area.getHeight();
  327. }
  328. public void renderSpanArea(SpanArea area) {
  329. Enumeration e = area.getChildren().elements();
  330. while (e.hasMoreElements()) {
  331. Box b = (Box) e.nextElement();
  332. b.render(this); // column areas
  333. }
  334. }
  335. private void doFrame(Area area) {
  336. int w, h;
  337. int rx = this.currentAreaContainerXPosition;
  338. w = area.getContentWidth();
  339. if (area instanceof BlockArea)
  340. rx += ((BlockArea) area).getStartIndent();
  341. h = area.getContentHeight();
  342. int ry = this.currentYPosition;
  343. ColorType bg = area.getBackgroundColor();
  344. rx = rx - area.getPaddingLeft();
  345. ry = ry + area.getPaddingTop();
  346. w = w + area.getPaddingLeft() + area.getPaddingRight();
  347. h = h + area.getPaddingTop() + area.getPaddingBottom();
  348. // I'm not sure I should have to check for bg being null
  349. // but I do
  350. if ((bg != null) && (bg.alpha() == 0)) {
  351. this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
  352. }
  353. rx = rx - area.borderWidthLeft;
  354. ry = ry + area.borderWidthTop;
  355. w = w + area.borderWidthLeft + area.borderWidthRight;
  356. h = h + area.borderWidthTop + area.borderWidthBottom;
  357. if (area.borderWidthTop != 0)
  358. addLine(rx, ry, rx + w, ry, area.borderWidthTop,
  359. new PDFColor(area.borderColorTop));
  360. if (area.borderWidthLeft != 0)
  361. addLine(rx, ry, rx, ry - h, area.borderWidthLeft,
  362. new PDFColor(area.borderColorLeft));
  363. if (area.borderWidthRight != 0)
  364. addLine(rx + w, ry, rx + w, ry - h, area.borderWidthRight,
  365. new PDFColor(area.borderColorRight));
  366. if (area.borderWidthBottom != 0)
  367. addLine(rx, ry - h, rx + w, ry - h, area.borderWidthBottom,
  368. new PDFColor(area.borderColorBottom));
  369. }
  370. /**
  371. * render block area to PDF
  372. *
  373. * @param area the block area to render
  374. */
  375. public void renderBlockArea(BlockArea area) {
  376. // KLease: Temporary test to fix block positioning
  377. // Offset ypos by padding and border widths
  378. // this.currentYPosition -= (area.getPaddingTop() + area.borderWidthTop);
  379. doFrame(area);
  380. Enumeration e = area.getChildren().elements();
  381. while (e.hasMoreElements()) {
  382. Box b = (Box) e.nextElement();
  383. b.render(this);
  384. }
  385. // this.currentYPosition -= (area.getPaddingBottom() + area.borderWidthBottom);
  386. }
  387. /**
  388. * render display space to PDF
  389. *
  390. * @param space the display space to render
  391. */
  392. public void renderDisplaySpace(DisplaySpace space) {
  393. int d = space.getSize();
  394. this.currentYPosition -= d;
  395. }
  396. /**
  397. * render image area to PDF
  398. *
  399. * @param area the image area to render
  400. */
  401. public void renderImageArea(ImageArea area) {
  402. // adapted from contribution by BoBoGi
  403. int x = this.currentAreaContainerXPosition + area.getXOffset();
  404. int y = this.currentYPosition;
  405. int w = area.getContentWidth();
  406. int h = area.getHeight();
  407. this.currentYPosition -= h;
  408. FopImage img = area.getImage();
  409. if (img instanceof SVGImage) {
  410. try {
  411. closeText();
  412. SVGSVGElement svg =
  413. ((SVGImage) img).getSVGDocument().getRootElement();
  414. currentStream.add("ET\nq\n" + (((float) w) / 1000f) +
  415. " 0 0 " + (((float) h) / 1000f) + " " +
  416. (((float) x) / 1000f) + " " +
  417. (((float)(y - h)) / 1000f) + " cm\n");
  418. // renderSVG(svg, (int) x, (int) y);
  419. currentStream.add("Q\nBT\n");
  420. } catch (FopImageException e) {
  421. }
  422. } else {
  423. int xObjectNum = this.pdfDoc.addImage(img);
  424. closeText();
  425. currentStream.add("ET\nq\n" + (((float) w) / 1000f) +
  426. " 0 0 " + (((float) h) / 1000f) + " " +
  427. (((float) x) / 1000f) + " " +
  428. (((float)(y - h)) / 1000f) + " cm\n" + "/Im" +
  429. xObjectNum + " Do\nQ\nBT\n");
  430. }
  431. }
  432. /** render a foreign object area */
  433. public void renderForeignObjectArea(ForeignObjectArea area) {
  434. // if necessary need to scale and align the content
  435. this.currentXPosition = this.currentXPosition + area.getXOffset();
  436. this.currentYPosition = this.currentYPosition;
  437. switch (area.getAlign()) {
  438. case TextAlign.START:
  439. break;
  440. case TextAlign.END:
  441. break;
  442. case TextAlign.CENTER:
  443. case TextAlign.JUSTIFY:
  444. break;
  445. }
  446. switch (area.getVerticalAlign()) {
  447. case VerticalAlign.BASELINE:
  448. break;
  449. case VerticalAlign.MIDDLE:
  450. break;
  451. case VerticalAlign.SUB:
  452. break;
  453. case VerticalAlign.SUPER:
  454. break;
  455. case VerticalAlign.TEXT_TOP:
  456. break;
  457. case VerticalAlign.TEXT_BOTTOM:
  458. break;
  459. case VerticalAlign.TOP:
  460. break;
  461. case VerticalAlign.BOTTOM:
  462. break;
  463. }
  464. closeText();
  465. // in general the content will not be text
  466. currentStream.add("ET\n");
  467. // align and scale
  468. currentStream.add("q\n");
  469. switch (area.scalingMethod()) {
  470. case Scaling.UNIFORM:
  471. break;
  472. case Scaling.NON_UNIFORM:
  473. break;
  474. }
  475. // if the overflow is auto (default), scroll or visible
  476. // then the contents should not be clipped, since this
  477. // is considered a printing medium.
  478. switch (area.getOverflow()) {
  479. case Overflow.VISIBLE:
  480. case Overflow.SCROLL:
  481. case Overflow.AUTO:
  482. break;
  483. case Overflow.HIDDEN:
  484. break;
  485. }
  486. area.getObject().render(this);
  487. currentStream.add("Q\n");
  488. currentStream.add("BT\n");
  489. this.currentXPosition += area.getEffectiveWidth();
  490. // this.currentYPosition -= area.getEffectiveHeight();
  491. }
  492. /**
  493. * render SVG area to PDF
  494. *
  495. * @param area the SVG area to render
  496. */
  497. public void renderSVGArea(SVGArea area) {
  498. // Buggy: Will align to the beginning of the last text
  499. //int x = this.currentXPosition;
  500. // Buggy: Will always left align
  501. int x = this.currentAreaContainerXPosition;
  502. // Buggy: Method getXOffset() not found in class org.apache.fop.dom.svg.SVGArea
  503. //int x = this.currentAreaContainerXPosition + area.getXOffset();
  504. int y = this.currentYPosition;
  505. SVGSVGElement svg = area.getSVGDocument().getRootElement();
  506. int w = (int)(svg.getWidth().getBaseVal().getValue() * 1000);
  507. int h = (int)(svg.getHeight().getBaseVal().getValue() * 1000);
  508. float sx = 1, sy = -1;
  509. int xOffset = x, yOffset = y;
  510. // translate and scale according to viewbox.
  511. if (svg.getViewBox () != null) {
  512. SVGRect view = svg.getViewBox().getBaseVal();
  513. /*
  514. System.out.println ("viewbox=\""+
  515. view.getX()+" "+
  516. view.getY()+" "+
  517. view.getWidth()+" "+
  518. view.getHeight()+"\"");
  519. */
  520. // TODO take aspect constraints (attribute preserveAspectRatio)
  521. // into account.
  522. // Viewbox coordinates are all relative to the viewport
  523. // (ie. the x,y,w and h values calculated above).
  524. sx = svg.getWidth().getBaseVal().getValue() / view.getWidth ();
  525. sy = svg.getHeight().getBaseVal().getValue() / view.getHeight ();
  526. // move the origin
  527. xOffset -= (int)(sx * view.getX () * 1000f);
  528. yOffset -= (int)(sy * view.getY () * 1000f);
  529. sy = -sy;
  530. }
  531. System.out.println ("viewBox: offsets="+
  532. xOffset+" (x="+x+") "+
  533. yOffset+" (y="+y+") scale="+
  534. sx+" "+
  535. sy+" w="+w+" h="+h
  536. );
  537. /*
  538. * Clip to the svg area.
  539. * Note: To have the svg overlay (under) a text area then use
  540. * an fo:block-container
  541. */
  542. currentStream.add("q\n");
  543. if (w != 0 && h != 0) {
  544. currentStream.add(x / 1000f + " " + y / 1000f + " m\n");
  545. currentStream.add((x + w) / 1000f + " " + y / 1000f + " l\n");
  546. currentStream.add((x + w) / 1000f + " " + (y - h) / 1000f + " l\n");
  547. currentStream.add(x / 1000f + " " + (y - h) / 1000f + " l\n");
  548. currentStream.add("h\n");
  549. currentStream.add("W\n");
  550. currentStream.add("n\n");
  551. }
  552. // transform so that the coordinates (0,0) is from the top left
  553. // and positive is down and to the right. (0,0) is where the
  554. // viewBox puts it.
  555. currentStream.add(sx + " 0 0 " + sy + " " +
  556. xOffset / 1000f + " " + yOffset / 1000f + " cm\n");
  557. SVGRenderer svgRenderer =
  558. new SVGRenderer(area.getFontState(), pdfDoc,
  559. currentFontName, currentFontSize, currentXPosition,
  560. currentYPosition);
  561. svgRenderer.renderSVG(svg, 0, 0);
  562. currentStream.add(svgRenderer.getString());
  563. currentStream.add("Q\n");
  564. }
  565. /**
  566. * render inline area to PDF
  567. *
  568. * @param area inline area to render
  569. */
  570. public void renderWordArea(WordArea area) {
  571. // char ch;
  572. StringBuffer pdf = new StringBuffer();
  573. Hashtable kerning = null;
  574. boolean kerningAvailable = false;
  575. if (useKerning) {
  576. kerning = area.getFontState().getKerning();
  577. if (kerning != null && !kerning.isEmpty()) {
  578. kerningAvailable = true;
  579. }
  580. }
  581. String name = area.getFontState().getFontName();
  582. int size = area.getFontState().getFontSize();
  583. PDFColor theAreaColor = new PDFColor((double) area.getRed(),
  584. (double) area.getGreen(),
  585. (double) area.getBlue());
  586. if ((!name.equals(this.currentFontName)) ||
  587. (size != this.currentFontSize)) {
  588. closeText();
  589. this.currentFontName = name;
  590. this.currentFontSize = size;
  591. pdf = pdf.append("/" + name + " " + (size / 1000) + " Tf\n");
  592. }
  593. if (!(theAreaColor.equals(this.currentFill))) {
  594. closeText();
  595. this.currentFill = theAreaColor;
  596. pdf.append(this.currentFill.getColorSpaceOut(true));
  597. }
  598. int rx = this.currentXPosition;
  599. int bl = this.currentYPosition;
  600. if (area.getUnderlined()) {
  601. int yPos = bl - size/10;
  602. addLine(rx, yPos, rx + area.getContentWidth(),
  603. yPos, size/14, theAreaColor);
  604. // save position for underlining a following InlineSpace
  605. prevUnderlineXEndPos = rx + area.getContentWidth();
  606. prevUnderlineYEndPos = yPos;
  607. prevUnderlineSize = size/14;
  608. prevUnderlineColor = theAreaColor;
  609. }
  610. if (area.getOverlined()) {
  611. int yPos = bl + area.getFontState().getAscender() + size/10;
  612. addLine(rx, yPos, rx + area.getContentWidth(),
  613. yPos, size/14, theAreaColor);
  614. prevOverlineXEndPos = rx + area.getContentWidth();
  615. prevOverlineYEndPos = yPos;
  616. prevOverlineSize = size/14;
  617. prevOverlineColor = theAreaColor;
  618. }
  619. if (area.getLineThrough()) {
  620. int yPos = bl + area.getFontState().getAscender() * 3/8;
  621. addLine(rx, yPos, rx + area.getContentWidth(),
  622. yPos, size/14, theAreaColor);
  623. prevLineThroughXEndPos = rx + area.getContentWidth();
  624. prevLineThroughYEndPos = yPos;
  625. prevLineThroughSize = size/14;
  626. prevLineThroughColor = theAreaColor;
  627. }
  628. if (OPTIMIZE_TEXT) {
  629. if (!textOpen || bl != prevWordY) {
  630. closeText();
  631. pdf.append("1 0 0 1 " +(rx / 1000f) + " " +
  632. (bl / 1000f) + " Tm [(");
  633. prevWordY = bl;
  634. textOpen = true;
  635. }
  636. else {
  637. // express the space between words in thousandths of an em
  638. int space = prevWordX - rx + prevWordWidth;
  639. float emDiff = (float)space / (float)currentFontSize * 1000f;
  640. pdf.append(emDiff + " (");
  641. }
  642. prevWordWidth = area.getContentWidth();
  643. prevWordX = rx;
  644. }
  645. else {
  646. // original text render code sets the text transformation matrix
  647. // for every word.
  648. pdf.append("1 0 0 1 " +(rx / 1000f) + " " + (bl / 1000f) + " Tm ");
  649. if (kerningAvailable) {
  650. pdf.append(" [(");
  651. }
  652. else {
  653. pdf.append(" (");
  654. }
  655. }
  656. String s;
  657. if (area.getPageNumberID() != null) { // this text is a page number, so resolve it
  658. s = idReferences.getPageNumber(area.getPageNumberID());
  659. if (s == null) {
  660. s = "";
  661. }
  662. } else {
  663. s = area.getText();
  664. }
  665. int l = s.length();
  666. // ForJAPANIZE
  667. Font f = (Font)area.getFontState().getFontInfo().getFonts().get(name);
  668. if ( f instanceof CIDFont ) {
  669. String enc = ((CIDFont)f).getCharEncoding();
  670. try {
  671. s = new String(s.getBytes(enc), "8859_1");
  672. } catch (java.io.UnsupportedEncodingException ex) {
  673. }
  674. if ( ((CIDFont)f).getCidType() == CIDFont.CIDFontType2 ) {
  675. // CIDFontType2
  676. pdf = pdf.append("< ");
  677. for (int i=0; i < s.length(); i++) {
  678. char ch = s.charAt(i);
  679. pdf = pdf.append(Integer.toHexString((int)ch));
  680. }
  681. pdf = pdf.append(" >");
  682. pdf = pdf.append(" Tj\n");
  683. } else {
  684. // CIDFontType0
  685. CMap cmap = ((CIDFont)f).getCMap();
  686. FontState fState = area.getFontState();
  687. StringBuffer cidStr = new StringBuffer();
  688. cidStr.append("<");
  689. for (int i=0; i < s.length(); i++) {
  690. char ch = s.charAt(i);
  691. if (!((ch >= 0x00 && 0x80 >= ch) ||
  692. (ch >= 0xA0 && 0xDF >= ch)) ) {
  693. i++;
  694. ch = (char)(((ch << 8) & 0xFF00) + s.charAt(i));
  695. }
  696. ch = cmap.mapping(ch);
  697. String hexS = Integer.toHexString(ch);
  698. hexS = "0000".substring(hexS.length()-1,3)+hexS;
  699. cidStr.append(hexS);
  700. }
  701. cidStr.append(">Tj\n");
  702. if ( "italic".equals(fState.getFontStyle()) ) {
  703. // Italic
  704. pdf = pdf.append("1 0 0.32634 1 ");
  705. pdf = pdf.append((rx / 1000f));
  706. pdf = pdf.append(" ");
  707. pdf = pdf.append((bl / 1000f));
  708. pdf = pdf.append(" Tm\n");
  709. }
  710. if ( "bold".equals(fState.getFontWeight()) ) {
  711. // Bold
  712. float boldsize = ((float)size) / 1000000f;
  713. pdf = pdf.append(boldsize);
  714. pdf = pdf.append(" ");
  715. pdf = pdf.append(-(boldsize));
  716. pdf = pdf.append(" TD\n");
  717. pdf = pdf.append(cidStr.toString());
  718. pdf = pdf.append(-boldsize);
  719. pdf = pdf.append(" ");
  720. pdf = pdf.append(-boldsize);
  721. pdf = pdf.append(" TD\n");
  722. pdf = pdf.append(cidStr.toString());
  723. pdf = pdf.append(boldsize);
  724. pdf = pdf.append(" 0 TD\n");
  725. pdf = pdf.append(cidStr.toString());
  726. pdf = pdf.append(-boldsize);
  727. pdf = pdf.append(" ");
  728. pdf = pdf.append(boldsize);
  729. pdf = pdf.append(" TD\n");
  730. }
  731. pdf = pdf.append(cidStr.toString());
  732. }
  733. } else {
  734. for (int i = 0; i < l; i++) {
  735. char ch = s.charAt(i);
  736. String prepend = "";
  737. if (ch > 127) {
  738. pdf.append("\\");
  739. pdf.append(Integer.toOctalString((int) ch));
  740. } else {
  741. switch (ch) {
  742. case '(':
  743. case ')':
  744. case '\\':
  745. prepend = "\\";
  746. break;
  747. }
  748. pdf.append(prepend+ch);
  749. }
  750. if (kerningAvailable && (i+1) < l) {
  751. pdf.append(addKerning((new Character(ch)).toString(),
  752. (new Character(s.charAt(i+1))).toString(),
  753. kerning));
  754. }
  755. }
  756. }
  757. pdf.append(") ");
  758. if (!OPTIMIZE_TEXT) {
  759. if (kerningAvailable) {
  760. pdf.append("] TJ\n");
  761. }
  762. else {
  763. pdf.append("Tj\n");
  764. }
  765. }
  766. currentStream.add(pdf.toString());
  767. this.currentXPosition += area.getContentWidth();
  768. }
  769. /** Checks to see if we have some text rendering commands open
  770. * still and writes out the TJ command to the stream if we do
  771. */
  772. private void closeText()
  773. {
  774. if (OPTIMIZE_TEXT && textOpen) {
  775. currentStream.add("] TJ\n");
  776. textOpen = false;
  777. prevWordX = 0;
  778. prevWordY = 0;
  779. }
  780. }
  781. /**
  782. * render inline space to PDF
  783. *
  784. * @param space space to render
  785. */
  786. public void renderInlineSpace(InlineSpace space) {
  787. this.currentXPosition += space.getSize();
  788. if (space.getUnderlined()) {
  789. if (prevUnderlineColor != null) {
  790. addLine(prevUnderlineXEndPos, prevUnderlineYEndPos,
  791. prevUnderlineXEndPos + space.getSize(),
  792. prevUnderlineYEndPos, prevUnderlineSize, prevUnderlineColor);
  793. }
  794. }
  795. if (space.getOverlined()) {
  796. if (prevOverlineColor != null) {
  797. addLine(prevOverlineXEndPos, prevOverlineYEndPos,
  798. prevOverlineXEndPos + space.getSize(),
  799. prevOverlineYEndPos, prevOverlineSize, prevOverlineColor);
  800. }
  801. }
  802. if (space.getLineThrough()) {
  803. if (prevLineThroughColor != null) {
  804. addLine(prevLineThroughXEndPos, prevLineThroughYEndPos,
  805. prevLineThroughXEndPos + space.getSize(),
  806. prevLineThroughYEndPos, prevLineThroughSize, prevLineThroughColor);
  807. }
  808. }
  809. }
  810. /**
  811. * render line area to PDF
  812. *
  813. * @param area area to render
  814. */
  815. public void renderLineArea(LineArea area) {
  816. int rx = this.currentAreaContainerXPosition + area.getStartIndent();
  817. int ry = this.currentYPosition;
  818. int w = area.getContentWidth();
  819. int h = area.getHeight();
  820. this.currentYPosition -= area.getPlacementOffset();
  821. this.currentXPosition = rx;
  822. int bl = this.currentYPosition;
  823. Enumeration e = area.getChildren().elements();
  824. while (e.hasMoreElements()) {
  825. Box b = (Box) e.nextElement();
  826. if(b instanceof InlineArea) {
  827. InlineArea ia = (InlineArea)b;
  828. this.currentYPosition = ry - ia.getYOffset();
  829. } else {
  830. this.currentYPosition = ry - area.getPlacementOffset();
  831. }
  832. b.render(this);
  833. }
  834. this.currentYPosition = ry - h;
  835. this.currentXPosition = rx;
  836. }
  837. private StringBuffer addKerning(String ch1, String ch2, Hashtable kerning) {
  838. Hashtable h2=(Hashtable)kerning.get(ch1);
  839. int pwdt=0;
  840. StringBuffer buf=new StringBuffer("");
  841. if (h2!=null) {
  842. Integer wdt=(Integer)h2.get(ch2);
  843. if (wdt!=null) {
  844. pwdt=-wdt.intValue();
  845. buf=buf.append(") " + pwdt + " (");
  846. }
  847. }
  848. return buf;
  849. }
  850. /**
  851. * render page into PDF
  852. *
  853. * @param page page to render
  854. */
  855. public void renderPage(Page page) {
  856. BodyAreaContainer body;
  857. AreaContainer before, after;
  858. currentStream = this.pdfDoc.makeStream();
  859. body = page.getBody();
  860. before = page.getBefore();
  861. after = page.getAfter();
  862. this.currentFontName = "";
  863. this.currentFontSize = 0;
  864. currentStream.add("BT\n");
  865. renderBodyAreaContainer(body);
  866. if (before != null) {
  867. renderAreaContainer(before);
  868. }
  869. if (after != null) {
  870. renderAreaContainer(after);
  871. }
  872. closeText();
  873. currentStream.add("ET\n");
  874. currentPage = this.pdfDoc.makePage(this.pdfResources, currentStream,
  875. page.getWidth() / 1000,
  876. page.getHeight() / 1000, page);
  877. if (page.hasLinks()) {
  878. currentAnnotList = this.pdfDoc.makeAnnotList();
  879. currentPage.setAnnotList(currentAnnotList);
  880. Enumeration e = page.getLinkSets().elements();
  881. while (e.hasMoreElements()) {
  882. LinkSet linkSet = (LinkSet) e.nextElement();
  883. linkSet.align();
  884. String dest = linkSet.getDest();
  885. int linkType = linkSet.getLinkType();
  886. Enumeration f = linkSet.getRects().elements();
  887. while (f.hasMoreElements()) {
  888. LinkedRectangle lrect =
  889. (LinkedRectangle) f.nextElement();
  890. currentAnnotList.addLink(
  891. this.pdfDoc.makeLink(lrect.getRectangle(),
  892. dest, linkType));
  893. }
  894. }
  895. } else {
  896. // just to be on the safe side
  897. currentAnnotList = null;
  898. }
  899. }
  900. /**
  901. * render leader area into PDF
  902. *
  903. * @param area area to render
  904. */
  905. public void renderLeaderArea(LeaderArea area) {
  906. int rx = this.currentXPosition;
  907. ;
  908. int ry = this.currentYPosition;
  909. int w = area.getContentWidth();
  910. int h = area.getHeight();
  911. int th = area.getRuleThickness();
  912. int st = area.getRuleStyle();
  913. String rs = setRuleStylePattern(st);
  914. //checks whether thickness is = 0, because of bug in pdf (or where?),
  915. //a line with thickness 0 is still displayed
  916. if (th != 0) {
  917. switch (st) {
  918. case org.apache.fop.fo.properties.RuleStyle.DOUBLE:
  919. addLine(rx, ry, rx + w, ry, th / 3, rs,
  920. new PDFColor(area.getRed(),
  921. area.getGreen(), area.getBlue()));
  922. addLine(rx, ry + (2 * th / 3), rx + w,
  923. ry + (2 * th / 3), th / 3, rs,
  924. new PDFColor(area.getRed(),
  925. area.getGreen(), area.getBlue()));
  926. break;
  927. case org.apache.fop.fo.properties.RuleStyle.GROOVE:
  928. addLine(rx, ry, rx + w, ry, th / 2, rs,
  929. new PDFColor(area.getRed(),
  930. area.getGreen(), area.getBlue()));
  931. addLine(rx, ry + (th / 2), rx + w, ry + (th / 2),
  932. th / 2, rs, new PDFColor(255, 255, 255));
  933. break;
  934. case org.apache.fop.fo.properties.RuleStyle.RIDGE:
  935. addLine(rx, ry, rx + w, ry, th / 2, rs,
  936. new PDFColor(255, 255, 255));
  937. addLine(rx, ry + (th / 2), rx + w, ry + (th / 2),
  938. th / 2, rs,
  939. new PDFColor(area.getRed(),
  940. area.getGreen(), area.getBlue()));
  941. break;
  942. default:
  943. addLine(rx, ry, rx + w, ry, th, rs,
  944. new PDFColor(area.getRed(),
  945. area.getGreen(), area.getBlue()));
  946. }
  947. this.currentXPosition += area.getContentWidth();
  948. this.currentYPosition += th;
  949. }
  950. }
  951. /**
  952. * set up the font info
  953. *
  954. * @param fontInfo font info to set up
  955. */
  956. public void setupFontInfo(FontInfo fontInfo) {
  957. FontSetup.setup(fontInfo);
  958. FontSetup.addToResources(this.pdfDoc, fontInfo);
  959. }
  960. /**
  961. * defines a string containing dashArray and dashPhase for the rule style
  962. */
  963. private String setRuleStylePattern (int style) {
  964. String rs = "";
  965. switch (style) {
  966. case org.apache.fop.fo.properties.RuleStyle.SOLID:
  967. rs = "[] 0 d ";
  968. break;
  969. case org.apache.fop.fo.properties.RuleStyle.DASHED:
  970. rs = "[3 3] 0 d ";
  971. break;
  972. case org.apache.fop.fo.properties.RuleStyle.DOTTED:
  973. rs = "[1 3] 0 d ";
  974. break;
  975. case org.apache.fop.fo.properties.RuleStyle.DOUBLE:
  976. rs = "[] 0 d ";
  977. break;
  978. default:
  979. rs = "[] 0 d ";
  980. }
  981. return rs;
  982. }
  983. protected void renderRootExtensions(AreaTree areaTree)
  984. {
  985. Vector v = areaTree.getExtensions();
  986. if (v != null) {
  987. Enumeration e = v.elements();
  988. while (e.hasMoreElements()) {
  989. ExtensionObj ext = (ExtensionObj)e.nextElement();
  990. if (ext instanceof Outline) {
  991. renderOutline((Outline)ext);
  992. }
  993. }
  994. }
  995. }
  996. private void renderOutline(Outline outline)
  997. {
  998. if (rootOutline == null) {
  999. rootOutline = this.pdfDoc.makeOutlineRoot();
  1000. }
  1001. PDFOutline pdfOutline = null;
  1002. Outline parent = outline.getParentOutline();
  1003. if (parent == null) {
  1004. pdfOutline = this.pdfDoc.makeOutline(rootOutline,
  1005. outline.getLabel().toString(),
  1006. outline.getInternalDestination());
  1007. }
  1008. else {
  1009. PDFOutline pdfParentOutline = (PDFOutline)parent.getRendererObject();
  1010. if (pdfParentOutline == null) {
  1011. MessageHandler.errorln("Error: pdfParentOutline is null");
  1012. }
  1013. else {
  1014. pdfOutline = this.pdfDoc.makeOutline(pdfParentOutline,
  1015. outline.getLabel().toString(),
  1016. outline.getInternalDestination());
  1017. }
  1018. }
  1019. outline.setRendererObject(pdfOutline);
  1020. // handle sub outlines
  1021. Vector v = outline.getOutlines();
  1022. Enumeration e = v.elements();
  1023. while (e.hasMoreElements()) {
  1024. renderOutline((Outline)e.nextElement());
  1025. }
  1026. }
  1027. }