mmultiscripts.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-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. 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. //
  45. // <mmultiscripts> children other than the base can be <none/>,
  46. // <mprescripts/>, <mrow></mrow> etc so try to get HTMLgetScale from the
  47. // first element with a spanID. See issue 362.
  48. //
  49. var sscale = scale;
  50. for (var i = 1; i < this.data.length; i++) {
  51. if (this.data[i] && this.data[i].spanID) {
  52. sscale = this.data[i].HTMLgetScale();
  53. break;
  54. }
  55. }
  56. var q = HTMLCSS.TeX.sup_drop * sscale, r = HTMLCSS.TeX.sub_drop * sscale;
  57. var u = base.bbox.h - q, v = base.bbox.d + r, delta = 0, p;
  58. if (base.bbox.ic) {delta = base.bbox.ic}
  59. if (this.data[this.base] &&
  60. (this.data[this.base].type === "mi" || this.data[this.base].type === "mo")) {
  61. if (this.data[this.base].data.join("").length === 1 && base.bbox.scale === 1 &&
  62. !this.data[this.base].Get("largeop")) {u = v = 0}
  63. }
  64. var min = this.getValues("subscriptshift","superscriptshift"), mu = this.HTMLgetMu(span);
  65. min.subscriptshift = (min.subscriptshift === "" ? 0 : HTMLCSS.length2em(min.subscriptshift,mu));
  66. min.superscriptshift = (min.superscriptshift === "" ? 0 : HTMLCSS.length2em(min.superscriptshift,mu));
  67. var dx = 0;
  68. if (presub) {dx = presub.bbox.w+delta} else if (presup) {dx = presup.bbox.w-delta}
  69. if (dx < 0) {dx = 0};
  70. HTMLCSS.placeBox(base,dx,0);
  71. if (!sup && !presup) {
  72. v = Math.max(v,HTMLCSS.TeX.sub1*scale,min.subscriptshift);
  73. if (sub) {v = Math.max(v,sub.bbox.h-(4/5)*x_height)}
  74. if (presub) {v = Math.max(v,presub.bbox.h-(4/5)*x_height)}
  75. if (sub) {HTMLCSS.placeBox(sub,dx+base.bbox.w+s-delta,-v)}
  76. if (presub) {HTMLCSS.placeBox(presub,0,-v)}
  77. } else {
  78. if (!sub && !presub) {
  79. values = this.getValues("displaystyle","texprimestyle");
  80. p = HTMLCSS.TeX[(values.displaystyle ? "sup1" : (values.texprimestyle ? "sup3" : "sup2"))];
  81. u = Math.max(u,p*scale,min.superscriptshift);
  82. if (sup) {u = Math.max(u,sup.bbox.d+(1/4)*x_height)}
  83. if (presup) {u = Math.max(u,presup.bbox.d+(1/4)*x_height)}
  84. if (sup) {HTMLCSS.placeBox(sup,dx+base.bbox.w+s,u)}
  85. if (presup) {HTMLCSS.placeBox(presup,0,u)}
  86. } else {
  87. v = Math.max(v,HTMLCSS.TeX.sub2*scale);
  88. var t = HTMLCSS.TeX.rule_thickness * scale;
  89. var h = (sub||presub).bbox.h, d = (sup||presup).bbox.d;
  90. if (presub) {h = Math.max(h,presub.bbox.h)}
  91. if (presup) {d = Math.max(d,presup.bbox.d)}
  92. if ((u - d) - (h - v) < 3*t) {
  93. v = 3*t - u + d + h; q = (4/5)*x_height - (u - d);
  94. if (q > 0) {u += q; v -= q}
  95. }
  96. u = Math.max(u,min.superscriptshift); v = Math.max(v,min.subscriptshift);
  97. if (sup) {HTMLCSS.placeBox(sup,dx+base.bbox.w+s,u)}
  98. if (presup) {HTMLCSS.placeBox(presup,dx+delta-presup.bbox.w,u)}
  99. if (sub) {HTMLCSS.placeBox(sub,dx+base.bbox.w+s-delta,-v)}
  100. if (presub) {HTMLCSS.placeBox(presub,dx-presub.bbox.w,-v)}
  101. }
  102. }
  103. this.HTMLhandleSpace(span);
  104. this.HTMLhandleColor(span);
  105. var bbox = span.bbox;
  106. bbox.dx = dx; bbox.s = s; bbox.u = u; bbox.v = v; bbox.delta = delta;
  107. bbox.px = dx+base.bbox.w;
  108. return span;
  109. },
  110. HTMLgetScripts: function (stack,s) {
  111. var sup, sub, BOX = [];
  112. var i = 1, m = this.data.length, W = 0;
  113. for (var k = 0; k < 4; k += 2) {
  114. while (i < m && (this.data[i]||{}).type !== "mprescripts") {
  115. var box = [null,null,null,null];
  116. for (var j = k; j < k+2; j++) {
  117. if (this.data[i] && this.data[i].type !== "none" && this.data[i].type !== "mprescripts") {
  118. if (!BOX[j]) {
  119. BOX[j] = HTMLCSS.createBox(stack); BOX[j].bbox = this.HTMLemptyBBox({});
  120. if (W) {HTMLCSS.createBlank(BOX[j],W); BOX[j].bbox.w = BOX[j].bbox.rw = W}
  121. }
  122. box[j] = this.data[i].toHTML(BOX[j]);
  123. } else {
  124. box[j] = MathJax.HTML.Element("span",{bbox:this.HTMLemptyBBox({})});
  125. }
  126. if ((this.data[i]||{}).type !== "mprescripts") i++;
  127. }
  128. var isPre = (k === 2);
  129. sub = BOX[k]; sup = BOX[k+1];
  130. if (sub && sup) {
  131. var w = box[k+1].bbox.w - box[k].bbox.w;
  132. if (w > 0) {
  133. if (isPre) {
  134. this.HTMLmoveColor(box[k],w,1);
  135. BOX[k].w += w;
  136. } else {
  137. HTMLCSS.createBlank(sub,w);
  138. }
  139. } else if (w < 0) {
  140. if (isPre) {
  141. this.HTMLmoveColor(box[k+1],-w,-1);
  142. BOX[k+1].w += -w;
  143. } else {
  144. HTMLCSS.createBlank(sup,-w);
  145. }
  146. }
  147. this.HTMLcombineBBoxes(box[k],sub.bbox);
  148. this.HTMLcombineBBoxes(box[k+1],sup.bbox);
  149. if (w > 0) {
  150. sub.bbox.w = sup.bbox.w;
  151. sub.bbox.rw = Math.max(sub.bbox.w,sub.bbox.rw);
  152. } else if (w < 0) {
  153. sup.bbox.w = sub.bbox.w;
  154. sup.bbox.rw = Math.max(sup.bbox.w,sup.bbox.rw);
  155. }
  156. } else {
  157. if (sub) this.HTMLcombineBBoxes(box[k],sub.bbox);
  158. if (sup) this.HTMLcombineBBoxes(box[k+1],sup.bbox);
  159. }
  160. if (sub) {W = sub.bbox.w} else if (sup) {W = sup.bbox.w}
  161. }
  162. i++; W = 0;
  163. }
  164. for (j = 0; j < 4; j++) {
  165. if (BOX[j]) {
  166. BOX[j].bbox.w += s;
  167. BOX[j].bbox.rw = Math.max(BOX[j].bbox.w,BOX[j].bbox.rw);
  168. BOX[j].bbox.name = (["sub","sup","presub","presup"])[j];
  169. this.HTMLcleanBBox(BOX[j].bbox);
  170. }
  171. }
  172. return BOX;
  173. },
  174. HTMLmoveColor: function (box,w,sign) {
  175. var W = w/(box.scale||1);
  176. box.style.paddingLeft = HTMLCSS.Em(W);
  177. var color = box.previousSibling;
  178. if (color && (color.id||"").match(/^MathJax-Color-/)) {
  179. color.style.marginLeft = HTMLCSS.Em(W+parseFloat(color.style.marginLeft));
  180. color.style.marginRight = HTMLCSS.Em(sign*(W-parseFloat(color.style.marginRight)));
  181. }
  182. },
  183. HTMLstretchH: MML.mbase.HTMLstretchH,
  184. HTMLstretchV: MML.mbase.HTMLstretchV
  185. });
  186. MathJax.Hub.Startup.signal.Post("HTML-CSS mmultiscripts Ready");
  187. MathJax.Ajax.loadComplete(HTMLCSS.autoloadDir+"/mmultiscripts.js");
  188. });