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.

MIFRenderer.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // Author : Seshadri G
  41. package org.apache.fop.render.mif;
  42. // FOP
  43. import org.apache.fop.render.Renderer;
  44. import org.apache.fop.messaging.MessageHandler;
  45. import org.apache.fop.image.ImageArea;
  46. import org.apache.fop.image.FopImage;
  47. import org.apache.fop.apps.FOPException;
  48. import org.apache.fop.fo.properties.*;
  49. import org.apache.fop.fo.*;
  50. import org.apache.fop.layout.*;
  51. import org.apache.fop.layout.inline.*;
  52. import org.apache.fop.datatypes.*;
  53. import org.apache.fop.svg.*;
  54. import org.apache.fop.mif.*;
  55. import org.apache.fop.layout.*;
  56. import org.apache.fop.image.*;
  57. import org.w3c.dom.*;
  58. import org.w3c.dom.svg.*;
  59. import org.w3c.dom.css.*;
  60. import org.w3c.dom.svg.SVGLength;
  61. // Java
  62. import java.io.IOException;
  63. import java.io.OutputStream;
  64. import java.util.Enumeration;
  65. import java.awt.Rectangle;
  66. import java.util.Vector;
  67. import java.util.Hashtable;
  68. /**
  69. * Renderer that renders areas to MIF
  70. */
  71. public class MIFRenderer implements Renderer {
  72. private String currentFontName;
  73. private String currentFontSize;
  74. private int pageHeight;
  75. private int pageWidth;
  76. /** the current vertical position in millipoints from bottom */
  77. protected int currentYPosition = 0;
  78. /** the current horizontal position in millipoints from left */
  79. protected int currentXPosition = 0;
  80. /** the horizontal position of the current area container */
  81. private int currentAreaContainerXPosition = 0;
  82. /** the MIF Document being created */
  83. protected MIFDocument mifDoc;
  84. /* is a table currently open? */
  85. private boolean inTable=false;
  86. /** options */
  87. protected Hashtable options;
  88. /**
  89. * create the MIF renderer
  90. */
  91. public MIFRenderer() {
  92. this.mifDoc = new MIFDocument();
  93. }
  94. /** set up renderer options */
  95. public void setOptions(Hashtable options) {
  96. this.options = options;
  97. }
  98. /**
  99. * render the areas into MIF
  100. *
  101. * @param areaTree the laid-out area tree
  102. * @param writer the PrintWriter to write the MIF with
  103. */
  104. public void render(AreaTree areaTree,
  105. OutputStream stream) throws IOException, FOPException {
  106. MessageHandler.logln("rendering areas to MIF");
  107. // idReferences=areaTree.getIDReferences();
  108. //this.pdfResources = this.pdfDoc.getResources();
  109. //this.pdfDoc.setIDReferences(idReferences);
  110. Enumeration e = areaTree.getPages().elements();
  111. while (e.hasMoreElements()) {
  112. this.renderPage((Page) e.nextElement());
  113. }
  114. // MessageHandler.logln("writing out MIF");
  115. this.mifDoc.output(stream);
  116. stream.close();
  117. }
  118. /** set up the given FontInfo */
  119. public void setupFontInfo(FontInfo fontInfo) {
  120. FontSetup.setup(fontInfo);
  121. //FontSetup.addToFontFormat(this.mifDoc, fontInfo);
  122. }
  123. /** set the producer of the rendering */
  124. public void setProducer(String producer) {}
  125. public void renderAreaContainer(AreaContainer area) {
  126. if (area.foCreator != null && area.foCreator.getName() == "fo:table") {
  127. this.mifDoc.createTable();
  128. this.inTable=true;
  129. }
  130. else
  131. if (area.foCreator != null && area.foCreator.getName() == "fo:table-body") {
  132. this.mifDoc.setCurrent("fo:table-body");
  133. }
  134. else
  135. if (area.foCreator != null && area.foCreator.getName() == "fo:table-column") {
  136. int colWidth=((org.apache.fop.fo.flow.TableColumn) area.foCreator).getColumnWidth();
  137. this.mifDoc.setColumnProp(colWidth);
  138. }
  139. else
  140. if (area.foCreator != null && area.foCreator.getName() == "fo:table-row") {
  141. this.mifDoc.startRow();
  142. }
  143. else
  144. if (area.foCreator != null && area.foCreator.getName() == "fo:table-cell") {
  145. int rowSpan=((org.apache.fop.fo.flow.TableCell) area.foCreator).getNumRowsSpanned();
  146. int colSpan=((org.apache.fop.fo.flow.TableCell) area.foCreator).getNumColumnsSpanned();
  147. this.mifDoc.startCell(rowSpan,colSpan);
  148. }
  149. else
  150. if (inTable) {
  151. inTable=false;
  152. this.mifDoc.endTable();
  153. }
  154. int saveY = this.currentYPosition;
  155. int saveX = this.currentAreaContainerXPosition;
  156. if (area.getPosition() == Position.ABSOLUTE) {
  157. // Y position is computed assuming positive Y axis, adjust for negative postscript one
  158. this.currentYPosition =area.getYPosition() - 2 * area.getPaddingTop() - 2 * area.getBorderTopWidth();
  159. this.currentAreaContainerXPosition = area.getXPosition();
  160. } else if (area.getPosition() == Position.RELATIVE) {
  161. this.currentYPosition -= area.getYPosition();
  162. this.currentAreaContainerXPosition += area.getXPosition();
  163. } else if (area.getPosition() == Position.STATIC) {
  164. this.currentYPosition -=
  165. area.getPaddingTop() + area.getBorderTopWidth();
  166. this.currentAreaContainerXPosition +=area.getPaddingLeft() + area.getBorderLeftWidth();
  167. }
  168. this.currentXPosition = this.currentAreaContainerXPosition;
  169. doFrame(area);
  170. Enumeration e = area.getChildren().elements();
  171. while (e.hasMoreElements()) {
  172. Box b = (Box) e.nextElement();
  173. b.render(this);
  174. }
  175. if (area.getPosition() != Position.STATIC) {
  176. this.currentYPosition = saveY;
  177. this.currentAreaContainerXPosition = saveX;
  178. } else
  179. this.currentYPosition -= area.getHeight();
  180. }
  181. public void renderBodyAreaContainer(BodyAreaContainer area) {
  182. int saveY = this.currentYPosition;
  183. int saveX = this.currentAreaContainerXPosition;
  184. if (area.getPosition() == Position.ABSOLUTE) {
  185. // Y position is computed assuming positive Y axis, adjust for negative postscript one
  186. this.currentYPosition = area.getYPosition();
  187. this.currentAreaContainerXPosition = area.getXPosition();
  188. } else if (area.getPosition() == Position.RELATIVE) {
  189. this.currentYPosition -= area.getYPosition();
  190. this.currentAreaContainerXPosition += area.getXPosition();
  191. }
  192. this.currentXPosition = this.currentAreaContainerXPosition;
  193. int w, h;
  194. int rx = this.currentAreaContainerXPosition;
  195. w = area.getContentWidth();
  196. h = area.getContentHeight();
  197. int ry = this.currentYPosition;
  198. ColorType bg = area.getBackgroundColor();
  199. /*
  200. // I'm not sure I should have to check for bg being null
  201. // but I do
  202. if ((bg != null) && (bg.alpha() == 0)) {
  203. this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
  204. }
  205. */
  206. /*
  207. // floats & footnotes stuff
  208. renderAreaContainer(area.getBeforeFloatReferenceArea());
  209. renderAreaContainer(area.getFootnoteReferenceArea());
  210. */
  211. // main reference area
  212. Enumeration e = area.getMainReferenceArea().getChildren().elements();
  213. while (e.hasMoreElements()) {
  214. Box b = (Box) e.nextElement();
  215. b.render(this); // span areas
  216. }
  217. if (area.getPosition() != Position.STATIC) {
  218. this.currentYPosition = saveY;
  219. this.currentAreaContainerXPosition = saveX;
  220. } else
  221. this.currentYPosition -= area.getHeight();
  222. }
  223. private void doFrame(Area area) {
  224. int w, h;
  225. int rx = this.currentAreaContainerXPosition;
  226. w = area.getContentWidth();
  227. if (area instanceof BlockArea)
  228. rx += ((BlockArea)area).getStartIndent();
  229. h = area.getContentHeight();
  230. int ry = this.currentYPosition;
  231. ColorType bg = area.getBackgroundColor();
  232. rx = rx - area.getPaddingLeft();
  233. ry = ry + area.getPaddingTop();
  234. w = w + area.getPaddingLeft() + area.getPaddingRight();
  235. h = h + area.getPaddingTop() + area.getPaddingBottom();
  236. /*
  237. // I'm not sure I should have to check for bg being null
  238. // but I do
  239. if ((bg != null) && (bg.alpha() == 0)) {
  240. this.addRect(rx, ry, w, -h,
  241. new PDFColor(bg),
  242. new PDFColor(bg));
  243. }
  244. */
  245. rx = rx - area.getBorderLeftWidth();
  246. ry = ry + area.getBorderTopWidth();
  247. w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
  248. h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
  249. //Create a textrect with these dimensions.
  250. //The y co-ordinate is measured +ve downwards so subtract page-height
  251. this.mifDoc.setTextRectProp(rx,pageHeight-ry,w,h);
  252. /*
  253. BorderAndPadding bp = area.getBorderAndPadding();
  254. if (area.getBorderTopWidth() != 0)
  255. addLine(rx, ry, rx + w, ry, area.getBorderTopWidth(),
  256. new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
  257. if (area.getBorderLeftWidth() != 0)
  258. addLine(rx, ry, rx, ry - h, area.getBorderLeftWidth(),
  259. new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
  260. if (area.getBorderRightWidth() != 0)
  261. addLine(rx + w, ry, rx + w, ry - h, area.getBorderRightWidth(),
  262. new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
  263. if (area.getBorderBottomWidth() != 0)
  264. addLine(rx, ry - h, rx + w, ry - h, area.getBorderBottomWidth(),
  265. new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));
  266. */
  267. }
  268. public void renderSpanArea(SpanArea area) {
  269. //A span maps to a textframe
  270. this.mifDoc.createTextRect(area.getColumnCount());
  271. Enumeration e = area.getChildren().elements();
  272. while (e.hasMoreElements()) {
  273. Box b = (Box) e.nextElement();
  274. b.render(this); // column areas
  275. }
  276. }
  277. /** render the given block area */
  278. public void renderBlockArea(BlockArea area) {
  279. this.mifDoc.setBlockProp(area.getStartIndent(),area.getEndIndent());
  280. Enumeration e = area.getChildren().elements();
  281. while (e.hasMoreElements()) {
  282. Box b = (Box) e.nextElement();
  283. b.render(this);
  284. }
  285. }
  286. /** render the given display space */
  287. public void renderDisplaySpace(DisplaySpace space) {
  288. int d = space.getSize();
  289. this.currentYPosition -= d;
  290. }
  291. /** render the given SVG area */
  292. public void renderSVGArea(SVGArea area) {}
  293. /** render a foreign object area */
  294. public void renderForeignObjectArea(ForeignObjectArea area) {
  295. }
  296. public void renderWordArea(WordArea area) {
  297. String s;
  298. s = area.getText();
  299. this.mifDoc.addToStream(s);
  300. this.currentXPosition += area.getContentWidth();
  301. }
  302. /** render the given image area */
  303. public void renderImageArea(ImageArea area) {
  304. int x = this.currentAreaContainerXPosition + area.getXOffset();
  305. int y = this.currentYPosition;
  306. int w = area.getContentWidth();
  307. int h = area.getHeight();
  308. this.currentYPosition -= h;
  309. FopImage img = area.getImage();
  310. if (img instanceof SVGImage) {
  311. /* try {
  312. SVGSVGElement svg =
  313. ((SVGImage) img).getSVGDocument().getRootElement();
  314. currentStream.add("ET\nq\n" + (((float) w) / 1000f) +
  315. " 0 0 " + (((float) h) / 1000f) + " " +
  316. (((float) x) / 1000f) + " " +
  317. (((float)(y - h)) / 1000f) + " cm\n");
  318. // renderSVG(svg, (int) x, (int) y);
  319. currentStream.add("Q\nBT\n");
  320. } catch (FopImageException e) {
  321. } */
  322. MessageHandler.logln("Warning: SVG images not supported in this version");
  323. } else {
  324. String url = img.getURL();
  325. this.mifDoc.addImage(url,x,pageHeight-y,w,h);
  326. }
  327. }
  328. /** render the given inline area */
  329. public void renderInlineArea(InlineArea area) {}
  330. /** render the given inline space */
  331. public void renderInlineSpace(InlineSpace space) {
  332. // I dont need the size of space! I just need to
  333. // leave a blank space each time
  334. String s=" ";
  335. this.mifDoc.addToStream(s); // cool!
  336. this.currentXPosition += space.getSize();
  337. }
  338. /** render the given line area */
  339. public void renderLineArea(LineArea area) {
  340. int rx = this.currentAreaContainerXPosition
  341. + area.getStartIndent();
  342. int ry = this.currentYPosition;
  343. int w = area.getContentWidth();
  344. int h = area.getHeight();
  345. this.currentYPosition -= area.getPlacementOffset();
  346. this.currentXPosition = rx;
  347. int bl = this.currentYPosition;
  348. //The start of a new linearea corresponds to a new para in FM
  349. this.mifDoc.startLine();
  350. Enumeration e = area.getChildren().elements();
  351. while (e.hasMoreElements()) {
  352. Box b = (Box) e.nextElement();
  353. this.currentYPosition = ry - area.getPlacementOffset();
  354. b.render(this);
  355. }
  356. this.currentYPosition = ry-h;
  357. this.currentXPosition = rx;
  358. }
  359. /** render the given page */
  360. public void renderPage(Page page) {
  361. AreaContainer before, after;
  362. BodyAreaContainer body;
  363. body = page.getBody();
  364. before = page.getBefore();
  365. after = page.getAfter();
  366. this.currentFontName = "";
  367. this.currentFontSize = "0";
  368. pageHeight=page.getHeight();
  369. pageWidth=page.getWidth();
  370. this.mifDoc.setDocumentHeightWidth(pageHeight,pageWidth);
  371. this.mifDoc.createPage();
  372. renderBodyAreaContainer(body);
  373. // If the area is an instance of anything other than body, it goes into the
  374. // corresponding master page.
  375. if (before != null) {
  376. this.mifDoc.createTextRect(1); // Create a rect with one col
  377. renderAreaContainer(before);
  378. }
  379. if (after != null) {
  380. this.mifDoc.createTextRect(1); // Create a rect with one col
  381. renderAreaContainer(after);
  382. }
  383. }
  384. /** render the given leader area */
  385. public void renderLeaderArea(LeaderArea area) {}
  386. }