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 throws NoSuchMethodException {
46
47 this.streamConst = ((Class<VectorGraphics>) converterClass)
48 .getConstructor(OutputStream.class, Dimension.class);
49 }
50
51
52 public Dimension convert(final Node doc, final LayoutContext context,
53 final OutputStream outStream) throws IOException {
54 final VectorGraphics tempg = this.createGraphics(
55 new ByteArrayOutputStream(), new Dimension(1, 1));
56 final JEuclidView view = new JEuclidView(doc, context, tempg);
57 final int ascent = (int) Math.ceil(view.getAscentHeight());
58 final Dimension size = new Dimension(
59 (int) Math.ceil(view.getWidth()), (int) Math.ceil(view
60 .getDescentHeight())
61 + ascent);
62
63 final VectorGraphics g = this.createGraphics(outStream, size);
64 g.setCreator("JEuclid (from MathML)");
65 g.startExport();
66 view.draw(g, 0, ascent);
67 g.endExport();
68
69 return size;
70 }
71
72
73 public DocumentWithDimension convert(final Node doc,
74 final LayoutContext context) {
75 return null;
76 }
77
78 private VectorGraphics createGraphics(final OutputStream os,
79 final Dimension d) throws IOException {
80 try {
81 return this.streamConst.newInstance(os, d);
82 } catch (final InvocationTargetException e) {
83 throw new IOException(e.toString());
84 } catch (final IllegalArgumentException e) {
85 throw new IOException(e.toString());
86 } catch (final InstantiationException e) {
87 throw new IOException(e.toString());
88 } catch (final IllegalAccessException e) {
89 throw new IOException(e.toString());
90 }
91 }
92 }