annotation-xml.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/jax/output/SVG/autoload/annotation-xml.js
  6. *
  7. * Implements the SVG output for <annotation-xml> elements.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2013 The MathJax Consortium
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. MathJax.Hub.Register.StartupHook("SVG Jax Ready",function () {
  26. var VERSION = "2.2";
  27. var MML = MathJax.ElementJax.mml,
  28. SVG = MathJax.OutputJax.SVG;
  29. var BBOX = SVG.BBOX;
  30. BBOX.FOREIGN = BBOX.Subclass({type: "foreignObject", removeable: false});
  31. MML["annotation-xml"].Augment({
  32. toSVG: function () {
  33. var svg = this.SVG(); this.SVGhandleSpace(svg);
  34. var encoding = this.Get("encoding");
  35. for (var i = 0, m = this.data.length; i < m; i++)
  36. {svg.Add(this.data[i].toSVG(encoding),svg.w,0)}
  37. svg.Clean();
  38. this.SVGhandleColor(svg);
  39. this.SVGsaveData(svg);
  40. return svg;
  41. }
  42. });
  43. MML.xml.Augment({
  44. toSVG: function (encoding) {
  45. //
  46. // Get size of xml content
  47. //
  48. var span = SVG.textSVG.parentNode;
  49. SVG.mathDiv.style.width = "auto"; // Firefox returns offsetWidth = 0 without this
  50. span.insertBefore(this.div,SVG.textSVG);
  51. var w = this.div.offsetWidth, h = this.div.offsetHeight;
  52. var strut = MathJax.HTML.addElement(this.div,"span",{
  53. style:{display:"inline-block", overflow:"hidden", height:h+"px",
  54. width:"1px", marginRight:"-1px"}
  55. });
  56. var d = this.div.offsetHeight - h; h -= d;
  57. this.div.removeChild(strut);
  58. span.removeChild(this.div); SVG.mathDiv.style.width = "";
  59. //
  60. // Create foreignObject element for the content
  61. //
  62. var scale = 1000/SVG.em;
  63. var svg = BBOX.FOREIGN({
  64. y:(-h)+"px", width:w+"px", height:(h+d)+"px",
  65. transform:"scale("+scale+") matrix(1 0 0 -1 0 0)"
  66. });
  67. //
  68. // Add the children to the foreignObject
  69. //
  70. for (var i = 0, m = this.data.length; i < m; i++)
  71. {svg.element.appendChild(this.data[i].cloneNode(true))}
  72. //
  73. // Set the scale and finish up
  74. //
  75. svg.w = w*scale; svg.h = h*scale; svg.d = d*scale;
  76. svg.r = svg.w; svg.l = 0;
  77. svg.Clean();
  78. this.SVGsaveData(svg);
  79. return svg;
  80. }
  81. });
  82. MathJax.Hub.Startup.signal.Post("SVG annotation-xml Ready");
  83. MathJax.Ajax.loadComplete(SVG.autoloadDir+"/annotation-xml.js");
  84. });