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.

AbstractPSDocumentGraphics2D.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.render.ps;
  52. //Java
  53. import java.awt.Graphics;
  54. import java.awt.Color;
  55. import java.awt.Shape;
  56. import java.awt.geom.AffineTransform;
  57. import java.io.OutputStream;
  58. import java.io.IOException;
  59. //FOP
  60. import org.apache.fop.apps.Document;
  61. import org.apache.fop.fonts.Font;
  62. import org.apache.fop.fonts.FontSetup;
  63. /**
  64. * This class is a wrapper for the <tt>PSGraphics2D</tt> that
  65. * is used to create a full document around the PostScript rendering from
  66. * <tt>PSGraphics2D</tt>.
  67. *
  68. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  69. * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
  70. * @version $Id$
  71. * @see org.apache.fop.render.ps.PSGraphics2D
  72. */
  73. public abstract class AbstractPSDocumentGraphics2D extends PSGraphics2D {
  74. protected static final Integer ZERO = new Integer(0);
  75. protected int width;
  76. protected int height;
  77. protected int pagecount;
  78. protected boolean pagePending;
  79. protected Shape initialClip;
  80. protected AffineTransform initialTransform;
  81. /**
  82. * Create a new AbstractPSDocumentGraphics2D.
  83. * This is used to create a new PostScript document, the height,
  84. * width and output stream can be setup later.
  85. * For use by the transcoder which needs font information
  86. * for the bridge before the document size is known.
  87. * The resulting document is written to the stream after rendering.
  88. *
  89. * @param textAsShapes set this to true so that text will be rendered
  90. * using curves and not the font.
  91. */
  92. AbstractPSDocumentGraphics2D(boolean textAsShapes) {
  93. super(textAsShapes);
  94. if (!textAsShapes) {
  95. this.document = new Document(null);
  96. FontSetup.setup(this.document, null);
  97. }
  98. }
  99. /**
  100. * Setup the document.
  101. * @param stream the output stream to write the document
  102. * @param width the width of the page
  103. * @param height the height of the page
  104. * @throws IOException an io exception if there is a problem
  105. * writing to the output stream
  106. */
  107. public void setupDocument(OutputStream stream, int width, int height) throws IOException {
  108. this.width = width;
  109. this.height = height;
  110. this.pagecount = 0;
  111. this.pagePending = false;
  112. //Setup for PostScript generation
  113. setPSGenerator(new PSGenerator(stream));
  114. writeFileHeader();
  115. }
  116. protected abstract void writeFileHeader() throws IOException;
  117. /**
  118. * Create a new AbstractPSDocumentGraphics2D.
  119. * This is used to create a new PostScript document of the given height
  120. * and width.
  121. * The resulting document is written to the stream after rendering.
  122. *
  123. * @param textAsShapes set this to true so that text will be rendered
  124. * using curves and not the font.
  125. * @param stream the stream that the final document should be written to.
  126. * @param width the width of the document
  127. * @param height the height of the document
  128. * @throws IOException an io exception if there is a problem
  129. * writing to the output stream
  130. */
  131. public AbstractPSDocumentGraphics2D(boolean textAsShapes, OutputStream stream,
  132. int width, int height) throws IOException {
  133. this(textAsShapes);
  134. setupDocument(stream, width, height);
  135. }
  136. /**
  137. * Get the context document.
  138. * @return the context document
  139. */
  140. public Document getDocument() {
  141. return this.document;
  142. }
  143. /**
  144. * Set the dimensions of the SVG document that will be drawn.
  145. * This is useful if the dimensions of the SVG document are different
  146. * from the PostScript document that is to be created.
  147. * The result is scaled so that the SVG fits correctly inside the
  148. * PostScript document.
  149. * @param w the width of the page
  150. * @param h the height of the page
  151. * @throws IOException in case of an I/O problem
  152. */
  153. public void setSVGDimension(float w, float h) throws IOException {
  154. if (w != this.width || h != this.height) {
  155. gen.concatMatrix(width / w, 0, 0, height / h, 0, 0);
  156. }
  157. }
  158. /**
  159. * Set the background of the PostScript document.
  160. * This is used to set the background for the PostScript document
  161. * Rather than leaving it as the default white.
  162. * @param col the background colour to fill
  163. */
  164. public void setBackgroundColor(Color col) {
  165. /**(todo) Implement this */
  166. /*
  167. Color c = col;
  168. PDFColor currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
  169. currentStream.write("q\n");
  170. currentStream.write(currentColour.getColorSpaceOut(true));
  171. currentStream.write("0 0 " + width + " " + height + " re\n");
  172. currentStream.write("f\n");
  173. currentStream.write("Q\n");
  174. */
  175. }
  176. public int getPageCount() {
  177. return this.pagecount;
  178. }
  179. public void nextPage() throws IOException {
  180. closePage();
  181. }
  182. protected void closePage() throws IOException {
  183. if (!this.pagePending) {
  184. return; //ignore
  185. }
  186. //Finish page
  187. writePageTrailer();
  188. this.pagePending = false;
  189. }
  190. /**
  191. * Writes the page header for a page.
  192. * @throws IOException In case an I/O error occurs
  193. */
  194. protected abstract void writePageHeader() throws IOException;
  195. /**
  196. * Writes the page trailer for a page.
  197. * @throws IOException In case an I/O error occurs
  198. */
  199. protected abstract void writePageTrailer() throws IOException;
  200. /** {@inheritDoc} */
  201. protected void preparePainting() {
  202. if (this.pagePending) {
  203. return;
  204. }
  205. try {
  206. startPage();
  207. } catch (IOException ioe) {
  208. handleIOException(ioe);
  209. }
  210. }
  211. protected void startPage() throws IOException {
  212. if (this.pagePending) {
  213. throw new IllegalStateException("Close page first before starting another");
  214. }
  215. //Start page
  216. this.pagecount++;
  217. if (this.initialTransform == null) {
  218. //Save initial transformation matrix
  219. this.initialTransform = getTransform();
  220. this.initialClip = getClip();
  221. } else {
  222. //Reset transformation matrix
  223. setTransform(this.initialTransform);
  224. setClip(this.initialClip);
  225. }
  226. writePageHeader();
  227. gen.writeln("0.001 0.001 scale");
  228. gen.concatMatrix(1, 0, 0, -1, 0, this.height * 1000);
  229. gen.writeDSCComment(DSCConstants.END_PAGE_SETUP);
  230. this.pagePending = true;
  231. }
  232. /**
  233. * The rendering process has finished.
  234. * This should be called after the rendering has completed as there is
  235. * no other indication it is complete.
  236. * This will then write the results to the output stream.
  237. * @throws IOException an io exception if there is a problem
  238. * writing to the output stream
  239. */
  240. public void finish() throws IOException {
  241. if (this.pagePending) {
  242. closePage();
  243. }
  244. //Finish document
  245. gen.writeDSCComment(DSCConstants.TRAILER);
  246. gen.writeDSCComment(DSCConstants.PAGES, new Integer(this.pagecount));
  247. gen.writeDSCComment(DSCConstants.EOF);
  248. gen.flush();
  249. }
  250. /**
  251. * This constructor supports the create method
  252. * @param g the PostScript document graphics to make a copy of
  253. */
  254. public AbstractPSDocumentGraphics2D(AbstractPSDocumentGraphics2D g) {
  255. super(g);
  256. }
  257. }