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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.mathml;
  18. import java.awt.Color;
  19. import java.awt.Dimension;
  20. import java.awt.geom.Point2D;
  21. import org.apache.fop.fo.FONode;
  22. import org.apache.fop.apps.FOPException;
  23. import org.w3c.dom.DOMImplementation;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import org.w3c.dom.Node;
  27. import org.xml.sax.Attributes;
  28. import org.apache.batik.svggen.SVGGraphics2D;
  29. import org.apache.batik.dom.svg.SVGDOMImplementation;
  30. import net.sourceforge.jeuclid.MathBase;
  31. import net.sourceforge.jeuclid.DOMMathBuilder;
  32. /**
  33. * Defines the top-level element for MathML.
  34. */
  35. public class MathMLElement extends MathMLObj {
  36. private Document svgDoc = null;
  37. private float width;
  38. private float height;
  39. private boolean converted = false;
  40. /**
  41. * @see org.apache.fop.fo.FONode#FONode(FONode)
  42. */
  43. public MathMLElement(FONode parent) {
  44. super(parent);
  45. }
  46. /**
  47. * @see org.apache.fop.fo.FONode#handleAttrs(Attributes)
  48. */
  49. public void handleAttrs(Attributes attlist) throws FOPException {
  50. super.handleAttrs(attlist);
  51. createBasicDocument();
  52. }
  53. /**
  54. * Converts the MathML to SVG.
  55. */
  56. public void convertToSVG() {
  57. try {
  58. if (!converted) {
  59. converted = true;
  60. String fontname = "Helvetica";
  61. int fontstyle = 0;
  62. int inlinefontstyle = 0;
  63. int displayfontsize = 12;
  64. int inlinefontsize = 12;
  65. MathBase base = new MathBase(
  66. (new DOMMathBuilder(doc)).getMathRootElement(),
  67. fontname, fontstyle, inlinefontsize,
  68. displayfontsize);
  69. base.setDebug(false);
  70. svgDoc = createSVG(base);
  71. width = base.getWidth();
  72. height = base.getHeight();
  73. doc = svgDoc;
  74. }
  75. } catch (Throwable t) {
  76. getLogger().error("Could not convert MathML to SVG", t);
  77. width = 0;
  78. height = 0;
  79. }
  80. }
  81. /**
  82. * Create the SVG from MathML.
  83. * @return the DOM document containing SVG
  84. */
  85. public static Document createSVG(MathBase base) {
  86. DOMImplementation impl =
  87. SVGDOMImplementation.getDOMImplementation();
  88. String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  89. Document svgdocument = impl.createDocument(svgNS, "svg", null);
  90. SVGGraphics2D g = new SVGGraphics2D(svgdocument);
  91. g.setSVGCanvasSize(
  92. new Dimension(base.getWidth(), base.getHeight()));
  93. //g.setColor(Color.white);
  94. //g.fillRect(0, 0, base.getWidth(), base.getHeight());
  95. g.setColor(Color.black);
  96. base.paint(g);
  97. //if (antialiasing)
  98. //element.setAttribute("text-rendering", "optimizeLegibility");
  99. //else
  100. //element.setAttribute("text-rendering", "geometricPrecision");
  101. // this should be done in a better way
  102. Element root = g.getRoot();
  103. svgdocument = impl.createDocument(svgNS, "svg", null);
  104. Node node = svgdocument.importNode(root, true);
  105. ((org.apache.batik.dom.svg.SVGOMDocument) svgdocument).
  106. getRootElement().appendChild(node);
  107. return svgdocument;
  108. }
  109. /**
  110. * @see org.apache.fop.fo.XMLObj#getDocument()
  111. */
  112. public Document getDocument() {
  113. convertToSVG();
  114. return doc;
  115. }
  116. /**
  117. * @see org.apache.fop.fo.XMLObj#getDocumentNamespace()
  118. */
  119. public String getDocumentNamespace() {
  120. if (svgDoc == null) {
  121. return MathMLElementMapping.NAMESPACE;
  122. }
  123. return "http://www.w3.org/2000/svg";
  124. }
  125. /**
  126. * @see org.apache.fop.fo.XMLObj#getDimension(Point2D)
  127. */
  128. public Point2D getDimension(Point2D view) {
  129. convertToSVG();
  130. return new Point2D.Float(width, height);
  131. }
  132. }