| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.converter; |
| 20 | |
|
| 21 | |
import java.awt.Dimension; |
| 22 | |
import java.io.ByteArrayOutputStream; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.io.OutputStream; |
| 25 | |
import java.lang.reflect.Constructor; |
| 26 | |
import java.lang.reflect.InvocationTargetException; |
| 27 | |
|
| 28 | |
import net.sourceforge.jeuclid.LayoutContext; |
| 29 | |
import net.sourceforge.jeuclid.layout.JEuclidView; |
| 30 | |
|
| 31 | |
import org.freehep.graphics2d.VectorGraphics; |
| 32 | |
import org.w3c.dom.Node; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class FreeHepConverter implements ConverterPlugin { |
| 40 | |
|
| 41 | |
private final Constructor<VectorGraphics> streamConst; |
| 42 | |
|
| 43 | |
@SuppressWarnings("unchecked") |
| 44 | |
FreeHepConverter(final Class<?> converterClass) |
| 45 | 1254 | throws NoSuchMethodException { |
| 46 | |
|
| 47 | 1254 | this.streamConst = ((Class<VectorGraphics>) converterClass) |
| 48 | |
.getConstructor(OutputStream.class, Dimension.class); |
| 49 | 1254 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
public Dimension convert(final Node doc, final LayoutContext context, |
| 53 | |
final OutputStream outStream) throws IOException { |
| 54 | 209 | final VectorGraphics tempg = this.createGraphics( |
| 55 | |
new ByteArrayOutputStream(), new Dimension(1, 1)); |
| 56 | 209 | final JEuclidView view = new JEuclidView(doc, context, tempg); |
| 57 | 209 | final int ascent = (int) Math.ceil(view.getAscentHeight()); |
| 58 | 209 | final Dimension size = new Dimension( |
| 59 | |
(int) Math.ceil(view.getWidth()), (int) Math.ceil(view |
| 60 | |
.getDescentHeight()) |
| 61 | |
+ ascent); |
| 62 | |
|
| 63 | 209 | final VectorGraphics g = this.createGraphics(outStream, size); |
| 64 | 209 | g.setCreator("JEuclid (from MathML)"); |
| 65 | 209 | g.startExport(); |
| 66 | 209 | view.draw(g, 0, ascent); |
| 67 | 209 | g.endExport(); |
| 68 | |
|
| 69 | 209 | return size; |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
public DocumentWithDimension convert(final Node doc, |
| 74 | |
final LayoutContext context) { |
| 75 | 0 | return null; |
| 76 | |
} |
| 77 | |
|
| 78 | |
private VectorGraphics createGraphics(final OutputStream os, |
| 79 | |
final Dimension d) throws IOException { |
| 80 | |
try { |
| 81 | 418 | return this.streamConst.newInstance(os, d); |
| 82 | 0 | } catch (final InvocationTargetException e) { |
| 83 | 0 | throw new IOException(e.toString()); |
| 84 | 0 | } catch (final IllegalArgumentException e) { |
| 85 | 0 | throw new IOException(e.toString()); |
| 86 | 0 | } catch (final InstantiationException e) { |
| 87 | 0 | throw new IOException(e.toString()); |
| 88 | 0 | } catch (final IllegalAccessException e) { |
| 89 | 0 | throw new IOException(e.toString()); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
} |