mfrac.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-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 Implements the CommonMfrac wrapper mixin for the MmlMfrac object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  23. import {CommonMo} from './mo.js';
  24. import {BBox} from '../../../util/BBox.js';
  25. import {DIRECTION} from '../FontData.js';
  26. /*****************************************************************/
  27. /**
  28. * The CommonMfrac interface
  29. */
  30. export interface CommonMfrac extends AnyWrapper {
  31. /**
  32. * @param {BBox} bbox The buonding box to modify
  33. * @param {boolean} display True for display-mode fractions
  34. * @param {number} t The thickness of the line
  35. */
  36. getFractionBBox(bbox: BBox, display: boolean, t: number): void;
  37. /**
  38. * @param {boolean} display True for display-mode fractions
  39. * @param {number} t The thickness of the line
  40. * @return {Object} The expanded rule thickness (T), and baseline offsets
  41. * for numerator and denomunator (u and v)
  42. */
  43. getTUV(display: boolean, t: number): {T: number, u: number, v: number};
  44. /**
  45. * @param {BBox} bbox The bounding box to modify
  46. * @param {boolean} display True for display-mode fractions
  47. */
  48. getAtopBBox(bbox: BBox, display: boolean): void;
  49. /**
  50. * @param {boolean} display True for diplay-mode fractions
  51. * @return {Object}
  52. * The vertical offsets of the numerator (u), the denominator (v),
  53. * the separation between the two, and the bboxes themselves.
  54. */
  55. getUVQ(display: boolean): {u: number, v: number, q: number, nbox: BBox, dbox: BBox};
  56. /**
  57. * @param {BBox} bbox The boundng box to modify
  58. * @param {boolean} display True for display-mode fractions
  59. */
  60. getBevelledBBox(bbox: BBox, display: boolean): void;
  61. /**
  62. * @param {boolean} display True for display-style fractions
  63. * @return {Object} The height (H) of the bevel, horizontal offest (delta)
  64. * vertical offsets (u and v) of the parts, and
  65. * bounding boxes of the parts.
  66. */
  67. getBevelData(display: boolean): {H: number, delta: number, u: number, v: number, nbox: BBox, dbox: BBox};
  68. /**
  69. * @return {boolean} True if in display mode, false otherwise
  70. */
  71. isDisplay(): boolean;
  72. }
  73. /**
  74. * Shorthand for the CommonMfrac constructor
  75. */
  76. export type MfracConstructor = Constructor<CommonMfrac>;
  77. /*****************************************************************/
  78. /**
  79. * The CommonMfrac wrapper mixin for the MmlMfrac object
  80. *
  81. * @template T The Wrapper class constructor type
  82. */
  83. export function CommonMfracMixin<T extends WrapperConstructor>(Base: T): MfracConstructor & T {
  84. return class extends Base {
  85. /**
  86. * Wrapper for <mo> to use for bevelled fraction
  87. */
  88. public bevel: CommonMo = null;
  89. /**
  90. * Padding around fractions
  91. */
  92. public pad: number;
  93. /************************************************/
  94. /**
  95. * @override
  96. * @constructor
  97. */
  98. constructor(...args: any[]) {
  99. super(...args);
  100. this.pad = (this.node.getProperty('withDelims') as boolean ? 0 : this.font.params.nulldelimiterspace);
  101. //
  102. // create internal bevel mo element
  103. //
  104. if (this.node.attributes.get('bevelled')) {
  105. const {H} = this.getBevelData(this.isDisplay());
  106. const bevel = this.bevel = this.createMo('/') as CommonMo;
  107. bevel.node.attributes.set('symmetric', true);
  108. bevel.canStretch(DIRECTION.Vertical);
  109. bevel.getStretchedVariant([H], true);
  110. }
  111. }
  112. /**
  113. * @override
  114. */
  115. public computeBBox(bbox: BBox, recompute: boolean = false) {
  116. bbox.empty();
  117. const {linethickness, bevelled} = this.node.attributes.getList('linethickness', 'bevelled');
  118. const display = this.isDisplay();
  119. let w = null as (number | null);
  120. if (bevelled) {
  121. this.getBevelledBBox(bbox, display);
  122. } else {
  123. const thickness = this.length2em(String(linethickness), .06);
  124. w = -2 * this.pad;
  125. if (thickness === 0) {
  126. this.getAtopBBox(bbox, display);
  127. } else {
  128. this.getFractionBBox(bbox, display, thickness);
  129. w -= .2;
  130. }
  131. w += bbox.w;
  132. }
  133. bbox.clean();
  134. this.setChildPWidths(recompute, w);
  135. }
  136. /************************************************/
  137. /**
  138. * @param {BBox} bbox The buonding box to modify
  139. * @param {boolean} display True for display-mode fractions
  140. * @param {number} t The thickness of the line
  141. */
  142. public getFractionBBox(bbox: BBox, display: boolean, t: number) {
  143. const nbox = this.childNodes[0].getOuterBBox();
  144. const dbox = this.childNodes[1].getOuterBBox();
  145. const tex = this.font.params;
  146. const a = tex.axis_height;
  147. const {T, u, v} = this.getTUV(display, t);
  148. bbox.combine(nbox, 0, a + T + Math.max(nbox.d * nbox.rscale, u));
  149. bbox.combine(dbox, 0, a - T - Math.max(dbox.h * dbox.rscale, v));
  150. bbox.w += 2 * this.pad + .2;
  151. }
  152. /**
  153. * @param {boolean} display True for display-mode fractions
  154. * @param {number} t The thickness of the line
  155. * @return {Object} The expanded rule thickness (T), and baseline offsets
  156. * for numerator and denomunator (u and v)
  157. */
  158. public getTUV(display: boolean, t: number): {T: number, u: number, v: number} {
  159. const tex = this.font.params;
  160. const a = tex.axis_height;
  161. const T = (display ? 3.5 : 1.5) * t;
  162. return {T: (display ? 3.5 : 1.5) * t,
  163. u: (display ? tex.num1 : tex.num2) - a - T,
  164. v: (display ? tex.denom1 : tex.denom2) + a - T};
  165. }
  166. /************************************************/
  167. /**
  168. * @param {BBox} bbox The bounding box to modify
  169. * @param {boolean} display True for display-mode fractions
  170. */
  171. public getAtopBBox(bbox: BBox, display: boolean) {
  172. const {u, v, nbox, dbox} = this.getUVQ(display);
  173. bbox.combine(nbox, 0, u);
  174. bbox.combine(dbox, 0, -v);
  175. bbox.w += 2 * this.pad;
  176. }
  177. /**
  178. * @param {boolean} display True for diplay-mode fractions
  179. * @return {Object}
  180. * The vertical offsets of the numerator (u), the denominator (v),
  181. * the separation between the two, and the bboxes themselves.
  182. */
  183. public getUVQ(display: boolean): {u: number, v: number, q: number, nbox: BBox, dbox: BBox} {
  184. const nbox = this.childNodes[0].getOuterBBox();
  185. const dbox = this.childNodes[1].getOuterBBox();
  186. const tex = this.font.params;
  187. //
  188. // Initial offsets (u, v)
  189. // Minimum separation (p)
  190. // Actual separation with initial positions (q)
  191. //
  192. let [u, v] = (display ? [tex.num1, tex.denom1] : [tex.num3, tex.denom2]);
  193. let p = (display ? 7 : 3) * tex.rule_thickness;
  194. let q = (u - nbox.d * nbox.scale) - (dbox.h * dbox.scale - v);
  195. //
  196. // If actual separation is less than minimum, move them farther apart
  197. //
  198. if (q < p) {
  199. u += (p - q) / 2;
  200. v += (p - q) / 2;
  201. q = p;
  202. }
  203. return {u, v, q, nbox, dbox};
  204. }
  205. /************************************************/
  206. /**
  207. * @param {BBox} bbox The boundng box to modify
  208. * @param {boolean} display True for display-mode fractions
  209. */
  210. public getBevelledBBox(bbox: BBox, display: boolean) {
  211. const {u, v, delta, nbox, dbox} = this.getBevelData(display);
  212. const lbox = this.bevel.getOuterBBox();
  213. bbox.combine(nbox, 0, u);
  214. bbox.combine(lbox, bbox.w - delta / 2, 0);
  215. bbox.combine(dbox, bbox.w - delta / 2, v);
  216. }
  217. /**
  218. * @param {boolean} display True for display-style fractions
  219. * @return {Object} The height (H) of the bevel, horizontal offest (delta)
  220. * vertical offsets (u and v) of the parts, and
  221. * bounding boxes of the parts.
  222. */
  223. public getBevelData(display: boolean): {
  224. H: number, delta: number, u: number, v: number, nbox: BBox, dbox: BBox
  225. } {
  226. const nbox = this.childNodes[0].getOuterBBox();
  227. const dbox = this.childNodes[1].getOuterBBox();
  228. const delta = (display ? .4 : .15);
  229. const H = Math.max(nbox.scale * (nbox.h + nbox.d), dbox.scale * (dbox.h + dbox.d)) + 2 * delta;
  230. const a = this.font.params.axis_height;
  231. const u = nbox.scale * (nbox.d - nbox.h) / 2 + a + delta;
  232. const v = dbox.scale * (dbox.d - dbox.h) / 2 + a - delta;
  233. return {H, delta, u, v, nbox, dbox};
  234. }
  235. /************************************************/
  236. /**
  237. * @override
  238. */
  239. public canStretch(_direction: DIRECTION) {
  240. return false;
  241. }
  242. /**
  243. * @return {boolean} True if in display mode, false otherwise
  244. */
  245. public isDisplay(): boolean {
  246. const {displaystyle, scriptlevel} = this.node.attributes.getList('displaystyle', 'scriptlevel');
  247. return displaystyle && scriptlevel === 0;
  248. }
  249. /**
  250. * @override
  251. */
  252. public getWrapWidth(i: number) {
  253. const attributes = this.node.attributes;
  254. if (attributes.get('bevelled')) {
  255. return this.childNodes[i].getOuterBBox().w;
  256. }
  257. const w = this.getBBox().w;
  258. const thickness = this.length2em(attributes.get('linethickness'));
  259. return w - (thickness ? .2 : 0) - 2 * this.pad;
  260. }
  261. /**
  262. * @override
  263. */
  264. public getChildAlign(i: number) {
  265. const attributes = this.node.attributes;
  266. return (attributes.get('bevelled') ? 'left' : attributes.get(['numalign', 'denomalign'][i]) as string);
  267. }
  268. };
  269. }