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.

MathMLElementMapping.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 1999-2004,2006 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 org.apache.fop.fo.FONode;
  19. import org.apache.fop.fo.ElementMapping;
  20. import org.apache.fop.image.analyser.XMLReader;
  21. import org.apache.fop.image.FopImage;
  22. import org.w3c.dom.DOMImplementation;
  23. import org.w3c.dom.Document;
  24. import java.util.HashMap;
  25. import net.sourceforge.jeuclid.MathBase;
  26. import net.sourceforge.jeuclid.DOMMathBuilder;
  27. /**
  28. * This class provides the element mapping for FOP.
  29. */
  30. public class MathMLElementMapping extends ElementMapping {
  31. /** MathML Namespace */
  32. public static final String NAMESPACE = "http://www.w3.org/1998/Math/MathML";
  33. /** Main constructor. */
  34. public MathMLElementMapping() {
  35. this.namespaceURI = NAMESPACE;
  36. }
  37. /** @see org.apache.fop.fo.ElementMapping#getDOMImplementation() */
  38. public DOMImplementation getDOMImplementation() {
  39. return getDefaultDOMImplementation();
  40. }
  41. /** @see org.apache.fop.fo.ElementMapping#initialize() */
  42. protected void initialize() {
  43. if (foObjs == null) {
  44. foObjs = new HashMap();
  45. foObjs.put("math", new ME());
  46. foObjs.put(DEFAULT, new MathMLMaker());
  47. XMLReader.setConverter(this.namespaceURI, new MathMLConverter());
  48. }
  49. }
  50. static class MathMLMaker extends ElementMapping.Maker {
  51. public FONode make(FONode parent) {
  52. return new MathMLObj(parent);
  53. }
  54. }
  55. static class ME extends ElementMapping.Maker {
  56. public FONode make(FONode parent) {
  57. return new MathMLElement(parent);
  58. }
  59. }
  60. static class MathMLConverter implements XMLReader.Converter {
  61. public FopImage.ImageInfo convert(Document doc) {
  62. try {
  63. FopImage.ImageInfo info = new FopImage.ImageInfo();
  64. String fontname = "Helvetica";
  65. int fontstyle = 0;
  66. int inlinefontstyle = 0;
  67. int inlinefontsize = 12;
  68. int displayfontsize = 12;
  69. MathBase base = new MathBase(
  70. (new DOMMathBuilder(doc)).getMathRootElement(),
  71. fontname, fontstyle, inlinefontsize,
  72. displayfontsize);
  73. base.setDebug(false);
  74. info.data = MathMLElement.createSVG(base);
  75. info.width = base.getWidth();
  76. info.height = base.getHeight();
  77. info.mimeType = "image/svg+xml";
  78. info.str = "http://www.w3.org/2000/svg";
  79. return info;
  80. } catch (Throwable t) {
  81. /**@todo log that properly */
  82. }
  83. return null;
  84. }
  85. }
  86. }