mglyph.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/HTML-CSS/autoload/mglyph.js
  6. *
  7. * Implements the HTML-CSS output for <mglyph> elements.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2010-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("HTML-CSS Jax Ready",function () {
  26. var VERSION = "2.7.2";
  27. var MML = MathJax.ElementJax.mml,
  28. HTMLCSS = MathJax.OutputJax["HTML-CSS"],
  29. LOCALE = MathJax.Localization;
  30. MML.mglyph.Augment({
  31. toHTML: function (span,variant) {
  32. var SPAN = span, values = this.getValues("src","width","height","valign","alt"), err;
  33. span = this.HTMLcreateSpan(span);
  34. if (values.src === "") {
  35. var index = this.Get("index");
  36. if (index) {
  37. variant = this.HTMLgetVariant(); var font = variant.defaultFont;
  38. if (font) {
  39. font.noStyleChar = true; font.testString = String.fromCharCode(index) + 'ABCabc';
  40. if (HTMLCSS.Font.testFont(font)) {
  41. this.HTMLhandleVariant(span,variant,String.fromCharCode(index));
  42. } else {
  43. if (values.alt === "")
  44. {values.alt = LOCALE._(["MathML","BadMglyphFont"],"Bad font: %1",font.family)}
  45. err = MML.Error(values.alt,{mathsize:"75%"});
  46. this.Append(err); err.toHTML(span); this.data.pop();
  47. span.bbox = err.HTMLspanElement().bbox;
  48. }
  49. }
  50. }
  51. } else {
  52. if (!this.img) {this.img = MML.mglyph.GLYPH[values.src]}
  53. if (!this.img) {
  54. this.img = MML.mglyph.GLYPH[values.src] = {img: new Image(), status: "pending"};
  55. var img = this.img.img;
  56. img.onload = MathJax.Callback(["HTMLimgLoaded",this]);
  57. img.onerror = MathJax.Callback(["HTMLimgError",this]);
  58. img.src = values.src;
  59. MathJax.Hub.RestartAfter(img.onload);
  60. }
  61. if (this.img.status !== "OK") {
  62. err = MML.Error(
  63. LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src),
  64. {mathsize:"75%"});
  65. this.Append(err); err.toHTML(span); this.data.pop();
  66. span.bbox = err.HTMLspanElement().bbox;
  67. } else {
  68. var mu = this.HTMLgetMu(span);
  69. img = HTMLCSS.addElement(span,"img",{isMathJax:true, src:values.src, alt:values.alt, title:values.alt});
  70. if (values.width) {
  71. img.style.width = HTMLCSS.Em(HTMLCSS.length2em(values.width,mu,this.img.img.width/HTMLCSS.em));
  72. }
  73. if (values.height) {
  74. img.style.height = HTMLCSS.Em(HTMLCSS.length2em(values.height,mu,this.img.img.height/HTMLCSS.em));
  75. }
  76. span.bbox.w = span.bbox.rw = img.offsetWidth/HTMLCSS.em;
  77. span.bbox.h = img.offsetHeight/HTMLCSS.em;
  78. if (values.valign) {
  79. span.bbox.d = -HTMLCSS.length2em(values.valign,mu,this.img.img.height/HTMLCSS.em);
  80. img.style.verticalAlign = HTMLCSS.Em(-span.bbox.d);
  81. span.bbox.h -= span.bbox.d;
  82. }
  83. }
  84. }
  85. if (!SPAN.bbox) {
  86. SPAN.bbox = {w: span.bbox.w, h: span.bbox.h, d: span.bbox.d,
  87. rw: span.bbox.rw, lw: span.bbox.lw};
  88. } else if (span.bbox) {
  89. SPAN.bbox.w += span.bbox.w;
  90. if (SPAN.bbox.w > SPAN.bbox.rw) {SPAN.bbox.rw = SPAN.bbox.w}
  91. if (span.bbox.h > SPAN.bbox.h) {SPAN.bbox.h = span.bbox.h}
  92. if (span.bbox.d > SPAN.bbox.d) {SPAN.bbox.d = span.bbox.d}
  93. }
  94. this.HTMLhandleSpace(span);
  95. this.HTMLhandleColor(span);
  96. return span;
  97. },
  98. HTMLimgLoaded: function (event,status) {
  99. if (typeof(event) === "string") {status = event}
  100. this.img.status = (status || "OK")
  101. },
  102. HTMLimgError: function () {this.img.img.onload("error")}
  103. },{
  104. GLYPH: {} // global list of all loaded glyphs
  105. });
  106. MathJax.Hub.Startup.signal.Post("HTML-CSS mglyph Ready");
  107. MathJax.Ajax.loadComplete(HTMLCSS.autoloadDir+"/mglyph.js");
  108. });