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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * $Id: PDFDocumentGraphics2D.java,v 1.27 2003/03/07 09:51:26 jeremias Exp $
  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.Font;
  55. import java.awt.Color;
  56. import java.awt.Shape;
  57. import java.awt.font.FontRenderContext;
  58. import java.awt.font.GlyphVector;
  59. import java.io.OutputStream;
  60. import java.io.IOException;
  61. //FOP
  62. import org.apache.fop.render.pdf.FontSetup;
  63. import org.apache.fop.layout.FontInfo;
  64. /**
  65. * This class is a wrapper for the <tt>PSGraphics2D</tt> that
  66. * is used to create a full document around the PostScript rendering from
  67. * <tt>PSGraphics2D</tt>.
  68. *
  69. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  70. * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
  71. * @version $Id: PDFDocumentGraphics2D.java,v 1.27 2003/03/07 09:51:26 jeremias Exp $
  72. * @see org.apache.fop.svg.PSGraphics2D
  73. */
  74. public class PSDocumentGraphics2D extends PSGraphics2D {
  75. private int width;
  76. private int height;
  77. /**
  78. * Create a new PSDocumentGraphics2D.
  79. * This is used to create a new PostScript document, the height,
  80. * width and output stream can be setup later.
  81. * For use by the transcoder which needs font information
  82. * for the bridge before the document size is known.
  83. * The resulting document is written to the stream after rendering.
  84. *
  85. * @param textAsShapes set this to true so that text will be rendered
  86. * using curves and not the font.
  87. */
  88. PSDocumentGraphics2D(boolean textAsShapes) {
  89. super(textAsShapes);
  90. if (!textAsShapes) {
  91. fontInfo = new FontInfo();
  92. FontSetup.setup(fontInfo, null);
  93. //FontState fontState = new FontState("Helvetica", "normal",
  94. // FontInfo.NORMAL, 12, 0);
  95. }
  96. currentFontName = "";
  97. currentFontSize = 0;
  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. final Integer zero = new Integer(0);
  111. final Long pagewidth = new Long(this.width);
  112. final Long pageheight = new Long(this.height);
  113. //Setup for PostScript generation
  114. setPSGenerator(new PSGenerator(stream));
  115. //PostScript Header
  116. gen.writeln(DSCConstants.PS_ADOBE_30);
  117. gen.writeDSCComment(DSCConstants.CREATOR,
  118. new String[] {"FOP PostScript Transcoder for SVG"});
  119. gen.writeDSCComment(DSCConstants.CREATION_DATE,
  120. new Object[] {new java.util.Date()});
  121. gen.writeDSCComment(DSCConstants.PAGES, new Object[] {new Integer(1)});
  122. gen.writeDSCComment(DSCConstants.BBOX, new Object[]
  123. {zero, zero, pagewidth, pageheight});
  124. gen.writeDSCComment(DSCConstants.END_COMMENTS);
  125. //Defaults
  126. gen.writeDSCComment(DSCConstants.BEGIN_DEFAULTS);
  127. gen.writeDSCComment(DSCConstants.END_DEFAULTS);
  128. //Prolog
  129. gen.writeDSCComment(DSCConstants.BEGIN_PROLOG);
  130. gen.writeDSCComment(DSCConstants.END_PROLOG);
  131. //Setup
  132. gen.writeDSCComment(DSCConstants.BEGIN_SETUP);
  133. PSProcSets.writeFOPStdProcSet(gen);
  134. PSProcSets.writeFOPEPSProcSet(gen);
  135. PSProcSets.writeFontDict(gen, fontInfo);
  136. gen.writeDSCComment(DSCConstants.END_SETUP);
  137. //Start page
  138. Integer pageNumber = new Integer(1);
  139. gen.writeDSCComment(DSCConstants.PAGE, new Object[]
  140. {pageNumber.toString(), pageNumber});
  141. gen.writeDSCComment(DSCConstants.PAGE_BBOX, new Object[]
  142. {zero, zero, pagewidth, pageheight});
  143. gen.writeDSCComment(DSCConstants.BEGIN_PAGE_SETUP);
  144. gen.writeln("FOPFonts begin");
  145. gen.writeln("0.001 0.001 scale");
  146. gen.concatMatrix(1, 0, 0, -1, 0, pageheight.doubleValue() * 1000);
  147. gen.writeDSCComment(DSCConstants.END_PAGE_SETUP);
  148. }
  149. /**
  150. * Create a new PSDocumentGraphics2D.
  151. * This is used to create a new PostScript document of the given height
  152. * and width.
  153. * The resulting document is written to the stream after rendering.
  154. *
  155. * @param textAsShapes set this to true so that text will be rendered
  156. * using curves and not the font.
  157. * @param stream the stream that the final document should be written to.
  158. * @param width the width of the document
  159. * @param height the height of the document
  160. * @throws IOException an io exception if there is a problem
  161. * writing to the output stream
  162. */
  163. public PSDocumentGraphics2D(boolean textAsShapes, OutputStream stream,
  164. int width, int height) throws IOException {
  165. this(textAsShapes);
  166. setupDocument(stream, width, height);
  167. }
  168. /**
  169. * Get the font info for this PostScript document.
  170. * @return the font information
  171. */
  172. public FontInfo getFontInfo() {
  173. return fontInfo;
  174. }
  175. /**
  176. * Set the dimensions of the SVG document that will be drawn.
  177. * This is useful if the dimensions of the SVG document are different
  178. * from the PostScript document that is to be created.
  179. * The result is scaled so that the SVG fits correctly inside the
  180. * PostScript document.
  181. * @param w the width of the page
  182. * @param h the height of the page
  183. * @throws IOException in case of an I/O problem
  184. */
  185. public void setSVGDimension(float w, float h) throws IOException {
  186. gen.concatMatrix(width / w, 0, 0, height / h, 0, 0);
  187. }
  188. /**
  189. * Set the background of the PostScript document.
  190. * This is used to set the background for the PostScript document
  191. * Rather than leaving it as the default white.
  192. * @param col the background colour to fill
  193. */
  194. public void setBackgroundColor(Color col) {
  195. /**(todo) Implement this */
  196. /*
  197. Color c = col;
  198. PDFColor currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
  199. currentStream.write("q\n");
  200. currentStream.write(currentColour.getColorSpaceOut(true));
  201. currentStream.write("0 0 " + width + " " + height + " re\n");
  202. currentStream.write("f\n");
  203. currentStream.write("Q\n");
  204. */
  205. }
  206. /**
  207. * The rendering process has finished.
  208. * This should be called after the rendering has completed as there is
  209. * no other indication it is complete.
  210. * This will then write the results to the output stream.
  211. * @throws IOException an io exception if there is a problem
  212. * writing to the output stream
  213. */
  214. public void finish() throws IOException {
  215. //Finish page
  216. gen.writeln("showpage");
  217. gen.writeDSCComment(DSCConstants.PAGE_TRAILER);
  218. gen.writeDSCComment(DSCConstants.END_PAGE);
  219. //Finish document
  220. gen.writeDSCComment(DSCConstants.TRAILER);
  221. gen.writeDSCComment(DSCConstants.EOF);
  222. gen.flush();
  223. }
  224. /**
  225. * This constructor supports the create method
  226. * @param g the PostScript document graphics to make a copy of
  227. */
  228. public PSDocumentGraphics2D(PSDocumentGraphics2D g) {
  229. super(g);
  230. }
  231. /**
  232. * Creates a new <code>Graphics</code> object that is
  233. * a copy of this <code>Graphics</code> object.
  234. * @return a new graphics context that is a copy of
  235. * this graphics context.
  236. */
  237. public Graphics create() {
  238. return new PSDocumentGraphics2D(this);
  239. }
  240. /**
  241. * Draw a string to the PostScript document.
  242. * This either draws the string directly or if drawing text as
  243. * shapes it converts the string into shapes and draws that.
  244. * @param s the string to draw
  245. * @param x the x position
  246. * @param y the y position
  247. */
  248. public void drawString(String s, float x, float y) {
  249. if (super.textAsShapes) {
  250. Font font = super.getFont();
  251. FontRenderContext frc = super.getFontRenderContext();
  252. GlyphVector gv = font.createGlyphVector(frc, s);
  253. Shape glyphOutline = gv.getOutline(x, y);
  254. super.fill(glyphOutline);
  255. } else {
  256. super.drawString(s, x, y);
  257. }
  258. }
  259. }