您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractPSDocumentGraphics2D.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 1999-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.ps;
  18. //Java
  19. import java.awt.Color;
  20. import java.awt.Shape;
  21. import java.awt.geom.AffineTransform;
  22. import java.io.OutputStream;
  23. import java.io.IOException;
  24. //FOP
  25. import org.apache.fop.fonts.FontInfo;
  26. import org.apache.fop.fonts.FontSetup;
  27. /**
  28. * This class is a wrapper for the <tt>PSGraphics2D</tt> that
  29. * is used to create a full document around the PostScript rendering from
  30. * <tt>PSGraphics2D</tt>.
  31. *
  32. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  33. * @version $Id$
  34. * @see org.apache.fop.render.ps.PSGraphics2D
  35. */
  36. public abstract class AbstractPSDocumentGraphics2D extends PSGraphics2D {
  37. protected static final Integer ZERO = new Integer(0);
  38. protected int width;
  39. protected int height;
  40. protected float viewportWidth;
  41. protected float viewportHeight;
  42. protected int pagecount;
  43. protected boolean pagePending;
  44. protected Shape initialClip;
  45. protected AffineTransform initialTransform;
  46. /**
  47. * Create a new AbstractPSDocumentGraphics2D.
  48. * This is used to create a new PostScript document, the height,
  49. * width and output stream can be setup later.
  50. * For use by the transcoder which needs font information
  51. * for the bridge before the document size is known.
  52. * The resulting document is written to the stream after rendering.
  53. *
  54. * @param textAsShapes set this to true so that text will be rendered
  55. * using curves and not the font.
  56. */
  57. AbstractPSDocumentGraphics2D(boolean textAsShapes) {
  58. super(textAsShapes);
  59. if (!textAsShapes) {
  60. fontInfo = new FontInfo();
  61. FontSetup.setup(fontInfo, null, null);
  62. }
  63. }
  64. /**
  65. * Setup the document.
  66. * @param stream the output stream to write the document
  67. * @param width the width of the page
  68. * @param height the height of the page
  69. * @throws IOException an io exception if there is a problem
  70. * writing to the output stream
  71. */
  72. public void setupDocument(OutputStream stream, int width, int height) throws IOException {
  73. this.width = width;
  74. this.height = height;
  75. this.pagecount = 0;
  76. this.pagePending = false;
  77. //Setup for PostScript generation
  78. setPSGenerator(new PSGenerator(stream));
  79. writeFileHeader();
  80. }
  81. protected abstract void writeFileHeader() throws IOException;
  82. /**
  83. * Create a new AbstractPSDocumentGraphics2D.
  84. * This is used to create a new PostScript document of the given height
  85. * and width.
  86. * The resulting document is written to the stream after rendering.
  87. *
  88. * @param textAsShapes set this to true so that text will be rendered
  89. * using curves and not the font.
  90. * @param stream the stream that the final document should be written to.
  91. * @param width the width of the document
  92. * @param height the height of the document
  93. * @throws IOException an io exception if there is a problem
  94. * writing to the output stream
  95. */
  96. public AbstractPSDocumentGraphics2D(boolean textAsShapes, OutputStream stream,
  97. int width, int height) throws IOException {
  98. this(textAsShapes);
  99. setupDocument(stream, width, height);
  100. }
  101. /**
  102. * Set the dimensions of the SVG document that will be drawn.
  103. * This is useful if the dimensions of the SVG document are different
  104. * from the PostScript document that is to be created.
  105. * The result is scaled so that the SVG fits correctly inside the
  106. * PostScript document.
  107. * @param w the width of the page
  108. * @param h the height of the page
  109. * @throws IOException in case of an I/O problem
  110. */
  111. public void setSVGDimension(float w, float h) throws IOException {
  112. this.viewportWidth = w;
  113. this.viewportHeight = h;
  114. /*
  115. if (w != this.width || h != this.height) {
  116. gen.concatMatrix(width / w, 0, 0, height / h, 0, 0);
  117. }*/
  118. }
  119. /**
  120. * Set the background of the PostScript document.
  121. * This is used to set the background for the PostScript document
  122. * Rather than leaving it as the default white.
  123. * @param col the background colour to fill
  124. */
  125. public void setBackgroundColor(Color col) {
  126. /**(todo) Implement this */
  127. /*
  128. Color c = col;
  129. PDFColor currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
  130. currentStream.write("q\n");
  131. currentStream.write(currentColour.getColorSpaceOut(true));
  132. currentStream.write("0 0 " + width + " " + height + " re\n");
  133. currentStream.write("f\n");
  134. currentStream.write("Q\n");
  135. */
  136. }
  137. public int getPageCount() {
  138. return this.pagecount;
  139. }
  140. public void nextPage() throws IOException {
  141. closePage();
  142. }
  143. protected void closePage() throws IOException {
  144. if (!this.pagePending) {
  145. return; //ignore
  146. }
  147. //Finish page
  148. writePageTrailer();
  149. this.pagePending = false;
  150. }
  151. /**
  152. * Writes the page header for a page.
  153. * @throws IOException In case an I/O error occurs
  154. */
  155. protected abstract void writePageHeader() throws IOException;
  156. /**
  157. * Writes the page trailer for a page.
  158. * @throws IOException In case an I/O error occurs
  159. */
  160. protected abstract void writePageTrailer() throws IOException;
  161. /** {@inheritDoc} */
  162. protected void preparePainting() {
  163. if (this.pagePending) {
  164. return;
  165. }
  166. try {
  167. startPage();
  168. } catch (IOException ioe) {
  169. handleIOException(ioe);
  170. }
  171. }
  172. protected void startPage() throws IOException {
  173. if (this.pagePending) {
  174. throw new IllegalStateException("Close page first before starting another");
  175. }
  176. //Start page
  177. this.pagecount++;
  178. if (this.initialTransform == null) {
  179. //Save initial transformation matrix
  180. this.initialTransform = getTransform();
  181. this.initialClip = getClip();
  182. } else {
  183. //Reset transformation matrix
  184. setTransform(this.initialTransform);
  185. setClip(this.initialClip);
  186. }
  187. writePageHeader();
  188. if ((this.viewportWidth != this.width
  189. || this.viewportHeight != this.height)
  190. && (this.viewportWidth > 0) && (this.viewportHeight > 0)){
  191. gen.concatMatrix(this.width / this.viewportWidth, 0,
  192. 0, -1 * (this.height / this.viewportHeight),
  193. 0, this.height);
  194. } else {
  195. gen.concatMatrix(1, 0, 0, -1, 0, this.height);
  196. }
  197. gen.writeDSCComment(DSCConstants.END_PAGE_SETUP);
  198. this.pagePending = true;
  199. }
  200. /**
  201. * The rendering process has finished.
  202. * This should be called after the rendering has completed as there is
  203. * no other indication it is complete.
  204. * This will then write the results to the output stream.
  205. * @throws IOException an io exception if there is a problem
  206. * writing to the output stream
  207. */
  208. public void finish() throws IOException {
  209. if (this.pagePending) {
  210. closePage();
  211. }
  212. //Finish document
  213. gen.writeDSCComment(DSCConstants.TRAILER);
  214. gen.writeDSCComment(DSCConstants.PAGES, new Integer(this.pagecount));
  215. gen.writeDSCComment(DSCConstants.EOF);
  216. gen.flush();
  217. }
  218. /**
  219. * This constructor supports the create method
  220. * @param g the PostScript document graphics to make a copy of
  221. */
  222. public AbstractPSDocumentGraphics2D(AbstractPSDocumentGraphics2D g) {
  223. super(g);
  224. }
  225. }