mglyph.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-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("HTML-CSS Jax Ready",function () {
  26. var VERSION = "2.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.merror(values.alt).With({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.merror(
  63. LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src)
  64. ).With({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. if (String(values.width).match(/^\s*-?\d+\s*$/)) {values.width += "px"}
  72. img.style.width = HTMLCSS.Em(HTMLCSS.length2em(values.width,mu,this.img.img.width/HTMLCSS.em));
  73. }
  74. if (values.height) {
  75. if (String(values.height).match(/^\s*-?\d+\s*$/)) {values.height += "px"}
  76. img.style.height = HTMLCSS.Em(HTMLCSS.length2em(values.height,mu,this.img.img.height/HTMLCSS.em));
  77. }
  78. span.bbox.w = span.bbox.rw = img.offsetWidth/HTMLCSS.em;
  79. span.bbox.h = img.offsetHeight/HTMLCSS.em;
  80. if (values.valign) {
  81. if (String(values.valign).match(/^\s*-?\d+\s*$/)) {values.valign += "px"}
  82. span.bbox.d = -HTMLCSS.length2em(values.valign,mu,this.img.img.height/HTMLCSS.em);
  83. img.style.verticalAlign = HTMLCSS.Em(-span.bbox.d);
  84. span.bbox.h -= span.bbox.d;
  85. }
  86. }
  87. }
  88. if (!SPAN.bbox) {
  89. SPAN.bbox = {w: span.bbox.w, h: span.bbox.h, d: span.bbox.d,
  90. rw: span.bbox.rw, lw: span.bbox.lw};
  91. } else if (span.bbox) {
  92. SPAN.bbox.w += span.bbox.w;
  93. if (SPAN.bbox.w > SPAN.bbox.rw) {SPAN.bbox.rw = SPAN.bbox.w}
  94. if (span.bbox.h > SPAN.bbox.h) {SPAN.bbox.h = span.bbox.h}
  95. if (span.bbox.d > SPAN.bbox.d) {SPAN.bbox.d = span.bbox.d}
  96. }
  97. this.HTMLhandleSpace(span);
  98. this.HTMLhandleColor(span);
  99. return span;
  100. },
  101. HTMLimgLoaded: function (event,status) {
  102. if (typeof(event) === "string") {status = event}
  103. this.img.status = (status || "OK")
  104. },
  105. HTMLimgError: function () {this.img.img.onload("error")}
  106. },{
  107. GLYPH: {} // global list of all loaded glyphs
  108. });
  109. MathJax.Hub.Startup.signal.Post("HTML-CSS mglyph Ready");
  110. MathJax.Ajax.loadComplete(HTMLCSS.autoloadDir+"/mglyph.js");
  111. });