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.

PSDocumentGraphics2D.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.io.OutputStream;
  55. import java.io.IOException;
  56. //FOP
  57. import org.apache.fop.apps.Document;
  58. import org.apache.fop.fonts.FontSetup;
  59. /**
  60. * This class is a wrapper for the <tt>PSGraphics2D</tt> that
  61. * is used to create a full document around the PostScript rendering from
  62. * <tt>PSGraphics2D</tt>.
  63. *
  64. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  65. * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
  66. * @version $Id$
  67. * @see org.apache.fop.render.ps.PSGraphics2D
  68. */
  69. public class PSDocumentGraphics2D extends AbstractPSDocumentGraphics2D {
  70. /**
  71. * Create a new AbstractPSDocumentGraphics2D.
  72. * This is used to create a new PostScript document, the height,
  73. * width and output stream can be setup later.
  74. * For use by the transcoder which needs font information
  75. * for the bridge before the document size is known.
  76. * The resulting document is written to the stream after rendering.
  77. *
  78. * @param textAsShapes set this to true so that text will be rendered
  79. * using curves and not the font.
  80. */
  81. PSDocumentGraphics2D(boolean textAsShapes) {
  82. super(textAsShapes);
  83. if (!textAsShapes) {
  84. this.document = new Document(null);
  85. FontSetup.setup(this.document, null);
  86. }
  87. }
  88. /**
  89. * Create a new AbstractPSDocumentGraphics2D.
  90. * This is used to create a new PostScript document of the given height
  91. * and width.
  92. * The resulting document is written to the stream after rendering.
  93. *
  94. * @param textAsShapes set this to true so that text will be rendered
  95. * using curves and not the font.
  96. * @param stream the stream that the final document should be written to.
  97. * @param width the width of the document
  98. * @param height the height of the document
  99. * @throws IOException an io exception if there is a problem
  100. * writing to the output stream
  101. */
  102. public PSDocumentGraphics2D(boolean textAsShapes, OutputStream stream,
  103. int width, int height) throws IOException {
  104. this(textAsShapes);
  105. setupDocument(stream, width, height);
  106. }
  107. public void nextPage() throws IOException {
  108. closePage();
  109. }
  110. protected void writeFileHeader() throws IOException {
  111. final Long pagewidth = new Long(this.width);
  112. final Long pageheight = new Long(this.height);
  113. //PostScript Header
  114. gen.writeln(DSCConstants.PS_ADOBE_30);
  115. gen.writeDSCComment(DSCConstants.CREATOR,
  116. new String[] {"FOP PostScript Transcoder for SVG"});
  117. gen.writeDSCComment(DSCConstants.CREATION_DATE,
  118. new Object[] {new java.util.Date()});
  119. gen.writeDSCComment(DSCConstants.PAGES, PSGenerator.ATEND);
  120. gen.writeDSCComment(DSCConstants.BBOX, new Object[]
  121. {ZERO, ZERO, pagewidth, pageheight});
  122. gen.writeDSCComment(DSCConstants.END_COMMENTS);
  123. //Defaults
  124. gen.writeDSCComment(DSCConstants.BEGIN_DEFAULTS);
  125. gen.writeDSCComment(DSCConstants.END_DEFAULTS);
  126. //Prolog
  127. gen.writeDSCComment(DSCConstants.BEGIN_PROLOG);
  128. gen.writeDSCComment(DSCConstants.END_PROLOG);
  129. //Setup
  130. gen.writeDSCComment(DSCConstants.BEGIN_SETUP);
  131. PSProcSets.writeFOPStdProcSet(gen);
  132. PSProcSets.writeFOPEPSProcSet(gen);
  133. if (document != null) {
  134. PSProcSets.writeFontDict(gen, document);
  135. }
  136. gen.writeDSCComment(DSCConstants.END_SETUP);
  137. }
  138. protected void writePageHeader() throws IOException {
  139. Integer pageNumber = new Integer(this.pagecount);
  140. gen.writeDSCComment(DSCConstants.PAGE, new Object[]
  141. {pageNumber.toString(), pageNumber});
  142. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[]
  143. {ZERO, ZERO, new Integer(width), new Integer(height)});
  144. gen.writeDSCComment(DSCConstants.BEGIN_PAGE_SETUP);
  145. gen.writeln("<<");
  146. gen.writeln("/PageSize [" + width + " " + height + "]");
  147. gen.writeln("/ImagingBBox null");
  148. gen.writeln(">> setpagedevice");
  149. if (this.document != null) {
  150. gen.writeln("FOPFonts begin");
  151. }
  152. }
  153. protected void writePageTrailer() throws IOException {
  154. gen.writeln("showpage");
  155. gen.writeDSCComment(DSCConstants.PAGE_TRAILER);
  156. gen.writeDSCComment(DSCConstants.END_PAGE);
  157. }
  158. /**
  159. * This constructor supports the create method
  160. * @param g the PostScript document graphics to make a copy of
  161. */
  162. public PSDocumentGraphics2D(PSDocumentGraphics2D g) {
  163. super(g);
  164. }
  165. /**
  166. * Creates a new <code>Graphics</code> object that is
  167. * a copy of this <code>Graphics</code> object.
  168. * @return a new graphics context that is a copy of
  169. * this graphics context.
  170. */
  171. public Graphics create() {
  172. return new PSDocumentGraphics2D(this);
  173. }
  174. }