bbox.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/bbox.js
  6. *
  7. * This file implements the \bbox macro, which creates an box that
  8. * can be styled (for background colors, and so on). You can include
  9. * an optional dimension that tells how much extra padding to include
  10. * around the bounding box for the mathematics, or a color specification
  11. * for the background color to use, or both. E.g.,
  12. *
  13. * \bbox[2pt]{x+y} % an invisible box around x+y with 2pt of extra space
  14. * \bbox[green]{x+y} % a green box around x+y
  15. * \bbox[green,2pt]{x+y} % a green box with 2pt of extra space
  16. *
  17. * You can also specify style attributes, for example
  18. *
  19. * \bbox[red,border:3px solid blue,5px]{x+y}
  20. *
  21. * would give a red background with a 3px solid blue border that has 5px
  22. * of padding between the border and the mathematics. Note that not all
  23. * output formats support the style specifications. In particular, the
  24. * NativeMML output depends on the browser to render the attributes, and
  25. * not all MathML renderers will honor them (e.g., MathPlayer2 doesn't
  26. * render border styles).
  27. *
  28. * This file will be loaded automatically when \bbox is first used.
  29. *
  30. * ---------------------------------------------------------------------
  31. *
  32. * Copyright (c) 2011-2017 The MathJax Consortium
  33. *
  34. * Licensed under the Apache License, Version 2.0 (the "License");
  35. * you may not use this file except in compliance with the License.
  36. * You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing, software
  41. * distributed under the License is distributed on an "AS IS" BASIS,
  42. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  43. * See the License for the specific language governing permissions and
  44. * limitations under the License.
  45. */
  46. MathJax.Extension["TeX/bbox"] = {
  47. version: "2.7.2"
  48. };
  49. MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  50. var TEX = MathJax.InputJax.TeX,
  51. MML = MathJax.ElementJax.mml;
  52. TEX.Definitions.Add({macros: {bbox: "BBox"}},null,true);
  53. TEX.Parse.Augment({
  54. BBox: function (name) {
  55. var bbox = this.GetBrackets(name,""),
  56. math = this.ParseArg(name);
  57. var parts = bbox.split(/,/), def, background, style;
  58. for (var i = 0, m = parts.length; i < m; i++) {
  59. var part = parts[i].replace(/^\s+/,'').replace(/\s+$/,'');
  60. var match = part.match(/^(\.\d+|\d+(\.\d*)?)(pt|em|ex|mu|px|in|cm|mm)$/);
  61. if (match) {
  62. if (def)
  63. {TEX.Error(["MultipleBBoxProperty","%1 specified twice in %2","Padding",name])}
  64. var pad = this.BBoxPadding(match[1]+match[3]);
  65. if (pad) def = {height:"+"+pad, depth:"+"+pad, lspace:pad, width:"+"+(2*match[1])+match[3]};
  66. } else if (part.match(/^([a-z0-9]+|\#[0-9a-f]{6}|\#[0-9a-f]{3})$/i)) {
  67. if (background)
  68. {TEX.Error(["MultipleBBoxProperty","%1 specified twice in %2","Background",name])}
  69. background = part;
  70. } else if (part.match(/^[-a-z]+:/i)) {
  71. if (style)
  72. {TEX.Error(["MultipleBBoxProperty","%1 specified twice in %2", "Style",name])}
  73. style = this.BBoxStyle(part);
  74. } else if (part !== "") {
  75. TEX.Error(
  76. ["InvalidBBoxProperty",
  77. "'%1' doesn't look like a color, a padding dimension, or a style",
  78. part]
  79. );
  80. }
  81. }
  82. if (def) {math = MML.mpadded(math).With(def)}
  83. if (background || style) {
  84. math = MML.mstyle(math).With({mathbackground:background, style:style});
  85. }
  86. this.Push(math);
  87. },
  88. BBoxStyle: function (styles) {return styles},
  89. BBoxPadding: function (pad) {return pad}
  90. });
  91. MathJax.Hub.Startup.signal.Post("TeX bbox Ready");
  92. });
  93. MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/bbox.js");