View Javadoc

1   /*
2    * Copyright 2007 - 2008 JEuclid, http://jeuclid.sf.net
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  
17  /* $Id: BatikConverter.java,v 70597ba9d706 2008/02/18 19:42:05 maxberger $ */
18  
19  package net.sourceforge.jeuclid.converter;
20  
21  import java.awt.Dimension;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerException;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.dom.DOMSource;
29  import javax.xml.transform.stream.StreamResult;
30  
31  import net.sourceforge.jeuclid.LayoutContext;
32  import net.sourceforge.jeuclid.layout.JEuclidView;
33  
34  import org.apache.batik.svggen.SVGGeneratorContext;
35  import org.apache.batik.svggen.SVGGraphics2D;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.w3c.dom.DOMImplementation;
39  import org.w3c.dom.Document;
40  import org.w3c.dom.Node;
41  
42  /**
43   * supports conversion to SVG output through Batik.
44   * 
45   * @version $Revision: 70597ba9d706 $
46   */
47  public class BatikConverter implements ConverterPlugin {
48  
49      /**
50       * Logger for this class
51       */
52      private static final Log LOGGER = LogFactory.getLog(BatikConverter.class);
53  
54      private final DOMImplementation domImplementation;
55  
56      BatikConverter(final DOMImplementation domImpl) {
57          this.domImplementation = domImpl;
58      }
59  
60      /** {@inheritDoc} */
61      public Dimension convert(final Node doc, final LayoutContext context,
62              final OutputStream outStream) throws IOException {
63          final DocumentWithDimension svgDocDim = this.convert(doc, context);
64          if (svgDocDim != null) {
65              try {
66                  final Transformer transformer = TransformerFactory
67                          .newInstance().newTransformer();
68                  final DOMSource source = new DOMSource(svgDocDim
69                          .getDocument());
70                  final StreamResult result = new StreamResult(outStream);
71                  transformer.transform(source, result);
72              } catch (final TransformerException e) {
73                  BatikConverter.LOGGER.warn(e);
74              }
75              return svgDocDim.getDimension();
76          }
77          return null;
78      }
79  
80      /** {@inheritDoc} */
81      public DocumentWithDimension convert(final Node doc,
82              final LayoutContext context) {
83  
84          Document document = null;
85          final String svgNS = "http://www.w3.org/2000/svg";
86          document = this.domImplementation.createDocument(svgNS, "svg", null);
87          if (document != null) {
88              final SVGGeneratorContext svgContext = SVGGeneratorContext
89                      .createDefault(document);
90              svgContext.setComment("Converted from MathML using JEuclid");
91              final SVGGraphics2D svgGenerator = new SVGGraphics2D(svgContext,
92                      true);
93              final JEuclidView view = new JEuclidView(doc, context,
94                      svgGenerator);
95              final int ascent = (int) Math.ceil(view.getAscentHeight());
96              final int descent = (int) Math.ceil(view.getDescentHeight());
97              final int height = ascent + descent;
98              final int width = (int) Math.ceil(view.getWidth());
99              final Dimension size = new Dimension(width, height);
100             svgGenerator.setSVGCanvasSize(size);
101             view.draw(svgGenerator, 0, ascent);
102             document.replaceChild(svgGenerator.getRoot(), document
103                     .getFirstChild());
104             return new DocumentWithDimension(document, new Dimension(width,
105                     height), descent);
106         }
107         return null;
108     }
109 }