mathchoice.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/extensions/TeX/mathchoice.js
  6. *
  7. * Implements the \mathchoice macro (rarely used)
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2009-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("TeX Jax Ready",function () {
  26. var VERSION = "2.7.2";
  27. var MML = MathJax.ElementJax.mml;
  28. var TEX = MathJax.InputJax.TeX;
  29. var TEXDEF = TEX.Definitions;
  30. TEXDEF.Add({macros: {mathchoice: 'MathChoice'}},null,true);
  31. TEX.Parse.Augment({
  32. MathChoice: function (name) {
  33. var D = this.ParseArg(name),
  34. T = this.ParseArg(name),
  35. S = this.ParseArg(name),
  36. SS = this.ParseArg(name);
  37. this.Push(MML.TeXmathchoice(D,T,S,SS));
  38. }
  39. });
  40. MML.TeXmathchoice = MML.mbase.Subclass({
  41. type: "TeXmathchoice", notParent: true,
  42. choice: function () {
  43. if (this.selection != null) return this.selection;
  44. if (this.choosing) return 2; // prevent infinite loops: see issue #1151
  45. this.choosing = true;
  46. var selection = 0, values = this.getValues("displaystyle","scriptlevel");
  47. if (values.scriptlevel > 0) {selection = Math.min(3,values.scriptlevel+1)}
  48. else {selection = (values.displaystyle ? 0 : 1)}
  49. // only cache the result if we are actually in place in a <math> tag.
  50. var node = this.inherit; while (node && node.type !== "math") node = node.inherit;
  51. if (node) this.selection = selection;
  52. this.choosing = false;
  53. return selection;
  54. },
  55. selected: function () {return this.data[this.choice()]},
  56. setTeXclass: function (prev) {return this.selected().setTeXclass(prev)},
  57. isSpacelike: function () {return this.selected().isSpacelike()},
  58. isEmbellished: function () {return this.selected().isEmbellished()},
  59. Core: function () {return this.selected()},
  60. CoreMO: function () {return this.selected().CoreMO()},
  61. toHTML: function (span) {
  62. span = this.HTMLcreateSpan(span);
  63. span.bbox = this.Core().toHTML(span).bbox;
  64. // Firefox doesn't correctly handle a span with a negatively sized content,
  65. // so move marginLeft to main span (this is a hack to get \iiiint to work).
  66. // FIXME: This is a symptom of a more general problem with Firefox, and
  67. // there probably needs to be a more general solution (e.g., modifying
  68. // HTMLhandleSpace() to get the width and adjust the right margin to
  69. // compensate for negative-width contents)
  70. if (span.firstChild && span.firstChild.style.marginLeft) {
  71. span.style.marginLeft = span.firstChild.style.marginLeft;
  72. span.firstChild.style.marginLeft = "";
  73. }
  74. return span;
  75. },
  76. toSVG: function () {
  77. var svg = this.Core().toSVG();
  78. this.SVGsaveData(svg);
  79. return svg;
  80. },
  81. toCommonHTML: function (node) {
  82. node = this.CHTMLcreateNode(node);
  83. this.CHTMLhandleStyle(node);
  84. this.CHTMLhandleColor(node);
  85. this.CHTMLaddChild(node,this.choice(),{});
  86. return node;
  87. },
  88. toPreviewHTML: function(span) {
  89. span = this.PHTMLcreateSpan(span);
  90. this.PHTMLhandleStyle(span);
  91. this.PHTMLhandleColor(span);
  92. this.PHTMLaddChild(span,this.choice(),{});
  93. return span;
  94. }
  95. });
  96. MathJax.Hub.Startup.signal.Post("TeX mathchoice Ready");
  97. });
  98. MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/mathchoice.js");