mmultiscripts.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/SVG/autoload/mmultiscripts.js
  6. *
  7. * Implements the SVG output for <mmultiscripts> elements.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2011-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("SVG Jax Ready",function () {
  26. var VERSION = "2.7.2";
  27. var MML = MathJax.ElementJax.mml,
  28. SVG = MathJax.OutputJax.SVG;
  29. MML.mmultiscripts.Augment({
  30. toSVG: function (HW,D) {
  31. this.SVGgetStyles();
  32. var svg = this.SVG(), scale = this.SVGgetScale(svg); this.SVGhandleSpace(svg);
  33. var base = (this.data[this.base] ? this.SVGdataStretched(this.base,HW,D) : SVG.BBOX.G().Clean());
  34. var x_height = SVG.TeX.x_height * scale,
  35. s = SVG.TeX.scriptspace * scale * .75; // FIXME: .75 can be removed when IC is right?
  36. var BOX = this.SVGgetScripts(s);
  37. var sub = BOX[0], sup = BOX[1], presub = BOX[2], presup = BOX[3];
  38. var sscale = (this.data[1]||this).SVGgetScale();
  39. var q = SVG.TeX.sup_drop * sscale, r = SVG.TeX.sub_drop * sscale;
  40. var u = base.h - q, v = base.d + r, delta = 0, p;
  41. if (base.ic) {delta = base.ic}
  42. if (this.data[this.base] &&
  43. (this.data[this.base].type === "mi" || this.data[this.base].type === "mo")) {
  44. if (this.data[this.base].data.join("").length === 1 && base.scale === 1 &&
  45. !base.stretched && !this.data[this.base].Get("largeop")) {u = v = 0}
  46. }
  47. var min = this.getValues("subscriptshift","superscriptshift"), mu = this.SVGgetMu(svg);
  48. min.subscriptshift = (min.subscriptshift === "" ? 0 : SVG.length2em(min.subscriptshift,mu));
  49. min.superscriptshift = (min.superscriptshift === "" ? 0 : SVG.length2em(min.superscriptshift,mu));
  50. var dx = 0;
  51. if (presub) {dx = presub.w+delta} else if (presup) {dx = presup.w-delta}
  52. svg.Add(base,Math.max(0,dx),0);
  53. if (!sup && !presup) {
  54. v = Math.max(v,SVG.TeX.sub1*scale,min.subscriptshift);
  55. if (sub) {v = Math.max(v,sub.h-(4/5)*x_height)}
  56. if (presub) {v = Math.max(v,presub.h-(4/5)*x_height)}
  57. if (sub) {svg.Add(sub,dx+base.w+s-delta,-v)}
  58. if (presub) {svg.Add(presub,0,-v)}
  59. } else {
  60. if (!sub && !presub) {
  61. var values = this.getValues("displaystyle","texprimestyle");
  62. p = SVG.TeX[(values.displaystyle ? "sup1" : (values.texprimestyle ? "sup3" : "sup2"))];
  63. u = Math.max(u,p*scale,min.superscriptshift);
  64. if (sup) {u = Math.max(u,sup.d+(1/4)*x_height)}
  65. if (presup) {u = Math.max(u,presup.d+(1/4)*x_height)}
  66. if (sup) {svg.Add(sup,dx+base.w+s,u)}
  67. if (presup) {svg.Add(presup,0,u)}
  68. } else {
  69. v = Math.max(v,SVG.TeX.sub2*scale);
  70. var t = SVG.TeX.rule_thickness * scale;
  71. var h = (sub||presub).h, d = (sup||presup).d;
  72. if (presub) {h = Math.max(h,presub.h)}
  73. if (presup) {d = Math.max(d,presup.d)}
  74. if ((u - d) - (h - v) < 3*t) {
  75. v = 3*t - u + d + h; q = (4/5)*x_height - (u - d);
  76. if (q > 0) {u += q; v -= q}
  77. }
  78. u = Math.max(u,min.superscriptshift); v = Math.max(v,min.subscriptshift);
  79. if (sup) {svg.Add(sup,dx+base.w+s,u)}
  80. if (presup) {svg.Add(presup,dx+delta-presup.w,u)}
  81. if (sub) {svg.Add(sub,dx+base.w+s-delta,-v)}
  82. if (presub) {svg.Add(presub,dx-presub.w,-v)}
  83. }
  84. }
  85. svg.Clean();
  86. this.SVGhandleColor(svg);
  87. this.SVGsaveData(svg);
  88. var data = this.SVGdata;
  89. data.dx = dx; data.s = s; data.u = u, data.v = v; data.delta = delta;
  90. return svg;
  91. },
  92. SVGgetScripts: function (s) {
  93. var sup, sub, BOX = [];
  94. var i = 1, m = this.data.length, W = 0;
  95. for (var k = 0; k < 4; k += 2) {
  96. while (i < m && (this.data[i]||{}).type !== "mprescripts") {
  97. var box = [null,null,null,null];
  98. for (var j = k; j < k+2; j++) {
  99. if (this.data[i] && this.data[i].type !== "none" && this.data[i].type !== "mprescripts") {
  100. if (!BOX[j]) {BOX[j] = SVG.BBOX.G()}
  101. box[j] = this.data[i].toSVG();
  102. }
  103. if ((this.data[i]||{}).type !== "mprescripts") i++;
  104. }
  105. var isPre = (k === 2);
  106. if (isPre) W += Math.max((box[k]||{w:0}).w,(box[k+1]||{w:0}).w);
  107. if (box[k]) BOX[k].Add(box[k].With({x:W-(isPre?box[k].w:0)}));
  108. if (box[k+1]) BOX[k+1].Add(box[k+1].With({x:W-(isPre?box[k+1].w:0)}));
  109. sub = BOX[k]||{w:0}; sup = BOX[k+1]||{w:0};
  110. sub.w = sup.w = W = Math.max(sub.w,sup.w);
  111. }
  112. i++; W = 0;
  113. }
  114. for (j = 0; j < 4; j++) {if (BOX[j]) {BOX[j].w += s; BOX[j].Clean()}}
  115. return BOX;
  116. }
  117. });
  118. MathJax.Hub.Startup.signal.Post("SVG mmultiscripts Ready");
  119. MathJax.Ajax.loadComplete(SVG.autoloadDir+"/mmultiscripts.js");
  120. });