Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.svg;
  19. import java.awt.Dimension;
  20. import java.awt.geom.AffineTransform;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.io.Writer;
  25. import org.w3c.dom.DOMImplementation;
  26. import org.w3c.dom.Document;
  27. import org.apache.batik.dom.GenericDOMImplementation;
  28. import org.apache.batik.svggen.SVGGeneratorContext;
  29. import org.apache.batik.svggen.SVGGraphics2D;
  30. import org.apache.commons.io.IOUtils;
  31. import org.apache.commons.logging.Log;
  32. import org.apache.commons.logging.LogFactory;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.MimeConstants;
  35. import org.apache.fop.area.PageViewport;
  36. import org.apache.fop.render.bitmap.MultiFileRenderingUtil;
  37. import org.apache.fop.render.java2d.Java2DGraphicsState;
  38. import org.apache.fop.render.java2d.Java2DRenderer;
  39. /**
  40. * <p>
  41. * This renderer generates SVG (Scalable Vector Graphics) format.
  42. * <p>
  43. * This class actually does not render itself, instead it extends
  44. * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and uses
  45. * Apache Batik's SVGGraphics2D for SVG generation.
  46. */
  47. public class SVGRenderer extends Java2DRenderer {
  48. /** logging instance */
  49. private static Log log = LogFactory.getLog(SVGRenderer.class);
  50. /** The MIME type for the SVG format */
  51. public static final String MIME_TYPE = MimeConstants.MIME_SVG;
  52. private static final String SVG_FILE_EXTENSION = "svg";
  53. private OutputStream firstOutputStream;
  54. private Document document;
  55. private SVGGraphics2D svgGenerator;
  56. /** Helper class for generating multiple files */
  57. private MultiFileRenderingUtil multiFileUtil;
  58. public SVGRenderer(FOUserAgent userAgent) {
  59. super(userAgent);
  60. }
  61. /** {@inheritDoc} */
  62. public String getMimeType() {
  63. return MIME_TYPE;
  64. }
  65. /** {@inheritDoc} */
  66. public void startRenderer(OutputStream outputStream) throws IOException {
  67. this.firstOutputStream = outputStream;
  68. this.multiFileUtil = new MultiFileRenderingUtil(SVG_FILE_EXTENSION,
  69. getUserAgent().getOutputFile());
  70. super.startRenderer(this.firstOutputStream);
  71. }
  72. /** {@inheritDoc} */
  73. public void renderPage(PageViewport pageViewport) throws IOException {
  74. log.debug("Rendering page: " + pageViewport.getPageNumberString());
  75. // Get a DOMImplementation
  76. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
  77. // Create an instance of org.w3c.dom.Document
  78. this.document = domImpl.createDocument(null, "svg", null);
  79. // Create an SVGGeneratorContext to customize SVG generation
  80. SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(this.document);
  81. ctx.setComment("Generated by " + userAgent.getProducer() + " with Batik SVG Generator");
  82. ctx.setEmbeddedFontsOn(true);
  83. // Create an instance of the SVG Generator
  84. this.svgGenerator = new SVGGraphics2D(ctx, true);
  85. Rectangle2D viewArea = pageViewport.getViewArea();
  86. Dimension dim = new Dimension();
  87. dim.setSize(viewArea.getWidth() / 1000, viewArea.getHeight() / 1000);
  88. this.svgGenerator.setSVGCanvasSize(dim);
  89. AffineTransform at = this.svgGenerator.getTransform();
  90. this.state = new Java2DGraphicsState(this.svgGenerator, this.fontInfo, at);
  91. try {
  92. //super.renderPage(pageViewport);
  93. renderPageAreas(pageViewport.getPage());
  94. } finally {
  95. this.state = null;
  96. }
  97. writeSVGFile(pageViewport.getPageIndex());
  98. this.svgGenerator = null;
  99. this.document = null;
  100. }
  101. /** {@inheritDoc} */
  102. public void stopRenderer() throws IOException {
  103. super.stopRenderer();
  104. // Cleaning
  105. clearViewportList();
  106. log.debug("SVG generation complete.");
  107. }
  108. private void writeSVGFile(int pageNumber) throws IOException {
  109. log.debug("Writing out SVG file...");
  110. // Finally, stream out SVG to the standard output using UTF-8
  111. // character to byte encoding
  112. boolean useCSS = true; // we want to use CSS style attribute
  113. OutputStream out = getCurrentOutputStream(pageNumber);
  114. if (out == null) {
  115. log.warn("No filename information available."
  116. + " Stopping early after the first page.");
  117. return;
  118. }
  119. try {
  120. Writer writer = new java.io.OutputStreamWriter(out, "UTF-8");
  121. this.svgGenerator.stream(writer, useCSS);
  122. } finally {
  123. if (out != this.firstOutputStream) {
  124. IOUtils.closeQuietly(out);
  125. } else {
  126. out.flush();
  127. }
  128. }
  129. }
  130. /**
  131. * Returns the OutputStream corresponding to this page
  132. * @param pageNumber 0-based page number
  133. * @return the corresponding OutputStream
  134. * @throws IOException In case of an I/O error
  135. */
  136. protected OutputStream getCurrentOutputStream(int pageNumber) throws IOException {
  137. if (pageNumber == 0) {
  138. return firstOutputStream;
  139. } else {
  140. return multiFileUtil.createOutputStream(pageNumber);
  141. }
  142. }
  143. }