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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /**
  58. * Will be notified when rendering progresses
  59. */
  60. protected StatusListener statusListener = null;
  61. /**
  62. * Creates a new AWTRenderer instance.
  63. *
  64. * @param userAgent the user agent that contains configuration data
  65. */
  66. public AWTRenderer(FOUserAgent userAgent) {
  67. this(userAgent, null, false, false);
  68. }
  69. /**
  70. * Creates a new AWTRenderer instance.
  71. *
  72. * @param userAgent the user agent that contains configuration data
  73. * @param renderable a Renderable instance can be set so the Preview Dialog can enable the
  74. * "Reload" button which causes the current document to be reprocessed and redisplayed.
  75. * @param previewAsMainWindow true if the preview dialog created by the renderer should be
  76. * the main window of the application.
  77. * @param show sets whether the preview dialog should be created and displayed when the
  78. * rendering has finished.
  79. */
  80. public AWTRenderer(FOUserAgent userAgent, Renderable renderable, boolean previewAsMainWindow,
  81. boolean show) {
  82. super(userAgent);
  83. if (show) {
  84. // MH: Not sure about this??? If show is false, there's no way for this class
  85. // to create a preview dialog... Previously a "setUserAgent" could be called.
  86. setStatusListener(PreviewDialog.createPreviewDialog(userAgent, renderable,
  87. previewAsMainWindow));
  88. }
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public void renderPage(PageViewport pageViewport) throws IOException {
  94. super.renderPage(pageViewport);
  95. if (statusListener != null) {
  96. statusListener.notifyPageRendered();
  97. }
  98. }
  99. /** {@inheritDoc} */
  100. public void stopRenderer() throws IOException {
  101. super.stopRenderer();
  102. if (statusListener != null) {
  103. statusListener.notifyRendererStopped(); // Refreshes view of page
  104. }
  105. }
  106. /**
  107. * @return the dimensions of the specified page
  108. * @param pageNum the page number
  109. * @exception FOPException If the page is out of range or has not been rendered.
  110. */
  111. public Dimension getPageImageSize(int pageNum) throws FOPException {
  112. Rectangle2D bounds = getPageViewport(pageNum).getViewArea();
  113. pageWidth = (int) Math.round(bounds.getWidth() / 1000f);
  114. pageHeight = (int) Math.round(bounds.getHeight() / 1000f);
  115. double scaleX = scaleFactor
  116. * (UnitConv.IN2MM / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
  117. / userAgent.getTargetPixelUnitToMillimeter();
  118. double scaleY = scaleFactor
  119. * (UnitConv.IN2MM / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
  120. / userAgent.getTargetPixelUnitToMillimeter();
  121. if (getPageViewport(pageNum).getForeignAttributes() != null) {
  122. String scale = (String) getPageViewport(pageNum).getForeignAttributes().get(
  123. PageScale.EXT_PAGE_SCALE);
  124. Point2D scales = PageScale.getScale(scale);
  125. if (scales != null) {
  126. scaleX *= scales.getX();
  127. scaleY *= scales.getY();
  128. }
  129. }
  130. int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
  131. int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
  132. return new Dimension(bitmapWidth, bitmapHeight);
  133. }
  134. /** {@inheritDoc} */
  135. public PageFormat getPageFormat(int pageIndex)
  136. throws IndexOutOfBoundsException {
  137. try {
  138. if (pageIndex >= getNumberOfPages()) {
  139. return null;
  140. }
  141. PageFormat pageFormat = new PageFormat();
  142. Paper paper = new Paper();
  143. Rectangle2D dim = getPageViewport(pageIndex).getViewArea();
  144. double width = dim.getWidth();
  145. double height = dim.getHeight();
  146. // if the width is greater than the height assume lanscape mode
  147. // and swap the width and height values in the paper format
  148. if (width > height) {
  149. paper.setImageableArea(0, 0, height / 1000d, width / 1000d);
  150. paper.setSize(height / 1000d, width / 1000d);
  151. pageFormat.setOrientation(PageFormat.LANDSCAPE);
  152. } else {
  153. paper.setImageableArea(0, 0, width / 1000d, height / 1000d);
  154. paper.setSize(width / 1000d, height / 1000d);
  155. pageFormat.setOrientation(PageFormat.PORTRAIT);
  156. }
  157. pageFormat.setPaper(paper);
  158. return pageFormat;
  159. } catch (FOPException fopEx) {
  160. throw new IndexOutOfBoundsException(fopEx.getMessage());
  161. }
  162. }
  163. /** {@inheritDoc} */
  164. public Printable getPrintable(int pageIndex)
  165. throws IndexOutOfBoundsException {
  166. return this;
  167. }
  168. /** {@inheritDoc} */
  169. public boolean supportsOutOfOrder() {
  170. return false;
  171. }
  172. /** {@inheritDoc} */
  173. public String getMimeType() {
  174. return MIME_TYPE;
  175. }
  176. /**
  177. * Draws the background and borders and adds a basic debug view // TODO
  178. * implement visual-debugging as standalone
  179. *
  180. * {@inheritDoc}
  181. * float, float, float, float)
  182. *
  183. * @param area the area to get the traits from
  184. * @param startx the start x position
  185. * @param starty the start y position
  186. * @param width the width of the area
  187. * @param height the height of the area
  188. */
  189. protected void drawBackAndBorders(Area area, float startx, float starty,
  190. float width, float height) {
  191. if (debug) {
  192. debugBackAndBorders(area, startx, starty, width, height);
  193. }
  194. super.drawBackAndBorders(area, startx, starty, width, height);
  195. }
  196. /** Draws a thin border around every area to help debugging */
  197. private void debugBackAndBorders(Area area, float startx, float starty,
  198. float width, float height) {
  199. // saves the graphics state in a stack
  200. saveGraphicsState();
  201. Color col = new Color(0.7f, 0.7f, 0.7f);
  202. state.updateColor(col);
  203. state.updateStroke(0.4f, EN_SOLID);
  204. state.getGraph().draw(
  205. new Rectangle2D.Float(startx, starty, width, height));
  206. // restores the last graphics state from the stack
  207. restoreGraphicsState();
  208. }
  209. /** @return the StatusListener. */
  210. public StatusListener getStatusListener() {
  211. return statusListener;
  212. }
  213. /**
  214. * Sets a StatusListener this renderer uses to notify about events.
  215. * @param statusListener The StatusListener to set.
  216. */
  217. public void setStatusListener(StatusListener statusListener) {
  218. this.statusListener = statusListener;
  219. }
  220. }