mglyph.js 4.0 KB

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