mglyph.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/CommonHTML/autoload/mglyph.js
  6. *
  7. * Implements the CommonHTML output for <mglyph> elements.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2015-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("CommonHTML Jax Ready",function () {
  26. var VERSION = "2.7.2";
  27. var MML = MathJax.ElementJax.mml,
  28. CHTML = MathJax.OutputJax.CommonHTML,
  29. LOCALE = MathJax.Localization;
  30. MML.mglyph.Augment({
  31. toCommonHTML: function (node,options) {
  32. var values = this.getValues("src","width","height","valign","alt");
  33. node = this.CHTMLcreateNode(node);
  34. this.CHTMLhandleStyle(node);
  35. this.CHTMLhandleScale(node);
  36. if (values.src === "") {
  37. var index = this.Get("index");
  38. this.CHTMLgetVariant();
  39. if (index && this.CHTMLvariant.style)
  40. this.CHTMLhandleText(node,String.fromCharCode(index),this.CHTMLvariant);
  41. } else {
  42. var bbox = this.CHTML;
  43. if (!bbox.img) bbox.img = MML.mglyph.GLYPH[values.src];
  44. if (!bbox.img) {
  45. bbox.img = MML.mglyph.GLYPH[values.src] = {img: new Image(), status: "pending"};
  46. bbox.img.img.onload = MathJax.Callback(["CHTMLimgLoaded",this]);
  47. bbox.img.img.onerror = MathJax.Callback(["CHTMLimgError",this]);
  48. bbox.img.img.src = values.src;
  49. MathJax.Hub.RestartAfter(bbox.img.img.onload);
  50. }
  51. if (bbox.img.status !== "OK") {
  52. var err = MML.Error(LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src));
  53. err.data[0].data[0].mathsize = "75%";
  54. this.Append(err); err.toCommonHTML(node); this.data.pop();
  55. bbox.combine(err.CHTML,0,0,1);
  56. } else {
  57. var img = CHTML.addElement(node,"img",{
  58. isMathJax:true, src:values.src, alt:values.alt, title:values.alt
  59. });
  60. var w = values.width, h = values.height;
  61. var W = bbox.img.img.width/CHTML.em, H = bbox.img.img.height/CHTML.em;
  62. var WW = W, HH = H;
  63. if (w !== "") {W = this.CHTMLlength2em(w,WW); H = (WW ? W/WW * HH : 0)}
  64. if (h !== "") {H = this.CHTMLlength2em(h,HH); if (w === "") W = (HH ? H/HH * WW : 0)}
  65. img.style.width = CHTML.Em(W); bbox.w = bbox.r = W;
  66. img.style.height = CHTML.Em(H); bbox.h = bbox.t = H;
  67. if (values.valign) {
  68. bbox.d = bbox.b = -this.CHTMLlength2em(values.valign,HH);
  69. img.style.verticalAlign = CHTML.Em(-bbox.d);
  70. bbox.h -= bbox.d; bbox.t = bbox.h;
  71. }
  72. }
  73. }
  74. this.CHTMLhandleSpace(node);
  75. this.CHTMLhandleBBox(node);
  76. this.CHTMLhandleColor(node);
  77. return node;
  78. },
  79. CHTMLimgLoaded: function (event,status) {
  80. if (typeof(event) === "string") status = event;
  81. this.CHTML.img.status = (status || "OK");
  82. },
  83. CHTMLimgError: function () {this.CHTML.img.img.onload("error")}
  84. },{
  85. GLYPH: {} // global list of all loaded glyphs
  86. });
  87. MathJax.Hub.Startup.signal.Post("CommonHTML mglyph Ready");
  88. MathJax.Ajax.loadComplete(CHTML.autoloadDir+"/mglyph.js");
  89. });