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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. package org.apache.fop.render.mif;
  8. // FOP
  9. import org.apache.fop.render.Renderer;
  10. import org.apache.fop.render.AbstractRenderer;
  11. import org.apache.fop.apps.FOPException;
  12. import org.apache.fop.fo.properties.*;
  13. import org.apache.fop.fo.*;
  14. import org.apache.fop.layout.*;
  15. import org.apache.fop.layout.inline.*;
  16. import org.apache.fop.datatypes.*;
  17. import org.apache.fop.svg.*;
  18. import org.apache.fop.mif.*;
  19. import org.apache.fop.layout.*;
  20. import org.apache.fop.image.*;
  21. import org.w3c.dom.*;
  22. import org.w3c.dom.svg.*;
  23. import org.w3c.dom.css.*;
  24. import org.w3c.dom.svg.SVGLength;
  25. // Java
  26. import java.io.IOException;
  27. import java.io.OutputStream;
  28. import java.awt.Rectangle;
  29. /**
  30. * Renders areas to MIF. Collects all the Pages and print them out at the end.
  31. * This means that the MIF renderer does not stream.
  32. *
  33. * @author Seshadri G
  34. * @author <a href="mailto:mark-fop@inomial.com">Mark Lillywhite</a>
  35. */
  36. public class MIFRenderer extends AbstractRenderer {
  37. private String currentFontName;
  38. private String currentFontSize;
  39. private int pageHeight;
  40. private int pageWidth;
  41. /**
  42. * the current vertical position in millipoints from bottom
  43. */
  44. protected int currentYPosition = 0;
  45. /**
  46. * the current horizontal position in millipoints from left
  47. */
  48. protected int currentXPosition = 0;
  49. /**
  50. * the horizontal position of the current area container
  51. */
  52. private int currentAreaContainerXPosition = 0;
  53. /**
  54. * the MIF Document being created
  55. */
  56. protected MIFDocument mifDoc;
  57. /* is a table currently open? */
  58. private boolean inTable = false;
  59. /**
  60. * options
  61. */
  62. protected java.util.HashMap options;
  63. /**
  64. * create the MIF renderer
  65. */
  66. public MIFRenderer() {
  67. this.mifDoc = new MIFDocument();
  68. }
  69. /**
  70. * set up renderer options
  71. */
  72. public void setOptions(java.util.HashMap options) {
  73. this.options = options;
  74. }
  75. /**
  76. * set up the given FontInfo
  77. */
  78. public void setupFontInfo(FontInfo fontInfo) {
  79. FontSetup.setup(fontInfo);
  80. // FontSetup.addToFontFormat(this.mifDoc, fontInfo);
  81. }
  82. /**
  83. * set the producer of the rendering
  84. */
  85. public void setProducer(String producer) {}
  86. public void renderAreaContainer(AreaContainer area) {
  87. if (area.foCreator != null
  88. && area.foCreator.getName() == "fo:table") {
  89. this.mifDoc.createTable();
  90. this.inTable = true;
  91. } else if (area.foCreator != null
  92. && area.foCreator.getName() == "fo:table-body") {
  93. this.mifDoc.setCurrent("fo:table-body");
  94. } else if (area.foCreator != null
  95. && area.foCreator.getName() == "fo:table-column") {
  96. int colWidth =
  97. ((org.apache.fop.fo.flow.TableColumn)area.foCreator).getColumnWidth();
  98. this.mifDoc.setColumnProp(colWidth);
  99. } else if (area.foCreator != null
  100. && area.foCreator.getName() == "fo:table-row") {
  101. this.mifDoc.startRow();
  102. } else if (area.foCreator != null
  103. && area.foCreator.getName() == "fo:table-cell") {
  104. int rowSpan =
  105. ((org.apache.fop.fo.flow.TableCell)area.foCreator).getNumRowsSpanned();
  106. int colSpan =
  107. ((org.apache.fop.fo.flow.TableCell)area.foCreator).getNumColumnsSpanned();
  108. this.mifDoc.startCell(rowSpan, colSpan);
  109. } else if (inTable) {
  110. inTable = false;
  111. this.mifDoc.endTable();
  112. }
  113. super.renderAreaContainer(area);
  114. }
  115. protected void addFilledRect(int x, int y, int w, int h,
  116. ColorType col) {
  117. }
  118. protected void doFrame(Area area) {
  119. int w, h;
  120. int rx = this.currentAreaContainerXPosition;
  121. w = area.getContentWidth();
  122. if (area instanceof BlockArea)
  123. rx += ((BlockArea)area).getStartIndent();
  124. h = area.getContentHeight();
  125. int ry = this.currentYPosition;
  126. rx = rx - area.getPaddingLeft();
  127. ry = ry + area.getPaddingTop();
  128. w = w + area.getPaddingLeft() + area.getPaddingRight();
  129. h = h + area.getPaddingTop() + area.getPaddingBottom();
  130. doBackground(area, rx, ry, w, h);
  131. rx = rx - area.getBorderLeftWidth();
  132. ry = ry + area.getBorderTopWidth();
  133. w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
  134. h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
  135. // Create a textrect with these dimensions.
  136. // The y co-ordinate is measured +ve downwards so subtract page-height
  137. this.mifDoc.setTextRectProp(rx, pageHeight - ry, w, h);
  138. /*
  139. * BorderAndPadding bp = area.getBorderAndPadding();
  140. * if (area.getBorderTopWidth() != 0)
  141. * addLine(rx, ry, rx + w, ry, area.getBorderTopWidth(),
  142. * new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
  143. * if (area.getBorderLeftWidth() != 0)
  144. * addLine(rx, ry, rx, ry - h, area.getBorderLeftWidth(),
  145. * new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
  146. * if (area.getBorderRightWidth() != 0)
  147. * addLine(rx + w, ry, rx + w, ry - h, area.getBorderRightWidth(),
  148. * new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
  149. * if (area.getBorderBottomWidth() != 0)
  150. * addLine(rx, ry - h, rx + w, ry - h, area.getBorderBottomWidth(),
  151. * new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));
  152. */
  153. }
  154. public void renderSpanArea(SpanArea area) {
  155. // A span maps to a textframe
  156. this.mifDoc.createTextRect(area.getColumnCount());
  157. super.renderSpanArea(area);
  158. }
  159. /**
  160. * render the given block area
  161. */
  162. public void renderBlockArea(BlockArea area) {
  163. this.mifDoc.setBlockProp(area.getStartIndent(), area.getEndIndent());
  164. super.renderBlockArea(area);
  165. }
  166. /**
  167. * render the given display space
  168. */
  169. public void renderDisplaySpace(DisplaySpace space) {
  170. int d = space.getSize();
  171. this.currentYPosition -= d;
  172. }
  173. /**
  174. * render the given SVG area
  175. */
  176. public void renderSVGArea(SVGArea area) {}
  177. /**
  178. * render a foreign object area
  179. */
  180. public void renderForeignObjectArea(ForeignObjectArea area) {}
  181. public void renderWordArea(WordArea area) {
  182. String s;
  183. s = area.getText();
  184. this.mifDoc.addToStream(s);
  185. this.currentXPosition += area.getContentWidth();
  186. }
  187. /**
  188. * Renders an image, scaling it to the given width and height.
  189. * If the scaled width and height is the same intrinsic size
  190. * of the image, the image is not scaled.
  191. *
  192. * @param x the x position of left edge in millipoints
  193. * @param y the y position of top edge in millipoints
  194. * @param w the width in millipoints
  195. * @param h the height in millipoints
  196. * @param image the image to be rendered
  197. * @param fs the font state to use when rendering text
  198. * in non-bitmapped images.
  199. */
  200. protected void drawImageScaled(int x, int y, int w, int h,
  201. FopImage image,
  202. FontState fs) {
  203. // XXX: implement this
  204. }
  205. /**
  206. * Renders an image, clipping it as specified.
  207. *
  208. * @param x the x position of left edge in millipoints.
  209. * @param y the y position of top edge in millipoints.
  210. * @param clipX the left edge of the clip in millipoints
  211. * @param clipY the top edge of the clip in millipoints
  212. * @param clipW the clip width in millipoints
  213. * @param clipH the clip height in millipoints
  214. * @param fill the image to be rendered
  215. * @param fs the font state to use when rendering text
  216. * in non-bitmapped images.
  217. */
  218. protected void drawImageClipped(int x, int y,
  219. int clipX, int clipY,
  220. int clipW, int clipH,
  221. FopImage image,
  222. FontState fs) {
  223. // XXX: implement this
  224. }
  225. /**
  226. * render the given image area
  227. */
  228. public void renderImageArea(ImageArea area) {
  229. int x = this.currentAreaContainerXPosition + area.getXOffset();
  230. int y = this.currentYPosition;
  231. int w = area.getContentWidth();
  232. int h = area.getHeight();
  233. this.currentYPosition -= h;
  234. FopImage img = area.getImage();
  235. if (img instanceof SVGImage) {
  236. /*
  237. * try {
  238. * SVGSVGElement svg =
  239. * ((SVGImage) img).getSVGDocument().getRootElement();
  240. * currentStream.add("ET\nq\n" + (((float) w) / 1000f) +
  241. * " 0 0 " + (((float) h) / 1000f) + " " +
  242. * (((float) x) / 1000f) + " " +
  243. * (((float)(y - h)) / 1000f) + " cm\n");
  244. * // renderSVG(svg, (int) x, (int) y);
  245. * currentStream.add("Q\nBT\n");
  246. * } catch (FopImageException e) {
  247. * }
  248. */
  249. log.warn("SVG images not supported in this version");
  250. } else {
  251. String url = img.getURL();
  252. this.mifDoc.addImage(url, x, pageHeight - y, w, h);
  253. }
  254. }
  255. /**
  256. * render the given inline area
  257. */
  258. public void renderInlineArea(InlineArea area) {}
  259. /**
  260. * render the given inline space
  261. */
  262. public void renderInlineSpace(InlineSpace space) {
  263. // I dont need the size of space! I just need to
  264. // leave a blank space each time
  265. String s = " ";
  266. this.mifDoc.addToStream(s); // cool!
  267. this.currentXPosition += space.getSize();
  268. }
  269. /**
  270. * render the given line area
  271. */
  272. public void renderLineArea(LineArea area) {
  273. // The start of a new linearea corresponds to a new para in FM
  274. this.mifDoc.startLine();
  275. super.renderLineArea(area);
  276. }
  277. /**
  278. * render the given page
  279. */
  280. public void renderPage(Page page) {
  281. AreaContainer before, after;
  282. BodyAreaContainer body;
  283. body = page.getBody();
  284. before = page.getBefore();
  285. after = page.getAfter();
  286. this.currentFontName = "";
  287. this.currentFontSize = "0";
  288. pageHeight = page.getHeight();
  289. pageWidth = page.getWidth();
  290. this.mifDoc.setDocumentHeightWidth(pageHeight, pageWidth);
  291. this.mifDoc.createPage();
  292. body.render(this);
  293. // If the area is an instance of anything other than body, it goes into the
  294. // corresponding master page.
  295. if (before != null) {
  296. this.mifDoc.createTextRect(1); // Create a rect with one col
  297. before.render(this);
  298. }
  299. if (after != null) {
  300. this.mifDoc.createTextRect(1); // Create a rect with one col
  301. after.render(this);
  302. }
  303. }
  304. /**
  305. * render the given leader area
  306. */
  307. public void renderLeaderArea(LeaderArea area) {}
  308. /**
  309. Default start renderer method. This would
  310. normally be overridden. (mark-fop@inomial.com).
  311. */
  312. public void startRenderer(OutputStream outputStream)
  313. throws IOException {
  314. log.info("rendering areas to MIF");
  315. }
  316. /**
  317. Default stop renderer method. This would
  318. normally be overridden. (mark-fop@inomial.com)
  319. */
  320. public void stopRenderer(OutputStream outputStream)
  321. throws IOException {
  322. log.info("writing out MIF");
  323. this.mifDoc.output(outputStream);
  324. outputStream.flush();
  325. }
  326. public void render(Page page, OutputStream outputStream) {
  327. this.renderPage(page);
  328. }
  329. }