mglyph.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/mglyph.js
  6. *
  7. * Implements the SVG output for <mglyph> elements.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2011-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. BBOX = SVG.BBOX,
  30. LOCALE = MathJax.Localization;
  31. var XLINKNS = "http://www.w3.org/1999/xlink";
  32. BBOX.MGLYPH = BBOX.Subclass({
  33. type: "image", removeable: false,
  34. Init: function (img,w,h,align,mu,def) {
  35. if (def == null) {def = {}}
  36. var W = img.width*1000/SVG.em, H = img.height*1000/SVG.em, y = 0;
  37. if (w !== "") {W = SVG.length2em(w,mu,W)}
  38. if (h !== "") {H = SVG.length2em(h,mu,H)}
  39. if (align !== "" && align.match(/\d/)) {y = SVG.length2em(align,mu); def.y = -y}
  40. def.height = Math.floor(H); def.width = Math.floor(W);
  41. def.transform = "translate(0,"+H+") matrix(1 0 0 -1 0 0)";
  42. def.preserveAspectRatio = "none";
  43. this.SUPER(arguments).Init.call(this,def);
  44. this.element.setAttributeNS(XLINKNS,"href",img.src);
  45. this.w = this.r = W; this.h = this.H = H + y;
  46. this.d = this.D = -y; this.l = 0;
  47. }
  48. });
  49. MML.mglyph.Augment({
  50. toSVG: function (variant,scale) {
  51. this.SVGgetStyles(); var svg = this.SVG(), img, err;
  52. this.SVGhandleSpace(svg);
  53. var values = this.getValues("src","width","height","valign","alt");
  54. if (values.src === "") {
  55. values = this.getValues("index","fontfamily");
  56. if (values.index) {
  57. if (!scale) {scale = this.SVGgetScale()}
  58. var def = {}; if (values.fontfamily) {def["font-family"] = values.fontfamily}
  59. svg.Add(BBOX.TEXT(scale,String.fromCharCode(values.index),def));
  60. }
  61. } else {
  62. if (!this.img) {this.img = MML.mglyph.GLYPH[values.src]}
  63. if (!this.img) {
  64. this.img = MML.mglyph.GLYPH[values.src] = {img: new Image(), status: "pending"};
  65. img = this.img.img;
  66. img.onload = MathJax.Callback(["SVGimgLoaded",this]);
  67. img.onerror = MathJax.Callback(["SVGimgError",this]);
  68. img.src = values.src;
  69. MathJax.Hub.RestartAfter(img.onload);
  70. }
  71. if (this.img.status !== "OK") {
  72. err = MML.merror(
  73. LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src)
  74. ).With({mathsize:"75%"});
  75. this.Append(err); svg = err.toSVG(); this.data.pop();
  76. } else {
  77. var mu = this.SVGgetMu(svg);
  78. svg.Add(BBOX.MGLYPH(this.img.img,values.width,values.height,values.valign,mu,
  79. {src:values.src, alt:values.alt, title:values.alt}));
  80. }
  81. }
  82. svg.Clean();
  83. this.SVGhandleColor(svg);
  84. this.SVGsaveData(svg);
  85. return svg;
  86. },
  87. SVGimgLoaded: function (event,status) {
  88. if (typeof(event) === "string") {status = event}
  89. this.img.status = (status || "OK")
  90. },
  91. SVGimgError: function () {this.img.img.onload("error")}
  92. },{
  93. GLYPH: {} // global list of all loaded glyphs
  94. });
  95. MathJax.Hub.Startup.signal.Post("SVG mglyph Ready");
  96. MathJax.Ajax.loadComplete(SVG.autoloadDir+"/mglyph.js");
  97. });