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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.Point2D;
  29. import java.awt.geom.Rectangle2D;
  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.xmlgraphics.util.UnitConv;
  36. import org.apache.fop.apps.FOPException;
  37. import org.apache.fop.apps.FOUserAgent;
  38. import org.apache.fop.apps.FopFactoryConfigurator;
  39. import org.apache.fop.apps.MimeConstants;
  40. import org.apache.fop.area.Area;
  41. import org.apache.fop.area.PageViewport;
  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.StatusListener;
  45. import org.apache.fop.render.extensions.prepress.PageScale;
  46. import org.apache.fop.render.java2d.Java2DRenderer;
  47. /**
  48. * The AWTRender outputs the pages generated by the layout engine to a Swing
  49. * window. This Swing window serves as default viewer for the -awt switch and as
  50. * an example of how to embed the AWTRenderer into an AWT/Swing application.
  51. */
  52. public class AWTRenderer extends Java2DRenderer implements Pageable {
  53. /** The MIME type for AWT-Rendering */
  54. public static final String MIME_TYPE = MimeConstants.MIME_FOP_AWT_PREVIEW;
  55. /** flag for debugging */
  56. public boolean debug; // CSOK: VisibilityModifier
  57. /** If true, preview dialog is shown. */
  58. public boolean dialogDisplay = true; // CSOK: VisibilityModifier
  59. /** true if the preview dialog should be the main window of the application */
  60. private boolean previewAsMainWindow;
  61. /**
  62. * Renderable instance that can be used to reload and re-render a document after
  63. * modifications.
  64. */
  65. protected Renderable renderable;
  66. /**
  67. * Will be notified when rendering progresses
  68. */
  69. protected StatusListener statusListener = null;
  70. /**
  71. * Creates a new AWTRenderer instance.
  72. */
  73. public AWTRenderer() {
  74. this(false);
  75. }
  76. /**
  77. * Creates a new AWTRenderer instance.
  78. * @param previewAsMainWindow true if the preview dialog created by the renderer should be
  79. * the main window of the application.
  80. */
  81. public AWTRenderer(boolean previewAsMainWindow) {
  82. this.previewAsMainWindow = previewAsMainWindow;
  83. }
  84. /** {@inheritDoc} */
  85. public void setUserAgent(FOUserAgent foUserAgent) {
  86. super.setUserAgent(foUserAgent);
  87. if (dialogDisplay) {
  88. setStatusListener(PreviewDialog.createPreviewDialog(userAgent, this.renderable,
  89. this.previewAsMainWindow));
  90. }
  91. }
  92. /**
  93. * A Renderable instance can be set so the Preview Dialog can enable the "Reload" button
  94. * which causes the current document to be reprocessed and redisplayed.
  95. * @param renderable the Renderable instance.
  96. */
  97. public void setRenderable(Renderable renderable) {
  98. this.renderable = renderable;
  99. }
  100. /**
  101. * Sets whether the preview dialog should be created and displayed when
  102. * the rendering is finished.
  103. * @param show If false, preview dialog is not shown. True by default
  104. */
  105. public void setPreviewDialogDisplayed(boolean show) {
  106. dialogDisplay = show;
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public void renderPage(PageViewport pageViewport) throws IOException {
  112. super.renderPage(pageViewport);
  113. if (statusListener != null) {
  114. statusListener.notifyPageRendered();
  115. }
  116. }
  117. /** {@inheritDoc} */
  118. public void stopRenderer() throws IOException {
  119. super.stopRenderer();
  120. if (statusListener != null) {
  121. statusListener.notifyRendererStopped(); // Refreshes view of page
  122. }
  123. }
  124. /**
  125. * @return the dimensions of the specified page
  126. * @param pageNum the page number
  127. * @exception FOPException If the page is out of range or has not been rendered.
  128. */
  129. public Dimension getPageImageSize(int pageNum) throws FOPException {
  130. Rectangle2D bounds = getPageViewport(pageNum).getViewArea();
  131. pageWidth = (int) Math.round(bounds.getWidth() / 1000f);
  132. pageHeight = (int) Math.round(bounds.getHeight() / 1000f);
  133. double scaleX = scaleFactor
  134. * (UnitConv.IN2MM / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
  135. / userAgent.getTargetPixelUnitToMillimeter();
  136. double scaleY = scaleFactor
  137. * (UnitConv.IN2MM / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
  138. / userAgent.getTargetPixelUnitToMillimeter();
  139. if (getPageViewport(pageNum).getForeignAttributes() != null) {
  140. String scale = (String) getPageViewport(pageNum).getForeignAttributes().get(
  141. PageScale.EXT_PAGE_SCALE);
  142. Point2D scales = PageScale.getScale(scale);
  143. if (scales != null) {
  144. scaleX *= scales.getX();
  145. scaleY *= scales.getY();
  146. }
  147. }
  148. int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
  149. int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
  150. return new Dimension(bitmapWidth, bitmapHeight);
  151. }
  152. /** {@inheritDoc} */
  153. public PageFormat getPageFormat(int pageIndex)
  154. throws IndexOutOfBoundsException {
  155. try {
  156. if (pageIndex >= getNumberOfPages()) {
  157. return null;
  158. }
  159. PageFormat pageFormat = new PageFormat();
  160. Paper paper = new Paper();
  161. Rectangle2D dim = getPageViewport(pageIndex).getViewArea();
  162. double width = dim.getWidth();
  163. double height = dim.getHeight();
  164. // if the width is greater than the height assume lanscape mode
  165. // and swap the width and height values in the paper format
  166. if (width > height) {
  167. paper.setImageableArea(0, 0, height / 1000d, width / 1000d);
  168. paper.setSize(height / 1000d, width / 1000d);
  169. pageFormat.setOrientation(PageFormat.LANDSCAPE);
  170. } else {
  171. paper.setImageableArea(0, 0, width / 1000d, height / 1000d);
  172. paper.setSize(width / 1000d, height / 1000d);
  173. pageFormat.setOrientation(PageFormat.PORTRAIT);
  174. }
  175. pageFormat.setPaper(paper);
  176. return pageFormat;
  177. } catch (FOPException fopEx) {
  178. throw new IndexOutOfBoundsException(fopEx.getMessage());
  179. }
  180. }
  181. /** {@inheritDoc} */
  182. public Printable getPrintable(int pageIndex)
  183. throws IndexOutOfBoundsException {
  184. return this;
  185. }
  186. /** {@inheritDoc} */
  187. public boolean supportsOutOfOrder() {
  188. return false;
  189. }
  190. /** {@inheritDoc} */
  191. public String getMimeType() {
  192. return MIME_TYPE;
  193. }
  194. /**
  195. * Draws the background and borders and adds a basic debug view // TODO
  196. * implement visual-debugging as standalone
  197. *
  198. * {@inheritDoc}
  199. * float, float, float, float)
  200. *
  201. * @param area the area to get the traits from
  202. * @param startx the start x position
  203. * @param starty the start y position
  204. * @param width the width of the area
  205. * @param height the height of the area
  206. */
  207. protected void drawBackAndBorders(Area area, float startx, float starty,
  208. float width, float height) {
  209. if (debug) {
  210. debugBackAndBorders(area, startx, starty, width, height);
  211. }
  212. super.drawBackAndBorders(area, startx, starty, width, height);
  213. }
  214. /** Draws a thin border around every area to help debugging */
  215. private void debugBackAndBorders(Area area, float startx, float starty,
  216. float width, float height) {
  217. // saves the graphics state in a stack
  218. saveGraphicsState();
  219. Color col = new Color(0.7f, 0.7f, 0.7f);
  220. state.updateColor(col);
  221. state.updateStroke(0.4f, EN_SOLID);
  222. state.getGraph().draw(
  223. new Rectangle2D.Float(startx, starty, width, height));
  224. // restores the last graphics state from the stack
  225. restoreGraphicsState();
  226. }
  227. /** @return the StatusListener. */
  228. public StatusListener getStatusListener() {
  229. return statusListener;
  230. }
  231. /**
  232. * Sets a StatusListener this renderer uses to notify about events.
  233. * @param statusListener The StatusListener to set.
  234. */
  235. public void setStatusListener(StatusListener statusListener) {
  236. this.statusListener = statusListener;
  237. }
  238. }