*/
class Loader {
private ImageInfo getImage(String uri, Source src, ImageContext context) {
InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src));
try {
int length = in.available();
in.mark(length + 1);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
Source source = new StreamSource(in);
SAXMathBuilder mathBuilder = new SAXMathBuilder();
SAXResult res = new SAXResult(mathBuilder);
transformer.transform(source, res);
String fontname = "Helvetica";
int fontstyle = 0;
int displayfontsize = 12;
int inlinefontsize = 12;
if (mathBuilder.getMathRootElement() == null) {
//not a MathML document
try {
in.reset();
} catch (IOException ioe) {
log.error("Error while resetting ImageInputStream", ioe);
}
return null;
}
final MathBase base = new MathBase(
mathBuilder.getMathRootElement(),
fontname, fontstyle, inlinefontsize,
displayfontsize);
ImageInfo info = new ImageInfo(uri, "text/mathml");
final ImageSize size = new ImageSize();
size.setSizeInMillipoints(
Math.round(base.getWidth() * 1000),
Math.round(base.getHeight() * 1000));
//Set the resolution to that of the FOUserAgent
size.setResolution(context.getSourceResolution());
size.calcPixelsFromSize();
info.setSize(size);
Graphics2DImagePainter painter = new Graphics2DImagePainter() {
public Dimension getImageSize() {
return size.getDimensionMpt();
}
public void paint(Graphics2D g2d, Rectangle2D area) {
base.paint(g2d);
}
};
//The whole image had to be loaded for this, so keep it
Image image = new ImageGraphics2D(info, painter);
info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, image);
return info;
} catch (NoClassDefFoundError ncdfe) {
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
jeuclidAvailable = false;
log.warn("JEuclid not in class path", ncdfe);
return null;
} catch (IOException e) {
// If the MathML is invalid then it throws an IOException
// so there is no way of knowing if it is an svg document
log.debug("Error while trying to load stream as an MathML file: "
+ e.getMessage());
// assuming any exception means this document is not svg
// or could not be loaded for some reason
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
return null;
} catch (TransformerException e) {
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
log.debug("Error while trying to parsing a MathML file: "
+ e.getMessage());