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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.awt;
  19. /*
  20. * originally contributed by
  21. * Juergen Verwohlt: Juergen.Verwohlt@jCatalog.com,
  22. * Rainer Steinkuhle: Rainer.Steinkuhle@jCatalog.com,
  23. * Stanislav Gorkhover: Stanislav.Gorkhover@jCatalog.com
  24. */
  25. // Java
  26. import java.awt.Color;
  27. import java.awt.Dimension;
  28. import java.awt.geom.Rectangle2D;
  29. import java.awt.geom.Point2D;
  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.FopFactoryConfigurator;
  38. import org.apache.fop.apps.MimeConstants;
  39. import org.apache.fop.area.Area;
  40. import org.apache.fop.area.PageViewport;
  41. import org.apache.fop.render.awt.viewer.PreviewDialog;
  42. import org.apache.fop.render.awt.viewer.Renderable;
  43. import org.apache.fop.render.awt.viewer.StatusListener;
  44. import org.apache.fop.render.java2d.Java2DRenderer;
  45. import org.apache.fop.render.extensions.prepress.PageScale;
  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. /** flag for debugging */
  55. public boolean debug;
  56. /** If true, preview dialog is shown. */
  57. public boolean dialogDisplay = true;
  58. /** true if the preview dialog should be the main window of the application */
  59. private boolean previewAsMainWindow;
  60. /**
  61. * Renderable instance that can be used to reload and re-render a document after
  62. * modifications.
  63. */
  64. protected Renderable renderable;
  65. /**
  66. * Will be notified when rendering progresses
  67. */
  68. protected StatusListener statusListener = null;
  69. /**
  70. * Creates a new AWTRenderer instance.
  71. */
  72. public AWTRenderer() {
  73. this(false);
  74. }
  75. /**
  76. * Creates a new AWTRenderer instance.
  77. * @param previewAsMainWindow true if the preview dialog created by the renderer should be
  78. * the main window of the application.
  79. */
  80. public AWTRenderer(boolean previewAsMainWindow) {
  81. this.previewAsMainWindow = previewAsMainWindow;
  82. }
  83. /** {@inheritDoc} */
  84. public void setUserAgent(FOUserAgent foUserAgent) {
  85. super.setUserAgent(foUserAgent);
  86. if (dialogDisplay) {
  87. setStatusListener(PreviewDialog.createPreviewDialog(userAgent, this.renderable,
  88. this.previewAsMainWindow));
  89. }
  90. }
  91. /**
  92. * A Renderable instance can be set so the Preview Dialog can enable the "Reload" button
  93. * which causes the current document to be reprocessed and redisplayed.
  94. * @param renderable the Renderable instance.
  95. */
  96. public void setRenderable(Renderable renderable) {
  97. this.renderable = renderable;
  98. }
  99. /**
  100. * Sets whether the preview dialog should be created and displayed when
  101. * the rendering is finished.
  102. * @param show If false, preview dialog is not shown. True by default
  103. */
  104. public void setPreviewDialogDisplayed(boolean show) {
  105. dialogDisplay = show;
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public void renderPage(PageViewport pageViewport) throws IOException {
  111. super.renderPage(pageViewport);
  112. if (statusListener != null) {
  113. statusListener.notifyPageRendered();
  114. }
  115. }
  116. /** {@inheritDoc} */
  117. public void stopRenderer() throws IOException {
  118. super.stopRenderer();
  119. if (statusListener != null) {
  120. statusListener.notifyRendererStopped(); // Refreshes view of page
  121. }
  122. }
  123. /**
  124. * @return the dimensions of the specified page
  125. * @param pageNum the page number
  126. * @exception FOPException If the page is out of range or has not been rendered.
  127. */
  128. public Dimension getPageImageSize(int pageNum) throws FOPException {
  129. Rectangle2D bounds = getPageViewport(pageNum).getViewArea();
  130. pageWidth = (int) Math.round(bounds.getWidth() / 1000f);
  131. pageHeight = (int) Math.round(bounds.getHeight() / 1000f);
  132. double scaleX = scaleFactor
  133. * (25.4 / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
  134. / userAgent.getTargetPixelUnitToMillimeter();
  135. double scaleY = scaleFactor
  136. * (25.4 / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
  137. / userAgent.getTargetPixelUnitToMillimeter();
  138. if (getPageViewport(pageNum).getForeignAttributes() != null) {
  139. String scale = (String) getPageViewport(pageNum).getForeignAttributes().get(
  140. PageScale.EXT_PAGE_SCALE);
  141. Point2D scales = PageScale.getScale(scale);
  142. if (scales != null) {
  143. scaleX *= scales.getX();
  144. scaleY *= scales.getY();
  145. }
  146. }
  147. int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
  148. int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
  149. return new Dimension(bitmapWidth, bitmapHeight);
  150. }
  151. /** {@inheritDoc} */
  152. public PageFormat getPageFormat(int pageIndex)
  153. throws IndexOutOfBoundsException {
  154. try {
  155. if (pageIndex >= getNumberOfPages()) {
  156. return null;
  157. }
  158. PageFormat pageFormat = new PageFormat();
  159. Paper paper = new Paper();
  160. Rectangle2D dim = getPageViewport(pageIndex).getViewArea();
  161. double width = dim.getWidth();
  162. double height = dim.getHeight();
  163. // if the width is greater than the height assume lanscape mode
  164. // and swap the width and height values in the paper format
  165. if (width > height) {
  166. paper.setImageableArea(0, 0, height / 1000d, width / 1000d);
  167. paper.setSize(height / 1000d, width / 1000d);
  168. pageFormat.setOrientation(PageFormat.LANDSCAPE);
  169. } else {
  170. paper.setImageableArea(0, 0, width / 1000d, height / 1000d);
  171. paper.setSize(width / 1000d, height / 1000d);
  172. pageFormat.setOrientation(PageFormat.PORTRAIT);
  173. }
  174. pageFormat.setPaper(paper);
  175. return pageFormat;
  176. } catch (FOPException fopEx) {
  177. throw new IndexOutOfBoundsException(fopEx.getMessage());
  178. }
  179. }
  180. /** {@inheritDoc} */
  181. public Printable getPrintable(int pageIndex)
  182. throws IndexOutOfBoundsException {
  183. return this;
  184. }
  185. /** {@inheritDoc} */
  186. public boolean supportsOutOfOrder() {
  187. return false;
  188. }
  189. /** {@inheritDoc} */
  190. public String getMimeType() {
  191. return MIME_TYPE;
  192. }
  193. /**
  194. * Draws the background and borders and adds a basic debug view // TODO
  195. * implement visual-debugging as standalone
  196. *
  197. * {@inheritDoc}
  198. * float, float, float, float)
  199. *
  200. * @param area the area to get the traits from
  201. * @param startx the start x position
  202. * @param starty the start y position
  203. * @param width the width of the area
  204. * @param height the height of the area
  205. */
  206. protected void drawBackAndBorders(Area area, float startx, float starty,
  207. float width, float height) {
  208. if (debug) {
  209. debugBackAndBorders(area, startx, starty, width, height);
  210. }
  211. super.drawBackAndBorders(area, startx, starty, width, height);
  212. }
  213. /** Draws a thin border around every area to help debugging */
  214. private void debugBackAndBorders(Area area, float startx, float starty,
  215. float width, float height) {
  216. // saves the graphics state in a stack
  217. saveGraphicsState();
  218. Color col = new Color(0.7f, 0.7f, 0.7f);
  219. state.updateColor(col);
  220. state.updateStroke(0.4f, EN_SOLID);
  221. state.getGraph().draw(
  222. new Rectangle2D.Float(startx, starty, width, height));
  223. // restores the last graphics state from the stack
  224. restoreGraphicsState();
  225. }
  226. /** @return the StatusListener. */
  227. public StatusListener getStatusListener() {
  228. return statusListener;
  229. }
  230. /**
  231. * Sets a StatusListener this renderer uses to notify about events.
  232. * @param statusListener The StatusListener to set.
  233. */
  234. public void setStatusListener(StatusListener statusListener) {
  235. this.statusListener = statusListener;
  236. }
  237. }