mmultiscripts.js 5.0 KB

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