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

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