You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreloaderMathML.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.mathml;
  19. import java.awt.Dimension;
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerException;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.sax.SAXResult;
  29. import javax.xml.transform.stream.StreamSource;
  30. import net.sourceforge.jeuclid.MathBase;
  31. import net.sourceforge.jeuclid.SAXMathBuilder;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. import org.apache.xmlgraphics.image.loader.Image;
  35. import org.apache.xmlgraphics.image.loader.ImageContext;
  36. import org.apache.xmlgraphics.image.loader.ImageInfo;
  37. import org.apache.xmlgraphics.image.loader.ImageSize;
  38. import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader;
  39. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  40. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  41. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  42. import org.apache.fop.util.UnclosableInputStream;
  43. /**
  44. * Image preloader for MathML images.
  45. */
  46. public class PreloaderMathML extends AbstractImagePreloader {
  47. /** Logger instance */
  48. private static Log log = LogFactory.getLog(PreloaderMathML.class);
  49. private boolean jeuclidAvailable = true;
  50. /** {@inheritDoc} */
  51. public ImageInfo preloadImage(String uri, Source src, ImageContext context)
  52. throws IOException {
  53. if (!ImageUtil.hasInputStream(src)) {
  54. //TODO Remove this and support DOMSource and possibly SAXSource
  55. return null;
  56. }
  57. ImageInfo info = null;
  58. if (jeuclidAvailable) {
  59. try {
  60. Loader loader = new Loader();
  61. info = loader.getImage(uri, src, context);
  62. } catch (NoClassDefFoundError e) {
  63. jeuclidAvailable = false;
  64. log.warn("JEuclid not in class path", e);
  65. return null;
  66. }
  67. }
  68. if (info != null) {
  69. ImageUtil.closeQuietly(src); //Image is fully read
  70. }
  71. return info;
  72. }
  73. /**
  74. * This method is put in another class so that the class loader does not
  75. * attempt to load JEuclid related classes when constructing the MathMLPreloader
  76. * class.
  77. */
  78. class Loader {
  79. private ImageInfo getImage(String uri, Source src, ImageContext context) {
  80. InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src));
  81. try {
  82. int length = in.available();
  83. in.mark(length + 1);
  84. TransformerFactory tFactory = TransformerFactory.newInstance();
  85. Transformer transformer = tFactory.newTransformer();
  86. Source source = new StreamSource(in);
  87. SAXMathBuilder mathBuilder = new SAXMathBuilder();
  88. SAXResult res = new SAXResult(mathBuilder);
  89. transformer.transform(source, res);
  90. String fontname = "Helvetica";
  91. int fontstyle = 0;
  92. int displayfontsize = 12;
  93. int inlinefontsize = 12;
  94. if (mathBuilder.getMathRootElement() == null) {
  95. //not a MathML document
  96. try {
  97. in.reset();
  98. } catch (IOException ioe) {
  99. log.error("Error while resetting ImageInputStream", ioe);
  100. }
  101. return null;
  102. }
  103. final MathBase base = new MathBase(
  104. mathBuilder.getMathRootElement(),
  105. fontname, fontstyle, inlinefontsize,
  106. displayfontsize);
  107. ImageInfo info = new ImageInfo(uri, "text/mathml");
  108. final ImageSize size = new ImageSize();
  109. size.setSizeInMillipoints(
  110. Math.round(base.getWidth() * 1000),
  111. Math.round(base.getHeight() * 1000));
  112. //Set the resolution to that of the FOUserAgent
  113. size.setResolution(context.getSourceResolution());
  114. size.calcPixelsFromSize();
  115. info.setSize(size);
  116. Graphics2DImagePainter painter = new Graphics2DImagePainter() {
  117. public Dimension getImageSize() {
  118. return size.getDimensionMpt();
  119. }
  120. public void paint(Graphics2D g2d, Rectangle2D area) {
  121. base.paint(g2d);
  122. }
  123. };
  124. //The whole image had to be loaded for this, so keep it
  125. Image image = new ImageGraphics2D(info, painter);
  126. info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, image);
  127. return info;
  128. } catch (NoClassDefFoundError ncdfe) {
  129. try {
  130. in.reset();
  131. } catch (IOException ioe) {
  132. // we're more interested in the original exception
  133. }
  134. jeuclidAvailable = false;
  135. log.warn("JEuclid not in class path", ncdfe);
  136. return null;
  137. } catch (IOException e) {
  138. // If the MathML is invalid then it throws an IOException
  139. // so there is no way of knowing if it is an svg document
  140. log.debug("Error while trying to load stream as an MathML file: "
  141. + e.getMessage());
  142. // assuming any exception means this document is not svg
  143. // or could not be loaded for some reason
  144. try {
  145. in.reset();
  146. } catch (IOException ioe) {
  147. // we're more interested in the original exception
  148. }
  149. return null;
  150. } catch (TransformerException e) {
  151. try {
  152. in.reset();
  153. } catch (IOException ioe) {
  154. // we're more interested in the original exception
  155. }
  156. log.debug("Error while trying to parsing a MathML file: "
  157. + e.getMessage());
  158. return null;
  159. }
  160. }
  161. }
  162. }