001 /*
002 * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: FreeHepConverter.java,v 1d64d806165c 2009/04/09 07:17:07 maxberger $ */
018
019 package net.sourceforge.jeuclid.converter;
020
021 import java.awt.Dimension;
022 import java.io.ByteArrayOutputStream;
023 import java.io.IOException;
024 import java.io.OutputStream;
025 import java.lang.reflect.Constructor;
026 import java.lang.reflect.InvocationTargetException;
027
028 import net.sourceforge.jeuclid.LayoutContext;
029 import net.sourceforge.jeuclid.layout.JEuclidView;
030
031 import org.freehep.graphics2d.VectorGraphics;
032 import org.w3c.dom.Node;
033
034 /**
035 * Converter for output formats supported by FreeHEP.
036 *
037 * @version $Revision: 1d64d806165c $
038 */
039 public class FreeHepConverter implements ConverterPlugin {
040
041 private final Constructor<VectorGraphics> streamConst;
042
043 @SuppressWarnings("unchecked")
044 FreeHepConverter(final Class<?> converterClass)
045 throws NoSuchMethodException {
046
047 this.streamConst = ((Class<VectorGraphics>) converterClass)
048 .getConstructor(OutputStream.class, Dimension.class);
049 }
050
051 /** {@inheritDoc} */
052 public Dimension convert(final Node doc, final LayoutContext context,
053 final OutputStream outStream) throws IOException {
054 final VectorGraphics tempg = this.createGraphics(
055 new ByteArrayOutputStream(), new Dimension(1, 1));
056 final JEuclidView view = new JEuclidView(doc, context, tempg);
057 final int ascent = (int) Math.ceil(view.getAscentHeight());
058 final Dimension size = new Dimension(
059 (int) Math.ceil(view.getWidth()), (int) Math.ceil(view
060 .getDescentHeight())
061 + ascent);
062
063 final VectorGraphics g = this.createGraphics(outStream, size);
064 g.setCreator("JEuclid (from MathML)");
065 g.startExport();
066 view.draw(g, 0, ascent);
067 g.endExport();
068
069 return size;
070 }
071
072 /** {@inheritDoc} */
073 public DocumentWithDimension convert(final Node doc,
074 final LayoutContext context) {
075 return null;
076 }
077
078 private VectorGraphics createGraphics(final OutputStream os,
079 final Dimension d) throws IOException {
080 try {
081 return this.streamConst.newInstance(os, d);
082 } catch (final InvocationTargetException e) {
083 throw new IOException(e.toString());
084 } catch (final IllegalArgumentException e) {
085 throw new IOException(e.toString());
086 } catch (final InstantiationException e) {
087 throw new IOException(e.toString());
088 } catch (final IllegalAccessException e) {
089 throw new IOException(e.toString());
090 }
091 }
092 }