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.

AWTRenderer.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.awt;
  18. /*
  19. * originally contributed by
  20. * Juergen Verwohlt: Juergen.Verwohlt@jCatalog.com,
  21. * Rainer Steinkuhle: Rainer.Steinkuhle@jCatalog.com,
  22. * Stanislav Gorkhover: Stanislav.Gorkhover@jCatalog.com
  23. */
  24. // Java
  25. import java.awt.Dimension;
  26. import java.awt.geom.Rectangle2D;
  27. import java.awt.Toolkit;
  28. import java.awt.event.WindowAdapter;
  29. import java.awt.event.WindowEvent;
  30. import java.awt.print.PageFormat;
  31. import java.awt.print.Pageable;
  32. import java.awt.print.Paper;
  33. import java.awt.print.Printable;
  34. import java.io.IOException;
  35. import org.apache.fop.apps.FOPException;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.MimeConstants;
  38. import org.apache.fop.area.Area;
  39. import org.apache.fop.area.PageViewport;
  40. import org.apache.fop.datatypes.ColorType;
  41. import org.apache.fop.fo.properties.ColorTypeProperty;
  42. import org.apache.fop.render.awt.viewer.PreviewDialog;
  43. import org.apache.fop.render.awt.viewer.Renderable;
  44. import org.apache.fop.render.awt.viewer.Translator;
  45. import org.apache.fop.render.java2d.Java2DRenderer;
  46. /**
  47. * The AWTRender outputs the pages generated by the layout engine to a Swing
  48. * window. This Swing window serves as default viewer for the -awt switch and as
  49. * an example of how to embed the AWTRenderer into an AWT/Swing application.
  50. */
  51. public class AWTRenderer extends Java2DRenderer implements Pageable {
  52. /** The MIME type for AWT-Rendering */
  53. public static final String MIME_TYPE = MimeConstants.MIME_FOP_AWT_PREVIEW;
  54. /** The resource bundle used for AWT messages. */
  55. protected Translator translator = null;
  56. /** flag for debugging */
  57. public boolean debug;
  58. /** If true, preview dialog is shown. */
  59. public boolean dialogDisplay = true;
  60. /**
  61. * The preview dialog frame used for display of the documents. Also used as
  62. * the AWT Component for FontSetup in generating valid font measures.
  63. */
  64. protected PreviewDialog frame;
  65. /**
  66. * Renderable instance that can be used to reload and re-render a document after
  67. * modifications.
  68. */
  69. protected Renderable renderable;
  70. /**
  71. * Creates a new AWTRenderer instance.
  72. */
  73. public AWTRenderer() {
  74. translator = new Translator();
  75. }
  76. /** @see org.apache.fop.render.Renderer#setUserAgent(org.apache.fop.apps.FOUserAgent) */
  77. public void setUserAgent(FOUserAgent foUserAgent) {
  78. super.setUserAgent(foUserAgent);
  79. if (dialogDisplay) {
  80. createPreviewDialog();
  81. }
  82. }
  83. /**
  84. * A Renderable instance can be set so the Preview Dialog can enable the "Reload" button
  85. * which causes the current document to be reprocessed and redisplayed.
  86. * @param renderable the Renderable instance.
  87. */
  88. public void setRenderable(Renderable renderable) {
  89. this.renderable = renderable;
  90. }
  91. /**
  92. * Sets whether the preview dialog should be created and displayed when
  93. * the rendering is finished.
  94. * @param show If false, preview dialog is not shown. True by default
  95. */
  96. public void setPreviewDialogDisplayed(boolean show) {
  97. dialogDisplay = show;
  98. }
  99. /** @see org.apache.fop.render.Renderer#renderPage(org.apache.fop.area.PageViewport) */
  100. public void renderPage(PageViewport pageViewport)
  101. throws IOException, FOPException {
  102. super.renderPage(pageViewport);
  103. if (frame != null) {
  104. frame.setInfo();
  105. }
  106. }
  107. /** @see org.apache.fop.render.Renderer#stopRenderer() */
  108. public void stopRenderer() throws IOException {
  109. super.stopRenderer();
  110. if (frame != null) {
  111. frame.setStatus(translator.getString("Status.Show"));
  112. frame.reload(); // Refreshes view of page
  113. }
  114. }
  115. /**
  116. * @return the dimensions of the specified page
  117. * @param pageNum the page number
  118. * @exception FOPException If the page is out of range or has not been rendered.
  119. */
  120. public Dimension getPageImageSize(int pageNum) throws FOPException {
  121. Rectangle2D bounds = getPageViewport(pageNum).getViewArea();
  122. pageWidth = (int) Math.round(bounds.getWidth() / 1000f);
  123. pageHeight = (int) Math.round(bounds.getHeight() / 1000f);
  124. double scaleX = scaleFactor
  125. * (25.4 / FOUserAgent.DEFAULT_TARGET_RESOLUTION)
  126. / userAgent.getTargetPixelUnitToMillimeter();
  127. double scaleY = scaleFactor
  128. * (25.4 / FOUserAgent.DEFAULT_TARGET_RESOLUTION)
  129. / userAgent.getTargetPixelUnitToMillimeter();
  130. int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
  131. int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
  132. return new Dimension(bitmapWidth, bitmapHeight);
  133. }
  134. /** Creates and initialize the AWT Viewer main window */
  135. private PreviewDialog createPreviewDialog() {
  136. frame = new PreviewDialog(userAgent, this.renderable);
  137. frame.addWindowListener(new WindowAdapter() {
  138. public void windowClosed(WindowEvent we) {
  139. System.exit(0);
  140. }
  141. });
  142. // Centers the window
  143. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  144. Dimension frameSize = frame.getSize();
  145. if (frameSize.height > screenSize.height) {
  146. frameSize.height = screenSize.height;
  147. }
  148. if (frameSize.width > screenSize.width) {
  149. frameSize.width = screenSize.width;
  150. }
  151. frame.setLocation((screenSize.width - frameSize.width) / 2,
  152. (screenSize.height - frameSize.height) / 2);
  153. frame.setStatus(translator.getString("Status.Build.FO.tree"));
  154. frame.setVisible(true);
  155. return frame;
  156. }
  157. /** @see java.awt.print.Pageable#getPageFormat(int) */
  158. public PageFormat getPageFormat(int pageIndex)
  159. throws IndexOutOfBoundsException {
  160. try {
  161. if (pageIndex >= getNumberOfPages()) {
  162. return null;
  163. }
  164. PageFormat pageFormat = new PageFormat();
  165. Paper paper = new Paper();
  166. pageFormat.setPaper(paper);
  167. Rectangle2D dim = getPageViewport(pageIndex).getViewArea();
  168. double width = dim.getWidth();
  169. double height = dim.getHeight();
  170. // if the width is greater than the height assume lanscape mode
  171. // and swap the width and height values in the paper format
  172. if (width > height) {
  173. paper.setImageableArea(0, 0, height / 1000d, width / 1000d);
  174. paper.setSize(height / 1000d, width / 1000d);
  175. pageFormat.setOrientation(PageFormat.LANDSCAPE);
  176. } else {
  177. paper.setImageableArea(0, 0, width / 1000d, height / 1000d);
  178. paper.setSize(width / 1000d, height / 1000d);
  179. pageFormat.setOrientation(PageFormat.PORTRAIT);
  180. }
  181. return pageFormat;
  182. } catch (FOPException fopEx) {
  183. throw new IndexOutOfBoundsException(fopEx.getMessage());
  184. }
  185. }
  186. /** @see java.awt.print.Pageable#getPrintable(int) */
  187. public Printable getPrintable(int pageIndex)
  188. throws IndexOutOfBoundsException {
  189. return this;
  190. }
  191. /** @see org.apache.fop.render.Renderer */
  192. public boolean supportsOutOfOrder() {
  193. return true; // TODO true?
  194. }
  195. /** @return the Translator for this renderer */
  196. public Translator getTranslator() {
  197. return translator;
  198. }
  199. /** @see org.apache.fop.render.AbstractRenderer */
  200. public String getMimeType() {
  201. return MIME_TYPE;
  202. }
  203. /**
  204. * Draws the background and borders and adds a basic debug view // TODO
  205. * implement visual-debugging as standalone
  206. *
  207. * @see org.apache.fop.render.java2d.Java2DRenderer#drawBackAndBorders(org.apache.fop.area.Area,
  208. * float, float, float, float)
  209. *
  210. * @param area the area to get the traits from
  211. * @param startx the start x position
  212. * @param starty the start y position
  213. * @param width the width of the area
  214. * @param height the height of the area
  215. */
  216. protected void drawBackAndBorders(Area area, float startx, float starty,
  217. float width, float height) {
  218. if (debug) {
  219. debugBackAndBorders(area, startx, starty, width, height);
  220. }
  221. super.drawBackAndBorders(area, startx, starty, width, height);
  222. }
  223. /** Draws a thin border around every area to help debugging */
  224. private void debugBackAndBorders(Area area, float startx, float starty,
  225. float width, float height) {
  226. // saves the graphics state in a stack
  227. state.push();
  228. ColorType ct = new ColorTypeProperty(0.7f, 0.7f, 0.7f);
  229. state.updateColor(ct, true, null);
  230. state.updateStroke(0.4f, EN_SOLID);
  231. state.getGraph().draw(
  232. new Rectangle2D.Float(startx, starty, width, height));
  233. // restores the last graphics state from the stack
  234. state.pop();
  235. }
  236. }