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.

MathMLElement.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Color;
  20. import java.awt.Dimension;
  21. import java.awt.geom.Point2D;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.fo.FONode;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.w3c.dom.DOMImplementation;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.Element;
  28. import org.w3c.dom.Node;
  29. import org.xml.sax.Attributes;
  30. import org.xml.sax.Locator;
  31. import org.apache.batik.svggen.SVGGraphics2D;
  32. import org.apache.batik.dom.svg.SVGDOMImplementation;
  33. import net.sourceforge.jeuclid.MathBase;
  34. import net.sourceforge.jeuclid.DOMMathBuilder;
  35. /**
  36. * Defines the top-level element for MathML.
  37. */
  38. public class MathMLElement extends MathMLObj {
  39. private Document svgDoc = null;
  40. private float width;
  41. private float height;
  42. private boolean converted = false;
  43. /**
  44. * @see org.apache.fop.fo.FONode#FONode(FONode)
  45. */
  46. public MathMLElement(FONode parent) {
  47. super(parent);
  48. }
  49. /**
  50. * @see org.apache.fop.fo.FONode#processNode
  51. */
  52. public void processNode(String elementName,
  53. Locator locator,
  54. Attributes attlist,
  55. PropertyList propertyList) throws FOPException {
  56. super.processNode(elementName, locator, attlist, propertyList);
  57. createBasicDocument();
  58. }
  59. /**
  60. * Converts the MathML to SVG.
  61. */
  62. public void convertToSVG() {
  63. try {
  64. if (!converted) {
  65. converted = true;
  66. String fontname = "Helvetica";
  67. int fontstyle = 0;
  68. //int inlinefontstyle = 0;
  69. int displayfontsize = 12;
  70. int inlinefontsize = 12;
  71. MathBase base = new MathBase(
  72. (new DOMMathBuilder(doc)).getMathRootElement(),
  73. fontname, fontstyle, inlinefontsize,
  74. displayfontsize);
  75. base.setDebug(false);
  76. svgDoc = createSVG(base);
  77. width = base.getWidth();
  78. height = base.getHeight();
  79. doc = svgDoc;
  80. }
  81. } catch (Throwable t) {
  82. getLogger().error("Could not convert MathML to SVG", t);
  83. width = 0;
  84. height = 0;
  85. }
  86. }
  87. /**
  88. * Create the SVG from MathML.
  89. * @param base the root element
  90. * @return the DOM document containing SVG
  91. */
  92. public static Document createSVG(MathBase base) {
  93. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  94. String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  95. Document svgdocument = impl.createDocument(svgNS, "svg", null);
  96. SVGGraphics2D g = new SVGGraphics2D(svgdocument);
  97. g.setSVGCanvasSize(
  98. new Dimension(base.getWidth(), base.getHeight()));
  99. //g.setColor(Color.white);
  100. //g.fillRect(0, 0, base.getWidth(), base.getHeight());
  101. g.setColor(Color.black);
  102. base.paint(g);
  103. //if (antialiasing)
  104. //element.setAttribute("text-rendering", "optimizeLegibility");
  105. //else
  106. //element.setAttribute("text-rendering", "geometricPrecision");
  107. // this should be done in a better way
  108. Element root = g.getRoot();
  109. svgdocument = impl.createDocument(svgNS, "svg", null);
  110. Node node = svgdocument.importNode(root, true);
  111. ((org.apache.batik.dom.svg.SVGOMDocument) svgdocument).
  112. getRootElement().appendChild(node);
  113. return svgdocument;
  114. }
  115. /** @see org.apache.fop.fo.XMLObj#getDOMDocument() */
  116. public Document getDOMDocument() {
  117. convertToSVG();
  118. return doc;
  119. }
  120. /**
  121. * @see org.apache.fop.fo.FONode#getNamespaceURI()
  122. */
  123. public String getNamespaceURI() {
  124. if (svgDoc == null) {
  125. return MathMLElementMapping.NAMESPACE;
  126. }
  127. return "http://www.w3.org/2000/svg";
  128. }
  129. /**
  130. * @see org.apache.fop.fo.XMLObj#getDimension(Point2D)
  131. */
  132. public Point2D getDimension(Point2D view) {
  133. convertToSVG();
  134. return new Point2D.Float(width, height);
  135. }
  136. }