BboxConfiguration.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Configuration file for the bbox package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {Configuration} from '../Configuration.js';
  23. import TexParser from '../TexParser.js';
  24. import {CommandMap} from '../SymbolMap.js';
  25. import {ParseMethod} from '../Types.js';
  26. import TexError from '../TexError.js';
  27. // Namespace
  28. export let BboxMethods: Record<string, ParseMethod> = {};
  29. /**
  30. * Implements MathJax Bbox macro to pad and colorize background boxes.
  31. * @param {TexParser} parser The current tex parser.
  32. * @param {string} name The name of the calling macro.
  33. */
  34. BboxMethods.BBox = function(parser: TexParser, name: string) {
  35. const bbox = parser.GetBrackets(name, '');
  36. let math = parser.ParseArg(name);
  37. const parts = bbox.split(/,/);
  38. let def, background, style;
  39. for (let i = 0, m = parts.length; i < m; i++) {
  40. const part = parts[i].trim();
  41. const match = part.match(/^(\.\d+|\d+(\.\d*)?)(pt|em|ex|mu|px|in|cm|mm)$/);
  42. if (match) {
  43. // @test Bbox-Padding
  44. if (def) {
  45. // @test Bbox-Padding-Error
  46. throw new TexError('MultipleBBoxProperty', '%1 specified twice in %2', 'Padding', name);
  47. }
  48. const pad = BBoxPadding(match[1] + match[3]);
  49. if (pad) {
  50. // @test Bbox-Padding
  51. def = {
  52. height: '+' + pad,
  53. depth: '+' + pad,
  54. lspace: pad,
  55. width: '+' + (2 * parseInt(match[1], 10)) + match[3]
  56. };
  57. }
  58. } else if (part.match(/^([a-z0-9]+|\#[0-9a-f]{6}|\#[0-9a-f]{3})$/i)) {
  59. // @test Bbox-Background
  60. if (background) {
  61. // @test Bbox-Background-Error
  62. throw new TexError('MultipleBBoxProperty', '%1 specified twice in %2',
  63. 'Background', name);
  64. }
  65. background = part;
  66. } else if (part.match(/^[-a-z]+:/i)) {
  67. // @test Bbox-Frame
  68. if (style) {
  69. // @test Bbox-Frame-Error
  70. throw new TexError('MultipleBBoxProperty', '%1 specified twice in %2',
  71. 'Style', name);
  72. }
  73. style = BBoxStyle(part);
  74. } else if (part !== '') {
  75. // @test Bbox-General-Error
  76. throw new TexError(
  77. 'InvalidBBoxProperty',
  78. '"%1" doesn\'t look like a color, a padding dimension, or a style',
  79. part);
  80. }
  81. }
  82. if (def) {
  83. // @test Bbox-Padding
  84. math = parser.create('node', 'mpadded', [math], def);
  85. }
  86. if (background || style) {
  87. def = {};
  88. if (background) {
  89. // @test Bbox-Background
  90. Object.assign(def, {mathbackground: background});
  91. }
  92. if (style) {
  93. // @test Bbox-Frame
  94. Object.assign(def, {style: style});
  95. }
  96. math = parser.create('node', 'mstyle', [math], def);
  97. }
  98. parser.Push(math);
  99. };
  100. // Dummy methods. Need to be made Safe with security check.
  101. let BBoxStyle = function(styles: string) {
  102. return styles;
  103. };
  104. let BBoxPadding = function(pad: string) {
  105. return pad;
  106. };
  107. new CommandMap('bbox', {bbox: 'BBox'}, BboxMethods);
  108. export const BboxConfiguration = Configuration.create(
  109. 'bbox', {handler: {macro: ['bbox']}}
  110. );