mmultiscripts.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/mmultiscripts.js
  6. *
  7. * Implements the HTML-CSS output for <mmultiscripts> 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. MML.mmultiscripts.Augment({
  30. toHTML: function (span,HW,D) {
  31. span = this.HTMLcreateSpan(span); var scale = this.HTMLgetScale();
  32. var stack = HTMLCSS.createStack(span), values;
  33. var base = HTMLCSS.createBox(stack);
  34. if (this.data[this.base]) {
  35. var child = this.data[this.base].toHTML(base);
  36. if (D != null) {this.data[this.base].HTMLstretchV(base,HW,D)}
  37. else if (HW != null) {this.data[this.base].HTMLstretchH(base,HW)}
  38. HTMLCSS.Measured(child,base);
  39. } else {base.bbox = this.HTMLzeroBBox()}
  40. var x_height = HTMLCSS.TeX.x_height * scale,
  41. s = HTMLCSS.TeX.scriptspace * scale * .75; // FIXME: .75 can be removed when IC is right?
  42. var BOX = this.HTMLgetScripts(stack,s);
  43. var sub = BOX[0], sup = BOX[1], presub = BOX[2], presup = BOX[3];
  44. // <mmultiscripts> children other than the base can be <none/>,
  45. // <mprescripts/>, <mrow></mrow> etc so try to get HTMLgetScale from the
  46. // first element with a spanID. See issue 362.
  47. var sscale = this.HTMLgetScale();
  48. for (var i = 1; i < this.data.length; i++) {
  49. if (this.data[i] && this.data[i].spanID) {
  50. sscale = this.data[i].HTMLgetScale();
  51. break;
  52. }
  53. }
  54. var q = HTMLCSS.TeX.sup_drop * sscale, r = HTMLCSS.TeX.sub_drop * sscale;
  55. var u = base.bbox.h - q, v = base.bbox.d + r, delta = 0, p;
  56. if (base.bbox.ic) {delta = base.bbox.ic}
  57. if (this.data[this.base] &&
  58. (this.data[this.base].type === "mi" || this.data[this.base].type === "mo")) {
  59. if (this.data[this.base].data.join("").length === 1 && base.bbox.scale === 1 &&
  60. !this.data[this.base].Get("largeop")) {u = v = 0}
  61. }
  62. var min = this.getValues("subscriptshift","superscriptshift"), mu = this.HTMLgetMu(span);
  63. min.subscriptshift = (min.subscriptshift === "" ? 0 : HTMLCSS.length2em(min.subscriptshift,mu));
  64. min.superscriptshift = (min.superscriptshift === "" ? 0 : HTMLCSS.length2em(min.superscriptshift,mu));
  65. var dx = 0;
  66. if (presub) {dx = presub.bbox.w+delta} else if (presup) {dx = presup.bbox.w-delta}
  67. if (dx < 0) {dx = 0};
  68. HTMLCSS.placeBox(base,dx,0);
  69. if (!sup && !presup) {
  70. v = Math.max(v,HTMLCSS.TeX.sub1*scale,min.subscriptshift);
  71. if (sub) {v = Math.max(v,sub.bbox.h-(4/5)*x_height)}
  72. if (presub) {v = Math.max(v,presub.bbox.h-(4/5)*x_height)}
  73. if (sub) {HTMLCSS.placeBox(sub,dx+base.bbox.w+s-delta,-v)}
  74. if (presub) {HTMLCSS.placeBox(presub,0,-v)}
  75. } else {
  76. if (!sub && !presub) {
  77. values = this.getValues("displaystyle","texprimestyle");
  78. p = HTMLCSS.TeX[(values.displaystyle ? "sup1" : (values.texprimestyle ? "sup3" : "sup2"))];
  79. u = Math.max(u,p*scale,min.superscriptshift);
  80. if (sup) {u = Math.max(u,sup.bbox.d+(1/4)*x_height)}
  81. if (presup) {u = Math.max(u,presup.bbox.d+(1/4)*x_height)}
  82. if (sup) {HTMLCSS.placeBox(sup,dx+base.bbox.w+s,u)}
  83. if (presup) {HTMLCSS.placeBox(presup,0,u)}
  84. } else {
  85. v = Math.max(v,HTMLCSS.TeX.sub2*scale);
  86. var t = HTMLCSS.TeX.rule_thickness * scale;
  87. var h = (sub||presub).bbox.h, d = (sup||presup).bbox.d;
  88. if (presub) {h = Math.max(h,presub.bbox.h)}
  89. if (presup) {d = Math.max(d,presup.bbox.d)}
  90. if ((u - d) - (h - v) < 3*t) {
  91. v = 3*t - u + d + h; q = (4/5)*x_height - (u - d);
  92. if (q > 0) {u += q; v -= q}
  93. }
  94. u = Math.max(u,min.superscriptshift); v = Math.max(v,min.subscriptshift);
  95. if (sup) {HTMLCSS.placeBox(sup,dx+base.bbox.w+s,u)}
  96. if (presup) {HTMLCSS.placeBox(presup,dx+delta-presup.bbox.w,u)}
  97. if (sub) {HTMLCSS.placeBox(sub,dx+base.bbox.w+s-delta,-v)}
  98. if (presub) {HTMLCSS.placeBox(presub,dx-presub.bbox.w,-v)}
  99. }
  100. }
  101. this.HTMLhandleSpace(span);
  102. this.HTMLhandleColor(span);
  103. return span;
  104. },
  105. HTMLgetScripts: function (stack,s) {
  106. var sup, sub, BOX = [];
  107. var i = 1, m = this.data.length, W = 0;
  108. for (var k = 0; k < 4; k += 2) {
  109. while (i < m && this.data[i].type !== "mprescripts") {
  110. for (var j = k; j < k+2; j++) {
  111. if (this.data[i] && this.data[i].type !== "none") {
  112. if (!BOX[j]) {
  113. BOX[j] = HTMLCSS.createBox(stack); BOX[j].bbox = this.HTMLemptyBBox({});
  114. if (W) {HTMLCSS.createBlank(BOX[j],W); BOX[j].bbox.w = BOX[j].bbox.rw = W}
  115. }
  116. this.data[i].toHTML(BOX[j]); this.HTMLcombineBBoxes(this.data[i],BOX[j].bbox);
  117. }
  118. i++;
  119. }
  120. sub = BOX[k]; sup = BOX[k+1];
  121. if (sub && sup) {
  122. if (sub.bbox.w < sup.bbox.w) {
  123. HTMLCSS.createBlank(sub,sup.bbox.w-sub.bbox.w);
  124. sub.bbox.w = sup.bbox.w; sub.bbox.rw = Math.max(sub.bbox.w,sub.bbox.rw);
  125. } else if (sub.bbox.w > sup.bbox.w) {
  126. HTMLCSS.createBlank(sup,sub.bbox.w-sup.bbox.w);
  127. sup.bbox.w = sub.bbox.w; sup.bbox.rw = Math.max(sup.bbox.w,sup.bbox.rw);
  128. }
  129. }
  130. if (sub) {W = sub.bbox.w} else if (sup) {W = sup.bbox.w}
  131. }
  132. i++; W = 0;
  133. }
  134. for (j = 0; j < 4; j++) {
  135. if (BOX[j]) {
  136. BOX[j].bbox.w += s;
  137. BOX[j].bbox.rw = Math.max(BOX[j].bbox.w,BOX[j].bbox.rw);
  138. this.HTMLcleanBBox(BOX[j].bbox);
  139. }
  140. }
  141. return BOX;
  142. },
  143. HTMLstretchH: MML.mbase.HTMLstretchH,
  144. HTMLstretchV: MML.mbase.HTMLstretchV
  145. });
  146. MathJax.Hub.Startup.signal.Post("HTML-CSS mmultiscripts Ready");
  147. MathJax.Ajax.loadComplete(HTMLCSS.autoloadDir+"/mmultiscripts.js");
  148. });