mtr.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 CcommonMtr wrapper mixin for the MmlMtr object
  19. * and CommonMlabeledtr wrapper mixin for MmlMlabeledtr
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  24. import {CommonMo} from './mo.js';
  25. import {BBox} from '../../../util/BBox.js';
  26. import {DIRECTION} from '../FontData.js';
  27. /*****************************************************************/
  28. /**
  29. * The CommonMtr interface
  30. *
  31. * @template C The class for table cells
  32. */
  33. export interface CommonMtr<C extends AnyWrapper> extends AnyWrapper {
  34. /**
  35. * The number of mtd's in the mtr
  36. */
  37. readonly numCells: number;
  38. /**
  39. * True if this is a labeled row
  40. */
  41. readonly labeled: boolean;
  42. /**
  43. * The child nodes that are part of the table (no label node)
  44. */
  45. readonly tableCells: C[];
  46. /**
  47. * @override;
  48. */
  49. childNodes: C[];
  50. /**
  51. * @param {number} i The index of the child to get (skipping labels)
  52. * @return {C} The ith child node wrapper
  53. */
  54. getChild(i: number): C;
  55. /**
  56. * @return {BBox[]} An array of the bounding boxes for the mtd's in the row
  57. */
  58. getChildBBoxes(): BBox[];
  59. /**
  60. * Handle vertical stretching of cells to match height of
  61. * other cells in the row.
  62. *
  63. * @param {number[]=} HD The total height and depth for the row [H, D]
  64. *
  65. * If this isn't specified, the maximum height and depth is computed.
  66. */
  67. stretchChildren(HD?: number[]): void;
  68. }
  69. /**
  70. * Shorthand for the CommonMtr constructor
  71. *
  72. * @template C The class for table cells
  73. */
  74. export type MtrConstructor<C extends AnyWrapper> = Constructor<CommonMtr<C>>;
  75. /*****************************************************************/
  76. /**
  77. * The CommonMtr wrapper for the MmlMtr object
  78. *
  79. * @template C The class for table cells
  80. * @template T The Wrapper class constructor type
  81. */
  82. export function CommonMtrMixin<
  83. C extends AnyWrapper,
  84. T extends WrapperConstructor
  85. >(Base: T): MtrConstructor<C> & T {
  86. return class extends Base {
  87. /**
  88. * @override
  89. */
  90. get fixesPWidth() {
  91. return false;
  92. }
  93. /**
  94. * @return {number} The number of mtd's in the mtr
  95. */
  96. get numCells(): number {
  97. return this.childNodes.length;
  98. }
  99. /**
  100. * @return {boolean} True if this is a labeled row
  101. */
  102. get labeled(): boolean {
  103. return false;
  104. }
  105. /**
  106. * @return {C[]} The child nodes that are part of the table (no label node)
  107. */
  108. get tableCells(): C[] {
  109. return this.childNodes;
  110. }
  111. /**
  112. * @param {number} i The index of the child to get (skipping labels)
  113. * @return {C} The ith child node wrapper
  114. */
  115. public getChild(i: number): C {
  116. return this.childNodes[i];
  117. }
  118. /**
  119. * @return {BBox[]} An array of the bounding boxes for the mtd's in the row
  120. */
  121. public getChildBBoxes(): BBox[] {
  122. return this.childNodes.map(cell => cell.getBBox());
  123. }
  124. /**
  125. * Handle vertical stretching of cells to match height of
  126. * other cells in the row.
  127. *
  128. * @param {number[]} HD The total height and depth for the row [H, D]
  129. *
  130. * If this isn't specified, the maximum height and depth is computed.
  131. */
  132. public stretchChildren(HD: number[] = null) {
  133. let stretchy: AnyWrapper[] = [];
  134. let children = (this.labeled ? this.childNodes.slice(1) : this.childNodes);
  135. //
  136. // Locate and count the stretchy children
  137. //
  138. for (const mtd of children) {
  139. const child = mtd.childNodes[0];
  140. if (child.canStretch(DIRECTION.Vertical)) {
  141. stretchy.push(child);
  142. }
  143. }
  144. let count = stretchy.length;
  145. let nodeCount = this.childNodes.length;
  146. if (count && nodeCount > 1) {
  147. if (HD === null) {
  148. let H = 0, D = 0;
  149. //
  150. // If all the children are stretchy, find the largest one,
  151. // otherwise, find the height and depth of the non-stretchy
  152. // children.
  153. //
  154. let all = (count > 1 && count === nodeCount);
  155. for (const mtd of children) {
  156. const child = mtd.childNodes[0];
  157. const noStretch = (child.stretch.dir === DIRECTION.None);
  158. if (all || noStretch) {
  159. const {h, d} = child.getBBox(noStretch);
  160. if (h > H) {
  161. H = h;
  162. }
  163. if (d > D) {
  164. D = d;
  165. }
  166. }
  167. }
  168. HD = [H, D];
  169. }
  170. //
  171. // Stretch the stretchable children
  172. //
  173. for (const child of stretchy) {
  174. (child.coreMO() as CommonMo).getStretchedVariant(HD);
  175. }
  176. }
  177. }
  178. };
  179. }
  180. /*****************************************************************/
  181. /**
  182. * The CommonMlabeledtr interface
  183. *
  184. * @template C The class for table cells
  185. */
  186. export interface CommonMlabeledtr<C extends AnyWrapper> extends CommonMtr<C> {
  187. }
  188. /**
  189. * Shorthand for the CommonMlabeledtr constructor
  190. *
  191. * @template C The class for table cells
  192. */
  193. export type MlabeledtrConstructor<C extends AnyWrapper> = Constructor<CommonMlabeledtr<C>>;
  194. /*****************************************************************/
  195. /**
  196. * The CommonMlabeledtr wrapper mixin for the MmlMlabeledtr object
  197. *
  198. * @template C The class for table cells
  199. * @template T The Wrapper class constructor type
  200. */
  201. export function CommonMlabeledtrMixin<
  202. C extends AnyWrapper,
  203. T extends MtrConstructor<C>
  204. >(Base: T): MlabeledtrConstructor<C> & T {
  205. return class extends Base {
  206. /**
  207. * @override
  208. */
  209. get numCells() {
  210. //
  211. // Don't include the label mtd
  212. //
  213. return Math.max(0, this.childNodes.length - 1);
  214. }
  215. /**
  216. * @override
  217. */
  218. get labeled() {
  219. return true;
  220. }
  221. /**
  222. * @override
  223. */
  224. get tableCells() {
  225. return this.childNodes.slice(1) as C[];
  226. }
  227. /**
  228. * @override
  229. */
  230. public getChild(i: number) {
  231. return this.childNodes[i + 1] as C;
  232. }
  233. /**
  234. * @override
  235. */
  236. public getChildBBoxes() {
  237. //
  238. // Don't include the label mtd
  239. //
  240. return this.childNodes.slice(1).map(cell => cell.getBBox());
  241. }
  242. };
  243. }